brintos

brintos / llvm-project-archived public Read only

0
0
Text · 69.4 KiB · fd54d20 Raw
1858 lines · cpp
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-exception6//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 54using namespace llvm;55using namespace llvm::codeview;56using namespace lld;57using namespace lld::coff;58 59using llvm::object::coff_section;60using llvm::pdb::StringTableFixup;61 62namespace {63class DebugSHandler;64 65class PDBLinker {66  friend DebugSHandler;67 68public: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 string72    // as the first "valid" string in the string table, so we do the same in73    // order to maintain as much byte-for-byte compatibility as possible.74    pdbStrTab.insert("");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 provided99  /// 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 be106  // 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 the114  // 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 records123  // 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 PDB136  void collectStats();137 138private: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 checksum152  /// table.153  DebugStringTableSubsection pdbStrTab;154 155  llvm::SmallString<128> nativePath;156};157 158/// Represents an unrelocated DEBUG_S_FRAMEDATA subsection.159struct 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.166enum : uint32_t { kSymbolStreamMagicSize = 4 };167 168class 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 by176  /// index from other records in the .debug$S section.  All of these strings177  /// need to be added to the global PDB string table, and all references to178  /// these strings need to have their indices re-written to refer to the179  /// global PDB string table.180  DebugStringTableSubsectionRef cvStrTab;181 182  /// The DEBUG_S_FILECHKSMS subsection.  As above, these are referred to183  /// by other records in the .debug$S section and need to be merged into the184  /// PDB.185  DebugChecksumsSubsectionRef checksums;186 187  /// The DEBUG_S_FRAMEDATA subsection(s).  There can be more than one of188  /// these and they need not appear in any specific order.  However, they189  /// contain string table references which need to be re-written, so we190  /// collect them all here and re-write them after all subsections have been191  /// discovered and processed.192  std::vector<UnrelocatedFpoData> frameDataSubsecs;193 194  /// List of string table references in symbol records. Later they will be195  /// 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 magic200  /// prefix.201  uint32_t moduleStreamSize = kSymbolStreamMagicSize;202 203  /// Next relocation index in the current .debug$S section. Resets every204  /// 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 215public: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 the226// PDB to work without additional configuration:227// https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box228void PDBLinker::pdbMakeAbsolute(SmallVectorImpl<char> &fileName) {229  // The default behavior is to produce paths that are valid within the context230  // of the machine that you perform the link on.  If the linker is running on231  // a POSIX system, we will output absolute POSIX paths.  If the linker is232  // running on a Windows system, we will output absolute Windows paths.  If the233  // user desires any other kind of behavior, they should explicitly pass234  // /pdbsourcepath, in which case we will treat the exact string the user235  // passed in as the gospel and not normalize, canonicalize it.236  if (sys::path::is_absolute(fileName, sys::path::Style::windows) ||237      sys::path::is_absolute(fileName, sys::path::Style::posix))238    return;239 240  // It's not absolute in any path syntax.  Relative paths necessarily refer to241  // the local file system, so we can make it native without ending up with a242  // nonsensical path.243  if (ctx.config.pdbSourcePath.empty()) {244    sys::path::native(fileName);245    sys::fs::make_absolute(fileName);246    sys::path::remove_dots(fileName, 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 only252  // decide that it's a unix path if we're fairly certain.  Specifically, if253  // it starts with a forward slash.254  SmallString<128> absoluteFileName = ctx.config.pdbSourcePath;255  sys::path::Style guessedStyle = absoluteFileName.starts_with("/")256                                      ? sys::path::Style::posix257                                      : sys::path::Style::windows;258  sys::path::append(absoluteFileName, guessedStyle, fileName);259  sys::path::native(absoluteFileName, guessedStyle);260  sys::path::remove_dots(absoluteFileName, true, guessedStyle);261 262  fileName = std::move(absoluteFileName);263}264 265static 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([&](TypeIndex ti, const CVType &type) {272    auto hash = pdb::hashTypeRecord(type);273    if (auto e = hash.takeError())274      fatal("type hashing error");275    tpiBuilder.addTypeRecord(type.RecordData, *hash);276  });277}278 279static 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(source->mergedTpi.recs,286                                           source->mergedTpi.recSizes,287                                           source->mergedTpi.recHashes);288    builder.getIpiBuilder().addTypeRecords(source->mergedIpi.recs,289                                           source->mergedIpi.recSizes,290                                           source->mergedIpi.recHashes);291  }292}293 294static void295recordStringTableReferences(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 for298  // S_DEFRANGE and S_DEFRANGE_SUBFIELD.  However, I cannot seem to generate any299  // PDBs that contain these types of records, so because of the uncertainty300  // they are omitted here until we can prove that it's necessary.301  switch (sym.kind()) {302  case SymbolKind::S_FILESTATIC: {303    // FileStaticSym::ModFileOffset304    uint32_t ref = *reinterpret_cast<const ulittle32_t *>(&sym.data()[8]);305    stringTableFixups.push_back({ref, symOffset + 8});306    break;307  }308  case SymbolKind::S_DEFRANGE:309  case SymbolKind::S_DEFRANGE_SUBFIELD:310    log("Not fixing up string table reference in S_DEFRANGE / "311        "S_DEFRANGE_SUBFIELD record");312    break;313  default:314    break;315  }316}317 318static 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]PROC32325void 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 the337  // single object file type index namespace.  This has already been translated338  // to the PDB file's ID stream index space, but we need to convert this to a339  // symbol that refers to the type stream index space.  So we remap again from340  // 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(sizeof(RecordPrefix));344    CVSymbol sym(recordData);345    discoverTypeIndicesInSymbol(sym, 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 in352    // 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, and354    // 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(*ti);359        if (idToType != tMerger.funcIdToType.end())360          newType = idToType->second;361      } else {362        if (tMerger.getIDTable().contains(*ti)) {363          CVType funcIdData = tMerger.getIDTable().getType(*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            "procedure symbol record for `{0}` in {1} refers to PDB "373            "item index {2:X} which is not a valid function ID record",374            getSymbolName(CVSymbol(recordData)), source->file->getName(),375            ti->getIndex());376      }377      *ti = newType;378    }379 380    kind = (kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32381                                              : SymbolKind::S_LPROC32;382    prefix->RecordKind = uint16_t(kind);383  }384}385 386namespace {387struct ScopeRecord {388  ulittle32_t ptrParent;389  ulittle32_t ptrEnd;390};391} // namespace392 393/// Given a pointer to a symbol record that opens a scope, return a pointer to394/// the scope fields.395static 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 the401// stack.402static void scopeStackOpen(SmallVectorImpl<uint32_t> &stack,403                           std::vector<uint8_t> &storage) {404  stack.push_back(storage.size());405}406 407// To close a scope, update the record that opened the scope.408static 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 the418  // 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(&(storage)[offOpen]);423  scopeRec->ptrParent = offParent;424  scopeRec->ptrEnd = offEnd;425}426 427static 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 place433  // since they are synthesized by the linker in response to S_GPROC32 and434  // S_LPROC32, but if we do see them, don't put them in the module stream I435  // 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 451static 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 place461  // since they are synthesized by the linker in response to S_GPROC32 and462  // 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 477static 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 record491    // to stabilize it.492    uint8_t *mem = bAlloc().Allocate<uint8_t>(sym.length());493    memcpy(mem, sym.data().data(), sym.length());494    builder.addGlobalSymbol(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);507    ps.SumName = 0;508    ps.SymOffset = symOffset;509    builder.addGlobalSymbol(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 out518// the padding bytes and update the record prefix with the new size.519static 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(recordBytes.data() + oldSize, 0, alignedSize - oldSize);527}528 529// Replace any record with a skip record of the same size. This is useful when530// we have reserved size for a symbol record, but type index remapping fails.531static void replaceWithSkipRecord(MutableArrayRef<uint8_t> recordBytes) {532  memset(recordBytes.data(), 0, 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 with540// S_SKIP records.541void 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(storage.size() + alignedSize);548  auto recordBytes = MutableArrayRef<uint8_t>(storage).take_back(alignedSize);549 550  // Copy the symbol record and relocate it.551  debugChunk->writeAndRelocateSubsection(sectionContents, sym.data(),552                                         nextRelocIndex, recordBytes.data());553  fixRecordAlignment(recordBytes, sym.length());554 555  // Re-map all the type index references.556  TpiSource *source = debugChunk->file->debugTypesObj;557  if (!source->remapTypesInSymbolRecord(recordBytes)) {558    Log(ctx) << "ignoring unknown symbol record with kind 0x"559             << utohexstr(sym.kind());560    replaceWithSkipRecord(recordBytes);561  }562 563  // An object file may have S_xxx_ID symbols, but these get converted to564  // "real" symbols in a PDB.565  translateIdSymbols(recordBytes, source);566}567 568void 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(symData.readBytes(0, symData.getLength(), symsBuffer));581 582  if (symsBuffer.empty())583    Warn(ctx) << "empty symbols subsection in " << file->getName();584 585  Error ec = forEachCodeViewRecord<CVSymbol>(586      symsBuffer, [&](CVSymbol sym) -> llvm::Error {587        // Track the current scope.588        if (symbolOpensScope(sym.kind()))589          ++scopeLevel;590        else if (symbolEndsScope(sym.kind()))591          --scopeLevel;592 593        uint32_t alignedSize =594            alignTo(sym.length(), alignOf(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, scopeLevel)) {599          storage.clear();600          writeSymbolRecord(debugChunk, sectionContents, sym, alignedSize,601                            nextRelocIndex, storage);602          addGlobalSymbol(builder.getGsiBuilder(),603                          file->moduleDBI->getModuleIndex(), moduleSymOffset,604                          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 index611        // references. There are very few of these and they will be rewritten612        // later during PDB writing.613        if (symbolGoesInModuleStream(sym, scopeLevel)) {614          recordStringTableReferences(sym, 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 wrote625  // any partial records, undo that. For globals, we just keep what we have and626  // continue.627  if (ec) {628    Warn(ctx) << "corrupt symbol records in " << file->getName();629    moduleSymOffset = moduleSymStart;630    consumeError(std::move(ec));631  }632}633 634Error 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(sectionContents, ".debug$S");649    DebugSubsectionArray subsections;650    BinaryStreamReader reader(contents, llvm::endianness::little);651    exitOnErr(reader.readArray(subsections, 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(sr.readBytes(0, sr.getLength(), symsBuffer));664      auto ec = forEachCodeViewRecord<CVSymbol>(665          symsBuffer, [&](CVSymbol sym) -> llvm::Error {666            // Track the current scope. Only update records in the postmerge667            // pass.668            if (symbolOpensScope(sym.kind()))669              scopeStackOpen(scopes, storage);670            else if (symbolEndsScope(sym.kind()))671              scopeStackClose(ctx, scopes, storage, moduleSymStart, file);672 673            // Copy, relocate, and rewrite each module symbol.674            if (symbolGoesInModuleStream(sym, scopes.size())) {675              uint32_t alignedSize =676                  alignTo(sym.length(), alignOf(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. We684      // already warned about them in the first analysis pass.685      if (ec) {686        consumeError(std::move(ec));687        storage.clear();688      }689 690      // Writing bytes has a very high overhead, so write the entire subsection691      // at once.692      // TODO: Consider buffering symbols for the entire object file to reduce693      // overhead even further.694      if (Error e = writer.writeBytes(storage))695        return e;696    }697  }698 699  return Error::success();700}701 702Error PDBLinker::commitSymbolsForObject(void *ctx, void *obj,703                                        BinaryStreamWriter &writer) {704  return static_cast<PDBLinker *>(ctx)->writeAllModuleSymbolRecords(705      static_cast<ObjFile *>(obj), writer);706}707 708static 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(&sc, 0, 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>(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(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; // FIXME728 729  return sc;730}731 732static uint32_t733translateStringTableIndex(COFFLinkerContext &ctx, uint32_t objIndex,734                          const DebugStringTableSubsectionRef &objStrTable,735                          DebugStringTableSubsection &pdbStrTable) {736  auto expectedString = objStrTable.getString(objIndex);737  if (!expectedString) {738    Warn(ctx) << "Invalid string table reference";739    consumeError(expectedString.takeError());740    return 0;741  }742 743  return pdbStrTable.insert(*expectedString);744}745 746void DebugSHandler::handleDebugS(SectionChunk *debugChunk) {747  // Note that we are processing the *unrelocated* section contents. They will748  // be relocated later during PDB writing.749  ArrayRef<uint8_t> contents = debugChunk->getContents();750  contents = SectionChunk::consumeDebugMagic(contents, ".debug$S");751  DebugSubsectionArray subsections;752  BinaryStreamReader reader(contents, llvm::endianness::little);753  ExitOnError exitOnErr;754  exitOnErr(reader.readArray(subsections, 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(ss.getRecordData()));771      break;772    }773    case DebugSubsectionKind::FileChecksums:774      assert(!checksums.valid() &&775             "Encountered multiple checksum subsections!");776      exitOnErr(checksums.initialize(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, moduleStreamSize,787                                     nextRelocIndex, stringTableFixups,788                                     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 use794      // 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(uint32_t(ss.kind())) << " in file "815                << toString(&file);816      break;817    }818  }819}820 821void 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 832namespace {833/// Wrapper class for unrelocated line and inlinee line subsections, which834/// require only relocation and type index remapping to add to the PDB.835class UnrelocatedDebugSubsection : public DebugSubsection {836public: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} // namespace850 851Error UnrelocatedDebugSubsection::commit(BinaryStreamWriter &writer) const {852  std::vector<uint8_t> relocatedBytes(subsec.size());853  uint32_t tmpRelocIndex = relocIndex;854  debugChunk->writeAndRelocateSubsection(debugChunk->getContents(), subsec,855                                         tmpRelocIndex, relocatedBytes.data());856 857  // Remap type indices in inlinee line records in place. Skip the remapping if858  // 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(storageReader));866    for (const InlineeSourceLine &line : inlineeLines) {867      TypeIndex &inlinee = *const_cast<TypeIndex *>(&line.Header->Inlinee);868      if (!source->remapTypeIndex(inlinee, TiRefKind::IndexRef)) {869        log("bad inlinee line record in " + debugChunk->file->getName() +870            " with bad inlinee index 0x" + utohexstr(inlinee.getIndex()));871      }872    }873  }874 875  return writer.writeBytes(relocatedBytes);876}877 878void DebugSHandler::addUnrelocatedSubsection(SectionChunk *debugChunk,879                                             const DebugSubsectionRecord &ss) {880  ArrayRef<uint8_t> subsec;881  BinaryStreamRef sr = ss.getRecordData();882  cantFail(sr.readBytes(0, sr.getLength(), subsec));883  advanceRelocIndex(debugChunk, subsec);884  file.moduleDBI->addDebugSubsection(885      std::make_shared<UnrelocatedDebugSubsection>(ss.kind(), debugChunk,886                                                   subsec, nextRelocIndex));887}888 889void DebugSHandler::addFrameDataSubsection(SectionChunk *debugChunk,890                                           const DebugSubsectionRecord &ss) {891  // We need to re-write string table indices here, so save off all892  // frame data subsections until we've processed the entire list of893  // subsections so that we can be sure we have the string table.894  ArrayRef<uint8_t> subsec;895  BinaryStreamRef sr = ss.getRecordData();896  cantFail(sr.readBytes(0, sr.getLength(), subsec));897  advanceRelocIndex(debugChunk, subsec);898  frameDataSubsecs.push_back({debugChunk, subsec, nextRelocIndex});899}900 901static Expected<StringRef>902getFileName(const DebugStringTableSubsectionRef &strings,903            const DebugChecksumsSubsectionRef &checksums, uint32_t fileID) {904  auto iter = checksums.getArray().at(fileID);905  if (iter == checksums.getArray().end())906    return make_error<CodeViewError>(cv_error_code::no_records);907  uint32_t offset = iter->FileNameOffset;908  return strings.getString(offset);909}910 911void 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(&file, moduleStreamSize -917                                                  kSymbolStreamMagicSize);918 919  // We should have seen all debug subsections across the entire object file now920  // which means that if a StringTable subsection and Checksums subsection were921  // present, now is the time to handle them.922  if (!cvStrTab.valid()) {923    if (checksums.valid())924      fatal(".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 base937  // relocation, which is then added to the RvaStart of each frame data record938  // when it is added to the PDB. The string table indices for the FPO program939  // 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 a942    // 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(sizeof(uint32_t));947    uint8_t relocatedRvaStart[sizeof(uint32_t)];948    debugChunk->writeAndRelocateSubsection(debugChunk->getContents(),949                                           unrelocatedRvaStart, relocIndex,950                                           &relocatedRvaStart[0]);951    // Use of memcpy here avoids violating type-based aliasing rules.952    support::ulittle32_t rvaStart;953    memcpy(&rvaStart, &relocatedRvaStart[0], sizeof(support::ulittle32_t));954 955    // Copy each frame data record, add in rvaStart, translate string table956    // indices, and add the record to the PDB.957    DebugFrameDataSubsectionRef fds;958    BinaryStreamReader reader(subsecData, llvm::endianness::little);959    exitOnErr(fds.initialize(reader));960    for (codeview::FrameData fd : fds) {961      fd.RvaStart += rvaStart;962      fd.FrameFunc = translateStringTableIndex(ctx, fd.FrameFunc, cvStrTab,963                                               linker.pdbStrTab);964      dbiBuilder.addNewFpoData(fd);965    }966  }967 968  // Translate the fixups and pass them off to the module builder so they will969  // be applied during writing.970  for (StringTableFixup &ref : stringTableFixups) {971    ref.StrTabOffset = translateStringTableIndex(ctx, ref.StrTabOffset,972                                                 cvStrTab, 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-wide977  // string table. Generally the string table subsection appears after the978  // checksum table, so we have to do this after looping over all the979  // subsections. The new checksum table must have the exact same layout and980  // size as the original. Otherwise, the file references in the line and981  // inlinee line tables will be incorrect.982  auto newChecksums = std::make_unique<DebugChecksumsSubsection>(linker.pdbStrTab);983  for (const FileChecksumEntry &fc : checksums) {984    SmallString<128> filename =985        exitOnErr(cvStrTab.getString(fc.FileNameOffset));986    linker.pdbMakeAbsolute(filename);987    exitOnErr(dbiBuilder.addModuleSourceFile(*file.moduleDBI, filename));988    newChecksums->addChecksum(filename, fc.Kind, 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(std::move(newChecksums));995}996 997static void warnUnusable(InputFile *f, Error e, bool shouldWarn) {998  if (!shouldWarn) {999    consumeError(std::move(e));1000    return;1001  }1002  auto diag = Warn(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.1009static ArrayRef<uint8_t> relocateDebugChunk(SectionChunk &debugChunk) {1010  uint8_t *buffer = bAlloc().Allocate<uint8_t>(debugChunk.getSize());1011  assert(debugChunk.getOutputSectionIdx() == 0 &&1012         "debug sections should not be in output sections");1013  debugChunk.writeTo(buffer);1014  return ArrayRef(buffer, debugChunk.getSize());1015}1016 1017void PDBLinker::addDebugSymbols(TpiSource *source) {1018  // If this TpiSource doesn't have an object file, it must be from a type1019  // 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);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(fpoRecords, count));1049 1050      // These are already relocated and don't refer to the string table, so we1051      // can just copy it.1052      for (const object::FpoData &fd : fpoRecords)1053        dbiBuilder.addOldFpoData(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 absolute1062// path to the object into the PDB. If this is a plain object, we make its1063// path absolute. If it's an object in an archive, we make the archive path1064// absolute.1065void 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(objName);1073  StringRef modName = inArchive ? file->getName() : objName.str();1074 1075  file->moduleDBI = &exitOnErr(dbiBuilder.addModuleInfo(modName));1076  file->moduleDBI->setObjFileName(objName);1077  file->moduleDBI->setMergeSymbolsCallback(this, &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>(c);1084    if (!secChunk || !secChunk->live)1085      continue;1086    pdb::SectionContrib sc = createSectionContrib(ctx, secChunk, modi);1087    file->moduleDBI->setFirstSectionContrib(sc);1088    break;1089  }1090}1091 1092void PDBLinker::addDebug(TpiSource *source) {1093  // Before we can process symbol substreams from .debug$S, we need to process1094  // type information, file checksums, and the string table. Add type info to1095  // the PDB first, so that we can get the map from object file type and item1096  // indices to PDB type and item indices.  If we are using ghashes, types have1097  // 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(&tMerger)) {1102      // If type merging failed, ignore the symbols.1103      warnUnusable(source->file, std::move(e),1104                   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(source->file, std::move(typeError),1113                 ctx.config.warnDebugInfoUnusable);1114    return;1115  }1116 1117  addDebugSymbols(source);1118}1119 1120static 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>(def)) {1127    if (d->getCOFFSymbol().isFunctionDefinition())1128      flags = PublicSymFlags::Function;1129  } else if (isa<DefinedImportThunk>(def)) {1130    flags = PublicSymFlags::Function;1131  }1132  pub.setFlags(flags);1133 1134  OutputSection *os = ctx.getOutputSection(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 and1145// TpiData.1146void PDBLinker::addObjectsToPDB() {1147  {1148    llvm::TimeTraceScope timeScope("Add objects to PDB");1149    ScopedTimer t1(ctx.addObjectsTimer);1150 1151    // Create module descriptors1152    for (ObjFile *obj : ctx.objFileInstances)1153      createModuleDBI(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(builder.getTpiBuilder(), tMerger.getTypeTable());1187      addTypeInfo(builder.getIpiBuilder(), 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 1199void 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([&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>(s);1209    if (def && def->isLive() && def->getChunk()) {1210      // Don't emit a public symbol for coverage data symbols. LLVM code1211      // coverage (and PGO) create a __profd_ and __profc_ symbol for every1212      // function. C++ mangled names are long, and tend to dominate symbol size.1213      // Including these names triples the size of the public stream, which1214      // results in bloated PDB files. These symbols generally are not helpful1215      // 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(1);1221        if (name.starts_with("__profd_") || name.starts_with("__profc_") ||1222            name.starts_with("__covrec_")) {1223          return;1224        }1225      }1226      publics.push_back(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(std::move(publics));1235}1236 1237void 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 duplicate1252    // bytes in the input files. These should be frequently emitted LF_CLASS and1253    // 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(recCounts)) {1267      TypeIndex typeIndex = TypeIndex::fromArrayIndex(e.index());1268      uint32_t typeSize = records.getType(typeIndex).length();1269      uint32_t dupCount = e.value();1270      tsis.push_back({typeSize, dupCount, typeIndex});1271    }1272 1273    if (!tsis.empty()) {1274      stream << "\nTop 10 types responsible for the most " << name1275             << " input:\n";1276      stream << "       index     total bytes   count     size\n";1277      llvm::sort(tsis);1278      unsigned i = 0;1279      for (const auto &tsi : reverse(tsis)) {1280        stream << formatv("  {0,10:X}: {1,14:N} = {2,5:N} * {3,6:N}\n",1281                          tsi.typeIndex.getIndex(), tsi.totalInputSize(),1282                          tsi.dupCount, tsi.typeSize);1283        if (++i >= 10)1284          break;1285      }1286      stream1287          << "Run llvm-pdbutil to print details about a particular record:\n";1288      stream << formatv("llvm-pdbutil dump -{0}s -{0}-index {1:X} {2}\n",1289                        (name == "TPI" ? "type" : "id"),1290                        tsis.back().typeIndex.getIndex(), 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 1303void 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(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(relativeToRoot(data->getBufferIdentifier()),1317                             data->getBuffer());1318 1319    builder.addInjectedSource(file, std::move(data));1320  }1321}1322 1323void 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(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(stream, data->getBuffer()));1336    ctx.driver.takeBuffer(std::move(data));1337  }1338}1339 1340static 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 be1363// executed again on the cmd-line.1364static std::string quote(ArrayRef<StringRef> args) {1365  std::string r;1366  r.reserve(256);1367  for (StringRef a : args) {1368    if (!r.empty())1369      r.push_back(' ');1370    bool hasWS = a.contains(' ');1371    bool hasQ = a.contains('"');1372    if (hasWS || hasQ)1373      r.push_back('"');1374    if (hasQ) {1375      SmallVector<StringRef, 4> s;1376      a.split(s, '"');1377      r.append(join(s, "\"\""));1378    } else {1379      r.append(std::string(a));1380    }1381    if (hasWS || hasQ)1382      r.push_back('"');1383  }1384  return r;1385}1386 1387static 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 view1390  // 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 are1392  // displayed properly.   As such, even though it is not representative of1393  // 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 the1401  // linker module (which is by definition a backend), so we don't need to do1402  // anything here.  Also, it seems we can use "LLVM Linker" for the linker name1403  // without any problems.  Only the backend version has to be hardcoded to a1404  // 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 1413void 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 linker1421  // module record.1422  if (isArm64EC(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("cwd");1432  SmallString<64> cwd;1433  if (ctx.config.pdbSourcePath.empty())1434    sys::fs::current_path(cwd);1435  else1436    cwd = ctx.config.pdbSourcePath;1437  ebs.Fields.push_back(cwd);1438  ebs.Fields.push_back("exe");1439  SmallString<64> exe = ctx.config.argv[0];1440  pdbMakeAbsolute(exe);1441  ebs.Fields.push_back(exe);1442  ebs.Fields.push_back("pdb");1443  ebs.Fields.push_back(path);1444  ebs.Fields.push_back("cmd");1445  ebs.Fields.push_back(argStr);1446  llvm::BumpPtrAllocator &bAlloc = lld::bAlloc();1447  mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(1448      ons, bAlloc, CodeViewContainer::Pdb));1449  mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(1450      cs, bAlloc, CodeViewContainer::Pdb));1451  mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(1452      ebs, bAlloc, CodeViewContainer::Pdb));1453}1454 1455static void addLinkerModuleCoffGroup(PartialSection *sec,1456                                     pdb::DbiModuleDescriptorBuilder &mod,1457                                     OutputSection &os) {1458  // If there's a section, there's at least one chunk1459  assert(!sec->chunks.empty());1460  const Chunk *firstChunk = *sec->chunks.begin();1461  const Chunk *lastChunk = *sec->chunks.rbegin();1462 1463  // Emit COFF group1464  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 have1472  // the "write" flag set. However the section header for the corresponding1473  // .idata section doesn't have it.1474  if (cgs.Name.starts_with(".idata"))1475    cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE;1476 1477  mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(1478      cgs, bAlloc(), CodeViewContainer::Pdb));1479}1480 1481static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &mod,1482                                         OutputSection &os, bool isMinGW) {1483  SectionSym sym(SymbolRecordKind::SectionSym);1484  sym.Alignment = 12; // 2^12 = 4KB1485  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(codeview::SymbolSerializer::writeOneSymbol(1491      sym, bAlloc(), CodeViewContainer::Pdb));1492 1493  // Skip COFF groups in MinGW because it adds a significant footprint to the1494  // PDB, due to each function being in its own section1495  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.1505void 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(libPath);1529      sys::path::native(libPath);1530 1531      // Name modules similar to MSVC's link.exe.1532      // The first module is the simple dll filename1533      llvm::pdb::DbiModuleDescriptorBuilder &firstMod =1534          exitOnErr(dbiBuilder.addModuleInfo(file->dllName));1535      firstMod.setObjFileName(libPath);1536      pdb::SectionContrib sc =1537          createSectionContrib(ctx, nullptr, llvm::pdb::kInvalidStreamIndex);1538      firstMod.setFirstSectionContrib(sc);1539 1540      // The second module is where the import stream goes.1541      mod = &exitOnErr(dbiBuilder.addModuleInfo("Import:" + file->dllName));1542      mod->setObjFileName(libPath);1543    }1544 1545    DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);1546    Chunk *thunkChunk = thunk->getChunk();1547    OutputSection *thunkOS = ctx.getOutputSection(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, 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(codeview::SymbolSerializer::writeOneSymbol(1570        ons, bAlloc, CodeViewContainer::Pdb));1571    mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol(1572        cs, bAlloc, CodeViewContainer::Pdb));1573 1574    CVSymbol newSym = codeview::SymbolSerializer::writeOneSymbol(1575        ts, bAlloc, CodeViewContainer::Pdb);1576 1577    // Write ptrEnd for the S_THUNK32.1578    ScopeRecord *thunkSymScope =1579        getSymbolScopeFields(const_cast<uint8_t *>(newSym.data().data()));1580 1581    mod->addSymbol(newSym);1582 1583    newSym = codeview::SymbolSerializer::writeOneSymbol(es, bAlloc,1584                                                        CodeViewContainer::Pdb);1585    thunkSymScope->ptrEnd = mod->getNextSymbolOffset();1586 1587    mod->addSymbol(newSym);1588 1589    pdb::SectionContrib sc =1590        createSectionContrib(ctx, thunk->getChunk(), mod->getModuleIndex());1591    mod->setFirstSectionContrib(sc);1592  }1593}1594 1595// Creates a PDB file.1596void 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);1620      memcpy(&buildId->PDB70.Signature, &guid, 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("PDBLinker destructor", StringRef(""));1629  }1630  // Manually end this profile point to measure ~PDBLinker().1631  if (getTimeTraceProfilerInstance() != nullptr)1632    timeTraceProfilerEnd();1633}1634 1635void PDBLinker::initialize(llvm::codeview::DebugInfo *buildId) {1636  ExitOnError exitOnErr;1637  exitOnErr(builder.initialize(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(buildId->PDB70.Signature, 0, 16);1642  buildId->PDB70.Age = 1;1643 1644  // Create streams in MSF for predefined streams, namely1645  // PDB, TPI, DBI and IPI.1646  for (int i = 0; i < (int)pdb::kSpecialStreamCount; ++i)1647    exitOnErr(builder.getMsfBuilder().addStream(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 where1660  // debugging tools on Windows expect Microsoft-specific version numbers or1661  // they fail to work at all.  Since we know we produce PDBs that are1662  // compatible with LINK 14.11, we set that version number here.1663  dbiBuilder.setBuildNumber(14, 11);1664}1665 1666void 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(nativePath);1673  uint32_t pdbFilePathNI = dbiBuilder.addECName(nativePath);1674  auto &linkerModule = exitOnErr(dbiBuilder.addModuleInfo("* Linker *"));1675  linkerModule.setPdbFilePathNI(pdbFilePathNI);1676  addCommonLinkerModuleSymbols(nativePath, linkerModule);1677 1678  // Add section contributions. They must be ordered by ascending RVA.1679  for (OutputSection *os : ctx.outputSections) {1680    addLinkerModuleSectionSymbol(linkerModule, *os, ctx.config.mingw);1681    for (Chunk *c : os->chunks) {1682      pdb::SectionContrib sc =1683          createSectionContrib(ctx, c, linkerModule.getModuleIndex());1684      builder.getDbiBuilder().addSectionContrib(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 this1690  // as "unused" because LLD doesn't support /INCREMENTAL link.1691  pdb::SectionContrib sc =1692      createSectionContrib(ctx, nullptr, 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(sections);1700 1701  // Add COFF section header stream.1702  exitOnErr(1703      dbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, sectionTable));1704}1705 1706void PDBLinker::commit(codeview::GUID *guid) {1707  // Print an error and continue if PDB writing fails. This is done mainly so1708  // the user can see the output of /time and /summary, which is very helpful1709  // when trying to figure out why a PDB file is too large.1710  if (Error e = builder.commit(ctx.config.pdbPath, guid)) {1711    e = handleErrors(std::move(e), [&](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(std::move(e));1717    Err(ctx) << "failed to write PDB file " << Twine(ctx.config.pdbPath);1718  }1719}1720 1721static 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 tables1738// that are used to interpret the line table, and the offset of Addr in the line1739// table are stored in the output arguments. Returns whether a line table was1740// found.1741static 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(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              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(dbgC->getContents(), ".debug$S");1767    DebugSubsectionArray subsections;1768    BinaryStreamReader reader(contents, llvm::endianness::little);1769    exitOnErr(reader.readArray(subsections, 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(ss.getRecordData()));1777        break;1778      }1779      case DebugSubsectionKind::FileChecksums:1780        assert(!checksums.valid() &&1781               "Encountered multiple checksum subsections!");1782        exitOnErr(checksums.initialize(ss.getRecordData()));1783        break;1784      case DebugSubsectionKind::Lines: {1785        ArrayRef<uint8_t> bytes;1786        auto ref = ss.getRecordData();1787        exitOnErr(ref.readLongestContiguousChunk(0, 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(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(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(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 given1821// offset into the given chunk and return them, or std::nullopt if a line table1822// was not found.1823std::optional<std::pair<StringRef, uint32_t>>1824lld::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(cvStrTab, checksums, *nameIndex));1847        return std::make_pair(filename, *lineNumber);1848      }1849      nameIndex = entry.NameIndex;1850      lineNumber = li.getStartLine();1851    }1852  }1853  if (!nameIndex)1854    return std::nullopt;1855  StringRef filename = exitOnErr(getFileName(cvStrTab, checksums, *nameIndex));1856  return std::make_pair(filename, *lineNumber);1857}1858