Skip to content

Commit 85036b7

Browse files
authored
Define wasm-eh tags in native code. NFC (#25284)
These tags have also historically be weakly defined in each object file by llvm (at least for non-PIC object files) but I hope to remove those definitions going forward and instead rely on the toolchain/compiler-rt to define them. This will remove one of the distinctions between PIC and non-PIC generated code. This change can land here on the emscripten side before the corresponding llvm changes since the strong definition I'm adding here will always take precedence over any weak definitions in object files. See llvm/llvm-project#159143
1 parent 91e88b8 commit 85036b7

File tree

8 files changed

+57
-14
lines changed

8 files changed

+57
-14
lines changed

src/lib/libcore.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,13 +2239,6 @@ addToLibrary({
22392239
__stack_high: '{{{ STACK_HIGH }}}',
22402240
__stack_low: '{{{ STACK_LOW }}}',
22412241
__global_base: '{{{ GLOBAL_BASE }}}',
2242-
#if WASM_EXCEPTIONS
2243-
// In dynamic linking we define tags here and feed them to each module
2244-
__cpp_exception: "new WebAssembly.Tag({'parameters': ['{{{ POINTER_WASM_TYPE }}}']})",
2245-
#endif
2246-
#if SUPPORT_LONGJMP == 'wasm'
2247-
__c_longjmp: "new WebAssembly.Tag({'parameters': ['{{{ POINTER_WASM_TYPE }}}']})",
2248-
#endif
22492242
#if ASYNCIFY == 1
22502243
__asyncify_state: "new WebAssembly.Global({'value': 'i32', 'mutable': true}, 0)",
22512244
__asyncify_data: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ to64(0) }}})",

src/lib/libdylink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ var LibraryDylink = {
276276
continue;
277277
}
278278
#endif
279-
if (typeof value == 'object') {
279+
if (typeof value?.value != 'undefined') {
280280
// a breaking change in the wasm spec, globals are now objects
281281
// https://github.com/WebAssembly/mutable-global/issues/1
282282
value = value.value;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2025 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*
7+
* Define the `__c_longjmp` Wasm EH tag which is used to implment setjmp/longjmp
8+
* in LLVM.
9+
*/
10+
11+
#ifdef __wasm_exception_handling__
12+
13+
#ifdef __wasm64__
14+
#define PTR i64
15+
#else
16+
#define PTR i32
17+
#endif
18+
19+
.globl __c_longjmp
20+
.tagtype __c_longjmp PTR
21+
__c_longjmp:
22+
23+
#endif // !__wasm_exception_handling__
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2025 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*
7+
* Define the `__cpp_exception` Wasm EH tag which is used to implmenent C++
8+
* exception handling in LLVM.
9+
*/
10+
11+
#ifdef __wasm_exception_handling__
12+
13+
#ifdef __wasm64__
14+
#define PTR i64
15+
#else
16+
#define PTR i32
17+
#endif
18+
19+
.globl __cpp_exception
20+
.tagtype __cpp_exception PTR
21+
__cpp_exception:
22+
23+
#endif // !__wasm_exception_handling__

test/code_size/test_codesize_hello_dylink.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 26969,
3-
"a.out.js.gz": 11479,
2+
"a.out.js": 26979,
3+
"a.out.js.gz": 11483,
44
"a.out.nodebug.wasm": 18567,
55
"a.out.nodebug.wasm.gz": 9199,
6-
"total": 45536,
7-
"total_gz": 20678,
6+
"total": 45546,
7+
"total_gz": 20682,
88
"sent": [
99
"__heap_base",
1010
"__indirect_function_table",

test/code_size/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 246021,
2+
"a.out.js": 246031,
33
"a.out.nodebug.wasm": 597767,
4-
"total": 843788,
4+
"total": 843798,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/test_core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,9 +4951,11 @@ def test_dylink_exceptions_try_catch_2(self):
49514951
@no_js_math('JS_MATH is not compatible with MAIN_MODULE')
49524952
def test_dylink_exceptions_try_catch_6(self):
49534953
create_file('main.cpp', r'''
4954+
#include <assert.h>
49544955
#include <dlfcn.h>
49554956
int main() {
49564957
void* handle = dlopen("liblib.so", RTLD_LAZY);
4958+
assert(handle);
49574959
void (*side)(void) = (void (*)(void))dlsym(handle, "side");
49584960
(side)();
49594961
return 0;

tools/system_libs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ class libcompiler_rt(MTLibrary, SjLjLibrary):
987987
'emscripten_setjmp.c',
988988
'emscripten_exception_builtins.c',
989989
'emscripten_tempret.s',
990+
'__c_longjmp.S',
990991
'__trap.c',
991992
])
992993

@@ -1661,6 +1662,7 @@ def get_files(self):
16611662
'stdlib_typeinfo.cpp',
16621663
'private_typeinfo.cpp',
16631664
'cxa_exception_js_utils.cpp',
1665+
'__cpp_exception.S',
16641666
]
16651667
if self.eh_mode == Exceptions.NONE:
16661668
filenames += ['cxa_noexception.cpp']

0 commit comments

Comments
 (0)