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