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/Option/ArgList.h"
21 #include "llvm/Support/CachePruning.h"
22 #include "llvm/Support/CodeGen.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Compression.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/GlobPattern.h"
27 #include "llvm/Support/PrettyStackTrace.h"
40 class InputSectionBase
;
43 class BitcodeCompiler
;
45 enum ELFKind
: uint8_t {
53 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions,
55 enum class BsymbolicKind
{ None
, NonWeakFunctions
, Functions
, All
};
58 enum class BuildIdKind
{ None
, Fast
, Md5
, Sha1
, Hexstring
, Uuid
};
60 // For --discard-{all,locals,none}.
61 enum class DiscardPolicy
{ Default
, All
, Locals
, None
};
63 // For --icf={none,safe,all}.
64 enum class ICFLevel
{ None
, Safe
, All
};
66 // For --strip-{all,debug}.
67 enum class StripPolicy
{ None
, All
, Debug
};
69 // For --unresolved-symbols.
70 enum class UnresolvedPolicy
{ ReportError
, Warn
, Ignore
};
72 // For --orphan-handling.
73 enum class OrphanHandlingPolicy
{ Place
, Warn
, Error
};
75 // For --sort-section and linkerscript sorting rules.
76 enum class SortSectionPolicy
{ Default
, None
, Alignment
, Name
, Priority
};
79 enum class Target2Policy
{ Abs
, Rel
, GotRel
};
81 // For tracking ARM Float Argument PCS
82 enum class ARMVFPArgKind
{ Default
, Base
, VFP
, ToolChain
};
84 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
85 enum class SeparateSegmentKind
{ None
, Code
, Loadable
};
88 enum class GnuStackKind
{ None
, Exec
, NoExec
};
90 struct SymbolVersion
{
96 // This struct contains symbols version definition that
97 // can be found in version script if it is used for link.
98 struct VersionDefinition
{
101 SmallVector
<SymbolVersion
, 0> nonLocalPatterns
;
102 SmallVector
<SymbolVersion
, 0> localPatterns
;
107 void linkerMain(ArrayRef
<const char *> args
);
108 void addFile(StringRef path
, bool withLOption
);
109 void addLibrary(StringRef name
);
112 void createFiles(llvm::opt::InputArgList
&args
);
113 void inferMachineType();
114 void link(llvm::opt::InputArgList
&args
);
115 template <class ELFT
> void compileBitcodeFiles(bool skipLinkedOutput
);
117 // True if we are in --whole-archive and --no-whole-archive.
118 bool inWholeArchive
= false;
120 // True if we are in --start-lib and --end-lib.
123 std::unique_ptr
<BitcodeCompiler
> lto
;
124 std::vector
<InputFile
*> files
;
127 SmallVector
<std::pair
<StringRef
, unsigned>, 0> archiveFiles
;
130 // This struct contains the global configuration for the linker.
131 // Most fields are direct mapping from the command line options
132 // and such fields have the same name as the corresponding options.
133 // Most fields are initialized by the ctx.driver.
136 uint32_t andFeatures
= 0;
137 llvm::CachePruningPolicy thinLTOCachePolicy
;
138 llvm::SetVector
<llvm::CachedHashString
> dependencyFiles
; // for --dependency-file
139 llvm::StringMap
<uint64_t> sectionStartMap
;
140 llvm::StringRef bfdname
;
141 llvm::StringRef chroot
;
142 llvm::StringRef dependencyFile
;
143 llvm::StringRef dwoDir
;
144 llvm::StringRef dynamicLinker
;
145 llvm::StringRef entry
;
146 llvm::StringRef emulation
;
147 llvm::StringRef fini
;
148 llvm::StringRef init
;
149 llvm::StringRef ltoAAPipeline
;
150 llvm::StringRef ltoCSProfileFile
;
151 llvm::StringRef ltoNewPmPasses
;
152 llvm::StringRef ltoObjPath
;
153 llvm::StringRef ltoSampleProfile
;
154 llvm::StringRef mapFile
;
155 llvm::StringRef outputFile
;
156 llvm::StringRef optRemarksFilename
;
157 std::optional
<uint64_t> optRemarksHotnessThreshold
= 0;
158 llvm::StringRef optRemarksPasses
;
159 llvm::StringRef optRemarksFormat
;
160 llvm::StringRef optStatsFilename
;
161 llvm::StringRef progName
;
162 llvm::StringRef printArchiveStats
;
163 llvm::StringRef printSymbolOrder
;
164 llvm::StringRef soName
;
165 llvm::StringRef sysroot
;
166 llvm::StringRef thinLTOCacheDir
;
167 llvm::StringRef thinLTOIndexOnlyArg
;
168 llvm::StringRef whyExtract
;
169 StringRef zBtiReport
= "none";
170 StringRef zCetReport
= "none";
171 llvm::StringRef ltoBasicBlockSections
;
172 std::pair
<llvm::StringRef
, llvm::StringRef
> thinLTOObjectSuffixReplace
;
173 std::pair
<llvm::StringRef
, llvm::StringRef
> thinLTOPrefixReplace
;
175 llvm::SmallVector
<VersionDefinition
, 0> versionDefinitions
;
176 llvm::SmallVector
<llvm::StringRef
, 0> auxiliaryList
;
177 llvm::SmallVector
<llvm::StringRef
, 0> filterList
;
178 llvm::SmallVector
<llvm::StringRef
, 0> passPlugins
;
179 llvm::SmallVector
<llvm::StringRef
, 0> searchPaths
;
180 llvm::SmallVector
<llvm::StringRef
, 0> symbolOrderingFile
;
181 llvm::SmallVector
<llvm::StringRef
, 0> thinLTOModulesToCompile
;
182 llvm::SmallVector
<llvm::StringRef
, 0> undefined
;
183 llvm::SmallVector
<SymbolVersion
, 0> dynamicList
;
184 llvm::SmallVector
<uint8_t, 0> buildIdVector
;
185 llvm::SmallVector
<llvm::StringRef
, 0> mllvmOpts
;
186 llvm::MapVector
<std::pair
<const InputSectionBase
*, const InputSectionBase
*>,
189 bool allowMultipleDefinition
;
190 bool androidPackDynRelocs
= false;
191 bool armHasBlx
= false;
192 bool armHasMovtMovw
= false;
193 bool armJ1J2BranchEncoding
= false;
194 bool asNeeded
= false;
195 BsymbolicKind bsymbolic
= BsymbolicKind::None
;
196 bool callGraphProfileSort
;
198 bool checkDynamicRelocs
;
199 llvm::DebugCompressionType compressDebugSections
;
201 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, uint64_t>, 0>
203 bool demangle
= true;
204 bool dependentLibraries
;
212 bool fixCortexA53Errata843419
;
214 bool formatBinary
= false;
218 bool gnuHash
= false;
221 bool ignoreDataAddressEquality
;
222 bool ignoreFunctionAddressEquality
;
223 bool ltoCSProfileGenerate
;
224 bool ltoPGOWarnMismatch
;
225 bool ltoDebugPassManager
;
227 bool ltoUniqueBasicBlockSectionNames
;
228 bool ltoWholeProgramVisibility
;
230 bool mipsN32Abi
= false;
233 bool noDynamicLinker
= false;
240 bool optimizeBBJumps
;
241 bool optRemarksWithHotness
;
244 bool printGcSections
;
245 bool printIcfSections
;
248 bool relrGlibc
= false;
249 bool relrPackDynRelocs
= false;
250 llvm::DenseSet
<llvm::StringRef
> saveTempsArgs
;
251 llvm::SmallVector
<std::pair
<llvm::GlobPattern
, uint32_t>, 0> shuffleSections
;
255 bool isStatic
= false;
256 bool sysvHash
= false;
259 bool thinLTOEmitImportsFiles
;
260 bool thinLTOEmitIndexFiles
;
261 bool thinLTOIndexOnly
;
262 bool timeTraceEnabled
;
265 bool undefinedVersion
;
267 bool useAndroidRelrTags
= false;
269 llvm::SmallVector
<llvm::GlobPattern
, 0> warnBackrefsExclude
;
271 bool warnMissingEntry
;
272 bool warnSymbolOrdering
;
283 bool zKeepTextSectionPrefix
;
294 uint8_t zStartStopVisibility
;
298 DiscardPolicy discard
;
299 GnuStackKind zGnustack
;
301 OrphanHandlingPolicy orphanHandling
;
302 SortSectionPolicy sortSection
;
304 UnresolvedPolicy unresolvedSymbols
;
305 UnresolvedPolicy unresolvedSymbolsInShlib
;
306 Target2Policy target2
;
308 ARMVFPArgKind armVFPArgs
= ARMVFPArgKind::Default
;
309 BuildIdKind buildId
= BuildIdKind::None
;
310 SeparateSegmentKind zSeparate
;
311 ELFKind ekind
= ELFNoneKind
;
312 uint16_t emachine
= llvm::ELF::EM_NONE
;
313 std::optional
<uint64_t> imageBase
;
314 uint64_t commonPageSize
;
315 uint64_t maxPageSize
;
316 uint64_t mipsGotSize
;
318 unsigned ltoPartitions
;
321 StringRef thinLTOJobs
;
322 unsigned timeTraceGranularity
;
323 int32_t splitStackAdjustSize
;
324 StringRef packageMetadata
;
326 // The following config options do not directly correspond to any
327 // particular command line options.
329 // True if we need to pass through relocations in input files to the
330 // output file. Usually false because we consume relocations.
333 // True if the target is ELF64. False if ELF32.
336 // True if the target is little-endian. False if big-endian.
339 // endianness::little if isLE is true. endianness::big otherwise.
340 llvm::support::endianness endianness
;
342 // True if the target is the little-endian MIPS64.
344 // The reason why we have this variable only for the MIPS is because
345 // we use this often. Some ELF headers for MIPS64EL are in a
346 // mixed-endian (which is horrible and I'd say that's a serious spec
347 // bug), and we need to know whether we are reading MIPS ELF files or
348 // not in various places.
350 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
351 // name whatever that means. A fun hypothesis is that "EL" is short for
352 // little-endian written in the little-endian order, but I don't know
356 // True if we need to set the DF_STATIC_TLS flag to an output file, which
357 // works as a hint to the dynamic loader that the shared object contains code
358 // compiled with the initial-exec TLS model.
359 bool hasTlsIe
= false;
361 // Holds set of ELF header flags for the target.
364 // The ELF spec defines two types of relocation table entries, RELA and
365 // REL. RELA is a triplet of (offset, info, addend) while REL is a
366 // tuple of (offset, info). Addends for REL are implicit and read from
367 // the location where the relocations are applied. So, REL is more
368 // compact than RELA but requires a bit of more work to process.
370 // (From the linker writer's view, this distinction is not necessary.
371 // If the ELF had chosen whichever and sticked with it, it would have
372 // been easier to write code to process relocations, but it's too late
373 // to change the spec.)
375 // Each ABI defines its relocation type. IsRela is true if target
376 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
377 // few 32-bit ABIs are using RELA too.
380 // True if we are creating position-independent code.
383 // 4 for ELF32, 8 for ELF64.
386 // Mode of MTE to write to the ELF note. Should be one of NT_MEMTAG_ASYNC (for
387 // async), NT_MEMTAG_SYNC (for sync), or NT_MEMTAG_LEVEL_NONE (for none). If
388 // async or sync is enabled, write the ELF note specifying the default MTE
390 int androidMemtagMode
;
391 // Signal to the dynamic loader to enable heap MTE.
392 bool androidMemtagHeap
;
393 // Signal to the dynamic loader that this binary expects stack MTE. Generally,
394 // this means to map the primary and thread stacks as PROT_MTE. Note: This is
395 // not supported on Android 11 & 12.
396 bool androidMemtagStack
;
398 unsigned threadCount
;
400 struct ConfigWrapper
{
402 Config
*operator->() { return &c
; }
405 LLVM_LIBRARY_VISIBILITY
extern ConfigWrapper config
;
407 struct DuplicateSymbol
{
409 const InputFile
*file
;
410 InputSectionBase
*section
;
416 SmallVector
<std::unique_ptr
<MemoryBuffer
>> memoryBuffers
;
417 SmallVector
<ELFFileBase
*, 0> objectFiles
;
418 SmallVector
<SharedFile
*, 0> sharedFiles
;
419 SmallVector
<BinaryFile
*, 0> binaryFiles
;
420 SmallVector
<BitcodeFile
*, 0> bitcodeFiles
;
421 SmallVector
<BitcodeFile
*, 0> lazyBitcodeFiles
;
422 SmallVector
<InputSectionBase
*, 0> inputSections
;
423 SmallVector
<EhInputSection
*, 0> ehInputSections
;
424 // Duplicate symbol candidates.
425 SmallVector
<DuplicateSymbol
, 0> duplicates
;
426 // Symbols in a non-prevailing COMDAT group which should be changed to an
428 SmallVector
<std::pair
<Symbol
*, unsigned>, 0> nonPrevailingSyms
;
429 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
430 SmallVector
<std::tuple
<std::string
, const InputFile
*, const Symbol
&>, 0>
432 // A mapping from a symbol to an InputFile referencing it backward. Used by
434 llvm::DenseMap
<const Symbol
*,
435 std::pair
<const InputFile
*, const InputFile
*>>
437 // True if SHT_LLVM_SYMPART is used.
438 std::atomic
<bool> hasSympart
{false};
439 // True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared.
440 std::atomic
<bool> hasTlsIe
{false};
441 // True if we need to reserve two .got entries for local-dynamic TLS model.
442 std::atomic
<bool> needsTlsLd
{false};
447 LLVM_LIBRARY_VISIBILITY
extern Ctx ctx
;
449 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
450 // VER_NDX_GLOBAL. This helper returns other elements.
451 static inline ArrayRef
<VersionDefinition
> namedVersionDefs() {
452 return llvm::ArrayRef(config
->versionDefinitions
).slice(2);
455 void errorOrWarn(const Twine
&msg
);
457 static inline void internalLinkerError(StringRef loc
, const Twine
&msg
) {
458 errorOrWarn(loc
+ "internal linker error: " + msg
+ "\n" +
459 llvm::getBugReportMsg());
462 } // namespace lld::elf