PDB.cpp revision 344779
1292934Sdim//===- PDB.cpp ------------------------------------------------------------===// 2292934Sdim// 3292934Sdim// The LLVM Linker 4292934Sdim// 5292934Sdim// This file is distributed under the University of Illinois Open Source 6292934Sdim// License. See LICENSE.TXT for details. 7292934Sdim// 8292934Sdim//===----------------------------------------------------------------------===// 9292934Sdim 10314564Sdim#include "PDB.h" 11314564Sdim#include "Chunks.h" 12314564Sdim#include "Config.h" 13327952Sdim#include "Driver.h" 14314564Sdim#include "SymbolTable.h" 15292934Sdim#include "Symbols.h" 16327952Sdim#include "Writer.h" 17327952Sdim#include "lld/Common/ErrorHandler.h" 18341825Sdim#include "lld/Common/Timer.h" 19344779Sdim#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" 20321369Sdim#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" 21327952Sdim#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h" 22321369Sdim#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 23327952Sdim#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" 24327952Sdim#include "llvm/DebugInfo/CodeView/RecordName.h" 25327952Sdim#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 26344779Sdim#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h" 27321369Sdim#include "llvm/DebugInfo/CodeView/SymbolSerializer.h" 28321369Sdim#include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 29314564Sdim#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 30321369Sdim#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" 31314564Sdim#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" 32314564Sdim#include "llvm/DebugInfo/MSF/MSFBuilder.h" 33314564Sdim#include "llvm/DebugInfo/MSF/MSFCommon.h" 34321369Sdim#include "llvm/DebugInfo/PDB/GenericError.h" 35321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" 36321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiStream.h" 37321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" 38327952Sdim#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" 39321369Sdim#include "llvm/DebugInfo/PDB/Native/InfoStream.h" 40321369Sdim#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" 41321369Sdim#include "llvm/DebugInfo/PDB/Native/NativeSession.h" 42321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBFile.h" 43321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" 44321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 45327952Sdim#include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 46321369Sdim#include "llvm/DebugInfo/PDB/Native/TpiStream.h" 47321369Sdim#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" 48321369Sdim#include "llvm/DebugInfo/PDB/PDB.h" 49314564Sdim#include "llvm/Object/COFF.h" 50341825Sdim#include "llvm/Object/CVDebugRecord.h" 51321369Sdim#include "llvm/Support/BinaryByteStream.h" 52292934Sdim#include "llvm/Support/Endian.h" 53344779Sdim#include "llvm/Support/Errc.h" 54341825Sdim#include "llvm/Support/FormatVariadic.h" 55327952Sdim#include "llvm/Support/JamCRC.h" 56344779Sdim#include "llvm/Support/Parallel.h" 57321369Sdim#include "llvm/Support/Path.h" 58314564Sdim#include "llvm/Support/ScopedPrinter.h" 59292934Sdim#include <memory> 60292934Sdim 61314564Sdimusing namespace lld; 62314564Sdimusing namespace lld::coff; 63292934Sdimusing namespace llvm; 64314564Sdimusing namespace llvm::codeview; 65292934Sdim 66314564Sdimusing llvm::object::coff_section; 67292934Sdim 68314564Sdimstatic ExitOnError ExitOnErr; 69314564Sdim 70341825Sdimstatic Timer TotalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root()); 71341825Sdim 72341825Sdimstatic Timer AddObjectsTimer("Add Objects", TotalPdbLinkTimer); 73341825Sdimstatic Timer TypeMergingTimer("Type Merging", AddObjectsTimer); 74341825Sdimstatic Timer SymbolMergingTimer("Symbol Merging", AddObjectsTimer); 75341825Sdimstatic Timer GlobalsLayoutTimer("Globals Stream Layout", TotalPdbLinkTimer); 76341825Sdimstatic Timer TpiStreamLayoutTimer("TPI Stream Layout", TotalPdbLinkTimer); 77341825Sdimstatic Timer DiskCommitTimer("Commit to Disk", TotalPdbLinkTimer); 78341825Sdim 79321369Sdimnamespace { 80321369Sdim/// Map from type index and item index in a type server PDB to the 81321369Sdim/// corresponding index in the destination PDB. 82321369Sdimstruct CVIndexMap { 83321369Sdim SmallVector<TypeIndex, 0> TPIMap; 84321369Sdim SmallVector<TypeIndex, 0> IPIMap; 85321369Sdim bool IsTypeServerMap = false; 86344779Sdim bool IsPrecompiledTypeMap = false; 87321369Sdim}; 88321369Sdim 89344779Sdimclass DebugSHandler; 90344779Sdim 91321369Sdimclass PDBLinker { 92344779Sdim friend DebugSHandler; 93344779Sdim 94321369Sdimpublic: 95321369Sdim PDBLinker(SymbolTable *Symtab) 96321369Sdim : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc), 97341825Sdim IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) { 98341825Sdim // This isn't strictly necessary, but link.exe usually puts an empty string 99341825Sdim // as the first "valid" string in the string table, so we do the same in 100341825Sdim // order to maintain as much byte-for-byte compatibility as possible. 101341825Sdim PDBStrTab.insert(""); 102341825Sdim } 103321369Sdim 104321369Sdim /// Emit the basic PDB structure: initial streams, headers, etc. 105344779Sdim void initialize(llvm::codeview::DebugInfo *BuildId); 106321369Sdim 107341825Sdim /// Add natvis files specified on the command line. 108341825Sdim void addNatvisFiles(); 109341825Sdim 110321369Sdim /// Link CodeView from each object file in the symbol table into the PDB. 111321369Sdim void addObjectsToPDB(); 112321369Sdim 113344779Sdim /// Link CodeView from a single object file into the target (output) PDB. 114344779Sdim /// When a precompiled headers object is linked, its TPI map might be provided 115344779Sdim /// externally. 116344779Sdim void addObjFile(ObjFile *File, CVIndexMap *ExternIndexMap = nullptr); 117321369Sdim 118321369Sdim /// Produce a mapping from the type and item indices used in the object 119321369Sdim /// file to those in the destination PDB. 120321369Sdim /// 121321369Sdim /// If the object file uses a type server PDB (compiled with /Zi), merge TPI 122321369Sdim /// and IPI from the type server PDB and return a map for it. Each unique type 123321369Sdim /// server PDB is merged at most once, so this may return an existing index 124321369Sdim /// mapping. 125321369Sdim /// 126321369Sdim /// If the object does not use a type server PDB (compiled with /Z7), we merge 127321369Sdim /// all the type and item records from the .debug$S stream and fill in the 128321369Sdim /// caller-provided ObjectIndexMap. 129344779Sdim Expected<const CVIndexMap &> mergeDebugT(ObjFile *File, 130344779Sdim CVIndexMap *ObjectIndexMap); 131321369Sdim 132344779Sdim /// Reads and makes available a PDB. 133344779Sdim Expected<const CVIndexMap &> maybeMergeTypeServerPDB(ObjFile *File, 134344779Sdim const CVType &FirstType); 135321369Sdim 136344779Sdim /// Merges a precompiled headers TPI map into the current TPI map. The 137344779Sdim /// precompiled headers object will also be loaded and remapped in the 138344779Sdim /// process. 139344779Sdim Expected<const CVIndexMap &> 140344779Sdim mergeInPrecompHeaderObj(ObjFile *File, const CVType &FirstType, 141344779Sdim CVIndexMap *ObjectIndexMap); 142344779Sdim 143344779Sdim /// Reads and makes available a precompiled headers object. 144344779Sdim /// 145344779Sdim /// This is a requirement for objects compiled with cl.exe /Yu. In that 146344779Sdim /// case, the referenced object (which was compiled with /Yc) has to be loaded 147344779Sdim /// first. This is mainly because the current object's TPI stream has external 148344779Sdim /// references to the precompiled headers object. 149344779Sdim /// 150344779Sdim /// If the precompiled headers object was already loaded, this function will 151344779Sdim /// simply return its (remapped) TPI map. 152344779Sdim Expected<const CVIndexMap &> aquirePrecompObj(ObjFile *File, 153344779Sdim PrecompRecord Precomp); 154344779Sdim 155344779Sdim /// Adds a precompiled headers object signature -> TPI mapping. 156344779Sdim std::pair<CVIndexMap &, bool /*already there*/> 157344779Sdim registerPrecompiledHeaders(uint32_t Signature); 158344779Sdim 159344779Sdim void mergeSymbolRecords(ObjFile *File, const CVIndexMap &IndexMap, 160344779Sdim std::vector<ulittle32_t *> &StringTableRefs, 161344779Sdim BinaryStreamRef SymData); 162344779Sdim 163321369Sdim /// Add the section map and section contributions to the PDB. 164327952Sdim void addSections(ArrayRef<OutputSection *> OutputSections, 165327952Sdim ArrayRef<uint8_t> SectionTable); 166321369Sdim 167344779Sdim /// Get the type table or the global type table if /DEBUG:GHASH is enabled. 168344779Sdim TypeCollection &getTypeTable() { 169344779Sdim if (Config->DebugGHashes) 170344779Sdim return GlobalTypeTable; 171344779Sdim return TypeTable; 172344779Sdim } 173321369Sdim 174344779Sdim /// Get the ID table or the global ID table if /DEBUG:GHASH is enabled. 175344779Sdim TypeCollection &getIDTable() { 176344779Sdim if (Config->DebugGHashes) 177344779Sdim return GlobalIDTable; 178344779Sdim return IDTable; 179344779Sdim } 180344779Sdim 181344779Sdim /// Write the PDB to disk and store the Guid generated for it in *Guid. 182344779Sdim void commit(codeview::GUID *Guid); 183344779Sdim 184321369Sdimprivate: 185321369Sdim BumpPtrAllocator Alloc; 186321369Sdim 187321369Sdim SymbolTable *Symtab; 188321369Sdim 189321369Sdim pdb::PDBFileBuilder Builder; 190321369Sdim 191321369Sdim /// Type records that will go into the PDB TPI stream. 192327952Sdim MergingTypeTableBuilder TypeTable; 193321369Sdim 194321369Sdim /// Item records that will go into the PDB IPI stream. 195327952Sdim MergingTypeTableBuilder IDTable; 196321369Sdim 197327952Sdim /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH) 198327952Sdim GlobalTypeTableBuilder GlobalTypeTable; 199327952Sdim 200327952Sdim /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH) 201327952Sdim GlobalTypeTableBuilder GlobalIDTable; 202327952Sdim 203321369Sdim /// PDBs use a single global string table for filenames in the file checksum 204321369Sdim /// table. 205321369Sdim DebugStringTableSubsection PDBStrTab; 206321369Sdim 207321369Sdim llvm::SmallString<128> NativePath; 208321369Sdim 209341825Sdim /// A list of other PDBs which are loaded during the linking process and which 210341825Sdim /// we need to keep around since the linking operation may reference pointers 211341825Sdim /// inside of these PDBs. 212341825Sdim llvm::SmallVector<std::unique_ptr<pdb::NativeSession>, 2> LoadedPDBs; 213341825Sdim 214321369Sdim std::vector<pdb::SecMapEntry> SectionMap; 215321369Sdim 216321369Sdim /// Type index mappings of type server PDBs that we've loaded so far. 217344779Sdim std::map<codeview::GUID, CVIndexMap> TypeServerIndexMappings; 218329410Sdim 219344779Sdim /// Type index mappings of precompiled objects type map that we've loaded so 220344779Sdim /// far. 221344779Sdim std::map<uint32_t, CVIndexMap> PrecompTypeIndexMappings; 222344779Sdim 223329410Sdim /// List of TypeServer PDBs which cannot be loaded. 224329410Sdim /// Cached to prevent repeated load attempts. 225344779Sdim std::map<codeview::GUID, std::string> MissingTypeServerPDBs; 226321369Sdim}; 227344779Sdim 228344779Sdimclass DebugSHandler { 229344779Sdim PDBLinker &Linker; 230344779Sdim 231344779Sdim /// The object file whose .debug$S sections we're processing. 232344779Sdim ObjFile &File; 233344779Sdim 234344779Sdim /// The result of merging type indices. 235344779Sdim const CVIndexMap &IndexMap; 236344779Sdim 237344779Sdim /// The DEBUG_S_STRINGTABLE subsection. These strings are referred to by 238344779Sdim /// index from other records in the .debug$S section. All of these strings 239344779Sdim /// need to be added to the global PDB string table, and all references to 240344779Sdim /// these strings need to have their indices re-written to refer to the 241344779Sdim /// global PDB string table. 242344779Sdim DebugStringTableSubsectionRef CVStrTab; 243344779Sdim 244344779Sdim /// The DEBUG_S_FILECHKSMS subsection. As above, these are referred to 245344779Sdim /// by other records in the .debug$S section and need to be merged into the 246344779Sdim /// PDB. 247344779Sdim DebugChecksumsSubsectionRef Checksums; 248344779Sdim 249344779Sdim /// The DEBUG_S_FRAMEDATA subsection(s). There can be more than one of 250344779Sdim /// these and they need not appear in any specific order. However, they 251344779Sdim /// contain string table references which need to be re-written, so we 252344779Sdim /// collect them all here and re-write them after all subsections have been 253344779Sdim /// discovered and processed. 254344779Sdim std::vector<DebugFrameDataSubsectionRef> NewFpoFrames; 255344779Sdim 256344779Sdim /// Pointers to raw memory that we determine have string table references 257344779Sdim /// that need to be re-written. We first process all .debug$S subsections 258344779Sdim /// to ensure that we can handle subsections written in any order, building 259344779Sdim /// up this list as we go. At the end, we use the string table (which must 260344779Sdim /// have been discovered by now else it is an error) to re-write these 261344779Sdim /// references. 262344779Sdim std::vector<ulittle32_t *> StringTableReferences; 263344779Sdim 264344779Sdimpublic: 265344779Sdim DebugSHandler(PDBLinker &Linker, ObjFile &File, const CVIndexMap &IndexMap) 266344779Sdim : Linker(Linker), File(File), IndexMap(IndexMap) {} 267344779Sdim 268344779Sdim void handleDebugS(lld::coff::SectionChunk &DebugS); 269344779Sdim void finish(); 270344779Sdim}; 271321369Sdim} 272321369Sdim 273344779Sdim// Visual Studio's debugger requires absolute paths in various places in the 274344779Sdim// PDB to work without additional configuration: 275344779Sdim// https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box 276344779Sdimstatic void pdbMakeAbsolute(SmallVectorImpl<char> &FileName) { 277344779Sdim // The default behavior is to produce paths that are valid within the context 278344779Sdim // of the machine that you perform the link on. If the linker is running on 279344779Sdim // a POSIX system, we will output absolute POSIX paths. If the linker is 280344779Sdim // running on a Windows system, we will output absolute Windows paths. If the 281344779Sdim // user desires any other kind of behavior, they should explicitly pass 282344779Sdim // /pdbsourcepath, in which case we will treat the exact string the user 283344779Sdim // passed in as the gospel and not normalize, canonicalize it. 284344779Sdim if (sys::path::is_absolute(FileName, sys::path::Style::windows) || 285344779Sdim sys::path::is_absolute(FileName, sys::path::Style::posix)) 286344779Sdim return; 287344779Sdim 288344779Sdim // It's not absolute in any path syntax. Relative paths necessarily refer to 289344779Sdim // the local file system, so we can make it native without ending up with a 290344779Sdim // nonsensical path. 291344779Sdim if (Config->PDBSourcePath.empty()) { 292344779Sdim sys::path::native(FileName); 293344779Sdim sys::fs::make_absolute(FileName); 294344779Sdim return; 295344779Sdim } 296344779Sdim 297344779Sdim // Try to guess whether /PDBSOURCEPATH is a unix path or a windows path. 298344779Sdim // Since PDB's are more of a Windows thing, we make this conservative and only 299344779Sdim // decide that it's a unix path if we're fairly certain. Specifically, if 300344779Sdim // it starts with a forward slash. 301344779Sdim SmallString<128> AbsoluteFileName = Config->PDBSourcePath; 302344779Sdim sys::path::Style GuessedStyle = AbsoluteFileName.startswith("/") 303344779Sdim ? sys::path::Style::posix 304344779Sdim : sys::path::Style::windows; 305344779Sdim sys::path::append(AbsoluteFileName, GuessedStyle, FileName); 306344779Sdim sys::path::native(AbsoluteFileName, GuessedStyle); 307344779Sdim sys::path::remove_dots(AbsoluteFileName, true, GuessedStyle); 308344779Sdim 309344779Sdim FileName = std::move(AbsoluteFileName); 310344779Sdim} 311344779Sdim 312327952Sdimstatic SectionChunk *findByName(ArrayRef<SectionChunk *> Sections, 313314564Sdim StringRef Name) { 314314564Sdim for (SectionChunk *C : Sections) 315314564Sdim if (C->getSectionName() == Name) 316314564Sdim return C; 317314564Sdim return nullptr; 318314564Sdim} 319292934Sdim 320321369Sdimstatic ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data, 321321369Sdim StringRef SecName) { 322314564Sdim // First 4 bytes are section magic. 323314564Sdim if (Data.size() < 4) 324314564Sdim fatal(SecName + " too short"); 325321369Sdim if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) 326314564Sdim fatal(SecName + " has an invalid magic"); 327314564Sdim return Data.slice(4); 328292934Sdim} 329314564Sdim 330327952Sdimstatic ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) { 331321369Sdim if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName)) 332321369Sdim return consumeDebugMagic(Sec->getContents(), SecName); 333321369Sdim return {}; 334321369Sdim} 335314564Sdim 336327952Sdim// A COFF .debug$H section is currently a clang extension. This function checks 337327952Sdim// if a .debug$H section is in a format that we expect / understand, so that we 338327952Sdim// can ignore any sections which are coincidentally also named .debug$H but do 339327952Sdim// not contain a format we recognize. 340327952Sdimstatic bool canUseDebugH(ArrayRef<uint8_t> DebugH) { 341327952Sdim if (DebugH.size() < sizeof(object::debug_h_header)) 342327952Sdim return false; 343327952Sdim auto *Header = 344327952Sdim reinterpret_cast<const object::debug_h_header *>(DebugH.data()); 345327952Sdim DebugH = DebugH.drop_front(sizeof(object::debug_h_header)); 346327952Sdim return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC && 347327952Sdim Header->Version == 0 && 348341825Sdim Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) && 349341825Sdim (DebugH.size() % 8 == 0); 350327952Sdim} 351327952Sdim 352327952Sdimstatic Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) { 353327952Sdim SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H"); 354327952Sdim if (!Sec) 355327952Sdim return llvm::None; 356327952Sdim ArrayRef<uint8_t> Contents = Sec->getContents(); 357327952Sdim if (!canUseDebugH(Contents)) 358327952Sdim return None; 359327952Sdim return Contents; 360327952Sdim} 361327952Sdim 362327952Sdimstatic ArrayRef<GloballyHashedType> 363327952SdimgetHashesFromDebugH(ArrayRef<uint8_t> DebugH) { 364327952Sdim assert(canUseDebugH(DebugH)); 365327952Sdim 366327952Sdim DebugH = DebugH.drop_front(sizeof(object::debug_h_header)); 367327952Sdim uint32_t Count = DebugH.size() / sizeof(GloballyHashedType); 368327952Sdim return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count}; 369327952Sdim} 370327952Sdim 371321369Sdimstatic void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, 372327952Sdim TypeCollection &TypeTable) { 373321369Sdim // Start the TPI or IPI stream header. 374321369Sdim TpiBuilder.setVersionHeader(pdb::PdbTpiV80); 375314564Sdim 376327952Sdim // Flatten the in memory type table and hash each type. 377327952Sdim TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) { 378327952Sdim auto Hash = pdb::hashTypeRecord(Type); 379327952Sdim if (auto E = Hash.takeError()) 380327952Sdim fatal("type hashing error"); 381327952Sdim TpiBuilder.addTypeRecord(Type.RecordData, *Hash); 382314564Sdim }); 383314564Sdim} 384314564Sdim 385344779Sdim// OBJs usually start their symbol stream with a S_OBJNAME record. This record 386344779Sdim// also contains the signature/key of the current PCH session. The signature 387344779Sdim// must be same for all objects which depend on the precompiled object. 388344779Sdim// Recompiling the precompiled headers will generate a new PCH key and thus 389344779Sdim// invalidate all the dependent objects. 390344779Sdimstatic uint32_t extractPCHSignature(ObjFile *File) { 391344779Sdim auto DbgIt = find_if(File->getDebugChunks(), [](SectionChunk *C) { 392344779Sdim return C->getSectionName() == ".debug$S"; 393344779Sdim }); 394344779Sdim if (!DbgIt) 395344779Sdim return 0; 396344779Sdim 397344779Sdim ArrayRef<uint8_t> Contents = 398344779Sdim consumeDebugMagic((*DbgIt)->getContents(), ".debug$S"); 399344779Sdim DebugSubsectionArray Subsections; 400344779Sdim BinaryStreamReader Reader(Contents, support::little); 401344779Sdim ExitOnErr(Reader.readArray(Subsections, Contents.size())); 402344779Sdim 403344779Sdim for (const DebugSubsectionRecord &SS : Subsections) { 404344779Sdim if (SS.kind() != DebugSubsectionKind::Symbols) 405344779Sdim continue; 406344779Sdim 407344779Sdim // If it's there, the S_OBJNAME record shall come first in the stream. 408344779Sdim Expected<CVSymbol> Sym = readSymbolFromStream(SS.getRecordData(), 0); 409344779Sdim if (!Sym) { 410344779Sdim consumeError(Sym.takeError()); 411344779Sdim continue; 412344779Sdim } 413344779Sdim if (auto ObjName = SymbolDeserializer::deserializeAs<ObjNameSym>(Sym.get())) 414344779Sdim return ObjName->Signature; 415344779Sdim } 416344779Sdim return 0; 417321369Sdim} 418321369Sdim 419344779SdimExpected<const CVIndexMap &> 420344779SdimPDBLinker::mergeDebugT(ObjFile *File, CVIndexMap *ObjectIndexMap) { 421341825Sdim ScopedTimer T(TypeMergingTimer); 422341825Sdim 423344779Sdim bool IsPrecompiledHeader = false; 424344779Sdim 425314564Sdim ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); 426344779Sdim if (Data.empty()) { 427344779Sdim // Try again, Microsoft precompiled headers use .debug$P instead of 428344779Sdim // .debug$T 429344779Sdim Data = getDebugSection(File, ".debug$P"); 430344779Sdim IsPrecompiledHeader = true; 431344779Sdim } 432314564Sdim if (Data.empty()) 433344779Sdim return *ObjectIndexMap; // no debug info 434314564Sdim 435344779Sdim // Precompiled headers objects need to save the index map for further 436344779Sdim // reference by other objects which use the precompiled headers. 437344779Sdim if (IsPrecompiledHeader) { 438344779Sdim uint32_t PCHSignature = extractPCHSignature(File); 439344779Sdim if (PCHSignature == 0) 440344779Sdim fatal("No signature found for the precompiled headers OBJ (" + 441344779Sdim File->getName() + ")"); 442344779Sdim 443344779Sdim // When a precompiled headers object comes first on the command-line, we 444344779Sdim // update the mapping here. Otherwise, if an object referencing the 445344779Sdim // precompiled headers object comes first, the mapping is created in 446344779Sdim // aquirePrecompObj(), thus we would skip this block. 447344779Sdim if (!ObjectIndexMap->IsPrecompiledTypeMap) { 448344779Sdim auto R = registerPrecompiledHeaders(PCHSignature); 449344779Sdim if (R.second) 450344779Sdim fatal( 451344779Sdim "A precompiled headers OBJ with the same signature was already " 452344779Sdim "provided! (" + 453344779Sdim File->getName() + ")"); 454344779Sdim 455344779Sdim ObjectIndexMap = &R.first; 456344779Sdim } 457344779Sdim } 458344779Sdim 459321369Sdim BinaryByteStream Stream(Data, support::little); 460321369Sdim CVTypeArray Types; 461321369Sdim BinaryStreamReader Reader(Stream); 462321369Sdim if (auto EC = Reader.readArray(Types, Reader.getLength())) 463327952Sdim fatal("Reader::readArray failed: " + toString(std::move(EC))); 464321369Sdim 465344779Sdim auto FirstType = Types.begin(); 466344779Sdim if (FirstType == Types.end()) 467344779Sdim return *ObjectIndexMap; 468321369Sdim 469344779Sdim if (FirstType->kind() == LF_TYPESERVER2) { 470344779Sdim // Look through type servers. If we've already seen this type server, 471344779Sdim // don't merge any type information. 472344779Sdim return maybeMergeTypeServerPDB(File, *FirstType); 473344779Sdim } else if (FirstType->kind() == LF_PRECOMP) { 474344779Sdim // This object was compiled with /Yu, so process the corresponding 475344779Sdim // precompiled headers object (/Yc) first. Some type indices in the current 476344779Sdim // object are referencing data in the precompiled headers object, so we need 477344779Sdim // both to be loaded. 478344779Sdim auto E = mergeInPrecompHeaderObj(File, *FirstType, ObjectIndexMap); 479344779Sdim if (!E) 480344779Sdim return E.takeError(); 481344779Sdim 482344779Sdim // Drop LF_PRECOMP record from the input stream, as it needs to be replaced 483344779Sdim // with the precompiled headers object type stream. 484344779Sdim // Note that we can't just call Types.drop_front(), as we explicitly want to 485344779Sdim // rebase the stream. 486344779Sdim Types.setUnderlyingStream( 487344779Sdim Types.getUnderlyingStream().drop_front(FirstType->RecordData.size())); 488344779Sdim } 489344779Sdim 490344779Sdim // Fill in the temporary, caller-provided ObjectIndexMap. 491327952Sdim if (Config->DebugGHashes) { 492327952Sdim ArrayRef<GloballyHashedType> Hashes; 493327952Sdim std::vector<GloballyHashedType> OwnedHashes; 494327952Sdim if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File)) 495327952Sdim Hashes = getHashesFromDebugH(*DebugH); 496327952Sdim else { 497327952Sdim OwnedHashes = GloballyHashedType::hashTypes(Types); 498327952Sdim Hashes = OwnedHashes; 499327952Sdim } 500327952Sdim 501327952Sdim if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable, 502344779Sdim ObjectIndexMap->TPIMap, Types, Hashes, 503344779Sdim File->PCHSignature)) 504327952Sdim fatal("codeview::mergeTypeAndIdRecords failed: " + 505327952Sdim toString(std::move(Err))); 506327952Sdim } else { 507344779Sdim if (auto Err = 508344779Sdim mergeTypeAndIdRecords(IDTable, TypeTable, ObjectIndexMap->TPIMap, 509344779Sdim Types, File->PCHSignature)) 510327952Sdim fatal("codeview::mergeTypeAndIdRecords failed: " + 511327952Sdim toString(std::move(Err))); 512327952Sdim } 513344779Sdim return *ObjectIndexMap; 514314564Sdim} 515314564Sdim 516321369Sdimstatic Expected<std::unique_ptr<pdb::NativeSession>> 517344779SdimtryToLoadPDB(const codeview::GUID &GuidFromObj, StringRef TSPath) { 518344779Sdim // Ensure the file exists before anything else. We want to return ENOENT, 519344779Sdim // "file not found", even if the path points to a removable device (in which 520344779Sdim // case the return message would be EAGAIN, "resource unavailable try again") 521344779Sdim if (!llvm::sys::fs::exists(TSPath)) 522344779Sdim return errorCodeToError(std::error_code(ENOENT, std::generic_category())); 523344779Sdim 524327952Sdim ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile( 525327952Sdim TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false); 526327952Sdim if (!MBOrErr) 527327952Sdim return errorCodeToError(MBOrErr.getError()); 528327952Sdim 529321369Sdim std::unique_ptr<pdb::IPDBSession> ThisSession; 530327952Sdim if (auto EC = pdb::NativeSession::createFromPdb( 531327952Sdim MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)), 532327952Sdim /*RequiresNullTerminator=*/false), 533327952Sdim ThisSession)) 534321369Sdim return std::move(EC); 535321369Sdim 536321369Sdim std::unique_ptr<pdb::NativeSession> NS( 537321369Sdim static_cast<pdb::NativeSession *>(ThisSession.release())); 538321369Sdim pdb::PDBFile &File = NS->getPDBFile(); 539321369Sdim auto ExpectedInfo = File.getPDBInfoStream(); 540321369Sdim // All PDB Files should have an Info stream. 541321369Sdim if (!ExpectedInfo) 542321369Sdim return ExpectedInfo.takeError(); 543321369Sdim 544321369Sdim // Just because a file with a matching name was found and it was an actual 545321369Sdim // PDB file doesn't mean it matches. For it to match the InfoStream's GUID 546321369Sdim // must match the GUID specified in the TypeServer2 record. 547321369Sdim if (ExpectedInfo->getGuid() != GuidFromObj) 548344779Sdim return make_error<pdb::PDBError>(pdb::pdb_error_code::signature_out_of_date); 549321369Sdim 550321369Sdim return std::move(NS); 551321369Sdim} 552321369Sdim 553344779SdimExpected<const CVIndexMap &> 554344779SdimPDBLinker::maybeMergeTypeServerPDB(ObjFile *File, const CVType &FirstType) { 555344779Sdim TypeServer2Record TS; 556344779Sdim if (auto EC = 557344779Sdim TypeDeserializer::deserializeAs(const_cast<CVType &>(FirstType), TS)) 558344779Sdim fatal("error reading record: " + toString(std::move(EC))); 559344779Sdim 560344779Sdim const codeview::GUID &TSId = TS.getGuid(); 561329410Sdim StringRef TSPath = TS.getName(); 562329410Sdim 563329410Sdim // First, check if the PDB has previously failed to load. 564344779Sdim auto PrevErr = MissingTypeServerPDBs.find(TSId); 565344779Sdim if (PrevErr != MissingTypeServerPDBs.end()) 566344779Sdim return createFileError( 567344779Sdim TSPath, 568344779Sdim make_error<StringError>(PrevErr->second, inconvertibleErrorCode())); 569329410Sdim 570329410Sdim // Second, check if we already loaded a PDB with this GUID. Return the type 571321369Sdim // index mapping if we have it. 572329410Sdim auto Insertion = TypeServerIndexMappings.insert({TSId, CVIndexMap()}); 573321369Sdim CVIndexMap &IndexMap = Insertion.first->second; 574321369Sdim if (!Insertion.second) 575321369Sdim return IndexMap; 576321369Sdim 577321369Sdim // Mark this map as a type server map. 578321369Sdim IndexMap.IsTypeServerMap = true; 579321369Sdim 580321369Sdim // Check for a PDB at: 581321369Sdim // 1. The given file path 582321369Sdim // 2. Next to the object file or archive file 583344779Sdim auto ExpectedSession = handleExpected( 584344779Sdim tryToLoadPDB(TSId, TSPath), 585344779Sdim [&]() { 586344779Sdim StringRef LocalPath = 587344779Sdim !File->ParentName.empty() ? File->ParentName : File->getName(); 588344779Sdim SmallString<128> Path = sys::path::parent_path(LocalPath); 589344779Sdim // Currently, type server PDBs are only created by cl, which only runs 590344779Sdim // on Windows, so we can assume type server paths are Windows style. 591344779Sdim sys::path::append( 592344779Sdim Path, sys::path::filename(TSPath, sys::path::Style::windows)); 593344779Sdim return tryToLoadPDB(TSId, Path); 594344779Sdim }, 595344779Sdim [&](std::unique_ptr<ECError> EC) -> Error { 596344779Sdim auto SysErr = EC->convertToErrorCode(); 597344779Sdim // Only re-try loading if the previous error was "No such file or 598344779Sdim // directory" 599344779Sdim if (SysErr.category() == std::generic_category() && 600344779Sdim SysErr.value() == ENOENT) 601344779Sdim return Error::success(); 602344779Sdim return Error(std::move(EC)); 603344779Sdim }); 604344779Sdim 605329410Sdim if (auto E = ExpectedSession.takeError()) { 606329410Sdim TypeServerIndexMappings.erase(TSId); 607344779Sdim 608344779Sdim // Flatten the error to a string, for later display, if the error occurs 609344779Sdim // again on the same PDB. 610344779Sdim std::string ErrMsg; 611344779Sdim raw_string_ostream S(ErrMsg); 612344779Sdim S << E; 613344779Sdim MissingTypeServerPDBs.emplace(TSId, S.str()); 614344779Sdim 615344779Sdim return createFileError(TSPath, std::move(E)); 616329410Sdim } 617321369Sdim 618341825Sdim pdb::NativeSession *Session = ExpectedSession->get(); 619341825Sdim 620341825Sdim // Keep a strong reference to this PDB, so that it's safe to hold pointers 621341825Sdim // into the file. 622341825Sdim LoadedPDBs.push_back(std::move(*ExpectedSession)); 623341825Sdim 624341825Sdim auto ExpectedTpi = Session->getPDBFile().getPDBTpiStream(); 625321369Sdim if (auto E = ExpectedTpi.takeError()) 626327952Sdim fatal("Type server does not have TPI stream: " + toString(std::move(E))); 627341825Sdim auto ExpectedIpi = Session->getPDBFile().getPDBIpiStream(); 628321369Sdim if (auto E = ExpectedIpi.takeError()) 629327952Sdim fatal("Type server does not have TPI stream: " + toString(std::move(E))); 630321369Sdim 631327952Sdim if (Config->DebugGHashes) { 632327952Sdim // PDBs do not actually store global hashes, so when merging a type server 633327952Sdim // PDB we have to synthesize global hashes. To do this, we first synthesize 634327952Sdim // global hashes for the TPI stream, since it is independent, then we 635327952Sdim // synthesize hashes for the IPI stream, using the hashes for the TPI stream 636327952Sdim // as inputs. 637327952Sdim auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray()); 638327952Sdim auto IpiHashes = 639327952Sdim GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes); 640327952Sdim 641344779Sdim Optional<uint32_t> EndPrecomp; 642327952Sdim // Merge TPI first, because the IPI stream will reference type indices. 643327952Sdim if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap, 644344779Sdim ExpectedTpi->typeArray(), TpiHashes, EndPrecomp)) 645327952Sdim fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err))); 646327952Sdim 647327952Sdim // Merge IPI. 648327952Sdim if (auto Err = 649327952Sdim mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap, 650327952Sdim ExpectedIpi->typeArray(), IpiHashes)) 651327952Sdim fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err))); 652327952Sdim } else { 653327952Sdim // Merge TPI first, because the IPI stream will reference type indices. 654327952Sdim if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap, 655327952Sdim ExpectedTpi->typeArray())) 656327952Sdim fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err))); 657327952Sdim 658327952Sdim // Merge IPI. 659327952Sdim if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap, 660327952Sdim ExpectedIpi->typeArray())) 661327952Sdim fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err))); 662327952Sdim } 663327952Sdim 664321369Sdim return IndexMap; 665321369Sdim} 666321369Sdim 667344779SdimExpected<const CVIndexMap &> 668344779SdimPDBLinker::mergeInPrecompHeaderObj(ObjFile *File, const CVType &FirstType, 669344779Sdim CVIndexMap *ObjectIndexMap) { 670344779Sdim PrecompRecord Precomp; 671344779Sdim if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(FirstType), 672344779Sdim Precomp)) 673344779Sdim fatal("error reading record: " + toString(std::move(EC))); 674344779Sdim 675344779Sdim auto E = aquirePrecompObj(File, Precomp); 676344779Sdim if (!E) 677344779Sdim return E.takeError(); 678344779Sdim 679344779Sdim const CVIndexMap &PrecompIndexMap = *E; 680344779Sdim assert(PrecompIndexMap.IsPrecompiledTypeMap); 681344779Sdim 682344779Sdim if (PrecompIndexMap.TPIMap.empty()) 683344779Sdim return PrecompIndexMap; 684344779Sdim 685344779Sdim assert(Precomp.getStartTypeIndex() == TypeIndex::FirstNonSimpleIndex); 686344779Sdim assert(Precomp.getTypesCount() <= PrecompIndexMap.TPIMap.size()); 687344779Sdim // Use the previously remapped index map from the precompiled headers. 688344779Sdim ObjectIndexMap->TPIMap.append(PrecompIndexMap.TPIMap.begin(), 689344779Sdim PrecompIndexMap.TPIMap.begin() + 690344779Sdim Precomp.getTypesCount()); 691344779Sdim return *ObjectIndexMap; 692344779Sdim} 693344779Sdim 694344779Sdimstatic bool equals_path(StringRef path1, StringRef path2) { 695344779Sdim#if defined(_WIN32) 696344779Sdim return path1.equals_lower(path2); 697344779Sdim#else 698344779Sdim return path1.equals(path2); 699344779Sdim#endif 700344779Sdim} 701344779Sdim 702344779Sdim// Find by name an OBJ provided on the command line 703344779Sdimstatic ObjFile *findObjByName(StringRef FileNameOnly) { 704344779Sdim SmallString<128> CurrentPath; 705344779Sdim 706344779Sdim for (ObjFile *F : ObjFile::Instances) { 707344779Sdim StringRef CurrentFileName = sys::path::filename(F->getName()); 708344779Sdim 709344779Sdim // Compare based solely on the file name (link.exe behavior) 710344779Sdim if (equals_path(CurrentFileName, FileNameOnly)) 711344779Sdim return F; 712344779Sdim } 713344779Sdim return nullptr; 714344779Sdim} 715344779Sdim 716344779Sdimstd::pair<CVIndexMap &, bool /*already there*/> 717344779SdimPDBLinker::registerPrecompiledHeaders(uint32_t Signature) { 718344779Sdim auto Insertion = PrecompTypeIndexMappings.insert({Signature, CVIndexMap()}); 719344779Sdim CVIndexMap &IndexMap = Insertion.first->second; 720344779Sdim if (!Insertion.second) 721344779Sdim return {IndexMap, true}; 722344779Sdim // Mark this map as a precompiled types map. 723344779Sdim IndexMap.IsPrecompiledTypeMap = true; 724344779Sdim return {IndexMap, false}; 725344779Sdim} 726344779Sdim 727344779SdimExpected<const CVIndexMap &> 728344779SdimPDBLinker::aquirePrecompObj(ObjFile *File, PrecompRecord Precomp) { 729344779Sdim // First, check if we already loaded the precompiled headers object with this 730344779Sdim // signature. Return the type index mapping if we've already seen it. 731344779Sdim auto R = registerPrecompiledHeaders(Precomp.getSignature()); 732344779Sdim if (R.second) 733344779Sdim return R.first; 734344779Sdim 735344779Sdim CVIndexMap &IndexMap = R.first; 736344779Sdim 737344779Sdim // Cross-compile warning: given that Clang doesn't generate LF_PRECOMP 738344779Sdim // records, we assume the OBJ comes from a Windows build of cl.exe. Thusly, 739344779Sdim // the paths embedded in the OBJs are in the Windows format. 740344779Sdim SmallString<128> PrecompFileName = sys::path::filename( 741344779Sdim Precomp.getPrecompFilePath(), sys::path::Style::windows); 742344779Sdim 743344779Sdim // link.exe requires that a precompiled headers object must always be provided 744344779Sdim // on the command-line, even if that's not necessary. 745344779Sdim auto PrecompFile = findObjByName(PrecompFileName); 746344779Sdim if (!PrecompFile) 747344779Sdim return createFileError( 748344779Sdim PrecompFileName.str(), 749344779Sdim make_error<pdb::PDBError>(pdb::pdb_error_code::external_cmdline_ref)); 750344779Sdim 751344779Sdim addObjFile(PrecompFile, &IndexMap); 752344779Sdim 753344779Sdim if (!PrecompFile->PCHSignature) 754344779Sdim fatal(PrecompFile->getName() + " is not a precompiled headers object"); 755344779Sdim 756344779Sdim if (Precomp.getSignature() != PrecompFile->PCHSignature.getValueOr(0)) 757344779Sdim return createFileError( 758344779Sdim Precomp.getPrecompFilePath().str(), 759344779Sdim make_error<pdb::PDBError>(pdb::pdb_error_code::signature_out_of_date)); 760344779Sdim 761344779Sdim return IndexMap; 762344779Sdim} 763344779Sdim 764321369Sdimstatic bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) { 765321369Sdim if (TI.isSimple()) 766321369Sdim return true; 767321369Sdim if (TI.toArrayIndex() >= TypeIndexMap.size()) 768321369Sdim return false; 769321369Sdim TI = TypeIndexMap[TI.toArrayIndex()]; 770321369Sdim return true; 771321369Sdim} 772321369Sdim 773327952Sdimstatic void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind, 774344779Sdim MutableArrayRef<uint8_t> RecordBytes, 775321369Sdim const CVIndexMap &IndexMap, 776321369Sdim ArrayRef<TiReference> TypeRefs) { 777344779Sdim MutableArrayRef<uint8_t> Contents = 778344779Sdim RecordBytes.drop_front(sizeof(RecordPrefix)); 779321369Sdim for (const TiReference &Ref : TypeRefs) { 780321369Sdim unsigned ByteSize = Ref.Count * sizeof(TypeIndex); 781321369Sdim if (Contents.size() < Ref.Offset + ByteSize) 782321369Sdim fatal("symbol record too short"); 783321369Sdim 784321369Sdim // This can be an item index or a type index. Choose the appropriate map. 785321369Sdim ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap; 786327952Sdim bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef; 787327952Sdim if (IsItemIndex && IndexMap.IsTypeServerMap) 788321369Sdim TypeOrItemMap = IndexMap.IPIMap; 789321369Sdim 790321369Sdim MutableArrayRef<TypeIndex> TIs( 791321369Sdim reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count); 792321369Sdim for (TypeIndex &TI : TIs) { 793321369Sdim if (!remapTypeIndex(TI, TypeOrItemMap)) { 794327952Sdim log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " + 795327952Sdim File->getName() + " with bad " + (IsItemIndex ? "item" : "type") + 796327952Sdim " index 0x" + utohexstr(TI.getIndex())); 797321369Sdim TI = TypeIndex(SimpleTypeKind::NotTranslated); 798321369Sdim continue; 799321369Sdim } 800321369Sdim } 801321369Sdim } 802321369Sdim} 803321369Sdim 804341825Sdimstatic void 805341825SdimrecordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents, 806341825Sdim uint32_t Offset, 807341825Sdim std::vector<ulittle32_t *> &StrTableRefs) { 808341825Sdim Contents = 809341825Sdim Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t)); 810341825Sdim ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data()); 811341825Sdim StrTableRefs.push_back(Index); 812341825Sdim} 813341825Sdim 814341825Sdimstatic void 815341825SdimrecordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents, 816341825Sdim std::vector<ulittle32_t *> &StrTableRefs) { 817341825Sdim // For now we only handle S_FILESTATIC, but we may need the same logic for 818341825Sdim // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any 819341825Sdim // PDBs that contain these types of records, so because of the uncertainty 820341825Sdim // they are omitted here until we can prove that it's necessary. 821341825Sdim switch (Kind) { 822341825Sdim case SymbolKind::S_FILESTATIC: 823341825Sdim // FileStaticSym::ModFileOffset 824344779Sdim recordStringTableReferenceAtOffset(Contents, 8, StrTableRefs); 825341825Sdim break; 826341825Sdim case SymbolKind::S_DEFRANGE: 827341825Sdim case SymbolKind::S_DEFRANGE_SUBFIELD: 828341825Sdim log("Not fixing up string table reference in S_DEFRANGE / " 829341825Sdim "S_DEFRANGE_SUBFIELD record"); 830341825Sdim break; 831341825Sdim default: 832341825Sdim break; 833341825Sdim } 834341825Sdim} 835341825Sdim 836327952Sdimstatic SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) { 837327952Sdim const RecordPrefix *Prefix = 838327952Sdim reinterpret_cast<const RecordPrefix *>(RecordData.data()); 839327952Sdim return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind)); 840321369Sdim} 841321369Sdim 842327952Sdim/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32 843327952Sdimstatic void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData, 844327952Sdim TypeCollection &IDTable) { 845327952Sdim RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data()); 846327952Sdim 847327952Sdim SymbolKind Kind = symbolKind(RecordData); 848327952Sdim 849327952Sdim if (Kind == SymbolKind::S_PROC_ID_END) { 850327952Sdim Prefix->RecordKind = SymbolKind::S_END; 851327952Sdim return; 852327952Sdim } 853327952Sdim 854327952Sdim // In an object file, GPROC32_ID has an embedded reference which refers to the 855327952Sdim // single object file type index namespace. This has already been translated 856327952Sdim // to the PDB file's ID stream index space, but we need to convert this to a 857327952Sdim // symbol that refers to the type stream index space. So we remap again from 858327952Sdim // ID index space to type index space. 859327952Sdim if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) { 860327952Sdim SmallVector<TiReference, 1> Refs; 861327952Sdim auto Content = RecordData.drop_front(sizeof(RecordPrefix)); 862327952Sdim CVSymbol Sym(Kind, RecordData); 863327952Sdim discoverTypeIndicesInSymbol(Sym, Refs); 864327952Sdim assert(Refs.size() == 1); 865327952Sdim assert(Refs.front().Count == 1); 866327952Sdim 867327952Sdim TypeIndex *TI = 868327952Sdim reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset); 869327952Sdim // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in 870327952Sdim // the IPI stream, whose `FunctionType` member refers to the TPI stream. 871327952Sdim // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and 872327952Sdim // in both cases we just need the second type index. 873327952Sdim if (!TI->isSimple() && !TI->isNoneType()) { 874327952Sdim CVType FuncIdData = IDTable.getType(*TI); 875327952Sdim SmallVector<TypeIndex, 2> Indices; 876327952Sdim discoverTypeIndices(FuncIdData, Indices); 877327952Sdim assert(Indices.size() == 2); 878327952Sdim *TI = Indices[1]; 879327952Sdim } 880327952Sdim 881327952Sdim Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32 882327952Sdim : SymbolKind::S_LPROC32; 883327952Sdim Prefix->RecordKind = uint16_t(Kind); 884327952Sdim } 885327952Sdim} 886327952Sdim 887321369Sdim/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. 888321369Sdim/// The object file may not be aligned. 889344779Sdimstatic MutableArrayRef<uint8_t> 890344779SdimcopyAndAlignSymbol(const CVSymbol &Sym, MutableArrayRef<uint8_t> &AlignedMem) { 891321369Sdim size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb)); 892321369Sdim assert(Size >= 4 && "record too short"); 893321369Sdim assert(Size <= MaxRecordLength && "record too long"); 894344779Sdim assert(AlignedMem.size() >= Size && "didn't preallocate enough"); 895321369Sdim 896321369Sdim // Copy the symbol record and zero out any padding bytes. 897344779Sdim MutableArrayRef<uint8_t> NewData = AlignedMem.take_front(Size); 898344779Sdim AlignedMem = AlignedMem.drop_front(Size); 899321369Sdim memcpy(NewData.data(), Sym.data().data(), Sym.length()); 900321369Sdim memset(NewData.data() + Sym.length(), 0, Size - Sym.length()); 901321369Sdim 902321369Sdim // Update the record prefix length. It should point to the beginning of the 903327952Sdim // next record. 904344779Sdim auto *Prefix = reinterpret_cast<RecordPrefix *>(NewData.data()); 905321369Sdim Prefix->RecordLen = Size - 2; 906321369Sdim return NewData; 907321369Sdim} 908321369Sdim 909321369Sdimstruct ScopeRecord { 910321369Sdim ulittle32_t PtrParent; 911321369Sdim ulittle32_t PtrEnd; 912321369Sdim}; 913321369Sdim 914321369Sdimstruct SymbolScope { 915321369Sdim ScopeRecord *OpeningRecord; 916321369Sdim uint32_t ScopeOffset; 917321369Sdim}; 918321369Sdim 919321369Sdimstatic void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack, 920321369Sdim uint32_t CurOffset, CVSymbol &Sym) { 921321369Sdim assert(symbolOpensScope(Sym.kind())); 922321369Sdim SymbolScope S; 923321369Sdim S.ScopeOffset = CurOffset; 924321369Sdim S.OpeningRecord = const_cast<ScopeRecord *>( 925321369Sdim reinterpret_cast<const ScopeRecord *>(Sym.content().data())); 926321369Sdim S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset; 927321369Sdim Stack.push_back(S); 928321369Sdim} 929321369Sdim 930321369Sdimstatic void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack, 931327952Sdim uint32_t CurOffset, ObjFile *File) { 932321369Sdim if (Stack.empty()) { 933321369Sdim warn("symbol scopes are not balanced in " + File->getName()); 934314564Sdim return; 935321369Sdim } 936321369Sdim SymbolScope S = Stack.pop_back_val(); 937321369Sdim S.OpeningRecord->PtrEnd = CurOffset; 938321369Sdim} 939314564Sdim 940344779Sdimstatic bool symbolGoesInModuleStream(const CVSymbol &Sym, bool IsGlobalScope) { 941327952Sdim switch (Sym.kind()) { 942327952Sdim case SymbolKind::S_GDATA32: 943327952Sdim case SymbolKind::S_CONSTANT: 944327952Sdim // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 945327952Sdim // since they are synthesized by the linker in response to S_GPROC32 and 946327952Sdim // S_LPROC32, but if we do see them, don't put them in the module stream I 947327952Sdim // guess. 948327952Sdim case SymbolKind::S_PROCREF: 949327952Sdim case SymbolKind::S_LPROCREF: 950327952Sdim return false; 951344779Sdim // S_UDT records go in the module stream if it is not a global S_UDT. 952344779Sdim case SymbolKind::S_UDT: 953344779Sdim return !IsGlobalScope; 954327952Sdim // S_GDATA32 does not go in the module stream, but S_LDATA32 does. 955327952Sdim case SymbolKind::S_LDATA32: 956327952Sdim default: 957327952Sdim return true; 958327952Sdim } 959327952Sdim} 960327952Sdim 961344779Sdimstatic bool symbolGoesInGlobalsStream(const CVSymbol &Sym, bool IsGlobalScope) { 962327952Sdim switch (Sym.kind()) { 963327952Sdim case SymbolKind::S_CONSTANT: 964327952Sdim case SymbolKind::S_GDATA32: 965327952Sdim // S_LDATA32 goes in both the module stream and the globals stream. 966327952Sdim case SymbolKind::S_LDATA32: 967327952Sdim case SymbolKind::S_GPROC32: 968327952Sdim case SymbolKind::S_LPROC32: 969327952Sdim // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 970327952Sdim // since they are synthesized by the linker in response to S_GPROC32 and 971327952Sdim // S_LPROC32, but if we do see them, copy them straight through. 972327952Sdim case SymbolKind::S_PROCREF: 973327952Sdim case SymbolKind::S_LPROCREF: 974327952Sdim return true; 975344779Sdim // S_UDT records go in the globals stream if it is a global S_UDT. 976327952Sdim case SymbolKind::S_UDT: 977344779Sdim return IsGlobalScope; 978327952Sdim default: 979327952Sdim return false; 980327952Sdim } 981327952Sdim} 982327952Sdim 983344779Sdimstatic void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, uint16_t ModIndex, 984344779Sdim unsigned SymOffset, const CVSymbol &Sym) { 985327952Sdim switch (Sym.kind()) { 986327952Sdim case SymbolKind::S_CONSTANT: 987327952Sdim case SymbolKind::S_UDT: 988327952Sdim case SymbolKind::S_GDATA32: 989327952Sdim case SymbolKind::S_LDATA32: 990327952Sdim case SymbolKind::S_PROCREF: 991327952Sdim case SymbolKind::S_LPROCREF: 992327952Sdim Builder.addGlobalSymbol(Sym); 993327952Sdim break; 994327952Sdim case SymbolKind::S_GPROC32: 995327952Sdim case SymbolKind::S_LPROC32: { 996327952Sdim SymbolRecordKind K = SymbolRecordKind::ProcRefSym; 997327952Sdim if (Sym.kind() == SymbolKind::S_LPROC32) 998327952Sdim K = SymbolRecordKind::LocalProcRef; 999327952Sdim ProcRefSym PS(K); 1000344779Sdim PS.Module = ModIndex; 1001327952Sdim // For some reason, MSVC seems to add one to this value. 1002327952Sdim ++PS.Module; 1003327952Sdim PS.Name = getSymbolName(Sym); 1004327952Sdim PS.SumName = 0; 1005344779Sdim PS.SymOffset = SymOffset; 1006327952Sdim Builder.addGlobalSymbol(PS); 1007327952Sdim break; 1008327952Sdim } 1009327952Sdim default: 1010327952Sdim llvm_unreachable("Invalid symbol kind!"); 1011327952Sdim } 1012327952Sdim} 1013327952Sdim 1014344779Sdimvoid PDBLinker::mergeSymbolRecords(ObjFile *File, const CVIndexMap &IndexMap, 1015344779Sdim std::vector<ulittle32_t *> &StringTableRefs, 1016344779Sdim BinaryStreamRef SymData) { 1017341825Sdim ArrayRef<uint8_t> SymsBuffer; 1018341825Sdim cantFail(SymData.readBytes(0, SymData.getLength(), SymsBuffer)); 1019321369Sdim SmallVector<SymbolScope, 4> Scopes; 1020314564Sdim 1021344779Sdim // Iterate every symbol to check if any need to be realigned, and if so, how 1022344779Sdim // much space we need to allocate for them. 1023344779Sdim bool NeedsRealignment = false; 1024344779Sdim unsigned TotalRealignedSize = 0; 1025341825Sdim auto EC = forEachCodeViewRecord<CVSymbol>( 1026344779Sdim SymsBuffer, [&](CVSymbol Sym) -> llvm::Error { 1027344779Sdim unsigned RealignedSize = 1028344779Sdim alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb)); 1029344779Sdim NeedsRealignment |= RealignedSize != Sym.length(); 1030344779Sdim TotalRealignedSize += RealignedSize; 1031344779Sdim return Error::success(); 1032344779Sdim }); 1033344779Sdim 1034344779Sdim // If any of the symbol record lengths was corrupt, ignore them all, warn 1035344779Sdim // about it, and move on. 1036344779Sdim if (EC) { 1037344779Sdim warn("corrupt symbol records in " + File->getName()); 1038344779Sdim consumeError(std::move(EC)); 1039344779Sdim return; 1040344779Sdim } 1041344779Sdim 1042344779Sdim // If any symbol needed realignment, allocate enough contiguous memory for 1043344779Sdim // them all. Typically symbol subsections are small enough that this will not 1044344779Sdim // cause fragmentation. 1045344779Sdim MutableArrayRef<uint8_t> AlignedSymbolMem; 1046344779Sdim if (NeedsRealignment) { 1047344779Sdim void *AlignedData = 1048344779Sdim Alloc.Allocate(TotalRealignedSize, alignOf(CodeViewContainer::Pdb)); 1049344779Sdim AlignedSymbolMem = makeMutableArrayRef( 1050344779Sdim reinterpret_cast<uint8_t *>(AlignedData), TotalRealignedSize); 1051344779Sdim } 1052344779Sdim 1053344779Sdim // Iterate again, this time doing the real work. 1054344779Sdim unsigned CurSymOffset = File->ModuleDBI->getNextSymbolOffset(); 1055344779Sdim ArrayRef<uint8_t> BulkSymbols; 1056344779Sdim cantFail(forEachCodeViewRecord<CVSymbol>( 1057344779Sdim SymsBuffer, [&](CVSymbol Sym) -> llvm::Error { 1058344779Sdim // Align the record if required. 1059344779Sdim MutableArrayRef<uint8_t> RecordBytes; 1060344779Sdim if (NeedsRealignment) { 1061344779Sdim RecordBytes = copyAndAlignSymbol(Sym, AlignedSymbolMem); 1062344779Sdim Sym = CVSymbol(Sym.kind(), RecordBytes); 1063344779Sdim } else { 1064344779Sdim // Otherwise, we can actually mutate the symbol directly, since we 1065344779Sdim // copied it to apply relocations. 1066344779Sdim RecordBytes = makeMutableArrayRef( 1067344779Sdim const_cast<uint8_t *>(Sym.data().data()), Sym.length()); 1068344779Sdim } 1069344779Sdim 1070341825Sdim // Discover type index references in the record. Skip it if we don't 1071341825Sdim // know where they are. 1072341825Sdim SmallVector<TiReference, 32> TypeRefs; 1073341825Sdim if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) { 1074341825Sdim log("ignoring unknown symbol record with kind 0x" + 1075341825Sdim utohexstr(Sym.kind())); 1076341825Sdim return Error::success(); 1077341825Sdim } 1078321369Sdim 1079341825Sdim // Re-map all the type index references. 1080344779Sdim remapTypesInSymbolRecord(File, Sym.kind(), RecordBytes, IndexMap, 1081341825Sdim TypeRefs); 1082327952Sdim 1083341825Sdim // An object file may have S_xxx_ID symbols, but these get converted to 1084341825Sdim // "real" symbols in a PDB. 1085344779Sdim translateIdSymbols(RecordBytes, getIDTable()); 1086344779Sdim Sym = CVSymbol(symbolKind(RecordBytes), RecordBytes); 1087327952Sdim 1088341825Sdim // If this record refers to an offset in the object file's string table, 1089341825Sdim // add that item to the global PDB string table and re-write the index. 1090344779Sdim recordStringTableReferences(Sym.kind(), RecordBytes, StringTableRefs); 1091321369Sdim 1092341825Sdim // Fill in "Parent" and "End" fields by maintaining a stack of scopes. 1093344779Sdim if (symbolOpensScope(Sym.kind())) 1094344779Sdim scopeStackOpen(Scopes, CurSymOffset, Sym); 1095344779Sdim else if (symbolEndsScope(Sym.kind())) 1096344779Sdim scopeStackClose(Scopes, CurSymOffset, File); 1097341825Sdim 1098341825Sdim // Add the symbol to the globals stream if necessary. Do this before 1099341825Sdim // adding the symbol to the module since we may need to get the next 1100341825Sdim // symbol offset, and writing to the module's symbol stream will update 1101341825Sdim // that offset. 1102344779Sdim if (symbolGoesInGlobalsStream(Sym, Scopes.empty())) 1103344779Sdim addGlobalSymbol(Builder.getGsiBuilder(), 1104344779Sdim File->ModuleDBI->getModuleIndex(), CurSymOffset, Sym); 1105341825Sdim 1106344779Sdim if (symbolGoesInModuleStream(Sym, Scopes.empty())) { 1107344779Sdim // Add symbols to the module in bulk. If this symbol is contiguous 1108344779Sdim // with the previous run of symbols to add, combine the ranges. If 1109344779Sdim // not, close the previous range of symbols and start a new one. 1110344779Sdim if (Sym.data().data() == BulkSymbols.end()) { 1111344779Sdim BulkSymbols = makeArrayRef(BulkSymbols.data(), 1112344779Sdim BulkSymbols.size() + Sym.length()); 1113344779Sdim } else { 1114344779Sdim File->ModuleDBI->addSymbolsInBulk(BulkSymbols); 1115344779Sdim BulkSymbols = RecordBytes; 1116344779Sdim } 1117344779Sdim CurSymOffset += Sym.length(); 1118344779Sdim } 1119341825Sdim return Error::success(); 1120344779Sdim })); 1121344779Sdim 1122344779Sdim // Add any remaining symbols we've accumulated. 1123344779Sdim File->ModuleDBI->addSymbolsInBulk(BulkSymbols); 1124314564Sdim} 1125314564Sdim 1126344779Sdim// Allocate memory for a .debug$S / .debug$F section and relocate it. 1127321369Sdimstatic ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, 1128344779Sdim SectionChunk &DebugChunk) { 1129344779Sdim uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk.getSize()); 1130344779Sdim assert(DebugChunk.OutputSectionOff == 0 && 1131321369Sdim "debug sections should not be in output sections"); 1132344779Sdim DebugChunk.readRelocTargets(); 1133344779Sdim DebugChunk.writeTo(Buffer); 1134344779Sdim return makeArrayRef(Buffer, DebugChunk.getSize()); 1135321369Sdim} 1136314564Sdim 1137341825Sdimstatic pdb::SectionContrib createSectionContrib(const Chunk *C, uint32_t Modi) { 1138341825Sdim OutputSection *OS = C->getOutputSection(); 1139341825Sdim pdb::SectionContrib SC; 1140341825Sdim memset(&SC, 0, sizeof(SC)); 1141341825Sdim SC.ISect = OS->SectionIndex; 1142341825Sdim SC.Off = C->getRVA() - OS->getRVA(); 1143341825Sdim SC.Size = C->getSize(); 1144341825Sdim if (auto *SecChunk = dyn_cast<SectionChunk>(C)) { 1145341825Sdim SC.Characteristics = SecChunk->Header->Characteristics; 1146341825Sdim SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex(); 1147341825Sdim ArrayRef<uint8_t> Contents = SecChunk->getContents(); 1148341825Sdim JamCRC CRC(0); 1149341825Sdim ArrayRef<char> CharContents = makeArrayRef( 1150341825Sdim reinterpret_cast<const char *>(Contents.data()), Contents.size()); 1151341825Sdim CRC.update(CharContents); 1152341825Sdim SC.DataCrc = CRC.getCRC(); 1153341825Sdim } else { 1154341825Sdim SC.Characteristics = OS->Header.Characteristics; 1155341825Sdim // FIXME: When we start creating DBI for import libraries, use those here. 1156341825Sdim SC.Imod = Modi; 1157341825Sdim } 1158341825Sdim SC.RelocCrc = 0; // FIXME 1159341825Sdim 1160341825Sdim return SC; 1161341825Sdim} 1162341825Sdim 1163344779Sdimstatic uint32_t 1164344779SdimtranslateStringTableIndex(uint32_t ObjIndex, 1165344779Sdim const DebugStringTableSubsectionRef &ObjStrTable, 1166344779Sdim DebugStringTableSubsection &PdbStrTable) { 1167344779Sdim auto ExpectedString = ObjStrTable.getString(ObjIndex); 1168344779Sdim if (!ExpectedString) { 1169344779Sdim warn("Invalid string table reference"); 1170344779Sdim consumeError(ExpectedString.takeError()); 1171344779Sdim return 0; 1172344779Sdim } 1173344779Sdim 1174344779Sdim return PdbStrTable.insert(*ExpectedString); 1175344779Sdim} 1176344779Sdim 1177344779Sdimvoid DebugSHandler::handleDebugS(lld::coff::SectionChunk &DebugS) { 1178344779Sdim DebugSubsectionArray Subsections; 1179344779Sdim 1180344779Sdim ArrayRef<uint8_t> RelocatedDebugContents = consumeDebugMagic( 1181344779Sdim relocateDebugChunk(Linker.Alloc, DebugS), DebugS.getSectionName()); 1182344779Sdim 1183344779Sdim BinaryStreamReader Reader(RelocatedDebugContents, support::little); 1184344779Sdim ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size())); 1185344779Sdim 1186344779Sdim for (const DebugSubsectionRecord &SS : Subsections) { 1187344779Sdim switch (SS.kind()) { 1188344779Sdim case DebugSubsectionKind::StringTable: { 1189344779Sdim assert(!CVStrTab.valid() && 1190344779Sdim "Encountered multiple string table subsections!"); 1191344779Sdim ExitOnErr(CVStrTab.initialize(SS.getRecordData())); 1192344779Sdim break; 1193344779Sdim } 1194344779Sdim case DebugSubsectionKind::FileChecksums: 1195344779Sdim assert(!Checksums.valid() && 1196344779Sdim "Encountered multiple checksum subsections!"); 1197344779Sdim ExitOnErr(Checksums.initialize(SS.getRecordData())); 1198344779Sdim break; 1199344779Sdim case DebugSubsectionKind::Lines: 1200344779Sdim // We can add the relocated line table directly to the PDB without 1201344779Sdim // modification because the file checksum offsets will stay the same. 1202344779Sdim File.ModuleDBI->addDebugSubsection(SS); 1203344779Sdim break; 1204344779Sdim case DebugSubsectionKind::FrameData: { 1205344779Sdim // We need to re-write string table indices here, so save off all 1206344779Sdim // frame data subsections until we've processed the entire list of 1207344779Sdim // subsections so that we can be sure we have the string table. 1208344779Sdim DebugFrameDataSubsectionRef FDS; 1209344779Sdim ExitOnErr(FDS.initialize(SS.getRecordData())); 1210344779Sdim NewFpoFrames.push_back(std::move(FDS)); 1211344779Sdim break; 1212344779Sdim } 1213344779Sdim case DebugSubsectionKind::Symbols: { 1214344779Sdim Linker.mergeSymbolRecords(&File, IndexMap, StringTableReferences, 1215344779Sdim SS.getRecordData()); 1216344779Sdim break; 1217344779Sdim } 1218344779Sdim default: 1219344779Sdim // FIXME: Process the rest of the subsections. 1220344779Sdim break; 1221344779Sdim } 1222344779Sdim } 1223344779Sdim} 1224344779Sdim 1225344779Sdimvoid DebugSHandler::finish() { 1226344779Sdim pdb::DbiStreamBuilder &DbiBuilder = Linker.Builder.getDbiBuilder(); 1227344779Sdim 1228344779Sdim // We should have seen all debug subsections across the entire object file now 1229344779Sdim // which means that if a StringTable subsection and Checksums subsection were 1230344779Sdim // present, now is the time to handle them. 1231344779Sdim if (!CVStrTab.valid()) { 1232344779Sdim if (Checksums.valid()) 1233344779Sdim fatal(".debug$S sections with a checksums subsection must also contain a " 1234344779Sdim "string table subsection"); 1235344779Sdim 1236344779Sdim if (!StringTableReferences.empty()) 1237344779Sdim warn("No StringTable subsection was encountered, but there are string " 1238344779Sdim "table references"); 1239344779Sdim return; 1240344779Sdim } 1241344779Sdim 1242344779Sdim // Rewrite string table indices in the Fpo Data and symbol records to refer to 1243344779Sdim // the global PDB string table instead of the object file string table. 1244344779Sdim for (DebugFrameDataSubsectionRef &FDS : NewFpoFrames) { 1245344779Sdim const ulittle32_t *Reloc = FDS.getRelocPtr(); 1246344779Sdim for (codeview::FrameData FD : FDS) { 1247344779Sdim FD.RvaStart += *Reloc; 1248344779Sdim FD.FrameFunc = 1249344779Sdim translateStringTableIndex(FD.FrameFunc, CVStrTab, Linker.PDBStrTab); 1250344779Sdim DbiBuilder.addNewFpoData(FD); 1251344779Sdim } 1252344779Sdim } 1253344779Sdim 1254344779Sdim for (ulittle32_t *Ref : StringTableReferences) 1255344779Sdim *Ref = translateStringTableIndex(*Ref, CVStrTab, Linker.PDBStrTab); 1256344779Sdim 1257344779Sdim // Make a new file checksum table that refers to offsets in the PDB-wide 1258344779Sdim // string table. Generally the string table subsection appears after the 1259344779Sdim // checksum table, so we have to do this after looping over all the 1260344779Sdim // subsections. 1261344779Sdim auto NewChecksums = make_unique<DebugChecksumsSubsection>(Linker.PDBStrTab); 1262344779Sdim for (FileChecksumEntry &FC : Checksums) { 1263344779Sdim SmallString<128> FileName = 1264344779Sdim ExitOnErr(CVStrTab.getString(FC.FileNameOffset)); 1265344779Sdim pdbMakeAbsolute(FileName); 1266344779Sdim ExitOnErr(Linker.Builder.getDbiBuilder().addModuleSourceFile( 1267344779Sdim *File.ModuleDBI, FileName)); 1268344779Sdim NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum); 1269344779Sdim } 1270344779Sdim File.ModuleDBI->addDebugSubsection(std::move(NewChecksums)); 1271344779Sdim} 1272344779Sdim 1273344779Sdimvoid PDBLinker::addObjFile(ObjFile *File, CVIndexMap *ExternIndexMap) { 1274344779Sdim if (File->wasProcessedForPDB()) 1275344779Sdim return; 1276321369Sdim // Add a module descriptor for every object file. We need to put an absolute 1277321369Sdim // path to the object into the PDB. If this is a plain object, we make its 1278321369Sdim // path absolute. If it's an object in an archive, we make the archive path 1279321369Sdim // absolute. 1280321369Sdim bool InArchive = !File->ParentName.empty(); 1281321369Sdim SmallString<128> Path = InArchive ? File->ParentName : File->getName(); 1282344779Sdim pdbMakeAbsolute(Path); 1283321369Sdim StringRef Name = InArchive ? File->getName() : StringRef(Path); 1284321369Sdim 1285344779Sdim pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); 1286344779Sdim File->ModuleDBI = &ExitOnErr(DbiBuilder.addModuleInfo(Name)); 1287321369Sdim File->ModuleDBI->setObjFileName(Path); 1288321369Sdim 1289341825Sdim auto Chunks = File->getChunks(); 1290341825Sdim uint32_t Modi = File->ModuleDBI->getModuleIndex(); 1291341825Sdim for (Chunk *C : Chunks) { 1292341825Sdim auto *SecChunk = dyn_cast<SectionChunk>(C); 1293344779Sdim if (!SecChunk || !SecChunk->Live) 1294341825Sdim continue; 1295341825Sdim pdb::SectionContrib SC = createSectionContrib(SecChunk, Modi); 1296341825Sdim File->ModuleDBI->setFirstSectionContrib(SC); 1297341825Sdim break; 1298341825Sdim } 1299341825Sdim 1300321369Sdim // Before we can process symbol substreams from .debug$S, we need to process 1301321369Sdim // type information, file checksums, and the string table. Add type info to 1302321369Sdim // the PDB first, so that we can get the map from object file type and item 1303321369Sdim // indices to PDB type and item indices. 1304321369Sdim CVIndexMap ObjectIndexMap; 1305344779Sdim auto IndexMapResult = 1306344779Sdim mergeDebugT(File, ExternIndexMap ? ExternIndexMap : &ObjectIndexMap); 1307321369Sdim 1308329410Sdim // If the .debug$T sections fail to merge, assume there is no debug info. 1309329410Sdim if (!IndexMapResult) { 1310344779Sdim if (!Config->WarnDebugInfoUnusable) { 1311344779Sdim consumeError(IndexMapResult.takeError()); 1312344779Sdim return; 1313344779Sdim } 1314344779Sdim StringRef FileName = sys::path::filename(Path); 1315344779Sdim warn("Cannot use debug info for '" + FileName + "' [LNK4099]\n" + 1316344779Sdim ">>> failed to load reference " + 1317344779Sdim StringRef(toString(IndexMapResult.takeError()))); 1318329410Sdim return; 1319329410Sdim } 1320329410Sdim 1321341825Sdim ScopedTimer T(SymbolMergingTimer); 1322341825Sdim 1323344779Sdim DebugSHandler DSH(*this, *File, *IndexMapResult); 1324344779Sdim // Now do all live .debug$S and .debug$F sections. 1325321369Sdim for (SectionChunk *DebugChunk : File->getDebugChunks()) { 1326344779Sdim if (!DebugChunk->Live || DebugChunk->getSize() == 0) 1327321369Sdim continue; 1328321369Sdim 1329344779Sdim if (DebugChunk->getSectionName() == ".debug$S") { 1330344779Sdim DSH.handleDebugS(*DebugChunk); 1331321369Sdim continue; 1332321369Sdim } 1333321369Sdim 1334344779Sdim if (DebugChunk->getSectionName() == ".debug$F") { 1335344779Sdim ArrayRef<uint8_t> RelocatedDebugContents = 1336344779Sdim relocateDebugChunk(Alloc, *DebugChunk); 1337341825Sdim 1338344779Sdim FixedStreamArray<object::FpoData> FpoRecords; 1339344779Sdim BinaryStreamReader Reader(RelocatedDebugContents, support::little); 1340344779Sdim uint32_t Count = RelocatedDebugContents.size() / sizeof(object::FpoData); 1341344779Sdim ExitOnErr(Reader.readArray(FpoRecords, Count)); 1342341825Sdim 1343344779Sdim // These are already relocated and don't refer to the string table, so we 1344344779Sdim // can just copy it. 1345344779Sdim for (const object::FpoData &FD : FpoRecords) 1346344779Sdim DbiBuilder.addOldFpoData(FD); 1347341825Sdim continue; 1348321369Sdim } 1349314564Sdim } 1350341825Sdim 1351344779Sdim // Do any post-processing now that all .debug$S sections have been processed. 1352344779Sdim DSH.finish(); 1353314564Sdim} 1354314564Sdim 1355327952Sdimstatic PublicSym32 createPublic(Defined *Def) { 1356327952Sdim PublicSym32 Pub(SymbolKind::S_PUB32); 1357327952Sdim Pub.Name = Def->getName(); 1358327952Sdim if (auto *D = dyn_cast<DefinedCOFF>(Def)) { 1359327952Sdim if (D->getCOFFSymbol().isFunctionDefinition()) 1360327952Sdim Pub.Flags = PublicSymFlags::Function; 1361327952Sdim } else if (isa<DefinedImportThunk>(Def)) { 1362327952Sdim Pub.Flags = PublicSymFlags::Function; 1363327952Sdim } 1364327952Sdim 1365327952Sdim OutputSection *OS = Def->getChunk()->getOutputSection(); 1366327952Sdim assert(OS && "all publics should be in final image"); 1367327952Sdim Pub.Offset = Def->getRVA() - OS->getRVA(); 1368327952Sdim Pub.Segment = OS->SectionIndex; 1369327952Sdim return Pub; 1370327952Sdim} 1371327952Sdim 1372321369Sdim// Add all object files to the PDB. Merge .debug$T sections into IpiData and 1373321369Sdim// TpiData. 1374321369Sdimvoid PDBLinker::addObjectsToPDB() { 1375341825Sdim ScopedTimer T1(AddObjectsTimer); 1376327952Sdim for (ObjFile *File : ObjFile::Instances) 1377327952Sdim addObjFile(File); 1378321369Sdim 1379321369Sdim Builder.getStringTableBuilder().setStrings(PDBStrTab); 1380341825Sdim T1.stop(); 1381321369Sdim 1382327952Sdim // Construct TPI and IPI stream contents. 1383341825Sdim ScopedTimer T2(TpiStreamLayoutTimer); 1384344779Sdim addTypeInfo(Builder.getTpiBuilder(), getTypeTable()); 1385344779Sdim addTypeInfo(Builder.getIpiBuilder(), getIDTable()); 1386341825Sdim T2.stop(); 1387321369Sdim 1388341825Sdim ScopedTimer T3(GlobalsLayoutTimer); 1389327952Sdim // Compute the public and global symbols. 1390327952Sdim auto &GsiBuilder = Builder.getGsiBuilder(); 1391327952Sdim std::vector<PublicSym32> Publics; 1392327952Sdim Symtab->forEachSymbol([&Publics](Symbol *S) { 1393327952Sdim // Only emit defined, live symbols that have a chunk. 1394327952Sdim auto *Def = dyn_cast<Defined>(S); 1395327952Sdim if (Def && Def->isLive() && Def->getChunk()) 1396327952Sdim Publics.push_back(createPublic(Def)); 1397327952Sdim }); 1398321369Sdim 1399327952Sdim if (!Publics.empty()) { 1400327952Sdim // Sort the public symbols and add them to the stream. 1401344779Sdim sort(parallel::par, Publics.begin(), Publics.end(), 1402344779Sdim [](const PublicSym32 &L, const PublicSym32 &R) { 1403344779Sdim return L.Name < R.Name; 1404344779Sdim }); 1405327952Sdim for (const PublicSym32 &Pub : Publics) 1406327952Sdim GsiBuilder.addPublicSymbol(Pub); 1407327952Sdim } 1408314564Sdim} 1409314564Sdim 1410341825Sdimvoid PDBLinker::addNatvisFiles() { 1411341825Sdim for (StringRef File : Config->NatvisFiles) { 1412341825Sdim ErrorOr<std::unique_ptr<MemoryBuffer>> DataOrErr = 1413341825Sdim MemoryBuffer::getFile(File); 1414341825Sdim if (!DataOrErr) { 1415341825Sdim warn("Cannot open input file: " + File); 1416341825Sdim continue; 1417341825Sdim } 1418341825Sdim Builder.addInjectedSource(File, std::move(*DataOrErr)); 1419341825Sdim } 1420341825Sdim} 1421341825Sdim 1422341825Sdimstatic codeview::CPUType toCodeViewMachine(COFF::MachineTypes Machine) { 1423341825Sdim switch (Machine) { 1424341825Sdim case COFF::IMAGE_FILE_MACHINE_AMD64: 1425341825Sdim return codeview::CPUType::X64; 1426341825Sdim case COFF::IMAGE_FILE_MACHINE_ARM: 1427341825Sdim return codeview::CPUType::ARM7; 1428341825Sdim case COFF::IMAGE_FILE_MACHINE_ARM64: 1429341825Sdim return codeview::CPUType::ARM64; 1430341825Sdim case COFF::IMAGE_FILE_MACHINE_ARMNT: 1431341825Sdim return codeview::CPUType::ARMNT; 1432341825Sdim case COFF::IMAGE_FILE_MACHINE_I386: 1433341825Sdim return codeview::CPUType::Intel80386; 1434341825Sdim default: 1435341825Sdim llvm_unreachable("Unsupported CPU Type"); 1436341825Sdim } 1437341825Sdim} 1438341825Sdim 1439344779Sdim// Mimic MSVC which surrounds arguments containing whitespace with quotes. 1440344779Sdim// Double double-quotes are handled, so that the resulting string can be 1441344779Sdim// executed again on the cmd-line. 1442344779Sdimstatic std::string quote(ArrayRef<StringRef> Args) { 1443344779Sdim std::string R; 1444344779Sdim R.reserve(256); 1445344779Sdim for (StringRef A : Args) { 1446344779Sdim if (!R.empty()) 1447344779Sdim R.push_back(' '); 1448344779Sdim bool HasWS = A.find(' ') != StringRef::npos; 1449344779Sdim bool HasQ = A.find('"') != StringRef::npos; 1450344779Sdim if (HasWS || HasQ) 1451344779Sdim R.push_back('"'); 1452344779Sdim if (HasQ) { 1453344779Sdim SmallVector<StringRef, 4> S; 1454344779Sdim A.split(S, '"'); 1455344779Sdim R.append(join(S, "\"\"")); 1456344779Sdim } else { 1457344779Sdim R.append(A); 1458344779Sdim } 1459344779Sdim if (HasWS || HasQ) 1460344779Sdim R.push_back('"'); 1461344779Sdim } 1462344779Sdim return R; 1463344779Sdim} 1464344779Sdim 1465327952Sdimstatic void addCommonLinkerModuleSymbols(StringRef Path, 1466327952Sdim pdb::DbiModuleDescriptorBuilder &Mod, 1467327952Sdim BumpPtrAllocator &Allocator) { 1468327952Sdim ObjNameSym ONS(SymbolRecordKind::ObjNameSym); 1469327952Sdim Compile3Sym CS(SymbolRecordKind::Compile3Sym); 1470327952Sdim EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym); 1471321369Sdim 1472321369Sdim ONS.Name = "* Linker *"; 1473321369Sdim ONS.Signature = 0; 1474321369Sdim 1475341825Sdim CS.Machine = toCodeViewMachine(Config->Machine); 1476327952Sdim // Interestingly, if we set the string to 0.0.0.0, then when trying to view 1477327952Sdim // local variables WinDbg emits an error that private symbols are not present. 1478327952Sdim // By setting this to a valid MSVC linker version string, local variables are 1479327952Sdim // displayed properly. As such, even though it is not representative of 1480327952Sdim // LLVM's version information, we need this for compatibility. 1481321369Sdim CS.Flags = CompileSym3Flags::None; 1482327952Sdim CS.VersionBackendBuild = 25019; 1483327952Sdim CS.VersionBackendMajor = 14; 1484327952Sdim CS.VersionBackendMinor = 10; 1485321369Sdim CS.VersionBackendQFE = 0; 1486327952Sdim 1487327952Sdim // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the 1488327952Sdim // linker module (which is by definition a backend), so we don't need to do 1489327952Sdim // anything here. Also, it seems we can use "LLVM Linker" for the linker name 1490327952Sdim // without any problems. Only the backend version has to be hardcoded to a 1491327952Sdim // magic number. 1492321369Sdim CS.VersionFrontendBuild = 0; 1493321369Sdim CS.VersionFrontendMajor = 0; 1494321369Sdim CS.VersionFrontendMinor = 0; 1495321369Sdim CS.VersionFrontendQFE = 0; 1496321369Sdim CS.Version = "LLVM Linker"; 1497321369Sdim CS.setLanguage(SourceLanguage::Link); 1498321369Sdim 1499321369Sdim ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front(); 1500344779Sdim std::string ArgStr = quote(Args); 1501321369Sdim EBS.Fields.push_back("cwd"); 1502321369Sdim SmallString<64> cwd; 1503344779Sdim if (Config->PDBSourcePath.empty()) 1504344779Sdim sys::fs::current_path(cwd); 1505344779Sdim else 1506344779Sdim cwd = Config->PDBSourcePath; 1507321369Sdim EBS.Fields.push_back(cwd); 1508321369Sdim EBS.Fields.push_back("exe"); 1509327952Sdim SmallString<64> exe = Config->Argv[0]; 1510344779Sdim pdbMakeAbsolute(exe); 1511327952Sdim EBS.Fields.push_back(exe); 1512321369Sdim EBS.Fields.push_back("pdb"); 1513321369Sdim EBS.Fields.push_back(Path); 1514321369Sdim EBS.Fields.push_back("cmd"); 1515321369Sdim EBS.Fields.push_back(ArgStr); 1516321369Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1517321369Sdim ONS, Allocator, CodeViewContainer::Pdb)); 1518321369Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1519321369Sdim CS, Allocator, CodeViewContainer::Pdb)); 1520321369Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1521321369Sdim EBS, Allocator, CodeViewContainer::Pdb)); 1522321369Sdim} 1523321369Sdim 1524327952Sdimstatic void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod, 1525327952Sdim OutputSection &OS, 1526327952Sdim BumpPtrAllocator &Allocator) { 1527327952Sdim SectionSym Sym(SymbolRecordKind::SectionSym); 1528327952Sdim Sym.Alignment = 12; // 2^12 = 4KB 1529341825Sdim Sym.Characteristics = OS.Header.Characteristics; 1530327952Sdim Sym.Length = OS.getVirtualSize(); 1531341825Sdim Sym.Name = OS.Name; 1532327952Sdim Sym.Rva = OS.getRVA(); 1533327952Sdim Sym.SectionNumber = OS.SectionIndex; 1534327952Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1535327952Sdim Sym, Allocator, CodeViewContainer::Pdb)); 1536327952Sdim} 1537327952Sdim 1538314564Sdim// Creates a PDB file. 1539327952Sdimvoid coff::createPDB(SymbolTable *Symtab, 1540327952Sdim ArrayRef<OutputSection *> OutputSections, 1541327952Sdim ArrayRef<uint8_t> SectionTable, 1542344779Sdim llvm::codeview::DebugInfo *BuildId) { 1543341825Sdim ScopedTimer T1(TotalPdbLinkTimer); 1544321369Sdim PDBLinker PDB(Symtab); 1545341825Sdim 1546327952Sdim PDB.initialize(BuildId); 1547321369Sdim PDB.addObjectsToPDB(); 1548327952Sdim PDB.addSections(OutputSections, SectionTable); 1549341825Sdim PDB.addNatvisFiles(); 1550341825Sdim 1551341825Sdim ScopedTimer T2(DiskCommitTimer); 1552344779Sdim codeview::GUID Guid; 1553344779Sdim PDB.commit(&Guid); 1554344779Sdim memcpy(&BuildId->PDB70.Signature, &Guid, 16); 1555321369Sdim} 1556314564Sdim 1557344779Sdimvoid PDBLinker::initialize(llvm::codeview::DebugInfo *BuildId) { 1558314564Sdim ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize 1559314564Sdim 1560344779Sdim BuildId->Signature.CVSignature = OMF::Signature::PDB70; 1561344779Sdim // Signature is set to a hash of the PDB contents when the PDB is done. 1562344779Sdim memset(BuildId->PDB70.Signature, 0, 16); 1563344779Sdim BuildId->PDB70.Age = 1; 1564344779Sdim 1565314564Sdim // Create streams in MSF for predefined streams, namely 1566314564Sdim // PDB, TPI, DBI and IPI. 1567314564Sdim for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) 1568314564Sdim ExitOnErr(Builder.getMsfBuilder().addStream(0)); 1569314564Sdim 1570314564Sdim // Add an Info stream. 1571314564Sdim auto &InfoBuilder = Builder.getInfoBuilder(); 1572314564Sdim InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); 1573344779Sdim InfoBuilder.setHashPDBContentsToGUID(true); 1574314564Sdim 1575321369Sdim // Add an empty DBI stream. 1576321369Sdim pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); 1577344779Sdim DbiBuilder.setAge(BuildId->PDB70.Age); 1578321369Sdim DbiBuilder.setVersionHeader(pdb::PdbDbiV70); 1579341825Sdim DbiBuilder.setMachineType(Config->Machine); 1580341825Sdim // Technically we are not link.exe 14.11, but there are known cases where 1581341825Sdim // debugging tools on Windows expect Microsoft-specific version numbers or 1582341825Sdim // they fail to work at all. Since we know we produce PDBs that are 1583341825Sdim // compatible with LINK 14.11, we set that version number here. 1584341825Sdim DbiBuilder.setBuildNumber(14, 11); 1585321369Sdim} 1586314564Sdim 1587327952Sdimvoid PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections, 1588327952Sdim ArrayRef<uint8_t> SectionTable) { 1589327952Sdim // It's not entirely clear what this is, but the * Linker * module uses it. 1590321369Sdim pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); 1591327952Sdim NativePath = Config->PDBPath; 1592344779Sdim pdbMakeAbsolute(NativePath); 1593327952Sdim uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath); 1594327952Sdim auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); 1595327952Sdim LinkerModule.setPdbFilePathNI(PdbFilePathNI); 1596327952Sdim addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc); 1597314564Sdim 1598327952Sdim // Add section contributions. They must be ordered by ascending RVA. 1599327952Sdim for (OutputSection *OS : OutputSections) { 1600327952Sdim addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc); 1601344779Sdim for (Chunk *C : OS->Chunks) { 1602341825Sdim pdb::SectionContrib SC = 1603341825Sdim createSectionContrib(C, LinkerModule.getModuleIndex()); 1604341825Sdim Builder.getDbiBuilder().addSectionContrib(SC); 1605341825Sdim } 1606327952Sdim } 1607327952Sdim 1608314564Sdim // Add Section Map stream. 1609314564Sdim ArrayRef<object::coff_section> Sections = { 1610314564Sdim (const object::coff_section *)SectionTable.data(), 1611314564Sdim SectionTable.size() / sizeof(object::coff_section)}; 1612321369Sdim SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections); 1613314564Sdim DbiBuilder.setSectionMap(SectionMap); 1614314564Sdim 1615314564Sdim // Add COFF section header stream. 1616314564Sdim ExitOnErr( 1617314564Sdim DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); 1618321369Sdim} 1619314564Sdim 1620344779Sdimvoid PDBLinker::commit(codeview::GUID *Guid) { 1621314564Sdim // Write to a file. 1622344779Sdim ExitOnErr(Builder.commit(Config->PDBPath, Guid)); 1623314564Sdim} 1624341825Sdim 1625341825Sdimstatic Expected<StringRef> 1626341825SdimgetFileName(const DebugStringTableSubsectionRef &Strings, 1627341825Sdim const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) { 1628341825Sdim auto Iter = Checksums.getArray().at(FileID); 1629341825Sdim if (Iter == Checksums.getArray().end()) 1630341825Sdim return make_error<CodeViewError>(cv_error_code::no_records); 1631341825Sdim uint32_t Offset = Iter->FileNameOffset; 1632341825Sdim return Strings.getString(Offset); 1633341825Sdim} 1634341825Sdim 1635341825Sdimstatic uint32_t getSecrelReloc() { 1636341825Sdim switch (Config->Machine) { 1637341825Sdim case AMD64: 1638341825Sdim return COFF::IMAGE_REL_AMD64_SECREL; 1639341825Sdim case I386: 1640341825Sdim return COFF::IMAGE_REL_I386_SECREL; 1641341825Sdim case ARMNT: 1642341825Sdim return COFF::IMAGE_REL_ARM_SECREL; 1643341825Sdim case ARM64: 1644341825Sdim return COFF::IMAGE_REL_ARM64_SECREL; 1645341825Sdim default: 1646341825Sdim llvm_unreachable("unknown machine type"); 1647341825Sdim } 1648341825Sdim} 1649341825Sdim 1650341825Sdim// Try to find a line table for the given offset Addr into the given chunk C. 1651341825Sdim// If a line table was found, the line table, the string and checksum tables 1652341825Sdim// that are used to interpret the line table, and the offset of Addr in the line 1653341825Sdim// table are stored in the output arguments. Returns whether a line table was 1654341825Sdim// found. 1655341825Sdimstatic bool findLineTable(const SectionChunk *C, uint32_t Addr, 1656341825Sdim DebugStringTableSubsectionRef &CVStrTab, 1657341825Sdim DebugChecksumsSubsectionRef &Checksums, 1658341825Sdim DebugLinesSubsectionRef &Lines, 1659341825Sdim uint32_t &OffsetInLinetable) { 1660341825Sdim ExitOnError ExitOnErr; 1661341825Sdim uint32_t SecrelReloc = getSecrelReloc(); 1662341825Sdim 1663341825Sdim for (SectionChunk *DbgC : C->File->getDebugChunks()) { 1664341825Sdim if (DbgC->getSectionName() != ".debug$S") 1665341825Sdim continue; 1666341825Sdim 1667341825Sdim // Build a mapping of SECREL relocations in DbgC that refer to C. 1668341825Sdim DenseMap<uint32_t, uint32_t> Secrels; 1669341825Sdim for (const coff_relocation &R : DbgC->Relocs) { 1670341825Sdim if (R.Type != SecrelReloc) 1671341825Sdim continue; 1672341825Sdim 1673341825Sdim if (auto *S = dyn_cast_or_null<DefinedRegular>( 1674341825Sdim C->File->getSymbols()[R.SymbolTableIndex])) 1675341825Sdim if (S->getChunk() == C) 1676341825Sdim Secrels[R.VirtualAddress] = S->getValue(); 1677341825Sdim } 1678341825Sdim 1679341825Sdim ArrayRef<uint8_t> Contents = 1680341825Sdim consumeDebugMagic(DbgC->getContents(), ".debug$S"); 1681341825Sdim DebugSubsectionArray Subsections; 1682341825Sdim BinaryStreamReader Reader(Contents, support::little); 1683341825Sdim ExitOnErr(Reader.readArray(Subsections, Contents.size())); 1684341825Sdim 1685341825Sdim for (const DebugSubsectionRecord &SS : Subsections) { 1686341825Sdim switch (SS.kind()) { 1687341825Sdim case DebugSubsectionKind::StringTable: { 1688341825Sdim assert(!CVStrTab.valid() && 1689341825Sdim "Encountered multiple string table subsections!"); 1690341825Sdim ExitOnErr(CVStrTab.initialize(SS.getRecordData())); 1691341825Sdim break; 1692341825Sdim } 1693341825Sdim case DebugSubsectionKind::FileChecksums: 1694341825Sdim assert(!Checksums.valid() && 1695341825Sdim "Encountered multiple checksum subsections!"); 1696341825Sdim ExitOnErr(Checksums.initialize(SS.getRecordData())); 1697341825Sdim break; 1698341825Sdim case DebugSubsectionKind::Lines: { 1699341825Sdim ArrayRef<uint8_t> Bytes; 1700341825Sdim auto Ref = SS.getRecordData(); 1701341825Sdim ExitOnErr(Ref.readLongestContiguousChunk(0, Bytes)); 1702341825Sdim size_t OffsetInDbgC = Bytes.data() - DbgC->getContents().data(); 1703341825Sdim 1704341825Sdim // Check whether this line table refers to C. 1705341825Sdim auto I = Secrels.find(OffsetInDbgC); 1706341825Sdim if (I == Secrels.end()) 1707341825Sdim break; 1708341825Sdim 1709341825Sdim // Check whether this line table covers Addr in C. 1710341825Sdim DebugLinesSubsectionRef LinesTmp; 1711341825Sdim ExitOnErr(LinesTmp.initialize(BinaryStreamReader(Ref))); 1712341825Sdim uint32_t OffsetInC = I->second + LinesTmp.header()->RelocOffset; 1713341825Sdim if (Addr < OffsetInC || Addr >= OffsetInC + LinesTmp.header()->CodeSize) 1714341825Sdim break; 1715341825Sdim 1716341825Sdim assert(!Lines.header() && 1717341825Sdim "Encountered multiple line tables for function!"); 1718341825Sdim ExitOnErr(Lines.initialize(BinaryStreamReader(Ref))); 1719341825Sdim OffsetInLinetable = Addr - OffsetInC; 1720341825Sdim break; 1721341825Sdim } 1722341825Sdim default: 1723341825Sdim break; 1724341825Sdim } 1725341825Sdim 1726341825Sdim if (CVStrTab.valid() && Checksums.valid() && Lines.header()) 1727341825Sdim return true; 1728341825Sdim } 1729341825Sdim } 1730341825Sdim 1731341825Sdim return false; 1732341825Sdim} 1733341825Sdim 1734341825Sdim// Use CodeView line tables to resolve a file and line number for the given 1735341825Sdim// offset into the given chunk and return them, or {"", 0} if a line table was 1736341825Sdim// not found. 1737341825Sdimstd::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *C, 1738341825Sdim uint32_t Addr) { 1739341825Sdim ExitOnError ExitOnErr; 1740341825Sdim 1741341825Sdim DebugStringTableSubsectionRef CVStrTab; 1742341825Sdim DebugChecksumsSubsectionRef Checksums; 1743341825Sdim DebugLinesSubsectionRef Lines; 1744341825Sdim uint32_t OffsetInLinetable; 1745341825Sdim 1746341825Sdim if (!findLineTable(C, Addr, CVStrTab, Checksums, Lines, OffsetInLinetable)) 1747341825Sdim return {"", 0}; 1748341825Sdim 1749344779Sdim Optional<uint32_t> NameIndex; 1750344779Sdim Optional<uint32_t> LineNumber; 1751341825Sdim for (LineColumnEntry &Entry : Lines) { 1752341825Sdim for (const LineNumberEntry &LN : Entry.LineNumbers) { 1753344779Sdim LineInfo LI(LN.Flags); 1754341825Sdim if (LN.Offset > OffsetInLinetable) { 1755344779Sdim if (!NameIndex) { 1756344779Sdim NameIndex = Entry.NameIndex; 1757344779Sdim LineNumber = LI.getStartLine(); 1758344779Sdim } 1759341825Sdim StringRef Filename = 1760344779Sdim ExitOnErr(getFileName(CVStrTab, Checksums, *NameIndex)); 1761344779Sdim return {Filename, *LineNumber}; 1762341825Sdim } 1763341825Sdim NameIndex = Entry.NameIndex; 1764341825Sdim LineNumber = LI.getStartLine(); 1765341825Sdim } 1766341825Sdim } 1767344779Sdim if (!NameIndex) 1768344779Sdim return {"", 0}; 1769344779Sdim StringRef Filename = ExitOnErr(getFileName(CVStrTab, Checksums, *NameIndex)); 1770344779Sdim return {Filename, *LineNumber}; 1771341825Sdim} 1772