xref: /freebsd/contrib/llvm-project/lld/ELF/Config.h (revision 56e766af41cd68310f5583bb893b13c006fcb44f)
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 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
65 enum class SeparateSegmentKind { None, Code, Loadable };
66 
67 // For -z *stack
68 enum class GnuStackKind { None, Exec, NoExec };
69 
70 struct SymbolVersion {
71   llvm::StringRef name;
72   bool isExternCpp;
73   bool hasWildcard;
74 };
75 
76 // This struct contains symbols version definition that
77 // can be found in version script if it is used for link.
78 struct VersionDefinition {
79   llvm::StringRef name;
80   uint16_t id;
81   std::vector<SymbolVersion> patterns;
82 };
83 
84 // This struct contains the global configuration for the linker.
85 // Most fields are direct mapping from the command line options
86 // and such fields have the same name as the corresponding options.
87 // Most fields are initialized by the driver.
88 struct Configuration {
89   uint8_t osabi = 0;
90   uint32_t andFeatures = 0;
91   llvm::CachePruningPolicy thinLTOCachePolicy;
92   llvm::StringMap<uint64_t> sectionStartMap;
93   llvm::StringRef chroot;
94   llvm::StringRef dynamicLinker;
95   llvm::StringRef dwoDir;
96   llvm::StringRef entry;
97   llvm::StringRef emulation;
98   llvm::StringRef fini;
99   llvm::StringRef init;
100   llvm::StringRef ltoAAPipeline;
101   llvm::StringRef ltoCSProfileFile;
102   llvm::StringRef ltoNewPmPasses;
103   llvm::StringRef ltoObjPath;
104   llvm::StringRef ltoSampleProfile;
105   llvm::StringRef mapFile;
106   llvm::StringRef outputFile;
107   llvm::StringRef optRemarksFilename;
108   llvm::StringRef optRemarksPasses;
109   llvm::StringRef optRemarksFormat;
110   llvm::StringRef progName;
111   llvm::StringRef printSymbolOrder;
112   llvm::StringRef soName;
113   llvm::StringRef sysroot;
114   llvm::StringRef thinLTOCacheDir;
115   llvm::StringRef thinLTOIndexOnlyArg;
116   std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
117   std::pair<llvm::StringRef, llvm::StringRef> thinLTOPrefixReplace;
118   std::string rpath;
119   std::vector<VersionDefinition> versionDefinitions;
120   std::vector<llvm::StringRef> auxiliaryList;
121   std::vector<llvm::StringRef> filterList;
122   std::vector<llvm::StringRef> searchPaths;
123   std::vector<llvm::StringRef> symbolOrderingFile;
124   std::vector<llvm::StringRef> undefined;
125   std::vector<SymbolVersion> dynamicList;
126   std::vector<uint8_t> buildIdVector;
127   llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
128                   uint64_t>
129       callGraphProfile;
130   bool allowMultipleDefinition;
131   bool allowShlibUndefined;
132   bool androidPackDynRelocs;
133   bool armHasBlx = false;
134   bool armHasMovtMovw = false;
135   bool armJ1J2BranchEncoding = false;
136   bool asNeeded = false;
137   bool bsymbolic;
138   bool bsymbolicFunctions;
139   bool callGraphProfileSort;
140   bool checkSections;
141   bool compressDebugSections;
142   bool cref;
143   bool defineCommon;
144   bool demangle = true;
145   bool dependentLibraries;
146   bool disableVerify;
147   bool ehFrameHdr;
148   bool emitLLVM;
149   bool emitRelocs;
150   bool enableNewDtags;
151   bool executeOnly;
152   bool exportDynamic;
153   bool fixCortexA53Errata843419;
154   bool fixCortexA8;
155   bool forceBTI;
156   bool formatBinary = false;
157   bool gcSections;
158   bool gdbIndex;
159   bool gnuHash = false;
160   bool gnuUnique;
161   bool hasDynamicList = false;
162   bool hasDynSymTab;
163   bool ignoreDataAddressEquality;
164   bool ignoreFunctionAddressEquality;
165   bool ltoCSProfileGenerate;
166   bool ltoDebugPassManager;
167   bool ltoNewPassManager;
168   bool mergeArmExidx;
169   bool mipsN32Abi = false;
170   bool mmapOutputFile;
171   bool nmagic;
172   bool noDynamicLinker = false;
173   bool noinhibitExec;
174   bool nostdlib;
175   bool oFormatBinary;
176   bool omagic;
177   bool optRemarksWithHotness;
178   bool pacPlt;
179   bool picThunk;
180   bool pie;
181   bool printGcSections;
182   bool printIcfSections;
183   bool relocatable;
184   bool relrPackDynRelocs;
185   bool saveTemps;
186   bool singleRoRx;
187   bool shared;
188   bool isStatic = false;
189   bool sysvHash = false;
190   bool target1Rel;
191   bool trace;
192   bool thinLTOEmitImportsFiles;
193   bool thinLTOIndexOnly;
194   bool tocOptimize;
195   bool undefinedVersion;
196   bool useAndroidRelrTags = false;
197   bool warnBackrefs;
198   bool warnCommon;
199   bool warnIfuncTextrel;
200   bool warnMissingEntry;
201   bool warnSymbolOrdering;
202   bool writeAddends;
203   bool zCombreloc;
204   bool zCopyreloc;
205   bool zForceIbt;
206   bool zGlobal;
207   bool zHazardplt;
208   bool zIfuncNoplt;
209   bool zInitfirst;
210   bool zInterpose;
211   bool zKeepTextSectionPrefix;
212   bool zNodefaultlib;
213   bool zNodelete;
214   bool zNodlopen;
215   bool zNow;
216   bool zOrigin;
217   bool zRelro;
218   bool zRodynamic;
219   bool zShstk;
220   bool zText;
221   bool zRetpolineplt;
222   bool zWxneeded;
223   DiscardPolicy discard;
224   GnuStackKind zGnustack;
225   ICFLevel icf;
226   OrphanHandlingPolicy orphanHandling;
227   SortSectionPolicy sortSection;
228   StripPolicy strip;
229   UnresolvedPolicy unresolvedSymbols;
230   Target2Policy target2;
231   ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default;
232   BuildIdKind buildId = BuildIdKind::None;
233   SeparateSegmentKind zSeparate;
234   ELFKind ekind = ELFNoneKind;
235   uint16_t emachine = llvm::ELF::EM_NONE;
236   llvm::Optional<uint64_t> imageBase;
237   uint64_t commonPageSize;
238   uint64_t maxPageSize;
239   uint64_t mipsGotSize;
240   uint64_t zStackSize;
241   unsigned ltoPartitions;
242   unsigned ltoo;
243   unsigned optimize;
244   unsigned thinLTOJobs;
245   int32_t splitStackAdjustSize;
246 
247   // The following config options do not directly correspond to any
248   // particular command line options.
249 
250   // True if we need to pass through relocations in input files to the
251   // output file. Usually false because we consume relocations.
252   bool copyRelocs;
253 
254   // True if the target is ELF64. False if ELF32.
255   bool is64;
256 
257   // True if the target is little-endian. False if big-endian.
258   bool isLE;
259 
260   // endianness::little if isLE is true. endianness::big otherwise.
261   llvm::support::endianness endianness;
262 
263   // True if the target is the little-endian MIPS64.
264   //
265   // The reason why we have this variable only for the MIPS is because
266   // we use this often.  Some ELF headers for MIPS64EL are in a
267   // mixed-endian (which is horrible and I'd say that's a serious spec
268   // bug), and we need to know whether we are reading MIPS ELF files or
269   // not in various places.
270   //
271   // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
272   // name whatever that means. A fun hypothesis is that "EL" is short for
273   // little-endian written in the little-endian order, but I don't know
274   // if that's true.)
275   bool isMips64EL;
276 
277   // True if we need to set the DF_STATIC_TLS flag to an output file,
278   // which works as a hint to the dynamic loader that the file contains
279   // code compiled with the static TLS model. The thread-local variable
280   // compiled with the static TLS model is faster but less flexible, and
281   // it may not be loaded using dlopen().
282   //
283   // We set this flag to true when we see a relocation for the static TLS
284   // model. Once this becomes true, it will never become false.
285   //
286   // Since the flag is updated by multi-threaded code, we use std::atomic.
287   // (Writing to a variable is not considered thread-safe even if the
288   // variable is boolean and we always set the same value from all threads.)
289   std::atomic<bool> hasStaticTlsModel{false};
290 
291   // Holds set of ELF header flags for the target.
292   uint32_t eflags = 0;
293 
294   // The ELF spec defines two types of relocation table entries, RELA and
295   // REL. RELA is a triplet of (offset, info, addend) while REL is a
296   // tuple of (offset, info). Addends for REL are implicit and read from
297   // the location where the relocations are applied. So, REL is more
298   // compact than RELA but requires a bit of more work to process.
299   //
300   // (From the linker writer's view, this distinction is not necessary.
301   // If the ELF had chosen whichever and sticked with it, it would have
302   // been easier to write code to process relocations, but it's too late
303   // to change the spec.)
304   //
305   // Each ABI defines its relocation type. IsRela is true if target
306   // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
307   // few 32-bit ABIs are using RELA too.
308   bool isRela;
309 
310   // True if we are creating position-independent code.
311   bool isPic;
312 
313   // 4 for ELF32, 8 for ELF64.
314   int wordsize;
315 };
316 
317 // The only instance of Configuration struct.
318 extern Configuration *config;
319 
320 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
321 // VER_NDX_GLOBAL. This helper returns other elements.
322 static inline ArrayRef<VersionDefinition> namedVersionDefs() {
323   return llvm::makeArrayRef(config->versionDefinitions).slice(2);
324 }
325 
326 static inline void errorOrWarn(const Twine &msg) {
327   if (!config->noinhibitExec)
328     error(msg);
329   else
330     warn(msg);
331 }
332 } // namespace elf
333 } // namespace lld
334 
335 #endif
336