diff --git a/lld/test/wasm/pie.ll b/lld/test/wasm/pie.ll --- a/lld/test/wasm/pie.ll +++ b/lld/test/wasm/pie.ll @@ -40,6 +40,11 @@ ; CHECK-NEXT: Limits: ; CHECK-NEXT: Initial: 0x00000001 ; CHECK-NEXT: - Module: env +; CHECK-NEXT: Field: __stack_pointer +; CHECK-NEXT: Kind: GLOBAL +; CHECK-NEXT: GlobalType: I32 +; CHECK-NEXT: GlobalMutable: true +; CHECK-NEXT: - Module: env ; CHECK-NEXT: Field: __memory_base ; CHECK-NEXT: Kind: GLOBAL ; CHECK-NEXT: GlobalType: I32 diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -482,11 +482,22 @@ } } - // The __stack_pointer is imported in the shared library case, and exported - // in the non-shared (executable) case. - if (Config->Shared) { + if (!Config->Shared) + WasmSym::DataEnd = Symtab->addOptionalDataSymbol("__data_end"); + + if (Config->Pic) { WasmSym::StackPointer = createUndefinedGlobal("__stack_pointer", &MutableGlobalTypeI32); + // For PIC code, we import two global variables (__memory_base and + // __table_base) from the environment and use these as the offset at + // which to load our static data and function table. + // See: + // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md + WasmSym::MemoryBase = + createUndefinedGlobal("__memory_base", &GlobalTypeI32); + WasmSym::TableBase = createUndefinedGlobal("__table_base", &GlobalTypeI32); + WasmSym::MemoryBase->markLive(); + WasmSym::TableBase->markLive(); } else { llvm::wasm::WasmGlobal Global; Global.Type = {WASM_TYPE_I32, true}; @@ -501,24 +512,10 @@ // See: https://github.com/WebAssembly/mutable-global WasmSym::StackPointer = Symtab->addSyntheticGlobal( "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer); - WasmSym::DataEnd = Symtab->addOptionalDataSymbol("__data_end"); WasmSym::GlobalBase = Symtab->addOptionalDataSymbol("__global_base"); WasmSym::HeapBase = Symtab->addOptionalDataSymbol("__heap_base"); } - if (Config->Pic) { - // For PIC code, we import two global variables (__memory_base and - // __table_base) from the environment and use these as the offset at - // which to load our static data and function table. - // See: - // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md - WasmSym::MemoryBase = - createUndefinedGlobal("__memory_base", &GlobalTypeI32); - WasmSym::TableBase = createUndefinedGlobal("__table_base", &GlobalTypeI32); - WasmSym::MemoryBase->markLive(); - WasmSym::TableBase->markLive(); - } - WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol( "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN); } diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -204,7 +204,7 @@ uint32_t MemoryPtr = 0; auto PlaceStack = [&]() { - if (Config->Relocatable || Config->Shared) + if (Config->Relocatable || Config->Pic) return; MemoryPtr = alignTo(MemoryPtr, StackAlignment); if (Config->ZStackSize != alignTo(Config->ZStackSize, StackAlignment))