Skip to content

Commit a4d7f67

Browse files
authored
Make main symbol optional even in STANDALONE_WASM mode (#9641)
See: #9640 Fixes: #9635
1 parent e84acc9 commit a4d7f67

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

system/lib/libc/crt1.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@
1616
#include <stdio.h>
1717

1818
extern void __wasm_call_ctors(void) __attribute__((weak));
19-
extern int main(int argc, char** argv);
19+
20+
// TODO(sbc): We shouldn't even link this file if there is no main:
21+
// https://github.com/emscripten-core/emscripten/issues/9640
22+
extern int main(int argc, char** argv) __attribute__((weak));
2023

2124
void _start(void) {
25+
if (!main) {
26+
if (__wasm_call_ctors) {
27+
__wasm_call_ctors();
28+
}
29+
return;
30+
}
31+
2232
/* Fill in the arguments from WASI syscalls. */
2333
size_t argc;
2434
char **argv;

tests/test_core.py

+12
Original file line numberDiff line numberDiff line change
@@ -8213,6 +8213,18 @@ def test_safe_stack_dylink(self):
82138213
}
82148214
''', ['abort(stack overflow)', '__handle_stack_overflow'], assert_returncode=None)
82158215

8216+
@also_with_standalone_wasm
8217+
def test_undefined_main(self):
8218+
# By default in emscripten we allow main to be undefined. Its used when
8219+
# building library code that has no main.
8220+
# TODO(sbc): Simplify the code by making this an opt-in feature.
8221+
# https://github.com/emscripten-core/emscripten/issues/9640
8222+
src = '''
8223+
#include <emscripten.h>
8224+
EMSCRIPTEN_KEEPALIVE void foo() {}
8225+
'''
8226+
self.build(src, self.get_dir(), 'test.c')
8227+
82168228

82178229
# Generate tests for everything
82188230
def make_run(name, emcc_args, settings=None, env=None):

0 commit comments

Comments
 (0)