[cmake] Fix ms-compat version in WinMsvc.cmake
[llvm-project.git] / lld / ELF / Config.h
blobfd9d3b63b2a3837edb17d35b6a4a3ec25eac18a5
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 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_CONFIG_H
10 #define LLD_ELF_CONFIG_H
12 #include "lld/Common/ErrorHandler.h"
13 #include "llvm/ADT/CachedHashString.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/MapVector.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Support/CachePruning.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/GlobPattern.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include <atomic>
26 #include <memory>
27 #include <vector>
29 namespace lld {
30 namespace elf {
32 class InputFile;
33 class BinaryFile;
34 class BitcodeFile;
35 class ELFFileBase;
36 class SharedFile;
37 class InputSectionBase;
38 class Symbol;
40 enum ELFKind : uint8_t {
41 ELFNoneKind,
42 ELF32LEKind,
43 ELF32BEKind,
44 ELF64LEKind,
45 ELF64BEKind
48 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions,
49 // -Bsymbolic.
50 enum class BsymbolicKind { None, NonWeakFunctions, Functions, All };
52 // For --build-id.
53 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };
55 // For --discard-{all,locals,none}.
56 enum class DiscardPolicy { Default, All, Locals, None };
58 // For --icf={none,safe,all}.
59 enum class ICFLevel { None, Safe, All };
61 // For --strip-{all,debug}.
62 enum class StripPolicy { None, All, Debug };
64 // For --unresolved-symbols.
65 enum class UnresolvedPolicy { ReportError, Warn, Ignore };
67 // For --orphan-handling.
68 enum class OrphanHandlingPolicy { Place, Warn, Error };
70 // For --sort-section and linkerscript sorting rules.
71 enum class SortSectionPolicy { Default, None, Alignment, Name, Priority };
73 // For --target2
74 enum class Target2Policy { Abs, Rel, GotRel };
76 // For tracking ARM Float Argument PCS
77 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
79 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
80 enum class SeparateSegmentKind { None, Code, Loadable };
82 // For -z *stack
83 enum class GnuStackKind { None, Exec, NoExec };
85 struct SymbolVersion {
86 llvm::StringRef name;
87 bool isExternCpp;
88 bool hasWildcard;
91 // This struct contains symbols version definition that
92 // can be found in version script if it is used for link.
93 struct VersionDefinition {
94 llvm::StringRef name;
95 uint16_t id;
96 SmallVector<SymbolVersion, 0> nonLocalPatterns;
97 SmallVector<SymbolVersion, 0> localPatterns;
100 // This struct contains the global configuration for the linker.
101 // Most fields are direct mapping from the command line options
102 // and such fields have the same name as the corresponding options.
103 // Most fields are initialized by the driver.
104 struct Configuration {
105 uint8_t osabi = 0;
106 uint32_t andFeatures = 0;
107 llvm::CachePruningPolicy thinLTOCachePolicy;
108 llvm::SetVector<llvm::CachedHashString> dependencyFiles; // for --dependency-file
109 llvm::StringMap<uint64_t> sectionStartMap;
110 llvm::StringRef bfdname;
111 llvm::StringRef chroot;
112 llvm::StringRef dependencyFile;
113 llvm::StringRef dwoDir;
114 llvm::StringRef dynamicLinker;
115 llvm::StringRef entry;
116 llvm::StringRef emulation;
117 llvm::StringRef fini;
118 llvm::StringRef init;
119 llvm::StringRef ltoAAPipeline;
120 llvm::StringRef ltoCSProfileFile;
121 llvm::StringRef ltoNewPmPasses;
122 llvm::StringRef ltoObjPath;
123 llvm::StringRef ltoSampleProfile;
124 llvm::StringRef mapFile;
125 llvm::StringRef outputFile;
126 llvm::StringRef optRemarksFilename;
127 llvm::Optional<uint64_t> optRemarksHotnessThreshold = 0;
128 llvm::StringRef optRemarksPasses;
129 llvm::StringRef optRemarksFormat;
130 llvm::StringRef optStatsFilename;
131 llvm::StringRef progName;
132 llvm::StringRef printArchiveStats;
133 llvm::StringRef printSymbolOrder;
134 llvm::StringRef soName;
135 llvm::StringRef sysroot;
136 llvm::StringRef thinLTOCacheDir;
137 llvm::StringRef thinLTOIndexOnlyArg;
138 llvm::StringRef whyExtract;
139 StringRef zBtiReport = "none";
140 StringRef zCetReport = "none";
141 llvm::StringRef ltoBasicBlockSections;
142 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
143 std::pair<llvm::StringRef, llvm::StringRef> thinLTOPrefixReplace;
144 std::string rpath;
145 llvm::SmallVector<VersionDefinition, 0> versionDefinitions;
146 llvm::SmallVector<llvm::StringRef, 0> auxiliaryList;
147 llvm::SmallVector<llvm::StringRef, 0> filterList;
148 llvm::SmallVector<llvm::StringRef, 0> passPlugins;
149 llvm::SmallVector<llvm::StringRef, 0> searchPaths;
150 llvm::SmallVector<llvm::StringRef, 0> symbolOrderingFile;
151 llvm::SmallVector<llvm::StringRef, 0> thinLTOModulesToCompile;
152 llvm::SmallVector<llvm::StringRef, 0> undefined;
153 llvm::SmallVector<SymbolVersion, 0> dynamicList;
154 llvm::SmallVector<uint8_t, 0> buildIdVector;
155 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
156 uint64_t>
157 callGraphProfile;
158 bool allowMultipleDefinition;
159 bool androidPackDynRelocs = false;
160 bool armHasBlx = false;
161 bool armHasMovtMovw = false;
162 bool armJ1J2BranchEncoding = false;
163 bool asNeeded = false;
164 BsymbolicKind bsymbolic = BsymbolicKind::None;
165 bool callGraphProfileSort;
166 bool checkSections;
167 bool checkDynamicRelocs;
168 bool compressDebugSections;
169 bool cref;
170 llvm::SmallVector<std::pair<llvm::GlobPattern, uint64_t>, 0>
171 deadRelocInNonAlloc;
172 bool demangle = true;
173 bool dependentLibraries;
174 bool disableVerify;
175 bool ehFrameHdr;
176 bool emitLLVM;
177 bool emitRelocs;
178 bool enableNewDtags;
179 bool executeOnly;
180 bool exportDynamic;
181 bool fixCortexA53Errata843419;
182 bool fixCortexA8;
183 bool formatBinary = false;
184 bool fortranCommon;
185 bool gcSections;
186 bool gdbIndex;
187 bool gnuHash = false;
188 bool gnuUnique;
189 bool hasDynSymTab;
190 bool ignoreDataAddressEquality;
191 bool ignoreFunctionAddressEquality;
192 bool ltoCSProfileGenerate;
193 bool ltoPGOWarnMismatch;
194 bool ltoDebugPassManager;
195 bool ltoEmitAsm;
196 bool ltoUniqueBasicBlockSectionNames;
197 bool ltoWholeProgramVisibility;
198 bool mergeArmExidx;
199 bool mipsN32Abi = false;
200 bool mmapOutputFile;
201 bool nmagic;
202 bool noDynamicLinker = false;
203 bool noinhibitExec;
204 bool nostdlib;
205 bool oFormatBinary;
206 bool omagic;
207 bool opaquePointers;
208 bool optEB = false;
209 bool optEL = false;
210 bool optimizeBBJumps;
211 bool optRemarksWithHotness;
212 bool picThunk;
213 bool pie;
214 bool printGcSections;
215 bool printIcfSections;
216 bool relax;
217 bool relocatable;
218 bool relrGlibc = false;
219 bool relrPackDynRelocs = false;
220 llvm::DenseSet<llvm::StringRef> saveTempsArgs;
221 llvm::SmallVector<std::pair<llvm::GlobPattern, uint32_t>, 0> shuffleSections;
222 bool singleRoRx;
223 bool shared;
224 bool symbolic;
225 bool isStatic = false;
226 bool sysvHash = false;
227 bool target1Rel;
228 bool trace;
229 bool thinLTOEmitImportsFiles;
230 bool thinLTOEmitIndexFiles;
231 bool thinLTOIndexOnly;
232 bool timeTraceEnabled;
233 bool tocOptimize;
234 bool pcRelOptimize;
235 bool undefinedVersion;
236 bool unique;
237 bool useAndroidRelrTags = false;
238 bool warnBackrefs;
239 llvm::SmallVector<llvm::GlobPattern, 0> warnBackrefsExclude;
240 bool warnCommon;
241 bool warnMissingEntry;
242 bool warnSymbolOrdering;
243 bool writeAddends;
244 bool zCombreloc;
245 bool zCopyreloc;
246 bool zForceBti;
247 bool zForceIbt;
248 bool zGlobal;
249 bool zHazardplt;
250 bool zIfuncNoplt;
251 bool zInitfirst;
252 bool zInterpose;
253 bool zKeepTextSectionPrefix;
254 bool zNodefaultlib;
255 bool zNodelete;
256 bool zNodlopen;
257 bool zNow;
258 bool zOrigin;
259 bool zPacPlt;
260 bool zRelro;
261 bool zRodynamic;
262 bool zShstk;
263 bool zStartStopGC;
264 uint8_t zStartStopVisibility;
265 bool zText;
266 bool zRetpolineplt;
267 bool zWxneeded;
268 DiscardPolicy discard;
269 GnuStackKind zGnustack;
270 ICFLevel icf;
271 OrphanHandlingPolicy orphanHandling;
272 SortSectionPolicy sortSection;
273 StripPolicy strip;
274 UnresolvedPolicy unresolvedSymbols;
275 UnresolvedPolicy unresolvedSymbolsInShlib;
276 Target2Policy target2;
277 bool power10Stubs;
278 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default;
279 BuildIdKind buildId = BuildIdKind::None;
280 SeparateSegmentKind zSeparate;
281 ELFKind ekind = ELFNoneKind;
282 uint16_t emachine = llvm::ELF::EM_NONE;
283 llvm::Optional<uint64_t> imageBase;
284 uint64_t commonPageSize;
285 uint64_t maxPageSize;
286 uint64_t mipsGotSize;
287 uint64_t zStackSize;
288 unsigned ltoPartitions;
289 unsigned ltoo;
290 unsigned optimize;
291 StringRef thinLTOJobs;
292 unsigned timeTraceGranularity;
293 int32_t splitStackAdjustSize;
294 StringRef packageMetadata;
296 // The following config options do not directly correspond to any
297 // particular command line options.
299 // True if we need to pass through relocations in input files to the
300 // output file. Usually false because we consume relocations.
301 bool copyRelocs;
303 // True if the target is ELF64. False if ELF32.
304 bool is64;
306 // True if the target is little-endian. False if big-endian.
307 bool isLE;
309 // endianness::little if isLE is true. endianness::big otherwise.
310 llvm::support::endianness endianness;
312 // True if the target is the little-endian MIPS64.
314 // The reason why we have this variable only for the MIPS is because
315 // we use this often. Some ELF headers for MIPS64EL are in a
316 // mixed-endian (which is horrible and I'd say that's a serious spec
317 // bug), and we need to know whether we are reading MIPS ELF files or
318 // not in various places.
320 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
321 // name whatever that means. A fun hypothesis is that "EL" is short for
322 // little-endian written in the little-endian order, but I don't know
323 // if that's true.)
324 bool isMips64EL;
326 // True if we need to reserve two .got entries for local-dynamic TLS model.
327 bool needsTlsLd = false;
329 // True if we need to set the DF_STATIC_TLS flag to an output file, which
330 // works as a hint to the dynamic loader that the shared object contains code
331 // compiled with the initial-exec TLS model.
332 bool hasTlsIe = false;
334 // Holds set of ELF header flags for the target.
335 uint32_t eflags = 0;
337 // The ELF spec defines two types of relocation table entries, RELA and
338 // REL. RELA is a triplet of (offset, info, addend) while REL is a
339 // tuple of (offset, info). Addends for REL are implicit and read from
340 // the location where the relocations are applied. So, REL is more
341 // compact than RELA but requires a bit of more work to process.
343 // (From the linker writer's view, this distinction is not necessary.
344 // If the ELF had chosen whichever and sticked with it, it would have
345 // been easier to write code to process relocations, but it's too late
346 // to change the spec.)
348 // Each ABI defines its relocation type. IsRela is true if target
349 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
350 // few 32-bit ABIs are using RELA too.
351 bool isRela;
353 // True if we are creating position-independent code.
354 bool isPic;
356 // 4 for ELF32, 8 for ELF64.
357 int wordsize;
359 // Mode of MTE to write to the ELF note. Should be one of NT_MEMTAG_ASYNC (for
360 // async), NT_MEMTAG_SYNC (for sync), or NT_MEMTAG_LEVEL_NONE (for none). If
361 // async or sync is enabled, write the ELF note specifying the default MTE
362 // mode.
363 int androidMemtagMode;
364 // Signal to the dynamic loader to enable heap MTE.
365 bool androidMemtagHeap;
366 // Signal to the dynamic loader that this binary expects stack MTE. Generally,
367 // this means to map the primary and thread stacks as PROT_MTE. Note: This is
368 // not supported on Android 11 & 12.
369 bool androidMemtagStack;
372 // The only instance of Configuration struct.
373 extern std::unique_ptr<Configuration> config;
375 struct DuplicateSymbol {
376 const Symbol *sym;
377 const InputFile *file;
378 InputSectionBase *section;
379 uint64_t value;
382 struct Ctx {
383 SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
384 SmallVector<ELFFileBase *, 0> objectFiles;
385 SmallVector<SharedFile *, 0> sharedFiles;
386 SmallVector<BinaryFile *, 0> binaryFiles;
387 SmallVector<BitcodeFile *, 0> bitcodeFiles;
388 SmallVector<BitcodeFile *, 0> lazyBitcodeFiles;
389 // Duplicate symbol candidates.
390 SmallVector<DuplicateSymbol, 0> duplicates;
391 // Symbols in a non-prevailing COMDAT group which should be changed to an
392 // Undefined.
393 SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;
394 // True if SHT_LLVM_SYMPART is used.
395 std::atomic<bool> hasSympart{false};
396 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
397 SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
398 whyExtractRecords;
399 // A mapping from a symbol to an InputFile referencing it backward. Used by
400 // --warn-backrefs.
401 llvm::DenseMap<const Symbol *,
402 std::pair<const InputFile *, const InputFile *>>
403 backwardReferences;
406 // The only instance of Ctx struct.
407 extern std::unique_ptr<Ctx> ctx;
409 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
410 // VER_NDX_GLOBAL. This helper returns other elements.
411 static inline ArrayRef<VersionDefinition> namedVersionDefs() {
412 return llvm::makeArrayRef(config->versionDefinitions).slice(2);
415 void errorOrWarn(const Twine &msg);
417 static inline void internalLinkerError(StringRef loc, const Twine &msg) {
418 errorOrWarn(loc + "internal linker error: " + msg + "\n" +
419 llvm::getBugReportMsg());
422 } // namespace elf
423 } // namespace lld
425 #endif