1 //===- Config.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLD_ELF_CONFIG_H
11 #define LLD_ELF_CONFIG_H
12 
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/ADT/MapVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/Support/CachePruning.h"
19 #include "llvm/Support/CodeGen.h"
20 #include "llvm/Support/Endian.h"
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, IgnoreAll };
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 struct SymbolVersion {
65   llvm::StringRef Name;
66   bool IsExternCpp;
67   bool HasWildcard;
68 };
69 
70 // This struct contains symbols version definition that
71 // can be found in version script if it is used for link.
72 struct VersionDefinition {
73   llvm::StringRef Name;
74   uint16_t Id = 0;
75   std::vector<SymbolVersion> Globals;
76   size_t NameOff = 0; // Offset in the string table
77 };
78 
79 // This struct contains the global configuration for the linker.
80 // Most fields are direct mapping from the command line options
81 // and such fields have the same name as the corresponding options.
82 // Most fields are initialized by the driver.
83 struct Configuration {
84   uint8_t OSABI = 0;
85   llvm::CachePruningPolicy ThinLTOCachePolicy;
86   llvm::StringMap<uint64_t> SectionStartMap;
87   llvm::StringRef Chroot;
88   llvm::StringRef DynamicLinker;
89   llvm::StringRef DwoDir;
90   llvm::StringRef Entry;
91   llvm::StringRef Emulation;
92   llvm::StringRef Fini;
93   llvm::StringRef Init;
94   llvm::StringRef LTOAAPipeline;
95   llvm::StringRef LTONewPmPasses;
96   llvm::StringRef LTOObjPath;
97   llvm::StringRef LTOSampleProfile;
98   llvm::StringRef MapFile;
99   llvm::StringRef OutputFile;
100   llvm::StringRef OptRemarksFilename;
101   llvm::StringRef ProgName;
102   llvm::StringRef SoName;
103   llvm::StringRef Sysroot;
104   llvm::StringRef ThinLTOCacheDir;
105   llvm::StringRef ThinLTOIndexOnlyArg;
106   std::pair<llvm::StringRef, llvm::StringRef> ThinLTOObjectSuffixReplace;
107   std::pair<llvm::StringRef, llvm::StringRef> ThinLTOPrefixReplace;
108   std::string Rpath;
109   std::vector<VersionDefinition> VersionDefinitions;
110   std::vector<llvm::StringRef> AuxiliaryList;
111   std::vector<llvm::StringRef> FilterList;
112   std::vector<llvm::StringRef> SearchPaths;
113   std::vector<llvm::StringRef> SymbolOrderingFile;
114   std::vector<llvm::StringRef> Undefined;
115   std::vector<SymbolVersion> DynamicList;
116   std::vector<SymbolVersion> VersionScriptGlobals;
117   std::vector<SymbolVersion> VersionScriptLocals;
118   std::vector<uint8_t> BuildIdVector;
119   llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
120                   uint64_t>
121       CallGraphProfile;
122   bool AllowMultipleDefinition;
123   bool AndroidPackDynRelocs;
124   bool ARMHasBlx = false;
125   bool ARMHasMovtMovw = false;
126   bool ARMJ1J2BranchEncoding = false;
127   bool AsNeeded = false;
128   bool Bsymbolic;
129   bool BsymbolicFunctions;
130   bool CheckSections;
131   bool CompressDebugSections;
132   bool Cref;
133   bool DefineCommon;
134   bool Demangle = true;
135   bool DisableVerify;
136   bool EhFrameHdr;
137   bool EmitRelocs;
138   bool EnableNewDtags;
139   bool ExecuteOnly;
140   bool ExportDynamic;
141   bool FixCortexA53Errata843419;
142   bool GcSections;
143   bool GdbIndex;
144   bool GnuHash = false;
145   bool GnuUnique;
146   bool HasDynamicList = false;
147   bool HasDynSymTab;
148   bool IgnoreDataAddressEquality;
149   bool IgnoreFunctionAddressEquality;
150   bool LTODebugPassManager;
151   bool LTONewPassManager;
152   bool MergeArmExidx;
153   bool MipsN32Abi = false;
154   bool NoinhibitExec;
155   bool Nostdlib;
156   bool OFormatBinary;
157   bool Omagic;
158   bool OptRemarksWithHotness;
159   bool Pie;
160   bool PrintGcSections;
161   bool PrintIcfSections;
162   bool Relocatable;
163   bool RelrPackDynRelocs;
164   bool SaveTemps;
165   bool SingleRoRx;
166   bool Shared;
167   bool Static = false;
168   bool SysvHash = false;
169   bool Target1Rel;
170   bool Trace;
171   bool ThinLTOEmitImportsFiles;
172   bool ThinLTOIndexOnly;
173   bool UndefinedVersion;
174   bool UseAndroidRelrTags = false;
175   bool WarnBackrefs;
176   bool WarnCommon;
177   bool WarnMissingEntry;
178   bool WarnSymbolOrdering;
179   bool WriteAddends;
180   bool ZCombreloc;
181   bool ZCopyreloc;
182   bool ZExecstack;
183   bool ZHazardplt;
184   bool ZIfuncnoplt;
185   bool ZInitfirst;
186   bool ZInterpose;
187   bool ZKeepTextSectionPrefix;
188   bool ZNodelete;
189   bool ZNodlopen;
190   bool ZNow;
191   bool ZOrigin;
192   bool ZRelro;
193   bool ZRodynamic;
194   bool ZText;
195   bool ZRetpolineplt;
196   bool ZWxneeded;
197   DiscardPolicy Discard;
198   ICFLevel ICF;
199   OrphanHandlingPolicy OrphanHandling;
200   SortSectionPolicy SortSection;
201   StripPolicy Strip;
202   UnresolvedPolicy UnresolvedSymbols;
203   Target2Policy Target2;
204   ARMVFPArgKind ARMVFPArgs = ARMVFPArgKind::Default;
205   BuildIdKind BuildId = BuildIdKind::None;
206   ELFKind EKind = ELFNoneKind;
207   uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL;
208   uint16_t EMachine = llvm::ELF::EM_NONE;
209   llvm::Optional<uint64_t> ImageBase;
210   uint64_t MaxPageSize;
211   uint64_t MipsGotSize;
212   uint64_t ZStackSize;
213   unsigned LTOPartitions;
214   unsigned LTOO;
215   unsigned Optimize;
216   unsigned ThinLTOJobs;
217 
218   // The following config options do not directly correspond to any
219   // particualr command line options.
220 
221   // True if we need to pass through relocations in input files to the
222   // output file. Usually false because we consume relocations.
223   bool CopyRelocs;
224 
225   // True if the target is ELF64. False if ELF32.
226   bool Is64;
227 
228   // True if the target is little-endian. False if big-endian.
229   bool IsLE;
230 
231   // endianness::little if IsLE is true. endianness::big otherwise.
232   llvm::support::endianness Endianness;
233 
234   // True if the target is the little-endian MIPS64.
235   //
236   // The reason why we have this variable only for the MIPS is because
237   // we use this often.  Some ELF headers for MIPS64EL are in a
238   // mixed-endian (which is horrible and I'd say that's a serious spec
239   // bug), and we need to know whether we are reading MIPS ELF files or
240   // not in various places.
241   //
242   // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
243   // name whatever that means. A fun hypothesis is that "EL" is short for
244   // little-endian written in the little-endian order, but I don't know
245   // if that's true.)
246   bool IsMips64EL;
247 
248   // Holds set of ELF header flags for the target.
249   uint32_t EFlags = 0;
250 
251   // The ELF spec defines two types of relocation table entries, RELA and
252   // REL. RELA is a triplet of (offset, info, addend) while REL is a
253   // tuple of (offset, info). Addends for REL are implicit and read from
254   // the location where the relocations are applied. So, REL is more
255   // compact than RELA but requires a bit of more work to process.
256   //
257   // (From the linker writer's view, this distinction is not necessary.
258   // If the ELF had chosen whichever and sticked with it, it would have
259   // been easier to write code to process relocations, but it's too late
260   // to change the spec.)
261   //
262   // Each ABI defines its relocation type. IsRela is true if target
263   // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
264   // few 32-bit ABIs are using RELA too.
265   bool IsRela;
266 
267   // True if we are creating position-independent code.
268   bool Pic;
269 
270   // 4 for ELF32, 8 for ELF64.
271   int Wordsize;
272 };
273 
274 // The only instance of Configuration struct.
275 extern Configuration *Config;
276 
errorOrWarn(const Twine & Msg)277 static inline void errorOrWarn(const Twine &Msg) {
278   if (!Config->NoinhibitExec)
279     error(Msg);
280   else
281     warn(Msg);
282 }
283 } // namespace elf
284 } // namespace lld
285 
286 #endif
287