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