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/CachedHashString.h"
14 #include "llvm/ADT/MapVector.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/Support/CachePruning.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/GlobPattern.h"
23 #include "llvm/Support/PrettyStackTrace.h"
24 #include <atomic>
25 #include <vector>
26
27 namespace lld {
28 namespace elf {
29
30 class InputFile;
31 class InputSectionBase;
32
33 enum ELFKind {
34 ELFNoneKind,
35 ELF32LEKind,
36 ELF32BEKind,
37 ELF64LEKind,
38 ELF64BEKind
39 };
40
41 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions,
42 // -Bsymbolic.
43 enum class BsymbolicKind { None, NonWeakFunctions, Functions, All };
44
45 // For --build-id.
46 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };
47
48 // For --discard-{all,locals,none}.
49 enum class DiscardPolicy { Default, All, Locals, None };
50
51 // For --icf={none,safe,all}.
52 enum class ICFLevel { None, Safe, All };
53
54 // For --strip-{all,debug}.
55 enum class StripPolicy { None, All, Debug };
56
57 // For --unresolved-symbols.
58 enum class UnresolvedPolicy { ReportError, Warn, Ignore };
59
60 // For --orphan-handling.
61 enum class OrphanHandlingPolicy { Place, Warn, Error };
62
63 // For --sort-section and linkerscript sorting rules.
64 enum class SortSectionPolicy { Default, None, Alignment, Name, Priority };
65
66 // For --target2
67 enum class Target2Policy { Abs, Rel, GotRel };
68
69 // For tracking ARM Float Argument PCS
70 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
71
72 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
73 enum class SeparateSegmentKind { None, Code, Loadable };
74
75 // For -z *stack
76 enum class GnuStackKind { None, Exec, NoExec };
77
78 struct SymbolVersion {
79 llvm::StringRef name;
80 bool isExternCpp;
81 bool hasWildcard;
82 };
83
84 // This struct contains symbols version definition that
85 // can be found in version script if it is used for link.
86 struct VersionDefinition {
87 llvm::StringRef name;
88 uint16_t id;
89 std::vector<SymbolVersion> nonLocalPatterns;
90 std::vector<SymbolVersion> localPatterns;
91 };
92
93 // This struct contains the global configuration for the linker.
94 // Most fields are direct mapping from the command line options
95 // and such fields have the same name as the corresponding options.
96 // Most fields are initialized by the driver.
97 struct Configuration {
98 uint8_t osabi = 0;
99 uint32_t andFeatures = 0;
100 llvm::CachePruningPolicy thinLTOCachePolicy;
101 llvm::SetVector<llvm::CachedHashString> dependencyFiles; // for --dependency-file
102 llvm::StringMap<uint64_t> sectionStartMap;
103 llvm::StringRef bfdname;
104 llvm::StringRef chroot;
105 llvm::StringRef dependencyFile;
106 llvm::StringRef dwoDir;
107 llvm::StringRef dynamicLinker;
108 llvm::StringRef entry;
109 llvm::StringRef emulation;
110 llvm::StringRef fini;
111 llvm::StringRef init;
112 llvm::StringRef ltoAAPipeline;
113 llvm::StringRef ltoCSProfileFile;
114 llvm::StringRef ltoNewPmPasses;
115 llvm::StringRef ltoObjPath;
116 llvm::StringRef ltoSampleProfile;
117 llvm::StringRef mapFile;
118 llvm::StringRef outputFile;
119 llvm::StringRef optRemarksFilename;
120 llvm::Optional<uint64_t> optRemarksHotnessThreshold = 0;
121 llvm::StringRef optRemarksPasses;
122 llvm::StringRef optRemarksFormat;
123 llvm::StringRef progName;
124 llvm::StringRef printArchiveStats;
125 llvm::StringRef printSymbolOrder;
126 llvm::StringRef soName;
127 llvm::StringRef sysroot;
128 llvm::StringRef thinLTOCacheDir;
129 llvm::StringRef thinLTOIndexOnlyArg;
130 llvm::StringRef ltoBasicBlockSections;
131 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
132 std::pair<llvm::StringRef, llvm::StringRef> thinLTOPrefixReplace;
133 std::string rpath;
134 std::vector<VersionDefinition> versionDefinitions;
135 std::vector<llvm::StringRef> auxiliaryList;
136 std::vector<llvm::StringRef> filterList;
137 std::vector<llvm::StringRef> searchPaths;
138 std::vector<llvm::StringRef> symbolOrderingFile;
139 std::vector<llvm::StringRef> thinLTOModulesToCompile;
140 std::vector<llvm::StringRef> undefined;
141 std::vector<SymbolVersion> dynamicList;
142 std::vector<uint8_t> buildIdVector;
143 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
144 uint64_t>
145 callGraphProfile;
146 bool allowMultipleDefinition;
147 bool androidPackDynRelocs;
148 bool armHasBlx = false;
149 bool armHasMovtMovw = false;
150 bool armJ1J2BranchEncoding = false;
151 bool asNeeded = false;
152 BsymbolicKind bsymbolic = BsymbolicKind::None;
153 bool callGraphProfileSort;
154 bool checkSections;
155 bool checkDynamicRelocs;
156 bool compressDebugSections;
157 bool cref;
158 std::vector<std::pair<llvm::GlobPattern, uint64_t>> deadRelocInNonAlloc;
159 bool defineCommon;
160 bool demangle = true;
161 bool dependentLibraries;
162 bool disableVerify;
163 bool ehFrameHdr;
164 bool emitLLVM;
165 bool emitRelocs;
166 bool enableNewDtags;
167 bool executeOnly;
168 bool exportDynamic;
169 bool fixCortexA53Errata843419;
170 bool fixCortexA8;
171 bool formatBinary = false;
172 bool fortranCommon;
173 bool gcSections;
174 bool gdbIndex;
175 bool gnuHash = false;
176 bool gnuUnique;
177 bool hasDynSymTab;
178 bool ignoreDataAddressEquality;
179 bool ignoreFunctionAddressEquality;
180 bool ltoCSProfileGenerate;
181 bool ltoDebugPassManager;
182 bool ltoEmitAsm;
183 bool ltoNewPassManager;
184 bool ltoPseudoProbeForProfiling;
185 bool ltoUniqueBasicBlockSectionNames;
186 bool ltoWholeProgramVisibility;
187 bool mergeArmExidx;
188 bool mipsN32Abi = false;
189 bool mmapOutputFile;
190 bool nmagic;
191 bool noDynamicLinker = false;
192 bool noinhibitExec;
193 bool nostdlib;
194 bool oFormatBinary;
195 bool omagic;
196 bool optEB = false;
197 bool optEL = false;
198 bool optimizeBBJumps;
199 bool optRemarksWithHotness;
200 bool picThunk;
201 bool pie;
202 bool printGcSections;
203 bool printIcfSections;
204 bool relocatable;
205 bool relrPackDynRelocs;
206 bool saveTemps;
207 std::vector<std::pair<llvm::GlobPattern, uint32_t>> shuffleSections;
208 bool singleRoRx;
209 bool shared;
210 bool symbolic;
211 bool isStatic = false;
212 bool sysvHash = false;
213 bool target1Rel;
214 bool trace;
215 bool thinLTOEmitImportsFiles;
216 bool thinLTOIndexOnly;
217 bool timeTraceEnabled;
218 bool tocOptimize;
219 bool pcRelOptimize;
220 bool undefinedVersion;
221 bool unique;
222 bool useAndroidRelrTags = false;
223 bool warnBackrefs;
224 std::vector<llvm::GlobPattern> warnBackrefsExclude;
225 bool warnCommon;
226 bool warnMissingEntry;
227 bool warnSymbolOrdering;
228 bool writeAddends;
229 bool zCombreloc;
230 bool zCopyreloc;
231 bool zForceBti;
232 bool zForceIbt;
233 bool zGlobal;
234 bool zHazardplt;
235 bool zIfuncNoplt;
236 bool zInitfirst;
237 bool zInterpose;
238 bool zKeepTextSectionPrefix;
239 bool zNodefaultlib;
240 bool zNodelete;
241 bool zNodlopen;
242 bool zNow;
243 bool zOrigin;
244 bool zPacPlt;
245 bool zRelro;
246 bool zRodynamic;
247 bool zShstk;
248 bool zStartStopGC;
249 uint8_t zStartStopVisibility;
250 bool zText;
251 bool zRetpolineplt;
252 bool zWxneeded;
253 DiscardPolicy discard;
254 GnuStackKind zGnustack;
255 ICFLevel icf;
256 OrphanHandlingPolicy orphanHandling;
257 SortSectionPolicy sortSection;
258 StripPolicy strip;
259 UnresolvedPolicy unresolvedSymbols;
260 UnresolvedPolicy unresolvedSymbolsInShlib;
261 Target2Policy target2;
262 bool Power10Stub;
263 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default;
264 BuildIdKind buildId = BuildIdKind::None;
265 SeparateSegmentKind zSeparate;
266 ELFKind ekind = ELFNoneKind;
267 uint16_t emachine = llvm::ELF::EM_NONE;
268 llvm::Optional<uint64_t> imageBase;
269 uint64_t commonPageSize;
270 uint64_t maxPageSize;
271 uint64_t mipsGotSize;
272 uint64_t zStackSize;
273 unsigned ltoPartitions;
274 unsigned ltoo;
275 unsigned optimize;
276 StringRef thinLTOJobs;
277 unsigned timeTraceGranularity;
278 int32_t splitStackAdjustSize;
279
280 // The following config options do not directly correspond to any
281 // particular command line options.
282
283 // True if we need to pass through relocations in input files to the
284 // output file. Usually false because we consume relocations.
285 bool copyRelocs;
286
287 // True if the target is ELF64. False if ELF32.
288 bool is64;
289
290 // True if the target is little-endian. False if big-endian.
291 bool isLE;
292
293 // endianness::little if isLE is true. endianness::big otherwise.
294 llvm::support::endianness endianness;
295
296 // True if the target is the little-endian MIPS64.
297 //
298 // The reason why we have this variable only for the MIPS is because
299 // we use this often. Some ELF headers for MIPS64EL are in a
300 // mixed-endian (which is horrible and I'd say that's a serious spec
301 // bug), and we need to know whether we are reading MIPS ELF files or
302 // not in various places.
303 //
304 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
305 // name whatever that means. A fun hypothesis is that "EL" is short for
306 // little-endian written in the little-endian order, but I don't know
307 // if that's true.)
308 bool isMips64EL;
309
310 // True if we need to set the DF_STATIC_TLS flag to an output file,
311 // which works as a hint to the dynamic loader that the file contains
312 // code compiled with the static TLS model. The thread-local variable
313 // compiled with the static TLS model is faster but less flexible, and
314 // it may not be loaded using dlopen().
315 //
316 // We set this flag to true when we see a relocation for the static TLS
317 // model. Once this becomes true, it will never become false.
318 //
319 // Since the flag is updated by multi-threaded code, we use std::atomic.
320 // (Writing to a variable is not considered thread-safe even if the
321 // variable is boolean and we always set the same value from all threads.)
322 std::atomic<bool> hasStaticTlsModel{false};
323
324 // Holds set of ELF header flags for the target.
325 uint32_t eflags = 0;
326
327 // The ELF spec defines two types of relocation table entries, RELA and
328 // REL. RELA is a triplet of (offset, info, addend) while REL is a
329 // tuple of (offset, info). Addends for REL are implicit and read from
330 // the location where the relocations are applied. So, REL is more
331 // compact than RELA but requires a bit of more work to process.
332 //
333 // (From the linker writer's view, this distinction is not necessary.
334 // If the ELF had chosen whichever and sticked with it, it would have
335 // been easier to write code to process relocations, but it's too late
336 // to change the spec.)
337 //
338 // Each ABI defines its relocation type. IsRela is true if target
339 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
340 // few 32-bit ABIs are using RELA too.
341 bool isRela;
342
343 // True if we are creating position-independent code.
344 bool isPic;
345
346 // 4 for ELF32, 8 for ELF64.
347 int wordsize;
348 };
349
350 // The only instance of Configuration struct.
351 extern Configuration *config;
352
353 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
354 // VER_NDX_GLOBAL. This helper returns other elements.
namedVersionDefs()355 static inline ArrayRef<VersionDefinition> namedVersionDefs() {
356 return llvm::makeArrayRef(config->versionDefinitions).slice(2);
357 }
358
errorOrWarn(const Twine & msg)359 static inline void errorOrWarn(const Twine &msg) {
360 if (!config->noinhibitExec)
361 error(msg);
362 else
363 warn(msg);
364 }
365
internalLinkerError(StringRef loc,const Twine & msg)366 static inline void internalLinkerError(StringRef loc, const Twine &msg) {
367 errorOrWarn(loc + "internal linker error: " + msg + "\n" +
368 llvm::getBugReportMsg());
369 }
370
371 } // namespace elf
372 } // namespace lld
373
374 #endif
375