PDB.cpp revision 341825
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" 19321369Sdim#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" 20327952Sdim#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h" 21321369Sdim#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 22327952Sdim#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" 23327952Sdim#include "llvm/DebugInfo/CodeView/RecordName.h" 24327952Sdim#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 25321369Sdim#include "llvm/DebugInfo/CodeView/SymbolSerializer.h" 26321369Sdim#include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 27314564Sdim#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 28321369Sdim#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" 29314564Sdim#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" 30314564Sdim#include "llvm/DebugInfo/MSF/MSFBuilder.h" 31314564Sdim#include "llvm/DebugInfo/MSF/MSFCommon.h" 32321369Sdim#include "llvm/DebugInfo/PDB/GenericError.h" 33321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" 34321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiStream.h" 35321369Sdim#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" 36327952Sdim#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" 37321369Sdim#include "llvm/DebugInfo/PDB/Native/InfoStream.h" 38321369Sdim#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" 39321369Sdim#include "llvm/DebugInfo/PDB/Native/NativeSession.h" 40321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBFile.h" 41321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" 42321369Sdim#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 43327952Sdim#include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 44321369Sdim#include "llvm/DebugInfo/PDB/Native/TpiStream.h" 45321369Sdim#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" 46321369Sdim#include "llvm/DebugInfo/PDB/PDB.h" 47314564Sdim#include "llvm/Object/COFF.h" 48341825Sdim#include "llvm/Object/CVDebugRecord.h" 49321369Sdim#include "llvm/Support/BinaryByteStream.h" 50292934Sdim#include "llvm/Support/Endian.h" 51341825Sdim#include "llvm/Support/FormatVariadic.h" 52327952Sdim#include "llvm/Support/JamCRC.h" 53321369Sdim#include "llvm/Support/Path.h" 54314564Sdim#include "llvm/Support/ScopedPrinter.h" 55292934Sdim#include <memory> 56292934Sdim 57314564Sdimusing namespace lld; 58314564Sdimusing namespace lld::coff; 59292934Sdimusing namespace llvm; 60314564Sdimusing namespace llvm::codeview; 61292934Sdim 62314564Sdimusing llvm::object::coff_section; 63292934Sdim 64314564Sdimstatic ExitOnError ExitOnErr; 65314564Sdim 66341825Sdimstatic Timer TotalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root()); 67341825Sdim 68341825Sdimstatic Timer AddObjectsTimer("Add Objects", TotalPdbLinkTimer); 69341825Sdimstatic Timer TypeMergingTimer("Type Merging", AddObjectsTimer); 70341825Sdimstatic Timer SymbolMergingTimer("Symbol Merging", AddObjectsTimer); 71341825Sdimstatic Timer GlobalsLayoutTimer("Globals Stream Layout", TotalPdbLinkTimer); 72341825Sdimstatic Timer TpiStreamLayoutTimer("TPI Stream Layout", TotalPdbLinkTimer); 73341825Sdimstatic Timer DiskCommitTimer("Commit to Disk", TotalPdbLinkTimer); 74341825Sdim 75321369Sdimnamespace { 76321369Sdim/// Map from type index and item index in a type server PDB to the 77321369Sdim/// corresponding index in the destination PDB. 78321369Sdimstruct CVIndexMap { 79321369Sdim SmallVector<TypeIndex, 0> TPIMap; 80321369Sdim SmallVector<TypeIndex, 0> IPIMap; 81321369Sdim bool IsTypeServerMap = false; 82321369Sdim}; 83321369Sdim 84321369Sdimclass PDBLinker { 85321369Sdimpublic: 86321369Sdim PDBLinker(SymbolTable *Symtab) 87321369Sdim : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc), 88341825Sdim IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) { 89341825Sdim // This isn't strictly necessary, but link.exe usually puts an empty string 90341825Sdim // as the first "valid" string in the string table, so we do the same in 91341825Sdim // order to maintain as much byte-for-byte compatibility as possible. 92341825Sdim PDBStrTab.insert(""); 93341825Sdim } 94321369Sdim 95321369Sdim /// Emit the basic PDB structure: initial streams, headers, etc. 96327952Sdim void initialize(const llvm::codeview::DebugInfo &BuildId); 97321369Sdim 98341825Sdim /// Add natvis files specified on the command line. 99341825Sdim void addNatvisFiles(); 100341825Sdim 101321369Sdim /// Link CodeView from each object file in the symbol table into the PDB. 102321369Sdim void addObjectsToPDB(); 103321369Sdim 104321369Sdim /// Link CodeView from a single object file into the PDB. 105327952Sdim void addObjFile(ObjFile *File); 106321369Sdim 107321369Sdim /// Produce a mapping from the type and item indices used in the object 108321369Sdim /// file to those in the destination PDB. 109321369Sdim /// 110321369Sdim /// If the object file uses a type server PDB (compiled with /Zi), merge TPI 111321369Sdim /// and IPI from the type server PDB and return a map for it. Each unique type 112321369Sdim /// server PDB is merged at most once, so this may return an existing index 113321369Sdim /// mapping. 114321369Sdim /// 115321369Sdim /// If the object does not use a type server PDB (compiled with /Z7), we merge 116321369Sdim /// all the type and item records from the .debug$S stream and fill in the 117321369Sdim /// caller-provided ObjectIndexMap. 118329410Sdim Expected<const CVIndexMap&> mergeDebugT(ObjFile *File, 119329410Sdim CVIndexMap &ObjectIndexMap); 120321369Sdim 121329410Sdim Expected<const CVIndexMap&> maybeMergeTypeServerPDB(ObjFile *File, 122329410Sdim TypeServer2Record &TS); 123321369Sdim 124321369Sdim /// Add the section map and section contributions to the PDB. 125327952Sdim void addSections(ArrayRef<OutputSection *> OutputSections, 126327952Sdim ArrayRef<uint8_t> SectionTable); 127321369Sdim 128321369Sdim /// Write the PDB to disk. 129321369Sdim void commit(); 130321369Sdim 131321369Sdimprivate: 132321369Sdim BumpPtrAllocator Alloc; 133321369Sdim 134321369Sdim SymbolTable *Symtab; 135321369Sdim 136321369Sdim pdb::PDBFileBuilder Builder; 137321369Sdim 138321369Sdim /// Type records that will go into the PDB TPI stream. 139327952Sdim MergingTypeTableBuilder TypeTable; 140321369Sdim 141321369Sdim /// Item records that will go into the PDB IPI stream. 142327952Sdim MergingTypeTableBuilder IDTable; 143321369Sdim 144327952Sdim /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH) 145327952Sdim GlobalTypeTableBuilder GlobalTypeTable; 146327952Sdim 147327952Sdim /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH) 148327952Sdim GlobalTypeTableBuilder GlobalIDTable; 149327952Sdim 150321369Sdim /// PDBs use a single global string table for filenames in the file checksum 151321369Sdim /// table. 152321369Sdim DebugStringTableSubsection PDBStrTab; 153321369Sdim 154321369Sdim llvm::SmallString<128> NativePath; 155321369Sdim 156341825Sdim /// A list of other PDBs which are loaded during the linking process and which 157341825Sdim /// we need to keep around since the linking operation may reference pointers 158341825Sdim /// inside of these PDBs. 159341825Sdim llvm::SmallVector<std::unique_ptr<pdb::NativeSession>, 2> LoadedPDBs; 160341825Sdim 161321369Sdim std::vector<pdb::SecMapEntry> SectionMap; 162321369Sdim 163321369Sdim /// Type index mappings of type server PDBs that we've loaded so far. 164321369Sdim std::map<GUID, CVIndexMap> TypeServerIndexMappings; 165329410Sdim 166329410Sdim /// List of TypeServer PDBs which cannot be loaded. 167329410Sdim /// Cached to prevent repeated load attempts. 168329410Sdim std::set<GUID> MissingTypeServerPDBs; 169321369Sdim}; 170321369Sdim} 171321369Sdim 172327952Sdimstatic SectionChunk *findByName(ArrayRef<SectionChunk *> Sections, 173314564Sdim StringRef Name) { 174314564Sdim for (SectionChunk *C : Sections) 175314564Sdim if (C->getSectionName() == Name) 176314564Sdim return C; 177314564Sdim return nullptr; 178314564Sdim} 179292934Sdim 180321369Sdimstatic ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data, 181321369Sdim StringRef SecName) { 182314564Sdim // First 4 bytes are section magic. 183314564Sdim if (Data.size() < 4) 184314564Sdim fatal(SecName + " too short"); 185321369Sdim if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) 186314564Sdim fatal(SecName + " has an invalid magic"); 187314564Sdim return Data.slice(4); 188292934Sdim} 189314564Sdim 190327952Sdimstatic ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) { 191321369Sdim if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName)) 192321369Sdim return consumeDebugMagic(Sec->getContents(), SecName); 193321369Sdim return {}; 194321369Sdim} 195314564Sdim 196327952Sdim// A COFF .debug$H section is currently a clang extension. This function checks 197327952Sdim// if a .debug$H section is in a format that we expect / understand, so that we 198327952Sdim// can ignore any sections which are coincidentally also named .debug$H but do 199327952Sdim// not contain a format we recognize. 200327952Sdimstatic bool canUseDebugH(ArrayRef<uint8_t> DebugH) { 201327952Sdim if (DebugH.size() < sizeof(object::debug_h_header)) 202327952Sdim return false; 203327952Sdim auto *Header = 204327952Sdim reinterpret_cast<const object::debug_h_header *>(DebugH.data()); 205327952Sdim DebugH = DebugH.drop_front(sizeof(object::debug_h_header)); 206327952Sdim return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC && 207327952Sdim Header->Version == 0 && 208341825Sdim Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) && 209341825Sdim (DebugH.size() % 8 == 0); 210327952Sdim} 211327952Sdim 212327952Sdimstatic Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) { 213327952Sdim SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H"); 214327952Sdim if (!Sec) 215327952Sdim return llvm::None; 216327952Sdim ArrayRef<uint8_t> Contents = Sec->getContents(); 217327952Sdim if (!canUseDebugH(Contents)) 218327952Sdim return None; 219327952Sdim return Contents; 220327952Sdim} 221327952Sdim 222327952Sdimstatic ArrayRef<GloballyHashedType> 223327952SdimgetHashesFromDebugH(ArrayRef<uint8_t> DebugH) { 224327952Sdim assert(canUseDebugH(DebugH)); 225327952Sdim 226327952Sdim DebugH = DebugH.drop_front(sizeof(object::debug_h_header)); 227327952Sdim uint32_t Count = DebugH.size() / sizeof(GloballyHashedType); 228327952Sdim return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count}; 229327952Sdim} 230327952Sdim 231321369Sdimstatic void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, 232327952Sdim TypeCollection &TypeTable) { 233321369Sdim // Start the TPI or IPI stream header. 234321369Sdim TpiBuilder.setVersionHeader(pdb::PdbTpiV80); 235314564Sdim 236327952Sdim // Flatten the in memory type table and hash each type. 237327952Sdim TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) { 238327952Sdim auto Hash = pdb::hashTypeRecord(Type); 239327952Sdim if (auto E = Hash.takeError()) 240327952Sdim fatal("type hashing error"); 241327952Sdim TpiBuilder.addTypeRecord(Type.RecordData, *Hash); 242314564Sdim }); 243314564Sdim} 244314564Sdim 245321369Sdimstatic Optional<TypeServer2Record> 246321369SdimmaybeReadTypeServerRecord(CVTypeArray &Types) { 247321369Sdim auto I = Types.begin(); 248321369Sdim if (I == Types.end()) 249321369Sdim return None; 250321369Sdim const CVType &Type = *I; 251321369Sdim if (Type.kind() != LF_TYPESERVER2) 252321369Sdim return None; 253321369Sdim TypeServer2Record TS; 254321369Sdim if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS)) 255327952Sdim fatal("error reading type server record: " + toString(std::move(EC))); 256321369Sdim return std::move(TS); 257321369Sdim} 258321369Sdim 259329410SdimExpected<const CVIndexMap&> PDBLinker::mergeDebugT(ObjFile *File, 260329410Sdim CVIndexMap &ObjectIndexMap) { 261341825Sdim ScopedTimer T(TypeMergingTimer); 262341825Sdim 263314564Sdim ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); 264314564Sdim if (Data.empty()) 265321369Sdim return ObjectIndexMap; 266314564Sdim 267321369Sdim BinaryByteStream Stream(Data, support::little); 268321369Sdim CVTypeArray Types; 269321369Sdim BinaryStreamReader Reader(Stream); 270321369Sdim if (auto EC = Reader.readArray(Types, Reader.getLength())) 271327952Sdim fatal("Reader::readArray failed: " + toString(std::move(EC))); 272321369Sdim 273321369Sdim // Look through type servers. If we've already seen this type server, don't 274321369Sdim // merge any type information. 275321369Sdim if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types)) 276321369Sdim return maybeMergeTypeServerPDB(File, *TS); 277321369Sdim 278321369Sdim // This is a /Z7 object. Fill in the temporary, caller-provided 279321369Sdim // ObjectIndexMap. 280327952Sdim if (Config->DebugGHashes) { 281327952Sdim ArrayRef<GloballyHashedType> Hashes; 282327952Sdim std::vector<GloballyHashedType> OwnedHashes; 283327952Sdim if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File)) 284327952Sdim Hashes = getHashesFromDebugH(*DebugH); 285327952Sdim else { 286327952Sdim OwnedHashes = GloballyHashedType::hashTypes(Types); 287327952Sdim Hashes = OwnedHashes; 288327952Sdim } 289327952Sdim 290327952Sdim if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable, 291327952Sdim ObjectIndexMap.TPIMap, Types, Hashes)) 292327952Sdim fatal("codeview::mergeTypeAndIdRecords failed: " + 293327952Sdim toString(std::move(Err))); 294327952Sdim } else { 295327952Sdim if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable, 296327952Sdim ObjectIndexMap.TPIMap, Types)) 297327952Sdim fatal("codeview::mergeTypeAndIdRecords failed: " + 298327952Sdim toString(std::move(Err))); 299327952Sdim } 300321369Sdim return ObjectIndexMap; 301314564Sdim} 302314564Sdim 303321369Sdimstatic Expected<std::unique_ptr<pdb::NativeSession>> 304321369SdimtryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) { 305327952Sdim ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile( 306327952Sdim TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false); 307327952Sdim if (!MBOrErr) 308327952Sdim return errorCodeToError(MBOrErr.getError()); 309327952Sdim 310321369Sdim std::unique_ptr<pdb::IPDBSession> ThisSession; 311327952Sdim if (auto EC = pdb::NativeSession::createFromPdb( 312327952Sdim MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)), 313327952Sdim /*RequiresNullTerminator=*/false), 314327952Sdim ThisSession)) 315321369Sdim return std::move(EC); 316321369Sdim 317321369Sdim std::unique_ptr<pdb::NativeSession> NS( 318321369Sdim static_cast<pdb::NativeSession *>(ThisSession.release())); 319321369Sdim pdb::PDBFile &File = NS->getPDBFile(); 320321369Sdim auto ExpectedInfo = File.getPDBInfoStream(); 321321369Sdim // All PDB Files should have an Info stream. 322321369Sdim if (!ExpectedInfo) 323321369Sdim return ExpectedInfo.takeError(); 324321369Sdim 325321369Sdim // Just because a file with a matching name was found and it was an actual 326321369Sdim // PDB file doesn't mean it matches. For it to match the InfoStream's GUID 327321369Sdim // must match the GUID specified in the TypeServer2 record. 328321369Sdim if (ExpectedInfo->getGuid() != GuidFromObj) 329321369Sdim return make_error<pdb::GenericError>( 330321369Sdim pdb::generic_error_code::type_server_not_found, TSPath); 331321369Sdim 332321369Sdim return std::move(NS); 333321369Sdim} 334321369Sdim 335329410SdimExpected<const CVIndexMap&> PDBLinker::maybeMergeTypeServerPDB(ObjFile *File, 336329410Sdim TypeServer2Record &TS) { 337329410Sdim const GUID& TSId = TS.getGuid(); 338329410Sdim StringRef TSPath = TS.getName(); 339329410Sdim 340329410Sdim // First, check if the PDB has previously failed to load. 341329410Sdim if (MissingTypeServerPDBs.count(TSId)) 342329410Sdim return make_error<pdb::GenericError>( 343329410Sdim pdb::generic_error_code::type_server_not_found, TSPath); 344329410Sdim 345329410Sdim // Second, check if we already loaded a PDB with this GUID. Return the type 346321369Sdim // index mapping if we have it. 347329410Sdim auto Insertion = TypeServerIndexMappings.insert({TSId, CVIndexMap()}); 348321369Sdim CVIndexMap &IndexMap = Insertion.first->second; 349321369Sdim if (!Insertion.second) 350321369Sdim return IndexMap; 351321369Sdim 352321369Sdim // Mark this map as a type server map. 353321369Sdim IndexMap.IsTypeServerMap = true; 354321369Sdim 355321369Sdim // Check for a PDB at: 356321369Sdim // 1. The given file path 357321369Sdim // 2. Next to the object file or archive file 358329410Sdim auto ExpectedSession = tryToLoadPDB(TSId, TSPath); 359321369Sdim if (!ExpectedSession) { 360321369Sdim consumeError(ExpectedSession.takeError()); 361321369Sdim StringRef LocalPath = 362321369Sdim !File->ParentName.empty() ? File->ParentName : File->getName(); 363321369Sdim SmallString<128> Path = sys::path::parent_path(LocalPath); 364321369Sdim sys::path::append( 365329410Sdim Path, sys::path::filename(TSPath, sys::path::Style::windows)); 366329410Sdim ExpectedSession = tryToLoadPDB(TSId, Path); 367321369Sdim } 368329410Sdim if (auto E = ExpectedSession.takeError()) { 369329410Sdim TypeServerIndexMappings.erase(TSId); 370329410Sdim MissingTypeServerPDBs.emplace(TSId); 371329410Sdim return std::move(E); 372329410Sdim } 373321369Sdim 374341825Sdim pdb::NativeSession *Session = ExpectedSession->get(); 375341825Sdim 376341825Sdim // Keep a strong reference to this PDB, so that it's safe to hold pointers 377341825Sdim // into the file. 378341825Sdim LoadedPDBs.push_back(std::move(*ExpectedSession)); 379341825Sdim 380341825Sdim auto ExpectedTpi = Session->getPDBFile().getPDBTpiStream(); 381321369Sdim if (auto E = ExpectedTpi.takeError()) 382327952Sdim fatal("Type server does not have TPI stream: " + toString(std::move(E))); 383341825Sdim auto ExpectedIpi = Session->getPDBFile().getPDBIpiStream(); 384321369Sdim if (auto E = ExpectedIpi.takeError()) 385327952Sdim fatal("Type server does not have TPI stream: " + toString(std::move(E))); 386321369Sdim 387327952Sdim if (Config->DebugGHashes) { 388327952Sdim // PDBs do not actually store global hashes, so when merging a type server 389327952Sdim // PDB we have to synthesize global hashes. To do this, we first synthesize 390327952Sdim // global hashes for the TPI stream, since it is independent, then we 391327952Sdim // synthesize hashes for the IPI stream, using the hashes for the TPI stream 392327952Sdim // as inputs. 393327952Sdim auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray()); 394327952Sdim auto IpiHashes = 395327952Sdim GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes); 396327952Sdim 397327952Sdim // Merge TPI first, because the IPI stream will reference type indices. 398327952Sdim if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap, 399327952Sdim ExpectedTpi->typeArray(), TpiHashes)) 400327952Sdim fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err))); 401327952Sdim 402327952Sdim // Merge IPI. 403327952Sdim if (auto Err = 404327952Sdim mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap, 405327952Sdim ExpectedIpi->typeArray(), IpiHashes)) 406327952Sdim fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err))); 407327952Sdim } else { 408327952Sdim // Merge TPI first, because the IPI stream will reference type indices. 409327952Sdim if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap, 410327952Sdim ExpectedTpi->typeArray())) 411327952Sdim fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err))); 412327952Sdim 413327952Sdim // Merge IPI. 414327952Sdim if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap, 415327952Sdim ExpectedIpi->typeArray())) 416327952Sdim fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err))); 417327952Sdim } 418327952Sdim 419321369Sdim return IndexMap; 420321369Sdim} 421321369Sdim 422321369Sdimstatic bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) { 423321369Sdim if (TI.isSimple()) 424321369Sdim return true; 425321369Sdim if (TI.toArrayIndex() >= TypeIndexMap.size()) 426321369Sdim return false; 427321369Sdim TI = TypeIndexMap[TI.toArrayIndex()]; 428321369Sdim return true; 429321369Sdim} 430321369Sdim 431327952Sdimstatic void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind, 432321369Sdim MutableArrayRef<uint8_t> Contents, 433321369Sdim const CVIndexMap &IndexMap, 434321369Sdim ArrayRef<TiReference> TypeRefs) { 435321369Sdim for (const TiReference &Ref : TypeRefs) { 436321369Sdim unsigned ByteSize = Ref.Count * sizeof(TypeIndex); 437321369Sdim if (Contents.size() < Ref.Offset + ByteSize) 438321369Sdim fatal("symbol record too short"); 439321369Sdim 440321369Sdim // This can be an item index or a type index. Choose the appropriate map. 441321369Sdim ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap; 442327952Sdim bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef; 443327952Sdim if (IsItemIndex && IndexMap.IsTypeServerMap) 444321369Sdim TypeOrItemMap = IndexMap.IPIMap; 445321369Sdim 446321369Sdim MutableArrayRef<TypeIndex> TIs( 447321369Sdim reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count); 448321369Sdim for (TypeIndex &TI : TIs) { 449321369Sdim if (!remapTypeIndex(TI, TypeOrItemMap)) { 450327952Sdim log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " + 451327952Sdim File->getName() + " with bad " + (IsItemIndex ? "item" : "type") + 452327952Sdim " index 0x" + utohexstr(TI.getIndex())); 453321369Sdim TI = TypeIndex(SimpleTypeKind::NotTranslated); 454321369Sdim continue; 455321369Sdim } 456321369Sdim } 457321369Sdim } 458321369Sdim} 459321369Sdim 460341825Sdimstatic void 461341825SdimrecordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents, 462341825Sdim uint32_t Offset, 463341825Sdim std::vector<ulittle32_t *> &StrTableRefs) { 464341825Sdim Contents = 465341825Sdim Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t)); 466341825Sdim ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data()); 467341825Sdim StrTableRefs.push_back(Index); 468341825Sdim} 469341825Sdim 470341825Sdimstatic void 471341825SdimrecordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents, 472341825Sdim std::vector<ulittle32_t *> &StrTableRefs) { 473341825Sdim // For now we only handle S_FILESTATIC, but we may need the same logic for 474341825Sdim // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any 475341825Sdim // PDBs that contain these types of records, so because of the uncertainty 476341825Sdim // they are omitted here until we can prove that it's necessary. 477341825Sdim switch (Kind) { 478341825Sdim case SymbolKind::S_FILESTATIC: 479341825Sdim // FileStaticSym::ModFileOffset 480341825Sdim recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs); 481341825Sdim break; 482341825Sdim case SymbolKind::S_DEFRANGE: 483341825Sdim case SymbolKind::S_DEFRANGE_SUBFIELD: 484341825Sdim log("Not fixing up string table reference in S_DEFRANGE / " 485341825Sdim "S_DEFRANGE_SUBFIELD record"); 486341825Sdim break; 487341825Sdim default: 488341825Sdim break; 489341825Sdim } 490341825Sdim} 491341825Sdim 492327952Sdimstatic SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) { 493327952Sdim const RecordPrefix *Prefix = 494327952Sdim reinterpret_cast<const RecordPrefix *>(RecordData.data()); 495327952Sdim return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind)); 496321369Sdim} 497321369Sdim 498327952Sdim/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32 499327952Sdimstatic void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData, 500327952Sdim TypeCollection &IDTable) { 501327952Sdim RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data()); 502327952Sdim 503327952Sdim SymbolKind Kind = symbolKind(RecordData); 504327952Sdim 505327952Sdim if (Kind == SymbolKind::S_PROC_ID_END) { 506327952Sdim Prefix->RecordKind = SymbolKind::S_END; 507327952Sdim return; 508327952Sdim } 509327952Sdim 510327952Sdim // In an object file, GPROC32_ID has an embedded reference which refers to the 511327952Sdim // single object file type index namespace. This has already been translated 512327952Sdim // to the PDB file's ID stream index space, but we need to convert this to a 513327952Sdim // symbol that refers to the type stream index space. So we remap again from 514327952Sdim // ID index space to type index space. 515327952Sdim if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) { 516327952Sdim SmallVector<TiReference, 1> Refs; 517327952Sdim auto Content = RecordData.drop_front(sizeof(RecordPrefix)); 518327952Sdim CVSymbol Sym(Kind, RecordData); 519327952Sdim discoverTypeIndicesInSymbol(Sym, Refs); 520327952Sdim assert(Refs.size() == 1); 521327952Sdim assert(Refs.front().Count == 1); 522327952Sdim 523327952Sdim TypeIndex *TI = 524327952Sdim reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset); 525327952Sdim // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in 526327952Sdim // the IPI stream, whose `FunctionType` member refers to the TPI stream. 527327952Sdim // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and 528327952Sdim // in both cases we just need the second type index. 529327952Sdim if (!TI->isSimple() && !TI->isNoneType()) { 530327952Sdim CVType FuncIdData = IDTable.getType(*TI); 531327952Sdim SmallVector<TypeIndex, 2> Indices; 532327952Sdim discoverTypeIndices(FuncIdData, Indices); 533327952Sdim assert(Indices.size() == 2); 534327952Sdim *TI = Indices[1]; 535327952Sdim } 536327952Sdim 537327952Sdim Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32 538327952Sdim : SymbolKind::S_LPROC32; 539327952Sdim Prefix->RecordKind = uint16_t(Kind); 540327952Sdim } 541327952Sdim} 542327952Sdim 543321369Sdim/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. 544321369Sdim/// The object file may not be aligned. 545321369Sdimstatic MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym, 546321369Sdim BumpPtrAllocator &Alloc) { 547321369Sdim size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb)); 548321369Sdim assert(Size >= 4 && "record too short"); 549321369Sdim assert(Size <= MaxRecordLength && "record too long"); 550321369Sdim void *Mem = Alloc.Allocate(Size, 4); 551321369Sdim 552321369Sdim // Copy the symbol record and zero out any padding bytes. 553321369Sdim MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size); 554321369Sdim memcpy(NewData.data(), Sym.data().data(), Sym.length()); 555321369Sdim memset(NewData.data() + Sym.length(), 0, Size - Sym.length()); 556321369Sdim 557321369Sdim // Update the record prefix length. It should point to the beginning of the 558327952Sdim // next record. 559321369Sdim auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem); 560321369Sdim Prefix->RecordLen = Size - 2; 561321369Sdim return NewData; 562321369Sdim} 563321369Sdim 564321369Sdim/// Return true if this symbol opens a scope. This implies that the symbol has 565321369Sdim/// "parent" and "end" fields, which contain the offset of the S_END or 566321369Sdim/// S_INLINESITE_END record. 567321369Sdimstatic bool symbolOpensScope(SymbolKind Kind) { 568321369Sdim switch (Kind) { 569321369Sdim case SymbolKind::S_GPROC32: 570321369Sdim case SymbolKind::S_LPROC32: 571321369Sdim case SymbolKind::S_LPROC32_ID: 572321369Sdim case SymbolKind::S_GPROC32_ID: 573321369Sdim case SymbolKind::S_BLOCK32: 574321369Sdim case SymbolKind::S_SEPCODE: 575321369Sdim case SymbolKind::S_THUNK32: 576321369Sdim case SymbolKind::S_INLINESITE: 577321369Sdim case SymbolKind::S_INLINESITE2: 578321369Sdim return true; 579321369Sdim default: 580321369Sdim break; 581321369Sdim } 582321369Sdim return false; 583321369Sdim} 584321369Sdim 585321369Sdimstatic bool symbolEndsScope(SymbolKind Kind) { 586321369Sdim switch (Kind) { 587321369Sdim case SymbolKind::S_END: 588321369Sdim case SymbolKind::S_PROC_ID_END: 589321369Sdim case SymbolKind::S_INLINESITE_END: 590321369Sdim return true; 591321369Sdim default: 592321369Sdim break; 593321369Sdim } 594321369Sdim return false; 595321369Sdim} 596321369Sdim 597321369Sdimstruct ScopeRecord { 598321369Sdim ulittle32_t PtrParent; 599321369Sdim ulittle32_t PtrEnd; 600321369Sdim}; 601321369Sdim 602321369Sdimstruct SymbolScope { 603321369Sdim ScopeRecord *OpeningRecord; 604321369Sdim uint32_t ScopeOffset; 605321369Sdim}; 606321369Sdim 607321369Sdimstatic void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack, 608321369Sdim uint32_t CurOffset, CVSymbol &Sym) { 609321369Sdim assert(symbolOpensScope(Sym.kind())); 610321369Sdim SymbolScope S; 611321369Sdim S.ScopeOffset = CurOffset; 612321369Sdim S.OpeningRecord = const_cast<ScopeRecord *>( 613321369Sdim reinterpret_cast<const ScopeRecord *>(Sym.content().data())); 614321369Sdim S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset; 615321369Sdim Stack.push_back(S); 616321369Sdim} 617321369Sdim 618321369Sdimstatic void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack, 619327952Sdim uint32_t CurOffset, ObjFile *File) { 620321369Sdim if (Stack.empty()) { 621321369Sdim warn("symbol scopes are not balanced in " + File->getName()); 622314564Sdim return; 623321369Sdim } 624321369Sdim SymbolScope S = Stack.pop_back_val(); 625321369Sdim S.OpeningRecord->PtrEnd = CurOffset; 626321369Sdim} 627314564Sdim 628327952Sdimstatic bool symbolGoesInModuleStream(const CVSymbol &Sym) { 629327952Sdim switch (Sym.kind()) { 630327952Sdim case SymbolKind::S_GDATA32: 631327952Sdim case SymbolKind::S_CONSTANT: 632327952Sdim case SymbolKind::S_UDT: 633327952Sdim // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 634327952Sdim // since they are synthesized by the linker in response to S_GPROC32 and 635327952Sdim // S_LPROC32, but if we do see them, don't put them in the module stream I 636327952Sdim // guess. 637327952Sdim case SymbolKind::S_PROCREF: 638327952Sdim case SymbolKind::S_LPROCREF: 639327952Sdim return false; 640327952Sdim // S_GDATA32 does not go in the module stream, but S_LDATA32 does. 641327952Sdim case SymbolKind::S_LDATA32: 642327952Sdim default: 643327952Sdim return true; 644327952Sdim } 645327952Sdim} 646327952Sdim 647327952Sdimstatic bool symbolGoesInGlobalsStream(const CVSymbol &Sym) { 648327952Sdim switch (Sym.kind()) { 649327952Sdim case SymbolKind::S_CONSTANT: 650327952Sdim case SymbolKind::S_GDATA32: 651327952Sdim // S_LDATA32 goes in both the module stream and the globals stream. 652327952Sdim case SymbolKind::S_LDATA32: 653327952Sdim case SymbolKind::S_GPROC32: 654327952Sdim case SymbolKind::S_LPROC32: 655327952Sdim // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 656327952Sdim // since they are synthesized by the linker in response to S_GPROC32 and 657327952Sdim // S_LPROC32, but if we do see them, copy them straight through. 658327952Sdim case SymbolKind::S_PROCREF: 659327952Sdim case SymbolKind::S_LPROCREF: 660327952Sdim return true; 661327952Sdim // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the 662327952Sdim // globals stream or the modules stream). These have special handling which 663327952Sdim // needs more investigation before we can get right, but by putting them all 664327952Sdim // into the globals stream WinDbg fails to display local variables of class 665327952Sdim // types saying that it cannot find the type Foo *. So as a stopgap just to 666327952Sdim // keep things working, we drop them. 667327952Sdim case SymbolKind::S_UDT: 668327952Sdim default: 669327952Sdim return false; 670327952Sdim } 671327952Sdim} 672327952Sdim 673327952Sdimstatic void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File, 674327952Sdim const CVSymbol &Sym) { 675327952Sdim switch (Sym.kind()) { 676327952Sdim case SymbolKind::S_CONSTANT: 677327952Sdim case SymbolKind::S_UDT: 678327952Sdim case SymbolKind::S_GDATA32: 679327952Sdim case SymbolKind::S_LDATA32: 680327952Sdim case SymbolKind::S_PROCREF: 681327952Sdim case SymbolKind::S_LPROCREF: 682327952Sdim Builder.addGlobalSymbol(Sym); 683327952Sdim break; 684327952Sdim case SymbolKind::S_GPROC32: 685327952Sdim case SymbolKind::S_LPROC32: { 686327952Sdim SymbolRecordKind K = SymbolRecordKind::ProcRefSym; 687327952Sdim if (Sym.kind() == SymbolKind::S_LPROC32) 688327952Sdim K = SymbolRecordKind::LocalProcRef; 689327952Sdim ProcRefSym PS(K); 690327952Sdim PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex()); 691327952Sdim // For some reason, MSVC seems to add one to this value. 692327952Sdim ++PS.Module; 693327952Sdim PS.Name = getSymbolName(Sym); 694327952Sdim PS.SumName = 0; 695327952Sdim PS.SymOffset = File.ModuleDBI->getNextSymbolOffset(); 696327952Sdim Builder.addGlobalSymbol(PS); 697327952Sdim break; 698327952Sdim } 699327952Sdim default: 700327952Sdim llvm_unreachable("Invalid symbol kind!"); 701327952Sdim } 702327952Sdim} 703327952Sdim 704327952Sdimstatic void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File, 705327952Sdim pdb::GSIStreamBuilder &GsiBuilder, 706321369Sdim const CVIndexMap &IndexMap, 707327952Sdim TypeCollection &IDTable, 708341825Sdim std::vector<ulittle32_t *> &StringTableRefs, 709321369Sdim BinaryStreamRef SymData) { 710321369Sdim // FIXME: Improve error recovery by warning and skipping records when 711321369Sdim // possible. 712341825Sdim ArrayRef<uint8_t> SymsBuffer; 713341825Sdim cantFail(SymData.readBytes(0, SymData.getLength(), SymsBuffer)); 714321369Sdim SmallVector<SymbolScope, 4> Scopes; 715314564Sdim 716341825Sdim auto EC = forEachCodeViewRecord<CVSymbol>( 717341825Sdim SymsBuffer, [&](const CVSymbol &Sym) -> llvm::Error { 718341825Sdim // Discover type index references in the record. Skip it if we don't 719341825Sdim // know where they are. 720341825Sdim SmallVector<TiReference, 32> TypeRefs; 721341825Sdim if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) { 722341825Sdim log("ignoring unknown symbol record with kind 0x" + 723341825Sdim utohexstr(Sym.kind())); 724341825Sdim return Error::success(); 725341825Sdim } 726321369Sdim 727341825Sdim // Copy the symbol record so we can mutate it. 728341825Sdim MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc); 729321369Sdim 730341825Sdim // Re-map all the type index references. 731341825Sdim MutableArrayRef<uint8_t> Contents = 732341825Sdim NewData.drop_front(sizeof(RecordPrefix)); 733341825Sdim remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap, 734341825Sdim TypeRefs); 735327952Sdim 736341825Sdim // An object file may have S_xxx_ID symbols, but these get converted to 737341825Sdim // "real" symbols in a PDB. 738341825Sdim translateIdSymbols(NewData, IDTable); 739327952Sdim 740341825Sdim // If this record refers to an offset in the object file's string table, 741341825Sdim // add that item to the global PDB string table and re-write the index. 742341825Sdim recordStringTableReferences(Sym.kind(), Contents, StringTableRefs); 743321369Sdim 744341825Sdim SymbolKind NewKind = symbolKind(NewData); 745327952Sdim 746341825Sdim // Fill in "Parent" and "End" fields by maintaining a stack of scopes. 747341825Sdim CVSymbol NewSym(NewKind, NewData); 748341825Sdim if (symbolOpensScope(NewKind)) 749341825Sdim scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), 750341825Sdim NewSym); 751341825Sdim else if (symbolEndsScope(NewKind)) 752341825Sdim scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File); 753341825Sdim 754341825Sdim // Add the symbol to the globals stream if necessary. Do this before 755341825Sdim // adding the symbol to the module since we may need to get the next 756341825Sdim // symbol offset, and writing to the module's symbol stream will update 757341825Sdim // that offset. 758341825Sdim if (symbolGoesInGlobalsStream(NewSym)) 759341825Sdim addGlobalSymbol(GsiBuilder, *File, NewSym); 760341825Sdim 761341825Sdim // Add the symbol to the module. 762341825Sdim if (symbolGoesInModuleStream(NewSym)) 763341825Sdim File->ModuleDBI->addSymbol(NewSym); 764341825Sdim return Error::success(); 765341825Sdim }); 766341825Sdim cantFail(std::move(EC)); 767314564Sdim} 768314564Sdim 769321369Sdim// Allocate memory for a .debug$S section and relocate it. 770321369Sdimstatic ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, 771321369Sdim SectionChunk *DebugChunk) { 772321369Sdim uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize()); 773321369Sdim assert(DebugChunk->OutputSectionOff == 0 && 774321369Sdim "debug sections should not be in output sections"); 775321369Sdim DebugChunk->writeTo(Buffer); 776321369Sdim return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()), 777321369Sdim ".debug$S"); 778321369Sdim} 779314564Sdim 780341825Sdimstatic pdb::SectionContrib createSectionContrib(const Chunk *C, uint32_t Modi) { 781341825Sdim OutputSection *OS = C->getOutputSection(); 782341825Sdim pdb::SectionContrib SC; 783341825Sdim memset(&SC, 0, sizeof(SC)); 784341825Sdim SC.ISect = OS->SectionIndex; 785341825Sdim SC.Off = C->getRVA() - OS->getRVA(); 786341825Sdim SC.Size = C->getSize(); 787341825Sdim if (auto *SecChunk = dyn_cast<SectionChunk>(C)) { 788341825Sdim SC.Characteristics = SecChunk->Header->Characteristics; 789341825Sdim SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex(); 790341825Sdim ArrayRef<uint8_t> Contents = SecChunk->getContents(); 791341825Sdim JamCRC CRC(0); 792341825Sdim ArrayRef<char> CharContents = makeArrayRef( 793341825Sdim reinterpret_cast<const char *>(Contents.data()), Contents.size()); 794341825Sdim CRC.update(CharContents); 795341825Sdim SC.DataCrc = CRC.getCRC(); 796341825Sdim } else { 797341825Sdim SC.Characteristics = OS->Header.Characteristics; 798341825Sdim // FIXME: When we start creating DBI for import libraries, use those here. 799341825Sdim SC.Imod = Modi; 800341825Sdim } 801341825Sdim SC.RelocCrc = 0; // FIXME 802341825Sdim 803341825Sdim return SC; 804341825Sdim} 805341825Sdim 806327952Sdimvoid PDBLinker::addObjFile(ObjFile *File) { 807321369Sdim // Add a module descriptor for every object file. We need to put an absolute 808321369Sdim // path to the object into the PDB. If this is a plain object, we make its 809321369Sdim // path absolute. If it's an object in an archive, we make the archive path 810321369Sdim // absolute. 811321369Sdim bool InArchive = !File->ParentName.empty(); 812321369Sdim SmallString<128> Path = InArchive ? File->ParentName : File->getName(); 813321369Sdim sys::fs::make_absolute(Path); 814321369Sdim sys::path::native(Path, sys::path::Style::windows); 815321369Sdim StringRef Name = InArchive ? File->getName() : StringRef(Path); 816321369Sdim 817321369Sdim File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name)); 818321369Sdim File->ModuleDBI->setObjFileName(Path); 819321369Sdim 820341825Sdim auto Chunks = File->getChunks(); 821341825Sdim uint32_t Modi = File->ModuleDBI->getModuleIndex(); 822341825Sdim for (Chunk *C : Chunks) { 823341825Sdim auto *SecChunk = dyn_cast<SectionChunk>(C); 824341825Sdim if (!SecChunk || !SecChunk->isLive()) 825341825Sdim continue; 826341825Sdim pdb::SectionContrib SC = createSectionContrib(SecChunk, Modi); 827341825Sdim File->ModuleDBI->setFirstSectionContrib(SC); 828341825Sdim break; 829341825Sdim } 830341825Sdim 831321369Sdim // Before we can process symbol substreams from .debug$S, we need to process 832321369Sdim // type information, file checksums, and the string table. Add type info to 833321369Sdim // the PDB first, so that we can get the map from object file type and item 834321369Sdim // indices to PDB type and item indices. 835321369Sdim CVIndexMap ObjectIndexMap; 836329410Sdim auto IndexMapResult = mergeDebugT(File, ObjectIndexMap); 837321369Sdim 838329410Sdim // If the .debug$T sections fail to merge, assume there is no debug info. 839329410Sdim if (!IndexMapResult) { 840329410Sdim warn("Type server PDB for " + Name + " is invalid, ignoring debug info. " + 841329410Sdim toString(IndexMapResult.takeError())); 842329410Sdim return; 843329410Sdim } 844329410Sdim 845329410Sdim const CVIndexMap &IndexMap = *IndexMapResult; 846329410Sdim 847341825Sdim ScopedTimer T(SymbolMergingTimer); 848341825Sdim 849321369Sdim // Now do all live .debug$S sections. 850341825Sdim DebugStringTableSubsectionRef CVStrTab; 851341825Sdim DebugChecksumsSubsectionRef Checksums; 852341825Sdim std::vector<ulittle32_t *> StringTableReferences; 853321369Sdim for (SectionChunk *DebugChunk : File->getDebugChunks()) { 854321369Sdim if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S") 855321369Sdim continue; 856321369Sdim 857321369Sdim ArrayRef<uint8_t> RelocatedDebugContents = 858321369Sdim relocateDebugChunk(Alloc, DebugChunk); 859321369Sdim if (RelocatedDebugContents.empty()) 860321369Sdim continue; 861321369Sdim 862321369Sdim DebugSubsectionArray Subsections; 863321369Sdim BinaryStreamReader Reader(RelocatedDebugContents, support::little); 864321369Sdim ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size())); 865321369Sdim 866321369Sdim for (const DebugSubsectionRecord &SS : Subsections) { 867321369Sdim switch (SS.kind()) { 868341825Sdim case DebugSubsectionKind::StringTable: { 869341825Sdim assert(!CVStrTab.valid() && 870341825Sdim "Encountered multiple string table subsections!"); 871321369Sdim ExitOnErr(CVStrTab.initialize(SS.getRecordData())); 872321369Sdim break; 873341825Sdim } 874321369Sdim case DebugSubsectionKind::FileChecksums: 875341825Sdim assert(!Checksums.valid() && 876341825Sdim "Encountered multiple checksum subsections!"); 877321369Sdim ExitOnErr(Checksums.initialize(SS.getRecordData())); 878321369Sdim break; 879321369Sdim case DebugSubsectionKind::Lines: 880321369Sdim // We can add the relocated line table directly to the PDB without 881321369Sdim // modification because the file checksum offsets will stay the same. 882321369Sdim File->ModuleDBI->addDebugSubsection(SS); 883321369Sdim break; 884321369Sdim case DebugSubsectionKind::Symbols: 885327952Sdim if (Config->DebugGHashes) { 886327952Sdim mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap, 887341825Sdim GlobalIDTable, StringTableReferences, 888341825Sdim SS.getRecordData()); 889327952Sdim } else { 890327952Sdim mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap, 891341825Sdim IDTable, StringTableReferences, 892341825Sdim SS.getRecordData()); 893327952Sdim } 894321369Sdim break; 895321369Sdim default: 896321369Sdim // FIXME: Process the rest of the subsections. 897321369Sdim break; 898321369Sdim } 899321369Sdim } 900341825Sdim } 901321369Sdim 902341825Sdim // We should have seen all debug subsections across the entire object file now 903341825Sdim // which means that if a StringTable subsection and Checksums subsection were 904341825Sdim // present, now is the time to handle them. 905341825Sdim if (!CVStrTab.valid()) { 906341825Sdim if (Checksums.valid()) 907341825Sdim fatal(".debug$S sections with a checksums subsection must also contain a " 908341825Sdim "string table subsection"); 909341825Sdim 910341825Sdim if (!StringTableReferences.empty()) 911341825Sdim warn("No StringTable subsection was encountered, but there are string " 912341825Sdim "table references"); 913341825Sdim return; 914341825Sdim } 915341825Sdim 916341825Sdim // Rewrite each string table reference based on the value that the string 917341825Sdim // assumes in the final PDB. 918341825Sdim for (ulittle32_t *Ref : StringTableReferences) { 919341825Sdim auto ExpectedString = CVStrTab.getString(*Ref); 920341825Sdim if (!ExpectedString) { 921341825Sdim warn("Invalid string table reference"); 922341825Sdim consumeError(ExpectedString.takeError()); 923341825Sdim continue; 924321369Sdim } 925341825Sdim 926341825Sdim *Ref = PDBStrTab.insert(*ExpectedString); 927314564Sdim } 928341825Sdim 929341825Sdim // Make a new file checksum table that refers to offsets in the PDB-wide 930341825Sdim // string table. Generally the string table subsection appears after the 931341825Sdim // checksum table, so we have to do this after looping over all the 932341825Sdim // subsections. 933341825Sdim auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab); 934341825Sdim for (FileChecksumEntry &FC : Checksums) { 935341825Sdim SmallString<128> FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset)); 936341825Sdim if (!sys::path::is_absolute(FileName) && 937341825Sdim !Config->PDBSourcePath.empty()) { 938341825Sdim SmallString<128> AbsoluteFileName = Config->PDBSourcePath; 939341825Sdim sys::path::append(AbsoluteFileName, FileName); 940341825Sdim sys::path::native(AbsoluteFileName); 941341825Sdim sys::path::remove_dots(AbsoluteFileName, /*remove_dot_dots=*/true); 942341825Sdim FileName = std::move(AbsoluteFileName); 943341825Sdim } 944341825Sdim ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI, 945341825Sdim FileName)); 946341825Sdim NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum); 947341825Sdim } 948341825Sdim File->ModuleDBI->addDebugSubsection(std::move(NewChecksums)); 949314564Sdim} 950314564Sdim 951327952Sdimstatic PublicSym32 createPublic(Defined *Def) { 952327952Sdim PublicSym32 Pub(SymbolKind::S_PUB32); 953327952Sdim Pub.Name = Def->getName(); 954327952Sdim if (auto *D = dyn_cast<DefinedCOFF>(Def)) { 955327952Sdim if (D->getCOFFSymbol().isFunctionDefinition()) 956327952Sdim Pub.Flags = PublicSymFlags::Function; 957327952Sdim } else if (isa<DefinedImportThunk>(Def)) { 958327952Sdim Pub.Flags = PublicSymFlags::Function; 959327952Sdim } 960327952Sdim 961327952Sdim OutputSection *OS = Def->getChunk()->getOutputSection(); 962327952Sdim assert(OS && "all publics should be in final image"); 963327952Sdim Pub.Offset = Def->getRVA() - OS->getRVA(); 964327952Sdim Pub.Segment = OS->SectionIndex; 965327952Sdim return Pub; 966327952Sdim} 967327952Sdim 968321369Sdim// Add all object files to the PDB. Merge .debug$T sections into IpiData and 969321369Sdim// TpiData. 970321369Sdimvoid PDBLinker::addObjectsToPDB() { 971341825Sdim ScopedTimer T1(AddObjectsTimer); 972327952Sdim for (ObjFile *File : ObjFile::Instances) 973327952Sdim addObjFile(File); 974321369Sdim 975321369Sdim Builder.getStringTableBuilder().setStrings(PDBStrTab); 976341825Sdim T1.stop(); 977321369Sdim 978327952Sdim // Construct TPI and IPI stream contents. 979341825Sdim ScopedTimer T2(TpiStreamLayoutTimer); 980327952Sdim if (Config->DebugGHashes) { 981327952Sdim addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable); 982327952Sdim addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable); 983327952Sdim } else { 984327952Sdim addTypeInfo(Builder.getTpiBuilder(), TypeTable); 985327952Sdim addTypeInfo(Builder.getIpiBuilder(), IDTable); 986327952Sdim } 987341825Sdim T2.stop(); 988321369Sdim 989341825Sdim ScopedTimer T3(GlobalsLayoutTimer); 990327952Sdim // Compute the public and global symbols. 991327952Sdim auto &GsiBuilder = Builder.getGsiBuilder(); 992327952Sdim std::vector<PublicSym32> Publics; 993327952Sdim Symtab->forEachSymbol([&Publics](Symbol *S) { 994327952Sdim // Only emit defined, live symbols that have a chunk. 995327952Sdim auto *Def = dyn_cast<Defined>(S); 996327952Sdim if (Def && Def->isLive() && Def->getChunk()) 997327952Sdim Publics.push_back(createPublic(Def)); 998327952Sdim }); 999321369Sdim 1000327952Sdim if (!Publics.empty()) { 1001327952Sdim // Sort the public symbols and add them to the stream. 1002327952Sdim std::sort(Publics.begin(), Publics.end(), 1003327952Sdim [](const PublicSym32 &L, const PublicSym32 &R) { 1004327952Sdim return L.Name < R.Name; 1005327952Sdim }); 1006327952Sdim for (const PublicSym32 &Pub : Publics) 1007327952Sdim GsiBuilder.addPublicSymbol(Pub); 1008327952Sdim } 1009314564Sdim} 1010314564Sdim 1011341825Sdimvoid PDBLinker::addNatvisFiles() { 1012341825Sdim for (StringRef File : Config->NatvisFiles) { 1013341825Sdim ErrorOr<std::unique_ptr<MemoryBuffer>> DataOrErr = 1014341825Sdim MemoryBuffer::getFile(File); 1015341825Sdim if (!DataOrErr) { 1016341825Sdim warn("Cannot open input file: " + File); 1017341825Sdim continue; 1018341825Sdim } 1019341825Sdim Builder.addInjectedSource(File, std::move(*DataOrErr)); 1020341825Sdim } 1021341825Sdim} 1022341825Sdim 1023341825Sdimstatic codeview::CPUType toCodeViewMachine(COFF::MachineTypes Machine) { 1024341825Sdim switch (Machine) { 1025341825Sdim case COFF::IMAGE_FILE_MACHINE_AMD64: 1026341825Sdim return codeview::CPUType::X64; 1027341825Sdim case COFF::IMAGE_FILE_MACHINE_ARM: 1028341825Sdim return codeview::CPUType::ARM7; 1029341825Sdim case COFF::IMAGE_FILE_MACHINE_ARM64: 1030341825Sdim return codeview::CPUType::ARM64; 1031341825Sdim case COFF::IMAGE_FILE_MACHINE_ARMNT: 1032341825Sdim return codeview::CPUType::ARMNT; 1033341825Sdim case COFF::IMAGE_FILE_MACHINE_I386: 1034341825Sdim return codeview::CPUType::Intel80386; 1035341825Sdim default: 1036341825Sdim llvm_unreachable("Unsupported CPU Type"); 1037341825Sdim } 1038341825Sdim} 1039341825Sdim 1040327952Sdimstatic void addCommonLinkerModuleSymbols(StringRef Path, 1041327952Sdim pdb::DbiModuleDescriptorBuilder &Mod, 1042327952Sdim BumpPtrAllocator &Allocator) { 1043327952Sdim ObjNameSym ONS(SymbolRecordKind::ObjNameSym); 1044327952Sdim Compile3Sym CS(SymbolRecordKind::Compile3Sym); 1045327952Sdim EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym); 1046321369Sdim 1047321369Sdim ONS.Name = "* Linker *"; 1048321369Sdim ONS.Signature = 0; 1049321369Sdim 1050341825Sdim CS.Machine = toCodeViewMachine(Config->Machine); 1051327952Sdim // Interestingly, if we set the string to 0.0.0.0, then when trying to view 1052327952Sdim // local variables WinDbg emits an error that private symbols are not present. 1053327952Sdim // By setting this to a valid MSVC linker version string, local variables are 1054327952Sdim // displayed properly. As such, even though it is not representative of 1055327952Sdim // LLVM's version information, we need this for compatibility. 1056321369Sdim CS.Flags = CompileSym3Flags::None; 1057327952Sdim CS.VersionBackendBuild = 25019; 1058327952Sdim CS.VersionBackendMajor = 14; 1059327952Sdim CS.VersionBackendMinor = 10; 1060321369Sdim CS.VersionBackendQFE = 0; 1061327952Sdim 1062327952Sdim // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the 1063327952Sdim // linker module (which is by definition a backend), so we don't need to do 1064327952Sdim // anything here. Also, it seems we can use "LLVM Linker" for the linker name 1065327952Sdim // without any problems. Only the backend version has to be hardcoded to a 1066327952Sdim // magic number. 1067321369Sdim CS.VersionFrontendBuild = 0; 1068321369Sdim CS.VersionFrontendMajor = 0; 1069321369Sdim CS.VersionFrontendMinor = 0; 1070321369Sdim CS.VersionFrontendQFE = 0; 1071321369Sdim CS.Version = "LLVM Linker"; 1072321369Sdim CS.setLanguage(SourceLanguage::Link); 1073321369Sdim 1074321369Sdim ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front(); 1075321369Sdim std::string ArgStr = llvm::join(Args, " "); 1076321369Sdim EBS.Fields.push_back("cwd"); 1077321369Sdim SmallString<64> cwd; 1078321369Sdim sys::fs::current_path(cwd); 1079321369Sdim EBS.Fields.push_back(cwd); 1080321369Sdim EBS.Fields.push_back("exe"); 1081327952Sdim SmallString<64> exe = Config->Argv[0]; 1082327952Sdim llvm::sys::fs::make_absolute(exe); 1083327952Sdim EBS.Fields.push_back(exe); 1084321369Sdim EBS.Fields.push_back("pdb"); 1085321369Sdim EBS.Fields.push_back(Path); 1086321369Sdim EBS.Fields.push_back("cmd"); 1087321369Sdim EBS.Fields.push_back(ArgStr); 1088321369Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1089321369Sdim ONS, Allocator, CodeViewContainer::Pdb)); 1090321369Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1091321369Sdim CS, Allocator, CodeViewContainer::Pdb)); 1092321369Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1093321369Sdim EBS, Allocator, CodeViewContainer::Pdb)); 1094321369Sdim} 1095321369Sdim 1096327952Sdimstatic void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod, 1097327952Sdim OutputSection &OS, 1098327952Sdim BumpPtrAllocator &Allocator) { 1099327952Sdim SectionSym Sym(SymbolRecordKind::SectionSym); 1100327952Sdim Sym.Alignment = 12; // 2^12 = 4KB 1101341825Sdim Sym.Characteristics = OS.Header.Characteristics; 1102327952Sdim Sym.Length = OS.getVirtualSize(); 1103341825Sdim Sym.Name = OS.Name; 1104327952Sdim Sym.Rva = OS.getRVA(); 1105327952Sdim Sym.SectionNumber = OS.SectionIndex; 1106327952Sdim Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1107327952Sdim Sym, Allocator, CodeViewContainer::Pdb)); 1108327952Sdim} 1109327952Sdim 1110314564Sdim// Creates a PDB file. 1111327952Sdimvoid coff::createPDB(SymbolTable *Symtab, 1112327952Sdim ArrayRef<OutputSection *> OutputSections, 1113327952Sdim ArrayRef<uint8_t> SectionTable, 1114327952Sdim const llvm::codeview::DebugInfo &BuildId) { 1115341825Sdim ScopedTimer T1(TotalPdbLinkTimer); 1116321369Sdim PDBLinker PDB(Symtab); 1117341825Sdim 1118327952Sdim PDB.initialize(BuildId); 1119321369Sdim PDB.addObjectsToPDB(); 1120327952Sdim PDB.addSections(OutputSections, SectionTable); 1121341825Sdim PDB.addNatvisFiles(); 1122341825Sdim 1123341825Sdim ScopedTimer T2(DiskCommitTimer); 1124321369Sdim PDB.commit(); 1125321369Sdim} 1126314564Sdim 1127327952Sdimvoid PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) { 1128314564Sdim ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize 1129314564Sdim 1130314564Sdim // Create streams in MSF for predefined streams, namely 1131314564Sdim // PDB, TPI, DBI and IPI. 1132314564Sdim for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) 1133314564Sdim ExitOnErr(Builder.getMsfBuilder().addStream(0)); 1134314564Sdim 1135314564Sdim // Add an Info stream. 1136314564Sdim auto &InfoBuilder = Builder.getInfoBuilder(); 1137327952Sdim GUID uuid; 1138327952Sdim memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid)); 1139341825Sdim InfoBuilder.setAge(BuildId.PDB70.Age); 1140321369Sdim InfoBuilder.setGuid(uuid); 1141314564Sdim InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); 1142314564Sdim 1143321369Sdim // Add an empty DBI stream. 1144321369Sdim pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); 1145327952Sdim DbiBuilder.setAge(BuildId.PDB70.Age); 1146321369Sdim DbiBuilder.setVersionHeader(pdb::PdbDbiV70); 1147341825Sdim DbiBuilder.setMachineType(Config->Machine); 1148341825Sdim // Technically we are not link.exe 14.11, but there are known cases where 1149341825Sdim // debugging tools on Windows expect Microsoft-specific version numbers or 1150341825Sdim // they fail to work at all. Since we know we produce PDBs that are 1151341825Sdim // compatible with LINK 14.11, we set that version number here. 1152341825Sdim DbiBuilder.setBuildNumber(14, 11); 1153321369Sdim} 1154314564Sdim 1155327952Sdimvoid PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections, 1156327952Sdim ArrayRef<uint8_t> SectionTable) { 1157327952Sdim // It's not entirely clear what this is, but the * Linker * module uses it. 1158321369Sdim pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); 1159327952Sdim NativePath = Config->PDBPath; 1160327952Sdim sys::fs::make_absolute(NativePath); 1161327952Sdim sys::path::native(NativePath, sys::path::Style::windows); 1162327952Sdim uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath); 1163327952Sdim auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); 1164327952Sdim LinkerModule.setPdbFilePathNI(PdbFilePathNI); 1165327952Sdim addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc); 1166314564Sdim 1167327952Sdim // Add section contributions. They must be ordered by ascending RVA. 1168327952Sdim for (OutputSection *OS : OutputSections) { 1169327952Sdim addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc); 1170341825Sdim for (Chunk *C : OS->getChunks()) { 1171341825Sdim pdb::SectionContrib SC = 1172341825Sdim createSectionContrib(C, LinkerModule.getModuleIndex()); 1173341825Sdim Builder.getDbiBuilder().addSectionContrib(SC); 1174341825Sdim } 1175327952Sdim } 1176327952Sdim 1177314564Sdim // Add Section Map stream. 1178314564Sdim ArrayRef<object::coff_section> Sections = { 1179314564Sdim (const object::coff_section *)SectionTable.data(), 1180314564Sdim SectionTable.size() / sizeof(object::coff_section)}; 1181321369Sdim SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections); 1182314564Sdim DbiBuilder.setSectionMap(SectionMap); 1183314564Sdim 1184314564Sdim // Add COFF section header stream. 1185314564Sdim ExitOnErr( 1186314564Sdim DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); 1187321369Sdim} 1188314564Sdim 1189321369Sdimvoid PDBLinker::commit() { 1190314564Sdim // Write to a file. 1191321369Sdim ExitOnErr(Builder.commit(Config->PDBPath)); 1192314564Sdim} 1193341825Sdim 1194341825Sdimstatic Expected<StringRef> 1195341825SdimgetFileName(const DebugStringTableSubsectionRef &Strings, 1196341825Sdim const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) { 1197341825Sdim auto Iter = Checksums.getArray().at(FileID); 1198341825Sdim if (Iter == Checksums.getArray().end()) 1199341825Sdim return make_error<CodeViewError>(cv_error_code::no_records); 1200341825Sdim uint32_t Offset = Iter->FileNameOffset; 1201341825Sdim return Strings.getString(Offset); 1202341825Sdim} 1203341825Sdim 1204341825Sdimstatic uint32_t getSecrelReloc() { 1205341825Sdim switch (Config->Machine) { 1206341825Sdim case AMD64: 1207341825Sdim return COFF::IMAGE_REL_AMD64_SECREL; 1208341825Sdim case I386: 1209341825Sdim return COFF::IMAGE_REL_I386_SECREL; 1210341825Sdim case ARMNT: 1211341825Sdim return COFF::IMAGE_REL_ARM_SECREL; 1212341825Sdim case ARM64: 1213341825Sdim return COFF::IMAGE_REL_ARM64_SECREL; 1214341825Sdim default: 1215341825Sdim llvm_unreachable("unknown machine type"); 1216341825Sdim } 1217341825Sdim} 1218341825Sdim 1219341825Sdim// Try to find a line table for the given offset Addr into the given chunk C. 1220341825Sdim// If a line table was found, the line table, the string and checksum tables 1221341825Sdim// that are used to interpret the line table, and the offset of Addr in the line 1222341825Sdim// table are stored in the output arguments. Returns whether a line table was 1223341825Sdim// found. 1224341825Sdimstatic bool findLineTable(const SectionChunk *C, uint32_t Addr, 1225341825Sdim DebugStringTableSubsectionRef &CVStrTab, 1226341825Sdim DebugChecksumsSubsectionRef &Checksums, 1227341825Sdim DebugLinesSubsectionRef &Lines, 1228341825Sdim uint32_t &OffsetInLinetable) { 1229341825Sdim ExitOnError ExitOnErr; 1230341825Sdim uint32_t SecrelReloc = getSecrelReloc(); 1231341825Sdim 1232341825Sdim for (SectionChunk *DbgC : C->File->getDebugChunks()) { 1233341825Sdim if (DbgC->getSectionName() != ".debug$S") 1234341825Sdim continue; 1235341825Sdim 1236341825Sdim // Build a mapping of SECREL relocations in DbgC that refer to C. 1237341825Sdim DenseMap<uint32_t, uint32_t> Secrels; 1238341825Sdim for (const coff_relocation &R : DbgC->Relocs) { 1239341825Sdim if (R.Type != SecrelReloc) 1240341825Sdim continue; 1241341825Sdim 1242341825Sdim if (auto *S = dyn_cast_or_null<DefinedRegular>( 1243341825Sdim C->File->getSymbols()[R.SymbolTableIndex])) 1244341825Sdim if (S->getChunk() == C) 1245341825Sdim Secrels[R.VirtualAddress] = S->getValue(); 1246341825Sdim } 1247341825Sdim 1248341825Sdim ArrayRef<uint8_t> Contents = 1249341825Sdim consumeDebugMagic(DbgC->getContents(), ".debug$S"); 1250341825Sdim DebugSubsectionArray Subsections; 1251341825Sdim BinaryStreamReader Reader(Contents, support::little); 1252341825Sdim ExitOnErr(Reader.readArray(Subsections, Contents.size())); 1253341825Sdim 1254341825Sdim for (const DebugSubsectionRecord &SS : Subsections) { 1255341825Sdim switch (SS.kind()) { 1256341825Sdim case DebugSubsectionKind::StringTable: { 1257341825Sdim assert(!CVStrTab.valid() && 1258341825Sdim "Encountered multiple string table subsections!"); 1259341825Sdim ExitOnErr(CVStrTab.initialize(SS.getRecordData())); 1260341825Sdim break; 1261341825Sdim } 1262341825Sdim case DebugSubsectionKind::FileChecksums: 1263341825Sdim assert(!Checksums.valid() && 1264341825Sdim "Encountered multiple checksum subsections!"); 1265341825Sdim ExitOnErr(Checksums.initialize(SS.getRecordData())); 1266341825Sdim break; 1267341825Sdim case DebugSubsectionKind::Lines: { 1268341825Sdim ArrayRef<uint8_t> Bytes; 1269341825Sdim auto Ref = SS.getRecordData(); 1270341825Sdim ExitOnErr(Ref.readLongestContiguousChunk(0, Bytes)); 1271341825Sdim size_t OffsetInDbgC = Bytes.data() - DbgC->getContents().data(); 1272341825Sdim 1273341825Sdim // Check whether this line table refers to C. 1274341825Sdim auto I = Secrels.find(OffsetInDbgC); 1275341825Sdim if (I == Secrels.end()) 1276341825Sdim break; 1277341825Sdim 1278341825Sdim // Check whether this line table covers Addr in C. 1279341825Sdim DebugLinesSubsectionRef LinesTmp; 1280341825Sdim ExitOnErr(LinesTmp.initialize(BinaryStreamReader(Ref))); 1281341825Sdim uint32_t OffsetInC = I->second + LinesTmp.header()->RelocOffset; 1282341825Sdim if (Addr < OffsetInC || Addr >= OffsetInC + LinesTmp.header()->CodeSize) 1283341825Sdim break; 1284341825Sdim 1285341825Sdim assert(!Lines.header() && 1286341825Sdim "Encountered multiple line tables for function!"); 1287341825Sdim ExitOnErr(Lines.initialize(BinaryStreamReader(Ref))); 1288341825Sdim OffsetInLinetable = Addr - OffsetInC; 1289341825Sdim break; 1290341825Sdim } 1291341825Sdim default: 1292341825Sdim break; 1293341825Sdim } 1294341825Sdim 1295341825Sdim if (CVStrTab.valid() && Checksums.valid() && Lines.header()) 1296341825Sdim return true; 1297341825Sdim } 1298341825Sdim } 1299341825Sdim 1300341825Sdim return false; 1301341825Sdim} 1302341825Sdim 1303341825Sdim// Use CodeView line tables to resolve a file and line number for the given 1304341825Sdim// offset into the given chunk and return them, or {"", 0} if a line table was 1305341825Sdim// not found. 1306341825Sdimstd::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *C, 1307341825Sdim uint32_t Addr) { 1308341825Sdim ExitOnError ExitOnErr; 1309341825Sdim 1310341825Sdim DebugStringTableSubsectionRef CVStrTab; 1311341825Sdim DebugChecksumsSubsectionRef Checksums; 1312341825Sdim DebugLinesSubsectionRef Lines; 1313341825Sdim uint32_t OffsetInLinetable; 1314341825Sdim 1315341825Sdim if (!findLineTable(C, Addr, CVStrTab, Checksums, Lines, OffsetInLinetable)) 1316341825Sdim return {"", 0}; 1317341825Sdim 1318341825Sdim uint32_t NameIndex; 1319341825Sdim uint32_t LineNumber; 1320341825Sdim for (LineColumnEntry &Entry : Lines) { 1321341825Sdim for (const LineNumberEntry &LN : Entry.LineNumbers) { 1322341825Sdim if (LN.Offset > OffsetInLinetable) { 1323341825Sdim StringRef Filename = 1324341825Sdim ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex)); 1325341825Sdim return {Filename, LineNumber}; 1326341825Sdim } 1327341825Sdim LineInfo LI(LN.Flags); 1328341825Sdim NameIndex = Entry.NameIndex; 1329341825Sdim LineNumber = LI.getStartLine(); 1330341825Sdim } 1331341825Sdim } 1332341825Sdim StringRef Filename = ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex)); 1333341825Sdim return {Filename, LineNumber}; 1334341825Sdim} 1335