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