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 };
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 CallGraphProfileSort;
131 bool CheckSections;
132 bool CompressDebugSections;
133 bool Cref;
134 bool DefineCommon;
135 bool Demangle = true;
136 bool DisableVerify;
137 bool EhFrameHdr;
138 bool EmitLLVM;
139 bool EmitRelocs;
140 bool EnableNewDtags;
141 bool ExecuteOnly;
142 bool ExportDynamic;
143 bool FixCortexA53Errata843419;
144 bool FormatBinary = false;
145 bool GcSections;
146 bool GdbIndex;
147 bool GnuHash = false;
148 bool GnuUnique;
149 bool HasDynamicList = false;
150 bool HasDynSymTab;
151 bool IgnoreDataAddressEquality;
152 bool IgnoreFunctionAddressEquality;
153 bool LTODebugPassManager;
154 bool LTONewPassManager;
155 bool MergeArmExidx;
156 bool MipsN32Abi = false;
157 bool NoinhibitExec;
158 bool Nostdlib;
159 bool OFormatBinary;
160 bool Omagic;
161 bool OptRemarksWithHotness;
162 bool PicThunk;
163 bool Pie;
164 bool PrintGcSections;
165 bool PrintIcfSections;
166 bool Relocatable;
167 bool RelrPackDynRelocs;
168 bool SaveTemps;
169 bool SingleRoRx;
170 bool Shared;
171 bool Static = false;
172 bool SysvHash = false;
173 bool Target1Rel;
174 bool Trace;
175 bool ThinLTOEmitImportsFiles;
176 bool ThinLTOIndexOnly;
177 bool TocOptimize;
178 bool UndefinedVersion;
179 bool UseAndroidRelrTags = false;
180 bool WarnBackrefs;
181 bool WarnCommon;
182 bool WarnIfuncTextrel;
183 bool WarnMissingEntry;
184 bool WarnSymbolOrdering;
185 bool WriteAddends;
186 bool ZCombreloc;
187 bool ZCopyreloc;
188 bool ZExecstack;
189 bool ZGlobal;
190 bool ZHazardplt;
191 bool ZIfuncnoplt;
192 bool ZInitfirst;
193 bool ZInterpose;
194 bool ZKeepTextSectionPrefix;
195 bool ZNodefaultlib;
196 bool ZNodelete;
197 bool ZNodlopen;
198 bool ZNow;
199 bool ZOrigin;
200 bool ZRelro;
201 bool ZRodynamic;
202 bool ZText;
203 bool ZRetpolineplt;
204 bool ZWxneeded;
205 DiscardPolicy Discard;
206 ICFLevel ICF;
207 OrphanHandlingPolicy OrphanHandling;
208 SortSectionPolicy SortSection;
209 StripPolicy Strip;
210 UnresolvedPolicy UnresolvedSymbols;
211 Target2Policy Target2;
212 ARMVFPArgKind ARMVFPArgs = ARMVFPArgKind::Default;
213 BuildIdKind BuildId = BuildIdKind::None;
214 ELFKind EKind = ELFNoneKind;
215 uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL;
216 uint16_t EMachine = llvm::ELF::EM_NONE;
217 llvm::Optional<uint64_t> ImageBase;
218 uint64_t MaxPageSize;
219 uint64_t MipsGotSize;
220 uint64_t ZStackSize;
221 unsigned LTOPartitions;
222 unsigned LTOO;
223 unsigned Optimize;
224 unsigned ThinLTOJobs;
225 int32_t SplitStackAdjustSize;
226
227 // The following config options do not directly correspond to any
228 // particualr command line options.
229
230 // True if we need to pass through relocations in input files to the
231 // output file. Usually false because we consume relocations.
232 bool CopyRelocs;
233
234 // True if the target is ELF64. False if ELF32.
235 bool Is64;
236
237 // True if the target is little-endian. False if big-endian.
238 bool IsLE;
239
240 // endianness::little if IsLE is true. endianness::big otherwise.
241 llvm::support::endianness Endianness;
242
243 // True if the target is the little-endian MIPS64.
244 //
245 // The reason why we have this variable only for the MIPS is because
246 // we use this often. Some ELF headers for MIPS64EL are in a
247 // mixed-endian (which is horrible and I'd say that's a serious spec
248 // bug), and we need to know whether we are reading MIPS ELF files or
249 // not in various places.
250 //
251 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
252 // name whatever that means. A fun hypothesis is that "EL" is short for
253 // little-endian written in the little-endian order, but I don't know
254 // if that's true.)
255 bool IsMips64EL;
256
257 // Holds set of ELF header flags for the target.
258 uint32_t EFlags = 0;
259
260 // The ELF spec defines two types of relocation table entries, RELA and
261 // REL. RELA is a triplet of (offset, info, addend) while REL is a
262 // tuple of (offset, info). Addends for REL are implicit and read from
263 // the location where the relocations are applied. So, REL is more
264 // compact than RELA but requires a bit of more work to process.
265 //
266 // (From the linker writer's view, this distinction is not necessary.
267 // If the ELF had chosen whichever and sticked with it, it would have
268 // been easier to write code to process relocations, but it's too late
269 // to change the spec.)
270 //
271 // Each ABI defines its relocation type. IsRela is true if target
272 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
273 // few 32-bit ABIs are using RELA too.
274 bool IsRela;
275
276 // True if we are creating position-independent code.
277 bool Pic;
278
279 // 4 for ELF32, 8 for ELF64.
280 int Wordsize;
281 };
282
283 // The only instance of Configuration struct.
284 extern Configuration *Config;
285
errorOrWarn(const Twine & Msg)286 static inline void errorOrWarn(const Twine &Msg) {
287 if (!Config->NoinhibitExec)
288 error(Msg);
289 else
290 warn(Msg);
291 }
292 } // namespace elf
293 } // namespace lld
294
295 #endif
296