Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lld / ELF / Config.h
blob56229334f9a44aec2b614c975c6623af7ec89ae3
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/SmallSet.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Support/CachePruning.h"
23 #include "llvm/Support/CodeGen.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/Compression.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/GlobPattern.h"
29 #include "llvm/Support/PrettyStackTrace.h"
30 #include <atomic>
31 #include <memory>
32 #include <optional>
33 #include <vector>
35 namespace lld::elf {
37 class InputFile;
38 class BinaryFile;
39 class BitcodeFile;
40 class ELFFileBase;
41 class SharedFile;
42 class InputSectionBase;
43 class EhInputSection;
44 class Symbol;
45 class BitcodeCompiler;
47 enum ELFKind : uint8_t {
48 ELFNoneKind,
49 ELF32LEKind,
50 ELF32BEKind,
51 ELF64LEKind,
52 ELF64BEKind
55 // For -Bno-symbolic, -Bsymbolic-non-weak-functions, -Bsymbolic-functions,
56 // -Bsymbolic-non-weak, -Bsymbolic.
57 enum class BsymbolicKind { None, NonWeakFunctions, Functions, NonWeak, All };
59 // For --build-id.
60 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };
62 // For --call-graph-profile-sort={none,hfsort,cdsort}.
63 enum class CGProfileSortKind { None, Hfsort, Cdsort };
65 // For --discard-{all,locals,none}.
66 enum class DiscardPolicy { Default, All, Locals, None };
68 // For --icf={none,safe,all}.
69 enum class ICFLevel { None, Safe, All };
71 // For --strip-{all,debug}.
72 enum class StripPolicy { None, All, Debug };
74 // For --unresolved-symbols.
75 enum class UnresolvedPolicy { ReportError, Warn, Ignore };
77 // For --orphan-handling.
78 enum class OrphanHandlingPolicy { Place, Warn, Error };
80 // For --sort-section and linkerscript sorting rules.
81 enum class SortSectionPolicy {
82 Default,
83 None,
84 Alignment,
85 Name,
86 Priority,
87 Reverse,
90 // For --target2
91 enum class Target2Policy { Abs, Rel, GotRel };
93 // For tracking ARM Float Argument PCS
94 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
96 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments.
97 enum class SeparateSegmentKind { None, Code, Loadable };
99 // For -z *stack
100 enum class GnuStackKind { None, Exec, NoExec };
102 // For --lto=
103 enum LtoKind : uint8_t {UnifiedThin, UnifiedRegular, Default};
105 struct SymbolVersion {
106 llvm::StringRef name;
107 bool isExternCpp;
108 bool hasWildcard;
111 // This struct contains symbols version definition that
112 // can be found in version script if it is used for link.
113 struct VersionDefinition {
114 llvm::StringRef name;
115 uint16_t id;
116 SmallVector<SymbolVersion, 0> nonLocalPatterns;
117 SmallVector<SymbolVersion, 0> localPatterns;
120 class LinkerDriver {
121 public:
122 void linkerMain(ArrayRef<const char *> args);
123 void addFile(StringRef path, bool withLOption);
124 void addLibrary(StringRef name);
126 private:
127 void createFiles(llvm::opt::InputArgList &args);
128 void inferMachineType();
129 void link(llvm::opt::InputArgList &args);
130 template <class ELFT> void compileBitcodeFiles(bool skipLinkedOutput);
131 bool tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
132 uint64_t offsetInArchive, bool lazy);
133 // True if we are in --whole-archive and --no-whole-archive.
134 bool inWholeArchive = false;
136 // True if we are in --start-lib and --end-lib.
137 bool inLib = false;
139 std::unique_ptr<BitcodeCompiler> lto;
140 std::vector<InputFile *> files;
141 std::optional<InputFile *> armCmseImpLib;
143 public:
144 SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
147 // This struct contains the global configuration for the linker.
148 // Most fields are direct mapping from the command line options
149 // and such fields have the same name as the corresponding options.
150 // Most fields are initialized by the ctx.driver.
151 struct Config {
152 uint8_t osabi = 0;
153 uint32_t andFeatures = 0;
154 llvm::CachePruningPolicy thinLTOCachePolicy;
155 llvm::SetVector<llvm::CachedHashString> dependencyFiles; // for --dependency-file
156 llvm::StringMap<uint64_t> sectionStartMap;
157 llvm::StringRef bfdname;
158 llvm::StringRef chroot;
159 llvm::StringRef dependencyFile;
160 llvm::StringRef dwoDir;
161 llvm::StringRef dynamicLinker;
162 llvm::StringRef entry;
163 llvm::StringRef emulation;
164 llvm::StringRef fini;
165 llvm::StringRef init;
166 llvm::StringRef ltoAAPipeline;
167 llvm::StringRef ltoCSProfileFile;
168 llvm::StringRef ltoNewPmPasses;
169 llvm::StringRef ltoObjPath;
170 llvm::StringRef ltoSampleProfile;
171 llvm::StringRef mapFile;
172 llvm::StringRef outputFile;
173 llvm::StringRef optRemarksFilename;
174 std::optional<uint64_t> optRemarksHotnessThreshold = 0;
175 llvm::StringRef optRemarksPasses;
176 llvm::StringRef optRemarksFormat;
177 llvm::StringRef optStatsFilename;
178 llvm::StringRef progName;
179 llvm::StringRef printArchiveStats;
180 llvm::StringRef printSymbolOrder;
181 llvm::StringRef soName;
182 llvm::StringRef sysroot;
183 llvm::StringRef thinLTOCacheDir;
184 llvm::StringRef thinLTOIndexOnlyArg;
185 llvm::StringRef whyExtract;
186 llvm::StringRef cmseInputLib;
187 llvm::StringRef cmseOutputLib;
188 StringRef zBtiReport = "none";
189 StringRef zCetReport = "none";
190 llvm::StringRef ltoBasicBlockSections;
191 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
192 llvm::StringRef thinLTOPrefixReplaceOld;
193 llvm::StringRef thinLTOPrefixReplaceNew;
194 llvm::StringRef thinLTOPrefixReplaceNativeObject;
195 std::string rpath;
196 llvm::SmallVector<VersionDefinition, 0> versionDefinitions;
197 llvm::SmallVector<llvm::StringRef, 0> auxiliaryList;
198 llvm::SmallVector<llvm::StringRef, 0> filterList;
199 llvm::SmallVector<llvm::StringRef, 0> passPlugins;
200 llvm::SmallVector<llvm::StringRef, 0> searchPaths;
201 llvm::SmallVector<llvm::StringRef, 0> symbolOrderingFile;
202 llvm::SmallVector<llvm::StringRef, 0> thinLTOModulesToCompile;
203 llvm::SmallVector<llvm::StringRef, 0> undefined;
204 llvm::SmallVector<SymbolVersion, 0> dynamicList;
205 llvm::SmallVector<uint8_t, 0> buildIdVector;
206 llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
207 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
208 uint64_t>
209 callGraphProfile;
210 bool cmseImplib = false;
211 bool allowMultipleDefinition;
212 bool fatLTOObjects;
213 bool androidPackDynRelocs = false;
214 bool armHasBlx = false;
215 bool armHasMovtMovw = false;
216 bool armJ1J2BranchEncoding = false;
217 bool armCMSESupport = false;
218 bool asNeeded = false;
219 bool armBe8 = false;
220 BsymbolicKind bsymbolic = BsymbolicKind::None;
221 CGProfileSortKind callGraphProfileSort;
222 bool checkSections;
223 bool checkDynamicRelocs;
224 llvm::DebugCompressionType compressDebugSections;
225 bool cref;
226 llvm::SmallVector<std::pair<llvm::GlobPattern, uint64_t>, 0>
227 deadRelocInNonAlloc;
228 bool demangle = true;
229 bool dependentLibraries;
230 bool disableVerify;
231 bool ehFrameHdr;
232 bool emitLLVM;
233 bool emitRelocs;
234 bool enableNewDtags;
235 bool executeOnly;
236 bool exportDynamic;
237 bool fixCortexA53Errata843419;
238 bool fixCortexA8;
239 bool formatBinary = false;
240 bool fortranCommon;
241 bool gcSections;
242 bool gdbIndex;
243 bool gnuHash = false;
244 bool gnuUnique;
245 bool hasDynSymTab;
246 bool ignoreDataAddressEquality;
247 bool ignoreFunctionAddressEquality;
248 bool ltoCSProfileGenerate;
249 bool ltoPGOWarnMismatch;
250 bool ltoDebugPassManager;
251 bool ltoEmitAsm;
252 bool ltoUniqueBasicBlockSectionNames;
253 bool ltoValidateAllVtablesHaveTypeInfos;
254 bool ltoWholeProgramVisibility;
255 bool mergeArmExidx;
256 bool mipsN32Abi = false;
257 bool mmapOutputFile;
258 bool nmagic;
259 bool noDynamicLinker = false;
260 bool noinhibitExec;
261 bool nostdlib;
262 bool oFormatBinary;
263 bool omagic;
264 bool optEB = false;
265 bool optEL = false;
266 bool optimizeBBJumps;
267 bool optRemarksWithHotness;
268 bool picThunk;
269 bool pie;
270 bool printGcSections;
271 bool printIcfSections;
272 bool printMemoryUsage;
273 bool relax;
274 bool relaxGP;
275 bool relocatable;
276 bool relrGlibc = false;
277 bool relrPackDynRelocs = false;
278 llvm::DenseSet<llvm::StringRef> saveTempsArgs;
279 llvm::SmallVector<std::pair<llvm::GlobPattern, uint32_t>, 0> shuffleSections;
280 bool singleRoRx;
281 bool shared;
282 bool symbolic;
283 bool isStatic = false;
284 bool sysvHash = false;
285 bool target1Rel;
286 bool trace;
287 bool thinLTOEmitImportsFiles;
288 bool thinLTOEmitIndexFiles;
289 bool thinLTOIndexOnly;
290 bool timeTraceEnabled;
291 bool tocOptimize;
292 bool pcRelOptimize;
293 bool undefinedVersion;
294 bool unique;
295 bool useAndroidRelrTags = false;
296 bool warnBackrefs;
297 llvm::SmallVector<llvm::GlobPattern, 0> warnBackrefsExclude;
298 bool warnCommon;
299 bool warnMissingEntry;
300 bool warnSymbolOrdering;
301 bool writeAddends;
302 bool zCombreloc;
303 bool zCopyreloc;
304 bool zForceBti;
305 bool zForceIbt;
306 bool zGlobal;
307 bool zHazardplt;
308 bool zIfuncNoplt;
309 bool zInitfirst;
310 bool zInterpose;
311 bool zKeepTextSectionPrefix;
312 bool zNodefaultlib;
313 bool zNodelete;
314 bool zNodlopen;
315 bool zNow;
316 bool zOrigin;
317 bool zPacPlt;
318 bool zRelro;
319 bool zRodynamic;
320 bool zShstk;
321 bool zStartStopGC;
322 uint8_t zStartStopVisibility;
323 bool zText;
324 bool zRetpolineplt;
325 bool zWxneeded;
326 DiscardPolicy discard;
327 GnuStackKind zGnustack;
328 ICFLevel icf;
329 OrphanHandlingPolicy orphanHandling;
330 SortSectionPolicy sortSection;
331 StripPolicy strip;
332 UnresolvedPolicy unresolvedSymbols;
333 UnresolvedPolicy unresolvedSymbolsInShlib;
334 Target2Policy target2;
335 bool power10Stubs;
336 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default;
337 BuildIdKind buildId = BuildIdKind::None;
338 SeparateSegmentKind zSeparate;
339 ELFKind ekind = ELFNoneKind;
340 uint16_t emachine = llvm::ELF::EM_NONE;
341 std::optional<uint64_t> imageBase;
342 uint64_t commonPageSize;
343 uint64_t maxPageSize;
344 uint64_t mipsGotSize;
345 uint64_t zStackSize;
346 unsigned ltoPartitions;
347 unsigned ltoo;
348 llvm::CodeGenOptLevel ltoCgo;
349 unsigned optimize;
350 StringRef thinLTOJobs;
351 unsigned timeTraceGranularity;
352 int32_t splitStackAdjustSize;
353 StringRef packageMetadata;
355 // The following config options do not directly correspond to any
356 // particular command line options.
358 // True if we need to pass through relocations in input files to the
359 // output file. Usually false because we consume relocations.
360 bool copyRelocs;
362 // True if the target is ELF64. False if ELF32.
363 bool is64;
365 // True if the target is little-endian. False if big-endian.
366 bool isLE;
368 // endianness::little if isLE is true. endianness::big otherwise.
369 llvm::endianness endianness;
371 // True if the target is the little-endian MIPS64.
373 // The reason why we have this variable only for the MIPS is because
374 // we use this often. Some ELF headers for MIPS64EL are in a
375 // mixed-endian (which is horrible and I'd say that's a serious spec
376 // bug), and we need to know whether we are reading MIPS ELF files or
377 // not in various places.
379 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official
380 // name whatever that means. A fun hypothesis is that "EL" is short for
381 // little-endian written in the little-endian order, but I don't know
382 // if that's true.)
383 bool isMips64EL;
385 // True if we need to set the DF_STATIC_TLS flag to an output file, which
386 // works as a hint to the dynamic loader that the shared object contains code
387 // compiled with the initial-exec TLS model.
388 bool hasTlsIe = false;
390 // Holds set of ELF header flags for the target.
391 uint32_t eflags = 0;
393 // The ELF spec defines two types of relocation table entries, RELA and
394 // REL. RELA is a triplet of (offset, info, addend) while REL is a
395 // tuple of (offset, info). Addends for REL are implicit and read from
396 // the location where the relocations are applied. So, REL is more
397 // compact than RELA but requires a bit of more work to process.
399 // (From the linker writer's view, this distinction is not necessary.
400 // If the ELF had chosen whichever and sticked with it, it would have
401 // been easier to write code to process relocations, but it's too late
402 // to change the spec.)
404 // Each ABI defines its relocation type. IsRela is true if target
405 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A
406 // few 32-bit ABIs are using RELA too.
407 bool isRela;
409 // True if we are creating position-independent code.
410 bool isPic;
412 // 4 for ELF32, 8 for ELF64.
413 int wordsize;
415 // Mode of MTE to write to the ELF note. Should be one of NT_MEMTAG_ASYNC (for
416 // async), NT_MEMTAG_SYNC (for sync), or NT_MEMTAG_LEVEL_NONE (for none). If
417 // async or sync is enabled, write the ELF note specifying the default MTE
418 // mode.
419 int androidMemtagMode;
420 // Signal to the dynamic loader to enable heap MTE.
421 bool androidMemtagHeap;
422 // Signal to the dynamic loader that this binary expects stack MTE. Generally,
423 // this means to map the primary and thread stacks as PROT_MTE. Note: This is
424 // not supported on Android 11 & 12.
425 bool androidMemtagStack;
427 // When using a unified pre-link LTO pipeline, specify the backend LTO mode.
428 LtoKind ltoKind = LtoKind::Default;
430 unsigned threadCount;
432 // If an input file equals a key, remap it to the value.
433 llvm::DenseMap<llvm::StringRef, llvm::StringRef> remapInputs;
434 // If an input file matches a wildcard pattern, remap it to the value.
435 llvm::SmallVector<std::pair<llvm::GlobPattern, llvm::StringRef>, 0>
436 remapInputsWildcards;
438 struct ConfigWrapper {
439 Config c;
440 Config *operator->() { return &c; }
443 LLVM_LIBRARY_VISIBILITY extern ConfigWrapper config;
445 struct DuplicateSymbol {
446 const Symbol *sym;
447 const InputFile *file;
448 InputSectionBase *section;
449 uint64_t value;
452 struct Ctx {
453 LinkerDriver driver;
454 SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
455 SmallVector<ELFFileBase *, 0> objectFiles;
456 SmallVector<SharedFile *, 0> sharedFiles;
457 SmallVector<BinaryFile *, 0> binaryFiles;
458 SmallVector<BitcodeFile *, 0> bitcodeFiles;
459 SmallVector<BitcodeFile *, 0> lazyBitcodeFiles;
460 SmallVector<InputSectionBase *, 0> inputSections;
461 SmallVector<EhInputSection *, 0> ehInputSections;
462 // Duplicate symbol candidates.
463 SmallVector<DuplicateSymbol, 0> duplicates;
464 // Symbols in a non-prevailing COMDAT group which should be changed to an
465 // Undefined.
466 SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;
467 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
468 SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
469 whyExtractRecords;
470 // A mapping from a symbol to an InputFile referencing it backward. Used by
471 // --warn-backrefs.
472 llvm::DenseMap<const Symbol *,
473 std::pair<const InputFile *, const InputFile *>>
474 backwardReferences;
475 llvm::SmallSet<llvm::StringRef, 0> auxiliaryFiles;
476 // True if SHT_LLVM_SYMPART is used.
477 std::atomic<bool> hasSympart{false};
478 // True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared.
479 std::atomic<bool> hasTlsIe{false};
480 // True if we need to reserve two .got entries for local-dynamic TLS model.
481 std::atomic<bool> needsTlsLd{false};
482 // True if all native vtable symbols have corresponding type info symbols
483 // during LTO.
484 bool ltoAllVtablesHaveTypeInfos;
486 // Each symbol assignment and DEFINED(sym) reference is assigned an increasing
487 // order. Each DEFINED(sym) evaluation checks whether the reference happens
488 // before a possible `sym = expr;`.
489 unsigned scriptSymOrderCounter = 1;
490 llvm::DenseMap<const Symbol *, unsigned> scriptSymOrder;
492 void reset();
494 llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);
497 LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
499 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
500 // VER_NDX_GLOBAL. This helper returns other elements.
501 static inline ArrayRef<VersionDefinition> namedVersionDefs() {
502 return llvm::ArrayRef(config->versionDefinitions).slice(2);
505 void errorOrWarn(const Twine &msg);
507 static inline void internalLinkerError(StringRef loc, const Twine &msg) {
508 errorOrWarn(loc + "internal linker error: " + msg + "\n" +
509 llvm::getBugReportMsg());
512 } // namespace lld::elf
514 #endif