Skip to content

Commit 1917a52

Browse files
committed
Handle -Wl,--stack-first being passed explicitly
Fixes: emscripten-core#21215
1 parent 75892b3 commit 1917a52

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

test/test_other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,15 @@ def test_wl_linkflags(self):
11971197
self.run_process([EMCC, 'main.c', '-Wl,-L.', '-Wl,-lfoo'])
11981198
self.run_process([EMCC, 'main.c', '-Wl,@linkflags.txt'])
11991199

1200+
def test_wl_stackfirst(self):
1201+
cmd = [EMCC, test_file('hello_world.c'), '-Wl,--stack-first']
1202+
self.run_process(cmd + ['-O0'])
1203+
self.run_process(cmd + ['-O2'])
1204+
err = self.expect_fail(cmd + ['-fsanitize=address'])
1205+
self.assertContained('error: --stack-first is not compatible with asan', err)
1206+
err = self.expect_fail(cmd + ['-sGLOBAL_BASE=1024'])
1207+
self.assertContained('error: --stack-first is not compatible with -sGLOBAL_BASE', err)
1208+
12001209
def test_l_link(self):
12011210
# Linking with -lLIBNAME and -L/DIRNAME should work, also should work with spaces
12021211
create_file('main.c', '''

tools/link.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -879,14 +879,6 @@ def phase_linker_setup(options, state, newargs):
879879
else:
880880
default_setting('INCOMING_MODULE_JS_API', [])
881881

882-
if 'GLOBAL_BASE' not in user_settings and not settings.SHRINK_LEVEL and not settings.OPT_LEVEL:
883-
# When optimizing for size it helps to put static data first before
884-
# the stack (since this makes instructions for accessing this data
885-
# use a smaller LEB encoding).
886-
# However, for debugability is better to have the stack come first
887-
# (because stack overflows will trap rather than corrupting data).
888-
settings.STACK_FIRST = True
889-
890882
# Default to TEXTDECODER=2 (always use TextDecoder to decode UTF-8 strings)
891883
# in -Oz builds, since custom decoder for UTF-8 takes up space.
892884
# In pthreads enabled builds, TEXTDECODER==2 may not work, see
@@ -1592,7 +1584,6 @@ def check_memory_setting(setting):
15921584
# We start our global data after the shadow memory.
15931585
# We don't need to worry about alignment here. wasm-ld will take care of that.
15941586
settings.GLOBAL_BASE = shadow_size
1595-
settings.STACK_FIRST = False
15961587

15971588
if not settings.ALLOW_MEMORY_GROWTH:
15981589
settings.INITIAL_MEMORY = total_mem
@@ -1615,6 +1606,21 @@ def check_memory_setting(setting):
16151606
if sanitize and settings.GENERATE_SOURCE_MAP:
16161607
settings.LOAD_SOURCE_MAP = 1
16171608

1609+
if 'GLOBAL_BASE' not in user_settings and not settings.SHRINK_LEVEL and not settings.OPT_LEVEL and not settings.USE_ASAN:
1610+
# When optimizing for size it helps to put static data first before
1611+
# the stack (since this makes instructions for accessing this data
1612+
# use a smaller LEB encoding).
1613+
# However, for debugability is better to have the stack come first
1614+
# (because stack overflows will trap rather than corrupting data).
1615+
settings.STACK_FIRST = True
1616+
1617+
if '--stack-first' in [x for _, x in state.link_flags]:
1618+
settings.STACK_FIRST = True
1619+
if settings.USE_ASAN:
1620+
exit_with_error('--stack-first is not compatible with asan')
1621+
if 'GLOBAL_BASE' in user_settings:
1622+
exit_with_error('--stack-first is not compatible with -sGLOBAL_BASE')
1623+
16181624
set_max_memory()
16191625

16201626
# check if we can address the 2GB mark and higher.

0 commit comments

Comments
 (0)