1292934Sdim//===- PDB.cpp ------------------------------------------------------------===// 2292934Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6292934Sdim// 7292934Sdim//===----------------------------------------------------------------------===// 8292934Sdim 9314564Sdim#include "PDB.h" 10314564Sdim#include "Chunks.h" 11314564Sdim#include "Config.h" 12353358Sdim#include "DebugTypes.h" 13327952Sdim#include "Driver.h" 14314564Sdim#include "SymbolTable.h" 15292934Sdim#include "Symbols.h" 16353358Sdim#include "TypeMerger.h" 17327952Sdim#include "Writer.h" 18327952Sdim#include "lld/Common/ErrorHandler.h" 19360784Sdim#include "lld/Common/Threads.h" 20341825Sdim#include "lld/Common/Timer.h" 21344779Sdim#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" 22321369Sdim#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" 23327952Sdim#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h" 24321369Sdim#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 25327952Sdim#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" 26327952Sdim#include "llvm/DebugInfo/CodeView/RecordName.h" 27327952Sdim#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 28344779Sdim#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h" 29321369Sdim#include "llvm/DebugInfo/CodeView/SymbolSerializer.h" 30321369Sdim#include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 31314564Sdim#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 32321369Sdim#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" 33360784Sdim#include "llvm/DebugInfo/CodeView/TypeRecordHelpers.h" 34314564Sdim#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" 35314564Sdim#include "llvm/DebugInfo/MSF/MSFBuilder.h" 36314564Sdim#include "llvm/DebugInfo/MSF/MSFCommon.h" 37321369Sdim#include "llvm/DebugInfo/PDB/GenericError.h" 38321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" 39321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiStream.h" 40321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" 41327952Sdim#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" 42321369Sdim#include "llvm/DebugInfo/PDB/Native/InfoStream.h" 43321369Sdim#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" 44321369Sdim#include "llvm/DebugInfo/PDB/Native/NativeSession.h" 45321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBFile.h" 46321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" 47321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 48327952Sdim#include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 49321369Sdim#include "llvm/DebugInfo/PDB/Native/TpiStream.h" 50321369Sdim#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" 51321369Sdim#include "llvm/DebugInfo/PDB/PDB.h" 52314564Sdim#include "llvm/Object/COFF.h" 53341825Sdim#include "llvm/Object/CVDebugRecord.h" 54321369Sdim#include "llvm/Support/BinaryByteStream.h" 55360784Sdim#include "llvm/Support/CRC.h" 56292934Sdim#include "llvm/Support/Endian.h" 57344779Sdim#include "llvm/Support/Errc.h" 58360784Sdim#include "llvm/Support/FormatAdapters.h" 59341825Sdim#include "llvm/Support/FormatVariadic.h" 60321369Sdim#include "llvm/Support/Path.h" 61314564Sdim#include "llvm/Support/ScopedPrinter.h" 62292934Sdim#include <memory> 63292934Sdim 64292934Sdimusing namespace llvm; 65314564Sdimusing namespace llvm::codeview; 66292934Sdim 67314564Sdimusing llvm::object::coff_section; 68292934Sdim 69360784Sdimnamespace lld { 70360784Sdimnamespace coff { 71360784Sdim 72353358Sdimstatic ExitOnError exitOnErr; 73314564Sdim 74353358Sdimstatic Timer totalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root()); 75341825Sdim 76353358Sdimstatic Timer addObjectsTimer("Add Objects", totalPdbLinkTimer); 77353358Sdimstatic Timer typeMergingTimer("Type Merging", addObjectsTimer); 78353358Sdimstatic Timer symbolMergingTimer("Symbol Merging", addObjectsTimer); 79353358Sdimstatic Timer globalsLayoutTimer("Globals Stream Layout", totalPdbLinkTimer); 80353358Sdimstatic Timer tpiStreamLayoutTimer("TPI Stream Layout", totalPdbLinkTimer); 81353358Sdimstatic Timer diskCommitTimer("Commit to Disk", totalPdbLinkTimer); 82341825Sdim 83321369Sdimnamespace { 84344779Sdimclass DebugSHandler; 85344779Sdim 86321369Sdimclass PDBLinker { 87344779Sdim friend DebugSHandler; 88344779Sdim 89321369Sdimpublic: 90353358Sdim PDBLinker(SymbolTable *symtab) 91353358Sdim : alloc(), symtab(symtab), builder(alloc), tMerger(alloc) { 92341825Sdim // This isn't strictly necessary, but link.exe usually puts an empty string 93341825Sdim // as the first "valid" string in the string table, so we do the same in 94341825Sdim // order to maintain as much byte-for-byte compatibility as possible. 95353358Sdim pdbStrTab.insert(""); 96341825Sdim } 97321369Sdim 98321369Sdim /// Emit the basic PDB structure: initial streams, headers, etc. 99353358Sdim void initialize(llvm::codeview::DebugInfo *buildId); 100321369Sdim 101341825Sdim /// Add natvis files specified on the command line. 102341825Sdim void addNatvisFiles(); 103341825Sdim 104321369Sdim /// Link CodeView from each object file in the symbol table into the PDB. 105321369Sdim void addObjectsToPDB(); 106321369Sdim 107353358Sdim /// Link info for each import file in the symbol table into the PDB. 108353358Sdim void addImportFilesToPDB(ArrayRef<OutputSection *> outputSections); 109353358Sdim 110344779Sdim /// Link CodeView from a single object file into the target (output) PDB. 111344779Sdim /// When a precompiled headers object is linked, its TPI map might be provided 112344779Sdim /// externally. 113353358Sdim void addObjFile(ObjFile *file, CVIndexMap *externIndexMap = nullptr); 114321369Sdim 115321369Sdim /// Produce a mapping from the type and item indices used in the object 116321369Sdim /// file to those in the destination PDB. 117321369Sdim /// 118321369Sdim /// If the object file uses a type server PDB (compiled with /Zi), merge TPI 119321369Sdim /// and IPI from the type server PDB and return a map for it. Each unique type 120321369Sdim /// server PDB is merged at most once, so this may return an existing index 121321369Sdim /// mapping. 122321369Sdim /// 123321369Sdim /// If the object does not use a type server PDB (compiled with /Z7), we merge 124321369Sdim /// all the type and item records from the .debug$S stream and fill in the 125353358Sdim /// caller-provided objectIndexMap. 126353358Sdim Expected<const CVIndexMap &> mergeDebugT(ObjFile *file, 127353358Sdim CVIndexMap *objectIndexMap); 128321369Sdim 129344779Sdim /// Reads and makes available a PDB. 130353358Sdim Expected<const CVIndexMap &> maybeMergeTypeServerPDB(ObjFile *file); 131321369Sdim 132344779Sdim /// Merges a precompiled headers TPI map into the current TPI map. The 133344779Sdim /// precompiled headers object will also be loaded and remapped in the 134344779Sdim /// process. 135353358Sdim Error mergeInPrecompHeaderObj(ObjFile *file, CVIndexMap *objectIndexMap); 136344779Sdim 137344779Sdim /// Reads and makes available a precompiled headers object. 138344779Sdim /// 139344779Sdim /// This is a requirement for objects compiled with cl.exe /Yu. In that 140344779Sdim /// case, the referenced object (which was compiled with /Yc) has to be loaded 141344779Sdim /// first. This is mainly because the current object's TPI stream has external 142344779Sdim /// references to the precompiled headers object. 143344779Sdim /// 144344779Sdim /// If the precompiled headers object was already loaded, this function will 145344779Sdim /// simply return its (remapped) TPI map. 146353358Sdim Expected<const CVIndexMap &> aquirePrecompObj(ObjFile *file); 147344779Sdim 148344779Sdim /// Adds a precompiled headers object signature -> TPI mapping. 149344779Sdim std::pair<CVIndexMap &, bool /*already there*/> 150353358Sdim registerPrecompiledHeaders(uint32_t signature); 151344779Sdim 152353358Sdim void mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap, 153353358Sdim std::vector<ulittle32_t *> &stringTableRefs, 154353358Sdim BinaryStreamRef symData); 155344779Sdim 156321369Sdim /// Add the section map and section contributions to the PDB. 157353358Sdim void addSections(ArrayRef<OutputSection *> outputSections, 158353358Sdim ArrayRef<uint8_t> sectionTable); 159321369Sdim 160353358Sdim /// Write the PDB to disk and store the Guid generated for it in *Guid. 161353358Sdim void commit(codeview::GUID *guid); 162321369Sdim 163353358Sdim // Print statistics regarding the final PDB 164353358Sdim void printStats(); 165344779Sdim 166321369Sdimprivate: 167353358Sdim BumpPtrAllocator alloc; 168321369Sdim 169353358Sdim SymbolTable *symtab; 170321369Sdim 171353358Sdim pdb::PDBFileBuilder builder; 172321369Sdim 173353358Sdim TypeMerger tMerger; 174321369Sdim 175321369Sdim /// PDBs use a single global string table for filenames in the file checksum 176321369Sdim /// table. 177353358Sdim DebugStringTableSubsection pdbStrTab; 178321369Sdim 179353358Sdim llvm::SmallString<128> nativePath; 180321369Sdim 181353358Sdim std::vector<pdb::SecMapEntry> sectionMap; 182341825Sdim 183321369Sdim /// Type index mappings of type server PDBs that we've loaded so far. 184353358Sdim std::map<codeview::GUID, CVIndexMap> typeServerIndexMappings; 185329410Sdim 186344779Sdim /// Type index mappings of precompiled objects type map that we've loaded so 187344779Sdim /// far. 188353358Sdim std::map<uint32_t, CVIndexMap> precompTypeIndexMappings; 189344779Sdim 190353358Sdim // For statistics 191353358Sdim uint64_t globalSymbols = 0; 192353358Sdim uint64_t moduleSymbols = 0; 193353358Sdim uint64_t publicSymbols = 0; 194360784Sdim 195360784Sdim // When showSummary is enabled, these are histograms of TPI and IPI records 196360784Sdim // keyed by type index. 197360784Sdim SmallVector<uint32_t, 0> tpiCounts; 198360784Sdim SmallVector<uint32_t, 0> ipiCounts; 199321369Sdim}; 200344779Sdim 201344779Sdimclass DebugSHandler { 202353358Sdim PDBLinker &linker; 203344779Sdim 204344779Sdim /// The object file whose .debug$S sections we're processing. 205353358Sdim ObjFile &file; 206344779Sdim 207344779Sdim /// The result of merging type indices. 208353358Sdim const CVIndexMap &indexMap; 209344779Sdim 210344779Sdim /// The DEBUG_S_STRINGTABLE subsection. These strings are referred to by 211344779Sdim /// index from other records in the .debug$S section. All of these strings 212344779Sdim /// need to be added to the global PDB string table, and all references to 213344779Sdim /// these strings need to have their indices re-written to refer to the 214344779Sdim /// global PDB string table. 215353358Sdim DebugStringTableSubsectionRef cVStrTab; 216344779Sdim 217344779Sdim /// The DEBUG_S_FILECHKSMS subsection. As above, these are referred to 218344779Sdim /// by other records in the .debug$S section and need to be merged into the 219344779Sdim /// PDB. 220353358Sdim DebugChecksumsSubsectionRef checksums; 221344779Sdim 222353358Sdim /// The DEBUG_S_INLINEELINES subsection. There can be only one of these per 223353358Sdim /// object file. 224353358Sdim DebugInlineeLinesSubsectionRef inlineeLines; 225353358Sdim 226344779Sdim /// The DEBUG_S_FRAMEDATA subsection(s). There can be more than one of 227344779Sdim /// these and they need not appear in any specific order. However, they 228344779Sdim /// contain string table references which need to be re-written, so we 229344779Sdim /// collect them all here and re-write them after all subsections have been 230344779Sdim /// discovered and processed. 231353358Sdim std::vector<DebugFrameDataSubsectionRef> newFpoFrames; 232344779Sdim 233344779Sdim /// Pointers to raw memory that we determine have string table references 234344779Sdim /// that need to be re-written. We first process all .debug$S subsections 235344779Sdim /// to ensure that we can handle subsections written in any order, building 236344779Sdim /// up this list as we go. At the end, we use the string table (which must 237344779Sdim /// have been discovered by now else it is an error) to re-write these 238344779Sdim /// references. 239353358Sdim std::vector<ulittle32_t *> stringTableReferences; 240344779Sdim 241344779Sdimpublic: 242353358Sdim DebugSHandler(PDBLinker &linker, ObjFile &file, const CVIndexMap &indexMap) 243353358Sdim : linker(linker), file(file), indexMap(indexMap) {} 244344779Sdim 245353358Sdim void handleDebugS(lld::coff::SectionChunk &debugS); 246353358Sdim 247353358Sdim std::shared_ptr<DebugInlineeLinesSubsection> 248353358Sdim mergeInlineeLines(DebugChecksumsSubsection *newChecksums); 249353358Sdim 250344779Sdim void finish(); 251344779Sdim}; 252321369Sdim} 253321369Sdim 254344779Sdim// Visual Studio's debugger requires absolute paths in various places in the 255344779Sdim// PDB to work without additional configuration: 256344779Sdim// https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box 257353358Sdimstatic void pdbMakeAbsolute(SmallVectorImpl<char> &fileName) { 258344779Sdim // The default behavior is to produce paths that are valid within the context 259344779Sdim // of the machine that you perform the link on. If the linker is running on 260344779Sdim // a POSIX system, we will output absolute POSIX paths. If the linker is 261344779Sdim // running on a Windows system, we will output absolute Windows paths. If the 262344779Sdim // user desires any other kind of behavior, they should explicitly pass 263344779Sdim // /pdbsourcepath, in which case we will treat the exact string the user 264344779Sdim // passed in as the gospel and not normalize, canonicalize it. 265353358Sdim if (sys::path::is_absolute(fileName, sys::path::Style::windows) || 266353358Sdim sys::path::is_absolute(fileName, sys::path::Style::posix)) 267344779Sdim return; 268344779Sdim 269344779Sdim // It's not absolute in any path syntax. Relative paths necessarily refer to 270344779Sdim // the local file system, so we can make it native without ending up with a 271344779Sdim // nonsensical path. 272353358Sdim if (config->pdbSourcePath.empty()) { 273353358Sdim sys::path::native(fileName); 274353358Sdim sys::fs::make_absolute(fileName); 275344779Sdim return; 276344779Sdim } 277344779Sdim 278344779Sdim // Try to guess whether /PDBSOURCEPATH is a unix path or a windows path. 279344779Sdim // Since PDB's are more of a Windows thing, we make this conservative and only 280344779Sdim // decide that it's a unix path if we're fairly certain. Specifically, if 281344779Sdim // it starts with a forward slash. 282353358Sdim SmallString<128> absoluteFileName = config->pdbSourcePath; 283353358Sdim sys::path::Style guessedStyle = absoluteFileName.startswith("/") 284344779Sdim ? sys::path::Style::posix 285344779Sdim : sys::path::Style::windows; 286353358Sdim sys::path::append(absoluteFileName, guessedStyle, fileName); 287353358Sdim sys::path::native(absoluteFileName, guessedStyle); 288353358Sdim sys::path::remove_dots(absoluteFileName, true, guessedStyle); 289344779Sdim 290353358Sdim fileName = std::move(absoluteFileName); 291344779Sdim} 292344779Sdim 293327952Sdim// A COFF .debug$H section is currently a clang extension. This function checks 294327952Sdim// if a .debug$H section is in a format that we expect / understand, so that we 295327952Sdim// can ignore any sections which are coincidentally also named .debug$H but do 296327952Sdim// not contain a format we recognize. 297353358Sdimstatic bool canUseDebugH(ArrayRef<uint8_t> debugH) { 298353358Sdim if (debugH.size() < sizeof(object::debug_h_header)) 299327952Sdim return false; 300353358Sdim auto *header = 301353358Sdim reinterpret_cast<const object::debug_h_header *>(debugH.data()); 302353358Sdim debugH = debugH.drop_front(sizeof(object::debug_h_header)); 303353358Sdim return header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC && 304353358Sdim header->Version == 0 && 305353358Sdim header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) && 306353358Sdim (debugH.size() % 8 == 0); 307327952Sdim} 308327952Sdim 309353358Sdimstatic Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *file) { 310353358Sdim SectionChunk *sec = 311353358Sdim SectionChunk::findByName(file->getDebugChunks(), ".debug$H"); 312353358Sdim if (!sec) 313327952Sdim return llvm::None; 314353358Sdim ArrayRef<uint8_t> contents = sec->getContents(); 315353358Sdim if (!canUseDebugH(contents)) 316327952Sdim return None; 317353358Sdim return contents; 318327952Sdim} 319327952Sdim 320327952Sdimstatic ArrayRef<GloballyHashedType> 321353358SdimgetHashesFromDebugH(ArrayRef<uint8_t> debugH) { 322353358Sdim assert(canUseDebugH(debugH)); 323327952Sdim 324353358Sdim debugH = debugH.drop_front(sizeof(object::debug_h_header)); 325353358Sdim uint32_t count = debugH.size() / sizeof(GloballyHashedType); 326353358Sdim return {reinterpret_cast<const GloballyHashedType *>(debugH.data()), count}; 327327952Sdim} 328327952Sdim 329353358Sdimstatic void addTypeInfo(pdb::TpiStreamBuilder &tpiBuilder, 330353358Sdim TypeCollection &typeTable) { 331321369Sdim // Start the TPI or IPI stream header. 332353358Sdim tpiBuilder.setVersionHeader(pdb::PdbTpiV80); 333314564Sdim 334327952Sdim // Flatten the in memory type table and hash each type. 335353358Sdim typeTable.ForEachRecord([&](TypeIndex ti, const CVType &type) { 336353358Sdim auto hash = pdb::hashTypeRecord(type); 337353358Sdim if (auto e = hash.takeError()) 338327952Sdim fatal("type hashing error"); 339353358Sdim tpiBuilder.addTypeRecord(type.RecordData, *hash); 340314564Sdim }); 341314564Sdim} 342314564Sdim 343344779SdimExpected<const CVIndexMap &> 344353358SdimPDBLinker::mergeDebugT(ObjFile *file, CVIndexMap *objectIndexMap) { 345353358Sdim ScopedTimer t(typeMergingTimer); 346341825Sdim 347353358Sdim if (!file->debugTypesObj) 348353358Sdim return *objectIndexMap; // no Types stream 349344779Sdim 350344779Sdim // Precompiled headers objects need to save the index map for further 351344779Sdim // reference by other objects which use the precompiled headers. 352353358Sdim if (file->debugTypesObj->kind == TpiSource::PCH) { 353353358Sdim uint32_t pchSignature = file->pchSignature.getValueOr(0); 354353358Sdim if (pchSignature == 0) 355344779Sdim fatal("No signature found for the precompiled headers OBJ (" + 356353358Sdim file->getName() + ")"); 357344779Sdim 358344779Sdim // When a precompiled headers object comes first on the command-line, we 359344779Sdim // update the mapping here. Otherwise, if an object referencing the 360344779Sdim // precompiled headers object comes first, the mapping is created in 361344779Sdim // aquirePrecompObj(), thus we would skip this block. 362353358Sdim if (!objectIndexMap->isPrecompiledTypeMap) { 363353358Sdim auto r = registerPrecompiledHeaders(pchSignature); 364353358Sdim if (r.second) 365344779Sdim fatal( 366344779Sdim "A precompiled headers OBJ with the same signature was already " 367344779Sdim "provided! (" + 368353358Sdim file->getName() + ")"); 369344779Sdim 370353358Sdim objectIndexMap = &r.first; 371344779Sdim } 372344779Sdim } 373344779Sdim 374353358Sdim if (file->debugTypesObj->kind == TpiSource::UsingPDB) { 375344779Sdim // Look through type servers. If we've already seen this type server, 376344779Sdim // don't merge any type information. 377353358Sdim return maybeMergeTypeServerPDB(file); 378353358Sdim } 379353358Sdim 380360784Sdim CVTypeArray types; 381360784Sdim BinaryStreamReader reader(file->debugTypes, support::little); 382360784Sdim cantFail(reader.readArray(types, reader.getLength())); 383360784Sdim 384353358Sdim if (file->debugTypesObj->kind == TpiSource::UsingPCH) { 385344779Sdim // This object was compiled with /Yu, so process the corresponding 386344779Sdim // precompiled headers object (/Yc) first. Some type indices in the current 387344779Sdim // object are referencing data in the precompiled headers object, so we need 388344779Sdim // both to be loaded. 389353358Sdim Error e = mergeInPrecompHeaderObj(file, objectIndexMap); 390353358Sdim if (e) 391353358Sdim return std::move(e); 392344779Sdim 393353358Sdim // Drop LF_PRECOMP record from the input stream, as it has been replaced 394353358Sdim // with the precompiled headers Type stream in the mergeInPrecompHeaderObj() 395353358Sdim // call above. Note that we can't just call Types.drop_front(), as we 396353358Sdim // explicitly want to rebase the stream. 397353358Sdim CVTypeArray::Iterator firstType = types.begin(); 398353358Sdim types.setUnderlyingStream( 399353358Sdim types.getUnderlyingStream().drop_front(firstType->RecordData.size())); 400344779Sdim } 401344779Sdim 402344779Sdim // Fill in the temporary, caller-provided ObjectIndexMap. 403353358Sdim if (config->debugGHashes) { 404353358Sdim ArrayRef<GloballyHashedType> hashes; 405353358Sdim std::vector<GloballyHashedType> ownedHashes; 406353358Sdim if (Optional<ArrayRef<uint8_t>> debugH = getDebugH(file)) 407353358Sdim hashes = getHashesFromDebugH(*debugH); 408327952Sdim else { 409353358Sdim ownedHashes = GloballyHashedType::hashTypes(types); 410353358Sdim hashes = ownedHashes; 411327952Sdim } 412327952Sdim 413353358Sdim if (auto err = mergeTypeAndIdRecords( 414353358Sdim tMerger.globalIDTable, tMerger.globalTypeTable, 415353358Sdim objectIndexMap->tpiMap, types, hashes, file->pchSignature)) 416327952Sdim fatal("codeview::mergeTypeAndIdRecords failed: " + 417353358Sdim toString(std::move(err))); 418327952Sdim } else { 419353358Sdim if (auto err = mergeTypeAndIdRecords(tMerger.iDTable, tMerger.typeTable, 420353358Sdim objectIndexMap->tpiMap, types, 421353358Sdim file->pchSignature)) 422327952Sdim fatal("codeview::mergeTypeAndIdRecords failed: " + 423353358Sdim toString(std::move(err))); 424327952Sdim } 425360784Sdim 426360784Sdim if (config->showSummary) { 427360784Sdim // Count how many times we saw each type record in our input. This 428360784Sdim // calculation requires a second pass over the type records to classify each 429360784Sdim // record as a type or index. This is slow, but this code executes when 430360784Sdim // collecting statistics. 431360784Sdim tpiCounts.resize(tMerger.getTypeTable().size()); 432360784Sdim ipiCounts.resize(tMerger.getIDTable().size()); 433360784Sdim uint32_t srcIdx = 0; 434360784Sdim for (CVType &ty : types) { 435360784Sdim TypeIndex dstIdx = objectIndexMap->tpiMap[srcIdx++]; 436360784Sdim // Type merging may fail, so a complex source type may become the simple 437360784Sdim // NotTranslated type, which cannot be used as an array index. 438360784Sdim if (dstIdx.isSimple()) 439360784Sdim continue; 440360784Sdim SmallVectorImpl<uint32_t> &counts = 441360784Sdim isIdRecord(ty.kind()) ? ipiCounts : tpiCounts; 442360784Sdim ++counts[dstIdx.toArrayIndex()]; 443360784Sdim } 444360784Sdim } 445360784Sdim 446353358Sdim return *objectIndexMap; 447314564Sdim} 448314564Sdim 449353358SdimExpected<const CVIndexMap &> PDBLinker::maybeMergeTypeServerPDB(ObjFile *file) { 450353358Sdim Expected<llvm::pdb::NativeSession *> pdbSession = findTypeServerSource(file); 451353358Sdim if (!pdbSession) 452353358Sdim return pdbSession.takeError(); 453344779Sdim 454353358Sdim pdb::PDBFile &pdbFile = pdbSession.get()->getPDBFile(); 455353358Sdim pdb::InfoStream &info = cantFail(pdbFile.getPDBInfoStream()); 456327952Sdim 457353358Sdim auto it = typeServerIndexMappings.emplace(info.getGuid(), CVIndexMap()); 458353358Sdim CVIndexMap &indexMap = it.first->second; 459353358Sdim if (!it.second) 460353358Sdim return indexMap; // already merged 461321369Sdim 462321369Sdim // Mark this map as a type server map. 463353358Sdim indexMap.isTypeServerMap = true; 464321369Sdim 465353358Sdim Expected<pdb::TpiStream &> expectedTpi = pdbFile.getPDBTpiStream(); 466353358Sdim if (auto e = expectedTpi.takeError()) 467353358Sdim fatal("Type server does not have TPI stream: " + toString(std::move(e))); 468353358Sdim pdb::TpiStream *maybeIpi = nullptr; 469353358Sdim if (pdbFile.hasPDBIpiStream()) { 470353358Sdim Expected<pdb::TpiStream &> expectedIpi = pdbFile.getPDBIpiStream(); 471353358Sdim if (auto e = expectedIpi.takeError()) 472353358Sdim fatal("Error getting type server IPI stream: " + toString(std::move(e))); 473353358Sdim maybeIpi = &*expectedIpi; 474329410Sdim } 475321369Sdim 476353358Sdim if (config->debugGHashes) { 477327952Sdim // PDBs do not actually store global hashes, so when merging a type server 478327952Sdim // PDB we have to synthesize global hashes. To do this, we first synthesize 479327952Sdim // global hashes for the TPI stream, since it is independent, then we 480327952Sdim // synthesize hashes for the IPI stream, using the hashes for the TPI stream 481327952Sdim // as inputs. 482353358Sdim auto tpiHashes = GloballyHashedType::hashTypes(expectedTpi->typeArray()); 483353358Sdim Optional<uint32_t> endPrecomp; 484327952Sdim // Merge TPI first, because the IPI stream will reference type indices. 485353358Sdim if (auto err = 486353358Sdim mergeTypeRecords(tMerger.globalTypeTable, indexMap.tpiMap, 487353358Sdim expectedTpi->typeArray(), tpiHashes, endPrecomp)) 488353358Sdim fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err))); 489327952Sdim 490327952Sdim // Merge IPI. 491353358Sdim if (maybeIpi) { 492353358Sdim auto ipiHashes = 493353358Sdim GloballyHashedType::hashIds(maybeIpi->typeArray(), tpiHashes); 494353358Sdim if (auto err = 495353358Sdim mergeIdRecords(tMerger.globalIDTable, indexMap.tpiMap, 496353358Sdim indexMap.ipiMap, maybeIpi->typeArray(), ipiHashes)) 497353358Sdim fatal("codeview::mergeIdRecords failed: " + toString(std::move(err))); 498353358Sdim } 499327952Sdim } else { 500327952Sdim // Merge TPI first, because the IPI stream will reference type indices. 501353358Sdim if (auto err = mergeTypeRecords(tMerger.typeTable, indexMap.tpiMap, 502353358Sdim expectedTpi->typeArray())) 503353358Sdim fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err))); 504327952Sdim 505327952Sdim // Merge IPI. 506353358Sdim if (maybeIpi) { 507353358Sdim if (auto err = mergeIdRecords(tMerger.iDTable, indexMap.tpiMap, 508353358Sdim indexMap.ipiMap, maybeIpi->typeArray())) 509353358Sdim fatal("codeview::mergeIdRecords failed: " + toString(std::move(err))); 510353358Sdim } 511327952Sdim } 512327952Sdim 513360784Sdim if (config->showSummary) { 514360784Sdim // Count how many times we saw each type record in our input. If a 515360784Sdim // destination type index is present in the source to destination type index 516360784Sdim // map, that means we saw it once in the input. Add it to our histogram. 517360784Sdim tpiCounts.resize(tMerger.getTypeTable().size()); 518360784Sdim ipiCounts.resize(tMerger.getIDTable().size()); 519360784Sdim for (TypeIndex ti : indexMap.tpiMap) 520360784Sdim if (!ti.isSimple()) 521360784Sdim ++tpiCounts[ti.toArrayIndex()]; 522360784Sdim for (TypeIndex ti : indexMap.ipiMap) 523360784Sdim if (!ti.isSimple()) 524360784Sdim ++ipiCounts[ti.toArrayIndex()]; 525360784Sdim } 526360784Sdim 527353358Sdim return indexMap; 528321369Sdim} 529321369Sdim 530353358SdimError PDBLinker::mergeInPrecompHeaderObj(ObjFile *file, 531353358Sdim CVIndexMap *objectIndexMap) { 532353358Sdim const PrecompRecord &precomp = 533353358Sdim retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj); 534344779Sdim 535353358Sdim Expected<const CVIndexMap &> e = aquirePrecompObj(file); 536353358Sdim if (!e) 537353358Sdim return e.takeError(); 538344779Sdim 539353358Sdim const CVIndexMap &precompIndexMap = *e; 540353358Sdim assert(precompIndexMap.isPrecompiledTypeMap); 541344779Sdim 542353358Sdim if (precompIndexMap.tpiMap.empty()) 543353358Sdim return Error::success(); 544344779Sdim 545353358Sdim assert(precomp.getStartTypeIndex() == TypeIndex::FirstNonSimpleIndex); 546353358Sdim assert(precomp.getTypesCount() <= precompIndexMap.tpiMap.size()); 547344779Sdim // Use the previously remapped index map from the precompiled headers. 548353358Sdim objectIndexMap->tpiMap.append(precompIndexMap.tpiMap.begin(), 549353358Sdim precompIndexMap.tpiMap.begin() + 550353358Sdim precomp.getTypesCount()); 551353358Sdim return Error::success(); 552344779Sdim} 553344779Sdim 554344779Sdimstatic bool equals_path(StringRef path1, StringRef path2) { 555344779Sdim#if defined(_WIN32) 556344779Sdim return path1.equals_lower(path2); 557344779Sdim#else 558344779Sdim return path1.equals(path2); 559344779Sdim#endif 560344779Sdim} 561344779Sdim// Find by name an OBJ provided on the command line 562360784Sdimstatic ObjFile *findObjWithPrecompSignature(StringRef fileNameOnly, 563360784Sdim uint32_t precompSignature) { 564353358Sdim for (ObjFile *f : ObjFile::instances) { 565353358Sdim StringRef currentFileName = sys::path::filename(f->getName()); 566344779Sdim 567360784Sdim if (f->pchSignature.hasValue() && 568360784Sdim f->pchSignature.getValue() == precompSignature && 569360784Sdim equals_path(fileNameOnly, currentFileName)) 570353358Sdim return f; 571344779Sdim } 572344779Sdim return nullptr; 573344779Sdim} 574344779Sdim 575344779Sdimstd::pair<CVIndexMap &, bool /*already there*/> 576353358SdimPDBLinker::registerPrecompiledHeaders(uint32_t signature) { 577353358Sdim auto insertion = precompTypeIndexMappings.insert({signature, CVIndexMap()}); 578353358Sdim CVIndexMap &indexMap = insertion.first->second; 579353358Sdim if (!insertion.second) 580353358Sdim return {indexMap, true}; 581344779Sdim // Mark this map as a precompiled types map. 582353358Sdim indexMap.isPrecompiledTypeMap = true; 583353358Sdim return {indexMap, false}; 584344779Sdim} 585344779Sdim 586353358SdimExpected<const CVIndexMap &> PDBLinker::aquirePrecompObj(ObjFile *file) { 587353358Sdim const PrecompRecord &precomp = 588353358Sdim retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj); 589353358Sdim 590344779Sdim // First, check if we already loaded the precompiled headers object with this 591344779Sdim // signature. Return the type index mapping if we've already seen it. 592353358Sdim auto r = registerPrecompiledHeaders(precomp.getSignature()); 593353358Sdim if (r.second) 594353358Sdim return r.first; 595344779Sdim 596353358Sdim CVIndexMap &indexMap = r.first; 597344779Sdim 598344779Sdim // Cross-compile warning: given that Clang doesn't generate LF_PRECOMP 599344779Sdim // records, we assume the OBJ comes from a Windows build of cl.exe. Thusly, 600344779Sdim // the paths embedded in the OBJs are in the Windows format. 601353358Sdim SmallString<128> precompFileName = sys::path::filename( 602353358Sdim precomp.getPrecompFilePath(), sys::path::Style::windows); 603344779Sdim 604344779Sdim // link.exe requires that a precompiled headers object must always be provided 605344779Sdim // on the command-line, even if that's not necessary. 606360784Sdim auto precompFile = 607360784Sdim findObjWithPrecompSignature(precompFileName, precomp.Signature); 608353358Sdim if (!precompFile) 609344779Sdim return createFileError( 610360784Sdim precomp.getPrecompFilePath().str(), 611360784Sdim make_error<pdb::PDBError>(pdb::pdb_error_code::no_matching_pch)); 612344779Sdim 613353358Sdim addObjFile(precompFile, &indexMap); 614344779Sdim 615353358Sdim return indexMap; 616344779Sdim} 617344779Sdim 618353358Sdimstatic bool remapTypeIndex(TypeIndex &ti, ArrayRef<TypeIndex> typeIndexMap) { 619353358Sdim if (ti.isSimple()) 620321369Sdim return true; 621353358Sdim if (ti.toArrayIndex() >= typeIndexMap.size()) 622321369Sdim return false; 623353358Sdim ti = typeIndexMap[ti.toArrayIndex()]; 624321369Sdim return true; 625321369Sdim} 626321369Sdim 627353358Sdimstatic void remapTypesInSymbolRecord(ObjFile *file, SymbolKind symKind, 628353358Sdim MutableArrayRef<uint8_t> recordBytes, 629353358Sdim const CVIndexMap &indexMap, 630353358Sdim ArrayRef<TiReference> typeRefs) { 631353358Sdim MutableArrayRef<uint8_t> contents = 632353358Sdim recordBytes.drop_front(sizeof(RecordPrefix)); 633353358Sdim for (const TiReference &ref : typeRefs) { 634353358Sdim unsigned byteSize = ref.Count * sizeof(TypeIndex); 635353358Sdim if (contents.size() < ref.Offset + byteSize) 636321369Sdim fatal("symbol record too short"); 637321369Sdim 638321369Sdim // This can be an item index or a type index. Choose the appropriate map. 639353358Sdim ArrayRef<TypeIndex> typeOrItemMap = indexMap.tpiMap; 640353358Sdim bool isItemIndex = ref.Kind == TiRefKind::IndexRef; 641353358Sdim if (isItemIndex && indexMap.isTypeServerMap) 642353358Sdim typeOrItemMap = indexMap.ipiMap; 643321369Sdim 644353358Sdim MutableArrayRef<TypeIndex> tIs( 645353358Sdim reinterpret_cast<TypeIndex *>(contents.data() + ref.Offset), ref.Count); 646353358Sdim for (TypeIndex &ti : tIs) { 647353358Sdim if (!remapTypeIndex(ti, typeOrItemMap)) { 648353358Sdim log("ignoring symbol record of kind 0x" + utohexstr(symKind) + " in " + 649353358Sdim file->getName() + " with bad " + (isItemIndex ? "item" : "type") + 650353358Sdim " index 0x" + utohexstr(ti.getIndex())); 651353358Sdim ti = TypeIndex(SimpleTypeKind::NotTranslated); 652321369Sdim continue; 653321369Sdim } 654321369Sdim } 655321369Sdim } 656321369Sdim} 657321369Sdim 658341825Sdimstatic void 659353358SdimrecordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> contents, 660353358Sdim uint32_t offset, 661353358Sdim std::vector<ulittle32_t *> &strTableRefs) { 662353358Sdim contents = 663353358Sdim contents.drop_front(offset).take_front(sizeof(support::ulittle32_t)); 664353358Sdim ulittle32_t *index = reinterpret_cast<ulittle32_t *>(contents.data()); 665353358Sdim strTableRefs.push_back(index); 666341825Sdim} 667341825Sdim 668341825Sdimstatic void 669353358SdimrecordStringTableReferences(SymbolKind kind, MutableArrayRef<uint8_t> contents, 670353358Sdim std::vector<ulittle32_t *> &strTableRefs) { 671341825Sdim // For now we only handle S_FILESTATIC, but we may need the same logic for 672341825Sdim // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any 673341825Sdim // PDBs that contain these types of records, so because of the uncertainty 674341825Sdim // they are omitted here until we can prove that it's necessary. 675353358Sdim switch (kind) { 676341825Sdim case SymbolKind::S_FILESTATIC: 677341825Sdim // FileStaticSym::ModFileOffset 678353358Sdim recordStringTableReferenceAtOffset(contents, 8, strTableRefs); 679341825Sdim break; 680341825Sdim case SymbolKind::S_DEFRANGE: 681341825Sdim case SymbolKind::S_DEFRANGE_SUBFIELD: 682341825Sdim log("Not fixing up string table reference in S_DEFRANGE / " 683341825Sdim "S_DEFRANGE_SUBFIELD record"); 684341825Sdim break; 685341825Sdim default: 686341825Sdim break; 687341825Sdim } 688341825Sdim} 689341825Sdim 690353358Sdimstatic SymbolKind symbolKind(ArrayRef<uint8_t> recordData) { 691353358Sdim const RecordPrefix *prefix = 692353358Sdim reinterpret_cast<const RecordPrefix *>(recordData.data()); 693353358Sdim return static_cast<SymbolKind>(uint16_t(prefix->RecordKind)); 694321369Sdim} 695321369Sdim 696327952Sdim/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32 697353358Sdimstatic void translateIdSymbols(MutableArrayRef<uint8_t> &recordData, 698353358Sdim TypeCollection &iDTable) { 699353358Sdim RecordPrefix *prefix = reinterpret_cast<RecordPrefix *>(recordData.data()); 700327952Sdim 701353358Sdim SymbolKind kind = symbolKind(recordData); 702327952Sdim 703353358Sdim if (kind == SymbolKind::S_PROC_ID_END) { 704353358Sdim prefix->RecordKind = SymbolKind::S_END; 705327952Sdim return; 706327952Sdim } 707327952Sdim 708327952Sdim // In an object file, GPROC32_ID has an embedded reference which refers to the 709327952Sdim // single object file type index namespace. This has already been translated 710327952Sdim // to the PDB file's ID stream index space, but we need to convert this to a 711327952Sdim // symbol that refers to the type stream index space. So we remap again from 712327952Sdim // ID index space to type index space. 713353358Sdim if (kind == SymbolKind::S_GPROC32_ID || kind == SymbolKind::S_LPROC32_ID) { 714353358Sdim SmallVector<TiReference, 1> refs; 715353358Sdim auto content = recordData.drop_front(sizeof(RecordPrefix)); 716353358Sdim CVSymbol sym(recordData); 717353358Sdim discoverTypeIndicesInSymbol(sym, refs); 718353358Sdim assert(refs.size() == 1); 719353358Sdim assert(refs.front().Count == 1); 720327952Sdim 721353358Sdim TypeIndex *ti = 722353358Sdim reinterpret_cast<TypeIndex *>(content.data() + refs[0].Offset); 723353358Sdim // `ti` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in 724327952Sdim // the IPI stream, whose `FunctionType` member refers to the TPI stream. 725327952Sdim // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and 726327952Sdim // in both cases we just need the second type index. 727353358Sdim if (!ti->isSimple() && !ti->isNoneType()) { 728353358Sdim CVType funcIdData = iDTable.getType(*ti); 729353358Sdim SmallVector<TypeIndex, 2> indices; 730353358Sdim discoverTypeIndices(funcIdData, indices); 731353358Sdim assert(indices.size() == 2); 732353358Sdim *ti = indices[1]; 733327952Sdim } 734327952Sdim 735353358Sdim kind = (kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32 736327952Sdim : SymbolKind::S_LPROC32; 737353358Sdim prefix->RecordKind = uint16_t(kind); 738327952Sdim } 739327952Sdim} 740327952Sdim 741321369Sdim/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. 742321369Sdim/// The object file may not be aligned. 743344779Sdimstatic MutableArrayRef<uint8_t> 744353358SdimcopyAndAlignSymbol(const CVSymbol &sym, MutableArrayRef<uint8_t> &alignedMem) { 745353358Sdim size_t size = alignTo(sym.length(), alignOf(CodeViewContainer::Pdb)); 746353358Sdim assert(size >= 4 && "record too short"); 747353358Sdim assert(size <= MaxRecordLength && "record too long"); 748353358Sdim assert(alignedMem.size() >= size && "didn't preallocate enough"); 749321369Sdim 750321369Sdim // Copy the symbol record and zero out any padding bytes. 751353358Sdim MutableArrayRef<uint8_t> newData = alignedMem.take_front(size); 752353358Sdim alignedMem = alignedMem.drop_front(size); 753353358Sdim memcpy(newData.data(), sym.data().data(), sym.length()); 754353358Sdim memset(newData.data() + sym.length(), 0, size - sym.length()); 755321369Sdim 756321369Sdim // Update the record prefix length. It should point to the beginning of the 757327952Sdim // next record. 758353358Sdim auto *prefix = reinterpret_cast<RecordPrefix *>(newData.data()); 759353358Sdim prefix->RecordLen = size - 2; 760353358Sdim return newData; 761321369Sdim} 762321369Sdim 763321369Sdimstruct ScopeRecord { 764353358Sdim ulittle32_t ptrParent; 765353358Sdim ulittle32_t ptrEnd; 766321369Sdim}; 767321369Sdim 768321369Sdimstruct SymbolScope { 769353358Sdim ScopeRecord *openingRecord; 770353358Sdim uint32_t scopeOffset; 771321369Sdim}; 772321369Sdim 773353358Sdimstatic void scopeStackOpen(SmallVectorImpl<SymbolScope> &stack, 774353358Sdim uint32_t curOffset, CVSymbol &sym) { 775353358Sdim assert(symbolOpensScope(sym.kind())); 776353358Sdim SymbolScope s; 777353358Sdim s.scopeOffset = curOffset; 778353358Sdim s.openingRecord = const_cast<ScopeRecord *>( 779353358Sdim reinterpret_cast<const ScopeRecord *>(sym.content().data())); 780353358Sdim s.openingRecord->ptrParent = stack.empty() ? 0 : stack.back().scopeOffset; 781353358Sdim stack.push_back(s); 782321369Sdim} 783321369Sdim 784353358Sdimstatic void scopeStackClose(SmallVectorImpl<SymbolScope> &stack, 785353358Sdim uint32_t curOffset, InputFile *file) { 786353358Sdim if (stack.empty()) { 787353358Sdim warn("symbol scopes are not balanced in " + file->getName()); 788314564Sdim return; 789321369Sdim } 790353358Sdim SymbolScope s = stack.pop_back_val(); 791353358Sdim s.openingRecord->ptrEnd = curOffset; 792321369Sdim} 793314564Sdim 794353358Sdimstatic bool symbolGoesInModuleStream(const CVSymbol &sym, bool isGlobalScope) { 795353358Sdim switch (sym.kind()) { 796327952Sdim case SymbolKind::S_GDATA32: 797327952Sdim case SymbolKind::S_CONSTANT: 798327952Sdim // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 799327952Sdim // since they are synthesized by the linker in response to S_GPROC32 and 800327952Sdim // S_LPROC32, but if we do see them, don't put them in the module stream I 801327952Sdim // guess. 802327952Sdim case SymbolKind::S_PROCREF: 803327952Sdim case SymbolKind::S_LPROCREF: 804327952Sdim return false; 805344779Sdim // S_UDT records go in the module stream if it is not a global S_UDT. 806344779Sdim case SymbolKind::S_UDT: 807353358Sdim return !isGlobalScope; 808327952Sdim // S_GDATA32 does not go in the module stream, but S_LDATA32 does. 809327952Sdim case SymbolKind::S_LDATA32: 810327952Sdim default: 811327952Sdim return true; 812327952Sdim } 813327952Sdim} 814327952Sdim 815353358Sdimstatic bool symbolGoesInGlobalsStream(const CVSymbol &sym, bool isGlobalScope) { 816353358Sdim switch (sym.kind()) { 817327952Sdim case SymbolKind::S_CONSTANT: 818327952Sdim case SymbolKind::S_GDATA32: 819327952Sdim // S_LDATA32 goes in both the module stream and the globals stream. 820327952Sdim case SymbolKind::S_LDATA32: 821327952Sdim case SymbolKind::S_GPROC32: 822327952Sdim case SymbolKind::S_LPROC32: 823327952Sdim // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 824327952Sdim // since they are synthesized by the linker in response to S_GPROC32 and 825327952Sdim // S_LPROC32, but if we do see them, copy them straight through. 826327952Sdim case SymbolKind::S_PROCREF: 827327952Sdim case SymbolKind::S_LPROCREF: 828327952Sdim return true; 829344779Sdim // S_UDT records go in the globals stream if it is a global S_UDT. 830327952Sdim case SymbolKind::S_UDT: 831353358Sdim return isGlobalScope; 832327952Sdim default: 833327952Sdim return false; 834327952Sdim } 835327952Sdim} 836327952Sdim 837353358Sdimstatic void addGlobalSymbol(pdb::GSIStreamBuilder &builder, uint16_t modIndex, 838353358Sdim unsigned symOffset, const CVSymbol &sym) { 839353358Sdim switch (sym.kind()) { 840327952Sdim case SymbolKind::S_CONSTANT: 841327952Sdim case SymbolKind::S_UDT: 842327952Sdim case SymbolKind::S_GDATA32: 843327952Sdim case SymbolKind::S_LDATA32: 844327952Sdim case SymbolKind::S_PROCREF: 845327952Sdim case SymbolKind::S_LPROCREF: 846353358Sdim builder.addGlobalSymbol(sym); 847327952Sdim break; 848327952Sdim case SymbolKind::S_GPROC32: 849327952Sdim case SymbolKind::S_LPROC32: { 850353358Sdim SymbolRecordKind k = SymbolRecordKind::ProcRefSym; 851353358Sdim if (sym.kind() == SymbolKind::S_LPROC32) 852353358Sdim k = SymbolRecordKind::LocalProcRef; 853353358Sdim ProcRefSym ps(k); 854353358Sdim ps.Module = modIndex; 855327952Sdim // For some reason, MSVC seems to add one to this value. 856353358Sdim ++ps.Module; 857353358Sdim ps.Name = getSymbolName(sym); 858353358Sdim ps.SumName = 0; 859353358Sdim ps.SymOffset = symOffset; 860353358Sdim builder.addGlobalSymbol(ps); 861327952Sdim break; 862327952Sdim } 863327952Sdim default: 864327952Sdim llvm_unreachable("Invalid symbol kind!"); 865327952Sdim } 866327952Sdim} 867327952Sdim 868353358Sdimvoid PDBLinker::mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap, 869353358Sdim std::vector<ulittle32_t *> &stringTableRefs, 870353358Sdim BinaryStreamRef symData) { 871353358Sdim ArrayRef<uint8_t> symsBuffer; 872353358Sdim cantFail(symData.readBytes(0, symData.getLength(), symsBuffer)); 873353358Sdim SmallVector<SymbolScope, 4> scopes; 874314564Sdim 875344779Sdim // Iterate every symbol to check if any need to be realigned, and if so, how 876344779Sdim // much space we need to allocate for them. 877353358Sdim bool needsRealignment = false; 878353358Sdim unsigned totalRealignedSize = 0; 879353358Sdim auto ec = forEachCodeViewRecord<CVSymbol>( 880353358Sdim symsBuffer, [&](CVSymbol sym) -> llvm::Error { 881353358Sdim unsigned realignedSize = 882353358Sdim alignTo(sym.length(), alignOf(CodeViewContainer::Pdb)); 883353358Sdim needsRealignment |= realignedSize != sym.length(); 884353358Sdim totalRealignedSize += realignedSize; 885344779Sdim return Error::success(); 886344779Sdim }); 887344779Sdim 888344779Sdim // If any of the symbol record lengths was corrupt, ignore them all, warn 889344779Sdim // about it, and move on. 890353358Sdim if (ec) { 891353358Sdim warn("corrupt symbol records in " + file->getName()); 892353358Sdim consumeError(std::move(ec)); 893344779Sdim return; 894344779Sdim } 895344779Sdim 896344779Sdim // If any symbol needed realignment, allocate enough contiguous memory for 897344779Sdim // them all. Typically symbol subsections are small enough that this will not 898344779Sdim // cause fragmentation. 899353358Sdim MutableArrayRef<uint8_t> alignedSymbolMem; 900353358Sdim if (needsRealignment) { 901353358Sdim void *alignedData = 902353358Sdim alloc.Allocate(totalRealignedSize, alignOf(CodeViewContainer::Pdb)); 903353358Sdim alignedSymbolMem = makeMutableArrayRef( 904353358Sdim reinterpret_cast<uint8_t *>(alignedData), totalRealignedSize); 905344779Sdim } 906344779Sdim 907344779Sdim // Iterate again, this time doing the real work. 908353358Sdim unsigned curSymOffset = file->moduleDBI->getNextSymbolOffset(); 909353358Sdim ArrayRef<uint8_t> bulkSymbols; 910344779Sdim cantFail(forEachCodeViewRecord<CVSymbol>( 911353358Sdim symsBuffer, [&](CVSymbol sym) -> llvm::Error { 912344779Sdim // Align the record if required. 913353358Sdim MutableArrayRef<uint8_t> recordBytes; 914353358Sdim if (needsRealignment) { 915353358Sdim recordBytes = copyAndAlignSymbol(sym, alignedSymbolMem); 916353358Sdim sym = CVSymbol(recordBytes); 917344779Sdim } else { 918344779Sdim // Otherwise, we can actually mutate the symbol directly, since we 919344779Sdim // copied it to apply relocations. 920353358Sdim recordBytes = makeMutableArrayRef( 921353358Sdim const_cast<uint8_t *>(sym.data().data()), sym.length()); 922344779Sdim } 923344779Sdim 924341825Sdim // Discover type index references in the record. Skip it if we don't 925341825Sdim // know where they are. 926353358Sdim SmallVector<TiReference, 32> typeRefs; 927353358Sdim if (!discoverTypeIndicesInSymbol(sym, typeRefs)) { 928341825Sdim log("ignoring unknown symbol record with kind 0x" + 929353358Sdim utohexstr(sym.kind())); 930341825Sdim return Error::success(); 931341825Sdim } 932321369Sdim 933341825Sdim // Re-map all the type index references. 934353358Sdim remapTypesInSymbolRecord(file, sym.kind(), recordBytes, indexMap, 935353358Sdim typeRefs); 936327952Sdim 937341825Sdim // An object file may have S_xxx_ID symbols, but these get converted to 938341825Sdim // "real" symbols in a PDB. 939353358Sdim translateIdSymbols(recordBytes, tMerger.getIDTable()); 940353358Sdim sym = CVSymbol(recordBytes); 941327952Sdim 942341825Sdim // If this record refers to an offset in the object file's string table, 943341825Sdim // add that item to the global PDB string table and re-write the index. 944353358Sdim recordStringTableReferences(sym.kind(), recordBytes, stringTableRefs); 945321369Sdim 946341825Sdim // Fill in "Parent" and "End" fields by maintaining a stack of scopes. 947353358Sdim if (symbolOpensScope(sym.kind())) 948353358Sdim scopeStackOpen(scopes, curSymOffset, sym); 949353358Sdim else if (symbolEndsScope(sym.kind())) 950353358Sdim scopeStackClose(scopes, curSymOffset, file); 951341825Sdim 952341825Sdim // Add the symbol to the globals stream if necessary. Do this before 953341825Sdim // adding the symbol to the module since we may need to get the next 954341825Sdim // symbol offset, and writing to the module's symbol stream will update 955341825Sdim // that offset. 956353358Sdim if (symbolGoesInGlobalsStream(sym, scopes.empty())) { 957353358Sdim addGlobalSymbol(builder.getGsiBuilder(), 958353358Sdim file->moduleDBI->getModuleIndex(), curSymOffset, sym); 959353358Sdim ++globalSymbols; 960353358Sdim } 961341825Sdim 962353358Sdim if (symbolGoesInModuleStream(sym, scopes.empty())) { 963344779Sdim // Add symbols to the module in bulk. If this symbol is contiguous 964344779Sdim // with the previous run of symbols to add, combine the ranges. If 965344779Sdim // not, close the previous range of symbols and start a new one. 966353358Sdim if (sym.data().data() == bulkSymbols.end()) { 967353358Sdim bulkSymbols = makeArrayRef(bulkSymbols.data(), 968353358Sdim bulkSymbols.size() + sym.length()); 969344779Sdim } else { 970353358Sdim file->moduleDBI->addSymbolsInBulk(bulkSymbols); 971353358Sdim bulkSymbols = recordBytes; 972344779Sdim } 973353358Sdim curSymOffset += sym.length(); 974353358Sdim ++moduleSymbols; 975344779Sdim } 976341825Sdim return Error::success(); 977344779Sdim })); 978344779Sdim 979344779Sdim // Add any remaining symbols we've accumulated. 980353358Sdim file->moduleDBI->addSymbolsInBulk(bulkSymbols); 981314564Sdim} 982314564Sdim 983344779Sdim// Allocate memory for a .debug$S / .debug$F section and relocate it. 984353358Sdimstatic ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &alloc, 985353358Sdim SectionChunk &debugChunk) { 986353358Sdim uint8_t *buffer = alloc.Allocate<uint8_t>(debugChunk.getSize()); 987353358Sdim assert(debugChunk.getOutputSectionIdx() == 0 && 988321369Sdim "debug sections should not be in output sections"); 989353358Sdim debugChunk.writeTo(buffer); 990353358Sdim return makeArrayRef(buffer, debugChunk.getSize()); 991321369Sdim} 992314564Sdim 993353358Sdimstatic pdb::SectionContrib createSectionContrib(const Chunk *c, uint32_t modi) { 994353358Sdim OutputSection *os = c ? c->getOutputSection() : nullptr; 995353358Sdim pdb::SectionContrib sc; 996353358Sdim memset(&sc, 0, sizeof(sc)); 997353358Sdim sc.ISect = os ? os->sectionIndex : llvm::pdb::kInvalidStreamIndex; 998353358Sdim sc.Off = c && os ? c->getRVA() - os->getRVA() : 0; 999353358Sdim sc.Size = c ? c->getSize() : -1; 1000353358Sdim if (auto *secChunk = dyn_cast_or_null<SectionChunk>(c)) { 1001353358Sdim sc.Characteristics = secChunk->header->Characteristics; 1002353358Sdim sc.Imod = secChunk->file->moduleDBI->getModuleIndex(); 1003353358Sdim ArrayRef<uint8_t> contents = secChunk->getContents(); 1004353358Sdim JamCRC crc(0); 1005360784Sdim crc.update(contents); 1006353358Sdim sc.DataCrc = crc.getCRC(); 1007341825Sdim } else { 1008353358Sdim sc.Characteristics = os ? os->header.Characteristics : 0; 1009353358Sdim sc.Imod = modi; 1010341825Sdim } 1011353358Sdim sc.RelocCrc = 0; // FIXME 1012341825Sdim 1013353358Sdim return sc; 1014341825Sdim} 1015341825Sdim 1016344779Sdimstatic uint32_t 1017353358SdimtranslateStringTableIndex(uint32_t objIndex, 1018353358Sdim const DebugStringTableSubsectionRef &objStrTable, 1019353358Sdim DebugStringTableSubsection &pdbStrTable) { 1020353358Sdim auto expectedString = objStrTable.getString(objIndex); 1021353358Sdim if (!expectedString) { 1022344779Sdim warn("Invalid string table reference"); 1023353358Sdim consumeError(expectedString.takeError()); 1024344779Sdim return 0; 1025344779Sdim } 1026344779Sdim 1027353358Sdim return pdbStrTable.insert(*expectedString); 1028344779Sdim} 1029344779Sdim 1030353358Sdimvoid DebugSHandler::handleDebugS(lld::coff::SectionChunk &debugS) { 1031353358Sdim DebugSubsectionArray subsections; 1032344779Sdim 1033353358Sdim ArrayRef<uint8_t> relocatedDebugContents = SectionChunk::consumeDebugMagic( 1034353358Sdim relocateDebugChunk(linker.alloc, debugS), debugS.getSectionName()); 1035344779Sdim 1036353358Sdim BinaryStreamReader reader(relocatedDebugContents, support::little); 1037353358Sdim exitOnErr(reader.readArray(subsections, relocatedDebugContents.size())); 1038344779Sdim 1039353358Sdim for (const DebugSubsectionRecord &ss : subsections) { 1040353358Sdim // Ignore subsections with the 'ignore' bit. Some versions of the Visual C++ 1041353358Sdim // runtime have subsections with this bit set. 1042353358Sdim if (uint32_t(ss.kind()) & codeview::SubsectionIgnoreFlag) 1043353358Sdim continue; 1044353358Sdim 1045353358Sdim switch (ss.kind()) { 1046344779Sdim case DebugSubsectionKind::StringTable: { 1047353358Sdim assert(!cVStrTab.valid() && 1048344779Sdim "Encountered multiple string table subsections!"); 1049353358Sdim exitOnErr(cVStrTab.initialize(ss.getRecordData())); 1050344779Sdim break; 1051344779Sdim } 1052344779Sdim case DebugSubsectionKind::FileChecksums: 1053353358Sdim assert(!checksums.valid() && 1054344779Sdim "Encountered multiple checksum subsections!"); 1055353358Sdim exitOnErr(checksums.initialize(ss.getRecordData())); 1056344779Sdim break; 1057344779Sdim case DebugSubsectionKind::Lines: 1058344779Sdim // We can add the relocated line table directly to the PDB without 1059344779Sdim // modification because the file checksum offsets will stay the same. 1060353358Sdim file.moduleDBI->addDebugSubsection(ss); 1061344779Sdim break; 1062353358Sdim case DebugSubsectionKind::InlineeLines: 1063353358Sdim assert(!inlineeLines.valid() && 1064353358Sdim "Encountered multiple inlinee lines subsections!"); 1065353358Sdim exitOnErr(inlineeLines.initialize(ss.getRecordData())); 1066353358Sdim break; 1067344779Sdim case DebugSubsectionKind::FrameData: { 1068344779Sdim // We need to re-write string table indices here, so save off all 1069344779Sdim // frame data subsections until we've processed the entire list of 1070344779Sdim // subsections so that we can be sure we have the string table. 1071353358Sdim DebugFrameDataSubsectionRef fds; 1072353358Sdim exitOnErr(fds.initialize(ss.getRecordData())); 1073353358Sdim newFpoFrames.push_back(std::move(fds)); 1074344779Sdim break; 1075344779Sdim } 1076344779Sdim case DebugSubsectionKind::Symbols: { 1077353358Sdim linker.mergeSymbolRecords(&file, indexMap, stringTableReferences, 1078353358Sdim ss.getRecordData()); 1079344779Sdim break; 1080344779Sdim } 1081353358Sdim 1082353358Sdim case DebugSubsectionKind::CrossScopeImports: 1083353358Sdim case DebugSubsectionKind::CrossScopeExports: 1084353358Sdim // These appear to relate to cross-module optimization, so we might use 1085353358Sdim // these for ThinLTO. 1086353358Sdim break; 1087353358Sdim 1088353358Sdim case DebugSubsectionKind::ILLines: 1089353358Sdim case DebugSubsectionKind::FuncMDTokenMap: 1090353358Sdim case DebugSubsectionKind::TypeMDTokenMap: 1091353358Sdim case DebugSubsectionKind::MergedAssemblyInput: 1092353358Sdim // These appear to relate to .Net assembly info. 1093353358Sdim break; 1094353358Sdim 1095353358Sdim case DebugSubsectionKind::CoffSymbolRVA: 1096353358Sdim // Unclear what this is for. 1097353358Sdim break; 1098353358Sdim 1099344779Sdim default: 1100353358Sdim warn("ignoring unknown debug$S subsection kind 0x" + 1101353358Sdim utohexstr(uint32_t(ss.kind())) + " in file " + toString(&file)); 1102344779Sdim break; 1103344779Sdim } 1104344779Sdim } 1105344779Sdim} 1106344779Sdim 1107353358Sdimstatic Expected<StringRef> 1108353358SdimgetFileName(const DebugStringTableSubsectionRef &strings, 1109353358Sdim const DebugChecksumsSubsectionRef &checksums, uint32_t fileID) { 1110353358Sdim auto iter = checksums.getArray().at(fileID); 1111353358Sdim if (iter == checksums.getArray().end()) 1112353358Sdim return make_error<CodeViewError>(cv_error_code::no_records); 1113353358Sdim uint32_t offset = iter->FileNameOffset; 1114353358Sdim return strings.getString(offset); 1115353358Sdim} 1116353358Sdim 1117353358Sdimstd::shared_ptr<DebugInlineeLinesSubsection> 1118353358SdimDebugSHandler::mergeInlineeLines(DebugChecksumsSubsection *newChecksums) { 1119353358Sdim auto newInlineeLines = std::make_shared<DebugInlineeLinesSubsection>( 1120353358Sdim *newChecksums, inlineeLines.hasExtraFiles()); 1121353358Sdim 1122353358Sdim for (const InlineeSourceLine &line : inlineeLines) { 1123353358Sdim TypeIndex inlinee = line.Header->Inlinee; 1124353358Sdim uint32_t fileID = line.Header->FileID; 1125353358Sdim uint32_t sourceLine = line.Header->SourceLineNum; 1126353358Sdim 1127353358Sdim ArrayRef<TypeIndex> typeOrItemMap = 1128353358Sdim indexMap.isTypeServerMap ? indexMap.ipiMap : indexMap.tpiMap; 1129353358Sdim if (!remapTypeIndex(inlinee, typeOrItemMap)) { 1130353358Sdim log("ignoring inlinee line record in " + file.getName() + 1131353358Sdim " with bad inlinee index 0x" + utohexstr(inlinee.getIndex())); 1132353358Sdim continue; 1133353358Sdim } 1134353358Sdim 1135353358Sdim SmallString<128> filename = 1136353358Sdim exitOnErr(getFileName(cVStrTab, checksums, fileID)); 1137353358Sdim pdbMakeAbsolute(filename); 1138353358Sdim newInlineeLines->addInlineSite(inlinee, filename, sourceLine); 1139353358Sdim 1140353358Sdim if (inlineeLines.hasExtraFiles()) { 1141353358Sdim for (uint32_t extraFileId : line.ExtraFiles) { 1142353358Sdim filename = exitOnErr(getFileName(cVStrTab, checksums, extraFileId)); 1143353358Sdim pdbMakeAbsolute(filename); 1144353358Sdim newInlineeLines->addExtraFile(filename); 1145353358Sdim } 1146353358Sdim } 1147353358Sdim } 1148353358Sdim 1149353358Sdim return newInlineeLines; 1150353358Sdim} 1151353358Sdim 1152344779Sdimvoid DebugSHandler::finish() { 1153353358Sdim pdb::DbiStreamBuilder &dbiBuilder = linker.builder.getDbiBuilder(); 1154344779Sdim 1155344779Sdim // We should have seen all debug subsections across the entire object file now 1156344779Sdim // which means that if a StringTable subsection and Checksums subsection were 1157344779Sdim // present, now is the time to handle them. 1158353358Sdim if (!cVStrTab.valid()) { 1159353358Sdim if (checksums.valid()) 1160344779Sdim fatal(".debug$S sections with a checksums subsection must also contain a " 1161344779Sdim "string table subsection"); 1162344779Sdim 1163353358Sdim if (!stringTableReferences.empty()) 1164344779Sdim warn("No StringTable subsection was encountered, but there are string " 1165344779Sdim "table references"); 1166344779Sdim return; 1167344779Sdim } 1168344779Sdim 1169344779Sdim // Rewrite string table indices in the Fpo Data and symbol records to refer to 1170344779Sdim // the global PDB string table instead of the object file string table. 1171353358Sdim for (DebugFrameDataSubsectionRef &fds : newFpoFrames) { 1172353358Sdim const ulittle32_t *reloc = fds.getRelocPtr(); 1173353358Sdim for (codeview::FrameData fd : fds) { 1174353358Sdim fd.RvaStart += *reloc; 1175353358Sdim fd.FrameFunc = 1176353358Sdim translateStringTableIndex(fd.FrameFunc, cVStrTab, linker.pdbStrTab); 1177353358Sdim dbiBuilder.addNewFpoData(fd); 1178344779Sdim } 1179344779Sdim } 1180344779Sdim 1181353358Sdim for (ulittle32_t *ref : stringTableReferences) 1182353358Sdim *ref = translateStringTableIndex(*ref, cVStrTab, linker.pdbStrTab); 1183344779Sdim 1184344779Sdim // Make a new file checksum table that refers to offsets in the PDB-wide 1185344779Sdim // string table. Generally the string table subsection appears after the 1186344779Sdim // checksum table, so we have to do this after looping over all the 1187344779Sdim // subsections. 1188360784Sdim auto newChecksums = std::make_unique<DebugChecksumsSubsection>(linker.pdbStrTab); 1189353358Sdim for (FileChecksumEntry &fc : checksums) { 1190353358Sdim SmallString<128> filename = 1191353358Sdim exitOnErr(cVStrTab.getString(fc.FileNameOffset)); 1192353358Sdim pdbMakeAbsolute(filename); 1193353358Sdim exitOnErr(dbiBuilder.addModuleSourceFile(*file.moduleDBI, filename)); 1194353358Sdim newChecksums->addChecksum(filename, fc.Kind, fc.Checksum); 1195344779Sdim } 1196353358Sdim 1197353358Sdim // Rewrite inlinee item indices if present. 1198353358Sdim if (inlineeLines.valid()) 1199353358Sdim file.moduleDBI->addDebugSubsection(mergeInlineeLines(newChecksums.get())); 1200353358Sdim 1201353358Sdim file.moduleDBI->addDebugSubsection(std::move(newChecksums)); 1202344779Sdim} 1203344779Sdim 1204353358Sdimvoid PDBLinker::addObjFile(ObjFile *file, CVIndexMap *externIndexMap) { 1205353358Sdim if (file->mergedIntoPDB) 1206344779Sdim return; 1207353358Sdim file->mergedIntoPDB = true; 1208321369Sdim 1209321369Sdim // Before we can process symbol substreams from .debug$S, we need to process 1210321369Sdim // type information, file checksums, and the string table. Add type info to 1211321369Sdim // the PDB first, so that we can get the map from object file type and item 1212321369Sdim // indices to PDB type and item indices. 1213353358Sdim CVIndexMap objectIndexMap; 1214353358Sdim auto indexMapResult = 1215353358Sdim mergeDebugT(file, externIndexMap ? externIndexMap : &objectIndexMap); 1216321369Sdim 1217329410Sdim // If the .debug$T sections fail to merge, assume there is no debug info. 1218353358Sdim if (!indexMapResult) { 1219353358Sdim if (!config->warnDebugInfoUnusable) { 1220353358Sdim consumeError(indexMapResult.takeError()); 1221344779Sdim return; 1222344779Sdim } 1223353358Sdim warn("Cannot use debug info for '" + toString(file) + "' [LNK4099]\n" + 1224344779Sdim ">>> failed to load reference " + 1225353358Sdim StringRef(toString(indexMapResult.takeError()))); 1226329410Sdim return; 1227329410Sdim } 1228329410Sdim 1229353358Sdim ScopedTimer t(symbolMergingTimer); 1230341825Sdim 1231353358Sdim pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1232353358Sdim DebugSHandler dsh(*this, *file, *indexMapResult); 1233344779Sdim // Now do all live .debug$S and .debug$F sections. 1234353358Sdim for (SectionChunk *debugChunk : file->getDebugChunks()) { 1235353358Sdim if (!debugChunk->live || debugChunk->getSize() == 0) 1236321369Sdim continue; 1237321369Sdim 1238353358Sdim if (debugChunk->getSectionName() == ".debug$S") { 1239353358Sdim dsh.handleDebugS(*debugChunk); 1240321369Sdim continue; 1241321369Sdim } 1242321369Sdim 1243353358Sdim if (debugChunk->getSectionName() == ".debug$F") { 1244353358Sdim ArrayRef<uint8_t> relocatedDebugContents = 1245353358Sdim relocateDebugChunk(alloc, *debugChunk); 1246341825Sdim 1247353358Sdim FixedStreamArray<object::FpoData> fpoRecords; 1248353358Sdim BinaryStreamReader reader(relocatedDebugContents, support::little); 1249353358Sdim uint32_t count = relocatedDebugContents.size() / sizeof(object::FpoData); 1250353358Sdim exitOnErr(reader.readArray(fpoRecords, count)); 1251341825Sdim 1252344779Sdim // These are already relocated and don't refer to the string table, so we 1253344779Sdim // can just copy it. 1254353358Sdim for (const object::FpoData &fd : fpoRecords) 1255353358Sdim dbiBuilder.addOldFpoData(fd); 1256341825Sdim continue; 1257321369Sdim } 1258314564Sdim } 1259341825Sdim 1260344779Sdim // Do any post-processing now that all .debug$S sections have been processed. 1261353358Sdim dsh.finish(); 1262314564Sdim} 1263314564Sdim 1264353358Sdim// Add a module descriptor for every object file. We need to put an absolute 1265353358Sdim// path to the object into the PDB. If this is a plain object, we make its 1266353358Sdim// path absolute. If it's an object in an archive, we make the archive path 1267353358Sdim// absolute. 1268353358Sdimstatic void createModuleDBI(pdb::PDBFileBuilder &builder) { 1269353358Sdim pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1270353358Sdim SmallString<128> objName; 1271353358Sdim 1272353358Sdim for (ObjFile *file : ObjFile::instances) { 1273353358Sdim 1274353358Sdim bool inArchive = !file->parentName.empty(); 1275353358Sdim objName = inArchive ? file->parentName : file->getName(); 1276353358Sdim pdbMakeAbsolute(objName); 1277353358Sdim StringRef modName = inArchive ? file->getName() : StringRef(objName); 1278353358Sdim 1279353358Sdim file->moduleDBI = &exitOnErr(dbiBuilder.addModuleInfo(modName)); 1280353358Sdim file->moduleDBI->setObjFileName(objName); 1281353358Sdim 1282353358Sdim ArrayRef<Chunk *> chunks = file->getChunks(); 1283353358Sdim uint32_t modi = file->moduleDBI->getModuleIndex(); 1284353358Sdim 1285353358Sdim for (Chunk *c : chunks) { 1286353358Sdim auto *secChunk = dyn_cast<SectionChunk>(c); 1287353358Sdim if (!secChunk || !secChunk->live) 1288353358Sdim continue; 1289353358Sdim pdb::SectionContrib sc = createSectionContrib(secChunk, modi); 1290353358Sdim file->moduleDBI->setFirstSectionContrib(sc); 1291353358Sdim break; 1292353358Sdim } 1293327952Sdim } 1294353358Sdim} 1295327952Sdim 1296353358Sdimstatic PublicSym32 createPublic(Defined *def) { 1297353358Sdim PublicSym32 pub(SymbolKind::S_PUB32); 1298353358Sdim pub.Name = def->getName(); 1299353358Sdim if (auto *d = dyn_cast<DefinedCOFF>(def)) { 1300353358Sdim if (d->getCOFFSymbol().isFunctionDefinition()) 1301353358Sdim pub.Flags = PublicSymFlags::Function; 1302353358Sdim } else if (isa<DefinedImportThunk>(def)) { 1303353358Sdim pub.Flags = PublicSymFlags::Function; 1304353358Sdim } 1305353358Sdim 1306353358Sdim OutputSection *os = def->getChunk()->getOutputSection(); 1307353358Sdim assert(os && "all publics should be in final image"); 1308353358Sdim pub.Offset = def->getRVA() - os->getRVA(); 1309353358Sdim pub.Segment = os->sectionIndex; 1310353358Sdim return pub; 1311327952Sdim} 1312327952Sdim 1313321369Sdim// Add all object files to the PDB. Merge .debug$T sections into IpiData and 1314321369Sdim// TpiData. 1315321369Sdimvoid PDBLinker::addObjectsToPDB() { 1316353358Sdim ScopedTimer t1(addObjectsTimer); 1317321369Sdim 1318353358Sdim createModuleDBI(builder); 1319321369Sdim 1320353358Sdim for (ObjFile *file : ObjFile::instances) 1321353358Sdim addObjFile(file); 1322353358Sdim 1323353358Sdim builder.getStringTableBuilder().setStrings(pdbStrTab); 1324353358Sdim t1.stop(); 1325353358Sdim 1326327952Sdim // Construct TPI and IPI stream contents. 1327353358Sdim ScopedTimer t2(tpiStreamLayoutTimer); 1328353358Sdim addTypeInfo(builder.getTpiBuilder(), tMerger.getTypeTable()); 1329353358Sdim addTypeInfo(builder.getIpiBuilder(), tMerger.getIDTable()); 1330353358Sdim t2.stop(); 1331321369Sdim 1332353358Sdim ScopedTimer t3(globalsLayoutTimer); 1333327952Sdim // Compute the public and global symbols. 1334353358Sdim auto &gsiBuilder = builder.getGsiBuilder(); 1335353358Sdim std::vector<PublicSym32> publics; 1336353358Sdim symtab->forEachSymbol([&publics](Symbol *s) { 1337327952Sdim // Only emit defined, live symbols that have a chunk. 1338353358Sdim auto *def = dyn_cast<Defined>(s); 1339353358Sdim if (def && def->isLive() && def->getChunk()) 1340353358Sdim publics.push_back(createPublic(def)); 1341327952Sdim }); 1342321369Sdim 1343353358Sdim if (!publics.empty()) { 1344353358Sdim publicSymbols = publics.size(); 1345327952Sdim // Sort the public symbols and add them to the stream. 1346353358Sdim parallelSort(publics, [](const PublicSym32 &l, const PublicSym32 &r) { 1347353358Sdim return l.Name < r.Name; 1348353358Sdim }); 1349353358Sdim for (const PublicSym32 &pub : publics) 1350353358Sdim gsiBuilder.addPublicSymbol(pub); 1351327952Sdim } 1352314564Sdim} 1353314564Sdim 1354353358Sdimvoid PDBLinker::printStats() { 1355353358Sdim if (!config->showSummary) 1356353358Sdim return; 1357353358Sdim 1358353358Sdim SmallString<256> buffer; 1359353358Sdim raw_svector_ostream stream(buffer); 1360353358Sdim 1361353358Sdim stream << center_justify("Summary", 80) << '\n' 1362353358Sdim << std::string(80, '-') << '\n'; 1363353358Sdim 1364353358Sdim auto print = [&](uint64_t v, StringRef s) { 1365353358Sdim stream << format_decimal(v, 15) << " " << s << '\n'; 1366353358Sdim }; 1367353358Sdim 1368353358Sdim print(ObjFile::instances.size(), 1369353358Sdim "Input OBJ files (expanded from all cmd-line inputs)"); 1370353358Sdim print(typeServerIndexMappings.size(), "PDB type server dependencies"); 1371353358Sdim print(precompTypeIndexMappings.size(), "Precomp OBJ dependencies"); 1372353358Sdim print(tMerger.getTypeTable().size() + tMerger.getIDTable().size(), 1373353358Sdim "Merged TPI records"); 1374353358Sdim print(pdbStrTab.size(), "Output PDB strings"); 1375353358Sdim print(globalSymbols, "Global symbol records"); 1376353358Sdim print(moduleSymbols, "Module symbol records"); 1377353358Sdim print(publicSymbols, "Public symbol records"); 1378353358Sdim 1379360784Sdim auto printLargeInputTypeRecs = [&](StringRef name, 1380360784Sdim ArrayRef<uint32_t> recCounts, 1381360784Sdim TypeCollection &records) { 1382360784Sdim // Figure out which type indices were responsible for the most duplicate 1383360784Sdim // bytes in the input files. These should be frequently emitted LF_CLASS and 1384360784Sdim // LF_FIELDLIST records. 1385360784Sdim struct TypeSizeInfo { 1386360784Sdim uint32_t typeSize; 1387360784Sdim uint32_t dupCount; 1388360784Sdim TypeIndex typeIndex; 1389360784Sdim uint64_t totalInputSize() const { return uint64_t(dupCount) * typeSize; } 1390360784Sdim bool operator<(const TypeSizeInfo &rhs) const { 1391360784Sdim return totalInputSize() < rhs.totalInputSize(); 1392360784Sdim } 1393360784Sdim }; 1394360784Sdim SmallVector<TypeSizeInfo, 0> tsis; 1395360784Sdim for (auto e : enumerate(recCounts)) { 1396360784Sdim TypeIndex typeIndex = TypeIndex::fromArrayIndex(e.index()); 1397360784Sdim uint32_t typeSize = records.getType(typeIndex).length(); 1398360784Sdim uint32_t dupCount = e.value(); 1399360784Sdim tsis.push_back({typeSize, dupCount, typeIndex}); 1400360784Sdim } 1401360784Sdim 1402360784Sdim if (!tsis.empty()) { 1403360784Sdim stream << "\nTop 10 types responsible for the most " << name 1404360784Sdim << " input:\n"; 1405360784Sdim stream << " index total bytes count size\n"; 1406360784Sdim llvm::sort(tsis); 1407360784Sdim unsigned i = 0; 1408360784Sdim for (const auto &tsi : reverse(tsis)) { 1409360784Sdim stream << formatv(" {0,10:X}: {1,14:N} = {2,5:N} * {3,6:N}\n", 1410360784Sdim tsi.typeIndex.getIndex(), tsi.totalInputSize(), 1411360784Sdim tsi.dupCount, tsi.typeSize); 1412360784Sdim if (++i >= 10) 1413360784Sdim break; 1414360784Sdim } 1415360784Sdim stream 1416360784Sdim << "Run llvm-pdbutil to print details about a particular record:\n"; 1417360784Sdim stream << formatv("llvm-pdbutil dump -{0}s -{0}-index {1:X} {2}\n", 1418360784Sdim (name == "TPI" ? "type" : "id"), 1419360784Sdim tsis.back().typeIndex.getIndex(), config->pdbPath); 1420360784Sdim } 1421360784Sdim }; 1422360784Sdim 1423360784Sdim printLargeInputTypeRecs("TPI", tpiCounts, tMerger.getTypeTable()); 1424360784Sdim printLargeInputTypeRecs("IPI", ipiCounts, tMerger.getIDTable()); 1425360784Sdim 1426353358Sdim message(buffer); 1427353358Sdim} 1428353358Sdim 1429341825Sdimvoid PDBLinker::addNatvisFiles() { 1430353358Sdim for (StringRef file : config->natvisFiles) { 1431353358Sdim ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr = 1432353358Sdim MemoryBuffer::getFile(file); 1433353358Sdim if (!dataOrErr) { 1434353358Sdim warn("Cannot open input file: " + file); 1435341825Sdim continue; 1436341825Sdim } 1437353358Sdim builder.addInjectedSource(file, std::move(*dataOrErr)); 1438341825Sdim } 1439341825Sdim} 1440341825Sdim 1441353358Sdimstatic codeview::CPUType toCodeViewMachine(COFF::MachineTypes machine) { 1442353358Sdim switch (machine) { 1443341825Sdim case COFF::IMAGE_FILE_MACHINE_AMD64: 1444341825Sdim return codeview::CPUType::X64; 1445341825Sdim case COFF::IMAGE_FILE_MACHINE_ARM: 1446341825Sdim return codeview::CPUType::ARM7; 1447341825Sdim case COFF::IMAGE_FILE_MACHINE_ARM64: 1448341825Sdim return codeview::CPUType::ARM64; 1449341825Sdim case COFF::IMAGE_FILE_MACHINE_ARMNT: 1450341825Sdim return codeview::CPUType::ARMNT; 1451341825Sdim case COFF::IMAGE_FILE_MACHINE_I386: 1452341825Sdim return codeview::CPUType::Intel80386; 1453341825Sdim default: 1454341825Sdim llvm_unreachable("Unsupported CPU Type"); 1455341825Sdim } 1456341825Sdim} 1457341825Sdim 1458344779Sdim// Mimic MSVC which surrounds arguments containing whitespace with quotes. 1459344779Sdim// Double double-quotes are handled, so that the resulting string can be 1460344779Sdim// executed again on the cmd-line. 1461353358Sdimstatic std::string quote(ArrayRef<StringRef> args) { 1462353358Sdim std::string r; 1463353358Sdim r.reserve(256); 1464353358Sdim for (StringRef a : args) { 1465353358Sdim if (!r.empty()) 1466353358Sdim r.push_back(' '); 1467353358Sdim bool hasWS = a.find(' ') != StringRef::npos; 1468353358Sdim bool hasQ = a.find('"') != StringRef::npos; 1469353358Sdim if (hasWS || hasQ) 1470353358Sdim r.push_back('"'); 1471353358Sdim if (hasQ) { 1472353358Sdim SmallVector<StringRef, 4> s; 1473353358Sdim a.split(s, '"'); 1474353358Sdim r.append(join(s, "\"\"")); 1475344779Sdim } else { 1476353358Sdim r.append(a); 1477344779Sdim } 1478353358Sdim if (hasWS || hasQ) 1479353358Sdim r.push_back('"'); 1480344779Sdim } 1481353358Sdim return r; 1482344779Sdim} 1483344779Sdim 1484353358Sdimstatic void fillLinkerVerRecord(Compile3Sym &cs) { 1485353358Sdim cs.Machine = toCodeViewMachine(config->machine); 1486327952Sdim // Interestingly, if we set the string to 0.0.0.0, then when trying to view 1487327952Sdim // local variables WinDbg emits an error that private symbols are not present. 1488327952Sdim // By setting this to a valid MSVC linker version string, local variables are 1489327952Sdim // displayed properly. As such, even though it is not representative of 1490327952Sdim // LLVM's version information, we need this for compatibility. 1491353358Sdim cs.Flags = CompileSym3Flags::None; 1492353358Sdim cs.VersionBackendBuild = 25019; 1493353358Sdim cs.VersionBackendMajor = 14; 1494353358Sdim cs.VersionBackendMinor = 10; 1495353358Sdim cs.VersionBackendQFE = 0; 1496327952Sdim 1497327952Sdim // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the 1498327952Sdim // linker module (which is by definition a backend), so we don't need to do 1499327952Sdim // anything here. Also, it seems we can use "LLVM Linker" for the linker name 1500327952Sdim // without any problems. Only the backend version has to be hardcoded to a 1501327952Sdim // magic number. 1502353358Sdim cs.VersionFrontendBuild = 0; 1503353358Sdim cs.VersionFrontendMajor = 0; 1504353358Sdim cs.VersionFrontendMinor = 0; 1505353358Sdim cs.VersionFrontendQFE = 0; 1506353358Sdim cs.Version = "LLVM Linker"; 1507353358Sdim cs.setLanguage(SourceLanguage::Link); 1508353358Sdim} 1509321369Sdim 1510353358Sdimstatic void addCommonLinkerModuleSymbols(StringRef path, 1511353358Sdim pdb::DbiModuleDescriptorBuilder &mod, 1512353358Sdim BumpPtrAllocator &allocator) { 1513353358Sdim ObjNameSym ons(SymbolRecordKind::ObjNameSym); 1514353358Sdim EnvBlockSym ebs(SymbolRecordKind::EnvBlockSym); 1515353358Sdim Compile3Sym cs(SymbolRecordKind::Compile3Sym); 1516353358Sdim fillLinkerVerRecord(cs); 1517353358Sdim 1518353358Sdim ons.Name = "* Linker *"; 1519353358Sdim ons.Signature = 0; 1520353358Sdim 1521353358Sdim ArrayRef<StringRef> args = makeArrayRef(config->argv).drop_front(); 1522353358Sdim std::string argStr = quote(args); 1523353358Sdim ebs.Fields.push_back("cwd"); 1524321369Sdim SmallString<64> cwd; 1525353358Sdim if (config->pdbSourcePath.empty()) 1526344779Sdim sys::fs::current_path(cwd); 1527344779Sdim else 1528353358Sdim cwd = config->pdbSourcePath; 1529353358Sdim ebs.Fields.push_back(cwd); 1530353358Sdim ebs.Fields.push_back("exe"); 1531353358Sdim SmallString<64> exe = config->argv[0]; 1532344779Sdim pdbMakeAbsolute(exe); 1533353358Sdim ebs.Fields.push_back(exe); 1534353358Sdim ebs.Fields.push_back("pdb"); 1535353358Sdim ebs.Fields.push_back(path); 1536353358Sdim ebs.Fields.push_back("cmd"); 1537353358Sdim ebs.Fields.push_back(argStr); 1538353358Sdim mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1539353358Sdim ons, allocator, CodeViewContainer::Pdb)); 1540353358Sdim mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1541353358Sdim cs, allocator, CodeViewContainer::Pdb)); 1542353358Sdim mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1543353358Sdim ebs, allocator, CodeViewContainer::Pdb)); 1544321369Sdim} 1545321369Sdim 1546353358Sdimstatic void addLinkerModuleCoffGroup(PartialSection *sec, 1547353358Sdim pdb::DbiModuleDescriptorBuilder &mod, 1548353358Sdim OutputSection &os, 1549353358Sdim BumpPtrAllocator &allocator) { 1550353358Sdim // If there's a section, there's at least one chunk 1551353358Sdim assert(!sec->chunks.empty()); 1552353358Sdim const Chunk *firstChunk = *sec->chunks.begin(); 1553353358Sdim const Chunk *lastChunk = *sec->chunks.rbegin(); 1554353358Sdim 1555353358Sdim // Emit COFF group 1556353358Sdim CoffGroupSym cgs(SymbolRecordKind::CoffGroupSym); 1557353358Sdim cgs.Name = sec->name; 1558353358Sdim cgs.Segment = os.sectionIndex; 1559353358Sdim cgs.Offset = firstChunk->getRVA() - os.getRVA(); 1560353358Sdim cgs.Size = lastChunk->getRVA() + lastChunk->getSize() - firstChunk->getRVA(); 1561353358Sdim cgs.Characteristics = sec->characteristics; 1562353358Sdim 1563353358Sdim // Somehow .idata sections & sections groups in the debug symbol stream have 1564353358Sdim // the "write" flag set. However the section header for the corresponding 1565353358Sdim // .idata section doesn't have it. 1566353358Sdim if (cgs.Name.startswith(".idata")) 1567353358Sdim cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE; 1568353358Sdim 1569353358Sdim mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1570353358Sdim cgs, allocator, CodeViewContainer::Pdb)); 1571327952Sdim} 1572327952Sdim 1573353358Sdimstatic void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &mod, 1574353358Sdim OutputSection &os, 1575353358Sdim BumpPtrAllocator &allocator) { 1576353358Sdim SectionSym sym(SymbolRecordKind::SectionSym); 1577353358Sdim sym.Alignment = 12; // 2^12 = 4KB 1578353358Sdim sym.Characteristics = os.header.Characteristics; 1579353358Sdim sym.Length = os.getVirtualSize(); 1580353358Sdim sym.Name = os.name; 1581353358Sdim sym.Rva = os.getRVA(); 1582353358Sdim sym.SectionNumber = os.sectionIndex; 1583353358Sdim mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1584353358Sdim sym, allocator, CodeViewContainer::Pdb)); 1585353358Sdim 1586353358Sdim // Skip COFF groups in MinGW because it adds a significant footprint to the 1587353358Sdim // PDB, due to each function being in its own section 1588353358Sdim if (config->mingw) 1589353358Sdim return; 1590353358Sdim 1591353358Sdim // Output COFF groups for individual chunks of this section. 1592353358Sdim for (PartialSection *sec : os.contribSections) { 1593353358Sdim addLinkerModuleCoffGroup(sec, mod, os, allocator); 1594353358Sdim } 1595353358Sdim} 1596353358Sdim 1597353358Sdim// Add all import files as modules to the PDB. 1598353358Sdimvoid PDBLinker::addImportFilesToPDB(ArrayRef<OutputSection *> outputSections) { 1599353358Sdim if (ImportFile::instances.empty()) 1600353358Sdim return; 1601353358Sdim 1602353358Sdim std::map<std::string, llvm::pdb::DbiModuleDescriptorBuilder *> dllToModuleDbi; 1603353358Sdim 1604353358Sdim for (ImportFile *file : ImportFile::instances) { 1605353358Sdim if (!file->live) 1606353358Sdim continue; 1607353358Sdim 1608353358Sdim if (!file->thunkSym) 1609353358Sdim continue; 1610353358Sdim 1611353358Sdim if (!file->thunkLive) 1612353358Sdim continue; 1613353358Sdim 1614353358Sdim std::string dll = StringRef(file->dllName).lower(); 1615353358Sdim llvm::pdb::DbiModuleDescriptorBuilder *&mod = dllToModuleDbi[dll]; 1616353358Sdim if (!mod) { 1617353358Sdim pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1618353358Sdim SmallString<128> libPath = file->parentName; 1619353358Sdim pdbMakeAbsolute(libPath); 1620353358Sdim sys::path::native(libPath); 1621353358Sdim 1622353358Sdim // Name modules similar to MSVC's link.exe. 1623353358Sdim // The first module is the simple dll filename 1624353358Sdim llvm::pdb::DbiModuleDescriptorBuilder &firstMod = 1625353358Sdim exitOnErr(dbiBuilder.addModuleInfo(file->dllName)); 1626353358Sdim firstMod.setObjFileName(libPath); 1627353358Sdim pdb::SectionContrib sc = 1628353358Sdim createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex); 1629353358Sdim firstMod.setFirstSectionContrib(sc); 1630353358Sdim 1631353358Sdim // The second module is where the import stream goes. 1632353358Sdim mod = &exitOnErr(dbiBuilder.addModuleInfo("Import:" + file->dllName)); 1633353358Sdim mod->setObjFileName(libPath); 1634353358Sdim } 1635353358Sdim 1636353358Sdim DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym); 1637353358Sdim Chunk *thunkChunk = thunk->getChunk(); 1638353358Sdim OutputSection *thunkOS = thunkChunk->getOutputSection(); 1639353358Sdim 1640353358Sdim ObjNameSym ons(SymbolRecordKind::ObjNameSym); 1641353358Sdim Compile3Sym cs(SymbolRecordKind::Compile3Sym); 1642353358Sdim Thunk32Sym ts(SymbolRecordKind::Thunk32Sym); 1643353358Sdim ScopeEndSym es(SymbolRecordKind::ScopeEndSym); 1644353358Sdim 1645353358Sdim ons.Name = file->dllName; 1646353358Sdim ons.Signature = 0; 1647353358Sdim 1648353358Sdim fillLinkerVerRecord(cs); 1649353358Sdim 1650353358Sdim ts.Name = thunk->getName(); 1651353358Sdim ts.Parent = 0; 1652353358Sdim ts.End = 0; 1653353358Sdim ts.Next = 0; 1654353358Sdim ts.Thunk = ThunkOrdinal::Standard; 1655353358Sdim ts.Length = thunkChunk->getSize(); 1656353358Sdim ts.Segment = thunkOS->sectionIndex; 1657353358Sdim ts.Offset = thunkChunk->getRVA() - thunkOS->getRVA(); 1658353358Sdim 1659353358Sdim mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1660353358Sdim ons, alloc, CodeViewContainer::Pdb)); 1661353358Sdim mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1662353358Sdim cs, alloc, CodeViewContainer::Pdb)); 1663353358Sdim 1664353358Sdim SmallVector<SymbolScope, 4> scopes; 1665353358Sdim CVSymbol newSym = codeview::SymbolSerializer::writeOneSymbol( 1666353358Sdim ts, alloc, CodeViewContainer::Pdb); 1667353358Sdim scopeStackOpen(scopes, mod->getNextSymbolOffset(), newSym); 1668353358Sdim 1669353358Sdim mod->addSymbol(newSym); 1670353358Sdim 1671353358Sdim newSym = codeview::SymbolSerializer::writeOneSymbol(es, alloc, 1672353358Sdim CodeViewContainer::Pdb); 1673353358Sdim scopeStackClose(scopes, mod->getNextSymbolOffset(), file); 1674353358Sdim 1675353358Sdim mod->addSymbol(newSym); 1676353358Sdim 1677353358Sdim pdb::SectionContrib sc = 1678353358Sdim createSectionContrib(thunk->getChunk(), mod->getModuleIndex()); 1679353358Sdim mod->setFirstSectionContrib(sc); 1680353358Sdim } 1681353358Sdim} 1682353358Sdim 1683314564Sdim// Creates a PDB file. 1684360784Sdimvoid createPDB(SymbolTable *symtab, 1685353358Sdim ArrayRef<OutputSection *> outputSections, 1686353358Sdim ArrayRef<uint8_t> sectionTable, 1687353358Sdim llvm::codeview::DebugInfo *buildId) { 1688353358Sdim ScopedTimer t1(totalPdbLinkTimer); 1689353358Sdim PDBLinker pdb(symtab); 1690341825Sdim 1691353358Sdim pdb.initialize(buildId); 1692353358Sdim pdb.addObjectsToPDB(); 1693353358Sdim pdb.addImportFilesToPDB(outputSections); 1694353358Sdim pdb.addSections(outputSections, sectionTable); 1695353358Sdim pdb.addNatvisFiles(); 1696341825Sdim 1697353358Sdim ScopedTimer t2(diskCommitTimer); 1698353358Sdim codeview::GUID guid; 1699353358Sdim pdb.commit(&guid); 1700353358Sdim memcpy(&buildId->PDB70.Signature, &guid, 16); 1701353358Sdim 1702353358Sdim t2.stop(); 1703353358Sdim t1.stop(); 1704353358Sdim pdb.printStats(); 1705321369Sdim} 1706314564Sdim 1707353358Sdimvoid PDBLinker::initialize(llvm::codeview::DebugInfo *buildId) { 1708353358Sdim exitOnErr(builder.initialize(4096)); // 4096 is blocksize 1709314564Sdim 1710353358Sdim buildId->Signature.CVSignature = OMF::Signature::PDB70; 1711344779Sdim // Signature is set to a hash of the PDB contents when the PDB is done. 1712353358Sdim memset(buildId->PDB70.Signature, 0, 16); 1713353358Sdim buildId->PDB70.Age = 1; 1714344779Sdim 1715314564Sdim // Create streams in MSF for predefined streams, namely 1716314564Sdim // PDB, TPI, DBI and IPI. 1717353358Sdim for (int i = 0; i < (int)pdb::kSpecialStreamCount; ++i) 1718353358Sdim exitOnErr(builder.getMsfBuilder().addStream(0)); 1719314564Sdim 1720314564Sdim // Add an Info stream. 1721353358Sdim auto &infoBuilder = builder.getInfoBuilder(); 1722353358Sdim infoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); 1723353358Sdim infoBuilder.setHashPDBContentsToGUID(true); 1724314564Sdim 1725321369Sdim // Add an empty DBI stream. 1726353358Sdim pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1727353358Sdim dbiBuilder.setAge(buildId->PDB70.Age); 1728353358Sdim dbiBuilder.setVersionHeader(pdb::PdbDbiV70); 1729353358Sdim dbiBuilder.setMachineType(config->machine); 1730341825Sdim // Technically we are not link.exe 14.11, but there are known cases where 1731341825Sdim // debugging tools on Windows expect Microsoft-specific version numbers or 1732341825Sdim // they fail to work at all. Since we know we produce PDBs that are 1733341825Sdim // compatible with LINK 14.11, we set that version number here. 1734353358Sdim dbiBuilder.setBuildNumber(14, 11); 1735321369Sdim} 1736314564Sdim 1737353358Sdimvoid PDBLinker::addSections(ArrayRef<OutputSection *> outputSections, 1738353358Sdim ArrayRef<uint8_t> sectionTable) { 1739327952Sdim // It's not entirely clear what this is, but the * Linker * module uses it. 1740353358Sdim pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1741353358Sdim nativePath = config->pdbPath; 1742353358Sdim pdbMakeAbsolute(nativePath); 1743353358Sdim uint32_t pdbFilePathNI = dbiBuilder.addECName(nativePath); 1744353358Sdim auto &linkerModule = exitOnErr(dbiBuilder.addModuleInfo("* Linker *")); 1745353358Sdim linkerModule.setPdbFilePathNI(pdbFilePathNI); 1746353358Sdim addCommonLinkerModuleSymbols(nativePath, linkerModule, alloc); 1747314564Sdim 1748327952Sdim // Add section contributions. They must be ordered by ascending RVA. 1749353358Sdim for (OutputSection *os : outputSections) { 1750353358Sdim addLinkerModuleSectionSymbol(linkerModule, *os, alloc); 1751353358Sdim for (Chunk *c : os->chunks) { 1752353358Sdim pdb::SectionContrib sc = 1753353358Sdim createSectionContrib(c, linkerModule.getModuleIndex()); 1754353358Sdim builder.getDbiBuilder().addSectionContrib(sc); 1755341825Sdim } 1756327952Sdim } 1757327952Sdim 1758353358Sdim // The * Linker * first section contrib is only used along with /INCREMENTAL, 1759353358Sdim // to provide trampolines thunks for incremental function patching. Set this 1760353358Sdim // as "unused" because LLD doesn't support /INCREMENTAL link. 1761353358Sdim pdb::SectionContrib sc = 1762353358Sdim createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex); 1763353358Sdim linkerModule.setFirstSectionContrib(sc); 1764353358Sdim 1765314564Sdim // Add Section Map stream. 1766353358Sdim ArrayRef<object::coff_section> sections = { 1767353358Sdim (const object::coff_section *)sectionTable.data(), 1768353358Sdim sectionTable.size() / sizeof(object::coff_section)}; 1769353358Sdim sectionMap = pdb::DbiStreamBuilder::createSectionMap(sections); 1770353358Sdim dbiBuilder.setSectionMap(sectionMap); 1771314564Sdim 1772314564Sdim // Add COFF section header stream. 1773353358Sdim exitOnErr( 1774353358Sdim dbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, sectionTable)); 1775321369Sdim} 1776314564Sdim 1777353358Sdimvoid PDBLinker::commit(codeview::GUID *guid) { 1778360784Sdim ExitOnError exitOnErr((config->pdbPath + ": ").str()); 1779314564Sdim // Write to a file. 1780353358Sdim exitOnErr(builder.commit(config->pdbPath, guid)); 1781314564Sdim} 1782341825Sdim 1783341825Sdimstatic uint32_t getSecrelReloc() { 1784353358Sdim switch (config->machine) { 1785341825Sdim case AMD64: 1786341825Sdim return COFF::IMAGE_REL_AMD64_SECREL; 1787341825Sdim case I386: 1788341825Sdim return COFF::IMAGE_REL_I386_SECREL; 1789341825Sdim case ARMNT: 1790341825Sdim return COFF::IMAGE_REL_ARM_SECREL; 1791341825Sdim case ARM64: 1792341825Sdim return COFF::IMAGE_REL_ARM64_SECREL; 1793341825Sdim default: 1794341825Sdim llvm_unreachable("unknown machine type"); 1795341825Sdim } 1796341825Sdim} 1797341825Sdim 1798341825Sdim// Try to find a line table for the given offset Addr into the given chunk C. 1799341825Sdim// If a line table was found, the line table, the string and checksum tables 1800341825Sdim// that are used to interpret the line table, and the offset of Addr in the line 1801341825Sdim// table are stored in the output arguments. Returns whether a line table was 1802341825Sdim// found. 1803353358Sdimstatic bool findLineTable(const SectionChunk *c, uint32_t addr, 1804353358Sdim DebugStringTableSubsectionRef &cVStrTab, 1805353358Sdim DebugChecksumsSubsectionRef &checksums, 1806353358Sdim DebugLinesSubsectionRef &lines, 1807353358Sdim uint32_t &offsetInLinetable) { 1808353358Sdim ExitOnError exitOnErr; 1809353358Sdim uint32_t secrelReloc = getSecrelReloc(); 1810341825Sdim 1811353358Sdim for (SectionChunk *dbgC : c->file->getDebugChunks()) { 1812353358Sdim if (dbgC->getSectionName() != ".debug$S") 1813341825Sdim continue; 1814341825Sdim 1815353358Sdim // Build a mapping of SECREL relocations in dbgC that refer to `c`. 1816353358Sdim DenseMap<uint32_t, uint32_t> secrels; 1817353358Sdim for (const coff_relocation &r : dbgC->getRelocs()) { 1818353358Sdim if (r.Type != secrelReloc) 1819341825Sdim continue; 1820341825Sdim 1821353358Sdim if (auto *s = dyn_cast_or_null<DefinedRegular>( 1822353358Sdim c->file->getSymbols()[r.SymbolTableIndex])) 1823353358Sdim if (s->getChunk() == c) 1824353358Sdim secrels[r.VirtualAddress] = s->getValue(); 1825341825Sdim } 1826341825Sdim 1827353358Sdim ArrayRef<uint8_t> contents = 1828353358Sdim SectionChunk::consumeDebugMagic(dbgC->getContents(), ".debug$S"); 1829353358Sdim DebugSubsectionArray subsections; 1830353358Sdim BinaryStreamReader reader(contents, support::little); 1831353358Sdim exitOnErr(reader.readArray(subsections, contents.size())); 1832341825Sdim 1833353358Sdim for (const DebugSubsectionRecord &ss : subsections) { 1834353358Sdim switch (ss.kind()) { 1835341825Sdim case DebugSubsectionKind::StringTable: { 1836353358Sdim assert(!cVStrTab.valid() && 1837341825Sdim "Encountered multiple string table subsections!"); 1838353358Sdim exitOnErr(cVStrTab.initialize(ss.getRecordData())); 1839341825Sdim break; 1840341825Sdim } 1841341825Sdim case DebugSubsectionKind::FileChecksums: 1842353358Sdim assert(!checksums.valid() && 1843341825Sdim "Encountered multiple checksum subsections!"); 1844353358Sdim exitOnErr(checksums.initialize(ss.getRecordData())); 1845341825Sdim break; 1846341825Sdim case DebugSubsectionKind::Lines: { 1847353358Sdim ArrayRef<uint8_t> bytes; 1848353358Sdim auto ref = ss.getRecordData(); 1849353358Sdim exitOnErr(ref.readLongestContiguousChunk(0, bytes)); 1850353358Sdim size_t offsetInDbgC = bytes.data() - dbgC->getContents().data(); 1851341825Sdim 1852341825Sdim // Check whether this line table refers to C. 1853353358Sdim auto i = secrels.find(offsetInDbgC); 1854353358Sdim if (i == secrels.end()) 1855341825Sdim break; 1856341825Sdim 1857341825Sdim // Check whether this line table covers Addr in C. 1858353358Sdim DebugLinesSubsectionRef linesTmp; 1859353358Sdim exitOnErr(linesTmp.initialize(BinaryStreamReader(ref))); 1860353358Sdim uint32_t offsetInC = i->second + linesTmp.header()->RelocOffset; 1861353358Sdim if (addr < offsetInC || addr >= offsetInC + linesTmp.header()->CodeSize) 1862341825Sdim break; 1863341825Sdim 1864353358Sdim assert(!lines.header() && 1865341825Sdim "Encountered multiple line tables for function!"); 1866353358Sdim exitOnErr(lines.initialize(BinaryStreamReader(ref))); 1867353358Sdim offsetInLinetable = addr - offsetInC; 1868341825Sdim break; 1869341825Sdim } 1870341825Sdim default: 1871341825Sdim break; 1872341825Sdim } 1873341825Sdim 1874353358Sdim if (cVStrTab.valid() && checksums.valid() && lines.header()) 1875341825Sdim return true; 1876341825Sdim } 1877341825Sdim } 1878341825Sdim 1879341825Sdim return false; 1880341825Sdim} 1881341825Sdim 1882341825Sdim// Use CodeView line tables to resolve a file and line number for the given 1883360784Sdim// offset into the given chunk and return them, or None if a line table was 1884341825Sdim// not found. 1885360784SdimOptional<std::pair<StringRef, uint32_t>> 1886360784SdimgetFileLineCodeView(const SectionChunk *c, uint32_t addr) { 1887353358Sdim ExitOnError exitOnErr; 1888341825Sdim 1889353358Sdim DebugStringTableSubsectionRef cVStrTab; 1890353358Sdim DebugChecksumsSubsectionRef checksums; 1891353358Sdim DebugLinesSubsectionRef lines; 1892353358Sdim uint32_t offsetInLinetable; 1893341825Sdim 1894353358Sdim if (!findLineTable(c, addr, cVStrTab, checksums, lines, offsetInLinetable)) 1895360784Sdim return None; 1896341825Sdim 1897353358Sdim Optional<uint32_t> nameIndex; 1898353358Sdim Optional<uint32_t> lineNumber; 1899353358Sdim for (LineColumnEntry &entry : lines) { 1900353358Sdim for (const LineNumberEntry &ln : entry.LineNumbers) { 1901353358Sdim LineInfo li(ln.Flags); 1902353358Sdim if (ln.Offset > offsetInLinetable) { 1903353358Sdim if (!nameIndex) { 1904353358Sdim nameIndex = entry.NameIndex; 1905353358Sdim lineNumber = li.getStartLine(); 1906344779Sdim } 1907353358Sdim StringRef filename = 1908353358Sdim exitOnErr(getFileName(cVStrTab, checksums, *nameIndex)); 1909360784Sdim return std::make_pair(filename, *lineNumber); 1910341825Sdim } 1911353358Sdim nameIndex = entry.NameIndex; 1912353358Sdim lineNumber = li.getStartLine(); 1913341825Sdim } 1914341825Sdim } 1915353358Sdim if (!nameIndex) 1916360784Sdim return None; 1917353358Sdim StringRef filename = exitOnErr(getFileName(cVStrTab, checksums, *nameIndex)); 1918360784Sdim return std::make_pair(filename, *lineNumber); 1919341825Sdim} 1920360784Sdim 1921360784Sdim} // namespace coff 1922360784Sdim} // namespace lld 1923