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