Skip to content

Commit 4fe29d7

Browse files
committed
Add better error message when mixing exceptions modes. NFC
Depends on llvm/llvm-project#128564 to allow wasm-ld to report the object file in question. Fixes: emscripten-core#23732
1 parent cbcbba9 commit 4fe29d7

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/lib/libexceptions.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ var LibraryExceptions = {
257257
},
258258

259259
#endif
260+
260261
#if WASM_EXCEPTIONS || !DISABLE_EXCEPTION_CATCHING
261262
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$stackSave', '$stackRestore', '$stackAlloc'],
262263
$getExceptionMessageCommon: (ptr) => {
@@ -277,17 +278,23 @@ var LibraryExceptions = {
277278
return [type, message];
278279
},
279280
#endif
281+
280282
#if WASM_EXCEPTIONS
283+
#if ASSERTIONS
284+
// Try to give a more useful error message than simply `Undefined reference to `emscripten_longjmp`
285+
emscripten_longjmp__deps: [() => error('undefined reference to `emscripten_longjmp`. One or more object files was not compiled with `-fwasm-exceptions`. Build with `-sASSERTIONS=0` to have wasm-ld report which one.')],
286+
emscripten_longjmp: () => {},
287+
#endif
288+
281289
$getCppExceptionTag: () =>
282290
// In static linking, tags are defined within the wasm module and are
283291
// exported, whereas in dynamic linking, tags are defined in library.js in
284292
// JS code and wasm modules import them.
285293
#if RELOCATABLE
286-
___cpp_exception // defined in library.js
294+
___cpp_exception, // defined in library.js
287295
#else
288-
wasmExports['__cpp_exception']
296+
wasmExports['__cpp_exception'],
289297
#endif
290-
,
291298

292299
#if EXCEPTION_STACK_TRACES
293300
// Throw a WebAssembly.Exception object with the C++ tag with a stack trace

src/lib/libexceptions_stub.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var LibraryExceptions = {};
2323
LibraryExceptions[name] = function() { abort(); };
2424
#if !INCLUDE_FULL_LIBRARY
2525
// This method of link-time error generation is not compatible with INCLUDE_FULL_LIBRARY
26-
LibraryExceptions[name + '__deps'] = [function() {
26+
LibraryExceptions[name + '__deps'] = [() => {
2727
error(`DISABLE_EXCEPTION_THROWING was set (likely due to -fno-exceptions), which means no C++ exception throwing support code is linked in, but such support is required by symbol '${name}'. Either do not set DISABLE_EXCEPTION_THROWING (if you do want exception throwing) or compile all source files with -fno-except (so that no exceptions support code is required); also make sure DISABLE_EXCEPTION_CATCHING is set to the right value - if you want exceptions, it should be off, and vice versa.`);
2828
}];
2929
#endif

test/test_other.py

+16
Original file line numberDiff line numberDiff line change
@@ -9438,6 +9438,22 @@ def test_exceptions_c_linker(self):
94389438
stderr = self.expect_fail([EMCC, '-sSTRICT', test_file('other/test_exceptions_c_linker.c')])
94399439
self.assertContained('error: undefined symbol: __cxa_find_matching_catch_1', stderr)
94409440

9441+
def test_exceptions_mismatch(self):
9442+
create_file('main.c', '''
9443+
#include <setjmp.h>
9444+
int main() {
9445+
jmp_buf buf;
9446+
longjmp(buf, 8);
9447+
}
9448+
''')
9449+
self.emcc('main.c', ['-c'])
9450+
err = self.expect_fail([EMCC, 'main.o', '-fwasm-exceptions'])
9451+
self.assertContained('error: undefined reference to `emscripten_longjmp`. One or more object files was not compiled with `-fwasm-exceptions`. Build with `-sASSERTIONS=0` to have wasm-ld report which one.', err)
9452+
9453+
err = self.expect_fail([EMCC, 'main.o', '-fwasm-exceptions', '-sASSERTIONS=0'])
9454+
self.assertContained('wasm-ld: error: main.o: undefined symbol: emscripten_longjmp', err)
9455+
9456+
94419457
@with_all_eh_sjlj
94429458
def test_exceptions_stack_trace_and_message(self):
94439459
src = r'''

0 commit comments

Comments
 (0)