diff --git a/lld/test/wasm/export-all.s b/lld/test/wasm/export-all.s index 009da9f6a38170..fa016a30d75024 100644 --- a/lld/test/wasm/export-all.s +++ b/lld/test/wasm/export-all.s @@ -34,15 +34,21 @@ foo: # CHECK-NEXT: - Name: __data_end # CHECK-NEXT: Kind: GLOBAL # CHECK-NEXT: Index: 2 -# CHECK-NEXT: - Name: __global_base +# CHECK-NEXT: - Name: __stack_low # CHECK-NEXT: Kind: GLOBAL # CHECK-NEXT: Index: 3 -# CHECK-NEXT: - Name: __heap_base +# CHECK-NEXT: - Name: __stack_high # CHECK-NEXT: Kind: GLOBAL # CHECK-NEXT: Index: 4 -# CHECK-NEXT: - Name: __memory_base +# CHECK-NEXT: - Name: __global_base # CHECK-NEXT: Kind: GLOBAL # CHECK-NEXT: Index: 5 -# CHECK-NEXT: - Name: __table_base +# CHECK-NEXT: - Name: __heap_base # CHECK-NEXT: Kind: GLOBAL # CHECK-NEXT: Index: 6 +# CHECK-NEXT: - Name: __memory_base +# CHECK-NEXT: Kind: GLOBAL +# CHECK-NEXT: Index: 7 +# CHECK-NEXT: - Name: __table_base +# CHECK-NEXT: Kind: GLOBAL +# CHECK-NEXT: Index: 8 diff --git a/lld/test/wasm/mutable-global-exports.s b/lld/test/wasm/mutable-global-exports.s index 278be3a2e3b191..c85c21f1b4ca80 100644 --- a/lld/test/wasm/mutable-global-exports.s +++ b/lld/test/wasm/mutable-global-exports.s @@ -80,18 +80,24 @@ _start: # CHECK-ALL-NEXT: - Name: __data_end # CHECK-ALL-NEXT: Kind: GLOBAL # CHECK-ALL-NEXT: Index: 3 -# CHECK-ALL-NEXT: - Name: __global_base +# CHECK-ALL-NEXT: - Name: __stack_low # CHECK-ALL-NEXT: Kind: GLOBAL # CHECK-ALL-NEXT: Index: 4 -# CHECK-ALL-NEXT: - Name: __heap_base +# CHECK-ALL-NEXT: - Name: __stack_high # CHECK-ALL-NEXT: Kind: GLOBAL # CHECK-ALL-NEXT: Index: 5 -# CHECK-ALL-NEXT: - Name: __memory_base +# CHECK-ALL-NEXT: - Name: __global_base # CHECK-ALL-NEXT: Kind: GLOBAL # CHECK-ALL-NEXT: Index: 6 -# CHECK-ALL-NEXT: - Name: __table_base +# CHECK-ALL-NEXT: - Name: __heap_base # CHECK-ALL-NEXT: Kind: GLOBAL # CHECK-ALL-NEXT: Index: 7 +# CHECK-ALL-NEXT: - Name: __memory_base +# CHECK-ALL-NEXT: Kind: GLOBAL +# CHECK-ALL-NEXT: Index: 8 +# CHECK-ALL-NEXT: - Name: __table_base +# CHECK-ALL-NEXT: Kind: GLOBAL +# CHECK-ALL-NEXT: Index: 9 # CHECK-ALL-NEXT: - Type: CODE # CHECK-ALL: Name: target_features diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index 62f90d7eaa7b4e..cb41cf64d5e251 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -732,6 +732,8 @@ static void createOptionalSymbols() { WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end"); if (!config->isPic) { + WasmSym::stackLow = symtab->addOptionalDataSymbol("__stack_low"); + WasmSym::stackHigh = symtab->addOptionalDataSymbol("__stack_high"); WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base"); WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base"); WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base"); diff --git a/lld/wasm/Symbols.cpp b/lld/wasm/Symbols.cpp index e0670cea6425e3..4ee5125c65abca 100644 --- a/lld/wasm/Symbols.cpp +++ b/lld/wasm/Symbols.cpp @@ -85,6 +85,8 @@ DefinedData *WasmSym::globalBase; DefinedData *WasmSym::heapBase; DefinedData *WasmSym::initMemoryFlag; GlobalSymbol *WasmSym::stackPointer; +DefinedData *WasmSym::stackLow; +DefinedData *WasmSym::stackHigh; GlobalSymbol *WasmSym::tlsBase; GlobalSymbol *WasmSym::tlsSize; GlobalSymbol *WasmSym::tlsAlign; diff --git a/lld/wasm/Symbols.h b/lld/wasm/Symbols.h index d2a0cb0becc379..98aae86b8982f9 100644 --- a/lld/wasm/Symbols.h +++ b/lld/wasm/Symbols.h @@ -516,10 +516,13 @@ struct WasmSym { // Symbol marking the start of the global section. static DefinedData *globalBase; - // __stack_pointer - // Global that holds the address of the top of the explicit value stack in - // linear memory. + // __stack_pointer/__stack_low/__stack_high + // Global that holds current value of stack pointer and data symbols marking + // the start and end of the stack region. stackPointer is initialized to + // stackHigh and grows downwards towards stackLow static GlobalSymbol *stackPointer; + static DefinedData *stackLow; + static DefinedData *stackHigh; // __tls_base // Global that holds the address of the base of the current thread's diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index c8377dd0f972c3..47ac23ff20a1fa 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -242,12 +242,16 @@ void Writer::layoutMemory() { if (config->relocatable || config->isPic) return; memoryPtr = alignTo(memoryPtr, stackAlignment); + if (WasmSym::stackLow) + WasmSym::stackLow->setVA(memoryPtr); if (config->zStackSize != alignTo(config->zStackSize, stackAlignment)) error("stack size must be " + Twine(stackAlignment) + "-byte aligned"); log("mem: stack size = " + Twine(config->zStackSize)); log("mem: stack base = " + Twine(memoryPtr)); memoryPtr += config->zStackSize; setGlobalPtr(cast(WasmSym::stackPointer), memoryPtr); + if (WasmSym::stackHigh) + WasmSym::stackHigh->setVA(memoryPtr); log("mem: stack top = " + Twine(memoryPtr)); };