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//===----------------------------------------------------------------------===//
8
9#ifndef LLD_WASM_CONFIG_H
10#define LLD_WASM_CONFIG_H
11
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/StringSet.h"
15#include "llvm/BinaryFormat/Wasm.h"
16#include "llvm/Support/CachePruning.h"
17#include <optional>
18
19namespace llvm {
20enum class CodeGenOptLevel;
21} // namespace llvm
22
23namespace lld::wasm {
24
25class InputFile;
26class StubFile;
27class ObjFile;
28class SharedFile;
29class BitcodeFile;
30class InputTable;
31class InputGlobal;
32class InputFunction;
33class Symbol;
34
35// For --unresolved-symbols.
36enum class UnresolvedPolicy { ReportError, Warn, Ignore, ImportDynamic };
37
38// For --build-id.
39enum class BuildIdKind { None, Fast, Sha1, Hexstring, Uuid };
40
41// This struct contains the global configuration for the linker.
42// Most fields are direct mapping from the command line options
43// and such fields have the same name as the corresponding options.
44// Most fields are initialized by the driver.
45struct Configuration {
46 bool bsymbolic;
47 bool checkFeatures;
48 bool compressRelocations;
49 bool demangle;
50 bool disableVerify;
51 bool experimentalPic;
52 bool emitRelocs;
53 bool exportAll;
54 bool exportDynamic;
55 bool exportTable;
56 bool extendedConst;
57 bool growableTable;
58 bool gcSections;
59 llvm::StringSet<> keepSections;
60 std::optional<std::pair<llvm::StringRef, llvm::StringRef>> memoryImport;
61 std::optional<llvm::StringRef> memoryExport;
62 bool sharedMemory;
63 bool importTable;
64 bool importUndefined;
65 std::optional<bool> is64;
66 bool mergeDataSegments;
67 bool pie;
68 bool printGcSections;
69 bool relocatable;
70 bool saveTemps;
71 bool shared;
72 bool shlibSigCheck;
73 bool stripAll;
74 bool stripDebug;
75 bool stackFirst;
76 // Because dyamanic linking under Wasm is still experimental we default to
77 // static linking
78 bool isStatic = true;
79 bool trace;
80 uint64_t globalBase;
81 uint64_t initialHeap;
82 uint64_t initialMemory;
83 uint64_t maxMemory;
84 bool noGrowableMemory;
85 // The table offset at which to place function addresses. We reserve zero
86 // for the null function pointer. This gets set to 1 for executables and 0
87 // for shared libraries (since they always added to a dynamic offset at
88 // runtime).
89 uint64_t tableBase;
90 uint64_t zStackSize;
91 unsigned ltoPartitions;
92 unsigned ltoo;
93 llvm::CodeGenOptLevel ltoCgo;
94 unsigned optimize;
95 llvm::StringRef thinLTOJobs;
96 bool ltoDebugPassManager;
97 UnresolvedPolicy unresolvedSymbols;
98 BuildIdKind buildId = BuildIdKind::None;
99
100 llvm::StringRef entry;
101 llvm::StringRef mapFile;
102 llvm::StringRef outputFile;
103 llvm::StringRef soName;
104 llvm::StringRef thinLTOCacheDir;
105 llvm::StringRef whyExtract;
106
107 llvm::StringSet<> allowUndefinedSymbols;
108 llvm::StringSet<> exportedSymbols;
109 std::vector<llvm::StringRef> requiredExports;
110 llvm::SmallVector<llvm::StringRef, 0> searchPaths;
111 llvm::CachePruningPolicy thinLTOCachePolicy;
112 std::optional<std::vector<std::string>> features;
113 std::optional<std::vector<std::string>> extraFeatures;
114 llvm::SmallVector<uint8_t, 0> buildIdVector;
115};
116
117// The only instance of Configuration struct.
118extern Configuration *config;
119
120// The Ctx object hold all other (non-configuration) global state.
121struct Ctx {
122 llvm::SmallVector<ObjFile *, 0> objectFiles;
123 llvm::SmallVector<StubFile *, 0> stubFiles;
124 llvm::SmallVector<SharedFile *, 0> sharedFiles;
125 llvm::SmallVector<BitcodeFile *, 0> bitcodeFiles;
126 llvm::SmallVector<InputFunction *, 0> syntheticFunctions;
127 llvm::SmallVector<InputGlobal *, 0> syntheticGlobals;
128 llvm::SmallVector<InputTable *, 0> syntheticTables;
129
130 // True if we are creating position-independent code.
131 bool isPic = false;
132
133 // True if we have an MVP input that uses __indirect_function_table and which
134 // requires it to be allocated to table number 0.
135 bool legacyFunctionTable = false;
136
137 // Will be set to true if bss data segments should be emitted. In most cases
138 // this is not necessary.
139 bool emitBssSegments = false;
140
141 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
142 llvm::SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>,
143 0>
144 whyExtractRecords;
145
146 void reset();
147};
148
149extern Ctx ctx;
150
151} // namespace lld::wasm
152
153#endif
154