blob: 9d903e0c799db476da3d7eb7ab5db7cc022055d7 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- Config.h -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam Cleggc94d3932017-11-17 18:14:09 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_WASM_CONFIG_H
10#define LLD_WASM_CONFIG_H
11
Fangrui Song4b2b68d2022-07-30 18:11:21 -070012#include "llvm/ADT/SmallVector.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000013#include "llvm/ADT/StringRef.h"
Rui Ueyama7d5a1372017-11-29 22:21:37 +000014#include "llvm/ADT/StringSet.h"
mzukovec03673052024-09-04 17:50:10 +020015#include "llvm/ADT/Twine.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000016#include "llvm/BinaryFormat/Wasm.h"
Sam Cleggc729c1b2018-05-30 18:07:52 +000017#include "llvm/Support/CachePruning.h"
Kazu Hiratab9ef5642023-01-02 18:29:04 -080018#include <optional>
Sam Cleggc94d3932017-11-17 18:14:09 +000019
Arthur Eubanks0a1aa6c2023-09-14 14:10:14 -070020namespace llvm {
21enum class CodeGenOptLevel;
22} // namespace llvm
Scott Linder45ee0a92023-02-15 17:12:47 +000023
Sam Cleggd32f71a2023-03-06 09:55:00 -080024namespace lld::wasm {
Sam Cleggc94d3932017-11-17 18:14:09 +000025
Sam Clegg8aef04f2023-03-06 09:18:03 -080026class InputFile;
Sam Clegg3c584572024-01-18 15:53:13 -080027class StubFile;
28class ObjFile;
29class SharedFile;
30class BitcodeFile;
31class InputTable;
32class InputGlobal;
33class InputFunction;
Sam Clegg8aef04f2023-03-06 09:18:03 -080034class Symbol;
Anutosh Bhat9cbbb742025-04-25 20:05:00 +053035class DefinedData;
36class GlobalSymbol;
37class DefinedFunction;
38class UndefinedGlobal;
39class TableSymbol;
Sam Clegg8aef04f2023-03-06 09:18:03 -080040
Sam Clegg206884b2020-05-01 09:14:59 -070041// For --unresolved-symbols.
Sam Clegg86c90f92020-11-16 10:11:37 -080042enum class UnresolvedPolicy { ReportError, Warn, Ignore, ImportDynamic };
Sam Clegg206884b2020-05-01 09:14:59 -070043
Derek Schuffc7af9ae2023-03-02 15:52:25 -080044// For --build-id.
45enum class BuildIdKind { None, Fast, Sha1, Hexstring, Uuid };
46
Sam Clegga1282a32019-05-08 16:20:05 +000047// This struct contains the global configuration for the linker.
48// Most fields are direct mapping from the command line options
49// and such fields have the same name as the corresponding options.
50// Most fields are initialized by the driver.
Fangrui Songa222d002024-12-13 19:14:32 -080051struct Config {
mzukovec03673052024-09-04 17:50:10 +020052 bool allowMultipleDefinition;
Sam Clegg25134072020-10-07 14:48:37 -070053 bool bsymbolic;
Rui Ueyama136d27a2019-07-11 05:40:30 +000054 bool checkFeatures;
55 bool compressRelocations;
56 bool demangle;
57 bool disableVerify;
Dan Gohman46a32682020-06-12 12:05:40 -070058 bool experimentalPic;
Rui Ueyama136d27a2019-07-11 05:40:30 +000059 bool emitRelocs;
60 bool exportAll;
61 bool exportDynamic;
62 bool exportTable;
Sam Clegg4690bf22022-03-07 15:50:30 -080063 bool extendedConst;
Jacob Gravelle92ed86d2019-08-27 22:58:21 +000064 bool growableTable;
Rui Ueyama136d27a2019-07-11 05:40:30 +000065 bool gcSections;
Sam Clegg89d56352023-05-04 16:04:04 -070066 llvm::StringSet<> keepSections;
Kazu Hiratab9ef5642023-01-02 18:29:04 -080067 std::optional<std::pair<llvm::StringRef, llvm::StringRef>> memoryImport;
68 std::optional<llvm::StringRef> memoryExport;
Rui Ueyama136d27a2019-07-11 05:40:30 +000069 bool sharedMemory;
Rui Ueyama136d27a2019-07-11 05:40:30 +000070 bool importTable;
Sam Clegg758633f2021-05-27 14:27:10 -070071 bool importUndefined;
Kazu Hiratab9ef5642023-01-02 18:29:04 -080072 std::optional<bool> is64;
Rui Ueyama136d27a2019-07-11 05:40:30 +000073 bool mergeDataSegments;
mzukovec03673052024-09-04 17:50:10 +020074 bool noinhibitExec;
Rui Ueyama136d27a2019-07-11 05:40:30 +000075 bool pie;
76 bool printGcSections;
77 bool relocatable;
78 bool saveTemps;
79 bool shared;
Sam Clegg22b7b842024-07-12 13:26:52 -070080 bool shlibSigCheck;
Rui Ueyama136d27a2019-07-11 05:40:30 +000081 bool stripAll;
82 bool stripDebug;
83 bool stackFirst;
YAMAMOTO Takashi2b6c6bb2024-06-12 08:45:53 +090084 // Because dyamanic linking under Wasm is still experimental we default to
85 // static linking
86 bool isStatic = true;
Sam Clegg9a450a02024-11-01 16:34:06 -070087 bool thinLTOEmitImportsFiles;
88 bool thinLTOEmitIndexFiles;
89 bool thinLTOIndexOnly;
Rui Ueyama136d27a2019-07-11 05:40:30 +000090 bool trace;
Thomas Livelyc496d842020-04-03 16:18:29 -070091 uint64_t globalBase;
SingleAccretionb2cdf3c2023-12-15 21:16:38 +030092 uint64_t initialHeap;
Thomas Livelyc496d842020-04-03 16:18:29 -070093 uint64_t initialMemory;
94 uint64_t maxMemory;
SingleAccretioncb4f94d2024-02-25 19:43:11 +030095 bool noGrowableMemory;
Sam Clegg184c22d2024-01-18 15:01:21 -080096 // The table offset at which to place function addresses. We reserve zero
97 // for the null function pointer. This gets set to 1 for executables and 0
98 // for shared libraries (since they always added to a dynamic offset at
99 // runtime).
100 uint64_t tableBase;
Thomas Livelyc496d842020-04-03 16:18:29 -0700101 uint64_t zStackSize;
Nick Fitzgerald60189302025-03-04 09:39:30 -0800102 uint64_t pageSize;
Rui Ueyama136d27a2019-07-11 05:40:30 +0000103 unsigned ltoPartitions;
104 unsigned ltoo;
Arthur Eubanks0a1aa6c2023-09-14 14:10:14 -0700105 llvm::CodeGenOptLevel ltoCgo;
Rui Ueyama136d27a2019-07-11 05:40:30 +0000106 unsigned optimize;
Arthur Eubanks1314a492020-12-01 12:22:27 -0800107 bool ltoDebugPassManager;
Sam Clegg206884b2020-05-01 09:14:59 -0700108 UnresolvedPolicy unresolvedSymbols;
Derek Schuffc7af9ae2023-03-02 15:52:25 -0800109 BuildIdKind buildId = BuildIdKind::None;
Sam Clegga1282a32019-05-08 16:20:05 +0000110
Rui Ueyama136d27a2019-07-11 05:40:30 +0000111 llvm::StringRef entry;
Sam Clegg9a450a02024-11-01 16:34:06 -0700112 llvm::StringRef ltoObjPath;
Sam Cleggcc2da552020-03-27 16:52:27 -0700113 llvm::StringRef mapFile;
Rui Ueyama136d27a2019-07-11 05:40:30 +0000114 llvm::StringRef outputFile;
Sam Clegg8e44f032023-05-26 18:17:45 -0700115 llvm::StringRef soName;
Rui Ueyama136d27a2019-07-11 05:40:30 +0000116 llvm::StringRef thinLTOCacheDir;
Sam Clegg9a450a02024-11-01 16:34:06 -0700117 llvm::StringRef thinLTOJobs;
118 llvm::StringRef thinLTOIndexOnlyArg;
Sam Cleggb70eb862024-11-08 16:48:30 -0800119 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
120 llvm::StringRef thinLTOPrefixReplaceOld;
121 llvm::StringRef thinLTOPrefixReplaceNew;
122 llvm::StringRef thinLTOPrefixReplaceNativeObject;
Sam Clegg8aef04f2023-03-06 09:18:03 -0800123 llvm::StringRef whyExtract;
Sam Cleggc94d3932017-11-17 18:14:09 +0000124
Rui Ueyama136d27a2019-07-11 05:40:30 +0000125 llvm::StringSet<> allowUndefinedSymbols;
126 llvm::StringSet<> exportedSymbols;
Sam Clegga6f40642021-04-05 08:00:30 -0700127 std::vector<llvm::StringRef> requiredExports;
Fangrui Song4b2b68d2022-07-30 18:11:21 -0700128 llvm::SmallVector<llvm::StringRef, 0> searchPaths;
Hood Chatham80ea31c2025-02-28 20:12:52 +0100129 llvm::SmallVector<llvm::StringRef, 0> rpath;
Rui Ueyama136d27a2019-07-11 05:40:30 +0000130 llvm::CachePruningPolicy thinLTOCachePolicy;
Kazu Hiratab9ef5642023-01-02 18:29:04 -0800131 std::optional<std::vector<std::string>> features;
132 std::optional<std::vector<std::string>> extraFeatures;
Derek Schuffc7af9ae2023-03-02 15:52:25 -0800133 llvm::SmallVector<uint8_t, 0> buildIdVector;
Sam Clegg184c22d2024-01-18 15:01:21 -0800134};
Sam Cleggbfb75342018-11-15 00:37:21 +0000135
Sam Clegg184c22d2024-01-18 15:01:21 -0800136// The Ctx object hold all other (non-configuration) global state.
137struct Ctx {
Fangrui Song3792b362025-01-02 17:08:18 -0800138 Config arg;
Fangrui Songa222d002024-12-13 19:14:32 -0800139
Sam Clegg3c584572024-01-18 15:53:13 -0800140 llvm::SmallVector<ObjFile *, 0> objectFiles;
141 llvm::SmallVector<StubFile *, 0> stubFiles;
142 llvm::SmallVector<SharedFile *, 0> sharedFiles;
143 llvm::SmallVector<BitcodeFile *, 0> bitcodeFiles;
Sam Clegg9a450a02024-11-01 16:34:06 -0700144 llvm::SmallVector<BitcodeFile *, 0> lazyBitcodeFiles;
Sam Clegg3c584572024-01-18 15:53:13 -0800145 llvm::SmallVector<InputFunction *, 0> syntheticFunctions;
146 llvm::SmallVector<InputGlobal *, 0> syntheticGlobals;
147 llvm::SmallVector<InputTable *, 0> syntheticTables;
148
Anutosh Bhat9cbbb742025-04-25 20:05:00 +0530149 // linker-generated symbols
150 struct WasmSym {
151 // __global_base
152 // Symbol marking the start of the global section.
153 DefinedData *globalBase;
154
155 // __stack_pointer/__stack_low/__stack_high
156 // Global that holds current value of stack pointer and data symbols marking
157 // the start and end of the stack region. stackPointer is initialized to
158 // stackHigh and grows downwards towards stackLow
159 GlobalSymbol *stackPointer;
160 DefinedData *stackLow;
161 DefinedData *stackHigh;
162
163 // __tls_base
164 // Global that holds the address of the base of the current thread's
165 // TLS block.
166 GlobalSymbol *tlsBase;
167
168 // __tls_size
169 // Symbol whose value is the size of the TLS block.
170 GlobalSymbol *tlsSize;
171
172 // __tls_size
173 // Symbol whose value is the alignment of the TLS block.
174 GlobalSymbol *tlsAlign;
175
176 // __data_end
177 // Symbol marking the end of the data and bss.
178 DefinedData *dataEnd;
179
180 // __heap_base/__heap_end
181 // Symbols marking the beginning and end of the "heap". It starts at the end
182 // of the data, bss and explicit stack, and extends to the end of the linear
183 // memory allocated by wasm-ld. This region of memory is not used by the
184 // linked code, so it may be used as a backing store for `sbrk` or `malloc`
185 // implementations.
186 DefinedData *heapBase;
187 DefinedData *heapEnd;
188
189 // __wasm_first_page_end
190 // A symbol whose address is the end of the first page in memory (if any).
191 DefinedData *firstPageEnd;
192
193 // __wasm_init_memory_flag
194 // Symbol whose contents are nonzero iff memory has already been
195 // initialized.
196 DefinedData *initMemoryFlag;
197
198 // __wasm_init_memory
199 // Function that initializes passive data segments during instantiation.
200 DefinedFunction *initMemory;
201
202 // __wasm_call_ctors
203 // Function that directly calls all ctors in priority order.
204 DefinedFunction *callCtors;
205
206 // __wasm_call_dtors
207 // Function that calls the libc/etc. cleanup function.
208 DefinedFunction *callDtors;
209
210 // __wasm_apply_global_relocs
211 // Function that applies relocations to wasm globals post-instantiation.
212 // Unlike __wasm_apply_data_relocs this needs to run on every thread.
213 DefinedFunction *applyGlobalRelocs;
214
215 // __wasm_apply_tls_relocs
216 // Like __wasm_apply_data_relocs but for TLS section. These must be
217 // delayed until __wasm_init_tls.
218 DefinedFunction *applyTLSRelocs;
219
220 // __wasm_apply_global_tls_relocs
221 // Like applyGlobalRelocs but for globals that hold TLS addresses. These
222 // must be delayed until __wasm_init_tls.
223 DefinedFunction *applyGlobalTLSRelocs;
224
225 // __wasm_init_tls
226 // Function that allocates thread-local storage and initializes it.
227 DefinedFunction *initTLS;
228
229 // Pointer to the function that is to be used in the start section.
230 // (normally an alias of initMemory, or applyGlobalRelocs).
231 DefinedFunction *startFunction;
232
233 // __dso_handle
234 // Symbol used in calls to __cxa_atexit to determine current DLL
235 DefinedData *dsoHandle;
236
237 // __table_base
238 // Used in PIC code for offset of indirect function table
239 UndefinedGlobal *tableBase;
240 DefinedData *definedTableBase;
241
242 // __memory_base
243 // Used in PIC code for offset of global data
244 UndefinedGlobal *memoryBase;
245 DefinedData *definedMemoryBase;
246
247 // __indirect_function_table
248 // Used as an address space for function pointers, with each function that
249 // is used as a function pointer being allocated a slot.
250 TableSymbol *indirectFunctionTable;
251 };
252 WasmSym sym;
253
Sam Cleggbfb75342018-11-15 00:37:21 +0000254 // True if we are creating position-independent code.
Sam Clegg2bfa5ca2024-01-19 13:51:35 -0800255 bool isPic = false;
Sam Clegg1a1df722019-08-27 04:19:34 +0000256
Andy Wingo4fc25572021-02-12 20:01:41 +0100257 // True if we have an MVP input that uses __indirect_function_table and which
258 // requires it to be allocated to table number 0.
259 bool legacyFunctionTable = false;
260
Sam Clegg1eb79e72021-10-26 18:08:07 -0700261 // Will be set to true if bss data segments should be emitted. In most cases
262 // this is not necessary.
263 bool emitBssSegments = false;
Sam Clegg8aef04f2023-03-06 09:18:03 -0800264
265 // A tuple of (reference, extractedFile, sym). Used by --why-extract=.
266 llvm::SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>,
267 0>
268 whyExtractRecords;
Sam Clegg2bfa5ca2024-01-19 13:51:35 -0800269
Fangrui Songa222d002024-12-13 19:14:32 -0800270 Ctx();
Sam Clegg2bfa5ca2024-01-19 13:51:35 -0800271 void reset();
Sam Cleggc94d3932017-11-17 18:14:09 +0000272};
273
Sam Clegg184c22d2024-01-18 15:01:21 -0800274extern Ctx ctx;
Sam Cleggc94d3932017-11-17 18:14:09 +0000275
mzukovec03673052024-09-04 17:50:10 +0200276void errorOrWarn(const llvm::Twine &msg);
277
Sam Cleggd32f71a2023-03-06 09:55:00 -0800278} // namespace lld::wasm
Sam Cleggc94d3932017-11-17 18:14:09 +0000279
280#endif