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/StringRef.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Support/CachePruning.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/GlobPattern.h"
24 #include "llvm/Support/PrettyStackTrace.h"
37 class InputSectionBase
;
40 enum ELFKind
: uint8_t {
48 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions,
50 enum class BsymbolicKind
{ None
, NonWeakFunctions
, Functions
, All
};
53 enum class BuildIdKind
{ None
, Fast
, Md5
, Sha1
, Hexstring
, Uuid
};
55 // For --discard-{all,locals,none}.
56 enum class DiscardPolicy
{ Default
, All
, Locals
, None
};
58 // For --icf={none,safe,all}.
59 enum class ICFLevel
{ None
, Safe
, All
};
61 // For --strip-{all,debug}.
62 enum class StripPolicy
{ None
, All
, Debug
};
64 // For --unresolved-symbols.
65 enum class UnresolvedPolicy
{ ReportError
, Warn
, Ignore
};
67 // For --orphan-handling.
68 enum class OrphanHandlingPolicy
{ Place
, Warn
, Error
};
70 // For --sort-section and linkerscript sorting rules.
71 enum class SortSectionPolicy
{ Default
, None
, Alignment
, Name
, Priority
};
74 enum class Target2Policy
{ Abs
, Rel
, GotRel
};
76 // For tracking ARM Float Argument PCS
77 enum class ARMVFPArgKind
{ Default
, Base
, VFP
, ToolChain
};
79 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
80 enum class SeparateSegmentKind
{ None
, Code
, Loadable
};
83 enum class GnuStackKind
{ None
, Exec
, NoExec
};
85 struct SymbolVersion
{
91 // This struct contains symbols version definition that
92 // can be found in version script if it is used for link.
93 struct VersionDefinition
{
96 SmallVector
<SymbolVersion
, 0> nonLocalPatterns
;
97 SmallVector
<SymbolVersion
, 0> localPatterns
;
100 // This struct contains the global configuration for the linker.
101 // Most fields are direct mapping from the command line options
102 // and such fields have the same name as the corresponding options.
103 // Most fields are initialized by the driver.
104 struct Configuration
{
106 uint32_t andFeatures
= 0;
107 llvm::CachePruningPolicy thinLTOCachePolicy
;
108 llvm::SetVector
<llvm::CachedHashString
> dependencyFiles
; // for --dependency-file
109 llvm::StringMap
<uint64_t> sectionStartMap
;
110 llvm::StringRef bfdname
;
111 llvm::StringRef chroot
;
112 llvm::StringRef dependencyFile
;
113 llvm::StringRef dwoDir
;
114 llvm::StringRef dynamicLinker
;
115 llvm::StringRef entry
;
116 llvm::StringRef emulation
;
117 llvm::StringRef fini
;
118 llvm::StringRef init
;
119 llvm::StringRef ltoAAPipeline
;
120 llvm::StringRef ltoCSProfileFile
;
121 llvm::StringRef ltoNewPmPasses
;
122 llvm::StringRef ltoObjPath
;
123 llvm::StringRef ltoSampleProfile
;
124 llvm::StringRef mapFile
;
125 llvm::StringRef outputFile
;
126 llvm::StringRef optRemarksFilename
;
127 llvm::Optional
<uint64_t> optRemarksHotnessThreshold
= 0;
128 llvm::StringRef optRemarksPasses
;
129 llvm::StringRef optRemarksFormat
;
130 llvm::StringRef optStatsFilename
;
131 llvm::StringRef progName
;
132 llvm::StringRef printArchiveStats
;
133 llvm::StringRef printSymbolOrder
;
134 llvm::StringRef soName
;
135 llvm::StringRef sysroot
;
136 llvm::StringRef thinLTOCacheDir
;
137 llvm::StringRef thinLTOIndexOnlyArg
;
138 llvm::StringRef whyExtract
;
139 StringRef zBtiReport
= "none";
140 StringRef zCetReport
= "none";
141 llvm::StringRef ltoBasicBlockSections
;
142 std::pair
<llvm::StringRef
, llvm::StringRef
> thinLTOObjectSuffixReplace
;
143 std::pair
<llvm::StringRef
, llvm::StringRef
> thinLTOPrefixReplace
;
145 llvm::SmallVector
<VersionDefinition
, 0> versionDefinitions
;
146 llvm::SmallVector
<llvm::StringRef
, 0> auxiliaryList
;
147 llvm::SmallVector
<llvm::StringRef
, 0> filterList
;
148 llvm::SmallVector
<llvm::StringRef
, 0> passPlugins
;
149 llvm::SmallVector
<llvm::StringRef
, 0> searchPaths
;
150 llvm::SmallVector
<llvm::StringRef
, 0> symbolOrderingFile
;
151 llvm::SmallVector
<llvm::StringRef
, 0> thinLTOModulesToCompile
;
152 llvm::SmallVector
<llvm::StringRef
, 0> undefined
;
153 llvm::SmallVector
<SymbolVersion
, 0> dynamicList
;
154 llvm::SmallVector
<uint8_t, 0> buildIdVector
;
155 llvm::MapVector
<std::pair
<const InputSectionBase
*, const InputSectionBase
*>,
158 bool allowMultipleDefinition
;
159 bool androidPackDynRelocs
= false;
160 bool armHasBlx
= false;
161 bool armHasMovtMovw
= false;
162 bool armJ1J2BranchEncoding
= false;
163 bool asNeeded
= false;
164 BsymbolicKind bsymbolic
= BsymbolicKind::None
;
165 bool callGraphProfileSort
;
167 bool checkDynamicRelocs
;
168 bool compressDebugSections
;
170 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, uint64_t>, 0>
172 bool demangle
= true;
173 bool dependentLibraries
;
181 bool fixCortexA53Errata843419
;
183 bool formatBinary
= false;
187 bool gnuHash
= false;
190 bool ignoreDataAddressEquality
;
191 bool ignoreFunctionAddressEquality
;
192 bool ltoCSProfileGenerate
;
193 bool ltoPGOWarnMismatch
;
194 bool ltoDebugPassManager
;
196 bool ltoUniqueBasicBlockSectionNames
;
197 bool ltoWholeProgramVisibility
;
199 bool mipsN32Abi
= false;
202 bool noDynamicLinker
= false;
210 bool optimizeBBJumps
;
211 bool optRemarksWithHotness
;
214 bool printGcSections
;
215 bool printIcfSections
;
218 bool relrGlibc
= false;
219 bool relrPackDynRelocs
= false;
220 llvm::DenseSet
<llvm::StringRef
> saveTempsArgs
;
221 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, uint32_t>, 0> shuffleSections
;
225 bool isStatic
= false;
226 bool sysvHash
= false;
229 bool thinLTOEmitImportsFiles
;
230 bool thinLTOEmitIndexFiles
;
231 bool thinLTOIndexOnly
;
232 bool timeTraceEnabled
;
235 bool undefinedVersion
;
237 bool useAndroidRelrTags
= false;
239 llvm::SmallVector
<llvm::GlobPattern
, 0> warnBackrefsExclude
;
241 bool warnMissingEntry
;
242 bool warnSymbolOrdering
;
253 bool zKeepTextSectionPrefix
;
264 uint8_t zStartStopVisibility
;
268 DiscardPolicy discard
;
269 GnuStackKind zGnustack
;
271 OrphanHandlingPolicy orphanHandling
;
272 SortSectionPolicy sortSection
;
274 UnresolvedPolicy unresolvedSymbols
;
275 UnresolvedPolicy unresolvedSymbolsInShlib
;
276 Target2Policy target2
;
278 ARMVFPArgKind armVFPArgs
= ARMVFPArgKind::Default
;
279 BuildIdKind buildId
= BuildIdKind::None
;
280 SeparateSegmentKind zSeparate
;
281 ELFKind ekind
= ELFNoneKind
;
282 uint16_t emachine
= llvm::ELF::EM_NONE
;
283 llvm::Optional
<uint64_t> imageBase
;
284 uint64_t commonPageSize
;
285 uint64_t maxPageSize
;
286 uint64_t mipsGotSize
;
288 unsigned ltoPartitions
;
291 StringRef thinLTOJobs
;
292 unsigned timeTraceGranularity
;
293 int32_t splitStackAdjustSize
;
294 StringRef packageMetadata
;
296 // The following config options do not directly correspond to any
297 // particular command line options.
299 // True if we need to pass through relocations in input files to the
300 // output file. Usually false because we consume relocations.
303 // True if the target is ELF64. False if ELF32.
306 // True if the target is little-endian. False if big-endian.
309 // endianness::little if isLE is true. endianness::big otherwise.
310 llvm::support::endianness endianness
;
312 // True if the target is the little-endian MIPS64.
314 // The reason why we have this variable only for the MIPS is because
315 // we use this often. Some ELF headers for MIPS64EL are in a
316 // mixed-endian (which is horrible and I'd say that's a serious spec
317 // bug), and we need to know whether we are reading MIPS ELF files or
318 // not in various places.
320 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
321 // name whatever that means. A fun hypothesis is that "EL" is short for
322 // little-endian written in the little-endian order, but I don't know
326 // True if we need to reserve two .got entries for local-dynamic TLS model.
327 bool needsTlsLd
= false;
329 // True if we need to set the DF_STATIC_TLS flag to an output file, which
330 // works as a hint to the dynamic loader that the shared object contains code
331 // compiled with the initial-exec TLS model.
332 bool hasTlsIe
= false;
334 // Holds set of ELF header flags for the target.
337 // The ELF spec defines two types of relocation table entries, RELA and
338 // REL. RELA is a triplet of (offset, info, addend) while REL is a
339 // tuple of (offset, info). Addends for REL are implicit and read from
340 // the location where the relocations are applied. So, REL is more
341 // compact than RELA but requires a bit of more work to process.
343 // (From the linker writer's view, this distinction is not necessary.
344 // If the ELF had chosen whichever and sticked with it, it would have
345 // been easier to write code to process relocations, but it's too late
346 // to change the spec.)
348 // Each ABI defines its relocation type. IsRela is true if target
349 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
350 // few 32-bit ABIs are using RELA too.
353 // True if we are creating position-independent code.
356 // 4 for ELF32, 8 for ELF64.
359 // Mode of MTE to write to the ELF note. Should be one of NT_MEMTAG_ASYNC (for
360 // async), NT_MEMTAG_SYNC (for sync), or NT_MEMTAG_LEVEL_NONE (for none). If
361 // async or sync is enabled, write the ELF note specifying the default MTE
363 int androidMemtagMode
;
364 // Signal to the dynamic loader to enable heap MTE.
365 bool androidMemtagHeap
;
366 // Signal to the dynamic loader that this binary expects stack MTE. Generally,
367 // this means to map the primary and thread stacks as PROT_MTE. Note: This is
368 // not supported on Android 11 & 12.
369 bool androidMemtagStack
;
372 // The only instance of Configuration struct.
373 extern std::unique_ptr
<Configuration
> config
;
375 struct DuplicateSymbol
{
377 const InputFile
*file
;
378 InputSectionBase
*section
;
383 SmallVector
<std::unique_ptr
<MemoryBuffer
>> memoryBuffers
;
384 SmallVector
<ELFFileBase
*, 0> objectFiles
;
385 SmallVector
<SharedFile
*, 0> sharedFiles
;
386 SmallVector
<BinaryFile
*, 0> binaryFiles
;
387 SmallVector
<BitcodeFile
*, 0> bitcodeFiles
;
388 SmallVector
<BitcodeFile
*, 0> lazyBitcodeFiles
;
389 // Duplicate symbol candidates.
390 SmallVector
<DuplicateSymbol
, 0> duplicates
;
391 // Symbols in a non-prevailing COMDAT group which should be changed to an
393 SmallVector
<std::pair
<Symbol
*, unsigned>, 0> nonPrevailingSyms
;
394 // True if SHT_LLVM_SYMPART is used.
395 std::atomic
<bool> hasSympart
{false};
396 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
397 SmallVector
<std::tuple
<std::string
, const InputFile
*, const Symbol
&>, 0>
399 // A mapping from a symbol to an InputFile referencing it backward. Used by
401 llvm::DenseMap
<const Symbol
*,
402 std::pair
<const InputFile
*, const InputFile
*>>
406 // The only instance of Ctx struct.
407 extern std::unique_ptr
<Ctx
> ctx
;
409 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
410 // VER_NDX_GLOBAL. This helper returns other elements.
411 static inline ArrayRef
<VersionDefinition
> namedVersionDefs() {
412 return llvm::makeArrayRef(config
->versionDefinitions
).slice(2);
415 void errorOrWarn(const Twine
&msg
);
417 static inline void internalLinkerError(StringRef loc
, const Twine
&msg
) {
418 errorOrWarn(loc
+ "internal linker error: " + msg
+ "\n" +
419 llvm::getBugReportMsg());