Index: lld/trunk/test/wasm/global-base.test =================================================================== --- lld/trunk/test/wasm/global-base.test +++ lld/trunk/test/wasm/global-base.test @@ -24,11 +24,7 @@ CHECK-1024-NEXT: Value: 1024 CHECK-1024: - Type: EXPORT -CHECK-1024-NEXT: Exports: -CHECK-1024-NEXT: - Name: memory -CHECK-1024-NEXT: Kind: MEMORY -CHECK-1024-NEXT: Index: 0 -CHECK-1024-NEXT: - Name: __data_end +CHECK-1024: - Name: __data_end CHECK-1024-NEXT: Kind: GLOBAL CHECK-1024-NEXT: Index: 1 CHECK-1024-NEXT: - Name: __global_base @@ -59,11 +55,7 @@ CHECK-16777216-NEXT: Value: 16777216 CHECK-16777216: - Type: EXPORT -CHECK-16777216-NEXT: Exports: -CHECK-16777216-NEXT: - Name: memory -CHECK-16777216-NEXT: Kind: MEMORY -CHECK-16777216-NEXT: Index: 0 -CHECK-16777216-NEXT: - Name: __data_end +CHECK-16777216: - Name: __data_end CHECK-16777216-NEXT: Kind: GLOBAL CHECK-16777216-NEXT: Index: 1 CHECK-16777216-NEXT: - Name: __global_base Index: lld/trunk/test/wasm/optional-symbol.ll =================================================================== --- lld/trunk/test/wasm/optional-symbol.ll +++ lld/trunk/test/wasm/optional-symbol.ll @@ -0,0 +1,14 @@ +; RUN: llc -filetype=obj -o %t.o %s +; RUN: wasm-ld --export=get_handle %t.o -o %t.wasm + +target triple = "wasm32-unknown-unknown" + +@__dso_handle = external global i8* + +define i8** @get_handle() { + ret i8** @__dso_handle +} + +define void @_start() { + ret void +} Index: lld/trunk/test/wasm/stack-first.test =================================================================== --- lld/trunk/test/wasm/stack-first.test +++ lld/trunk/test/wasm/stack-first.test @@ -32,12 +32,12 @@ CHECK-NEXT: - Name: memory CHECK-NEXT: Kind: MEMORY CHECK-NEXT: Index: 0 +CHECK-NEXT: - Name: _start +CHECK-NEXT: Kind: FUNCTION +CHECK-NEXT: Index: 0 CHECK-NEXT: - Name: __data_end CHECK-NEXT: Kind: GLOBAL CHECK-NEXT: Index: 1 CHECK-NEXT: - Name: __heap_base CHECK-NEXT: Kind: GLOBAL CHECK-NEXT: Index: 2 -CHECK-NEXT: - Name: _start -CHECK-NEXT: Kind: FUNCTION -CHECK-NEXT: Index: 0 Index: lld/trunk/wasm/Driver.cpp =================================================================== --- lld/trunk/wasm/Driver.cpp +++ lld/trunk/wasm/Driver.cpp @@ -493,8 +493,6 @@ } } - if (!config->shared) - WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end"); if (config->isPic) { WasmSym::stackPointer = @@ -523,8 +521,6 @@ // See: https://github.com/WebAssembly/mutable-global WasmSym::stackPointer = symtab->addSyntheticGlobal( "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, stackPointer); - WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base"); - WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base"); } if (config->sharedMemory && !config->shared) { @@ -535,9 +531,17 @@ "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN, make(i32ArgSignature, "__wasm_init_tls")); } +} + +static void createOptionalSymbols() { + if (!config->relocatable) + WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle"); - WasmSym::dsoHandle = symtab->addSyntheticDataSymbol( - "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN); + if (!config->isPic) { + WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end"); + WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base"); + WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base"); + } } // Reconstructs command line arguments so that so that you can re-run @@ -720,6 +724,8 @@ if (errorCount()) return; + createOptionalSymbols(); + // Handle the `--undefined ` options. for (auto *arg : args.filtered(OPT_undefined)) handleUndefined(arg->getValue()); Index: lld/trunk/wasm/SymbolTable.h =================================================================== --- lld/trunk/wasm/SymbolTable.h +++ lld/trunk/wasm/SymbolTable.h @@ -77,8 +77,7 @@ InputGlobal *global); DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags, InputFunction *function); - DefinedData *addOptionalDataSymbol(StringRef name, uint32_t value = 0, - uint32_t flags = 0); + DefinedData *addOptionalDataSymbol(StringRef name, uint32_t value = 0); void handleSymbolVariants(); void handleWeakUndefines(); Index: lld/trunk/wasm/SymbolTable.cpp =================================================================== --- lld/trunk/wasm/SymbolTable.cpp +++ lld/trunk/wasm/SymbolTable.cpp @@ -205,15 +205,15 @@ // Adds an optional, linker generated, data symbols. The symbol will only be // added if there is an undefine reference to it, or if it is explictly exported // via the --export flag. Otherwise we don't add the symbol and return nullptr. -DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, uint32_t value, - uint32_t flags) { +DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, + uint32_t value) { Symbol *s = find(name); if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0)) s = insertName(name).first; else if (!s || s->isDefined()) return nullptr; LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n"); - auto *rtn = replaceSymbol(s, name, flags); + auto *rtn = replaceSymbol(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN); rtn->setVirtualAddress(value); rtn->referenced = true; return rtn;