1 //===- Config.h -------------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_ELF_CONFIG_H 10 #define LLD_ELF_CONFIG_H 11 12 #include "lld/Common/CommonLinkerContext.h" 13 #include "lld/Common/ErrorHandler.h" 14 #include "llvm/ADT/CachedHashString.h" 15 #include "llvm/ADT/DenseSet.h" 16 #include "llvm/ADT/MapVector.h" 17 #include "llvm/ADT/SetVector.h" 18 #include "llvm/ADT/SmallSet.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/StringSet.h" 21 #include "llvm/BinaryFormat/ELF.h" 22 #include "llvm/Option/ArgList.h" 23 #include "llvm/Support/CachePruning.h" 24 #include "llvm/Support/CodeGen.h" 25 #include "llvm/Support/Compiler.h" 26 #include "llvm/Support/Compression.h" 27 #include "llvm/Support/Endian.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/GlobPattern.h" 30 #include "llvm/Support/TarWriter.h" 31 #include <atomic> 32 #include <memory> 33 #include <mutex> 34 #include <optional> 35 #include <vector> 36 37 namespace lld::elf { 38 39 class InputFile; 40 class BinaryFile; 41 class BitcodeFile; 42 class ELFFileBase; 43 class SharedFile; 44 class InputSectionBase; 45 class EhInputSection; 46 class Defined; 47 class Undefined; 48 class Symbol; 49 class SymbolTable; 50 class BitcodeCompiler; 51 class OutputSection; 52 class LinkerScript; 53 class TargetInfo; 54 struct Ctx; 55 struct Partition; 56 struct PhdrEntry; 57 58 class BssSection; 59 class GdbIndexSection; 60 class GotPltSection; 61 class GotSection; 62 class IBTPltSection; 63 class IgotPltSection; 64 class InputSection; 65 class IpltSection; 66 class MipsGotSection; 67 class MipsRldMapSection; 68 class PPC32Got2Section; 69 class PPC64LongBranchTargetSection; 70 class PltSection; 71 class RelocationBaseSection; 72 class RelroPaddingSection; 73 class StringTableSection; 74 class SymbolTableBaseSection; 75 class SymtabShndxSection; 76 class SyntheticSection; 77 78 enum ELFKind : uint8_t { 79 ELFNoneKind, 80 ELF32LEKind, 81 ELF32BEKind, 82 ELF64LEKind, 83 ELF64BEKind 84 }; 85 86 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions, 87 // -Bsymbolic-non-weak, -Bsymbolic. 88 enum class BsymbolicKind { None, NonWeakFunctions, Functions, NonWeak, All }; 89 90 // For --build-id. 91 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid }; 92 93 // For --call-graph-profile-sort={none,hfsort,cdsort}. 94 enum class CGProfileSortKind { None, Hfsort, Cdsort }; 95 96 // For --discard-{all,locals,none}. 97 enum class DiscardPolicy { Default, All, Locals, None }; 98 99 // For --icf={none,safe,all}. 100 enum class ICFLevel { None, Safe, All }; 101 102 // For --strip-{all,debug}. 103 enum class StripPolicy { None, All, Debug }; 104 105 // For --unresolved-symbols. 106 enum class UnresolvedPolicy { ReportError, Warn, Ignore }; 107 108 // For --orphan-handling. 109 enum class OrphanHandlingPolicy { Place, Warn, Error }; 110 111 // For --sort-section and linkerscript sorting rules. 112 enum class SortSectionPolicy { 113 Default, 114 None, 115 Alignment, 116 Name, 117 Priority, 118 Reverse, 119 }; 120 121 // For --target2 122 enum class Target2Policy { Abs, Rel, GotRel }; 123 124 // For tracking ARM Float Argument PCS 125 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain }; 126 127 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments. 128 enum class SeparateSegmentKind { None, Code, Loadable }; 129 130 // For -z *stack 131 enum class GnuStackKind { None, Exec, NoExec }; 132 133 // For --lto= 134 enum LtoKind : uint8_t {UnifiedThin, UnifiedRegular, Default}; 135 136 // For -z gcs= 137 enum class GcsPolicy { Implicit, Never, Always }; 138 139 struct SymbolVersion { 140 llvm::StringRef name; 141 bool isExternCpp; 142 bool hasWildcard; 143 }; 144 145 // This struct contains symbols version definition that 146 // can be found in version script if it is used for link. 147 struct VersionDefinition { 148 llvm::StringRef name; 149 uint16_t id; 150 SmallVector<SymbolVersion, 0> nonLocalPatterns; 151 SmallVector<SymbolVersion, 0> localPatterns; 152 }; 153 154 class LinkerDriver { 155 public: 156 LinkerDriver(Ctx &ctx); 157 LinkerDriver(LinkerDriver &) = delete; 158 void linkerMain(ArrayRef<const char *> args); 159 void addFile(StringRef path, bool withLOption); 160 void addLibrary(StringRef name); 161 162 private: 163 Ctx &ctx; 164 void createFiles(llvm::opt::InputArgList &args); 165 void inferMachineType(); 166 template <class ELFT> void link(llvm::opt::InputArgList &args); 167 template <class ELFT> void compileBitcodeFiles(bool skipLinkedOutput); 168 bool tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName, 169 uint64_t offsetInArchive, bool lazy); 170 // True if we are in --whole-archive and --no-whole-archive. 171 bool inWholeArchive = false; 172 173 // True if we are in --start-lib and --end-lib. 174 bool inLib = false; 175 176 std::unique_ptr<BitcodeCompiler> lto; 177 SmallVector<std::unique_ptr<InputFile>, 0> files, ltoObjectFiles; 178 179 public: 180 // See InputFile::groupId. 181 uint32_t nextGroupId; 182 bool isInGroup; 183 std::unique_ptr<InputFile> armCmseImpLib; 184 SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles; 185 }; 186 187 // This struct contains the global configuration for the linker. 188 // Most fields are direct mapping from the command line options 189 // and such fields have the same name as the corresponding options. 190 // Most fields are initialized by the ctx.driver. 191 struct Config { 192 uint8_t osabi = 0; 193 uint32_t andFeatures = 0; 194 llvm::CachePruningPolicy thinLTOCachePolicy; 195 llvm::SetVector<llvm::CachedHashString> dependencyFiles; // for --dependency-file 196 llvm::StringMap<uint64_t> sectionStartMap; 197 llvm::StringRef bfdname; 198 llvm::StringRef chroot; 199 llvm::StringRef dependencyFile; 200 llvm::StringRef dwoDir; 201 llvm::StringRef dynamicLinker; 202 llvm::StringRef entry; 203 llvm::StringRef emulation; 204 llvm::StringRef fini; 205 llvm::StringRef init; 206 llvm::StringRef ltoAAPipeline; 207 llvm::StringRef ltoCSProfileFile; 208 llvm::StringRef ltoNewPmPasses; 209 llvm::StringRef ltoObjPath; 210 llvm::StringRef ltoSampleProfile; 211 llvm::StringRef mapFile; 212 llvm::StringRef outputFile; 213 llvm::StringRef optRemarksFilename; 214 std::optional<uint64_t> optRemarksHotnessThreshold = 0; 215 llvm::StringRef optRemarksPasses; 216 llvm::StringRef optRemarksFormat; 217 llvm::StringRef optStatsFilename; 218 llvm::StringRef progName; 219 llvm::StringRef printArchiveStats; 220 llvm::StringRef printSymbolOrder; 221 llvm::StringRef soName; 222 llvm::StringRef sysroot; 223 llvm::StringRef thinLTOCacheDir; 224 llvm::StringRef thinLTOIndexOnlyArg; 225 llvm::StringRef whyExtract; 226 llvm::StringRef cmseInputLib; 227 llvm::StringRef cmseOutputLib; 228 StringRef zBtiReport = "none"; 229 StringRef zCetReport = "none"; 230 StringRef zPauthReport = "none"; 231 StringRef zGcsReport = "none"; 232 bool ltoBBAddrMap; 233 llvm::StringRef ltoBasicBlockSections; 234 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace; 235 llvm::StringRef thinLTOPrefixReplaceOld; 236 llvm::StringRef thinLTOPrefixReplaceNew; 237 llvm::StringRef thinLTOPrefixReplaceNativeObject; 238 std::string rpath; 239 llvm::SmallVector<VersionDefinition, 0> versionDefinitions; 240 llvm::SmallVector<llvm::StringRef, 0> auxiliaryList; 241 llvm::SmallVector<llvm::StringRef, 0> filterList; 242 llvm::SmallVector<llvm::StringRef, 0> passPlugins; 243 llvm::SmallVector<llvm::StringRef, 0> searchPaths; 244 llvm::SmallVector<llvm::StringRef, 0> symbolOrderingFile; 245 llvm::SmallVector<llvm::StringRef, 0> thinLTOModulesToCompile; 246 llvm::SmallVector<llvm::StringRef, 0> undefined; 247 llvm::SmallVector<SymbolVersion, 0> dynamicList; 248 llvm::SmallVector<uint8_t, 0> buildIdVector; 249 llvm::SmallVector<llvm::StringRef, 0> mllvmOpts; 250 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>, 251 uint64_t> 252 callGraphProfile; 253 bool cmseImplib = false; 254 bool allowMultipleDefinition; 255 bool fatLTOObjects; 256 bool androidPackDynRelocs = false; 257 bool armHasArmISA = false; 258 bool armHasThumb2ISA = false; 259 bool armHasBlx = false; 260 bool armHasMovtMovw = false; 261 bool armJ1J2BranchEncoding = false; 262 bool armCMSESupport = false; 263 bool asNeeded = false; 264 bool armBe8 = false; 265 BsymbolicKind bsymbolic = BsymbolicKind::None; 266 CGProfileSortKind callGraphProfileSort; 267 bool checkSections; 268 bool checkDynamicRelocs; 269 std::optional<llvm::DebugCompressionType> compressDebugSections; 270 llvm::SmallVector< 271 std::tuple<llvm::GlobPattern, llvm::DebugCompressionType, unsigned>, 0> 272 compressSections; 273 bool cref; 274 llvm::SmallVector<std::pair<llvm::GlobPattern, uint64_t>, 0> 275 deadRelocInNonAlloc; 276 bool debugNames; 277 bool demangle = true; 278 bool dependentLibraries; 279 bool disableVerify; 280 bool ehFrameHdr; 281 bool emitLLVM; 282 bool emitRelocs; 283 bool enableNewDtags; 284 bool enableNonContiguousRegions; 285 bool executeOnly; 286 bool exportDynamic; 287 bool fixCortexA53Errata843419; 288 bool fixCortexA8; 289 bool formatBinary = false; 290 bool fortranCommon; 291 bool gcSections; 292 bool gdbIndex; 293 bool gnuHash = false; 294 bool gnuUnique; 295 bool hasDynSymTab; 296 bool ignoreDataAddressEquality; 297 bool ignoreFunctionAddressEquality; 298 bool ltoCSProfileGenerate; 299 bool ltoPGOWarnMismatch; 300 bool ltoDebugPassManager; 301 bool ltoEmitAsm; 302 bool ltoUniqueBasicBlockSectionNames; 303 bool ltoValidateAllVtablesHaveTypeInfos; 304 bool ltoWholeProgramVisibility; 305 bool mergeArmExidx; 306 bool mipsN32Abi = false; 307 bool mmapOutputFile; 308 bool nmagic; 309 bool noDynamicLinker = false; 310 bool noinhibitExec; 311 bool nostdlib; 312 bool oFormatBinary; 313 bool omagic; 314 bool optEB = false; 315 bool optEL = false; 316 bool optimizeBBJumps; 317 bool optRemarksWithHotness; 318 bool picThunk; 319 bool pie; 320 bool printGcSections; 321 bool printIcfSections; 322 bool printMemoryUsage; 323 std::optional<uint64_t> randomizeSectionPadding; 324 bool rejectMismatch; 325 bool relax; 326 bool relaxGP; 327 bool relocatable; 328 bool resolveGroups; 329 bool relrGlibc = false; 330 bool relrPackDynRelocs = false; 331 llvm::DenseSet<llvm::StringRef> saveTempsArgs; 332 llvm::SmallVector<std::pair<llvm::GlobPattern, uint32_t>, 0> shuffleSections; 333 bool singleRoRx; 334 bool shared; 335 bool symbolic; 336 bool isStatic = false; 337 bool sysvHash = false; 338 bool target1Rel; 339 bool trace; 340 bool thinLTOEmitImportsFiles; 341 bool thinLTOEmitIndexFiles; 342 bool thinLTOIndexOnly; 343 bool timeTraceEnabled; 344 bool tocOptimize; 345 bool pcRelOptimize; 346 bool undefinedVersion; 347 bool unique; 348 bool useAndroidRelrTags = false; 349 bool warnBackrefs; 350 llvm::SmallVector<llvm::GlobPattern, 0> warnBackrefsExclude; 351 bool warnCommon; 352 bool warnMissingEntry; 353 bool warnSymbolOrdering; 354 bool writeAddends; 355 bool zCombreloc; 356 bool zCopyreloc; 357 bool zForceBti; 358 bool zForceIbt; 359 bool zGlobal; 360 bool zHazardplt; 361 bool zIfuncNoplt; 362 bool zInitfirst; 363 bool zInterpose; 364 bool zKeepTextSectionPrefix; 365 bool zLrodataAfterBss; 366 bool zNoBtCfi; 367 bool zNodefaultlib; 368 bool zNodelete; 369 bool zNodlopen; 370 bool zNow; 371 bool zOrigin; 372 bool zPacPlt; 373 bool zRelro; 374 bool zRodynamic; 375 bool zSectionHeader; 376 bool zShstk; 377 bool zStartStopGC; 378 uint8_t zStartStopVisibility; 379 bool zText; 380 bool zRetpolineplt; 381 bool zWxneeded; 382 DiscardPolicy discard; 383 GnuStackKind zGnustack; 384 ICFLevel icf; 385 OrphanHandlingPolicy orphanHandling; 386 SortSectionPolicy sortSection; 387 StripPolicy strip; 388 UnresolvedPolicy unresolvedSymbols; 389 UnresolvedPolicy unresolvedSymbolsInShlib; 390 Target2Policy target2; 391 GcsPolicy zGcs; 392 bool power10Stubs; 393 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default; 394 BuildIdKind buildId = BuildIdKind::None; 395 SeparateSegmentKind zSeparate; 396 ELFKind ekind = ELFNoneKind; 397 uint16_t emachine = llvm::ELF::EM_NONE; 398 std::optional<uint64_t> imageBase; 399 uint64_t commonPageSize; 400 uint64_t maxPageSize; 401 uint64_t mipsGotSize; 402 uint64_t zStackSize; 403 unsigned ltoPartitions; 404 unsigned ltoo; 405 llvm::CodeGenOptLevel ltoCgo; 406 unsigned optimize; 407 StringRef thinLTOJobs; 408 unsigned timeTraceGranularity; 409 int32_t splitStackAdjustSize; 410 StringRef packageMetadata; 411 412 // The following config options do not directly correspond to any 413 // particular command line options. 414 415 // True if we need to pass through relocations in input files to the 416 // output file. Usually false because we consume relocations. 417 bool copyRelocs; 418 419 // True if the target is ELF64. False if ELF32. 420 bool is64; 421 422 // True if the target is little-endian. False if big-endian. 423 bool isLE; 424 425 // endianness::little if isLE is true. endianness::big otherwise. 426 llvm::endianness endianness; 427 428 // True if the target is the little-endian MIPS64. 429 // 430 // The reason why we have this variable only for the MIPS is because 431 // we use this often. Some ELF headers for MIPS64EL are in a 432 // mixed-endian (which is horrible and I'd say that's a serious spec 433 // bug), and we need to know whether we are reading MIPS ELF files or 434 // not in various places. 435 // 436 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official 437 // name whatever that means. A fun hypothesis is that "EL" is short for 438 // little-endian written in the little-endian order, but I don't know 439 // if that's true.) 440 bool isMips64EL; 441 442 // True if we need to set the DF_STATIC_TLS flag to an output file, which 443 // works as a hint to the dynamic loader that the shared object contains code 444 // compiled with the initial-exec TLS model. 445 bool hasTlsIe = false; 446 447 // Holds set of ELF header flags for the target. 448 uint32_t eflags = 0; 449 450 // The ELF spec defines two types of relocation table entries, RELA and 451 // REL. RELA is a triplet of (offset, info, addend) while REL is a 452 // tuple of (offset, info). Addends for REL are implicit and read from 453 // the location where the relocations are applied. So, REL is more 454 // compact than RELA but requires a bit of more work to process. 455 // 456 // (From the linker writer's view, this distinction is not necessary. 457 // If the ELF had chosen whichever and sticked with it, it would have 458 // been easier to write code to process relocations, but it's too late 459 // to change the spec.) 460 // 461 // Each ABI defines its relocation type. IsRela is true if target 462 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A 463 // few 32-bit ABIs are using RELA too. 464 bool isRela; 465 466 // True if we are creating position-independent code. 467 bool isPic; 468 469 // 4 for ELF32, 8 for ELF64. 470 int wordsize; 471 472 // Mode of MTE to write to the ELF note. Should be one of NT_MEMTAG_ASYNC (for 473 // async), NT_MEMTAG_SYNC (for sync), or NT_MEMTAG_LEVEL_NONE (for none). If 474 // async or sync is enabled, write the ELF note specifying the default MTE 475 // mode. 476 int androidMemtagMode; 477 // Signal to the dynamic loader to enable heap MTE. 478 bool androidMemtagHeap; 479 // Signal to the dynamic loader that this binary expects stack MTE. Generally, 480 // this means to map the primary and thread stacks as PROT_MTE. Note: This is 481 // not supported on Android 11 & 12. 482 bool androidMemtagStack; 483 484 // When using a unified pre-link LTO pipeline, specify the backend LTO mode. 485 LtoKind ltoKind = LtoKind::Default; 486 487 unsigned threadCount; 488 489 // If an input file equals a key, remap it to the value. 490 llvm::DenseMap<llvm::StringRef, llvm::StringRef> remapInputs; 491 // If an input file matches a wildcard pattern, remap it to the value. 492 llvm::SmallVector<std::pair<llvm::GlobPattern, llvm::StringRef>, 0> 493 remapInputsWildcards; 494 }; 495 496 // Some index properties of a symbol are stored separately in this auxiliary 497 // struct to decrease sizeof(SymbolUnion) in the majority of cases. 498 struct SymbolAux { 499 uint32_t gotIdx = -1; 500 uint32_t pltIdx = -1; 501 uint32_t tlsDescIdx = -1; 502 uint32_t tlsGdIdx = -1; 503 }; 504 505 struct DuplicateSymbol { 506 const Symbol *sym; 507 const InputFile *file; 508 InputSectionBase *section; 509 uint64_t value; 510 }; 511 512 struct UndefinedDiag { 513 Undefined *sym; 514 struct Loc { 515 InputSectionBase *sec; 516 uint64_t offset; 517 }; 518 SmallVector<Loc, 0> locs; 519 bool isWarning; 520 }; 521 522 // Linker generated sections which can be used as inputs and are not specific to 523 // a partition. 524 struct InStruct { 525 std::unique_ptr<InputSection> attributes; 526 std::unique_ptr<SyntheticSection> riscvAttributes; 527 std::unique_ptr<BssSection> bss; 528 std::unique_ptr<BssSection> bssRelRo; 529 std::unique_ptr<SyntheticSection> gnuProperty; 530 std::unique_ptr<SyntheticSection> gnuStack; 531 std::unique_ptr<GotSection> got; 532 std::unique_ptr<GotPltSection> gotPlt; 533 std::unique_ptr<IgotPltSection> igotPlt; 534 std::unique_ptr<RelroPaddingSection> relroPadding; 535 std::unique_ptr<SyntheticSection> armCmseSGSection; 536 std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget; 537 std::unique_ptr<SyntheticSection> mipsAbiFlags; 538 std::unique_ptr<MipsGotSection> mipsGot; 539 std::unique_ptr<SyntheticSection> mipsOptions; 540 std::unique_ptr<SyntheticSection> mipsReginfo; 541 std::unique_ptr<MipsRldMapSection> mipsRldMap; 542 std::unique_ptr<SyntheticSection> partEnd; 543 std::unique_ptr<SyntheticSection> partIndex; 544 std::unique_ptr<PltSection> plt; 545 std::unique_ptr<IpltSection> iplt; 546 std::unique_ptr<PPC32Got2Section> ppc32Got2; 547 std::unique_ptr<IBTPltSection> ibtPlt; 548 std::unique_ptr<RelocationBaseSection> relaPlt; 549 // Non-SHF_ALLOC sections 550 std::unique_ptr<SyntheticSection> debugNames; 551 std::unique_ptr<GdbIndexSection> gdbIndex; 552 std::unique_ptr<StringTableSection> shStrTab; 553 std::unique_ptr<StringTableSection> strTab; 554 std::unique_ptr<SymbolTableBaseSection> symTab; 555 std::unique_ptr<SymtabShndxSection> symTabShndx; 556 }; 557 558 struct Ctx : CommonLinkerContext { 559 Config arg; 560 LinkerDriver driver; 561 LinkerScript *script; 562 std::unique_ptr<TargetInfo> target; 563 564 // These variables are initialized by Writer and should not be used before 565 // Writer is initialized. 566 uint8_t *bufferStart = nullptr; 567 Partition *mainPart = nullptr; 568 PhdrEntry *tlsPhdr = nullptr; 569 struct OutSections { 570 std::unique_ptr<OutputSection> elfHeader; 571 std::unique_ptr<OutputSection> programHeaders; 572 OutputSection *preinitArray = nullptr; 573 OutputSection *initArray = nullptr; 574 OutputSection *finiArray = nullptr; 575 }; 576 OutSections out; 577 SmallVector<OutputSection *, 0> outputSections; 578 std::vector<Partition> partitions; 579 580 InStruct in; 581 582 // Some linker-generated symbols need to be created as 583 // Defined symbols. 584 struct ElfSym { 585 // __bss_start 586 Defined *bss; 587 588 // etext and _etext 589 Defined *etext1; 590 Defined *etext2; 591 592 // edata and _edata 593 Defined *edata1; 594 Defined *edata2; 595 596 // end and _end 597 Defined *end1; 598 Defined *end2; 599 600 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to 601 // be at some offset from the base of the .got section, usually 0 or 602 // the end of the .got. 603 Defined *globalOffsetTable; 604 605 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS. 606 Defined *mipsGp; 607 Defined *mipsGpDisp; 608 Defined *mipsLocalGp; 609 610 // __global_pointer$ for RISC-V. 611 Defined *riscvGlobalPointer; 612 613 // __rel{,a}_iplt_{start,end} symbols. 614 Defined *relaIpltStart; 615 Defined *relaIpltEnd; 616 617 // _TLS_MODULE_BASE_ on targets that support TLSDESC. 618 Defined *tlsModuleBase; 619 }; 620 ElfSym sym{}; 621 std::unique_ptr<SymbolTable> symtab; 622 SmallVector<Symbol *, 0> synthesizedSymbols; 623 624 SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers; 625 SmallVector<ELFFileBase *, 0> objectFiles; 626 SmallVector<SharedFile *, 0> sharedFiles; 627 SmallVector<BinaryFile *, 0> binaryFiles; 628 SmallVector<BitcodeFile *, 0> bitcodeFiles; 629 SmallVector<BitcodeFile *, 0> lazyBitcodeFiles; 630 SmallVector<InputSectionBase *, 0> inputSections; 631 SmallVector<EhInputSection *, 0> ehInputSections; 632 633 SmallVector<SymbolAux, 0> symAux; 634 // Duplicate symbol candidates. 635 SmallVector<DuplicateSymbol, 0> duplicates; 636 // Undefined diagnostics are collected in a vector and emitted once all of 637 // them are known, so that some postprocessing on the list of undefined 638 // symbols can happen before lld emits diagnostics. 639 std::mutex relocMutex; 640 SmallVector<UndefinedDiag, 0> undefErrs; 641 // Symbols in a non-prevailing COMDAT group which should be changed to an 642 // Undefined. 643 SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms; 644 // A tuple of (reference, extractedFile, sym). Used by --why-extract=. 645 SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0> 646 whyExtractRecords; 647 // A mapping from a symbol to an InputFile referencing it backward. Used by 648 // --warn-backrefs. 649 llvm::DenseMap<const Symbol *, 650 std::pair<const InputFile *, const InputFile *>> 651 backwardReferences; 652 llvm::SmallSet<llvm::StringRef, 0> auxiliaryFiles; 653 // If --reproduce is specified, all input files are written to this tar 654 // archive. 655 std::unique_ptr<llvm::TarWriter> tar; 656 // InputFile for linker created symbols with no source location. 657 InputFile *internalFile = nullptr; 658 // True if symbols can be exported (isExported) or preemptible. 659 bool hasDynsym = false; 660 // True if SHT_LLVM_SYMPART is used. 661 std::atomic<bool> hasSympart{false}; 662 // True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared. 663 std::atomic<bool> hasTlsIe{false}; 664 // True if we need to reserve two .got entries for local-dynamic TLS model. 665 std::atomic<bool> needsTlsLd{false}; 666 // True if all native vtable symbols have corresponding type info symbols 667 // during LTO. 668 bool ltoAllVtablesHaveTypeInfos = false; 669 // Number of Vernaux entries (needed shared object names). 670 uint32_t vernauxNum = 0; 671 672 // Each symbol assignment and DEFINED(sym) reference is assigned an increasing 673 // order. Each DEFINED(sym) evaluation checks whether the reference happens 674 // before a possible `sym = expr;`. 675 unsigned scriptSymOrderCounter = 1; 676 llvm::DenseMap<const Symbol *, unsigned> scriptSymOrder; 677 678 // The set of TOC entries (.toc + addend) for which we should not apply 679 // toc-indirect to toc-relative relaxation. const Symbol * refers to the 680 // STT_SECTION symbol associated to the .toc input section. 681 llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax; 682 683 Ctx(); 684 685 llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &); 686 687 ArrayRef<uint8_t> aarch64PauthAbiCoreInfo; 688 }; 689 690 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and 691 // VER_NDX_GLOBAL. This helper returns other elements. 692 static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) { 693 return llvm::ArrayRef(ctx.arg.versionDefinitions).slice(2); 694 } 695 696 struct ELFSyncStream : SyncStream { 697 Ctx &ctx; 698 ELFSyncStream(Ctx &ctx, DiagLevel level) 699 : SyncStream(ctx.e, level), ctx(ctx) {} 700 }; 701 702 template <typename T> 703 std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>, 704 const ELFSyncStream &> 705 operator<<(const ELFSyncStream &s, T &&v) { 706 s.os << std::forward<T>(v); 707 return s; 708 } 709 710 inline const ELFSyncStream &operator<<(const ELFSyncStream &s, const char *v) { 711 s.os << v; 712 return s; 713 } 714 715 inline const ELFSyncStream &operator<<(const ELFSyncStream &s, Error v) { 716 s.os << llvm::toString(std::move(v)); 717 return s; 718 } 719 720 // Report a log if --verbose is specified. 721 ELFSyncStream Log(Ctx &ctx); 722 723 // Print a message to stdout. 724 ELFSyncStream Msg(Ctx &ctx); 725 726 // Report a warning. Upgraded to an error if --fatal-warnings is specified. 727 ELFSyncStream Warn(Ctx &ctx); 728 729 // Report an error that will suppress the output file generation. Downgraded to 730 // a warning if --noinhibit-exec is specified. 731 ELFSyncStream Err(Ctx &ctx); 732 733 // Report an error regardless of --noinhibit-exec. 734 ELFSyncStream ErrAlways(Ctx &ctx); 735 736 // Report a fatal error that exits immediately. This should generally be avoided 737 // in favor of Err. 738 ELFSyncStream Fatal(Ctx &ctx); 739 740 uint64_t errCount(Ctx &ctx); 741 742 ELFSyncStream InternalErr(Ctx &ctx, const uint8_t *buf); 743 744 #define CHECK2(E, S) lld::check2((E), [&] { return toStr(ctx, S); }) 745 746 } // namespace lld::elf 747 748 #endif 749