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/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"
20 #include <atomic>
21 #include <vector>
22
23 namespace lld {
24 namespace elf {
25
26 class InputFile;
27 class InputSectionBase;
28
29 enum ELFKind {
30 ELFNoneKind,
31 ELF32LEKind,
32 ELF32BEKind,
33 ELF64LEKind,
34 ELF64BEKind
35 };
36
37 // For --build-id.
38 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };
39
40 // For --discard-{all,locals,none}.
41 enum class DiscardPolicy { Default, All, Locals, None };
42
43 // For --icf={none,safe,all}.
44 enum class ICFLevel { None, Safe, All };
45
46 // For --strip-{all,debug}.
47 enum class StripPolicy { None, All, Debug };
48
49 // For --unresolved-symbols.
50 enum class UnresolvedPolicy { ReportError, Warn, Ignore };
51
52 // For --orphan-handling.
53 enum class OrphanHandlingPolicy { Place, Warn, Error };
54
55 // For --sort-section and linkerscript sorting rules.
56 enum class SortSectionPolicy { Default, None, Alignment, Name, Priority };
57
58 // For --target2
59 enum class Target2Policy { Abs, Rel, GotRel };
60
61 // For tracking ARM Float Argument PCS
62 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
63
64 struct SymbolVersion {
65 llvm::StringRef name;
66 bool isExternCpp;
67 bool hasWildcard;
68 };
69
70 // This struct contains symbols version definition that
71 // can be found in version script if it is used for link.
72 struct VersionDefinition {
73 llvm::StringRef name;
74 uint16_t id = 0;
75 std::vector<SymbolVersion> globals;
76 };
77
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 {
83 uint8_t osabi = 0;
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;
92 llvm::StringRef fini;
93 llvm::StringRef init;
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;
112 std::string rpath;
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 *>,
124 uint64_t>
125 callGraphProfile;
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;
133 bool bsymbolic;
134 bool bsymbolicFunctions;
135 bool callGraphProfileSort;
136 bool checkSections;
137 bool compressDebugSections;
138 bool cref;
139 bool defineCommon;
140 bool demangle = true;
141 bool dependentLibraries;
142 bool disableVerify;
143 bool ehFrameHdr;
144 bool emitLLVM;
145 bool emitRelocs;
146 bool enableNewDtags;
147 bool executeOnly;
148 bool exportDynamic;
149 bool fixCortexA53Errata843419;
150 bool forceBTI;
151 bool formatBinary = false;
152 bool requireCET;
153 bool gcSections;
154 bool gdbIndex;
155 bool gnuHash = false;
156 bool gnuUnique;
157 bool hasDynamicList = false;
158 bool hasDynSymTab;
159 bool ignoreDataAddressEquality;
160 bool ignoreFunctionAddressEquality;
161 bool ltoCSProfileGenerate;
162 bool ltoDebugPassManager;
163 bool ltoNewPassManager;
164 bool mergeArmExidx;
165 bool mipsN32Abi = false;
166 bool nmagic;
167 bool noinhibitExec;
168 bool nostdlib;
169 bool oFormatBinary;
170 bool omagic;
171 bool optRemarksWithHotness;
172 bool pacPlt;
173 bool picThunk;
174 bool pie;
175 bool printGcSections;
176 bool printIcfSections;
177 bool relocatable;
178 bool relrPackDynRelocs;
179 bool saveTemps;
180 bool singleRoRx;
181 bool shared;
182 bool isStatic = false;
183 bool sysvHash = false;
184 bool target1Rel;
185 bool trace;
186 bool thinLTOEmitImportsFiles;
187 bool thinLTOIndexOnly;
188 bool tocOptimize;
189 bool undefinedVersion;
190 bool useAndroidRelrTags = false;
191 bool warnBackrefs;
192 bool warnCommon;
193 bool warnIfuncTextrel;
194 bool warnMissingEntry;
195 bool warnSymbolOrdering;
196 bool writeAddends;
197 bool zCombreloc;
198 bool zCopyreloc;
199 bool zExecstack;
200 bool zGlobal;
201 bool zHazardplt;
202 bool zIfuncNoplt;
203 bool zInitfirst;
204 bool zInterpose;
205 bool zKeepTextSectionPrefix;
206 bool zNodefaultlib;
207 bool zNodelete;
208 bool zNodlopen;
209 bool zNow;
210 bool zOrigin;
211 bool zRelro;
212 bool zRodynamic;
213 bool zText;
214 bool zRetpolineplt;
215 bool zWxneeded;
216 DiscardPolicy discard;
217 ICFLevel icf;
218 OrphanHandlingPolicy orphanHandling;
219 SortSectionPolicy sortSection;
220 StripPolicy strip;
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;
232 uint64_t zStackSize;
233 unsigned ltoPartitions;
234 unsigned ltoo;
235 unsigned optimize;
236 unsigned thinLTOJobs;
237 int32_t splitStackAdjustSize;
238
239 // The following config options do not directly correspond to any
240 // particualr command line options.
241
242 // True if we need to pass through relocations in input files to the
243 // output file. Usually false because we consume relocations.
244 bool copyRelocs;
245
246 // True if the target is ELF64. False if ELF32.
247 bool is64;
248
249 // True if the target is little-endian. False if big-endian.
250 bool isLE;
251
252 // endianness::little if isLE is true. endianness::big otherwise.
253 llvm::support::endianness endianness;
254
255 // True if the target is the little-endian MIPS64.
256 //
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.
262 //
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
266 // if that's true.)
267 bool isMips64EL;
268
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().
274 //
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.
277 //
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};
282
283 // Holds set of ELF header flags for the target.
284 uint32_t eflags = 0;
285
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.
291 //
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.)
296 //
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.
300 bool isRela;
301
302 // True if we are creating position-independent code.
303 bool isPic;
304
305 // 4 for ELF32, 8 for ELF64.
306 int wordsize;
307 };
308
309 // The only instance of Configuration struct.
310 extern Configuration *config;
311
errorOrWarn(const Twine & msg)312 static inline void errorOrWarn(const Twine &msg) {
313 if (!config->noinhibitExec)
314 error(msg);
315 else
316 warn(msg);
317 }
318 } // namespace elf
319 } // namespace lld
320
321 #endif
322