PDB.cpp revision 360660
11556Srgrimes//===- PDB.cpp ------------------------------------------------------------===// 21556Srgrimes// 31556Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 41556Srgrimes// See https://llvm.org/LICENSE.txt for license information. 51556Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61556Srgrimes// 71556Srgrimes//===----------------------------------------------------------------------===// 81556Srgrimes 91556Srgrimes#include "PDB.h" 101556Srgrimes#include "Chunks.h" 111556Srgrimes#include "Config.h" 121556Srgrimes#include "DebugTypes.h" 131556Srgrimes#include "Driver.h" 141556Srgrimes#include "SymbolTable.h" 151556Srgrimes#include "Symbols.h" 161556Srgrimes#include "TypeMerger.h" 171556Srgrimes#include "Writer.h" 181556Srgrimes#include "lld/Common/ErrorHandler.h" 191556Srgrimes#include "lld/Common/Timer.h" 201556Srgrimes#include "lld/Common/Threads.h" 211556Srgrimes#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" 221556Srgrimes#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" 231556Srgrimes#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h" 241556Srgrimes#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 251556Srgrimes#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" 261556Srgrimes#include "llvm/DebugInfo/CodeView/RecordName.h" 271556Srgrimes#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 281556Srgrimes#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h" 291556Srgrimes#include "llvm/DebugInfo/CodeView/SymbolSerializer.h" 3090039Sobrien#include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 3127967Ssteve#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 3290039Sobrien#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" 3390039Sobrien#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" 3490039Sobrien#include "llvm/DebugInfo/MSF/MSFBuilder.h" 351556Srgrimes#include "llvm/DebugInfo/MSF/MSFCommon.h" 361556Srgrimes#include "llvm/DebugInfo/PDB/GenericError.h" 371556Srgrimes#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" 381556Srgrimes#include "llvm/DebugInfo/PDB/Native/DbiStream.h" 391556Srgrimes#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" 4061746Sjoe#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" 411556Srgrimes#include "llvm/DebugInfo/PDB/Native/InfoStream.h" 42150065Sstefanf#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" 431556Srgrimes#include "llvm/DebugInfo/PDB/Native/NativeSession.h" 44228972Sjilles#include "llvm/DebugInfo/PDB/Native/PDBFile.h" 4554827Sroberto#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" 46228972Sjilles#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 47228972Sjilles#include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 4854827Sroberto#include "llvm/DebugInfo/PDB/Native/TpiStream.h" 49228972Sjilles#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" 5054827Sroberto#include "llvm/DebugInfo/PDB/PDB.h" 51228972Sjilles#include "llvm/Object/COFF.h" 52228972Sjilles#include "llvm/Object/CVDebugRecord.h" 53228972Sjilles#include "llvm/Support/BinaryByteStream.h" 54228972Sjilles#include "llvm/Support/Endian.h" 55228972Sjilles#include "llvm/Support/Errc.h" 56228972Sjilles#include "llvm/Support/FormatVariadic.h" 57228972Sjilles#include "llvm/Support/JamCRC.h" 58228972Sjilles#include "llvm/Support/Path.h" 59228972Sjilles#include "llvm/Support/ScopedPrinter.h" 6094831Sjoe#include <memory> 61228972Sjilles 6294831Sjoeusing namespace lld; 63228972Sjillesusing namespace lld::coff; 64228972Sjillesusing namespace llvm; 65254627Skenusing namespace llvm::codeview; 66254627Sken 67254627Skenusing llvm::object::coff_section; 68254627Sken 69228972Sjillesstatic ExitOnError exitOnErr; 70228972Sjilles 71228972Sjillesstatic Timer totalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root()); 72228972Sjilles 73254627Skenstatic Timer addObjectsTimer("Add Objects", totalPdbLinkTimer); 74254627Skenstatic Timer typeMergingTimer("Type Merging", addObjectsTimer); 75254627Skenstatic Timer symbolMergingTimer("Symbol Merging", addObjectsTimer); 76254627Skenstatic Timer globalsLayoutTimer("Globals Stream Layout", totalPdbLinkTimer); 77228972Sjillesstatic Timer tpiStreamLayoutTimer("TPI Stream Layout", totalPdbLinkTimer); 78254627Skenstatic Timer diskCommitTimer("Commit to Disk", totalPdbLinkTimer); 79254627Sken 80254627Skennamespace { 81254627Skenclass DebugSHandler; 82254627Sken 83254627Skenclass PDBLinker { 84254627Sken friend DebugSHandler; 85254627Sken 86254627Skenpublic: 87254627Sken PDBLinker(SymbolTable *symtab) 8854827Sroberto : alloc(), symtab(symtab), builder(alloc), tMerger(alloc) { 8954827Sroberto // This isn't strictly necessary, but link.exe usually puts an empty string 901556Srgrimes // as the first "valid" string in the string table, so we do the same in 911556Srgrimes // order to maintain as much byte-for-byte compatibility as possible. 9261737Sjoe pdbStrTab.insert(""); 9361737Sjoe } 9461738Sjoe 951556Srgrimes /// Emit the basic PDB structure: initial streams, headers, etc. 961556Srgrimes void initialize(llvm::codeview::DebugInfo *buildId); 9761738Sjoe 981556Srgrimes /// Add natvis files specified on the command line. 991556Srgrimes void addNatvisFiles(); 10061746Sjoe 101229024Sdim /// Link CodeView from each object file in the symbol table into the PDB. 102229024Sdim void addObjectsToPDB(); 10354827Sroberto 10454827Sroberto /// Link info for each import file in the symbol table into the PDB. 1051556Srgrimes void addImportFilesToPDB(ArrayRef<OutputSection *> outputSections); 10661746Sjoe 10761746Sjoe /// Link CodeView from a single object file into the target (output) PDB. 10861746Sjoe /// When a precompiled headers object is linked, its TPI map might be provided 10954827Sroberto /// externally. 11054827Sroberto void addObjFile(ObjFile *file, CVIndexMap *externIndexMap = nullptr); 11154827Sroberto 11254827Sroberto /// Produce a mapping from the type and item indices used in the object 11354827Sroberto /// file to those in the destination PDB. 11454827Sroberto /// 11554827Sroberto /// If the object file uses a type server PDB (compiled with /Zi), merge TPI 11654827Sroberto /// and IPI from the type server PDB and return a map for it. Each unique type 11754827Sroberto /// server PDB is merged at most once, so this may return an existing index 11854827Sroberto /// mapping. 11954827Sroberto /// 12054948Sroberto /// If the object does not use a type server PDB (compiled with /Z7), we merge 12161738Sjoe /// all the type and item records from the .debug$S stream and fill in the 1221556Srgrimes /// caller-provided objectIndexMap. 1231556Srgrimes Expected<const CVIndexMap &> mergeDebugT(ObjFile *file, 1241556Srgrimes CVIndexMap *objectIndexMap); 12561737Sjoe 12661737Sjoe /// Reads and makes available a PDB. 1271556Srgrimes Expected<const CVIndexMap &> maybeMergeTypeServerPDB(ObjFile *file); 1281556Srgrimes 1291556Srgrimes /// Merges a precompiled headers TPI map into the current TPI map. The 1301556Srgrimes /// precompiled headers object will also be loaded and remapped in the 13161737Sjoe /// process. 1321556Srgrimes Error mergeInPrecompHeaderObj(ObjFile *file, CVIndexMap *objectIndexMap); 1331556Srgrimes 1341556Srgrimes /// Reads and makes available a precompiled headers object. 1351556Srgrimes /// 13654827Sroberto /// This is a requirement for objects compiled with cl.exe /Yu. In that 1371556Srgrimes /// case, the referenced object (which was compiled with /Yc) has to be loaded 1381556Srgrimes /// first. This is mainly because the current object's TPI stream has external 1391556Srgrimes /// references to the precompiled headers object. 1401556Srgrimes /// 1411556Srgrimes /// If the precompiled headers object was already loaded, this function will 1421556Srgrimes /// simply return its (remapped) TPI map. 1431556Srgrimes Expected<const CVIndexMap &> aquirePrecompObj(ObjFile *file); 1441556Srgrimes 1451556Srgrimes /// Adds a precompiled headers object signature -> TPI mapping. 1461556Srgrimes std::pair<CVIndexMap &, bool /*already there*/> 14754827Sroberto registerPrecompiledHeaders(uint32_t signature); 14854827Sroberto 14954827Sroberto void mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap, 15054827Sroberto std::vector<ulittle32_t *> &stringTableRefs, 15154827Sroberto BinaryStreamRef symData); 15254827Sroberto 15354827Sroberto /// Add the section map and section contributions to the PDB. 15454827Sroberto void addSections(ArrayRef<OutputSection *> outputSections, 15554827Sroberto ArrayRef<uint8_t> sectionTable); 15654827Sroberto 15754827Sroberto /// Write the PDB to disk and store the Guid generated for it in *Guid. 15854827Sroberto void commit(codeview::GUID *guid); 15954827Sroberto 16054827Sroberto // Print statistics regarding the final PDB 16154827Sroberto void printStats(); 16254827Sroberto 16354827Srobertoprivate: 16454827Sroberto BumpPtrAllocator alloc; 16554827Sroberto 16654827Sroberto SymbolTable *symtab; 1671556Srgrimes 16854827Sroberto pdb::PDBFileBuilder builder; 16954827Sroberto 1701556Srgrimes TypeMerger tMerger; 17154827Sroberto 1721556Srgrimes /// PDBs use a single global string table for filenames in the file checksum 173 /// table. 174 DebugStringTableSubsection pdbStrTab; 175 176 llvm::SmallString<128> nativePath; 177 178 std::vector<pdb::SecMapEntry> sectionMap; 179 180 /// Type index mappings of type server PDBs that we've loaded so far. 181 std::map<codeview::GUID, CVIndexMap> typeServerIndexMappings; 182 183 /// Type index mappings of precompiled objects type map that we've loaded so 184 /// far. 185 std::map<uint32_t, CVIndexMap> precompTypeIndexMappings; 186 187 // For statistics 188 uint64_t globalSymbols = 0; 189 uint64_t moduleSymbols = 0; 190 uint64_t publicSymbols = 0; 191}; 192 193class DebugSHandler { 194 PDBLinker &linker; 195 196 /// The object file whose .debug$S sections we're processing. 197 ObjFile &file; 198 199 /// The result of merging type indices. 200 const CVIndexMap &indexMap; 201 202 /// The DEBUG_S_STRINGTABLE subsection. These strings are referred to by 203 /// index from other records in the .debug$S section. All of these strings 204 /// need to be added to the global PDB string table, and all references to 205 /// these strings need to have their indices re-written to refer to the 206 /// global PDB string table. 207 DebugStringTableSubsectionRef cVStrTab; 208 209 /// The DEBUG_S_FILECHKSMS subsection. As above, these are referred to 210 /// by other records in the .debug$S section and need to be merged into the 211 /// PDB. 212 DebugChecksumsSubsectionRef checksums; 213 214 /// The DEBUG_S_INLINEELINES subsection. There can be only one of these per 215 /// object file. 216 DebugInlineeLinesSubsectionRef inlineeLines; 217 218 /// The DEBUG_S_FRAMEDATA subsection(s). There can be more than one of 219 /// these and they need not appear in any specific order. However, they 220 /// contain string table references which need to be re-written, so we 221 /// collect them all here and re-write them after all subsections have been 222 /// discovered and processed. 223 std::vector<DebugFrameDataSubsectionRef> newFpoFrames; 224 225 /// Pointers to raw memory that we determine have string table references 226 /// that need to be re-written. We first process all .debug$S subsections 227 /// to ensure that we can handle subsections written in any order, building 228 /// up this list as we go. At the end, we use the string table (which must 229 /// have been discovered by now else it is an error) to re-write these 230 /// references. 231 std::vector<ulittle32_t *> stringTableReferences; 232 233public: 234 DebugSHandler(PDBLinker &linker, ObjFile &file, const CVIndexMap &indexMap) 235 : linker(linker), file(file), indexMap(indexMap) {} 236 237 void handleDebugS(lld::coff::SectionChunk &debugS); 238 239 std::shared_ptr<DebugInlineeLinesSubsection> 240 mergeInlineeLines(DebugChecksumsSubsection *newChecksums); 241 242 void finish(); 243}; 244} 245 246// Visual Studio's debugger requires absolute paths in various places in the 247// PDB to work without additional configuration: 248// https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box 249static void pdbMakeAbsolute(SmallVectorImpl<char> &fileName) { 250 // The default behavior is to produce paths that are valid within the context 251 // of the machine that you perform the link on. If the linker is running on 252 // a POSIX system, we will output absolute POSIX paths. If the linker is 253 // running on a Windows system, we will output absolute Windows paths. If the 254 // user desires any other kind of behavior, they should explicitly pass 255 // /pdbsourcepath, in which case we will treat the exact string the user 256 // passed in as the gospel and not normalize, canonicalize it. 257 if (sys::path::is_absolute(fileName, sys::path::Style::windows) || 258 sys::path::is_absolute(fileName, sys::path::Style::posix)) 259 return; 260 261 // It's not absolute in any path syntax. Relative paths necessarily refer to 262 // the local file system, so we can make it native without ending up with a 263 // nonsensical path. 264 if (config->pdbSourcePath.empty()) { 265 sys::path::native(fileName); 266 sys::fs::make_absolute(fileName); 267 return; 268 } 269 270 // Try to guess whether /PDBSOURCEPATH is a unix path or a windows path. 271 // Since PDB's are more of a Windows thing, we make this conservative and only 272 // decide that it's a unix path if we're fairly certain. Specifically, if 273 // it starts with a forward slash. 274 SmallString<128> absoluteFileName = config->pdbSourcePath; 275 sys::path::Style guessedStyle = absoluteFileName.startswith("/") 276 ? sys::path::Style::posix 277 : sys::path::Style::windows; 278 sys::path::append(absoluteFileName, guessedStyle, fileName); 279 sys::path::native(absoluteFileName, guessedStyle); 280 sys::path::remove_dots(absoluteFileName, true, guessedStyle); 281 282 fileName = std::move(absoluteFileName); 283} 284 285// A COFF .debug$H section is currently a clang extension. This function checks 286// if a .debug$H section is in a format that we expect / understand, so that we 287// can ignore any sections which are coincidentally also named .debug$H but do 288// not contain a format we recognize. 289static bool canUseDebugH(ArrayRef<uint8_t> debugH) { 290 if (debugH.size() < sizeof(object::debug_h_header)) 291 return false; 292 auto *header = 293 reinterpret_cast<const object::debug_h_header *>(debugH.data()); 294 debugH = debugH.drop_front(sizeof(object::debug_h_header)); 295 return header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC && 296 header->Version == 0 && 297 header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) && 298 (debugH.size() % 8 == 0); 299} 300 301static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *file) { 302 SectionChunk *sec = 303 SectionChunk::findByName(file->getDebugChunks(), ".debug$H"); 304 if (!sec) 305 return llvm::None; 306 ArrayRef<uint8_t> contents = sec->getContents(); 307 if (!canUseDebugH(contents)) 308 return None; 309 return contents; 310} 311 312static ArrayRef<GloballyHashedType> 313getHashesFromDebugH(ArrayRef<uint8_t> debugH) { 314 assert(canUseDebugH(debugH)); 315 316 debugH = debugH.drop_front(sizeof(object::debug_h_header)); 317 uint32_t count = debugH.size() / sizeof(GloballyHashedType); 318 return {reinterpret_cast<const GloballyHashedType *>(debugH.data()), count}; 319} 320 321static void addTypeInfo(pdb::TpiStreamBuilder &tpiBuilder, 322 TypeCollection &typeTable) { 323 // Start the TPI or IPI stream header. 324 tpiBuilder.setVersionHeader(pdb::PdbTpiV80); 325 326 // Flatten the in memory type table and hash each type. 327 typeTable.ForEachRecord([&](TypeIndex ti, const CVType &type) { 328 auto hash = pdb::hashTypeRecord(type); 329 if (auto e = hash.takeError()) 330 fatal("type hashing error"); 331 tpiBuilder.addTypeRecord(type.RecordData, *hash); 332 }); 333} 334 335Expected<const CVIndexMap &> 336PDBLinker::mergeDebugT(ObjFile *file, CVIndexMap *objectIndexMap) { 337 ScopedTimer t(typeMergingTimer); 338 339 if (!file->debugTypesObj) 340 return *objectIndexMap; // no Types stream 341 342 // Precompiled headers objects need to save the index map for further 343 // reference by other objects which use the precompiled headers. 344 if (file->debugTypesObj->kind == TpiSource::PCH) { 345 uint32_t pchSignature = file->pchSignature.getValueOr(0); 346 if (pchSignature == 0) 347 fatal("No signature found for the precompiled headers OBJ (" + 348 file->getName() + ")"); 349 350 // When a precompiled headers object comes first on the command-line, we 351 // update the mapping here. Otherwise, if an object referencing the 352 // precompiled headers object comes first, the mapping is created in 353 // aquirePrecompObj(), thus we would skip this block. 354 if (!objectIndexMap->isPrecompiledTypeMap) { 355 auto r = registerPrecompiledHeaders(pchSignature); 356 if (r.second) 357 fatal( 358 "A precompiled headers OBJ with the same signature was already " 359 "provided! (" + 360 file->getName() + ")"); 361 362 objectIndexMap = &r.first; 363 } 364 } 365 366 if (file->debugTypesObj->kind == TpiSource::UsingPDB) { 367 // Look through type servers. If we've already seen this type server, 368 // don't merge any type information. 369 return maybeMergeTypeServerPDB(file); 370 } 371 372 CVTypeArray &types = *file->debugTypes; 373 374 if (file->debugTypesObj->kind == TpiSource::UsingPCH) { 375 // This object was compiled with /Yu, so process the corresponding 376 // precompiled headers object (/Yc) first. Some type indices in the current 377 // object are referencing data in the precompiled headers object, so we need 378 // both to be loaded. 379 Error e = mergeInPrecompHeaderObj(file, objectIndexMap); 380 if (e) 381 return std::move(e); 382 383 // Drop LF_PRECOMP record from the input stream, as it has been replaced 384 // with the precompiled headers Type stream in the mergeInPrecompHeaderObj() 385 // call above. Note that we can't just call Types.drop_front(), as we 386 // explicitly want to rebase the stream. 387 CVTypeArray::Iterator firstType = types.begin(); 388 types.setUnderlyingStream( 389 types.getUnderlyingStream().drop_front(firstType->RecordData.size())); 390 } 391 392 // Fill in the temporary, caller-provided ObjectIndexMap. 393 if (config->debugGHashes) { 394 ArrayRef<GloballyHashedType> hashes; 395 std::vector<GloballyHashedType> ownedHashes; 396 if (Optional<ArrayRef<uint8_t>> debugH = getDebugH(file)) 397 hashes = getHashesFromDebugH(*debugH); 398 else { 399 ownedHashes = GloballyHashedType::hashTypes(types); 400 hashes = ownedHashes; 401 } 402 403 if (auto err = mergeTypeAndIdRecords( 404 tMerger.globalIDTable, tMerger.globalTypeTable, 405 objectIndexMap->tpiMap, types, hashes, file->pchSignature)) 406 fatal("codeview::mergeTypeAndIdRecords failed: " + 407 toString(std::move(err))); 408 } else { 409 if (auto err = mergeTypeAndIdRecords(tMerger.iDTable, tMerger.typeTable, 410 objectIndexMap->tpiMap, types, 411 file->pchSignature)) 412 fatal("codeview::mergeTypeAndIdRecords failed: " + 413 toString(std::move(err))); 414 } 415 return *objectIndexMap; 416} 417 418Expected<const CVIndexMap &> PDBLinker::maybeMergeTypeServerPDB(ObjFile *file) { 419 Expected<llvm::pdb::NativeSession *> pdbSession = findTypeServerSource(file); 420 if (!pdbSession) 421 return pdbSession.takeError(); 422 423 pdb::PDBFile &pdbFile = pdbSession.get()->getPDBFile(); 424 pdb::InfoStream &info = cantFail(pdbFile.getPDBInfoStream()); 425 426 auto it = typeServerIndexMappings.emplace(info.getGuid(), CVIndexMap()); 427 CVIndexMap &indexMap = it.first->second; 428 if (!it.second) 429 return indexMap; // already merged 430 431 // Mark this map as a type server map. 432 indexMap.isTypeServerMap = true; 433 434 Expected<pdb::TpiStream &> expectedTpi = pdbFile.getPDBTpiStream(); 435 if (auto e = expectedTpi.takeError()) 436 fatal("Type server does not have TPI stream: " + toString(std::move(e))); 437 pdb::TpiStream *maybeIpi = nullptr; 438 if (pdbFile.hasPDBIpiStream()) { 439 Expected<pdb::TpiStream &> expectedIpi = pdbFile.getPDBIpiStream(); 440 if (auto e = expectedIpi.takeError()) 441 fatal("Error getting type server IPI stream: " + toString(std::move(e))); 442 maybeIpi = &*expectedIpi; 443 } 444 445 if (config->debugGHashes) { 446 // PDBs do not actually store global hashes, so when merging a type server 447 // PDB we have to synthesize global hashes. To do this, we first synthesize 448 // global hashes for the TPI stream, since it is independent, then we 449 // synthesize hashes for the IPI stream, using the hashes for the TPI stream 450 // as inputs. 451 auto tpiHashes = GloballyHashedType::hashTypes(expectedTpi->typeArray()); 452 Optional<uint32_t> endPrecomp; 453 // Merge TPI first, because the IPI stream will reference type indices. 454 if (auto err = 455 mergeTypeRecords(tMerger.globalTypeTable, indexMap.tpiMap, 456 expectedTpi->typeArray(), tpiHashes, endPrecomp)) 457 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err))); 458 459 // Merge IPI. 460 if (maybeIpi) { 461 auto ipiHashes = 462 GloballyHashedType::hashIds(maybeIpi->typeArray(), tpiHashes); 463 if (auto err = 464 mergeIdRecords(tMerger.globalIDTable, indexMap.tpiMap, 465 indexMap.ipiMap, maybeIpi->typeArray(), ipiHashes)) 466 fatal("codeview::mergeIdRecords failed: " + toString(std::move(err))); 467 } 468 } else { 469 // Merge TPI first, because the IPI stream will reference type indices. 470 if (auto err = mergeTypeRecords(tMerger.typeTable, indexMap.tpiMap, 471 expectedTpi->typeArray())) 472 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err))); 473 474 // Merge IPI. 475 if (maybeIpi) { 476 if (auto err = mergeIdRecords(tMerger.iDTable, indexMap.tpiMap, 477 indexMap.ipiMap, maybeIpi->typeArray())) 478 fatal("codeview::mergeIdRecords failed: " + toString(std::move(err))); 479 } 480 } 481 482 return indexMap; 483} 484 485Error PDBLinker::mergeInPrecompHeaderObj(ObjFile *file, 486 CVIndexMap *objectIndexMap) { 487 const PrecompRecord &precomp = 488 retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj); 489 490 Expected<const CVIndexMap &> e = aquirePrecompObj(file); 491 if (!e) 492 return e.takeError(); 493 494 const CVIndexMap &precompIndexMap = *e; 495 assert(precompIndexMap.isPrecompiledTypeMap); 496 497 if (precompIndexMap.tpiMap.empty()) 498 return Error::success(); 499 500 assert(precomp.getStartTypeIndex() == TypeIndex::FirstNonSimpleIndex); 501 assert(precomp.getTypesCount() <= precompIndexMap.tpiMap.size()); 502 // Use the previously remapped index map from the precompiled headers. 503 objectIndexMap->tpiMap.append(precompIndexMap.tpiMap.begin(), 504 precompIndexMap.tpiMap.begin() + 505 precomp.getTypesCount()); 506 return Error::success(); 507} 508 509static bool equals_path(StringRef path1, StringRef path2) { 510#if defined(_WIN32) 511 return path1.equals_lower(path2); 512#else 513 return path1.equals(path2); 514#endif 515} 516 517// Find by name an OBJ provided on the command line 518static ObjFile *findObjByName(StringRef fileNameOnly) { 519 SmallString<128> currentPath; 520 521 for (ObjFile *f : ObjFile::instances) { 522 StringRef currentFileName = sys::path::filename(f->getName()); 523 524 // Compare based solely on the file name (link.exe behavior) 525 if (equals_path(currentFileName, fileNameOnly)) 526 return f; 527 } 528 return nullptr; 529} 530 531std::pair<CVIndexMap &, bool /*already there*/> 532PDBLinker::registerPrecompiledHeaders(uint32_t signature) { 533 auto insertion = precompTypeIndexMappings.insert({signature, CVIndexMap()}); 534 CVIndexMap &indexMap = insertion.first->second; 535 if (!insertion.second) 536 return {indexMap, true}; 537 // Mark this map as a precompiled types map. 538 indexMap.isPrecompiledTypeMap = true; 539 return {indexMap, false}; 540} 541 542Expected<const CVIndexMap &> PDBLinker::aquirePrecompObj(ObjFile *file) { 543 const PrecompRecord &precomp = 544 retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj); 545 546 // First, check if we already loaded the precompiled headers object with this 547 // signature. Return the type index mapping if we've already seen it. 548 auto r = registerPrecompiledHeaders(precomp.getSignature()); 549 if (r.second) 550 return r.first; 551 552 CVIndexMap &indexMap = r.first; 553 554 // Cross-compile warning: given that Clang doesn't generate LF_PRECOMP 555 // records, we assume the OBJ comes from a Windows build of cl.exe. Thusly, 556 // the paths embedded in the OBJs are in the Windows format. 557 SmallString<128> precompFileName = sys::path::filename( 558 precomp.getPrecompFilePath(), sys::path::Style::windows); 559 560 // link.exe requires that a precompiled headers object must always be provided 561 // on the command-line, even if that's not necessary. 562 auto precompFile = findObjByName(precompFileName); 563 if (!precompFile) 564 return createFileError( 565 precompFileName.str(), 566 make_error<pdb::PDBError>(pdb::pdb_error_code::external_cmdline_ref)); 567 568 addObjFile(precompFile, &indexMap); 569 570 if (!precompFile->pchSignature) 571 fatal(precompFile->getName() + " is not a precompiled headers object"); 572 573 if (precomp.getSignature() != precompFile->pchSignature.getValueOr(0)) 574 return createFileError( 575 precomp.getPrecompFilePath().str(), 576 make_error<pdb::PDBError>(pdb::pdb_error_code::signature_out_of_date)); 577 578 return indexMap; 579} 580 581static bool remapTypeIndex(TypeIndex &ti, ArrayRef<TypeIndex> typeIndexMap) { 582 if (ti.isSimple()) 583 return true; 584 if (ti.toArrayIndex() >= typeIndexMap.size()) 585 return false; 586 ti = typeIndexMap[ti.toArrayIndex()]; 587 return true; 588} 589 590static void remapTypesInSymbolRecord(ObjFile *file, SymbolKind symKind, 591 MutableArrayRef<uint8_t> recordBytes, 592 const CVIndexMap &indexMap, 593 ArrayRef<TiReference> typeRefs) { 594 MutableArrayRef<uint8_t> contents = 595 recordBytes.drop_front(sizeof(RecordPrefix)); 596 for (const TiReference &ref : typeRefs) { 597 unsigned byteSize = ref.Count * sizeof(TypeIndex); 598 if (contents.size() < ref.Offset + byteSize) 599 fatal("symbol record too short"); 600 601 // This can be an item index or a type index. Choose the appropriate map. 602 ArrayRef<TypeIndex> typeOrItemMap = indexMap.tpiMap; 603 bool isItemIndex = ref.Kind == TiRefKind::IndexRef; 604 if (isItemIndex && indexMap.isTypeServerMap) 605 typeOrItemMap = indexMap.ipiMap; 606 607 MutableArrayRef<TypeIndex> tIs( 608 reinterpret_cast<TypeIndex *>(contents.data() + ref.Offset), ref.Count); 609 for (TypeIndex &ti : tIs) { 610 if (!remapTypeIndex(ti, typeOrItemMap)) { 611 log("ignoring symbol record of kind 0x" + utohexstr(symKind) + " in " + 612 file->getName() + " with bad " + (isItemIndex ? "item" : "type") + 613 " index 0x" + utohexstr(ti.getIndex())); 614 ti = TypeIndex(SimpleTypeKind::NotTranslated); 615 continue; 616 } 617 } 618 } 619} 620 621static void 622recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> contents, 623 uint32_t offset, 624 std::vector<ulittle32_t *> &strTableRefs) { 625 contents = 626 contents.drop_front(offset).take_front(sizeof(support::ulittle32_t)); 627 ulittle32_t *index = reinterpret_cast<ulittle32_t *>(contents.data()); 628 strTableRefs.push_back(index); 629} 630 631static void 632recordStringTableReferences(SymbolKind kind, MutableArrayRef<uint8_t> contents, 633 std::vector<ulittle32_t *> &strTableRefs) { 634 // For now we only handle S_FILESTATIC, but we may need the same logic for 635 // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any 636 // PDBs that contain these types of records, so because of the uncertainty 637 // they are omitted here until we can prove that it's necessary. 638 switch (kind) { 639 case SymbolKind::S_FILESTATIC: 640 // FileStaticSym::ModFileOffset 641 recordStringTableReferenceAtOffset(contents, 8, strTableRefs); 642 break; 643 case SymbolKind::S_DEFRANGE: 644 case SymbolKind::S_DEFRANGE_SUBFIELD: 645 log("Not fixing up string table reference in S_DEFRANGE / " 646 "S_DEFRANGE_SUBFIELD record"); 647 break; 648 default: 649 break; 650 } 651} 652 653static SymbolKind symbolKind(ArrayRef<uint8_t> recordData) { 654 const RecordPrefix *prefix = 655 reinterpret_cast<const RecordPrefix *>(recordData.data()); 656 return static_cast<SymbolKind>(uint16_t(prefix->RecordKind)); 657} 658 659/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32 660static void translateIdSymbols(MutableArrayRef<uint8_t> &recordData, 661 TypeCollection &iDTable) { 662 RecordPrefix *prefix = reinterpret_cast<RecordPrefix *>(recordData.data()); 663 664 SymbolKind kind = symbolKind(recordData); 665 666 if (kind == SymbolKind::S_PROC_ID_END) { 667 prefix->RecordKind = SymbolKind::S_END; 668 return; 669 } 670 671 // In an object file, GPROC32_ID has an embedded reference which refers to the 672 // single object file type index namespace. This has already been translated 673 // to the PDB file's ID stream index space, but we need to convert this to a 674 // symbol that refers to the type stream index space. So we remap again from 675 // ID index space to type index space. 676 if (kind == SymbolKind::S_GPROC32_ID || kind == SymbolKind::S_LPROC32_ID) { 677 SmallVector<TiReference, 1> refs; 678 auto content = recordData.drop_front(sizeof(RecordPrefix)); 679 CVSymbol sym(recordData); 680 discoverTypeIndicesInSymbol(sym, refs); 681 assert(refs.size() == 1); 682 assert(refs.front().Count == 1); 683 684 TypeIndex *ti = 685 reinterpret_cast<TypeIndex *>(content.data() + refs[0].Offset); 686 // `ti` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in 687 // the IPI stream, whose `FunctionType` member refers to the TPI stream. 688 // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and 689 // in both cases we just need the second type index. 690 if (!ti->isSimple() && !ti->isNoneType()) { 691 CVType funcIdData = iDTable.getType(*ti); 692 SmallVector<TypeIndex, 2> indices; 693 discoverTypeIndices(funcIdData, indices); 694 assert(indices.size() == 2); 695 *ti = indices[1]; 696 } 697 698 kind = (kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32 699 : SymbolKind::S_LPROC32; 700 prefix->RecordKind = uint16_t(kind); 701 } 702} 703 704/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. 705/// The object file may not be aligned. 706static MutableArrayRef<uint8_t> 707copyAndAlignSymbol(const CVSymbol &sym, MutableArrayRef<uint8_t> &alignedMem) { 708 size_t size = alignTo(sym.length(), alignOf(CodeViewContainer::Pdb)); 709 assert(size >= 4 && "record too short"); 710 assert(size <= MaxRecordLength && "record too long"); 711 assert(alignedMem.size() >= size && "didn't preallocate enough"); 712 713 // Copy the symbol record and zero out any padding bytes. 714 MutableArrayRef<uint8_t> newData = alignedMem.take_front(size); 715 alignedMem = alignedMem.drop_front(size); 716 memcpy(newData.data(), sym.data().data(), sym.length()); 717 memset(newData.data() + sym.length(), 0, size - sym.length()); 718 719 // Update the record prefix length. It should point to the beginning of the 720 // next record. 721 auto *prefix = reinterpret_cast<RecordPrefix *>(newData.data()); 722 prefix->RecordLen = size - 2; 723 return newData; 724} 725 726struct ScopeRecord { 727 ulittle32_t ptrParent; 728 ulittle32_t ptrEnd; 729}; 730 731struct SymbolScope { 732 ScopeRecord *openingRecord; 733 uint32_t scopeOffset; 734}; 735 736static void scopeStackOpen(SmallVectorImpl<SymbolScope> &stack, 737 uint32_t curOffset, CVSymbol &sym) { 738 assert(symbolOpensScope(sym.kind())); 739 SymbolScope s; 740 s.scopeOffset = curOffset; 741 s.openingRecord = const_cast<ScopeRecord *>( 742 reinterpret_cast<const ScopeRecord *>(sym.content().data())); 743 s.openingRecord->ptrParent = stack.empty() ? 0 : stack.back().scopeOffset; 744 stack.push_back(s); 745} 746 747static void scopeStackClose(SmallVectorImpl<SymbolScope> &stack, 748 uint32_t curOffset, InputFile *file) { 749 if (stack.empty()) { 750 warn("symbol scopes are not balanced in " + file->getName()); 751 return; 752 } 753 SymbolScope s = stack.pop_back_val(); 754 s.openingRecord->ptrEnd = curOffset; 755} 756 757static bool symbolGoesInModuleStream(const CVSymbol &sym, bool isGlobalScope) { 758 switch (sym.kind()) { 759 case SymbolKind::S_GDATA32: 760 case SymbolKind::S_CONSTANT: 761 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 762 // since they are synthesized by the linker in response to S_GPROC32 and 763 // S_LPROC32, but if we do see them, don't put them in the module stream I 764 // guess. 765 case SymbolKind::S_PROCREF: 766 case SymbolKind::S_LPROCREF: 767 return false; 768 // S_UDT records go in the module stream if it is not a global S_UDT. 769 case SymbolKind::S_UDT: 770 return !isGlobalScope; 771 // S_GDATA32 does not go in the module stream, but S_LDATA32 does. 772 case SymbolKind::S_LDATA32: 773 default: 774 return true; 775 } 776} 777 778static bool symbolGoesInGlobalsStream(const CVSymbol &sym, bool isGlobalScope) { 779 switch (sym.kind()) { 780 case SymbolKind::S_CONSTANT: 781 case SymbolKind::S_GDATA32: 782 // S_LDATA32 goes in both the module stream and the globals stream. 783 case SymbolKind::S_LDATA32: 784 case SymbolKind::S_GPROC32: 785 case SymbolKind::S_LPROC32: 786 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place 787 // since they are synthesized by the linker in response to S_GPROC32 and 788 // S_LPROC32, but if we do see them, copy them straight through. 789 case SymbolKind::S_PROCREF: 790 case SymbolKind::S_LPROCREF: 791 return true; 792 // S_UDT records go in the globals stream if it is a global S_UDT. 793 case SymbolKind::S_UDT: 794 return isGlobalScope; 795 default: 796 return false; 797 } 798} 799 800static void addGlobalSymbol(pdb::GSIStreamBuilder &builder, uint16_t modIndex, 801 unsigned symOffset, const CVSymbol &sym) { 802 switch (sym.kind()) { 803 case SymbolKind::S_CONSTANT: 804 case SymbolKind::S_UDT: 805 case SymbolKind::S_GDATA32: 806 case SymbolKind::S_LDATA32: 807 case SymbolKind::S_PROCREF: 808 case SymbolKind::S_LPROCREF: 809 builder.addGlobalSymbol(sym); 810 break; 811 case SymbolKind::S_GPROC32: 812 case SymbolKind::S_LPROC32: { 813 SymbolRecordKind k = SymbolRecordKind::ProcRefSym; 814 if (sym.kind() == SymbolKind::S_LPROC32) 815 k = SymbolRecordKind::LocalProcRef; 816 ProcRefSym ps(k); 817 ps.Module = modIndex; 818 // For some reason, MSVC seems to add one to this value. 819 ++ps.Module; 820 ps.Name = getSymbolName(sym); 821 ps.SumName = 0; 822 ps.SymOffset = symOffset; 823 builder.addGlobalSymbol(ps); 824 break; 825 } 826 default: 827 llvm_unreachable("Invalid symbol kind!"); 828 } 829} 830 831void PDBLinker::mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap, 832 std::vector<ulittle32_t *> &stringTableRefs, 833 BinaryStreamRef symData) { 834 ArrayRef<uint8_t> symsBuffer; 835 cantFail(symData.readBytes(0, symData.getLength(), symsBuffer)); 836 SmallVector<SymbolScope, 4> scopes; 837 838 // Iterate every symbol to check if any need to be realigned, and if so, how 839 // much space we need to allocate for them. 840 bool needsRealignment = false; 841 unsigned totalRealignedSize = 0; 842 auto ec = forEachCodeViewRecord<CVSymbol>( 843 symsBuffer, [&](CVSymbol sym) -> llvm::Error { 844 unsigned realignedSize = 845 alignTo(sym.length(), alignOf(CodeViewContainer::Pdb)); 846 needsRealignment |= realignedSize != sym.length(); 847 totalRealignedSize += realignedSize; 848 return Error::success(); 849 }); 850 851 // If any of the symbol record lengths was corrupt, ignore them all, warn 852 // about it, and move on. 853 if (ec) { 854 warn("corrupt symbol records in " + file->getName()); 855 consumeError(std::move(ec)); 856 return; 857 } 858 859 // If any symbol needed realignment, allocate enough contiguous memory for 860 // them all. Typically symbol subsections are small enough that this will not 861 // cause fragmentation. 862 MutableArrayRef<uint8_t> alignedSymbolMem; 863 if (needsRealignment) { 864 void *alignedData = 865 alloc.Allocate(totalRealignedSize, alignOf(CodeViewContainer::Pdb)); 866 alignedSymbolMem = makeMutableArrayRef( 867 reinterpret_cast<uint8_t *>(alignedData), totalRealignedSize); 868 } 869 870 // Iterate again, this time doing the real work. 871 unsigned curSymOffset = file->moduleDBI->getNextSymbolOffset(); 872 ArrayRef<uint8_t> bulkSymbols; 873 cantFail(forEachCodeViewRecord<CVSymbol>( 874 symsBuffer, [&](CVSymbol sym) -> llvm::Error { 875 // Align the record if required. 876 MutableArrayRef<uint8_t> recordBytes; 877 if (needsRealignment) { 878 recordBytes = copyAndAlignSymbol(sym, alignedSymbolMem); 879 sym = CVSymbol(recordBytes); 880 } else { 881 // Otherwise, we can actually mutate the symbol directly, since we 882 // copied it to apply relocations. 883 recordBytes = makeMutableArrayRef( 884 const_cast<uint8_t *>(sym.data().data()), sym.length()); 885 } 886 887 // Discover type index references in the record. Skip it if we don't 888 // know where they are. 889 SmallVector<TiReference, 32> typeRefs; 890 if (!discoverTypeIndicesInSymbol(sym, typeRefs)) { 891 log("ignoring unknown symbol record with kind 0x" + 892 utohexstr(sym.kind())); 893 return Error::success(); 894 } 895 896 // Re-map all the type index references. 897 remapTypesInSymbolRecord(file, sym.kind(), recordBytes, indexMap, 898 typeRefs); 899 900 // An object file may have S_xxx_ID symbols, but these get converted to 901 // "real" symbols in a PDB. 902 translateIdSymbols(recordBytes, tMerger.getIDTable()); 903 sym = CVSymbol(recordBytes); 904 905 // If this record refers to an offset in the object file's string table, 906 // add that item to the global PDB string table and re-write the index. 907 recordStringTableReferences(sym.kind(), recordBytes, stringTableRefs); 908 909 // Fill in "Parent" and "End" fields by maintaining a stack of scopes. 910 if (symbolOpensScope(sym.kind())) 911 scopeStackOpen(scopes, curSymOffset, sym); 912 else if (symbolEndsScope(sym.kind())) 913 scopeStackClose(scopes, curSymOffset, file); 914 915 // Add the symbol to the globals stream if necessary. Do this before 916 // adding the symbol to the module since we may need to get the next 917 // symbol offset, and writing to the module's symbol stream will update 918 // that offset. 919 if (symbolGoesInGlobalsStream(sym, scopes.empty())) { 920 addGlobalSymbol(builder.getGsiBuilder(), 921 file->moduleDBI->getModuleIndex(), curSymOffset, sym); 922 ++globalSymbols; 923 } 924 925 if (symbolGoesInModuleStream(sym, scopes.empty())) { 926 // Add symbols to the module in bulk. If this symbol is contiguous 927 // with the previous run of symbols to add, combine the ranges. If 928 // not, close the previous range of symbols and start a new one. 929 if (sym.data().data() == bulkSymbols.end()) { 930 bulkSymbols = makeArrayRef(bulkSymbols.data(), 931 bulkSymbols.size() + sym.length()); 932 } else { 933 file->moduleDBI->addSymbolsInBulk(bulkSymbols); 934 bulkSymbols = recordBytes; 935 } 936 curSymOffset += sym.length(); 937 ++moduleSymbols; 938 } 939 return Error::success(); 940 })); 941 942 // Add any remaining symbols we've accumulated. 943 file->moduleDBI->addSymbolsInBulk(bulkSymbols); 944} 945 946// Allocate memory for a .debug$S / .debug$F section and relocate it. 947static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &alloc, 948 SectionChunk &debugChunk) { 949 uint8_t *buffer = alloc.Allocate<uint8_t>(debugChunk.getSize()); 950 assert(debugChunk.getOutputSectionIdx() == 0 && 951 "debug sections should not be in output sections"); 952 debugChunk.writeTo(buffer); 953 return makeArrayRef(buffer, debugChunk.getSize()); 954} 955 956static pdb::SectionContrib createSectionContrib(const Chunk *c, uint32_t modi) { 957 OutputSection *os = c ? c->getOutputSection() : nullptr; 958 pdb::SectionContrib sc; 959 memset(&sc, 0, sizeof(sc)); 960 sc.ISect = os ? os->sectionIndex : llvm::pdb::kInvalidStreamIndex; 961 sc.Off = c && os ? c->getRVA() - os->getRVA() : 0; 962 sc.Size = c ? c->getSize() : -1; 963 if (auto *secChunk = dyn_cast_or_null<SectionChunk>(c)) { 964 sc.Characteristics = secChunk->header->Characteristics; 965 sc.Imod = secChunk->file->moduleDBI->getModuleIndex(); 966 ArrayRef<uint8_t> contents = secChunk->getContents(); 967 JamCRC crc(0); 968 ArrayRef<char> charContents = makeArrayRef( 969 reinterpret_cast<const char *>(contents.data()), contents.size()); 970 crc.update(charContents); 971 sc.DataCrc = crc.getCRC(); 972 } else { 973 sc.Characteristics = os ? os->header.Characteristics : 0; 974 sc.Imod = modi; 975 } 976 sc.RelocCrc = 0; // FIXME 977 978 return sc; 979} 980 981static uint32_t 982translateStringTableIndex(uint32_t objIndex, 983 const DebugStringTableSubsectionRef &objStrTable, 984 DebugStringTableSubsection &pdbStrTable) { 985 auto expectedString = objStrTable.getString(objIndex); 986 if (!expectedString) { 987 warn("Invalid string table reference"); 988 consumeError(expectedString.takeError()); 989 return 0; 990 } 991 992 return pdbStrTable.insert(*expectedString); 993} 994 995void DebugSHandler::handleDebugS(lld::coff::SectionChunk &debugS) { 996 DebugSubsectionArray subsections; 997 998 ArrayRef<uint8_t> relocatedDebugContents = SectionChunk::consumeDebugMagic( 999 relocateDebugChunk(linker.alloc, debugS), debugS.getSectionName()); 1000 1001 BinaryStreamReader reader(relocatedDebugContents, support::little); 1002 exitOnErr(reader.readArray(subsections, relocatedDebugContents.size())); 1003 1004 for (const DebugSubsectionRecord &ss : subsections) { 1005 // Ignore subsections with the 'ignore' bit. Some versions of the Visual C++ 1006 // runtime have subsections with this bit set. 1007 if (uint32_t(ss.kind()) & codeview::SubsectionIgnoreFlag) 1008 continue; 1009 1010 switch (ss.kind()) { 1011 case DebugSubsectionKind::StringTable: { 1012 assert(!cVStrTab.valid() && 1013 "Encountered multiple string table subsections!"); 1014 exitOnErr(cVStrTab.initialize(ss.getRecordData())); 1015 break; 1016 } 1017 case DebugSubsectionKind::FileChecksums: 1018 assert(!checksums.valid() && 1019 "Encountered multiple checksum subsections!"); 1020 exitOnErr(checksums.initialize(ss.getRecordData())); 1021 break; 1022 case DebugSubsectionKind::Lines: 1023 // We can add the relocated line table directly to the PDB without 1024 // modification because the file checksum offsets will stay the same. 1025 file.moduleDBI->addDebugSubsection(ss); 1026 break; 1027 case DebugSubsectionKind::InlineeLines: 1028 assert(!inlineeLines.valid() && 1029 "Encountered multiple inlinee lines subsections!"); 1030 exitOnErr(inlineeLines.initialize(ss.getRecordData())); 1031 break; 1032 case DebugSubsectionKind::FrameData: { 1033 // We need to re-write string table indices here, so save off all 1034 // frame data subsections until we've processed the entire list of 1035 // subsections so that we can be sure we have the string table. 1036 DebugFrameDataSubsectionRef fds; 1037 exitOnErr(fds.initialize(ss.getRecordData())); 1038 newFpoFrames.push_back(std::move(fds)); 1039 break; 1040 } 1041 case DebugSubsectionKind::Symbols: { 1042 linker.mergeSymbolRecords(&file, indexMap, stringTableReferences, 1043 ss.getRecordData()); 1044 break; 1045 } 1046 1047 case DebugSubsectionKind::CrossScopeImports: 1048 case DebugSubsectionKind::CrossScopeExports: 1049 // These appear to relate to cross-module optimization, so we might use 1050 // these for ThinLTO. 1051 break; 1052 1053 case DebugSubsectionKind::ILLines: 1054 case DebugSubsectionKind::FuncMDTokenMap: 1055 case DebugSubsectionKind::TypeMDTokenMap: 1056 case DebugSubsectionKind::MergedAssemblyInput: 1057 // These appear to relate to .Net assembly info. 1058 break; 1059 1060 case DebugSubsectionKind::CoffSymbolRVA: 1061 // Unclear what this is for. 1062 break; 1063 1064 default: 1065 warn("ignoring unknown debug$S subsection kind 0x" + 1066 utohexstr(uint32_t(ss.kind())) + " in file " + toString(&file)); 1067 break; 1068 } 1069 } 1070} 1071 1072static Expected<StringRef> 1073getFileName(const DebugStringTableSubsectionRef &strings, 1074 const DebugChecksumsSubsectionRef &checksums, uint32_t fileID) { 1075 auto iter = checksums.getArray().at(fileID); 1076 if (iter == checksums.getArray().end()) 1077 return make_error<CodeViewError>(cv_error_code::no_records); 1078 uint32_t offset = iter->FileNameOffset; 1079 return strings.getString(offset); 1080} 1081 1082std::shared_ptr<DebugInlineeLinesSubsection> 1083DebugSHandler::mergeInlineeLines(DebugChecksumsSubsection *newChecksums) { 1084 auto newInlineeLines = std::make_shared<DebugInlineeLinesSubsection>( 1085 *newChecksums, inlineeLines.hasExtraFiles()); 1086 1087 for (const InlineeSourceLine &line : inlineeLines) { 1088 TypeIndex inlinee = line.Header->Inlinee; 1089 uint32_t fileID = line.Header->FileID; 1090 uint32_t sourceLine = line.Header->SourceLineNum; 1091 1092 ArrayRef<TypeIndex> typeOrItemMap = 1093 indexMap.isTypeServerMap ? indexMap.ipiMap : indexMap.tpiMap; 1094 if (!remapTypeIndex(inlinee, typeOrItemMap)) { 1095 log("ignoring inlinee line record in " + file.getName() + 1096 " with bad inlinee index 0x" + utohexstr(inlinee.getIndex())); 1097 continue; 1098 } 1099 1100 SmallString<128> filename = 1101 exitOnErr(getFileName(cVStrTab, checksums, fileID)); 1102 pdbMakeAbsolute(filename); 1103 newInlineeLines->addInlineSite(inlinee, filename, sourceLine); 1104 1105 if (inlineeLines.hasExtraFiles()) { 1106 for (uint32_t extraFileId : line.ExtraFiles) { 1107 filename = exitOnErr(getFileName(cVStrTab, checksums, extraFileId)); 1108 pdbMakeAbsolute(filename); 1109 newInlineeLines->addExtraFile(filename); 1110 } 1111 } 1112 } 1113 1114 return newInlineeLines; 1115} 1116 1117void DebugSHandler::finish() { 1118 pdb::DbiStreamBuilder &dbiBuilder = linker.builder.getDbiBuilder(); 1119 1120 // We should have seen all debug subsections across the entire object file now 1121 // which means that if a StringTable subsection and Checksums subsection were 1122 // present, now is the time to handle them. 1123 if (!cVStrTab.valid()) { 1124 if (checksums.valid()) 1125 fatal(".debug$S sections with a checksums subsection must also contain a " 1126 "string table subsection"); 1127 1128 if (!stringTableReferences.empty()) 1129 warn("No StringTable subsection was encountered, but there are string " 1130 "table references"); 1131 return; 1132 } 1133 1134 // Rewrite string table indices in the Fpo Data and symbol records to refer to 1135 // the global PDB string table instead of the object file string table. 1136 for (DebugFrameDataSubsectionRef &fds : newFpoFrames) { 1137 const ulittle32_t *reloc = fds.getRelocPtr(); 1138 for (codeview::FrameData fd : fds) { 1139 fd.RvaStart += *reloc; 1140 fd.FrameFunc = 1141 translateStringTableIndex(fd.FrameFunc, cVStrTab, linker.pdbStrTab); 1142 dbiBuilder.addNewFpoData(fd); 1143 } 1144 } 1145 1146 for (ulittle32_t *ref : stringTableReferences) 1147 *ref = translateStringTableIndex(*ref, cVStrTab, linker.pdbStrTab); 1148 1149 // Make a new file checksum table that refers to offsets in the PDB-wide 1150 // string table. Generally the string table subsection appears after the 1151 // checksum table, so we have to do this after looping over all the 1152 // subsections. 1153 auto newChecksums = make_unique<DebugChecksumsSubsection>(linker.pdbStrTab); 1154 for (FileChecksumEntry &fc : checksums) { 1155 SmallString<128> filename = 1156 exitOnErr(cVStrTab.getString(fc.FileNameOffset)); 1157 pdbMakeAbsolute(filename); 1158 exitOnErr(dbiBuilder.addModuleSourceFile(*file.moduleDBI, filename)); 1159 newChecksums->addChecksum(filename, fc.Kind, fc.Checksum); 1160 } 1161 1162 // Rewrite inlinee item indices if present. 1163 if (inlineeLines.valid()) 1164 file.moduleDBI->addDebugSubsection(mergeInlineeLines(newChecksums.get())); 1165 1166 file.moduleDBI->addDebugSubsection(std::move(newChecksums)); 1167} 1168 1169void PDBLinker::addObjFile(ObjFile *file, CVIndexMap *externIndexMap) { 1170 if (file->mergedIntoPDB) 1171 return; 1172 file->mergedIntoPDB = true; 1173 1174 // Before we can process symbol substreams from .debug$S, we need to process 1175 // type information, file checksums, and the string table. Add type info to 1176 // the PDB first, so that we can get the map from object file type and item 1177 // indices to PDB type and item indices. 1178 CVIndexMap objectIndexMap; 1179 auto indexMapResult = 1180 mergeDebugT(file, externIndexMap ? externIndexMap : &objectIndexMap); 1181 1182 // If the .debug$T sections fail to merge, assume there is no debug info. 1183 if (!indexMapResult) { 1184 if (!config->warnDebugInfoUnusable) { 1185 consumeError(indexMapResult.takeError()); 1186 return; 1187 } 1188 warn("Cannot use debug info for '" + toString(file) + "' [LNK4099]\n" + 1189 ">>> failed to load reference " + 1190 StringRef(toString(indexMapResult.takeError()))); 1191 return; 1192 } 1193 1194 ScopedTimer t(symbolMergingTimer); 1195 1196 pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1197 DebugSHandler dsh(*this, *file, *indexMapResult); 1198 // Now do all live .debug$S and .debug$F sections. 1199 for (SectionChunk *debugChunk : file->getDebugChunks()) { 1200 if (!debugChunk->live || debugChunk->getSize() == 0) 1201 continue; 1202 1203 if (debugChunk->getSectionName() == ".debug$S") { 1204 dsh.handleDebugS(*debugChunk); 1205 continue; 1206 } 1207 1208 if (debugChunk->getSectionName() == ".debug$F") { 1209 ArrayRef<uint8_t> relocatedDebugContents = 1210 relocateDebugChunk(alloc, *debugChunk); 1211 1212 FixedStreamArray<object::FpoData> fpoRecords; 1213 BinaryStreamReader reader(relocatedDebugContents, support::little); 1214 uint32_t count = relocatedDebugContents.size() / sizeof(object::FpoData); 1215 exitOnErr(reader.readArray(fpoRecords, count)); 1216 1217 // These are already relocated and don't refer to the string table, so we 1218 // can just copy it. 1219 for (const object::FpoData &fd : fpoRecords) 1220 dbiBuilder.addOldFpoData(fd); 1221 continue; 1222 } 1223 } 1224 1225 // Do any post-processing now that all .debug$S sections have been processed. 1226 dsh.finish(); 1227} 1228 1229// Add a module descriptor for every object file. We need to put an absolute 1230// path to the object into the PDB. If this is a plain object, we make its 1231// path absolute. If it's an object in an archive, we make the archive path 1232// absolute. 1233static void createModuleDBI(pdb::PDBFileBuilder &builder) { 1234 pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1235 SmallString<128> objName; 1236 1237 for (ObjFile *file : ObjFile::instances) { 1238 1239 bool inArchive = !file->parentName.empty(); 1240 objName = inArchive ? file->parentName : file->getName(); 1241 pdbMakeAbsolute(objName); 1242 StringRef modName = inArchive ? file->getName() : StringRef(objName); 1243 1244 file->moduleDBI = &exitOnErr(dbiBuilder.addModuleInfo(modName)); 1245 file->moduleDBI->setObjFileName(objName); 1246 1247 ArrayRef<Chunk *> chunks = file->getChunks(); 1248 uint32_t modi = file->moduleDBI->getModuleIndex(); 1249 1250 for (Chunk *c : chunks) { 1251 auto *secChunk = dyn_cast<SectionChunk>(c); 1252 if (!secChunk || !secChunk->live) 1253 continue; 1254 pdb::SectionContrib sc = createSectionContrib(secChunk, modi); 1255 file->moduleDBI->setFirstSectionContrib(sc); 1256 break; 1257 } 1258 } 1259} 1260 1261static PublicSym32 createPublic(Defined *def) { 1262 PublicSym32 pub(SymbolKind::S_PUB32); 1263 pub.Name = def->getName(); 1264 if (auto *d = dyn_cast<DefinedCOFF>(def)) { 1265 if (d->getCOFFSymbol().isFunctionDefinition()) 1266 pub.Flags = PublicSymFlags::Function; 1267 } else if (isa<DefinedImportThunk>(def)) { 1268 pub.Flags = PublicSymFlags::Function; 1269 } 1270 1271 OutputSection *os = def->getChunk()->getOutputSection(); 1272 assert(os && "all publics should be in final image"); 1273 pub.Offset = def->getRVA() - os->getRVA(); 1274 pub.Segment = os->sectionIndex; 1275 return pub; 1276} 1277 1278// Add all object files to the PDB. Merge .debug$T sections into IpiData and 1279// TpiData. 1280void PDBLinker::addObjectsToPDB() { 1281 ScopedTimer t1(addObjectsTimer); 1282 1283 createModuleDBI(builder); 1284 1285 for (ObjFile *file : ObjFile::instances) 1286 addObjFile(file); 1287 1288 builder.getStringTableBuilder().setStrings(pdbStrTab); 1289 t1.stop(); 1290 1291 // Construct TPI and IPI stream contents. 1292 ScopedTimer t2(tpiStreamLayoutTimer); 1293 addTypeInfo(builder.getTpiBuilder(), tMerger.getTypeTable()); 1294 addTypeInfo(builder.getIpiBuilder(), tMerger.getIDTable()); 1295 t2.stop(); 1296 1297 ScopedTimer t3(globalsLayoutTimer); 1298 // Compute the public and global symbols. 1299 auto &gsiBuilder = builder.getGsiBuilder(); 1300 std::vector<PublicSym32> publics; 1301 symtab->forEachSymbol([&publics](Symbol *s) { 1302 // Only emit defined, live symbols that have a chunk. 1303 auto *def = dyn_cast<Defined>(s); 1304 if (def && def->isLive() && def->getChunk()) 1305 publics.push_back(createPublic(def)); 1306 }); 1307 1308 if (!publics.empty()) { 1309 publicSymbols = publics.size(); 1310 // Sort the public symbols and add them to the stream. 1311 parallelSort(publics, [](const PublicSym32 &l, const PublicSym32 &r) { 1312 return l.Name < r.Name; 1313 }); 1314 for (const PublicSym32 &pub : publics) 1315 gsiBuilder.addPublicSymbol(pub); 1316 } 1317} 1318 1319void PDBLinker::printStats() { 1320 if (!config->showSummary) 1321 return; 1322 1323 SmallString<256> buffer; 1324 raw_svector_ostream stream(buffer); 1325 1326 stream << center_justify("Summary", 80) << '\n' 1327 << std::string(80, '-') << '\n'; 1328 1329 auto print = [&](uint64_t v, StringRef s) { 1330 stream << format_decimal(v, 15) << " " << s << '\n'; 1331 }; 1332 1333 print(ObjFile::instances.size(), 1334 "Input OBJ files (expanded from all cmd-line inputs)"); 1335 print(typeServerIndexMappings.size(), "PDB type server dependencies"); 1336 print(precompTypeIndexMappings.size(), "Precomp OBJ dependencies"); 1337 print(tMerger.getTypeTable().size() + tMerger.getIDTable().size(), 1338 "Merged TPI records"); 1339 print(pdbStrTab.size(), "Output PDB strings"); 1340 print(globalSymbols, "Global symbol records"); 1341 print(moduleSymbols, "Module symbol records"); 1342 print(publicSymbols, "Public symbol records"); 1343 1344 message(buffer); 1345} 1346 1347void PDBLinker::addNatvisFiles() { 1348 for (StringRef file : config->natvisFiles) { 1349 ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr = 1350 MemoryBuffer::getFile(file); 1351 if (!dataOrErr) { 1352 warn("Cannot open input file: " + file); 1353 continue; 1354 } 1355 builder.addInjectedSource(file, std::move(*dataOrErr)); 1356 } 1357} 1358 1359static codeview::CPUType toCodeViewMachine(COFF::MachineTypes machine) { 1360 switch (machine) { 1361 case COFF::IMAGE_FILE_MACHINE_AMD64: 1362 return codeview::CPUType::X64; 1363 case COFF::IMAGE_FILE_MACHINE_ARM: 1364 return codeview::CPUType::ARM7; 1365 case COFF::IMAGE_FILE_MACHINE_ARM64: 1366 return codeview::CPUType::ARM64; 1367 case COFF::IMAGE_FILE_MACHINE_ARMNT: 1368 return codeview::CPUType::ARMNT; 1369 case COFF::IMAGE_FILE_MACHINE_I386: 1370 return codeview::CPUType::Intel80386; 1371 default: 1372 llvm_unreachable("Unsupported CPU Type"); 1373 } 1374} 1375 1376// Mimic MSVC which surrounds arguments containing whitespace with quotes. 1377// Double double-quotes are handled, so that the resulting string can be 1378// executed again on the cmd-line. 1379static std::string quote(ArrayRef<StringRef> args) { 1380 std::string r; 1381 r.reserve(256); 1382 for (StringRef a : args) { 1383 if (!r.empty()) 1384 r.push_back(' '); 1385 bool hasWS = a.find(' ') != StringRef::npos; 1386 bool hasQ = a.find('"') != StringRef::npos; 1387 if (hasWS || hasQ) 1388 r.push_back('"'); 1389 if (hasQ) { 1390 SmallVector<StringRef, 4> s; 1391 a.split(s, '"'); 1392 r.append(join(s, "\"\"")); 1393 } else { 1394 r.append(a); 1395 } 1396 if (hasWS || hasQ) 1397 r.push_back('"'); 1398 } 1399 return r; 1400} 1401 1402static void fillLinkerVerRecord(Compile3Sym &cs) { 1403 cs.Machine = toCodeViewMachine(config->machine); 1404 // Interestingly, if we set the string to 0.0.0.0, then when trying to view 1405 // local variables WinDbg emits an error that private symbols are not present. 1406 // By setting this to a valid MSVC linker version string, local variables are 1407 // displayed properly. As such, even though it is not representative of 1408 // LLVM's version information, we need this for compatibility. 1409 cs.Flags = CompileSym3Flags::None; 1410 cs.VersionBackendBuild = 25019; 1411 cs.VersionBackendMajor = 14; 1412 cs.VersionBackendMinor = 10; 1413 cs.VersionBackendQFE = 0; 1414 1415 // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the 1416 // linker module (which is by definition a backend), so we don't need to do 1417 // anything here. Also, it seems we can use "LLVM Linker" for the linker name 1418 // without any problems. Only the backend version has to be hardcoded to a 1419 // magic number. 1420 cs.VersionFrontendBuild = 0; 1421 cs.VersionFrontendMajor = 0; 1422 cs.VersionFrontendMinor = 0; 1423 cs.VersionFrontendQFE = 0; 1424 cs.Version = "LLVM Linker"; 1425 cs.setLanguage(SourceLanguage::Link); 1426} 1427 1428static void addCommonLinkerModuleSymbols(StringRef path, 1429 pdb::DbiModuleDescriptorBuilder &mod, 1430 BumpPtrAllocator &allocator) { 1431 ObjNameSym ons(SymbolRecordKind::ObjNameSym); 1432 EnvBlockSym ebs(SymbolRecordKind::EnvBlockSym); 1433 Compile3Sym cs(SymbolRecordKind::Compile3Sym); 1434 fillLinkerVerRecord(cs); 1435 1436 ons.Name = "* Linker *"; 1437 ons.Signature = 0; 1438 1439 ArrayRef<StringRef> args = makeArrayRef(config->argv).drop_front(); 1440 std::string argStr = quote(args); 1441 ebs.Fields.push_back("cwd"); 1442 SmallString<64> cwd; 1443 if (config->pdbSourcePath.empty()) 1444 sys::fs::current_path(cwd); 1445 else 1446 cwd = config->pdbSourcePath; 1447 ebs.Fields.push_back(cwd); 1448 ebs.Fields.push_back("exe"); 1449 SmallString<64> exe = config->argv[0]; 1450 pdbMakeAbsolute(exe); 1451 ebs.Fields.push_back(exe); 1452 ebs.Fields.push_back("pdb"); 1453 ebs.Fields.push_back(path); 1454 ebs.Fields.push_back("cmd"); 1455 ebs.Fields.push_back(argStr); 1456 mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1457 ons, allocator, CodeViewContainer::Pdb)); 1458 mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1459 cs, allocator, CodeViewContainer::Pdb)); 1460 mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1461 ebs, allocator, CodeViewContainer::Pdb)); 1462} 1463 1464static void addLinkerModuleCoffGroup(PartialSection *sec, 1465 pdb::DbiModuleDescriptorBuilder &mod, 1466 OutputSection &os, 1467 BumpPtrAllocator &allocator) { 1468 // If there's a section, there's at least one chunk 1469 assert(!sec->chunks.empty()); 1470 const Chunk *firstChunk = *sec->chunks.begin(); 1471 const Chunk *lastChunk = *sec->chunks.rbegin(); 1472 1473 // Emit COFF group 1474 CoffGroupSym cgs(SymbolRecordKind::CoffGroupSym); 1475 cgs.Name = sec->name; 1476 cgs.Segment = os.sectionIndex; 1477 cgs.Offset = firstChunk->getRVA() - os.getRVA(); 1478 cgs.Size = lastChunk->getRVA() + lastChunk->getSize() - firstChunk->getRVA(); 1479 cgs.Characteristics = sec->characteristics; 1480 1481 // Somehow .idata sections & sections groups in the debug symbol stream have 1482 // the "write" flag set. However the section header for the corresponding 1483 // .idata section doesn't have it. 1484 if (cgs.Name.startswith(".idata")) 1485 cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE; 1486 1487 mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1488 cgs, allocator, CodeViewContainer::Pdb)); 1489} 1490 1491static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &mod, 1492 OutputSection &os, 1493 BumpPtrAllocator &allocator) { 1494 SectionSym sym(SymbolRecordKind::SectionSym); 1495 sym.Alignment = 12; // 2^12 = 4KB 1496 sym.Characteristics = os.header.Characteristics; 1497 sym.Length = os.getVirtualSize(); 1498 sym.Name = os.name; 1499 sym.Rva = os.getRVA(); 1500 sym.SectionNumber = os.sectionIndex; 1501 mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1502 sym, allocator, CodeViewContainer::Pdb)); 1503 1504 // Skip COFF groups in MinGW because it adds a significant footprint to the 1505 // PDB, due to each function being in its own section 1506 if (config->mingw) 1507 return; 1508 1509 // Output COFF groups for individual chunks of this section. 1510 for (PartialSection *sec : os.contribSections) { 1511 addLinkerModuleCoffGroup(sec, mod, os, allocator); 1512 } 1513} 1514 1515// Add all import files as modules to the PDB. 1516void PDBLinker::addImportFilesToPDB(ArrayRef<OutputSection *> outputSections) { 1517 if (ImportFile::instances.empty()) 1518 return; 1519 1520 std::map<std::string, llvm::pdb::DbiModuleDescriptorBuilder *> dllToModuleDbi; 1521 1522 for (ImportFile *file : ImportFile::instances) { 1523 if (!file->live) 1524 continue; 1525 1526 if (!file->thunkSym) 1527 continue; 1528 1529 if (!file->thunkLive) 1530 continue; 1531 1532 std::string dll = StringRef(file->dllName).lower(); 1533 llvm::pdb::DbiModuleDescriptorBuilder *&mod = dllToModuleDbi[dll]; 1534 if (!mod) { 1535 pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1536 SmallString<128> libPath = file->parentName; 1537 pdbMakeAbsolute(libPath); 1538 sys::path::native(libPath); 1539 1540 // Name modules similar to MSVC's link.exe. 1541 // The first module is the simple dll filename 1542 llvm::pdb::DbiModuleDescriptorBuilder &firstMod = 1543 exitOnErr(dbiBuilder.addModuleInfo(file->dllName)); 1544 firstMod.setObjFileName(libPath); 1545 pdb::SectionContrib sc = 1546 createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex); 1547 firstMod.setFirstSectionContrib(sc); 1548 1549 // The second module is where the import stream goes. 1550 mod = &exitOnErr(dbiBuilder.addModuleInfo("Import:" + file->dllName)); 1551 mod->setObjFileName(libPath); 1552 } 1553 1554 DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym); 1555 Chunk *thunkChunk = thunk->getChunk(); 1556 OutputSection *thunkOS = thunkChunk->getOutputSection(); 1557 1558 ObjNameSym ons(SymbolRecordKind::ObjNameSym); 1559 Compile3Sym cs(SymbolRecordKind::Compile3Sym); 1560 Thunk32Sym ts(SymbolRecordKind::Thunk32Sym); 1561 ScopeEndSym es(SymbolRecordKind::ScopeEndSym); 1562 1563 ons.Name = file->dllName; 1564 ons.Signature = 0; 1565 1566 fillLinkerVerRecord(cs); 1567 1568 ts.Name = thunk->getName(); 1569 ts.Parent = 0; 1570 ts.End = 0; 1571 ts.Next = 0; 1572 ts.Thunk = ThunkOrdinal::Standard; 1573 ts.Length = thunkChunk->getSize(); 1574 ts.Segment = thunkOS->sectionIndex; 1575 ts.Offset = thunkChunk->getRVA() - thunkOS->getRVA(); 1576 1577 mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1578 ons, alloc, CodeViewContainer::Pdb)); 1579 mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol( 1580 cs, alloc, CodeViewContainer::Pdb)); 1581 1582 SmallVector<SymbolScope, 4> scopes; 1583 CVSymbol newSym = codeview::SymbolSerializer::writeOneSymbol( 1584 ts, alloc, CodeViewContainer::Pdb); 1585 scopeStackOpen(scopes, mod->getNextSymbolOffset(), newSym); 1586 1587 mod->addSymbol(newSym); 1588 1589 newSym = codeview::SymbolSerializer::writeOneSymbol(es, alloc, 1590 CodeViewContainer::Pdb); 1591 scopeStackClose(scopes, mod->getNextSymbolOffset(), file); 1592 1593 mod->addSymbol(newSym); 1594 1595 pdb::SectionContrib sc = 1596 createSectionContrib(thunk->getChunk(), mod->getModuleIndex()); 1597 mod->setFirstSectionContrib(sc); 1598 } 1599} 1600 1601// Creates a PDB file. 1602void coff::createPDB(SymbolTable *symtab, 1603 ArrayRef<OutputSection *> outputSections, 1604 ArrayRef<uint8_t> sectionTable, 1605 llvm::codeview::DebugInfo *buildId) { 1606 ScopedTimer t1(totalPdbLinkTimer); 1607 PDBLinker pdb(symtab); 1608 1609 pdb.initialize(buildId); 1610 pdb.addObjectsToPDB(); 1611 pdb.addImportFilesToPDB(outputSections); 1612 pdb.addSections(outputSections, sectionTable); 1613 pdb.addNatvisFiles(); 1614 1615 ScopedTimer t2(diskCommitTimer); 1616 codeview::GUID guid; 1617 pdb.commit(&guid); 1618 memcpy(&buildId->PDB70.Signature, &guid, 16); 1619 1620 t2.stop(); 1621 t1.stop(); 1622 pdb.printStats(); 1623} 1624 1625void PDBLinker::initialize(llvm::codeview::DebugInfo *buildId) { 1626 exitOnErr(builder.initialize(4096)); // 4096 is blocksize 1627 1628 buildId->Signature.CVSignature = OMF::Signature::PDB70; 1629 // Signature is set to a hash of the PDB contents when the PDB is done. 1630 memset(buildId->PDB70.Signature, 0, 16); 1631 buildId->PDB70.Age = 1; 1632 1633 // Create streams in MSF for predefined streams, namely 1634 // PDB, TPI, DBI and IPI. 1635 for (int i = 0; i < (int)pdb::kSpecialStreamCount; ++i) 1636 exitOnErr(builder.getMsfBuilder().addStream(0)); 1637 1638 // Add an Info stream. 1639 auto &infoBuilder = builder.getInfoBuilder(); 1640 infoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); 1641 infoBuilder.setHashPDBContentsToGUID(true); 1642 1643 // Add an empty DBI stream. 1644 pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1645 dbiBuilder.setAge(buildId->PDB70.Age); 1646 dbiBuilder.setVersionHeader(pdb::PdbDbiV70); 1647 dbiBuilder.setMachineType(config->machine); 1648 // Technically we are not link.exe 14.11, but there are known cases where 1649 // debugging tools on Windows expect Microsoft-specific version numbers or 1650 // they fail to work at all. Since we know we produce PDBs that are 1651 // compatible with LINK 14.11, we set that version number here. 1652 dbiBuilder.setBuildNumber(14, 11); 1653} 1654 1655void PDBLinker::addSections(ArrayRef<OutputSection *> outputSections, 1656 ArrayRef<uint8_t> sectionTable) { 1657 // It's not entirely clear what this is, but the * Linker * module uses it. 1658 pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder(); 1659 nativePath = config->pdbPath; 1660 pdbMakeAbsolute(nativePath); 1661 uint32_t pdbFilePathNI = dbiBuilder.addECName(nativePath); 1662 auto &linkerModule = exitOnErr(dbiBuilder.addModuleInfo("* Linker *")); 1663 linkerModule.setPdbFilePathNI(pdbFilePathNI); 1664 addCommonLinkerModuleSymbols(nativePath, linkerModule, alloc); 1665 1666 // Add section contributions. They must be ordered by ascending RVA. 1667 for (OutputSection *os : outputSections) { 1668 addLinkerModuleSectionSymbol(linkerModule, *os, alloc); 1669 for (Chunk *c : os->chunks) { 1670 pdb::SectionContrib sc = 1671 createSectionContrib(c, linkerModule.getModuleIndex()); 1672 builder.getDbiBuilder().addSectionContrib(sc); 1673 } 1674 } 1675 1676 // The * Linker * first section contrib is only used along with /INCREMENTAL, 1677 // to provide trampolines thunks for incremental function patching. Set this 1678 // as "unused" because LLD doesn't support /INCREMENTAL link. 1679 pdb::SectionContrib sc = 1680 createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex); 1681 linkerModule.setFirstSectionContrib(sc); 1682 1683 // Add Section Map stream. 1684 ArrayRef<object::coff_section> sections = { 1685 (const object::coff_section *)sectionTable.data(), 1686 sectionTable.size() / sizeof(object::coff_section)}; 1687 sectionMap = pdb::DbiStreamBuilder::createSectionMap(sections); 1688 dbiBuilder.setSectionMap(sectionMap); 1689 1690 // Add COFF section header stream. 1691 exitOnErr( 1692 dbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, sectionTable)); 1693} 1694 1695void PDBLinker::commit(codeview::GUID *guid) { 1696 // Write to a file. 1697 exitOnErr(builder.commit(config->pdbPath, guid)); 1698} 1699 1700static uint32_t getSecrelReloc() { 1701 switch (config->machine) { 1702 case AMD64: 1703 return COFF::IMAGE_REL_AMD64_SECREL; 1704 case I386: 1705 return COFF::IMAGE_REL_I386_SECREL; 1706 case ARMNT: 1707 return COFF::IMAGE_REL_ARM_SECREL; 1708 case ARM64: 1709 return COFF::IMAGE_REL_ARM64_SECREL; 1710 default: 1711 llvm_unreachable("unknown machine type"); 1712 } 1713} 1714 1715// Try to find a line table for the given offset Addr into the given chunk C. 1716// If a line table was found, the line table, the string and checksum tables 1717// that are used to interpret the line table, and the offset of Addr in the line 1718// table are stored in the output arguments. Returns whether a line table was 1719// found. 1720static bool findLineTable(const SectionChunk *c, uint32_t addr, 1721 DebugStringTableSubsectionRef &cVStrTab, 1722 DebugChecksumsSubsectionRef &checksums, 1723 DebugLinesSubsectionRef &lines, 1724 uint32_t &offsetInLinetable) { 1725 ExitOnError exitOnErr; 1726 uint32_t secrelReloc = getSecrelReloc(); 1727 1728 for (SectionChunk *dbgC : c->file->getDebugChunks()) { 1729 if (dbgC->getSectionName() != ".debug$S") 1730 continue; 1731 1732 // Build a mapping of SECREL relocations in dbgC that refer to `c`. 1733 DenseMap<uint32_t, uint32_t> secrels; 1734 for (const coff_relocation &r : dbgC->getRelocs()) { 1735 if (r.Type != secrelReloc) 1736 continue; 1737 1738 if (auto *s = dyn_cast_or_null<DefinedRegular>( 1739 c->file->getSymbols()[r.SymbolTableIndex])) 1740 if (s->getChunk() == c) 1741 secrels[r.VirtualAddress] = s->getValue(); 1742 } 1743 1744 ArrayRef<uint8_t> contents = 1745 SectionChunk::consumeDebugMagic(dbgC->getContents(), ".debug$S"); 1746 DebugSubsectionArray subsections; 1747 BinaryStreamReader reader(contents, support::little); 1748 exitOnErr(reader.readArray(subsections, contents.size())); 1749 1750 for (const DebugSubsectionRecord &ss : subsections) { 1751 switch (ss.kind()) { 1752 case DebugSubsectionKind::StringTable: { 1753 assert(!cVStrTab.valid() && 1754 "Encountered multiple string table subsections!"); 1755 exitOnErr(cVStrTab.initialize(ss.getRecordData())); 1756 break; 1757 } 1758 case DebugSubsectionKind::FileChecksums: 1759 assert(!checksums.valid() && 1760 "Encountered multiple checksum subsections!"); 1761 exitOnErr(checksums.initialize(ss.getRecordData())); 1762 break; 1763 case DebugSubsectionKind::Lines: { 1764 ArrayRef<uint8_t> bytes; 1765 auto ref = ss.getRecordData(); 1766 exitOnErr(ref.readLongestContiguousChunk(0, bytes)); 1767 size_t offsetInDbgC = bytes.data() - dbgC->getContents().data(); 1768 1769 // Check whether this line table refers to C. 1770 auto i = secrels.find(offsetInDbgC); 1771 if (i == secrels.end()) 1772 break; 1773 1774 // Check whether this line table covers Addr in C. 1775 DebugLinesSubsectionRef linesTmp; 1776 exitOnErr(linesTmp.initialize(BinaryStreamReader(ref))); 1777 uint32_t offsetInC = i->second + linesTmp.header()->RelocOffset; 1778 if (addr < offsetInC || addr >= offsetInC + linesTmp.header()->CodeSize) 1779 break; 1780 1781 assert(!lines.header() && 1782 "Encountered multiple line tables for function!"); 1783 exitOnErr(lines.initialize(BinaryStreamReader(ref))); 1784 offsetInLinetable = addr - offsetInC; 1785 break; 1786 } 1787 default: 1788 break; 1789 } 1790 1791 if (cVStrTab.valid() && checksums.valid() && lines.header()) 1792 return true; 1793 } 1794 } 1795 1796 return false; 1797} 1798 1799// Use CodeView line tables to resolve a file and line number for the given 1800// offset into the given chunk and return them, or {"", 0} if a line table was 1801// not found. 1802std::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *c, 1803 uint32_t addr) { 1804 ExitOnError exitOnErr; 1805 1806 DebugStringTableSubsectionRef cVStrTab; 1807 DebugChecksumsSubsectionRef checksums; 1808 DebugLinesSubsectionRef lines; 1809 uint32_t offsetInLinetable; 1810 1811 if (!findLineTable(c, addr, cVStrTab, checksums, lines, offsetInLinetable)) 1812 return {"", 0}; 1813 1814 Optional<uint32_t> nameIndex; 1815 Optional<uint32_t> lineNumber; 1816 for (LineColumnEntry &entry : lines) { 1817 for (const LineNumberEntry &ln : entry.LineNumbers) { 1818 LineInfo li(ln.Flags); 1819 if (ln.Offset > offsetInLinetable) { 1820 if (!nameIndex) { 1821 nameIndex = entry.NameIndex; 1822 lineNumber = li.getStartLine(); 1823 } 1824 StringRef filename = 1825 exitOnErr(getFileName(cVStrTab, checksums, *nameIndex)); 1826 return {filename, *lineNumber}; 1827 } 1828 nameIndex = entry.NameIndex; 1829 lineNumber = li.getStartLine(); 1830 } 1831 } 1832 if (!nameIndex) 1833 return {"", 0}; 1834 StringRef filename = exitOnErr(getFileName(cVStrTab, checksums, *nameIndex)); 1835 return {filename, *lineNumber}; 1836} 1837