Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 1 | //===- PDB.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 10 | #include "PDB.h" |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 11 | #include "Chunks.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 12 | #include "Config.h" |
Peter Collingbourne | 75257bc | 2017-10-20 19:48:26 +0000 | [diff] [blame] | 13 | #include "Driver.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 14 | #include "Error.h" |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" |
| 16 | #include "Symbols.h" |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 17 | #include "Writer.h" |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/CVDebugRecord.h" |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/CodeView/RecordName.h" |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/SymbolSerializer.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
| 28 | #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 34 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 37 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 38 | #include "llvm/DebugInfo/PDB/Native/NativeSession.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 39 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 40 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
Rafael Espindola | a0f30da | 2017-05-02 20:19:42 +0000 | [diff] [blame] | 41 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
Reid Kleckner | d91ca76 | 2017-07-19 17:26:07 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 43 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 44 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 45 | #include "llvm/DebugInfo/PDB/PDB.h" |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 46 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 47 | #include "llvm/Support/BinaryByteStream.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 49 | #include "llvm/Support/FileOutputBuffer.h" |
Zachary Turner | 676386ff | 2017-08-07 20:23:45 +0000 | [diff] [blame] | 50 | #include "llvm/Support/JamCRC.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Path.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ScopedPrinter.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 53 | #include <memory> |
| 54 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 55 | using namespace lld; |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 56 | using namespace lld::coff; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 57 | using namespace llvm; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 58 | using namespace llvm::codeview; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 59 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 60 | using llvm::object::coff_section; |
| 61 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 62 | static ExitOnError ExitOnErr; |
| 63 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 64 | namespace { |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 65 | /// Map from type index and item index in a type server PDB to the |
| 66 | /// corresponding index in the destination PDB. |
| 67 | struct CVIndexMap { |
| 68 | SmallVector<TypeIndex, 0> TPIMap; |
| 69 | SmallVector<TypeIndex, 0> IPIMap; |
| 70 | bool IsTypeServerMap = false; |
| 71 | }; |
| 72 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 73 | class PDBLinker { |
| 74 | public: |
| 75 | PDBLinker(SymbolTable *Symtab) |
| 76 | : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc), |
| 77 | IDTable(Alloc) {} |
| 78 | |
| 79 | /// Emit the basic PDB structure: initial streams, headers, etc. |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 80 | void initialize(const llvm::codeview::DebugInfo &BuildId); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 81 | |
| 82 | /// Link CodeView from each object file in the symbol table into the PDB. |
| 83 | void addObjectsToPDB(); |
| 84 | |
| 85 | /// Link CodeView from a single object file into the PDB. |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 86 | void addObjFile(ObjFile *File); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 87 | |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 88 | /// Produce a mapping from the type and item indices used in the object |
| 89 | /// file to those in the destination PDB. |
| 90 | /// |
| 91 | /// If the object file uses a type server PDB (compiled with /Zi), merge TPI |
| 92 | /// and IPI from the type server PDB and return a map for it. Each unique type |
| 93 | /// server PDB is merged at most once, so this may return an existing index |
| 94 | /// mapping. |
| 95 | /// |
| 96 | /// If the object does not use a type server PDB (compiled with /Z7), we merge |
| 97 | /// all the type and item records from the .debug$S stream and fill in the |
| 98 | /// caller-provided ObjectIndexMap. |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 99 | const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 100 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 101 | const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 102 | TypeServer2Record &TS); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 103 | |
| 104 | /// Add the section map and section contributions to the PDB. |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 105 | void addSections(ArrayRef<OutputSection *> OutputSections, |
| 106 | ArrayRef<uint8_t> SectionTable); |
| 107 | |
| 108 | void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule, |
| 109 | OutputSection *OS, Chunk *C); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 110 | |
| 111 | /// Write the PDB to disk. |
| 112 | void commit(); |
| 113 | |
| 114 | private: |
| 115 | BumpPtrAllocator Alloc; |
| 116 | |
| 117 | SymbolTable *Symtab; |
| 118 | |
| 119 | pdb::PDBFileBuilder Builder; |
| 120 | |
| 121 | /// Type records that will go into the PDB TPI stream. |
| 122 | TypeTableBuilder TypeTable; |
| 123 | |
| 124 | /// Item records that will go into the PDB IPI stream. |
| 125 | TypeTableBuilder IDTable; |
| 126 | |
| 127 | /// PDBs use a single global string table for filenames in the file checksum |
| 128 | /// table. |
| 129 | DebugStringTableSubsection PDBStrTab; |
| 130 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 131 | llvm::SmallString<128> NativePath; |
| 132 | |
| 133 | std::vector<pdb::SecMapEntry> SectionMap; |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 134 | |
| 135 | /// Type index mappings of type server PDBs that we've loaded so far. |
| 136 | std::map<GUID, CVIndexMap> TypeServerIndexMappings; |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 137 | }; |
| 138 | } |
| 139 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 140 | static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, |
| 141 | StringRef Name) { |
| 142 | for (SectionChunk *C : Sections) |
| 143 | if (C->getSectionName() == Name) |
| 144 | return C; |
| 145 | return nullptr; |
| 146 | } |
| 147 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 148 | static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data, |
| 149 | StringRef SecName) { |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 150 | // First 4 bytes are section magic. |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 151 | if (Data.size() < 4) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 152 | fatal(SecName + " too short"); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 153 | if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 154 | fatal(SecName + " has an invalid magic"); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 155 | return Data.slice(4); |
| 156 | } |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 157 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 158 | static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) { |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 159 | if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName)) |
| 160 | return consumeDebugMagic(Sec->getContents(), SecName); |
| 161 | return {}; |
| 162 | } |
| 163 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 164 | static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 165 | TypeTableBuilder &TypeTable) { |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 166 | // Start the TPI or IPI stream header. |
| 167 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 168 | |
Reid Kleckner | d91ca76 | 2017-07-19 17:26:07 +0000 | [diff] [blame] | 169 | // Flatten the in memory type table and hash each type. |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 170 | TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { |
Reid Kleckner | d91ca76 | 2017-07-19 17:26:07 +0000 | [diff] [blame] | 171 | assert(Rec.size() >= sizeof(RecordPrefix)); |
| 172 | const RecordPrefix *P = reinterpret_cast<const RecordPrefix *>(Rec.data()); |
| 173 | CVType Type(static_cast<TypeLeafKind>(unsigned(P->RecordKind)), Rec); |
| 174 | auto Hash = pdb::hashTypeRecord(Type); |
| 175 | if (auto E = Hash.takeError()) |
| 176 | fatal("type hashing error"); |
| 177 | TpiBuilder.addTypeRecord(Rec, *Hash); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 178 | }); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 181 | static Optional<TypeServer2Record> |
| 182 | maybeReadTypeServerRecord(CVTypeArray &Types) { |
| 183 | auto I = Types.begin(); |
| 184 | if (I == Types.end()) |
| 185 | return None; |
| 186 | const CVType &Type = *I; |
| 187 | if (Type.kind() != LF_TYPESERVER2) |
| 188 | return None; |
| 189 | TypeServer2Record TS; |
| 190 | if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS)) |
| 191 | fatal(EC, "error reading type server record"); |
| 192 | return std::move(TS); |
| 193 | } |
| 194 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 195 | const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 196 | CVIndexMap &ObjectIndexMap) { |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 197 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); |
| 198 | if (Data.empty()) |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 199 | return ObjectIndexMap; |
Reid Kleckner | 6597c28 | 2017-07-13 20:12:23 +0000 | [diff] [blame] | 200 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 201 | BinaryByteStream Stream(Data, support::little); |
| 202 | CVTypeArray Types; |
| 203 | BinaryStreamReader Reader(Stream); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 204 | if (auto EC = Reader.readArray(Types, Reader.getLength())) |
| 205 | fatal(EC, "Reader::readArray failed"); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 206 | |
| 207 | // Look through type servers. If we've already seen this type server, don't |
| 208 | // merge any type information. |
| 209 | if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types)) |
| 210 | return maybeMergeTypeServerPDB(File, *TS); |
| 211 | |
| 212 | // This is a /Z7 object. Fill in the temporary, caller-provided |
| 213 | // ObjectIndexMap. |
| 214 | if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable, |
| 215 | ObjectIndexMap.TPIMap, Types)) |
| 216 | fatal(Err, "codeview::mergeTypeAndIdRecords failed"); |
| 217 | return ObjectIndexMap; |
| 218 | } |
| 219 | |
| 220 | static Expected<std::unique_ptr<pdb::NativeSession>> |
| 221 | tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) { |
Peter Collingbourne | 75257bc | 2017-10-20 19:48:26 +0000 | [diff] [blame] | 222 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile( |
| 223 | TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false); |
| 224 | if (!MBOrErr) |
| 225 | return errorCodeToError(MBOrErr.getError()); |
| 226 | |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 227 | std::unique_ptr<pdb::IPDBSession> ThisSession; |
Peter Collingbourne | 75257bc | 2017-10-20 19:48:26 +0000 | [diff] [blame] | 228 | if (auto EC = pdb::NativeSession::createFromPdb( |
| 229 | MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)), |
| 230 | /*RequiresNullTerminator=*/false), |
| 231 | ThisSession)) |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 232 | return std::move(EC); |
| 233 | |
| 234 | std::unique_ptr<pdb::NativeSession> NS( |
| 235 | static_cast<pdb::NativeSession *>(ThisSession.release())); |
| 236 | pdb::PDBFile &File = NS->getPDBFile(); |
| 237 | auto ExpectedInfo = File.getPDBInfoStream(); |
| 238 | // All PDB Files should have an Info stream. |
| 239 | if (!ExpectedInfo) |
| 240 | return ExpectedInfo.takeError(); |
| 241 | |
| 242 | // Just because a file with a matching name was found and it was an actual |
| 243 | // PDB file doesn't mean it matches. For it to match the InfoStream's GUID |
| 244 | // must match the GUID specified in the TypeServer2 record. |
| 245 | if (ExpectedInfo->getGuid() != GuidFromObj) |
| 246 | return make_error<pdb::GenericError>( |
| 247 | pdb::generic_error_code::type_server_not_found, TSPath); |
| 248 | |
| 249 | return std::move(NS); |
| 250 | } |
| 251 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 252 | const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 253 | TypeServer2Record &TS) { |
| 254 | // First, check if we already loaded a PDB with this GUID. Return the type |
| 255 | // index mapping if we have it. |
| 256 | auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()}); |
| 257 | CVIndexMap &IndexMap = Insertion.first->second; |
| 258 | if (!Insertion.second) |
| 259 | return IndexMap; |
| 260 | |
| 261 | // Mark this map as a type server map. |
| 262 | IndexMap.IsTypeServerMap = true; |
| 263 | |
| 264 | // Check for a PDB at: |
| 265 | // 1. The given file path |
| 266 | // 2. Next to the object file or archive file |
| 267 | auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName()); |
| 268 | if (!ExpectedSession) { |
| 269 | consumeError(ExpectedSession.takeError()); |
| 270 | StringRef LocalPath = |
| 271 | !File->ParentName.empty() ? File->ParentName : File->getName(); |
| 272 | SmallString<128> Path = sys::path::parent_path(LocalPath); |
Reid Kleckner | f8522d8 | 2017-07-18 00:33:53 +0000 | [diff] [blame] | 273 | sys::path::append( |
| 274 | Path, sys::path::filename(TS.getName(), sys::path::Style::windows)); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 275 | ExpectedSession = tryToLoadPDB(TS.getGuid(), Path); |
| 276 | } |
| 277 | if (auto E = ExpectedSession.takeError()) |
| 278 | fatal(E, "Type server PDB was not found"); |
| 279 | |
| 280 | // Merge TPI first, because the IPI stream will reference type indices. |
| 281 | auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream(); |
| 282 | if (auto E = ExpectedTpi.takeError()) |
| 283 | fatal(E, "Type server does not have TPI stream"); |
| 284 | if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap, |
| 285 | ExpectedTpi->typeArray())) |
| 286 | fatal(Err, "codeview::mergeTypeRecords failed"); |
| 287 | |
| 288 | // Merge IPI. |
| 289 | auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream(); |
| 290 | if (auto E = ExpectedIpi.takeError()) |
| 291 | fatal(E, "Type server does not have TPI stream"); |
| 292 | if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap, |
| 293 | ExpectedIpi->typeArray())) |
| 294 | fatal(Err, "codeview::mergeIdRecords failed"); |
| 295 | |
| 296 | return IndexMap; |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 299 | static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) { |
| 300 | if (TI.isSimple()) |
| 301 | return true; |
| 302 | if (TI.toArrayIndex() >= TypeIndexMap.size()) |
| 303 | return false; |
| 304 | TI = TypeIndexMap[TI.toArrayIndex()]; |
| 305 | return true; |
| 306 | } |
| 307 | |
Reid Kleckner | 8aa32ff | 2017-10-24 17:02:40 +0000 | [diff] [blame^] | 308 | static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 309 | MutableArrayRef<uint8_t> Contents, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 310 | const CVIndexMap &IndexMap, |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 311 | const TypeTableBuilder &IDTable, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 312 | ArrayRef<TiReference> TypeRefs) { |
| 313 | for (const TiReference &Ref : TypeRefs) { |
| 314 | unsigned ByteSize = Ref.Count * sizeof(TypeIndex); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 315 | if (Contents.size() < Ref.Offset + ByteSize) |
| 316 | fatal("symbol record too short"); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 317 | |
| 318 | // This can be an item index or a type index. Choose the appropriate map. |
| 319 | ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap; |
Reid Kleckner | 8aa32ff | 2017-10-24 17:02:40 +0000 | [diff] [blame^] | 320 | bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef; |
| 321 | if (IsItemIndex && IndexMap.IsTypeServerMap) |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 322 | TypeOrItemMap = IndexMap.IPIMap; |
| 323 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 324 | MutableArrayRef<TypeIndex> TIs( |
| 325 | reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 326 | for (TypeIndex &TI : TIs) { |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 327 | if (!remapTypeIndex(TI, TypeOrItemMap)) { |
Reid Kleckner | 8aa32ff | 2017-10-24 17:02:40 +0000 | [diff] [blame^] | 328 | log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " + |
| 329 | File->getName() + " with bad " + (IsItemIndex ? "item" : "type") + |
| 330 | " index 0x" + utohexstr(TI.getIndex())); |
Reid Kleckner | 5d82f68 | 2017-10-23 22:44:51 +0000 | [diff] [blame] | 331 | TI = TypeIndex(SimpleTypeKind::NotTranslated); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 332 | continue; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 333 | } |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 334 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 335 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 338 | static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) { |
| 339 | const RecordPrefix *Prefix = |
| 340 | reinterpret_cast<const RecordPrefix *>(RecordData.data()); |
| 341 | return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind)); |
| 342 | } |
| 343 | |
| 344 | /// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32 |
| 345 | static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData, |
| 346 | const TypeTableBuilder &IDTable) { |
| 347 | RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data()); |
| 348 | |
| 349 | SymbolKind Kind = symbolKind(RecordData); |
| 350 | |
| 351 | if (Kind == SymbolKind::S_PROC_ID_END) { |
| 352 | Prefix->RecordKind = SymbolKind::S_END; |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | // In an object file, GPROC32_ID has an embedded reference which refers to the |
| 357 | // single object file type index namespace. This has already been translated |
| 358 | // to the PDB file's ID stream index space, but we need to convert this to a |
| 359 | // symbol that refers to the type stream index space. So we remap again from |
| 360 | // ID index space to type index space. |
| 361 | if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) { |
| 362 | SmallVector<TiReference, 1> Refs; |
| 363 | auto Content = RecordData.drop_front(sizeof(RecordPrefix)); |
| 364 | CVSymbol Sym(Kind, RecordData); |
| 365 | discoverTypeIndicesInSymbol(Sym, Refs); |
| 366 | assert(Refs.size() == 1); |
| 367 | assert(Refs.front().Count == 1); |
| 368 | |
| 369 | TypeIndex *TI = |
| 370 | reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset); |
| 371 | // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in |
| 372 | // the IPI stream, whose `FunctionType` member refers to the TPI stream. |
| 373 | // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and |
| 374 | // in both cases we just need the second type index. |
| 375 | if (!TI->isSimple() && !TI->isNoneType()) { |
| 376 | ArrayRef<uint8_t> FuncIdData = IDTable.records()[TI->toArrayIndex()]; |
| 377 | SmallVector<TypeIndex, 2> Indices; |
| 378 | discoverTypeIndices(FuncIdData, Indices); |
| 379 | assert(Indices.size() == 2); |
| 380 | *TI = Indices[1]; |
| 381 | } |
| 382 | |
| 383 | Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32 |
| 384 | : SymbolKind::S_LPROC32; |
| 385 | Prefix->RecordKind = uint16_t(Kind); |
| 386 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. |
| 390 | /// The object file may not be aligned. |
| 391 | static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym, |
| 392 | BumpPtrAllocator &Alloc) { |
| 393 | size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb)); |
| 394 | assert(Size >= 4 && "record too short"); |
| 395 | assert(Size <= MaxRecordLength && "record too long"); |
| 396 | void *Mem = Alloc.Allocate(Size, 4); |
| 397 | |
| 398 | // Copy the symbol record and zero out any padding bytes. |
| 399 | MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size); |
| 400 | memcpy(NewData.data(), Sym.data().data(), Sym.length()); |
| 401 | memset(NewData.data() + Sym.length(), 0, Size - Sym.length()); |
| 402 | |
| 403 | // Update the record prefix length. It should point to the beginning of the |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 404 | // next record. |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 405 | auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 406 | Prefix->RecordLen = Size - 2; |
| 407 | return NewData; |
| 408 | } |
| 409 | |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 410 | /// Return true if this symbol opens a scope. This implies that the symbol has |
| 411 | /// "parent" and "end" fields, which contain the offset of the S_END or |
| 412 | /// S_INLINESITE_END record. |
| 413 | static bool symbolOpensScope(SymbolKind Kind) { |
| 414 | switch (Kind) { |
| 415 | case SymbolKind::S_GPROC32: |
| 416 | case SymbolKind::S_LPROC32: |
| 417 | case SymbolKind::S_LPROC32_ID: |
| 418 | case SymbolKind::S_GPROC32_ID: |
| 419 | case SymbolKind::S_BLOCK32: |
| 420 | case SymbolKind::S_SEPCODE: |
| 421 | case SymbolKind::S_THUNK32: |
| 422 | case SymbolKind::S_INLINESITE: |
| 423 | case SymbolKind::S_INLINESITE2: |
| 424 | return true; |
| 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | static bool symbolEndsScope(SymbolKind Kind) { |
| 432 | switch (Kind) { |
| 433 | case SymbolKind::S_END: |
| 434 | case SymbolKind::S_PROC_ID_END: |
| 435 | case SymbolKind::S_INLINESITE_END: |
| 436 | return true; |
| 437 | default: |
| 438 | break; |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | struct ScopeRecord { |
| 444 | ulittle32_t PtrParent; |
| 445 | ulittle32_t PtrEnd; |
| 446 | }; |
| 447 | |
| 448 | struct SymbolScope { |
| 449 | ScopeRecord *OpeningRecord; |
| 450 | uint32_t ScopeOffset; |
| 451 | }; |
| 452 | |
| 453 | static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack, |
| 454 | uint32_t CurOffset, CVSymbol &Sym) { |
| 455 | assert(symbolOpensScope(Sym.kind())); |
| 456 | SymbolScope S; |
| 457 | S.ScopeOffset = CurOffset; |
| 458 | S.OpeningRecord = const_cast<ScopeRecord *>( |
| 459 | reinterpret_cast<const ScopeRecord *>(Sym.content().data())); |
| 460 | S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset; |
| 461 | Stack.push_back(S); |
| 462 | } |
| 463 | |
| 464 | static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack, |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 465 | uint32_t CurOffset, ObjFile *File) { |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 466 | if (Stack.empty()) { |
| 467 | warn("symbol scopes are not balanced in " + File->getName()); |
| 468 | return; |
| 469 | } |
| 470 | SymbolScope S = Stack.pop_back_val(); |
| 471 | S.OpeningRecord->PtrEnd = CurOffset; |
| 472 | } |
| 473 | |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 474 | static bool symbolGoesInModuleStream(const CVSymbol &Sym) { |
| 475 | switch (Sym.kind()) { |
| 476 | case SymbolKind::S_GDATA32: |
| 477 | case SymbolKind::S_CONSTANT: |
| 478 | case SymbolKind::S_UDT: |
| 479 | // We really should not be seeing S_PROCREF and S_LPROCREF in the first place |
| 480 | // since they are synthesized by the linker in response to S_GPROC32 and |
| 481 | // S_LPROC32, but if we do see them, don't put them in the module stream I |
| 482 | // guess. |
| 483 | case SymbolKind::S_PROCREF: |
| 484 | case SymbolKind::S_LPROCREF: |
| 485 | return false; |
| 486 | // S_GDATA32 does not go in the module stream, but S_LDATA32 does. |
| 487 | case SymbolKind::S_LDATA32: |
| 488 | default: |
| 489 | return true; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) { |
| 494 | switch (Sym.kind()) { |
| 495 | case SymbolKind::S_CONSTANT: |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 496 | case SymbolKind::S_GDATA32: |
| 497 | // S_LDATA32 goes in both the module stream and the globals stream. |
| 498 | case SymbolKind::S_LDATA32: |
| 499 | case SymbolKind::S_GPROC32: |
| 500 | case SymbolKind::S_LPROC32: |
| 501 | // We really should not be seeing S_PROCREF and S_LPROCREF in the first place |
| 502 | // since they are synthesized by the linker in response to S_GPROC32 and |
| 503 | // S_LPROC32, but if we do see them, copy them straight through. |
| 504 | case SymbolKind::S_PROCREF: |
| 505 | case SymbolKind::S_LPROCREF: |
| 506 | return true; |
Zachary Turner | 302dc8b | 2017-08-14 18:44:58 +0000 | [diff] [blame] | 507 | // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the |
| 508 | // globals stream or the modules stream). These have special handling which |
| 509 | // needs more investigation before we can get right, but by putting them all |
| 510 | // into the globals stream WinDbg fails to display local variables of class |
| 511 | // types saying that it cannot find the type Foo *. So as a stopgap just to |
| 512 | // keep things working, we drop them. |
| 513 | case SymbolKind::S_UDT: |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 514 | default: |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File, |
| 520 | const CVSymbol &Sym) { |
| 521 | switch (Sym.kind()) { |
| 522 | case SymbolKind::S_CONSTANT: |
| 523 | case SymbolKind::S_UDT: |
| 524 | case SymbolKind::S_GDATA32: |
| 525 | case SymbolKind::S_LDATA32: |
| 526 | case SymbolKind::S_PROCREF: |
| 527 | case SymbolKind::S_LPROCREF: |
| 528 | Builder.addGlobalSymbol(Sym); |
| 529 | break; |
| 530 | case SymbolKind::S_GPROC32: |
| 531 | case SymbolKind::S_LPROC32: { |
| 532 | SymbolRecordKind K = SymbolRecordKind::ProcRefSym; |
| 533 | if (Sym.kind() == SymbolKind::S_LPROC32) |
| 534 | K = SymbolRecordKind::LocalProcRef; |
| 535 | ProcRefSym PS(K); |
| 536 | PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex()); |
| 537 | // For some reason, MSVC seems to add one to this value. |
| 538 | ++PS.Module; |
| 539 | PS.Name = getSymbolName(Sym); |
| 540 | PS.SumName = 0; |
| 541 | PS.SymOffset = File.ModuleDBI->getNextSymbolOffset(); |
| 542 | Builder.addGlobalSymbol(PS); |
| 543 | break; |
| 544 | } |
| 545 | default: |
| 546 | llvm_unreachable("Invalid symbol kind!"); |
| 547 | } |
| 548 | } |
| 549 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 550 | static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File, |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 551 | pdb::GSIStreamBuilder &GsiBuilder, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 552 | const CVIndexMap &IndexMap, |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 553 | const TypeTableBuilder &IDTable, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 554 | BinaryStreamRef SymData) { |
| 555 | // FIXME: Improve error recovery by warning and skipping records when |
| 556 | // possible. |
| 557 | CVSymbolArray Syms; |
| 558 | BinaryStreamReader Reader(SymData); |
| 559 | ExitOnErr(Reader.readArray(Syms, Reader.getLength())); |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 560 | SmallVector<SymbolScope, 4> Scopes; |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 561 | for (CVSymbol Sym : Syms) { |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 562 | // Discover type index references in the record. Skip it if we don't know |
| 563 | // where they are. |
| 564 | SmallVector<TiReference, 32> TypeRefs; |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 565 | if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) { |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 566 | log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind())); |
| 567 | continue; |
| 568 | } |
| 569 | |
| 570 | // Copy the symbol record so we can mutate it. |
| 571 | MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc); |
| 572 | |
| 573 | // Re-map all the type index references. |
| 574 | MutableArrayRef<uint8_t> Contents = |
| 575 | NewData.drop_front(sizeof(RecordPrefix)); |
Reid Kleckner | 8aa32ff | 2017-10-24 17:02:40 +0000 | [diff] [blame^] | 576 | remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap, IDTable, |
| 577 | TypeRefs); |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 578 | |
| 579 | // An object file may have S_xxx_ID symbols, but these get converted to |
| 580 | // "real" symbols in a PDB. |
| 581 | translateIdSymbols(NewData, IDTable); |
| 582 | |
| 583 | SymbolKind NewKind = symbolKind(NewData); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 584 | |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 585 | // Fill in "Parent" and "End" fields by maintaining a stack of scopes. |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 586 | CVSymbol NewSym(NewKind, NewData); |
| 587 | if (symbolOpensScope(NewKind)) |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 588 | scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym); |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 589 | else if (symbolEndsScope(NewKind)) |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 590 | scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 591 | |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 592 | // Add the symbol to the globals stream if necessary. Do this before adding |
| 593 | // the symbol to the module since we may need to get the next symbol offset, |
| 594 | // and writing to the module's symbol stream will update that offset. |
| 595 | if (symbolGoesInGlobalsStream(NewSym)) |
| 596 | addGlobalSymbol(GsiBuilder, *File, NewSym); |
| 597 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 598 | // Add the symbol to the module. |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 599 | if (symbolGoesInModuleStream(NewSym)) |
| 600 | File->ModuleDBI->addSymbol(NewSym); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 604 | // Allocate memory for a .debug$S section and relocate it. |
| 605 | static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, |
| 606 | SectionChunk *DebugChunk) { |
| 607 | uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize()); |
| 608 | assert(DebugChunk->OutputSectionOff == 0 && |
| 609 | "debug sections should not be in output sections"); |
| 610 | DebugChunk->writeTo(Buffer); |
| 611 | return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()), |
| 612 | ".debug$S"); |
| 613 | } |
| 614 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 615 | void PDBLinker::addObjFile(ObjFile *File) { |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 616 | // Add a module descriptor for every object file. We need to put an absolute |
| 617 | // path to the object into the PDB. If this is a plain object, we make its |
| 618 | // path absolute. If it's an object in an archive, we make the archive path |
| 619 | // absolute. |
| 620 | bool InArchive = !File->ParentName.empty(); |
| 621 | SmallString<128> Path = InArchive ? File->ParentName : File->getName(); |
| 622 | sys::fs::make_absolute(Path); |
| 623 | sys::path::native(Path, sys::path::Style::windows); |
| 624 | StringRef Name = InArchive ? File->getName() : StringRef(Path); |
Zachary Turner | 2897e03 | 2017-05-25 21:16:03 +0000 | [diff] [blame] | 625 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 626 | File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name)); |
| 627 | File->ModuleDBI->setObjFileName(Path); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 628 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 629 | // Before we can process symbol substreams from .debug$S, we need to process |
| 630 | // type information, file checksums, and the string table. Add type info to |
| 631 | // the PDB first, so that we can get the map from object file type and item |
| 632 | // indices to PDB type and item indices. |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 633 | CVIndexMap ObjectIndexMap; |
| 634 | const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap); |
Zachary Turner | 448dea4 | 2017-07-07 18:46:14 +0000 | [diff] [blame] | 635 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 636 | // Now do all live .debug$S sections. |
| 637 | for (SectionChunk *DebugChunk : File->getDebugChunks()) { |
| 638 | if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S") |
| 639 | continue; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 640 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 641 | ArrayRef<uint8_t> RelocatedDebugContents = |
| 642 | relocateDebugChunk(Alloc, DebugChunk); |
| 643 | if (RelocatedDebugContents.empty()) |
| 644 | continue; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 645 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 646 | DebugSubsectionArray Subsections; |
| 647 | BinaryStreamReader Reader(RelocatedDebugContents, support::little); |
| 648 | ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size())); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 649 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 650 | DebugStringTableSubsectionRef CVStrTab; |
| 651 | DebugChecksumsSubsectionRef Checksums; |
| 652 | for (const DebugSubsectionRecord &SS : Subsections) { |
| 653 | switch (SS.kind()) { |
| 654 | case DebugSubsectionKind::StringTable: |
| 655 | ExitOnErr(CVStrTab.initialize(SS.getRecordData())); |
| 656 | break; |
| 657 | case DebugSubsectionKind::FileChecksums: |
| 658 | ExitOnErr(Checksums.initialize(SS.getRecordData())); |
| 659 | break; |
| 660 | case DebugSubsectionKind::Lines: |
| 661 | // We can add the relocated line table directly to the PDB without |
| 662 | // modification because the file checksum offsets will stay the same. |
| 663 | File->ModuleDBI->addDebugSubsection(SS); |
| 664 | break; |
| 665 | case DebugSubsectionKind::Symbols: |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 666 | mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap, |
| 667 | IDTable, SS.getRecordData()); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 668 | break; |
| 669 | default: |
| 670 | // FIXME: Process the rest of the subsections. |
| 671 | break; |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 674 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 675 | if (Checksums.valid()) { |
| 676 | // Make a new file checksum table that refers to offsets in the PDB-wide |
| 677 | // string table. Generally the string table subsection appears after the |
| 678 | // checksum table, so we have to do this after looping over all the |
| 679 | // subsections. |
| 680 | if (!CVStrTab.valid()) |
| 681 | fatal(".debug$S sections must have both a string table subsection " |
| 682 | "and a checksum subsection table or neither"); |
| 683 | auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab); |
| 684 | for (FileChecksumEntry &FC : Checksums) { |
| 685 | StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset)); |
| 686 | ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI, |
| 687 | FileName)); |
| 688 | NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum); |
| 689 | } |
| 690 | File->ModuleDBI->addDebugSubsection(std::move(NewChecksums)); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 695 | static PublicSym32 createPublic(Defined *Def) { |
| 696 | PublicSym32 Pub(SymbolKind::S_PUB32); |
| 697 | Pub.Name = Def->getName(); |
| 698 | if (auto *D = dyn_cast<DefinedCOFF>(Def)) { |
| 699 | if (D->getCOFFSymbol().isFunctionDefinition()) |
| 700 | Pub.Flags = PublicSymFlags::Function; |
| 701 | } else if (isa<DefinedImportThunk>(Def)) { |
| 702 | Pub.Flags = PublicSymFlags::Function; |
| 703 | } |
| 704 | |
| 705 | OutputSection *OS = Def->getChunk()->getOutputSection(); |
| 706 | assert(OS && "all publics should be in final image"); |
| 707 | Pub.Offset = Def->getRVA() - OS->getRVA(); |
| 708 | Pub.Segment = OS->SectionIndex; |
| 709 | return Pub; |
| 710 | } |
| 711 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 712 | // Add all object files to the PDB. Merge .debug$T sections into IpiData and |
| 713 | // TpiData. |
| 714 | void PDBLinker::addObjectsToPDB() { |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 +0000 | [diff] [blame] | 715 | for (ObjFile *File : ObjFile::Instances) |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 716 | addObjFile(File); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 717 | |
| 718 | Builder.getStringTableBuilder().setStrings(PDBStrTab); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 719 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 720 | // Construct TPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 721 | addTypeInfo(Builder.getTpiBuilder(), TypeTable); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 722 | |
| 723 | // Construct IPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 724 | addTypeInfo(Builder.getIpiBuilder(), IDTable); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 725 | |
Zachary Turner | ee9906d | 2017-08-11 19:00:03 +0000 | [diff] [blame] | 726 | // Compute the public and global symbols. |
| 727 | auto &GsiBuilder = Builder.getGsiBuilder(); |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 728 | std::vector<PublicSym32> Publics; |
Zachary Turner | 964e096 | 2017-08-11 20:37:49 +0000 | [diff] [blame] | 729 | Symtab->forEachSymbol([&Publics](Symbol *S) { |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 730 | // Only emit defined, live symbols that have a chunk. |
| 731 | auto *Def = dyn_cast<Defined>(S->body()); |
| 732 | if (Def && Def->isLive() && Def->getChunk()) |
| 733 | Publics.push_back(createPublic(Def)); |
| 734 | }); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 735 | |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 736 | if (!Publics.empty()) { |
| 737 | // Sort the public symbols and add them to the stream. |
| 738 | std::sort(Publics.begin(), Publics.end(), |
| 739 | [](const PublicSym32 &L, const PublicSym32 &R) { |
| 740 | return L.Name < R.Name; |
| 741 | }); |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 742 | for (const PublicSym32 &Pub : Publics) |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 743 | GsiBuilder.addPublicSymbol(Pub); |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 744 | } |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 747 | static void addCommonLinkerModuleSymbols(StringRef Path, |
| 748 | pdb::DbiModuleDescriptorBuilder &Mod, |
| 749 | BumpPtrAllocator &Allocator) { |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 750 | ObjNameSym ONS(SymbolRecordKind::ObjNameSym); |
| 751 | Compile3Sym CS(SymbolRecordKind::Compile3Sym); |
| 752 | EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 753 | |
| 754 | ONS.Name = "* Linker *"; |
| 755 | ONS.Signature = 0; |
| 756 | |
| 757 | CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386; |
Zachary Turner | b57884e | 2017-08-11 21:14:01 +0000 | [diff] [blame] | 758 | // Interestingly, if we set the string to 0.0.0.0, then when trying to view |
| 759 | // local variables WinDbg emits an error that private symbols are not present. |
| 760 | // By setting this to a valid MSVC linker version string, local variables are |
| 761 | // displayed properly. As such, even though it is not representative of |
Zachary Turner | f6822c8 | 2017-08-11 20:46:47 +0000 | [diff] [blame] | 762 | // LLVM's version information, we need this for compatibility. |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 763 | CS.Flags = CompileSym3Flags::None; |
Zachary Turner | f6822c8 | 2017-08-11 20:46:47 +0000 | [diff] [blame] | 764 | CS.VersionBackendBuild = 25019; |
| 765 | CS.VersionBackendMajor = 14; |
| 766 | CS.VersionBackendMinor = 10; |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 767 | CS.VersionBackendQFE = 0; |
Zachary Turner | f6822c8 | 2017-08-11 20:46:47 +0000 | [diff] [blame] | 768 | |
| 769 | // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the |
| 770 | // linker module (which is by definition a backend), so we don't need to do |
| 771 | // anything here. Also, it seems we can use "LLVM Linker" for the linker name |
| 772 | // without any problems. Only the backend version has to be hardcoded to a |
| 773 | // magic number. |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 774 | CS.VersionFrontendBuild = 0; |
| 775 | CS.VersionFrontendMajor = 0; |
| 776 | CS.VersionFrontendMinor = 0; |
| 777 | CS.VersionFrontendQFE = 0; |
| 778 | CS.Version = "LLVM Linker"; |
| 779 | CS.setLanguage(SourceLanguage::Link); |
| 780 | |
| 781 | ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front(); |
| 782 | std::string ArgStr = llvm::join(Args, " "); |
| 783 | EBS.Fields.push_back("cwd"); |
| 784 | SmallString<64> cwd; |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 785 | sys::fs::current_path(cwd); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 786 | EBS.Fields.push_back(cwd); |
| 787 | EBS.Fields.push_back("exe"); |
Zachary Turner | f6822c8 | 2017-08-11 20:46:47 +0000 | [diff] [blame] | 788 | SmallString<64> exe = Config->Argv[0]; |
| 789 | llvm::sys::fs::make_absolute(exe); |
| 790 | EBS.Fields.push_back(exe); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 791 | EBS.Fields.push_back("pdb"); |
| 792 | EBS.Fields.push_back(Path); |
| 793 | EBS.Fields.push_back("cmd"); |
| 794 | EBS.Fields.push_back(ArgStr); |
| 795 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 796 | ONS, Allocator, CodeViewContainer::Pdb)); |
| 797 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 798 | CS, Allocator, CodeViewContainer::Pdb)); |
| 799 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 800 | EBS, Allocator, CodeViewContainer::Pdb)); |
| 801 | } |
| 802 | |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 803 | static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod, |
| 804 | OutputSection &OS, |
| 805 | BumpPtrAllocator &Allocator) { |
| 806 | SectionSym Sym(SymbolRecordKind::SectionSym); |
Zachary Turner | f6822c8 | 2017-08-11 20:46:47 +0000 | [diff] [blame] | 807 | Sym.Alignment = 12; // 2^12 = 4KB |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 808 | Sym.Characteristics = OS.getCharacteristics(); |
| 809 | Sym.Length = OS.getVirtualSize(); |
| 810 | Sym.Name = OS.getName(); |
| 811 | Sym.Rva = OS.getRVA(); |
| 812 | Sym.SectionNumber = OS.SectionIndex; |
| 813 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 814 | Sym, Allocator, CodeViewContainer::Pdb)); |
| 815 | } |
| 816 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 817 | // Creates a PDB file. |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 818 | void coff::createPDB(SymbolTable *Symtab, |
| 819 | ArrayRef<OutputSection *> OutputSections, |
| 820 | ArrayRef<uint8_t> SectionTable, |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 821 | const llvm::codeview::DebugInfo &BuildId) { |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 822 | PDBLinker PDB(Symtab); |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 823 | PDB.initialize(BuildId); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 824 | PDB.addObjectsToPDB(); |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 825 | PDB.addSections(OutputSections, SectionTable); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 826 | PDB.commit(); |
| 827 | } |
| 828 | |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 829 | void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) { |
Rui Ueyama | 1297954 | 2016-09-30 20:53:45 +0000 | [diff] [blame] | 830 | ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 831 | |
Rui Ueyama | 8d3fb5d | 2016-10-05 22:08:58 +0000 | [diff] [blame] | 832 | // Create streams in MSF for predefined streams, namely |
| 833 | // PDB, TPI, DBI and IPI. |
| 834 | for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) |
| 835 | ExitOnErr(Builder.getMsfBuilder().addStream(0)); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 836 | |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 837 | // Add an Info stream. |
| 838 | auto &InfoBuilder = Builder.getInfoBuilder(); |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 839 | InfoBuilder.setAge(BuildId.PDB70.Age); |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 840 | |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 841 | GUID uuid; |
| 842 | memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid)); |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 843 | InfoBuilder.setGuid(uuid); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 844 | InfoBuilder.setSignature(time(nullptr)); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 845 | InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 846 | |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 847 | // Add an empty DBI stream. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 848 | pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); |
Zachary Turner | 024323c | 2017-08-15 21:31:41 +0000 | [diff] [blame] | 849 | DbiBuilder.setAge(BuildId.PDB70.Age); |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 850 | DbiBuilder.setVersionHeader(pdb::PdbDbiV70); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 851 | ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {})); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 852 | } |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 853 | |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 854 | void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule, |
| 855 | OutputSection *OS, Chunk *C) { |
| 856 | pdb::SectionContrib SC; |
| 857 | memset(&SC, 0, sizeof(SC)); |
| 858 | SC.ISect = OS->SectionIndex; |
| 859 | SC.Off = C->getRVA() - OS->getRVA(); |
| 860 | SC.Size = C->getSize(); |
| 861 | if (auto *SecChunk = dyn_cast<SectionChunk>(C)) { |
| 862 | SC.Characteristics = SecChunk->Header->Characteristics; |
| 863 | SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex(); |
Zachary Turner | 676386ff | 2017-08-07 20:23:45 +0000 | [diff] [blame] | 864 | ArrayRef<uint8_t> Contents = SecChunk->getContents(); |
| 865 | JamCRC CRC(0); |
| 866 | ArrayRef<char> CharContents = makeArrayRef( |
| 867 | reinterpret_cast<const char *>(Contents.data()), Contents.size()); |
| 868 | CRC.update(CharContents); |
| 869 | SC.DataCrc = CRC.getCRC(); |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 870 | } else { |
| 871 | SC.Characteristics = OS->getCharacteristics(); |
| 872 | // FIXME: When we start creating DBI for import libraries, use those here. |
| 873 | SC.Imod = LinkerModule.getModuleIndex(); |
| 874 | } |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 875 | SC.RelocCrc = 0; // FIXME |
| 876 | Builder.getDbiBuilder().addSectionContrib(SC); |
| 877 | } |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 878 | |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 879 | void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections, |
| 880 | ArrayRef<uint8_t> SectionTable) { |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 881 | // It's not entirely clear what this is, but the * Linker * module uses it. |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 882 | pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 883 | NativePath = Config->PDBPath; |
| 884 | sys::fs::make_absolute(NativePath); |
| 885 | sys::path::native(NativePath, sys::path::Style::windows); |
| 886 | uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath); |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 887 | auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); |
| 888 | LinkerModule.setPdbFilePathNI(PdbFilePathNI); |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 889 | addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc); |
Rui Ueyama | c91f716 | 2016-11-16 01:10:46 +0000 | [diff] [blame] | 890 | |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 891 | // Add section contributions. They must be ordered by ascending RVA. |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 892 | for (OutputSection *OS : OutputSections) { |
| 893 | addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc); |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 894 | for (Chunk *C : OS->getChunks()) |
| 895 | addSectionContrib(LinkerModule, OS, C); |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 896 | } |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame] | 897 | |
| 898 | // Add Section Map stream. |
| 899 | ArrayRef<object::coff_section> Sections = { |
| 900 | (const object::coff_section *)SectionTable.data(), |
| 901 | SectionTable.size() / sizeof(object::coff_section)}; |
| 902 | SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections); |
| 903 | DbiBuilder.setSectionMap(SectionMap); |
| 904 | |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 905 | // Add COFF section header stream. |
| 906 | ExitOnErr( |
| 907 | DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 908 | } |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 909 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 910 | void PDBLinker::commit() { |
Rui Ueyama | 3e9d6bb | 2016-09-26 23:53:55 +0000 | [diff] [blame] | 911 | // Write to a file. |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 912 | ExitOnErr(Builder.commit(Config->PDBPath)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 913 | } |