• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ltoUniqueBasicBlockSectionNames;
179   bool ltoWholeProgramVisibility;
180   bool mergeArmExidx;
181   bool mipsN32Abi = false;
182   bool mmapOutputFile;
183   bool nmagic;
184   bool noDynamicLinker = false;
185   bool noinhibitExec;
186   bool nostdlib;
187   bool oFormatBinary;
188   bool omagic;
189   bool optimizeBBJumps;
190   bool optRemarksWithHotness;
191   bool picThunk;
192   bool pie;
193   bool printGcSections;
194   bool printIcfSections;
195   bool relocatable;
196   bool relrPackDynRelocs;
197   bool saveTemps;
198   llvm::Optional<uint32_t> shuffleSectionSeed;
199   bool singleRoRx;
200   bool shared;
201   bool symbolic;
202   bool isStatic = false;
203   bool sysvHash = false;
204   bool target1Rel;
205   bool trace;
206   bool thinLTOEmitImportsFiles;
207   bool thinLTOIndexOnly;
208   bool timeTraceEnabled;
209   bool tocOptimize;
210   bool pcRelOptimize;
211   bool undefinedVersion;
212   bool unique;
213   bool useAndroidRelrTags = false;
214   bool warnBackrefs;
215   std::vector<llvm::GlobPattern> warnBackrefsExclude;
216   bool warnCommon;
217   bool warnIfuncTextrel;
218   bool warnMissingEntry;
219   bool warnSymbolOrdering;
220   bool writeAddends;
221   bool zCombreloc;
222   bool zCopyreloc;
223   bool zForceBti;
224   bool zForceIbt;
225   bool zGlobal;
226   bool zHazardplt;
227   bool zIfuncNoplt;
228   bool zInitfirst;
229   bool zInterpose;
230   bool zKeepTextSectionPrefix;
231   bool zNodefaultlib;
232   bool zNodelete;
233   bool zNodlopen;
234   bool zNow;
235   bool zOrigin;
236   bool zPacPlt;
237   bool zRelro;
238   bool zRodynamic;
239   bool zShstk;
240   uint8_t zStartStopVisibility;
241   bool zText;
242   bool zRetpolineplt;
243   bool zWxneeded;
244   DiscardPolicy discard;
245   GnuStackKind zGnustack;
246   ICFLevel icf;
247   OrphanHandlingPolicy orphanHandling;
248   SortSectionPolicy sortSection;
249   StripPolicy strip;
250   UnresolvedPolicy unresolvedSymbols;
251   UnresolvedPolicy unresolvedSymbolsInShlib;
252   Target2Policy target2;
253   ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default;
254   BuildIdKind buildId = BuildIdKind::None;
255   SeparateSegmentKind zSeparate;
256   ELFKind ekind = ELFNoneKind;
257   uint16_t emachine = llvm::ELF::EM_NONE;
258   llvm::Optional<uint64_t> imageBase;
259   uint64_t commonPageSize;
260   uint64_t maxPageSize;
261   uint64_t mipsGotSize;
262   uint64_t zStackSize;
263   unsigned ltoPartitions;
264   unsigned ltoo;
265   unsigned optimize;
266   StringRef thinLTOJobs;
267   unsigned timeTraceGranularity;
268   int32_t splitStackAdjustSize;
269 
270   // The following config options do not directly correspond to any
271   // particular command line options.
272 
273   // True if we need to pass through relocations in input files to the
274   // output file. Usually false because we consume relocations.
275   bool copyRelocs;
276 
277   // True if the target is ELF64. False if ELF32.
278   bool is64;
279 
280   // True if the target is little-endian. False if big-endian.
281   bool isLE;
282 
283   // endianness::little if isLE is true. endianness::big otherwise.
284   llvm::support::endianness endianness;
285 
286   // True if the target is the little-endian MIPS64.
287   //
288   // The reason why we have this variable only for the MIPS is because
289   // we use this often.  Some ELF headers for MIPS64EL are in a
290   // mixed-endian (which is horrible and I'd say that's a serious spec
291   // bug), and we need to know whether we are reading MIPS ELF files or
292   // not in various places.
293   //
294   // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
295   // name whatever that means. A fun hypothesis is that "EL" is short for
296   // little-endian written in the little-endian order, but I don't know
297   // if that's true.)
298   bool isMips64EL;
299 
300   // True if we need to set the DF_STATIC_TLS flag to an output file,
301   // which works as a hint to the dynamic loader that the file contains
302   // code compiled with the static TLS model. The thread-local variable
303   // compiled with the static TLS model is faster but less flexible, and
304   // it may not be loaded using dlopen().
305   //
306   // We set this flag to true when we see a relocation for the static TLS
307   // model. Once this becomes true, it will never become false.
308   //
309   // Since the flag is updated by multi-threaded code, we use std::atomic.
310   // (Writing to a variable is not considered thread-safe even if the
311   // variable is boolean and we always set the same value from all threads.)
312   std::atomic<bool> hasStaticTlsModel{false};
313 
314   // Holds set of ELF header flags for the target.
315   uint32_t eflags = 0;
316 
317   // The ELF spec defines two types of relocation table entries, RELA and
318   // REL. RELA is a triplet of (offset, info, addend) while REL is a
319   // tuple of (offset, info). Addends for REL are implicit and read from
320   // the location where the relocations are applied. So, REL is more
321   // compact than RELA but requires a bit of more work to process.
322   //
323   // (From the linker writer's view, this distinction is not necessary.
324   // If the ELF had chosen whichever and sticked with it, it would have
325   // been easier to write code to process relocations, but it's too late
326   // to change the spec.)
327   //
328   // Each ABI defines its relocation type. IsRela is true if target
329   // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
330   // few 32-bit ABIs are using RELA too.
331   bool isRela;
332 
333   // True if we are creating position-independent code.
334   bool isPic;
335 
336   // 4 for ELF32, 8 for ELF64.
337   int wordsize;
338 };
339 
340 // The only instance of Configuration struct.
341 extern Configuration *config;
342 
343 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
344 // VER_NDX_GLOBAL. This helper returns other elements.
namedVersionDefs()345 static inline ArrayRef<VersionDefinition> namedVersionDefs() {
346   return llvm::makeArrayRef(config->versionDefinitions).slice(2);
347 }
348 
errorOrWarn(const Twine & msg)349 static inline void errorOrWarn(const Twine &msg) {
350   if (!config->noinhibitExec)
351     error(msg);
352   else
353     warn(msg);
354 }
355 } // namespace elf
356 } // namespace lld
357 
358 #endif
359