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