| 1 | //===- PDB.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "PDB.h" |
| 10 | #include "COFFLinkerContext.h" |
| 11 | #include "Chunks.h" |
| 12 | #include "Config.h" |
| 13 | #include "DebugTypes.h" |
| 14 | #include "Driver.h" |
| 15 | #include "SymbolTable.h" |
| 16 | #include "Symbols.h" |
| 17 | #include "TypeMerger.h" |
| 18 | #include "Writer.h" |
| 19 | #include "lld/Common/Timer.h" |
| 20 | #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" |
| 21 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" |
| 22 | #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" |
| 23 | #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" |
| 24 | #include "llvm/DebugInfo/CodeView/RecordName.h" |
| 25 | #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h" |
| 26 | #include "llvm/DebugInfo/CodeView/SymbolSerializer.h" |
| 27 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
| 28 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
| 29 | #include "llvm/DebugInfo/MSF/MSFError.h" |
| 30 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
| 33 | #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" |
| 34 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 35 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
| 36 | #include "llvm/DebugInfo/PDB/Native/NativeSession.h" |
| 37 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 38 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
| 39 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
| 40 | #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" |
| 41 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 42 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
| 43 | #include "llvm/Object/COFF.h" |
| 44 | #include "llvm/Object/CVDebugRecord.h" |
| 45 | #include "llvm/Support/CRC.h" |
| 46 | #include "llvm/Support/Endian.h" |
| 47 | #include "llvm/Support/FormatVariadic.h" |
| 48 | #include "llvm/Support/Path.h" |
| 49 | #include "llvm/Support/ScopedPrinter.h" |
| 50 | #include "llvm/Support/TimeProfiler.h" |
| 51 | #include <memory> |
| 52 | #include <optional> |
| 53 | |
| 54 | using namespace llvm; |
| 55 | using namespace llvm::codeview; |
| 56 | using namespace lld; |
| 57 | using namespace lld::coff; |
| 58 | |
| 59 | using llvm::object::coff_section; |
| 60 | using llvm::pdb::StringTableFixup; |
| 61 | |
| 62 | namespace { |
| 63 | class DebugSHandler; |
| 64 | |
| 65 | class PDBLinker { |
| 66 | friend DebugSHandler; |
| 67 | |
| 68 | public: |
| 69 | PDBLinker(COFFLinkerContext &ctx) |
| 70 | : builder(bAlloc()), tMerger(ctx, bAlloc()), ctx(ctx) { |
| 71 | // This isn't strictly necessary, but link.exe usually puts an empty string |
| 72 | // as the first "valid" string in the string table, so we do the same in |
| 73 | // order to maintain as much byte-for-byte compatibility as possible. |
| 74 | pdbStrTab.insert(S: "" ); |
| 75 | } |
| 76 | |
| 77 | /// Emit the basic PDB structure: initial streams, headers, etc. |
| 78 | void initialize(llvm::codeview::DebugInfo *buildId); |
| 79 | |
| 80 | /// Add natvis files specified on the command line. |
| 81 | void addNatvisFiles(); |
| 82 | |
| 83 | /// Add named streams specified on the command line. |
| 84 | void addNamedStreams(); |
| 85 | |
| 86 | /// Link CodeView from each object file in the symbol table into the PDB. |
| 87 | void addObjectsToPDB(); |
| 88 | |
| 89 | /// Add every live, defined public symbol to the PDB. |
| 90 | void addPublicsToPDB(); |
| 91 | |
| 92 | /// Link info for each import file in the symbol table into the PDB. |
| 93 | void addImportFilesToPDB(); |
| 94 | |
| 95 | void createModuleDBI(ObjFile *file); |
| 96 | |
| 97 | /// Link CodeView from a single object file into the target (output) PDB. |
| 98 | /// When a precompiled headers object is linked, its TPI map might be provided |
| 99 | /// externally. |
| 100 | void addDebug(TpiSource *source); |
| 101 | |
| 102 | void addDebugSymbols(TpiSource *source); |
| 103 | |
| 104 | // Analyze the symbol records to separate module symbols from global symbols, |
| 105 | // find string references, and calculate how large the symbol stream will be |
| 106 | // in the PDB. |
| 107 | void analyzeSymbolSubsection(SectionChunk *debugChunk, |
| 108 | uint32_t &moduleSymOffset, |
| 109 | uint32_t &nextRelocIndex, |
| 110 | std::vector<StringTableFixup> &stringTableFixups, |
| 111 | BinaryStreamRef symData); |
| 112 | |
| 113 | // Write all module symbols from all live debug symbol subsections of the |
| 114 | // given object file into the given stream writer. |
| 115 | Error writeAllModuleSymbolRecords(ObjFile *file, BinaryStreamWriter &writer); |
| 116 | |
| 117 | // Callback to copy and relocate debug symbols during PDB file writing. |
| 118 | static Error commitSymbolsForObject(void *ctx, void *obj, |
| 119 | BinaryStreamWriter &writer); |
| 120 | |
| 121 | // Copy the symbol record, relocate it, and fix the alignment if necessary. |
| 122 | // Rewrite type indices in the record. Replace unrecognized symbol records |
| 123 | // with S_SKIP records. |
| 124 | void writeSymbolRecord(SectionChunk *debugChunk, |
| 125 | ArrayRef<uint8_t> sectionContents, CVSymbol sym, |
| 126 | size_t alignedSize, uint32_t &nextRelocIndex, |
| 127 | std::vector<uint8_t> &storage); |
| 128 | |
| 129 | /// Add the section map and section contributions to the PDB. |
| 130 | void addSections(ArrayRef<uint8_t> sectionTable); |
| 131 | |
| 132 | /// Write the PDB to disk and store the Guid generated for it in *Guid. |
| 133 | void commit(codeview::GUID *guid); |
| 134 | |
| 135 | // Print statistics regarding the final PDB |
| 136 | void printStats(); |
| 137 | |
| 138 | private: |
| 139 | void pdbMakeAbsolute(SmallVectorImpl<char> &fileName); |
| 140 | void translateIdSymbols(MutableArrayRef<uint8_t> &recordData, |
| 141 | TpiSource *source); |
| 142 | void addCommonLinkerModuleSymbols(StringRef path, |
| 143 | pdb::DbiModuleDescriptorBuilder &mod); |
| 144 | |
| 145 | pdb::PDBFileBuilder builder; |
| 146 | |
| 147 | TypeMerger tMerger; |
| 148 | |
| 149 | COFFLinkerContext &ctx; |
| 150 | |
| 151 | /// PDBs use a single global string table for filenames in the file checksum |
| 152 | /// table. |
| 153 | DebugStringTableSubsection pdbStrTab; |
| 154 | |
| 155 | llvm::SmallString<128> nativePath; |
| 156 | |
| 157 | // For statistics |
| 158 | uint64_t globalSymbols = 0; |
| 159 | uint64_t moduleSymbols = 0; |
| 160 | uint64_t publicSymbols = 0; |
| 161 | uint64_t nbTypeRecords = 0; |
| 162 | uint64_t nbTypeRecordsBytes = 0; |
| 163 | }; |
| 164 | |
| 165 | /// Represents an unrelocated DEBUG_S_FRAMEDATA subsection. |
| 166 | struct UnrelocatedFpoData { |
| 167 | SectionChunk *debugChunk = nullptr; |
| 168 | ArrayRef<uint8_t> subsecData; |
| 169 | uint32_t relocIndex = 0; |
| 170 | }; |
| 171 | |
| 172 | /// The size of the magic bytes at the beginning of a symbol section or stream. |
| 173 | enum : uint32_t { kSymbolStreamMagicSize = 4 }; |
| 174 | |
| 175 | class DebugSHandler { |
| 176 | COFFLinkerContext &ctx; |
| 177 | PDBLinker &linker; |
| 178 | |
| 179 | /// The object file whose .debug$S sections we're processing. |
| 180 | ObjFile &file; |
| 181 | |
| 182 | /// The DEBUG_S_STRINGTABLE subsection. These strings are referred to by |
| 183 | /// index from other records in the .debug$S section. All of these strings |
| 184 | /// need to be added to the global PDB string table, and all references to |
| 185 | /// these strings need to have their indices re-written to refer to the |
| 186 | /// global PDB string table. |
| 187 | DebugStringTableSubsectionRef cvStrTab; |
| 188 | |
| 189 | /// The DEBUG_S_FILECHKSMS subsection. As above, these are referred to |
| 190 | /// by other records in the .debug$S section and need to be merged into the |
| 191 | /// PDB. |
| 192 | DebugChecksumsSubsectionRef checksums; |
| 193 | |
| 194 | /// The DEBUG_S_FRAMEDATA subsection(s). There can be more than one of |
| 195 | /// these and they need not appear in any specific order. However, they |
| 196 | /// contain string table references which need to be re-written, so we |
| 197 | /// collect them all here and re-write them after all subsections have been |
| 198 | /// discovered and processed. |
| 199 | std::vector<UnrelocatedFpoData> frameDataSubsecs; |
| 200 | |
| 201 | /// List of string table references in symbol records. Later they will be |
| 202 | /// applied to the symbols during PDB writing. |
| 203 | std::vector<StringTableFixup> stringTableFixups; |
| 204 | |
| 205 | /// Sum of the size of all module symbol records across all .debug$S sections. |
| 206 | /// Includes record realignment and the size of the symbol stream magic |
| 207 | /// prefix. |
| 208 | uint32_t moduleStreamSize = kSymbolStreamMagicSize; |
| 209 | |
| 210 | /// Next relocation index in the current .debug$S section. Resets every |
| 211 | /// handleDebugS call. |
| 212 | uint32_t nextRelocIndex = 0; |
| 213 | |
| 214 | void advanceRelocIndex(SectionChunk *debugChunk, ArrayRef<uint8_t> subsec); |
| 215 | |
| 216 | void addUnrelocatedSubsection(SectionChunk *debugChunk, |
| 217 | const DebugSubsectionRecord &ss); |
| 218 | |
| 219 | void addFrameDataSubsection(SectionChunk *debugChunk, |
| 220 | const DebugSubsectionRecord &ss); |
| 221 | |
| 222 | public: |
| 223 | DebugSHandler(COFFLinkerContext &ctx, PDBLinker &linker, ObjFile &file) |
| 224 | : ctx(ctx), linker(linker), file(file) {} |
| 225 | |
| 226 | void handleDebugS(SectionChunk *debugChunk); |
| 227 | |
| 228 | void finish(); |
| 229 | }; |
| 230 | } |
| 231 | |
| 232 | // Visual Studio's debugger requires absolute paths in various places in the |
| 233 | // PDB to work without additional configuration: |
| 234 | // https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box |
| 235 | void PDBLinker::pdbMakeAbsolute(SmallVectorImpl<char> &fileName) { |
| 236 | // The default behavior is to produce paths that are valid within the context |
| 237 | // of the machine that you perform the link on. If the linker is running on |
| 238 | // a POSIX system, we will output absolute POSIX paths. If the linker is |
| 239 | // running on a Windows system, we will output absolute Windows paths. If the |
| 240 | // user desires any other kind of behavior, they should explicitly pass |
| 241 | // /pdbsourcepath, in which case we will treat the exact string the user |
| 242 | // passed in as the gospel and not normalize, canonicalize it. |
| 243 | if (sys::path::is_absolute(path: fileName, style: sys::path::Style::windows) || |
| 244 | sys::path::is_absolute(path: fileName, style: sys::path::Style::posix)) |
| 245 | return; |
| 246 | |
| 247 | // It's not absolute in any path syntax. Relative paths necessarily refer to |
| 248 | // the local file system, so we can make it native without ending up with a |
| 249 | // nonsensical path. |
| 250 | if (ctx.config.pdbSourcePath.empty()) { |
| 251 | sys::path::native(path&: fileName); |
| 252 | sys::fs::make_absolute(path&: fileName); |
| 253 | sys::path::remove_dots(path&: fileName, remove_dot_dot: true); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | // Try to guess whether /PDBSOURCEPATH is a unix path or a windows path. |
| 258 | // Since PDB's are more of a Windows thing, we make this conservative and only |
| 259 | // decide that it's a unix path if we're fairly certain. Specifically, if |
| 260 | // it starts with a forward slash. |
| 261 | SmallString<128> absoluteFileName = ctx.config.pdbSourcePath; |
| 262 | sys::path::Style guessedStyle = absoluteFileName.starts_with(Prefix: "/" ) |
| 263 | ? sys::path::Style::posix |
| 264 | : sys::path::Style::windows; |
| 265 | sys::path::append(path&: absoluteFileName, style: guessedStyle, a: fileName); |
| 266 | sys::path::native(path&: absoluteFileName, style: guessedStyle); |
| 267 | sys::path::remove_dots(path&: absoluteFileName, remove_dot_dot: true, style: guessedStyle); |
| 268 | |
| 269 | fileName = std::move(absoluteFileName); |
| 270 | } |
| 271 | |
| 272 | static void addTypeInfo(pdb::TpiStreamBuilder &tpiBuilder, |
| 273 | TypeCollection &typeTable) { |
| 274 | // Start the TPI or IPI stream header. |
| 275 | tpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
| 276 | |
| 277 | // Flatten the in memory type table and hash each type. |
| 278 | typeTable.ForEachRecord(Func: [&](TypeIndex ti, const CVType &type) { |
| 279 | auto hash = pdb::hashTypeRecord(Type: type); |
| 280 | if (auto e = hash.takeError()) |
| 281 | fatal(msg: "type hashing error" ); |
| 282 | tpiBuilder.addTypeRecord(Type: type.RecordData, Hash: *hash); |
| 283 | }); |
| 284 | } |
| 285 | |
| 286 | static void addGHashTypeInfo(COFFLinkerContext &ctx, |
| 287 | pdb::PDBFileBuilder &builder) { |
| 288 | // Start the TPI or IPI stream header. |
| 289 | builder.getTpiBuilder().setVersionHeader(pdb::PdbTpiV80); |
| 290 | builder.getIpiBuilder().setVersionHeader(pdb::PdbTpiV80); |
| 291 | for (TpiSource *source : ctx.tpiSourceList) { |
| 292 | builder.getTpiBuilder().addTypeRecords(Types: source->mergedTpi.recs, |
| 293 | Sizes: source->mergedTpi.recSizes, |
| 294 | Hashes: source->mergedTpi.recHashes); |
| 295 | builder.getIpiBuilder().addTypeRecords(Types: source->mergedIpi.recs, |
| 296 | Sizes: source->mergedIpi.recSizes, |
| 297 | Hashes: source->mergedIpi.recHashes); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static void |
| 302 | recordStringTableReferences(CVSymbol sym, uint32_t symOffset, |
| 303 | std::vector<StringTableFixup> &stringTableFixups) { |
| 304 | // For now we only handle S_FILESTATIC, but we may need the same logic for |
| 305 | // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any |
| 306 | // PDBs that contain these types of records, so because of the uncertainty |
| 307 | // they are omitted here until we can prove that it's necessary. |
| 308 | switch (sym.kind()) { |
| 309 | case SymbolKind::S_FILESTATIC: { |
| 310 | // FileStaticSym::ModFileOffset |
| 311 | uint32_t ref = *reinterpret_cast<const ulittle32_t *>(&sym.data()[8]); |
| 312 | stringTableFixups.push_back(x: {.StrTabOffset: ref, .SymOffsetOfReference: symOffset + 8}); |
| 313 | break; |
| 314 | } |
| 315 | case SymbolKind::S_DEFRANGE: |
| 316 | case SymbolKind::S_DEFRANGE_SUBFIELD: |
| 317 | log(msg: "Not fixing up string table reference in S_DEFRANGE / " |
| 318 | "S_DEFRANGE_SUBFIELD record" ); |
| 319 | break; |
| 320 | default: |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static SymbolKind symbolKind(ArrayRef<uint8_t> recordData) { |
| 326 | const RecordPrefix *prefix = |
| 327 | reinterpret_cast<const RecordPrefix *>(recordData.data()); |
| 328 | return static_cast<SymbolKind>(uint16_t(prefix->RecordKind)); |
| 329 | } |
| 330 | |
| 331 | /// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32 |
| 332 | void PDBLinker::translateIdSymbols(MutableArrayRef<uint8_t> &recordData, |
| 333 | TpiSource *source) { |
| 334 | RecordPrefix *prefix = reinterpret_cast<RecordPrefix *>(recordData.data()); |
| 335 | |
| 336 | SymbolKind kind = symbolKind(recordData); |
| 337 | |
| 338 | if (kind == SymbolKind::S_PROC_ID_END) { |
| 339 | prefix->RecordKind = SymbolKind::S_END; |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | // In an object file, GPROC32_ID has an embedded reference which refers to the |
| 344 | // single object file type index namespace. This has already been translated |
| 345 | // to the PDB file's ID stream index space, but we need to convert this to a |
| 346 | // symbol that refers to the type stream index space. So we remap again from |
| 347 | // ID index space to type index space. |
| 348 | if (kind == SymbolKind::S_GPROC32_ID || kind == SymbolKind::S_LPROC32_ID) { |
| 349 | SmallVector<TiReference, 1> refs; |
| 350 | auto content = recordData.drop_front(N: sizeof(RecordPrefix)); |
| 351 | CVSymbol sym(recordData); |
| 352 | discoverTypeIndicesInSymbol(Symbol: sym, Refs&: refs); |
| 353 | assert(refs.size() == 1); |
| 354 | assert(refs.front().Count == 1); |
| 355 | |
| 356 | TypeIndex *ti = |
| 357 | reinterpret_cast<TypeIndex *>(content.data() + refs[0].Offset); |
| 358 | // `ti` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in |
| 359 | // the IPI stream, whose `FunctionType` member refers to the TPI stream. |
| 360 | // Note that LF_FUNC_ID and LF_MFUNC_ID have the same record layout, and |
| 361 | // in both cases we just need the second type index. |
| 362 | if (!ti->isSimple() && !ti->isNoneType()) { |
| 363 | TypeIndex newType = TypeIndex(SimpleTypeKind::NotTranslated); |
| 364 | if (ctx.config.debugGHashes) { |
| 365 | auto idToType = tMerger.funcIdToType.find(Val: *ti); |
| 366 | if (idToType != tMerger.funcIdToType.end()) |
| 367 | newType = idToType->second; |
| 368 | } else { |
| 369 | if (tMerger.getIDTable().contains(Index: *ti)) { |
| 370 | CVType funcIdData = tMerger.getIDTable().getType(Index: *ti); |
| 371 | if (funcIdData.length() >= 8 && (funcIdData.kind() == LF_FUNC_ID || |
| 372 | funcIdData.kind() == LF_MFUNC_ID)) { |
| 373 | newType = *reinterpret_cast<const TypeIndex *>(&funcIdData.data()[8]); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | if (newType == TypeIndex(SimpleTypeKind::NotTranslated)) { |
| 378 | Warn(ctx) << formatv( |
| 379 | Fmt: "procedure symbol record for `{0}` in {1} refers to PDB " |
| 380 | "item index {2:X} which is not a valid function ID record" , |
| 381 | Vals: getSymbolName(Sym: CVSymbol(recordData)), Vals: source->file->getName(), |
| 382 | Vals: ti->getIndex()); |
| 383 | } |
| 384 | *ti = newType; |
| 385 | } |
| 386 | |
| 387 | kind = (kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32 |
| 388 | : SymbolKind::S_LPROC32; |
| 389 | prefix->RecordKind = uint16_t(kind); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | namespace { |
| 394 | struct ScopeRecord { |
| 395 | ulittle32_t ptrParent; |
| 396 | ulittle32_t ptrEnd; |
| 397 | }; |
| 398 | } // namespace |
| 399 | |
| 400 | /// Given a pointer to a symbol record that opens a scope, return a pointer to |
| 401 | /// the scope fields. |
| 402 | static ScopeRecord *getSymbolScopeFields(void *sym) { |
| 403 | return reinterpret_cast<ScopeRecord *>(reinterpret_cast<char *>(sym) + |
| 404 | sizeof(RecordPrefix)); |
| 405 | } |
| 406 | |
| 407 | // To open a scope, push the offset of the current symbol record onto the |
| 408 | // stack. |
| 409 | static void scopeStackOpen(SmallVectorImpl<uint32_t> &stack, |
| 410 | std::vector<uint8_t> &storage) { |
| 411 | stack.push_back(Elt: storage.size()); |
| 412 | } |
| 413 | |
| 414 | // To close a scope, update the record that opened the scope. |
| 415 | static void scopeStackClose(COFFLinkerContext &ctx, |
| 416 | SmallVectorImpl<uint32_t> &stack, |
| 417 | std::vector<uint8_t> &storage, |
| 418 | uint32_t storageBaseOffset, ObjFile *file) { |
| 419 | if (stack.empty()) { |
| 420 | Warn(ctx) << "symbol scopes are not balanced in " << file->getName(); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | // Update ptrEnd of the record that opened the scope to point to the |
| 425 | // current record, if we are writing into the module symbol stream. |
| 426 | uint32_t offOpen = stack.pop_back_val(); |
| 427 | uint32_t offEnd = storageBaseOffset + storage.size(); |
| 428 | uint32_t offParent = stack.empty() ? 0 : (stack.back() + storageBaseOffset); |
| 429 | ScopeRecord *scopeRec = getSymbolScopeFields(sym: &(storage)[offOpen]); |
| 430 | scopeRec->ptrParent = offParent; |
| 431 | scopeRec->ptrEnd = offEnd; |
| 432 | } |
| 433 | |
| 434 | static bool symbolGoesInModuleStream(const CVSymbol &sym, |
| 435 | unsigned symbolScopeDepth) { |
| 436 | switch (sym.kind()) { |
| 437 | case SymbolKind::S_GDATA32: |
| 438 | case SymbolKind::S_GTHREAD32: |
| 439 | // We really should not be seeing S_PROCREF and S_LPROCREF in the first place |
| 440 | // since they are synthesized by the linker in response to S_GPROC32 and |
| 441 | // S_LPROC32, but if we do see them, don't put them in the module stream I |
| 442 | // guess. |
| 443 | case SymbolKind::S_PROCREF: |
| 444 | case SymbolKind::S_LPROCREF: |
| 445 | return false; |
| 446 | // S_UDT and S_CONSTANT records go in the module stream if it is not a global record. |
| 447 | case SymbolKind::S_UDT: |
| 448 | case SymbolKind::S_CONSTANT: |
| 449 | return symbolScopeDepth > 0; |
| 450 | // S_GDATA32 does not go in the module stream, but S_LDATA32 does. |
| 451 | case SymbolKind::S_LDATA32: |
| 452 | case SymbolKind::S_LTHREAD32: |
| 453 | default: |
| 454 | return true; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | static bool symbolGoesInGlobalsStream(const CVSymbol &sym, |
| 459 | unsigned symbolScopeDepth) { |
| 460 | switch (sym.kind()) { |
| 461 | case SymbolKind::S_GDATA32: |
| 462 | case SymbolKind::S_GTHREAD32: |
| 463 | case SymbolKind::S_GPROC32: |
| 464 | case SymbolKind::S_LPROC32: |
| 465 | case SymbolKind::S_GPROC32_ID: |
| 466 | case SymbolKind::S_LPROC32_ID: |
| 467 | // We really should not be seeing S_PROCREF and S_LPROCREF in the first place |
| 468 | // since they are synthesized by the linker in response to S_GPROC32 and |
| 469 | // S_LPROC32, but if we do see them, copy them straight through. |
| 470 | case SymbolKind::S_PROCREF: |
| 471 | case SymbolKind::S_LPROCREF: |
| 472 | return true; |
| 473 | // Records that go in the globals stream, unless they are function-local. |
| 474 | case SymbolKind::S_UDT: |
| 475 | case SymbolKind::S_LDATA32: |
| 476 | case SymbolKind::S_LTHREAD32: |
| 477 | case SymbolKind::S_CONSTANT: |
| 478 | return symbolScopeDepth == 0; |
| 479 | default: |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void addGlobalSymbol(pdb::GSIStreamBuilder &builder, uint16_t modIndex, |
| 485 | unsigned symOffset, |
| 486 | std::vector<uint8_t> &symStorage) { |
| 487 | CVSymbol sym{ArrayRef(symStorage)}; |
| 488 | switch (sym.kind()) { |
| 489 | case SymbolKind::S_CONSTANT: |
| 490 | case SymbolKind::S_UDT: |
| 491 | case SymbolKind::S_GDATA32: |
| 492 | case SymbolKind::S_GTHREAD32: |
| 493 | case SymbolKind::S_LTHREAD32: |
| 494 | case SymbolKind::S_LDATA32: |
| 495 | case SymbolKind::S_PROCREF: |
| 496 | case SymbolKind::S_LPROCREF: { |
| 497 | // sym is a temporary object, so we have to copy and reallocate the record |
| 498 | // to stabilize it. |
| 499 | uint8_t *mem = bAlloc().Allocate<uint8_t>(Num: sym.length()); |
| 500 | memcpy(dest: mem, src: sym.data().data(), n: sym.length()); |
| 501 | builder.addGlobalSymbol(Sym: CVSymbol(ArrayRef(mem, sym.length()))); |
| 502 | break; |
| 503 | } |
| 504 | case SymbolKind::S_GPROC32: |
| 505 | case SymbolKind::S_LPROC32: { |
| 506 | SymbolRecordKind k = SymbolRecordKind::ProcRefSym; |
| 507 | if (sym.kind() == SymbolKind::S_LPROC32) |
| 508 | k = SymbolRecordKind::LocalProcRef; |
| 509 | ProcRefSym ps(k); |
| 510 | ps.Module = modIndex; |
| 511 | // For some reason, MSVC seems to add one to this value. |
| 512 | ++ps.Module; |
| 513 | ps.Name = getSymbolName(Sym: sym); |
| 514 | ps.SumName = 0; |
| 515 | ps.SymOffset = symOffset; |
| 516 | builder.addGlobalSymbol(Sym: ps); |
| 517 | break; |
| 518 | } |
| 519 | default: |
| 520 | llvm_unreachable("Invalid symbol kind!" ); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Check if the given symbol record was padded for alignment. If so, zero out |
| 525 | // the padding bytes and update the record prefix with the new size. |
| 526 | static void fixRecordAlignment(MutableArrayRef<uint8_t> recordBytes, |
| 527 | size_t oldSize) { |
| 528 | size_t alignedSize = recordBytes.size(); |
| 529 | if (oldSize == alignedSize) |
| 530 | return; |
| 531 | reinterpret_cast<RecordPrefix *>(recordBytes.data())->RecordLen = |
| 532 | alignedSize - 2; |
| 533 | memset(s: recordBytes.data() + oldSize, c: 0, n: alignedSize - oldSize); |
| 534 | } |
| 535 | |
| 536 | // Replace any record with a skip record of the same size. This is useful when |
| 537 | // we have reserved size for a symbol record, but type index remapping fails. |
| 538 | static void replaceWithSkipRecord(MutableArrayRef<uint8_t> recordBytes) { |
| 539 | memset(s: recordBytes.data(), c: 0, n: recordBytes.size()); |
| 540 | auto *prefix = reinterpret_cast<RecordPrefix *>(recordBytes.data()); |
| 541 | prefix->RecordKind = SymbolKind::S_SKIP; |
| 542 | prefix->RecordLen = recordBytes.size() - 2; |
| 543 | } |
| 544 | |
| 545 | // Copy the symbol record, relocate it, and fix the alignment if necessary. |
| 546 | // Rewrite type indices in the record. Replace unrecognized symbol records with |
| 547 | // S_SKIP records. |
| 548 | void PDBLinker::writeSymbolRecord(SectionChunk *debugChunk, |
| 549 | ArrayRef<uint8_t> sectionContents, |
| 550 | CVSymbol sym, size_t alignedSize, |
| 551 | uint32_t &nextRelocIndex, |
| 552 | std::vector<uint8_t> &storage) { |
| 553 | // Allocate space for the new record at the end of the storage. |
| 554 | storage.resize(new_size: storage.size() + alignedSize); |
| 555 | auto recordBytes = MutableArrayRef<uint8_t>(storage).take_back(N: alignedSize); |
| 556 | |
| 557 | // Copy the symbol record and relocate it. |
| 558 | debugChunk->writeAndRelocateSubsection(sec: sectionContents, subsec: sym.data(), |
| 559 | nextRelocIndex, buf: recordBytes.data()); |
| 560 | fixRecordAlignment(recordBytes, oldSize: sym.length()); |
| 561 | |
| 562 | // Re-map all the type index references. |
| 563 | TpiSource *source = debugChunk->file->debugTypesObj; |
| 564 | if (!source->remapTypesInSymbolRecord(rec: recordBytes)) { |
| 565 | Log(ctx) << "ignoring unknown symbol record with kind 0x" |
| 566 | << utohexstr(X: sym.kind()); |
| 567 | replaceWithSkipRecord(recordBytes); |
| 568 | } |
| 569 | |
| 570 | // An object file may have S_xxx_ID symbols, but these get converted to |
| 571 | // "real" symbols in a PDB. |
| 572 | translateIdSymbols(recordData&: recordBytes, source); |
| 573 | } |
| 574 | |
| 575 | void PDBLinker::analyzeSymbolSubsection( |
| 576 | SectionChunk *debugChunk, uint32_t &moduleSymOffset, |
| 577 | uint32_t &nextRelocIndex, std::vector<StringTableFixup> &stringTableFixups, |
| 578 | BinaryStreamRef symData) { |
| 579 | ObjFile *file = debugChunk->file; |
| 580 | uint32_t moduleSymStart = moduleSymOffset; |
| 581 | |
| 582 | uint32_t scopeLevel = 0; |
| 583 | std::vector<uint8_t> storage; |
| 584 | ArrayRef<uint8_t> sectionContents = debugChunk->getContents(); |
| 585 | |
| 586 | ArrayRef<uint8_t> symsBuffer; |
| 587 | cantFail(Err: symData.readBytes(Offset: 0, Size: symData.getLength(), Buffer&: symsBuffer)); |
| 588 | |
| 589 | if (symsBuffer.empty()) |
| 590 | Warn(ctx) << "empty symbols subsection in " << file->getName(); |
| 591 | |
| 592 | Error ec = forEachCodeViewRecord<CVSymbol>( |
| 593 | StreamBuffer: symsBuffer, F: [&](CVSymbol sym) -> llvm::Error { |
| 594 | // Track the current scope. |
| 595 | if (symbolOpensScope(Kind: sym.kind())) |
| 596 | ++scopeLevel; |
| 597 | else if (symbolEndsScope(Kind: sym.kind())) |
| 598 | --scopeLevel; |
| 599 | |
| 600 | uint32_t alignedSize = |
| 601 | alignTo(Value: sym.length(), Align: alignOf(Container: CodeViewContainer::Pdb)); |
| 602 | |
| 603 | // Copy global records. Some global records (mainly procedures) |
| 604 | // reference the current offset into the module stream. |
| 605 | if (symbolGoesInGlobalsStream(sym, symbolScopeDepth: scopeLevel)) { |
| 606 | storage.clear(); |
| 607 | writeSymbolRecord(debugChunk, sectionContents, sym, alignedSize, |
| 608 | nextRelocIndex, storage); |
| 609 | addGlobalSymbol(builder&: builder.getGsiBuilder(), |
| 610 | modIndex: file->moduleDBI->getModuleIndex(), symOffset: moduleSymOffset, |
| 611 | symStorage&: storage); |
| 612 | ++globalSymbols; |
| 613 | } |
| 614 | |
| 615 | // Update the module stream offset and record any string table index |
| 616 | // references. There are very few of these and they will be rewritten |
| 617 | // later during PDB writing. |
| 618 | if (symbolGoesInModuleStream(sym, symbolScopeDepth: scopeLevel)) { |
| 619 | recordStringTableReferences(sym, symOffset: moduleSymOffset, stringTableFixups); |
| 620 | moduleSymOffset += alignedSize; |
| 621 | ++moduleSymbols; |
| 622 | } |
| 623 | |
| 624 | return Error::success(); |
| 625 | }); |
| 626 | |
| 627 | // If we encountered corrupt records, ignore the whole subsection. If we wrote |
| 628 | // any partial records, undo that. For globals, we just keep what we have and |
| 629 | // continue. |
| 630 | if (ec) { |
| 631 | Warn(ctx) << "corrupt symbol records in " << file->getName(); |
| 632 | moduleSymOffset = moduleSymStart; |
| 633 | consumeError(Err: std::move(ec)); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | Error PDBLinker::writeAllModuleSymbolRecords(ObjFile *file, |
| 638 | BinaryStreamWriter &writer) { |
| 639 | ExitOnError exitOnErr; |
| 640 | std::vector<uint8_t> storage; |
| 641 | SmallVector<uint32_t, 4> scopes; |
| 642 | |
| 643 | // Visit all live .debug$S sections a second time, and write them to the PDB. |
| 644 | for (SectionChunk *debugChunk : file->getDebugChunks()) { |
| 645 | if (!debugChunk->live || debugChunk->getSize() == 0 || |
| 646 | debugChunk->getSectionName() != ".debug$S" ) |
| 647 | continue; |
| 648 | |
| 649 | ArrayRef<uint8_t> sectionContents = debugChunk->getContents(); |
| 650 | auto contents = |
| 651 | SectionChunk::consumeDebugMagic(data: sectionContents, sectionName: ".debug$S" ); |
| 652 | DebugSubsectionArray subsections; |
| 653 | BinaryStreamReader reader(contents, llvm::endianness::little); |
| 654 | exitOnErr(reader.readArray(Array&: subsections, Size: contents.size())); |
| 655 | |
| 656 | uint32_t nextRelocIndex = 0; |
| 657 | for (const DebugSubsectionRecord &ss : subsections) { |
| 658 | if (ss.kind() != DebugSubsectionKind::Symbols) |
| 659 | continue; |
| 660 | |
| 661 | uint32_t moduleSymStart = writer.getOffset(); |
| 662 | scopes.clear(); |
| 663 | storage.clear(); |
| 664 | ArrayRef<uint8_t> symsBuffer; |
| 665 | BinaryStreamRef sr = ss.getRecordData(); |
| 666 | cantFail(Err: sr.readBytes(Offset: 0, Size: sr.getLength(), Buffer&: symsBuffer)); |
| 667 | auto ec = forEachCodeViewRecord<CVSymbol>( |
| 668 | StreamBuffer: symsBuffer, F: [&](CVSymbol sym) -> llvm::Error { |
| 669 | // Track the current scope. Only update records in the postmerge |
| 670 | // pass. |
| 671 | if (symbolOpensScope(Kind: sym.kind())) |
| 672 | scopeStackOpen(stack&: scopes, storage); |
| 673 | else if (symbolEndsScope(Kind: sym.kind())) |
| 674 | scopeStackClose(ctx, stack&: scopes, storage, storageBaseOffset: moduleSymStart, file); |
| 675 | |
| 676 | // Copy, relocate, and rewrite each module symbol. |
| 677 | if (symbolGoesInModuleStream(sym, symbolScopeDepth: scopes.size())) { |
| 678 | uint32_t alignedSize = |
| 679 | alignTo(Value: sym.length(), Align: alignOf(Container: CodeViewContainer::Pdb)); |
| 680 | writeSymbolRecord(debugChunk, sectionContents, sym, alignedSize, |
| 681 | nextRelocIndex, storage); |
| 682 | } |
| 683 | return Error::success(); |
| 684 | }); |
| 685 | |
| 686 | // If we encounter corrupt records in the second pass, ignore them. We |
| 687 | // already warned about them in the first analysis pass. |
| 688 | if (ec) { |
| 689 | consumeError(Err: std::move(ec)); |
| 690 | storage.clear(); |
| 691 | } |
| 692 | |
| 693 | // Writing bytes has a very high overhead, so write the entire subsection |
| 694 | // at once. |
| 695 | // TODO: Consider buffering symbols for the entire object file to reduce |
| 696 | // overhead even further. |
| 697 | if (Error e = writer.writeBytes(Buffer: storage)) |
| 698 | return e; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return Error::success(); |
| 703 | } |
| 704 | |
| 705 | Error PDBLinker::commitSymbolsForObject(void *ctx, void *obj, |
| 706 | BinaryStreamWriter &writer) { |
| 707 | return static_cast<PDBLinker *>(ctx)->writeAllModuleSymbolRecords( |
| 708 | file: static_cast<ObjFile *>(obj), writer); |
| 709 | } |
| 710 | |
| 711 | static pdb::SectionContrib createSectionContrib(COFFLinkerContext &ctx, |
| 712 | const Chunk *c, uint32_t modi) { |
| 713 | OutputSection *os = c ? ctx.getOutputSection(c) : nullptr; |
| 714 | pdb::SectionContrib sc; |
| 715 | memset(s: &sc, c: 0, n: sizeof(sc)); |
| 716 | sc.ISect = os ? os->sectionIndex : llvm::pdb::kInvalidStreamIndex; |
| 717 | sc.Off = c && os ? c->getRVA() - os->getRVA() : 0; |
| 718 | sc.Size = c ? c->getSize() : -1; |
| 719 | if (auto *secChunk = dyn_cast_or_null<SectionChunk>(Val: c)) { |
| 720 | sc.Characteristics = secChunk->header->Characteristics; |
| 721 | sc.Imod = secChunk->file->moduleDBI->getModuleIndex(); |
| 722 | ArrayRef<uint8_t> contents = secChunk->getContents(); |
| 723 | JamCRC crc(0); |
| 724 | crc.update(Data: contents); |
| 725 | sc.DataCrc = crc.getCRC(); |
| 726 | } else { |
| 727 | sc.Characteristics = os ? os->header.Characteristics : 0; |
| 728 | sc.Imod = modi; |
| 729 | } |
| 730 | sc.RelocCrc = 0; // FIXME |
| 731 | |
| 732 | return sc; |
| 733 | } |
| 734 | |
| 735 | static uint32_t |
| 736 | translateStringTableIndex(COFFLinkerContext &ctx, uint32_t objIndex, |
| 737 | const DebugStringTableSubsectionRef &objStrTable, |
| 738 | DebugStringTableSubsection &pdbStrTable) { |
| 739 | auto expectedString = objStrTable.getString(Offset: objIndex); |
| 740 | if (!expectedString) { |
| 741 | Warn(ctx) << "Invalid string table reference" ; |
| 742 | consumeError(Err: expectedString.takeError()); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | return pdbStrTable.insert(S: *expectedString); |
| 747 | } |
| 748 | |
| 749 | void DebugSHandler::handleDebugS(SectionChunk *debugChunk) { |
| 750 | // Note that we are processing the *unrelocated* section contents. They will |
| 751 | // be relocated later during PDB writing. |
| 752 | ArrayRef<uint8_t> contents = debugChunk->getContents(); |
| 753 | contents = SectionChunk::consumeDebugMagic(data: contents, sectionName: ".debug$S" ); |
| 754 | DebugSubsectionArray subsections; |
| 755 | BinaryStreamReader reader(contents, llvm::endianness::little); |
| 756 | ExitOnError exitOnErr; |
| 757 | exitOnErr(reader.readArray(Array&: subsections, Size: contents.size())); |
| 758 | debugChunk->sortRelocations(); |
| 759 | |
| 760 | // Reset the relocation index, since this is a new section. |
| 761 | nextRelocIndex = 0; |
| 762 | |
| 763 | for (const DebugSubsectionRecord &ss : subsections) { |
| 764 | // Ignore subsections with the 'ignore' bit. Some versions of the Visual C++ |
| 765 | // runtime have subsections with this bit set. |
| 766 | if (uint32_t(ss.kind()) & codeview::SubsectionIgnoreFlag) |
| 767 | continue; |
| 768 | |
| 769 | switch (ss.kind()) { |
| 770 | case DebugSubsectionKind::StringTable: { |
| 771 | assert(!cvStrTab.valid() && |
| 772 | "Encountered multiple string table subsections!" ); |
| 773 | exitOnErr(cvStrTab.initialize(Contents: ss.getRecordData())); |
| 774 | break; |
| 775 | } |
| 776 | case DebugSubsectionKind::FileChecksums: |
| 777 | assert(!checksums.valid() && |
| 778 | "Encountered multiple checksum subsections!" ); |
| 779 | exitOnErr(checksums.initialize(Stream: ss.getRecordData())); |
| 780 | break; |
| 781 | case DebugSubsectionKind::Lines: |
| 782 | case DebugSubsectionKind::InlineeLines: |
| 783 | addUnrelocatedSubsection(debugChunk, ss); |
| 784 | break; |
| 785 | case DebugSubsectionKind::FrameData: |
| 786 | addFrameDataSubsection(debugChunk, ss); |
| 787 | break; |
| 788 | case DebugSubsectionKind::Symbols: |
| 789 | linker.analyzeSymbolSubsection(debugChunk, moduleSymOffset&: moduleStreamSize, |
| 790 | nextRelocIndex, stringTableFixups, |
| 791 | symData: ss.getRecordData()); |
| 792 | break; |
| 793 | |
| 794 | case DebugSubsectionKind::CrossScopeImports: |
| 795 | case DebugSubsectionKind::CrossScopeExports: |
| 796 | // These appear to relate to cross-module optimization, so we might use |
| 797 | // these for ThinLTO. |
| 798 | break; |
| 799 | |
| 800 | case DebugSubsectionKind::ILLines: |
| 801 | case DebugSubsectionKind::FuncMDTokenMap: |
| 802 | case DebugSubsectionKind::TypeMDTokenMap: |
| 803 | case DebugSubsectionKind::MergedAssemblyInput: |
| 804 | // These appear to relate to .Net assembly info. |
| 805 | break; |
| 806 | |
| 807 | case DebugSubsectionKind::CoffSymbolRVA: |
| 808 | // Unclear what this is for. |
| 809 | break; |
| 810 | |
| 811 | case DebugSubsectionKind::XfgHashType: |
| 812 | case DebugSubsectionKind::XfgHashVirtual: |
| 813 | break; |
| 814 | |
| 815 | default: |
| 816 | Warn(ctx) << "ignoring unknown debug$S subsection kind 0x" |
| 817 | << utohexstr(X: uint32_t(ss.kind())) << " in file " |
| 818 | << toString(file: &file); |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | void DebugSHandler::advanceRelocIndex(SectionChunk *sc, |
| 825 | ArrayRef<uint8_t> subsec) { |
| 826 | ptrdiff_t vaBegin = subsec.data() - sc->getContents().data(); |
| 827 | assert(vaBegin > 0); |
| 828 | auto relocs = sc->getRelocs(); |
| 829 | for (; nextRelocIndex < relocs.size(); ++nextRelocIndex) { |
| 830 | if (relocs[nextRelocIndex].VirtualAddress >= (uint32_t)vaBegin) |
| 831 | break; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | namespace { |
| 836 | /// Wrapper class for unrelocated line and inlinee line subsections, which |
| 837 | /// require only relocation and type index remapping to add to the PDB. |
| 838 | class UnrelocatedDebugSubsection : public DebugSubsection { |
| 839 | public: |
| 840 | UnrelocatedDebugSubsection(DebugSubsectionKind k, SectionChunk *debugChunk, |
| 841 | ArrayRef<uint8_t> subsec, uint32_t relocIndex) |
| 842 | : DebugSubsection(k), debugChunk(debugChunk), subsec(subsec), |
| 843 | relocIndex(relocIndex) {} |
| 844 | |
| 845 | Error commit(BinaryStreamWriter &writer) const override; |
| 846 | uint32_t calculateSerializedSize() const override { return subsec.size(); } |
| 847 | |
| 848 | SectionChunk *debugChunk; |
| 849 | ArrayRef<uint8_t> subsec; |
| 850 | uint32_t relocIndex; |
| 851 | }; |
| 852 | } // namespace |
| 853 | |
| 854 | Error UnrelocatedDebugSubsection::commit(BinaryStreamWriter &writer) const { |
| 855 | std::vector<uint8_t> relocatedBytes(subsec.size()); |
| 856 | uint32_t tmpRelocIndex = relocIndex; |
| 857 | debugChunk->writeAndRelocateSubsection(sec: debugChunk->getContents(), subsec, |
| 858 | nextRelocIndex&: tmpRelocIndex, buf: relocatedBytes.data()); |
| 859 | |
| 860 | // Remap type indices in inlinee line records in place. Skip the remapping if |
| 861 | // there is no type source info. |
| 862 | if (kind() == DebugSubsectionKind::InlineeLines && |
| 863 | debugChunk->file->debugTypesObj) { |
| 864 | TpiSource *source = debugChunk->file->debugTypesObj; |
| 865 | DebugInlineeLinesSubsectionRef inlineeLines; |
| 866 | BinaryStreamReader storageReader(relocatedBytes, llvm::endianness::little); |
| 867 | ExitOnError exitOnErr; |
| 868 | exitOnErr(inlineeLines.initialize(Reader: storageReader)); |
| 869 | for (const InlineeSourceLine &line : inlineeLines) { |
| 870 | TypeIndex &inlinee = *const_cast<TypeIndex *>(&line.Header->Inlinee); |
| 871 | if (!source->remapTypeIndex(ti&: inlinee, refKind: TiRefKind::IndexRef)) { |
| 872 | log(msg: "bad inlinee line record in " + debugChunk->file->getName() + |
| 873 | " with bad inlinee index 0x" + utohexstr(X: inlinee.getIndex())); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | return writer.writeBytes(Buffer: relocatedBytes); |
| 879 | } |
| 880 | |
| 881 | void DebugSHandler::addUnrelocatedSubsection(SectionChunk *debugChunk, |
| 882 | const DebugSubsectionRecord &ss) { |
| 883 | ArrayRef<uint8_t> subsec; |
| 884 | BinaryStreamRef sr = ss.getRecordData(); |
| 885 | cantFail(Err: sr.readBytes(Offset: 0, Size: sr.getLength(), Buffer&: subsec)); |
| 886 | advanceRelocIndex(sc: debugChunk, subsec); |
| 887 | file.moduleDBI->addDebugSubsection( |
| 888 | Subsection: std::make_shared<UnrelocatedDebugSubsection>(args: ss.kind(), args&: debugChunk, |
| 889 | args&: subsec, args&: nextRelocIndex)); |
| 890 | } |
| 891 | |
| 892 | void DebugSHandler::addFrameDataSubsection(SectionChunk *debugChunk, |
| 893 | const DebugSubsectionRecord &ss) { |
| 894 | // We need to re-write string table indices here, so save off all |
| 895 | // frame data subsections until we've processed the entire list of |
| 896 | // subsections so that we can be sure we have the string table. |
| 897 | ArrayRef<uint8_t> subsec; |
| 898 | BinaryStreamRef sr = ss.getRecordData(); |
| 899 | cantFail(Err: sr.readBytes(Offset: 0, Size: sr.getLength(), Buffer&: subsec)); |
| 900 | advanceRelocIndex(sc: debugChunk, subsec); |
| 901 | frameDataSubsecs.push_back(x: {.debugChunk: debugChunk, .subsecData: subsec, .relocIndex: nextRelocIndex}); |
| 902 | } |
| 903 | |
| 904 | static Expected<StringRef> |
| 905 | getFileName(const DebugStringTableSubsectionRef &strings, |
| 906 | const DebugChecksumsSubsectionRef &checksums, uint32_t fileID) { |
| 907 | auto iter = checksums.getArray().at(Offset: fileID); |
| 908 | if (iter == checksums.getArray().end()) |
| 909 | return make_error<CodeViewError>(Args: cv_error_code::no_records); |
| 910 | uint32_t offset = iter->FileNameOffset; |
| 911 | return strings.getString(Offset: offset); |
| 912 | } |
| 913 | |
| 914 | void DebugSHandler::finish() { |
| 915 | pdb::DbiStreamBuilder &dbiBuilder = linker.builder.getDbiBuilder(); |
| 916 | |
| 917 | // If we found any symbol records for the module symbol stream, defer them. |
| 918 | if (moduleStreamSize > kSymbolStreamMagicSize) |
| 919 | file.moduleDBI->addUnmergedSymbols(SymSrc: &file, SymLength: moduleStreamSize - |
| 920 | kSymbolStreamMagicSize); |
| 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(msg: ".debug$S sections with a checksums subsection must also contain a " |
| 928 | "string table subsection" ); |
| 929 | |
| 930 | if (!stringTableFixups.empty()) |
| 931 | Warn(ctx) |
| 932 | << "No StringTable subsection was encountered, but there are string " |
| 933 | "table references" ; |
| 934 | return; |
| 935 | } |
| 936 | |
| 937 | ExitOnError exitOnErr; |
| 938 | |
| 939 | // Handle FPO data. Each subsection begins with a single image base |
| 940 | // relocation, which is then added to the RvaStart of each frame data record |
| 941 | // when it is added to the PDB. The string table indices for the FPO program |
| 942 | // must also be rewritten to use the PDB string table. |
| 943 | for (const UnrelocatedFpoData &subsec : frameDataSubsecs) { |
| 944 | // Relocate the first four bytes of the subection and reinterpret them as a |
| 945 | // 32 bit little-endian integer. |
| 946 | SectionChunk *debugChunk = subsec.debugChunk; |
| 947 | ArrayRef<uint8_t> subsecData = subsec.subsecData; |
| 948 | uint32_t relocIndex = subsec.relocIndex; |
| 949 | auto unrelocatedRvaStart = subsecData.take_front(N: sizeof(uint32_t)); |
| 950 | uint8_t relocatedRvaStart[sizeof(uint32_t)]; |
| 951 | debugChunk->writeAndRelocateSubsection(sec: debugChunk->getContents(), |
| 952 | subsec: unrelocatedRvaStart, nextRelocIndex&: relocIndex, |
| 953 | buf: &relocatedRvaStart[0]); |
| 954 | // Use of memcpy here avoids violating type-based aliasing rules. |
| 955 | support::ulittle32_t rvaStart; |
| 956 | memcpy(dest: &rvaStart, src: &relocatedRvaStart[0], n: sizeof(support::ulittle32_t)); |
| 957 | |
| 958 | // Copy each frame data record, add in rvaStart, translate string table |
| 959 | // indices, and add the record to the PDB. |
| 960 | DebugFrameDataSubsectionRef fds; |
| 961 | BinaryStreamReader reader(subsecData, llvm::endianness::little); |
| 962 | exitOnErr(fds.initialize(Reader: reader)); |
| 963 | for (codeview::FrameData fd : fds) { |
| 964 | fd.RvaStart += rvaStart; |
| 965 | fd.FrameFunc = translateStringTableIndex(ctx, objIndex: fd.FrameFunc, objStrTable: cvStrTab, |
| 966 | pdbStrTable&: linker.pdbStrTab); |
| 967 | dbiBuilder.addNewFpoData(FD: fd); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // Translate the fixups and pass them off to the module builder so they will |
| 972 | // be applied during writing. |
| 973 | for (StringTableFixup &ref : stringTableFixups) { |
| 974 | ref.StrTabOffset = translateStringTableIndex(ctx, objIndex: ref.StrTabOffset, |
| 975 | objStrTable: cvStrTab, pdbStrTable&: linker.pdbStrTab); |
| 976 | } |
| 977 | file.moduleDBI->setStringTableFixups(std::move(stringTableFixups)); |
| 978 | |
| 979 | // Make a new file checksum table that refers to offsets in the PDB-wide |
| 980 | // string table. Generally the string table subsection appears after the |
| 981 | // checksum table, so we have to do this after looping over all the |
| 982 | // subsections. The new checksum table must have the exact same layout and |
| 983 | // size as the original. Otherwise, the file references in the line and |
| 984 | // inlinee line tables will be incorrect. |
| 985 | auto newChecksums = std::make_unique<DebugChecksumsSubsection>(args&: linker.pdbStrTab); |
| 986 | for (const FileChecksumEntry &fc : checksums) { |
| 987 | SmallString<128> filename = |
| 988 | exitOnErr(cvStrTab.getString(Offset: fc.FileNameOffset)); |
| 989 | linker.pdbMakeAbsolute(fileName&: filename); |
| 990 | exitOnErr(dbiBuilder.addModuleSourceFile(Module&: *file.moduleDBI, File: filename)); |
| 991 | newChecksums->addChecksum(FileName: filename, Kind: fc.Kind, Bytes: fc.Checksum); |
| 992 | } |
| 993 | assert(checksums.getArray().getUnderlyingStream().getLength() == |
| 994 | newChecksums->calculateSerializedSize() && |
| 995 | "file checksum table must have same layout" ); |
| 996 | |
| 997 | file.moduleDBI->addDebugSubsection(Subsection: std::move(newChecksums)); |
| 998 | } |
| 999 | |
| 1000 | static void warnUnusable(InputFile *f, Error e, bool shouldWarn) { |
| 1001 | if (!shouldWarn) { |
| 1002 | consumeError(Err: std::move(e)); |
| 1003 | return; |
| 1004 | } |
| 1005 | auto diag = Warn(ctx&: f->symtab.ctx); |
| 1006 | diag << "Cannot use debug info for '" << f << "' [LNK4099]" ; |
| 1007 | if (e) |
| 1008 | diag << "\n>>> failed to load reference " << std::move(e); |
| 1009 | } |
| 1010 | |
| 1011 | // Allocate memory for a .debug$S / .debug$F section and relocate it. |
| 1012 | static ArrayRef<uint8_t> relocateDebugChunk(SectionChunk &debugChunk) { |
| 1013 | uint8_t *buffer = bAlloc().Allocate<uint8_t>(Num: debugChunk.getSize()); |
| 1014 | assert(debugChunk.getOutputSectionIdx() == 0 && |
| 1015 | "debug sections should not be in output sections" ); |
| 1016 | debugChunk.writeTo(buf: buffer); |
| 1017 | return ArrayRef(buffer, debugChunk.getSize()); |
| 1018 | } |
| 1019 | |
| 1020 | void PDBLinker::addDebugSymbols(TpiSource *source) { |
| 1021 | // If this TpiSource doesn't have an object file, it must be from a type |
| 1022 | // server PDB. Type server PDBs do not contain symbols, so stop here. |
| 1023 | if (!source->file) |
| 1024 | return; |
| 1025 | |
| 1026 | llvm::TimeTraceScope timeScope("Merge symbols" ); |
| 1027 | ScopedTimer t(ctx.symbolMergingTimer); |
| 1028 | ExitOnError exitOnErr; |
| 1029 | pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); |
| 1030 | DebugSHandler dsh(ctx, *this, *source->file); |
| 1031 | // Now do all live .debug$S and .debug$F sections. |
| 1032 | for (SectionChunk *debugChunk : source->file->getDebugChunks()) { |
| 1033 | if (!debugChunk->live || debugChunk->getSize() == 0) |
| 1034 | continue; |
| 1035 | |
| 1036 | bool isDebugS = debugChunk->getSectionName() == ".debug$S" ; |
| 1037 | bool isDebugF = debugChunk->getSectionName() == ".debug$F" ; |
| 1038 | if (!isDebugS && !isDebugF) |
| 1039 | continue; |
| 1040 | |
| 1041 | if (isDebugS) { |
| 1042 | dsh.handleDebugS(debugChunk); |
| 1043 | } else if (isDebugF) { |
| 1044 | // Handle old FPO data .debug$F sections. These are relatively rare. |
| 1045 | ArrayRef<uint8_t> relocatedDebugContents = |
| 1046 | relocateDebugChunk(debugChunk&: *debugChunk); |
| 1047 | FixedStreamArray<object::FpoData> fpoRecords; |
| 1048 | BinaryStreamReader reader(relocatedDebugContents, |
| 1049 | llvm::endianness::little); |
| 1050 | uint32_t count = relocatedDebugContents.size() / sizeof(object::FpoData); |
| 1051 | exitOnErr(reader.readArray(Array&: fpoRecords, NumItems: count)); |
| 1052 | |
| 1053 | // These are already relocated and don't refer to the string table, so we |
| 1054 | // can just copy it. |
| 1055 | for (const object::FpoData &fd : fpoRecords) |
| 1056 | dbiBuilder.addOldFpoData(Fpo: fd); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // Do any post-processing now that all .debug$S sections have been processed. |
| 1061 | dsh.finish(); |
| 1062 | } |
| 1063 | |
| 1064 | // Add a module descriptor for every object file. We need to put an absolute |
| 1065 | // path to the object into the PDB. If this is a plain object, we make its |
| 1066 | // path absolute. If it's an object in an archive, we make the archive path |
| 1067 | // absolute. |
| 1068 | void PDBLinker::createModuleDBI(ObjFile *file) { |
| 1069 | pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); |
| 1070 | SmallString<128> objName; |
| 1071 | ExitOnError exitOnErr; |
| 1072 | |
| 1073 | bool inArchive = !file->parentName.empty(); |
| 1074 | objName = inArchive ? file->parentName : file->getName(); |
| 1075 | pdbMakeAbsolute(fileName&: objName); |
| 1076 | StringRef modName = inArchive ? file->getName() : objName.str(); |
| 1077 | |
| 1078 | file->moduleDBI = &exitOnErr(dbiBuilder.addModuleInfo(ModuleName: modName)); |
| 1079 | file->moduleDBI->setObjFileName(objName); |
| 1080 | file->moduleDBI->setMergeSymbolsCallback(Ctx: this, Callback: &commitSymbolsForObject); |
| 1081 | |
| 1082 | ArrayRef<Chunk *> chunks = file->getChunks(); |
| 1083 | uint32_t modi = file->moduleDBI->getModuleIndex(); |
| 1084 | |
| 1085 | for (Chunk *c : chunks) { |
| 1086 | auto *secChunk = dyn_cast<SectionChunk>(Val: c); |
| 1087 | if (!secChunk || !secChunk->live) |
| 1088 | continue; |
| 1089 | pdb::SectionContrib sc = createSectionContrib(ctx, c: secChunk, modi); |
| 1090 | file->moduleDBI->setFirstSectionContrib(sc); |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | void PDBLinker::addDebug(TpiSource *source) { |
| 1096 | // Before we can process symbol substreams from .debug$S, we need to process |
| 1097 | // type information, file checksums, and the string table. Add type info to |
| 1098 | // the PDB first, so that we can get the map from object file type and item |
| 1099 | // indices to PDB type and item indices. If we are using ghashes, types have |
| 1100 | // already been merged. |
| 1101 | if (!ctx.config.debugGHashes) { |
| 1102 | llvm::TimeTraceScope timeScope("Merge types (Non-GHASH)" ); |
| 1103 | ScopedTimer t(ctx.typeMergingTimer); |
| 1104 | if (Error e = source->mergeDebugT(m: &tMerger)) { |
| 1105 | // If type merging failed, ignore the symbols. |
| 1106 | warnUnusable(f: source->file, e: std::move(e), |
| 1107 | shouldWarn: ctx.config.warnDebugInfoUnusable); |
| 1108 | return; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // If type merging failed, ignore the symbols. |
| 1113 | Error typeError = std::move(source->typeMergingError); |
| 1114 | if (typeError) { |
| 1115 | warnUnusable(f: source->file, e: std::move(typeError), |
| 1116 | shouldWarn: ctx.config.warnDebugInfoUnusable); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | addDebugSymbols(source); |
| 1121 | } |
| 1122 | |
| 1123 | static pdb::BulkPublic createPublic(COFFLinkerContext &ctx, Defined *def) { |
| 1124 | pdb::BulkPublic pub; |
| 1125 | pub.Name = def->getName().data(); |
| 1126 | pub.NameLen = def->getName().size(); |
| 1127 | |
| 1128 | PublicSymFlags flags = PublicSymFlags::None; |
| 1129 | if (auto *d = dyn_cast<DefinedCOFF>(Val: def)) { |
| 1130 | if (d->getCOFFSymbol().isFunctionDefinition()) |
| 1131 | flags = PublicSymFlags::Function; |
| 1132 | } else if (isa<DefinedImportThunk>(Val: def)) { |
| 1133 | flags = PublicSymFlags::Function; |
| 1134 | } |
| 1135 | pub.setFlags(flags); |
| 1136 | |
| 1137 | OutputSection *os = ctx.getOutputSection(c: def->getChunk()); |
| 1138 | assert(os && "all publics should be in final image" ); |
| 1139 | pub.Offset = def->getRVA() - os->getRVA(); |
| 1140 | pub.Segment = os->sectionIndex; |
| 1141 | return pub; |
| 1142 | } |
| 1143 | |
| 1144 | // Add all object files to the PDB. Merge .debug$T sections into IpiData and |
| 1145 | // TpiData. |
| 1146 | void PDBLinker::addObjectsToPDB() { |
| 1147 | { |
| 1148 | llvm::TimeTraceScope timeScope("Add objects to PDB" ); |
| 1149 | ScopedTimer t1(ctx.addObjectsTimer); |
| 1150 | |
| 1151 | // Create module descriptors |
| 1152 | for (ObjFile *obj : ctx.objFileInstances) |
| 1153 | createModuleDBI(file: obj); |
| 1154 | |
| 1155 | // Reorder dependency type sources to come first. |
| 1156 | tMerger.sortDependencies(); |
| 1157 | |
| 1158 | // Merge type information from input files using global type hashing. |
| 1159 | if (ctx.config.debugGHashes) |
| 1160 | tMerger.mergeTypesWithGHash(); |
| 1161 | |
| 1162 | // Merge dependencies and then regular objects. |
| 1163 | { |
| 1164 | llvm::TimeTraceScope timeScope("Merge debug info (dependencies)" ); |
| 1165 | for (TpiSource *source : tMerger.dependencySources) |
| 1166 | addDebug(source); |
| 1167 | } |
| 1168 | { |
| 1169 | llvm::TimeTraceScope timeScope("Merge debug info (objects)" ); |
| 1170 | for (TpiSource *source : tMerger.objectSources) |
| 1171 | addDebug(source); |
| 1172 | } |
| 1173 | |
| 1174 | builder.getStringTableBuilder().setStrings(pdbStrTab); |
| 1175 | } |
| 1176 | |
| 1177 | // Construct TPI and IPI stream contents. |
| 1178 | { |
| 1179 | llvm::TimeTraceScope timeScope("TPI/IPI stream layout" ); |
| 1180 | ScopedTimer t2(ctx.tpiStreamLayoutTimer); |
| 1181 | |
| 1182 | // Collect all the merged types. |
| 1183 | if (ctx.config.debugGHashes) { |
| 1184 | addGHashTypeInfo(ctx, builder); |
| 1185 | } else { |
| 1186 | addTypeInfo(tpiBuilder&: builder.getTpiBuilder(), typeTable&: tMerger.getTypeTable()); |
| 1187 | addTypeInfo(tpiBuilder&: builder.getIpiBuilder(), typeTable&: tMerger.getIDTable()); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | if (ctx.config.showSummary) { |
| 1192 | for (TpiSource *source : ctx.tpiSourceList) { |
| 1193 | nbTypeRecords += source->nbTypeRecords; |
| 1194 | nbTypeRecordsBytes += source->nbTypeRecordsBytes; |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | void PDBLinker::addPublicsToPDB() { |
| 1200 | llvm::TimeTraceScope timeScope("Publics layout" ); |
| 1201 | ScopedTimer t3(ctx.publicsLayoutTimer); |
| 1202 | // Compute the public symbols. |
| 1203 | auto &gsiBuilder = builder.getGsiBuilder(); |
| 1204 | std::vector<pdb::BulkPublic> publics; |
| 1205 | ctx.symtab.forEachSymbol(callback: [&publics, this](Symbol *s) { |
| 1206 | // Only emit external, defined, live symbols that have a chunk. Static, |
| 1207 | // non-external symbols do not appear in the symbol table. |
| 1208 | auto *def = dyn_cast<Defined>(Val: s); |
| 1209 | if (def && def->isLive() && def->getChunk()) { |
| 1210 | // Don't emit a public symbol for coverage data symbols. LLVM code |
| 1211 | // coverage (and PGO) create a __profd_ and __profc_ symbol for every |
| 1212 | // function. C++ mangled names are long, and tend to dominate symbol size. |
| 1213 | // Including these names triples the size of the public stream, which |
| 1214 | // results in bloated PDB files. These symbols generally are not helpful |
| 1215 | // for debugging, so suppress them. |
| 1216 | StringRef name = def->getName(); |
| 1217 | if (name.data()[0] == '_' && name.data()[1] == '_') { |
| 1218 | // Drop the '_' prefix for x86. |
| 1219 | if (ctx.config.machine == I386) |
| 1220 | name = name.drop_front(N: 1); |
| 1221 | if (name.starts_with(Prefix: "__profd_" ) || name.starts_with(Prefix: "__profc_" ) || |
| 1222 | name.starts_with(Prefix: "__covrec_" )) { |
| 1223 | return; |
| 1224 | } |
| 1225 | } |
| 1226 | publics.push_back(x: createPublic(ctx, def)); |
| 1227 | } |
| 1228 | }); |
| 1229 | |
| 1230 | if (!publics.empty()) { |
| 1231 | publicSymbols = publics.size(); |
| 1232 | gsiBuilder.addPublicSymbols(PublicsIn: std::move(publics)); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | void PDBLinker::printStats() { |
| 1237 | if (!ctx.config.showSummary) |
| 1238 | return; |
| 1239 | |
| 1240 | SmallString<256> buffer; |
| 1241 | raw_svector_ostream stream(buffer); |
| 1242 | |
| 1243 | stream << center_justify(Str: "Summary" , Width: 80) << '\n' |
| 1244 | << std::string(80, '-') << '\n'; |
| 1245 | |
| 1246 | auto print = [&](uint64_t v, StringRef s) { |
| 1247 | stream << format_decimal(N: v, Width: 15) << " " << s << '\n'; |
| 1248 | }; |
| 1249 | |
| 1250 | print(ctx.objFileInstances.size(), |
| 1251 | "Input OBJ files (expanded from all cmd-line inputs)" ); |
| 1252 | print(ctx.typeServerSourceMappings.size(), "PDB type server dependencies" ); |
| 1253 | print(ctx.precompSourceMappings.size(), "Precomp OBJ dependencies" ); |
| 1254 | print(nbTypeRecords, "Input type records" ); |
| 1255 | print(nbTypeRecordsBytes, "Input type records bytes" ); |
| 1256 | print(builder.getTpiBuilder().getRecordCount(), "Merged TPI records" ); |
| 1257 | print(builder.getIpiBuilder().getRecordCount(), "Merged IPI records" ); |
| 1258 | print(pdbStrTab.size(), "Output PDB strings" ); |
| 1259 | print(globalSymbols, "Global symbol records" ); |
| 1260 | print(moduleSymbols, "Module symbol records" ); |
| 1261 | print(publicSymbols, "Public symbol records" ); |
| 1262 | |
| 1263 | auto printLargeInputTypeRecs = [&](StringRef name, |
| 1264 | ArrayRef<uint32_t> recCounts, |
| 1265 | TypeCollection &records) { |
| 1266 | // Figure out which type indices were responsible for the most duplicate |
| 1267 | // bytes in the input files. These should be frequently emitted LF_CLASS and |
| 1268 | // LF_FIELDLIST records. |
| 1269 | struct TypeSizeInfo { |
| 1270 | uint32_t typeSize; |
| 1271 | uint32_t dupCount; |
| 1272 | TypeIndex typeIndex; |
| 1273 | uint64_t totalInputSize() const { return uint64_t(dupCount) * typeSize; } |
| 1274 | bool operator<(const TypeSizeInfo &rhs) const { |
| 1275 | if (totalInputSize() == rhs.totalInputSize()) |
| 1276 | return typeIndex < rhs.typeIndex; |
| 1277 | return totalInputSize() < rhs.totalInputSize(); |
| 1278 | } |
| 1279 | }; |
| 1280 | SmallVector<TypeSizeInfo, 0> tsis; |
| 1281 | for (auto e : enumerate(First&: recCounts)) { |
| 1282 | TypeIndex typeIndex = TypeIndex::fromArrayIndex(Index: e.index()); |
| 1283 | uint32_t typeSize = records.getType(Index: typeIndex).length(); |
| 1284 | uint32_t dupCount = e.value(); |
| 1285 | tsis.push_back(Elt: {.typeSize: typeSize, .dupCount: dupCount, .typeIndex: typeIndex}); |
| 1286 | } |
| 1287 | |
| 1288 | if (!tsis.empty()) { |
| 1289 | stream << "\nTop 10 types responsible for the most " << name |
| 1290 | << " input:\n" ; |
| 1291 | stream << " index total bytes count size\n" ; |
| 1292 | llvm::sort(C&: tsis); |
| 1293 | unsigned i = 0; |
| 1294 | for (const auto &tsi : reverse(C&: tsis)) { |
| 1295 | stream << formatv(Fmt: " {0,10:X}: {1,14:N} = {2,5:N} * {3,6:N}\n" , |
| 1296 | Vals: tsi.typeIndex.getIndex(), Vals: tsi.totalInputSize(), |
| 1297 | Vals: tsi.dupCount, Vals: tsi.typeSize); |
| 1298 | if (++i >= 10) |
| 1299 | break; |
| 1300 | } |
| 1301 | stream |
| 1302 | << "Run llvm-pdbutil to print details about a particular record:\n" ; |
| 1303 | stream << formatv(Fmt: "llvm-pdbutil dump -{0}s -{0}-index {1:X} {2}\n" , |
| 1304 | Vals: (name == "TPI" ? "type" : "id" ), |
| 1305 | Vals: tsis.back().typeIndex.getIndex(), Vals&: ctx.config.pdbPath); |
| 1306 | } |
| 1307 | }; |
| 1308 | |
| 1309 | if (!ctx.config.debugGHashes) { |
| 1310 | // FIXME: Reimplement for ghash. |
| 1311 | printLargeInputTypeRecs("TPI" , tMerger.tpiCounts, tMerger.getTypeTable()); |
| 1312 | printLargeInputTypeRecs("IPI" , tMerger.ipiCounts, tMerger.getIDTable()); |
| 1313 | } |
| 1314 | |
| 1315 | Msg(ctx) << buffer; |
| 1316 | } |
| 1317 | |
| 1318 | void PDBLinker::addNatvisFiles() { |
| 1319 | llvm::TimeTraceScope timeScope("Natvis files" ); |
| 1320 | for (StringRef file : ctx.config.natvisFiles) { |
| 1321 | ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr = |
| 1322 | MemoryBuffer::getFile(Filename: file); |
| 1323 | if (!dataOrErr) { |
| 1324 | Warn(ctx) << "Cannot open input file: " << file; |
| 1325 | continue; |
| 1326 | } |
| 1327 | std::unique_ptr<MemoryBuffer> data = std::move(*dataOrErr); |
| 1328 | |
| 1329 | // Can't use takeBuffer() here since addInjectedSource() takes ownership. |
| 1330 | if (ctx.driver.tar) |
| 1331 | ctx.driver.tar->append(Path: relativeToRoot(path: data->getBufferIdentifier()), |
| 1332 | Data: data->getBuffer()); |
| 1333 | |
| 1334 | builder.addInjectedSource(Name: file, Buffer: std::move(data)); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | void PDBLinker::addNamedStreams() { |
| 1339 | llvm::TimeTraceScope timeScope("Named streams" ); |
| 1340 | ExitOnError exitOnErr; |
| 1341 | for (const auto &streamFile : ctx.config.namedStreams) { |
| 1342 | const StringRef stream = streamFile.getKey(), file = streamFile.getValue(); |
| 1343 | ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr = |
| 1344 | MemoryBuffer::getFile(Filename: file); |
| 1345 | if (!dataOrErr) { |
| 1346 | Warn(ctx) << "Cannot open input file: " << file; |
| 1347 | continue; |
| 1348 | } |
| 1349 | std::unique_ptr<MemoryBuffer> data = std::move(*dataOrErr); |
| 1350 | exitOnErr(builder.addNamedStream(Name: stream, Data: data->getBuffer())); |
| 1351 | ctx.driver.takeBuffer(mb: std::move(data)); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | static codeview::CPUType toCodeViewMachine(COFF::MachineTypes machine) { |
| 1356 | switch (machine) { |
| 1357 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 1358 | return codeview::CPUType::X64; |
| 1359 | case COFF::IMAGE_FILE_MACHINE_ARM: |
| 1360 | return codeview::CPUType::ARM7; |
| 1361 | case COFF::IMAGE_FILE_MACHINE_ARM64: |
| 1362 | return codeview::CPUType::ARM64; |
| 1363 | case COFF::IMAGE_FILE_MACHINE_ARM64EC: |
| 1364 | return codeview::CPUType::ARM64EC; |
| 1365 | case COFF::IMAGE_FILE_MACHINE_ARM64X: |
| 1366 | return codeview::CPUType::ARM64X; |
| 1367 | case COFF::IMAGE_FILE_MACHINE_ARMNT: |
| 1368 | return codeview::CPUType::ARMNT; |
| 1369 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 1370 | return codeview::CPUType::Intel80386; |
| 1371 | default: |
| 1372 | llvm_unreachable("Unsupported CPU Type" ); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | // Mimic MSVC which surrounds arguments containing whitespace with quotes. |
| 1377 | // Double double-quotes are handled, so that the resulting string can be |
| 1378 | // executed again on the cmd-line. |
| 1379 | static std::string quote(ArrayRef<StringRef> args) { |
| 1380 | std::string r; |
| 1381 | r.reserve(res_arg: 256); |
| 1382 | for (StringRef a : args) { |
| 1383 | if (!r.empty()) |
| 1384 | r.push_back(c: ' '); |
| 1385 | bool hasWS = a.contains(C: ' '); |
| 1386 | bool hasQ = a.contains(C: '"'); |
| 1387 | if (hasWS || hasQ) |
| 1388 | r.push_back(c: '"'); |
| 1389 | if (hasQ) { |
| 1390 | SmallVector<StringRef, 4> s; |
| 1391 | a.split(A&: s, Separator: '"'); |
| 1392 | r.append(str: join(R&: s, Separator: "\"\"" )); |
| 1393 | } else { |
| 1394 | r.append(str: std::string(a)); |
| 1395 | } |
| 1396 | if (hasWS || hasQ) |
| 1397 | r.push_back(c: '"'); |
| 1398 | } |
| 1399 | return r; |
| 1400 | } |
| 1401 | |
| 1402 | static void fillLinkerVerRecord(Compile3Sym &cs, MachineTypes machine) { |
| 1403 | cs.Machine = toCodeViewMachine(machine); |
| 1404 | // Interestingly, if we set the string to 0.0.0.0, then when trying to view |
| 1405 | // local variables WinDbg emits an error that private symbols are not present. |
| 1406 | // By setting this to a valid MSVC linker version string, local variables are |
| 1407 | // displayed properly. As such, even though it is not representative of |
| 1408 | // LLVM's version information, we need this for compatibility. |
| 1409 | cs.Flags = CompileSym3Flags::None; |
| 1410 | cs.VersionBackendBuild = 25019; |
| 1411 | cs.VersionBackendMajor = 14; |
| 1412 | cs.VersionBackendMinor = 10; |
| 1413 | cs.VersionBackendQFE = 0; |
| 1414 | |
| 1415 | // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the |
| 1416 | // linker module (which is by definition a backend), so we don't need to do |
| 1417 | // anything here. Also, it seems we can use "LLVM Linker" for the linker name |
| 1418 | // without any problems. Only the backend version has to be hardcoded to a |
| 1419 | // magic number. |
| 1420 | cs.VersionFrontendBuild = 0; |
| 1421 | cs.VersionFrontendMajor = 0; |
| 1422 | cs.VersionFrontendMinor = 0; |
| 1423 | cs.VersionFrontendQFE = 0; |
| 1424 | cs.Version = "LLVM Linker" ; |
| 1425 | cs.setLanguage(SourceLanguage::Link); |
| 1426 | } |
| 1427 | |
| 1428 | void PDBLinker::addCommonLinkerModuleSymbols( |
| 1429 | StringRef path, pdb::DbiModuleDescriptorBuilder &mod) { |
| 1430 | ObjNameSym ons(SymbolRecordKind::ObjNameSym); |
| 1431 | EnvBlockSym ebs(SymbolRecordKind::EnvBlockSym); |
| 1432 | Compile3Sym cs(SymbolRecordKind::Compile3Sym); |
| 1433 | |
| 1434 | MachineTypes machine = ctx.config.machine; |
| 1435 | // MSVC uses the ARM64X machine type for ARM64EC targets in the common linker |
| 1436 | // module record. |
| 1437 | if (isArm64EC(Machine: machine)) |
| 1438 | machine = ARM64X; |
| 1439 | fillLinkerVerRecord(cs, machine); |
| 1440 | |
| 1441 | ons.Name = "* Linker *" ; |
| 1442 | ons.Signature = 0; |
| 1443 | |
| 1444 | ArrayRef<StringRef> args = ArrayRef(ctx.config.argv).drop_front(); |
| 1445 | std::string argStr = quote(args); |
| 1446 | ebs.Fields.push_back(x: "cwd" ); |
| 1447 | SmallString<64> cwd; |
| 1448 | if (ctx.config.pdbSourcePath.empty()) |
| 1449 | sys::fs::current_path(result&: cwd); |
| 1450 | else |
| 1451 | cwd = ctx.config.pdbSourcePath; |
| 1452 | ebs.Fields.push_back(x: cwd); |
| 1453 | ebs.Fields.push_back(x: "exe" ); |
| 1454 | SmallString<64> exe = ctx.config.argv[0]; |
| 1455 | pdbMakeAbsolute(fileName&: exe); |
| 1456 | ebs.Fields.push_back(x: exe); |
| 1457 | ebs.Fields.push_back(x: "pdb" ); |
| 1458 | ebs.Fields.push_back(x: path); |
| 1459 | ebs.Fields.push_back(x: "cmd" ); |
| 1460 | ebs.Fields.push_back(x: argStr); |
| 1461 | llvm::BumpPtrAllocator &bAlloc = lld::bAlloc(); |
| 1462 | mod.addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1463 | Sym&: ons, Storage&: bAlloc, Container: CodeViewContainer::Pdb)); |
| 1464 | mod.addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1465 | Sym&: cs, Storage&: bAlloc, Container: CodeViewContainer::Pdb)); |
| 1466 | mod.addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1467 | Sym&: ebs, Storage&: bAlloc, Container: CodeViewContainer::Pdb)); |
| 1468 | } |
| 1469 | |
| 1470 | static void addLinkerModuleCoffGroup(PartialSection *sec, |
| 1471 | pdb::DbiModuleDescriptorBuilder &mod, |
| 1472 | OutputSection &os) { |
| 1473 | // If there's a section, there's at least one chunk |
| 1474 | assert(!sec->chunks.empty()); |
| 1475 | const Chunk *firstChunk = *sec->chunks.begin(); |
| 1476 | const Chunk *lastChunk = *sec->chunks.rbegin(); |
| 1477 | |
| 1478 | // Emit COFF group |
| 1479 | CoffGroupSym cgs(SymbolRecordKind::CoffGroupSym); |
| 1480 | cgs.Name = sec->name; |
| 1481 | cgs.Segment = os.sectionIndex; |
| 1482 | cgs.Offset = firstChunk->getRVA() - os.getRVA(); |
| 1483 | cgs.Size = lastChunk->getRVA() + lastChunk->getSize() - firstChunk->getRVA(); |
| 1484 | cgs.Characteristics = sec->characteristics; |
| 1485 | |
| 1486 | // Somehow .idata sections & sections groups in the debug symbol stream have |
| 1487 | // the "write" flag set. However the section header for the corresponding |
| 1488 | // .idata section doesn't have it. |
| 1489 | if (cgs.Name.starts_with(Prefix: ".idata" )) |
| 1490 | cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE; |
| 1491 | |
| 1492 | mod.addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1493 | Sym&: cgs, Storage&: bAlloc(), Container: CodeViewContainer::Pdb)); |
| 1494 | } |
| 1495 | |
| 1496 | static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &mod, |
| 1497 | OutputSection &os, bool isMinGW) { |
| 1498 | SectionSym sym(SymbolRecordKind::SectionSym); |
| 1499 | sym.Alignment = 12; // 2^12 = 4KB |
| 1500 | sym.Characteristics = os.header.Characteristics; |
| 1501 | sym.Length = os.getVirtualSize(); |
| 1502 | sym.Name = os.name; |
| 1503 | sym.Rva = os.getRVA(); |
| 1504 | sym.SectionNumber = os.sectionIndex; |
| 1505 | mod.addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1506 | Sym&: sym, Storage&: bAlloc(), Container: CodeViewContainer::Pdb)); |
| 1507 | |
| 1508 | // Skip COFF groups in MinGW because it adds a significant footprint to the |
| 1509 | // PDB, due to each function being in its own section |
| 1510 | if (isMinGW) |
| 1511 | return; |
| 1512 | |
| 1513 | // Output COFF groups for individual chunks of this section. |
| 1514 | for (PartialSection *sec : os.contribSections) { |
| 1515 | addLinkerModuleCoffGroup(sec, mod, os); |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | // Add all import files as modules to the PDB. |
| 1520 | void PDBLinker::addImportFilesToPDB() { |
| 1521 | if (ctx.importFileInstances.empty()) |
| 1522 | return; |
| 1523 | |
| 1524 | llvm::TimeTraceScope timeScope("Import files" ); |
| 1525 | ExitOnError exitOnErr; |
| 1526 | std::map<std::string, llvm::pdb::DbiModuleDescriptorBuilder *> dllToModuleDbi; |
| 1527 | |
| 1528 | for (ImportFile *file : ctx.importFileInstances) { |
| 1529 | if (!file->live) |
| 1530 | continue; |
| 1531 | |
| 1532 | if (!file->thunkSym) |
| 1533 | continue; |
| 1534 | |
| 1535 | if (!file->thunkSym->isLive()) |
| 1536 | continue; |
| 1537 | |
| 1538 | std::string dll = StringRef(file->dllName).lower(); |
| 1539 | llvm::pdb::DbiModuleDescriptorBuilder *&mod = dllToModuleDbi[dll]; |
| 1540 | if (!mod) { |
| 1541 | pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); |
| 1542 | SmallString<128> libPath = file->parentName; |
| 1543 | pdbMakeAbsolute(fileName&: libPath); |
| 1544 | sys::path::native(path&: libPath); |
| 1545 | |
| 1546 | // Name modules similar to MSVC's link.exe. |
| 1547 | // The first module is the simple dll filename |
| 1548 | llvm::pdb::DbiModuleDescriptorBuilder &firstMod = |
| 1549 | exitOnErr(dbiBuilder.addModuleInfo(ModuleName: file->dllName)); |
| 1550 | firstMod.setObjFileName(libPath); |
| 1551 | pdb::SectionContrib sc = |
| 1552 | createSectionContrib(ctx, c: nullptr, modi: llvm::pdb::kInvalidStreamIndex); |
| 1553 | firstMod.setFirstSectionContrib(sc); |
| 1554 | |
| 1555 | // The second module is where the import stream goes. |
| 1556 | mod = &exitOnErr(dbiBuilder.addModuleInfo(ModuleName: "Import:" + file->dllName)); |
| 1557 | mod->setObjFileName(libPath); |
| 1558 | } |
| 1559 | |
| 1560 | DefinedImportThunk *thunk = cast<DefinedImportThunk>(Val: file->thunkSym); |
| 1561 | Chunk *thunkChunk = thunk->getChunk(); |
| 1562 | OutputSection *thunkOS = ctx.getOutputSection(c: thunkChunk); |
| 1563 | |
| 1564 | ObjNameSym ons(SymbolRecordKind::ObjNameSym); |
| 1565 | Compile3Sym cs(SymbolRecordKind::Compile3Sym); |
| 1566 | Thunk32Sym ts(SymbolRecordKind::Thunk32Sym); |
| 1567 | ScopeEndSym es(SymbolRecordKind::ScopeEndSym); |
| 1568 | |
| 1569 | ons.Name = file->dllName; |
| 1570 | ons.Signature = 0; |
| 1571 | |
| 1572 | fillLinkerVerRecord(cs, machine: ctx.config.machine); |
| 1573 | |
| 1574 | ts.Name = thunk->getName(); |
| 1575 | ts.Parent = 0; |
| 1576 | ts.End = 0; |
| 1577 | ts.Next = 0; |
| 1578 | ts.Thunk = ThunkOrdinal::Standard; |
| 1579 | ts.Length = thunkChunk->getSize(); |
| 1580 | ts.Segment = thunkOS->sectionIndex; |
| 1581 | ts.Offset = thunkChunk->getRVA() - thunkOS->getRVA(); |
| 1582 | |
| 1583 | llvm::BumpPtrAllocator &bAlloc = lld::bAlloc(); |
| 1584 | mod->addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1585 | Sym&: ons, Storage&: bAlloc, Container: CodeViewContainer::Pdb)); |
| 1586 | mod->addSymbol(Symbol: codeview::SymbolSerializer::writeOneSymbol( |
| 1587 | Sym&: cs, Storage&: bAlloc, Container: CodeViewContainer::Pdb)); |
| 1588 | |
| 1589 | CVSymbol newSym = codeview::SymbolSerializer::writeOneSymbol( |
| 1590 | Sym&: ts, Storage&: bAlloc, Container: CodeViewContainer::Pdb); |
| 1591 | |
| 1592 | // Write ptrEnd for the S_THUNK32. |
| 1593 | ScopeRecord *thunkSymScope = |
| 1594 | getSymbolScopeFields(sym: const_cast<uint8_t *>(newSym.data().data())); |
| 1595 | |
| 1596 | mod->addSymbol(Symbol: newSym); |
| 1597 | |
| 1598 | newSym = codeview::SymbolSerializer::writeOneSymbol(Sym&: es, Storage&: bAlloc, |
| 1599 | Container: CodeViewContainer::Pdb); |
| 1600 | thunkSymScope->ptrEnd = mod->getNextSymbolOffset(); |
| 1601 | |
| 1602 | mod->addSymbol(Symbol: newSym); |
| 1603 | |
| 1604 | pdb::SectionContrib sc = |
| 1605 | createSectionContrib(ctx, c: thunk->getChunk(), modi: mod->getModuleIndex()); |
| 1606 | mod->setFirstSectionContrib(sc); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | // Creates a PDB file. |
| 1611 | void lld::coff::createPDB(COFFLinkerContext &ctx, |
| 1612 | ArrayRef<uint8_t> sectionTable, |
| 1613 | llvm::codeview::DebugInfo *buildId) { |
| 1614 | llvm::TimeTraceScope timeScope("PDB file" ); |
| 1615 | ScopedTimer t1(ctx.totalPdbLinkTimer); |
| 1616 | { |
| 1617 | PDBLinker pdb(ctx); |
| 1618 | |
| 1619 | pdb.initialize(buildId); |
| 1620 | pdb.addObjectsToPDB(); |
| 1621 | pdb.addImportFilesToPDB(); |
| 1622 | pdb.addSections(sectionTable); |
| 1623 | pdb.addNatvisFiles(); |
| 1624 | pdb.addNamedStreams(); |
| 1625 | pdb.addPublicsToPDB(); |
| 1626 | |
| 1627 | { |
| 1628 | llvm::TimeTraceScope timeScope("Commit PDB file to disk" ); |
| 1629 | ScopedTimer t2(ctx.diskCommitTimer); |
| 1630 | codeview::GUID guid; |
| 1631 | pdb.commit(guid: &guid); |
| 1632 | memcpy(dest: &buildId->PDB70.Signature, src: &guid, n: 16); |
| 1633 | } |
| 1634 | |
| 1635 | t1.stop(); |
| 1636 | pdb.printStats(); |
| 1637 | |
| 1638 | // Manually start this profile point to measure ~PDBLinker(). |
| 1639 | if (getTimeTraceProfilerInstance() != nullptr) |
| 1640 | timeTraceProfilerBegin(Name: "PDBLinker destructor" , Detail: StringRef("" )); |
| 1641 | } |
| 1642 | // Manually end this profile point to measure ~PDBLinker(). |
| 1643 | if (getTimeTraceProfilerInstance() != nullptr) |
| 1644 | timeTraceProfilerEnd(); |
| 1645 | } |
| 1646 | |
| 1647 | void PDBLinker::initialize(llvm::codeview::DebugInfo *buildId) { |
| 1648 | ExitOnError exitOnErr; |
| 1649 | exitOnErr(builder.initialize(BlockSize: ctx.config.pdbPageSize)); |
| 1650 | |
| 1651 | buildId->Signature.CVSignature = OMF::Signature::PDB70; |
| 1652 | // Signature is set to a hash of the PDB contents when the PDB is done. |
| 1653 | memset(s: buildId->PDB70.Signature, c: 0, n: 16); |
| 1654 | buildId->PDB70.Age = 1; |
| 1655 | |
| 1656 | // Create streams in MSF for predefined streams, namely |
| 1657 | // PDB, TPI, DBI and IPI. |
| 1658 | for (int i = 0; i < (int)pdb::kSpecialStreamCount; ++i) |
| 1659 | exitOnErr(builder.getMsfBuilder().addStream(Size: 0)); |
| 1660 | |
| 1661 | // Add an Info stream. |
| 1662 | auto &infoBuilder = builder.getInfoBuilder(); |
| 1663 | infoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
| 1664 | infoBuilder.setHashPDBContentsToGUID(true); |
| 1665 | |
| 1666 | // Add an empty DBI stream. |
| 1667 | pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); |
| 1668 | dbiBuilder.setAge(buildId->PDB70.Age); |
| 1669 | dbiBuilder.setVersionHeader(pdb::PdbDbiV70); |
| 1670 | dbiBuilder.setMachineType(ctx.config.machine); |
| 1671 | // Technically we are not link.exe 14.11, but there are known cases where |
| 1672 | // debugging tools on Windows expect Microsoft-specific version numbers or |
| 1673 | // they fail to work at all. Since we know we produce PDBs that are |
| 1674 | // compatible with LINK 14.11, we set that version number here. |
| 1675 | dbiBuilder.setBuildNumber(Major: 14, Minor: 11); |
| 1676 | } |
| 1677 | |
| 1678 | void PDBLinker::addSections(ArrayRef<uint8_t> sectionTable) { |
| 1679 | llvm::TimeTraceScope timeScope("PDB output sections" ); |
| 1680 | ExitOnError exitOnErr; |
| 1681 | // It's not entirely clear what this is, but the * Linker * module uses it. |
| 1682 | pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); |
| 1683 | nativePath = ctx.config.pdbPath; |
| 1684 | pdbMakeAbsolute(fileName&: nativePath); |
| 1685 | uint32_t pdbFilePathNI = dbiBuilder.addECName(Name: nativePath); |
| 1686 | auto &linkerModule = exitOnErr(dbiBuilder.addModuleInfo(ModuleName: "* Linker *" )); |
| 1687 | linkerModule.setPdbFilePathNI(pdbFilePathNI); |
| 1688 | addCommonLinkerModuleSymbols(path: nativePath, mod&: linkerModule); |
| 1689 | |
| 1690 | // Add section contributions. They must be ordered by ascending RVA. |
| 1691 | for (OutputSection *os : ctx.outputSections) { |
| 1692 | addLinkerModuleSectionSymbol(mod&: linkerModule, os&: *os, isMinGW: ctx.config.mingw); |
| 1693 | for (Chunk *c : os->chunks) { |
| 1694 | pdb::SectionContrib sc = |
| 1695 | createSectionContrib(ctx, c, modi: linkerModule.getModuleIndex()); |
| 1696 | builder.getDbiBuilder().addSectionContrib(SC: sc); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | // The * Linker * first section contrib is only used along with /INCREMENTAL, |
| 1701 | // to provide trampolines thunks for incremental function patching. Set this |
| 1702 | // as "unused" because LLD doesn't support /INCREMENTAL link. |
| 1703 | pdb::SectionContrib sc = |
| 1704 | createSectionContrib(ctx, c: nullptr, modi: llvm::pdb::kInvalidStreamIndex); |
| 1705 | linkerModule.setFirstSectionContrib(sc); |
| 1706 | |
| 1707 | // Add Section Map stream. |
| 1708 | ArrayRef<object::coff_section> sections = { |
| 1709 | (const object::coff_section *)sectionTable.data(), |
| 1710 | sectionTable.size() / sizeof(object::coff_section)}; |
| 1711 | dbiBuilder.createSectionMap(SecHdrs: sections); |
| 1712 | |
| 1713 | // Add COFF section header stream. |
| 1714 | exitOnErr( |
| 1715 | dbiBuilder.addDbgStream(Type: pdb::DbgHeaderType::SectionHdr, Data: sectionTable)); |
| 1716 | } |
| 1717 | |
| 1718 | void PDBLinker::commit(codeview::GUID *guid) { |
| 1719 | // Print an error and continue if PDB writing fails. This is done mainly so |
| 1720 | // the user can see the output of /time and /summary, which is very helpful |
| 1721 | // when trying to figure out why a PDB file is too large. |
| 1722 | if (Error e = builder.commit(Filename: ctx.config.pdbPath, Guid: guid)) { |
| 1723 | e = handleErrors(E: std::move(e), Hs: [&](const llvm::msf::MSFError &me) { |
| 1724 | Err(ctx) << me.message(); |
| 1725 | if (me.isPageOverflow()) |
| 1726 | Err(ctx) << "try setting a larger /pdbpagesize" ; |
| 1727 | }); |
| 1728 | checkError(e: std::move(e)); |
| 1729 | Err(ctx) << "failed to write PDB file " << Twine(ctx.config.pdbPath); |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | static uint32_t getSecrelReloc(Triple::ArchType arch) { |
| 1734 | switch (arch) { |
| 1735 | case Triple::x86_64: |
| 1736 | return COFF::IMAGE_REL_AMD64_SECREL; |
| 1737 | case Triple::x86: |
| 1738 | return COFF::IMAGE_REL_I386_SECREL; |
| 1739 | case Triple::thumb: |
| 1740 | return COFF::IMAGE_REL_ARM_SECREL; |
| 1741 | case Triple::aarch64: |
| 1742 | return COFF::IMAGE_REL_ARM64_SECREL; |
| 1743 | default: |
| 1744 | llvm_unreachable("unknown machine type" ); |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | // Try to find a line table for the given offset Addr into the given chunk C. |
| 1749 | // If a line table was found, the line table, the string and checksum tables |
| 1750 | // that are used to interpret the line table, and the offset of Addr in the line |
| 1751 | // table are stored in the output arguments. Returns whether a line table was |
| 1752 | // found. |
| 1753 | static bool findLineTable(const SectionChunk *c, uint32_t addr, |
| 1754 | DebugStringTableSubsectionRef &cvStrTab, |
| 1755 | DebugChecksumsSubsectionRef &checksums, |
| 1756 | DebugLinesSubsectionRef &lines, |
| 1757 | uint32_t &offsetInLinetable) { |
| 1758 | ExitOnError exitOnErr; |
| 1759 | const uint32_t secrelReloc = getSecrelReloc(arch: c->getArch()); |
| 1760 | |
| 1761 | for (SectionChunk *dbgC : c->file->getDebugChunks()) { |
| 1762 | if (dbgC->getSectionName() != ".debug$S" ) |
| 1763 | continue; |
| 1764 | |
| 1765 | // Build a mapping of SECREL relocations in dbgC that refer to `c`. |
| 1766 | DenseMap<uint32_t, uint32_t> secrels; |
| 1767 | for (const coff_relocation &r : dbgC->getRelocs()) { |
| 1768 | if (r.Type != secrelReloc) |
| 1769 | continue; |
| 1770 | |
| 1771 | if (auto *s = dyn_cast_or_null<DefinedRegular>( |
| 1772 | Val: c->file->getSymbols()[r.SymbolTableIndex])) |
| 1773 | if (s->getChunk() == c) |
| 1774 | secrels[r.VirtualAddress] = s->getValue(); |
| 1775 | } |
| 1776 | |
| 1777 | ArrayRef<uint8_t> contents = |
| 1778 | SectionChunk::consumeDebugMagic(data: dbgC->getContents(), sectionName: ".debug$S" ); |
| 1779 | DebugSubsectionArray subsections; |
| 1780 | BinaryStreamReader reader(contents, llvm::endianness::little); |
| 1781 | exitOnErr(reader.readArray(Array&: subsections, Size: contents.size())); |
| 1782 | |
| 1783 | for (const DebugSubsectionRecord &ss : subsections) { |
| 1784 | switch (ss.kind()) { |
| 1785 | case DebugSubsectionKind::StringTable: { |
| 1786 | assert(!cvStrTab.valid() && |
| 1787 | "Encountered multiple string table subsections!" ); |
| 1788 | exitOnErr(cvStrTab.initialize(Contents: ss.getRecordData())); |
| 1789 | break; |
| 1790 | } |
| 1791 | case DebugSubsectionKind::FileChecksums: |
| 1792 | assert(!checksums.valid() && |
| 1793 | "Encountered multiple checksum subsections!" ); |
| 1794 | exitOnErr(checksums.initialize(Stream: ss.getRecordData())); |
| 1795 | break; |
| 1796 | case DebugSubsectionKind::Lines: { |
| 1797 | ArrayRef<uint8_t> bytes; |
| 1798 | auto ref = ss.getRecordData(); |
| 1799 | exitOnErr(ref.readLongestContiguousChunk(Offset: 0, Buffer&: bytes)); |
| 1800 | size_t offsetInDbgC = bytes.data() - dbgC->getContents().data(); |
| 1801 | |
| 1802 | // Check whether this line table refers to C. |
| 1803 | auto i = secrels.find(Val: offsetInDbgC); |
| 1804 | if (i == secrels.end()) |
| 1805 | break; |
| 1806 | |
| 1807 | // Check whether this line table covers Addr in C. |
| 1808 | DebugLinesSubsectionRef linesTmp; |
| 1809 | exitOnErr(linesTmp.initialize(Reader: BinaryStreamReader(ref))); |
| 1810 | uint32_t offsetInC = i->second + linesTmp.header()->RelocOffset; |
| 1811 | if (addr < offsetInC || addr >= offsetInC + linesTmp.header()->CodeSize) |
| 1812 | break; |
| 1813 | |
| 1814 | assert(!lines.header() && |
| 1815 | "Encountered multiple line tables for function!" ); |
| 1816 | exitOnErr(lines.initialize(Reader: BinaryStreamReader(ref))); |
| 1817 | offsetInLinetable = addr - offsetInC; |
| 1818 | break; |
| 1819 | } |
| 1820 | default: |
| 1821 | break; |
| 1822 | } |
| 1823 | |
| 1824 | if (cvStrTab.valid() && checksums.valid() && lines.header()) |
| 1825 | return true; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | return false; |
| 1830 | } |
| 1831 | |
| 1832 | // Use CodeView line tables to resolve a file and line number for the given |
| 1833 | // offset into the given chunk and return them, or std::nullopt if a line table |
| 1834 | // was not found. |
| 1835 | std::optional<std::pair<StringRef, uint32_t>> |
| 1836 | lld::coff::getFileLineCodeView(const SectionChunk *c, uint32_t addr) { |
| 1837 | ExitOnError exitOnErr; |
| 1838 | |
| 1839 | DebugStringTableSubsectionRef cvStrTab; |
| 1840 | DebugChecksumsSubsectionRef checksums; |
| 1841 | DebugLinesSubsectionRef lines; |
| 1842 | uint32_t offsetInLinetable; |
| 1843 | |
| 1844 | if (!findLineTable(c, addr, cvStrTab, checksums, lines, offsetInLinetable)) |
| 1845 | return std::nullopt; |
| 1846 | |
| 1847 | std::optional<uint32_t> nameIndex; |
| 1848 | std::optional<uint32_t> lineNumber; |
| 1849 | for (const LineColumnEntry &entry : lines) { |
| 1850 | for (const LineNumberEntry &ln : entry.LineNumbers) { |
| 1851 | LineInfo li(ln.Flags); |
| 1852 | if (ln.Offset > offsetInLinetable) { |
| 1853 | if (!nameIndex) { |
| 1854 | nameIndex = entry.NameIndex; |
| 1855 | lineNumber = li.getStartLine(); |
| 1856 | } |
| 1857 | StringRef filename = |
| 1858 | exitOnErr(getFileName(strings: cvStrTab, checksums, fileID: *nameIndex)); |
| 1859 | return std::make_pair(x&: filename, y&: *lineNumber); |
| 1860 | } |
| 1861 | nameIndex = entry.NameIndex; |
| 1862 | lineNumber = li.getStartLine(); |
| 1863 | } |
| 1864 | } |
| 1865 | if (!nameIndex) |
| 1866 | return std::nullopt; |
| 1867 | StringRef filename = exitOnErr(getFileName(strings: cvStrTab, checksums, fileID: *nameIndex)); |
| 1868 | return std::make_pair(x&: filename, y&: *lineNumber); |
| 1869 | } |
| 1870 | |