Skip to content

Commit 830fcb8

Browse files
authored
Use explicit linker generated high/low symbols to determine stack bounds. NFC (#18057)
See https://reviews.llvm.org/D135910
1 parent bd5fd33 commit 830fcb8

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

emscripten.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ def compile_settings():
209209
def set_memory(static_bump):
210210
stack_low = align_memory(settings.GLOBAL_BASE + static_bump)
211211
stack_high = align_memory(stack_low + settings.TOTAL_STACK)
212-
settings.STACK_BASE = stack_high
213-
settings.STACK_MAX = stack_low
212+
settings.STACK_HIGH = stack_high
213+
settings.STACK_LOW = stack_low
214214
settings.HEAP_BASE = align_memory(stack_high)
215215

216216

@@ -353,7 +353,7 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
353353
dylink_sec = webassembly.parse_dylink_section(in_wasm)
354354
static_bump = align_memory(dylink_sec.mem_size)
355355
set_memory(static_bump)
356-
logger.debug('stack_base: %d, stack_max: %d, heap_base: %d', settings.STACK_BASE, settings.STACK_MAX, settings.HEAP_BASE)
356+
logger.debug('stack_low: %d, stack_high: %d, heap_base: %d', settings.STACK_LOW, settings.STACK_HIGH, settings.HEAP_BASE)
357357

358358
# When building relocatable output (e.g. MAIN_MODULE) the reported table
359359
# size does not include the reserved slot at zero for the null pointer.

src/library.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3600,7 +3600,7 @@ mergeInto(LibraryManager.library, {
36003600
#if RELOCATABLE
36013601
// Globals that are normally exported from the wasm module but in relocatable
36023602
// mode are created here and imported by the module.
3603-
__stack_pointer: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ to64(STACK_BASE) }}})",
3603+
__stack_pointer: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ to64(STACK_HIGH) }}})",
36043604
// tell the memory segments where to place themselves
36053605
__memory_base: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': false}, {{{ to64(GLOBAL_BASE) }}})",
36063606
// the wasm backend reserves slot 0 for the NULL function pointer
@@ -3617,6 +3617,8 @@ mergeInto(LibraryManager.library, {
36173617
// have __heap_base hardcoded into it - it receives it from JS as an extern
36183618
// global, basically).
36193619
__heap_base: '{{{ HEAP_BASE }}}',
3620+
__stack_high: '{{{ STACK_HIGH }}}',
3621+
__stack_low: '{{{ STACK_LOW }}}',
36203622
__global_base: '{{{ GLOBAL_BASE }}}',
36213623
#if WASM_EXCEPTIONS
36223624
// In dynamic linking we define tags here and feed them to each module

src/postamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function stackCheckInit() {
222222
assert(!ENVIRONMENT_IS_PTHREAD);
223223
#endif
224224
#if RELOCATABLE
225-
_emscripten_stack_set_limits({{{ STACK_BASE }}} , {{{ STACK_MAX }}});
225+
_emscripten_stack_set_limits({{{ STACK_HIGH }}} , {{{ STACK_LOW }}});
226226
#else
227227
_emscripten_stack_init();
228228
#endif

src/settings_internal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ var GENERATE_DWARF = false;
215215

216216
// Memory layout. These are only used/set in RELOCATABLE builds. Otherwise
217217
// memory layout is fixed in the wasm binary at link time.
218-
var STACK_BASE = 0;
219-
var STACK_MAX = 0;
218+
var STACK_HIGH = 0;
219+
var STACK_LOW = 0;
220220
var HEAP_BASE = 0;
221221

222222
// Used internally. set when there is a main() function.

system/lib/compiler-rt/stack_limits.S

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ emscripten_stack_init:
3939
# emscripten_stack_get_base, or emscripten_stack_get_free are called
4040
.functype emscripten_stack_init () -> ()
4141

42-
# The heap base is where the stack grown down from.
42+
# What llvm calls __stack_high is the high address from where is grows
43+
# downwards. We call this the stack base here in emscripten.
4344
#ifdef __PIC__
44-
global.get __heap_base@GOT
45+
global.get __stack_high@GOT
4546
#else
46-
PTR.const __heap_base
47+
PTR.const __stack_high
4748
#endif
4849
global.set __stack_base
4950

50-
# The end of stack data is the limit of the stack growth
51+
# What llvm calls __stack_low is that end of the stack
5152
#ifdef __PIC__
52-
global.get __data_end@GOT
53+
global.get __stack_low@GOT
5354
#else
54-
PTR.const __data_end
55+
PTR.const __stack_low
5556
#endif
5657
# Align up to 16 bytes
5758
PTR.const 0xf

0 commit comments

Comments
 (0)