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/MapVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/Support/CachePruning.h"
18 #include "llvm/Support/CodeGen.h"
19 #include "llvm/Support/Endian.h"
27 class InputSectionBase;
38 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };
40 // For --discard-{all,locals,none}.
41 enum class DiscardPolicy { Default, All, Locals, None };
43 // For --icf={none,safe,all}.
44 enum class ICFLevel { None, Safe, All };
46 // For --strip-{all,debug}.
47 enum class StripPolicy { None, All, Debug };
49 // For --unresolved-symbols.
50 enum class UnresolvedPolicy { ReportError, Warn, Ignore };
52 // For --orphan-handling.
53 enum class OrphanHandlingPolicy { Place, Warn, Error };
55 // For --sort-section and linkerscript sorting rules.
56 enum class SortSectionPolicy { Default, None, Alignment, Name, Priority };
59 enum class Target2Policy { Abs, Rel, GotRel };
61 // For tracking ARM Float Argument PCS
62 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
64 struct SymbolVersion {
70 // This struct contains symbols version definition that
71 // can be found in version script if it is used for link.
72 struct VersionDefinition {
75 std::vector<SymbolVersion> globals;
78 // This struct contains the global configuration for the linker.
79 // Most fields are direct mapping from the command line options
80 // and such fields have the same name as the corresponding options.
81 // Most fields are initialized by the driver.
82 struct Configuration {
84 uint32_t andFeatures = 0;
85 llvm::CachePruningPolicy thinLTOCachePolicy;
86 llvm::StringMap<uint64_t> sectionStartMap;
87 llvm::StringRef chroot;
88 llvm::StringRef dynamicLinker;
89 llvm::StringRef dwoDir;
90 llvm::StringRef entry;
91 llvm::StringRef emulation;
94 llvm::StringRef ltoAAPipeline;
95 llvm::StringRef ltoCSProfileFile;
96 llvm::StringRef ltoNewPmPasses;
97 llvm::StringRef ltoObjPath;
98 llvm::StringRef ltoSampleProfile;
99 llvm::StringRef mapFile;
100 llvm::StringRef outputFile;
101 llvm::StringRef optRemarksFilename;
102 llvm::StringRef optRemarksPasses;
103 llvm::StringRef optRemarksFormat;
104 llvm::StringRef progName;
105 llvm::StringRef printSymbolOrder;
106 llvm::StringRef soName;
107 llvm::StringRef sysroot;
108 llvm::StringRef thinLTOCacheDir;
109 llvm::StringRef thinLTOIndexOnlyArg;
110 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
111 std::pair<llvm::StringRef, llvm::StringRef> thinLTOPrefixReplace;
113 std::vector<VersionDefinition> versionDefinitions;
114 std::vector<llvm::StringRef> auxiliaryList;
115 std::vector<llvm::StringRef> filterList;
116 std::vector<llvm::StringRef> searchPaths;
117 std::vector<llvm::StringRef> symbolOrderingFile;
118 std::vector<llvm::StringRef> undefined;
119 std::vector<SymbolVersion> dynamicList;
120 std::vector<SymbolVersion> versionScriptGlobals;
121 std::vector<SymbolVersion> versionScriptLocals;
122 std::vector<uint8_t> buildIdVector;
123 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
126 bool allowMultipleDefinition;
127 bool allowShlibUndefined;
128 bool androidPackDynRelocs;
129 bool armHasBlx = false;
130 bool armHasMovtMovw = false;
131 bool armJ1J2BranchEncoding = false;
132 bool asNeeded = false;
134 bool bsymbolicFunctions;
135 bool callGraphProfileSort;
137 bool compressDebugSections;
140 bool demangle = true;
141 bool dependentLibraries;
149 bool fixCortexA53Errata843419;
151 bool formatBinary = false;
155 bool gnuHash = false;
157 bool hasDynamicList = false;
159 bool ignoreDataAddressEquality;
160 bool ignoreFunctionAddressEquality;
161 bool ltoCSProfileGenerate;
162 bool ltoDebugPassManager;
163 bool ltoNewPassManager;
165 bool mipsN32Abi = false;
171 bool optRemarksWithHotness;
175 bool printGcSections;
176 bool printIcfSections;
178 bool relrPackDynRelocs;
182 bool isStatic = false;
183 bool sysvHash = false;
186 bool thinLTOEmitImportsFiles;
187 bool thinLTOIndexOnly;
189 bool undefinedVersion;
190 bool useAndroidRelrTags = false;
193 bool warnIfuncTextrel;
194 bool warnMissingEntry;
195 bool warnSymbolOrdering;
205 bool zKeepTextSectionPrefix;
216 DiscardPolicy discard;
218 OrphanHandlingPolicy orphanHandling;
219 SortSectionPolicy sortSection;
221 UnresolvedPolicy unresolvedSymbols;
222 Target2Policy target2;
223 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default;
224 BuildIdKind buildId = BuildIdKind::None;
225 ELFKind ekind = ELFNoneKind;
226 uint16_t defaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL;
227 uint16_t emachine = llvm::ELF::EM_NONE;
228 llvm::Optional<uint64_t> imageBase;
229 uint64_t commonPageSize;
230 uint64_t maxPageSize;
231 uint64_t mipsGotSize;
233 unsigned ltoPartitions;
236 unsigned thinLTOJobs;
237 int32_t splitStackAdjustSize;
239 // The following config options do not directly correspond to any
240 // particualr command line options.
242 // True if we need to pass through relocations in input files to the
243 // output file. Usually false because we consume relocations.
246 // True if the target is ELF64. False if ELF32.
249 // True if the target is little-endian. False if big-endian.
252 // endianness::little if isLE is true. endianness::big otherwise.
253 llvm::support::endianness endianness;
255 // True if the target is the little-endian MIPS64.
257 // The reason why we have this variable only for the MIPS is because
258 // we use this often. Some ELF headers for MIPS64EL are in a
259 // mixed-endian (which is horrible and I'd say that's a serious spec
260 // bug), and we need to know whether we are reading MIPS ELF files or
261 // not in various places.
263 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
264 // name whatever that means. A fun hypothesis is that "EL" is short for
265 // little-endian written in the little-endian order, but I don't know
269 // True if we need to set the DF_STATIC_TLS flag to an output file,
270 // which works as a hint to the dynamic loader that the file contains
271 // code compiled with the static TLS model. The thread-local variable
272 // compiled with the static TLS model is faster but less flexible, and
273 // it may not be loaded using dlopen().
275 // We set this flag to true when we see a relocation for the static TLS
276 // model. Once this becomes true, it will never become false.
278 // Since the flag is updated by multi-threaded code, we use std::atomic.
279 // (Writing to a variable is not considered thread-safe even if the
280 // variable is boolean and we always set the same value from all threads.)
281 std::atomic<bool> hasStaticTlsModel{false};
283 // Holds set of ELF header flags for the target.
286 // The ELF spec defines two types of relocation table entries, RELA and
287 // REL. RELA is a triplet of (offset, info, addend) while REL is a
288 // tuple of (offset, info). Addends for REL are implicit and read from
289 // the location where the relocations are applied. So, REL is more
290 // compact than RELA but requires a bit of more work to process.
292 // (From the linker writer's view, this distinction is not necessary.
293 // If the ELF had chosen whichever and sticked with it, it would have
294 // been easier to write code to process relocations, but it's too late
295 // to change the spec.)
297 // Each ABI defines its relocation type. IsRela is true if target
298 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
299 // few 32-bit ABIs are using RELA too.
302 // True if we are creating position-independent code.
305 // 4 for ELF32, 8 for ELF64.
309 // The only instance of Configuration struct.
310 extern Configuration *config;
312 static inline void errorOrWarn(const Twine &msg) {
313 if (!config->noinhibitExec)