| 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/ADT/Twine.h" |
| 16 | #include "llvm/BinaryFormat/Wasm.h" |
| 17 | #include "llvm/Support/CachePruning.h" |
| 18 | #include <optional> |
| 19 | |
| 20 | namespace llvm { |
| 21 | enum class CodeGenOptLevel; |
| 22 | } // namespace llvm |
| 23 | |
| 24 | namespace lld::wasm { |
| 25 | |
| 26 | class InputFile; |
| 27 | class StubFile; |
| 28 | class ObjFile; |
| 29 | class SharedFile; |
| 30 | class BitcodeFile; |
| 31 | class InputTable; |
| 32 | class InputGlobal; |
| 33 | class InputFunction; |
| 34 | class Symbol; |
| 35 | class DefinedData; |
| 36 | class GlobalSymbol; |
| 37 | class DefinedFunction; |
| 38 | class UndefinedGlobal; |
| 39 | class TableSymbol; |
| 40 | |
| 41 | // For --unresolved-symbols. |
| 42 | enum class UnresolvedPolicy { ReportError, Warn, Ignore, ImportDynamic }; |
| 43 | |
| 44 | // For --build-id. |
| 45 | enum class BuildIdKind { None, Fast, Sha1, Hexstring, Uuid }; |
| 46 | |
| 47 | // 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. |
| 51 | struct Config { |
| 52 | bool allowMultipleDefinition; |
| 53 | bool bsymbolic; |
| 54 | bool checkFeatures; |
| 55 | bool compressRelocations; |
| 56 | bool demangle; |
| 57 | bool disableVerify; |
| 58 | bool experimentalPic; |
| 59 | bool emitRelocs; |
| 60 | bool exportAll; |
| 61 | bool exportDynamic; |
| 62 | bool exportTable; |
| 63 | bool extendedConst; |
| 64 | bool growableTable; |
| 65 | bool gcSections; |
| 66 | llvm::StringSet<> keepSections; |
| 67 | std::optional<std::pair<llvm::StringRef, llvm::StringRef>> memoryImport; |
| 68 | std::optional<llvm::StringRef> memoryExport; |
| 69 | bool sharedMemory; |
| 70 | bool importTable; |
| 71 | bool importUndefined; |
| 72 | std::optional<bool> is64; |
| 73 | bool mergeDataSegments; |
| 74 | bool noinhibitExec; |
| 75 | bool pie; |
| 76 | bool printGcSections; |
| 77 | bool relocatable; |
| 78 | bool saveTemps; |
| 79 | bool shared; |
| 80 | bool shlibSigCheck; |
| 81 | bool stripAll; |
| 82 | bool stripDebug; |
| 83 | bool stackFirst; |
| 84 | // Because dyamanic linking under Wasm is still experimental we default to |
| 85 | // static linking |
| 86 | bool isStatic = true; |
| 87 | bool thinLTOEmitImportsFiles; |
| 88 | bool thinLTOEmitIndexFiles; |
| 89 | bool thinLTOIndexOnly; |
| 90 | bool trace; |
| 91 | uint64_t globalBase; |
| 92 | uint64_t initialHeap; |
| 93 | uint64_t initialMemory; |
| 94 | uint64_t maxMemory; |
| 95 | bool noGrowableMemory; |
| 96 | // 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; |
| 101 | uint64_t zStackSize; |
| 102 | uint64_t pageSize; |
| 103 | unsigned ltoPartitions; |
| 104 | unsigned ltoo; |
| 105 | llvm::CodeGenOptLevel ltoCgo; |
| 106 | unsigned optimize; |
| 107 | bool ltoDebugPassManager; |
| 108 | UnresolvedPolicy unresolvedSymbols; |
| 109 | BuildIdKind buildId = BuildIdKind::None; |
| 110 | |
| 111 | llvm::StringRef entry; |
| 112 | llvm::StringRef ltoObjPath; |
| 113 | llvm::StringRef mapFile; |
| 114 | llvm::StringRef outputFile; |
| 115 | llvm::StringRef soName; |
| 116 | llvm::StringRef thinLTOCacheDir; |
| 117 | llvm::StringRef thinLTOJobs; |
| 118 | llvm::StringRef thinLTOIndexOnlyArg; |
| 119 | std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace; |
| 120 | llvm::StringRef thinLTOPrefixReplaceOld; |
| 121 | llvm::StringRef thinLTOPrefixReplaceNew; |
| 122 | llvm::StringRef thinLTOPrefixReplaceNativeObject; |
| 123 | llvm::StringRef ; |
| 124 | |
| 125 | llvm::StringSet<> allowUndefinedSymbols; |
| 126 | llvm::StringSet<> exportedSymbols; |
| 127 | std::vector<llvm::StringRef> requiredExports; |
| 128 | llvm::SmallVector<llvm::StringRef, 0> searchPaths; |
| 129 | llvm::SmallVector<llvm::StringRef, 0> rpath; |
| 130 | llvm::CachePruningPolicy thinLTOCachePolicy; |
| 131 | std::optional<std::vector<std::string>> features; |
| 132 | std::optional<std::vector<std::string>> ; |
| 133 | llvm::SmallVector<uint8_t, 0> buildIdVector; |
| 134 | }; |
| 135 | |
| 136 | // The Ctx object hold all other (non-configuration) global state. |
| 137 | struct Ctx { |
| 138 | Config arg; |
| 139 | |
| 140 | llvm::SmallVector<ObjFile *, 0> objectFiles; |
| 141 | llvm::SmallVector<StubFile *, 0> stubFiles; |
| 142 | llvm::SmallVector<SharedFile *, 0> sharedFiles; |
| 143 | llvm::SmallVector<BitcodeFile *, 0> bitcodeFiles; |
| 144 | llvm::SmallVector<BitcodeFile *, 0> lazyBitcodeFiles; |
| 145 | llvm::SmallVector<InputFunction *, 0> syntheticFunctions; |
| 146 | llvm::SmallVector<InputGlobal *, 0> syntheticGlobals; |
| 147 | llvm::SmallVector<InputTable *, 0> syntheticTables; |
| 148 | |
| 149 | // 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 | |
| 254 | // True if we are creating position-independent code. |
| 255 | bool isPic = false; |
| 256 | |
| 257 | // 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 | |
| 261 | // Will be set to true if bss data segments should be emitted. In most cases |
| 262 | // this is not necessary. |
| 263 | bool emitBssSegments = false; |
| 264 | |
| 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 | ; |
| 269 | |
| 270 | Ctx(); |
| 271 | void reset(); |
| 272 | }; |
| 273 | |
| 274 | extern Ctx ctx; |
| 275 | |
| 276 | void errorOrWarn(const llvm::Twine &msg); |
| 277 | |
| 278 | } // namespace lld::wasm |
| 279 | |
| 280 | #endif |
| 281 | |