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