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