1 //===- Config.h -------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_CONFIG_H
10 #define LLD_ELF_CONFIG_H
12 #include "lld/Common/ErrorHandler.h"
13 #include "llvm/ADT/CachedHashString.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/MapVector.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Support/CachePruning.h"
23 #include "llvm/Support/CodeGen.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/Compression.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/GlobPattern.h"
29 #include "llvm/Support/PrettyStackTrace.h"
42 class InputSectionBase
;
45 class BitcodeCompiler
;
47 enum ELFKind
: uint8_t {
55 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions,
56 // -Bsymbolic-non-weak, -Bsymbolic.
57 enum class BsymbolicKind
{ None
, NonWeakFunctions
, Functions
, NonWeak
, All
};
60 enum class BuildIdKind
{ None
, Fast
, Md5
, Sha1
, Hexstring
, Uuid
};
62 // For --call-graph-profile-sort={none,hfsort,cdsort}.
63 enum class CGProfileSortKind
{ None
, Hfsort
, Cdsort
};
65 // For --discard-{all,locals,none}.
66 enum class DiscardPolicy
{ Default
, All
, Locals
, None
};
68 // For --icf={none,safe,all}.
69 enum class ICFLevel
{ None
, Safe
, All
};
71 // For --strip-{all,debug}.
72 enum class StripPolicy
{ None
, All
, Debug
};
74 // For --unresolved-symbols.
75 enum class UnresolvedPolicy
{ ReportError
, Warn
, Ignore
};
77 // For --orphan-handling.
78 enum class OrphanHandlingPolicy
{ Place
, Warn
, Error
};
80 // For --sort-section and linkerscript sorting rules.
81 enum class SortSectionPolicy
{
91 enum class Target2Policy
{ Abs
, Rel
, GotRel
};
93 // For tracking ARM Float Argument PCS
94 enum class ARMVFPArgKind
{ Default
, Base
, VFP
, ToolChain
};
96 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
97 enum class SeparateSegmentKind
{ None
, Code
, Loadable
};
100 enum class GnuStackKind
{ None
, Exec
, NoExec
};
103 enum LtoKind
: uint8_t {UnifiedThin
, UnifiedRegular
, Default
};
105 struct SymbolVersion
{
106 llvm::StringRef name
;
111 // This struct contains symbols version definition that
112 // can be found in version script if it is used for link.
113 struct VersionDefinition
{
114 llvm::StringRef name
;
116 SmallVector
<SymbolVersion
, 0> nonLocalPatterns
;
117 SmallVector
<SymbolVersion
, 0> localPatterns
;
122 void linkerMain(ArrayRef
<const char *> args
);
123 void addFile(StringRef path
, bool withLOption
);
124 void addLibrary(StringRef name
);
127 void createFiles(llvm::opt::InputArgList
&args
);
128 void inferMachineType();
129 void link(llvm::opt::InputArgList
&args
);
130 template <class ELFT
> void compileBitcodeFiles(bool skipLinkedOutput
);
131 bool tryAddFatLTOFile(MemoryBufferRef mb
, StringRef archiveName
,
132 uint64_t offsetInArchive
, bool lazy
);
133 // True if we are in --whole-archive and --no-whole-archive.
134 bool inWholeArchive
= false;
136 // True if we are in --start-lib and --end-lib.
139 std::unique_ptr
<BitcodeCompiler
> lto
;
140 std::vector
<InputFile
*> files
;
141 std::optional
<InputFile
*> armCmseImpLib
;
144 SmallVector
<std::pair
<StringRef
, unsigned>, 0> archiveFiles
;
147 // This struct contains the global configuration for the linker.
148 // Most fields are direct mapping from the command line options
149 // and such fields have the same name as the corresponding options.
150 // Most fields are initialized by the ctx.driver.
153 uint32_t andFeatures
= 0;
154 llvm::CachePruningPolicy thinLTOCachePolicy
;
155 llvm::SetVector
<llvm::CachedHashString
> dependencyFiles
; // for --dependency-file
156 llvm::StringMap
<uint64_t> sectionStartMap
;
157 llvm::StringRef bfdname
;
158 llvm::StringRef chroot
;
159 llvm::StringRef dependencyFile
;
160 llvm::StringRef dwoDir
;
161 llvm::StringRef dynamicLinker
;
162 llvm::StringRef entry
;
163 llvm::StringRef emulation
;
164 llvm::StringRef fini
;
165 llvm::StringRef init
;
166 llvm::StringRef ltoAAPipeline
;
167 llvm::StringRef ltoCSProfileFile
;
168 llvm::StringRef ltoNewPmPasses
;
169 llvm::StringRef ltoObjPath
;
170 llvm::StringRef ltoSampleProfile
;
171 llvm::StringRef mapFile
;
172 llvm::StringRef outputFile
;
173 llvm::StringRef optRemarksFilename
;
174 std::optional
<uint64_t> optRemarksHotnessThreshold
= 0;
175 llvm::StringRef optRemarksPasses
;
176 llvm::StringRef optRemarksFormat
;
177 llvm::StringRef optStatsFilename
;
178 llvm::StringRef progName
;
179 llvm::StringRef printArchiveStats
;
180 llvm::StringRef printSymbolOrder
;
181 llvm::StringRef soName
;
182 llvm::StringRef sysroot
;
183 llvm::StringRef thinLTOCacheDir
;
184 llvm::StringRef thinLTOIndexOnlyArg
;
185 llvm::StringRef whyExtract
;
186 llvm::StringRef cmseInputLib
;
187 llvm::StringRef cmseOutputLib
;
188 StringRef zBtiReport
= "none";
189 StringRef zCetReport
= "none";
190 llvm::StringRef ltoBasicBlockSections
;
191 std::pair
<llvm::StringRef
, llvm::StringRef
> thinLTOObjectSuffixReplace
;
192 llvm::StringRef thinLTOPrefixReplaceOld
;
193 llvm::StringRef thinLTOPrefixReplaceNew
;
194 llvm::StringRef thinLTOPrefixReplaceNativeObject
;
196 llvm::SmallVector
<VersionDefinition
, 0> versionDefinitions
;
197 llvm::SmallVector
<llvm::StringRef
, 0> auxiliaryList
;
198 llvm::SmallVector
<llvm::StringRef
, 0> filterList
;
199 llvm::SmallVector
<llvm::StringRef
, 0> passPlugins
;
200 llvm::SmallVector
<llvm::StringRef
, 0> searchPaths
;
201 llvm::SmallVector
<llvm::StringRef
, 0> symbolOrderingFile
;
202 llvm::SmallVector
<llvm::StringRef
, 0> thinLTOModulesToCompile
;
203 llvm::SmallVector
<llvm::StringRef
, 0> undefined
;
204 llvm::SmallVector
<SymbolVersion
, 0> dynamicList
;
205 llvm::SmallVector
<uint8_t, 0> buildIdVector
;
206 llvm::SmallVector
<llvm::StringRef
, 0> mllvmOpts
;
207 llvm::MapVector
<std::pair
<const InputSectionBase
*, const InputSectionBase
*>,
210 bool cmseImplib
= false;
211 bool allowMultipleDefinition
;
213 bool androidPackDynRelocs
= false;
214 bool armHasBlx
= false;
215 bool armHasMovtMovw
= false;
216 bool armJ1J2BranchEncoding
= false;
217 bool armCMSESupport
= false;
218 bool asNeeded
= false;
220 BsymbolicKind bsymbolic
= BsymbolicKind::None
;
221 CGProfileSortKind callGraphProfileSort
;
223 bool checkDynamicRelocs
;
224 llvm::DebugCompressionType compressDebugSections
;
226 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, uint64_t>, 0>
228 bool demangle
= true;
229 bool dependentLibraries
;
237 bool fixCortexA53Errata843419
;
239 bool formatBinary
= false;
243 bool gnuHash
= false;
246 bool ignoreDataAddressEquality
;
247 bool ignoreFunctionAddressEquality
;
248 bool ltoCSProfileGenerate
;
249 bool ltoPGOWarnMismatch
;
250 bool ltoDebugPassManager
;
252 bool ltoUniqueBasicBlockSectionNames
;
253 bool ltoValidateAllVtablesHaveTypeInfos
;
254 bool ltoWholeProgramVisibility
;
256 bool mipsN32Abi
= false;
259 bool noDynamicLinker
= false;
266 bool optimizeBBJumps
;
267 bool optRemarksWithHotness
;
270 bool printGcSections
;
271 bool printIcfSections
;
272 bool printMemoryUsage
;
276 bool relrGlibc
= false;
277 bool relrPackDynRelocs
= false;
278 llvm::DenseSet
<llvm::StringRef
> saveTempsArgs
;
279 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, uint32_t>, 0> shuffleSections
;
283 bool isStatic
= false;
284 bool sysvHash
= false;
287 bool thinLTOEmitImportsFiles
;
288 bool thinLTOEmitIndexFiles
;
289 bool thinLTOIndexOnly
;
290 bool timeTraceEnabled
;
293 bool undefinedVersion
;
295 bool useAndroidRelrTags
= false;
297 llvm::SmallVector
<llvm::GlobPattern
, 0> warnBackrefsExclude
;
299 bool warnMissingEntry
;
300 bool warnSymbolOrdering
;
311 bool zKeepTextSectionPrefix
;
322 uint8_t zStartStopVisibility
;
326 DiscardPolicy discard
;
327 GnuStackKind zGnustack
;
329 OrphanHandlingPolicy orphanHandling
;
330 SortSectionPolicy sortSection
;
332 UnresolvedPolicy unresolvedSymbols
;
333 UnresolvedPolicy unresolvedSymbolsInShlib
;
334 Target2Policy target2
;
336 ARMVFPArgKind armVFPArgs
= ARMVFPArgKind::Default
;
337 BuildIdKind buildId
= BuildIdKind::None
;
338 SeparateSegmentKind zSeparate
;
339 ELFKind ekind
= ELFNoneKind
;
340 uint16_t emachine
= llvm::ELF::EM_NONE
;
341 std::optional
<uint64_t> imageBase
;
342 uint64_t commonPageSize
;
343 uint64_t maxPageSize
;
344 uint64_t mipsGotSize
;
346 unsigned ltoPartitions
;
348 llvm::CodeGenOptLevel ltoCgo
;
350 StringRef thinLTOJobs
;
351 unsigned timeTraceGranularity
;
352 int32_t splitStackAdjustSize
;
353 StringRef packageMetadata
;
355 // The following config options do not directly correspond to any
356 // particular command line options.
358 // True if we need to pass through relocations in input files to the
359 // output file. Usually false because we consume relocations.
362 // True if the target is ELF64. False if ELF32.
365 // True if the target is little-endian. False if big-endian.
368 // endianness::little if isLE is true. endianness::big otherwise.
369 llvm::endianness endianness
;
371 // True if the target is the little-endian MIPS64.
373 // The reason why we have this variable only for the MIPS is because
374 // we use this often. Some ELF headers for MIPS64EL are in a
375 // mixed-endian (which is horrible and I'd say that's a serious spec
376 // bug), and we need to know whether we are reading MIPS ELF files or
377 // not in various places.
379 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
380 // name whatever that means. A fun hypothesis is that "EL" is short for
381 // little-endian written in the little-endian order, but I don't know
385 // True if we need to set the DF_STATIC_TLS flag to an output file, which
386 // works as a hint to the dynamic loader that the shared object contains code
387 // compiled with the initial-exec TLS model.
388 bool hasTlsIe
= false;
390 // Holds set of ELF header flags for the target.
393 // The ELF spec defines two types of relocation table entries, RELA and
394 // REL. RELA is a triplet of (offset, info, addend) while REL is a
395 // tuple of (offset, info). Addends for REL are implicit and read from
396 // the location where the relocations are applied. So, REL is more
397 // compact than RELA but requires a bit of more work to process.
399 // (From the linker writer's view, this distinction is not necessary.
400 // If the ELF had chosen whichever and sticked with it, it would have
401 // been easier to write code to process relocations, but it's too late
402 // to change the spec.)
404 // Each ABI defines its relocation type. IsRela is true if target
405 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
406 // few 32-bit ABIs are using RELA too.
409 // True if we are creating position-independent code.
412 // 4 for ELF32, 8 for ELF64.
415 // Mode of MTE to write to the ELF note. Should be one of NT_MEMTAG_ASYNC (for
416 // async), NT_MEMTAG_SYNC (for sync), or NT_MEMTAG_LEVEL_NONE (for none). If
417 // async or sync is enabled, write the ELF note specifying the default MTE
419 int androidMemtagMode
;
420 // Signal to the dynamic loader to enable heap MTE.
421 bool androidMemtagHeap
;
422 // Signal to the dynamic loader that this binary expects stack MTE. Generally,
423 // this means to map the primary and thread stacks as PROT_MTE. Note: This is
424 // not supported on Android 11 & 12.
425 bool androidMemtagStack
;
427 // When using a unified pre-link LTO pipeline, specify the backend LTO mode.
428 LtoKind ltoKind
= LtoKind::Default
;
430 unsigned threadCount
;
432 // If an input file equals a key, remap it to the value.
433 llvm::DenseMap
<llvm::StringRef
, llvm::StringRef
> remapInputs
;
434 // If an input file matches a wildcard pattern, remap it to the value.
435 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, llvm::StringRef
>, 0>
436 remapInputsWildcards
;
438 struct ConfigWrapper
{
440 Config
*operator->() { return &c
; }
443 LLVM_LIBRARY_VISIBILITY
extern ConfigWrapper config
;
445 struct DuplicateSymbol
{
447 const InputFile
*file
;
448 InputSectionBase
*section
;
454 SmallVector
<std::unique_ptr
<MemoryBuffer
>> memoryBuffers
;
455 SmallVector
<ELFFileBase
*, 0> objectFiles
;
456 SmallVector
<SharedFile
*, 0> sharedFiles
;
457 SmallVector
<BinaryFile
*, 0> binaryFiles
;
458 SmallVector
<BitcodeFile
*, 0> bitcodeFiles
;
459 SmallVector
<BitcodeFile
*, 0> lazyBitcodeFiles
;
460 SmallVector
<InputSectionBase
*, 0> inputSections
;
461 SmallVector
<EhInputSection
*, 0> ehInputSections
;
462 // Duplicate symbol candidates.
463 SmallVector
<DuplicateSymbol
, 0> duplicates
;
464 // Symbols in a non-prevailing COMDAT group which should be changed to an
466 SmallVector
<std::pair
<Symbol
*, unsigned>, 0> nonPrevailingSyms
;
467 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
468 SmallVector
<std::tuple
<std::string
, const InputFile
*, const Symbol
&>, 0>
470 // A mapping from a symbol to an InputFile referencing it backward. Used by
472 llvm::DenseMap
<const Symbol
*,
473 std::pair
<const InputFile
*, const InputFile
*>>
475 llvm::SmallSet
<llvm::StringRef
, 0> auxiliaryFiles
;
476 // True if SHT_LLVM_SYMPART is used.
477 std::atomic
<bool> hasSympart
{false};
478 // True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared.
479 std::atomic
<bool> hasTlsIe
{false};
480 // True if we need to reserve two .got entries for local-dynamic TLS model.
481 std::atomic
<bool> needsTlsLd
{false};
482 // True if all native vtable symbols have corresponding type info symbols
484 bool ltoAllVtablesHaveTypeInfos
;
486 // Each symbol assignment and DEFINED(sym) reference is assigned an increasing
487 // order. Each DEFINED(sym) evaluation checks whether the reference happens
488 // before a possible `sym = expr;`.
489 unsigned scriptSymOrderCounter
= 1;
490 llvm::DenseMap
<const Symbol
*, unsigned> scriptSymOrder
;
494 llvm::raw_fd_ostream
openAuxiliaryFile(llvm::StringRef
, std::error_code
&);
497 LLVM_LIBRARY_VISIBILITY
extern Ctx ctx
;
499 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
500 // VER_NDX_GLOBAL. This helper returns other elements.
501 static inline ArrayRef
<VersionDefinition
> namedVersionDefs() {
502 return llvm::ArrayRef(config
->versionDefinitions
).slice(2);
505 void errorOrWarn(const Twine
&msg
);
507 static inline void internalLinkerError(StringRef loc
, const Twine
&msg
) {
508 errorOrWarn(loc
+ "internal linker error: " + msg
+ "\n" +
509 llvm::getBugReportMsg());
512 } // namespace lld::elf