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