Skip to content

Commit cac54a8

Browse files
authored
[WebAssembly] Require tags for Wasm EH and Wasm SJLJ to be defined externally (llvm#159143)
Rather then defining these tags in each object file that requires them we can can declare them as undefined and require that they defined externally in, for example, compiler-rt or libcxxabi.
1 parent e151e84 commit cac54a8

File tree

7 files changed

+21
-40
lines changed

7 files changed

+21
-40
lines changed

lld/test/wasm/Inputs/tags.s

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.globl __cpp_exception
2+
.tagtype __cpp_exception i32
3+
__cpp_exception:
4+
5+
.globl __c_longjmp
6+
.tagtype __c_longjmp i32
7+
__c_longjmp:

lld/test/wasm/tag-section.ll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
; Static code
1+
; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t_tags.o %p/Inputs/tags.s
2+
3+
; Static code, with tags defined in tags.s
24
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling %p/Inputs/tag-section1.ll -o %t1.o
35
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling %p/Inputs/tag-section2.ll -o %t2.o
46
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling %s -o %t.o
5-
; RUN: wasm-ld -o %t.wasm %t.o %t1.o %t2.o
6-
; RUN: wasm-ld --export-all -o %t-export-all.wasm %t.o %t1.o %t2.o
7+
; RUN: wasm-ld -o %t.wasm %t.o %t1.o %t2.o %t_tags.o
8+
; RUN: wasm-ld --export-all -o %t-export-all.wasm %t.o %t1.o %t2.o %t_tags.o
79
; RUN: obj2yaml %t.wasm | FileCheck %s --check-prefix=NOPIC
810
; RUN: obj2yaml %t-export-all.wasm | FileCheck %s --check-prefix=NOPIC-EXPORT-ALL
911

10-
; PIC code
12+
; PIC code with tags imported
1113
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic %p/Inputs/tag-section1.ll -o %t1.o
1214
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic %p/Inputs/tag-section2.ll -o %t2.o
1315
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic %s -o %t.o
14-
; RUN: wasm-ld --import-undefined --experimental-pic --unresolved-symbols=import-dynamic -pie -o %t.wasm %t.o %t1.o %t2.o
15-
; RUN: obj2yaml %t.wasm | FileCheck %s --check-prefix=PIC
16+
; RUN: wasm-ld --import-undefined --experimental-pic --unresolved-symbols=import-dynamic -pie -o %t_pic.wasm %t.o %t1.o %t2.o
17+
; RUN: obj2yaml %t_pic.wasm | FileCheck %s --check-prefix=PIC
1618

1719
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
1820
target triple = "wasm32-unknown-emscripten"
@@ -49,7 +51,7 @@ define void @_start() {
4951
; NOPIC-EXPORT-ALL: Kind: TAG
5052
; NOPIC-EXPORT-ALL: Index: 0
5153

52-
; In PIC mode, tags are undefined and imported from JS.
54+
; In PIC mode, we leave the tags as undefined and they should be imported
5355
; PIC: Sections:
5456
; PIC: - Type: TYPE
5557
; PIC-NEXT: Signatures:

llvm/lib/CodeGen/AsmPrinter/WasmException.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@
1919
#include "llvm/MC/MCStreamer.h"
2020
using namespace llvm;
2121

22-
void WasmException::endModule() {
23-
// These are symbols used to throw/catch C++ exceptions and C longjmps. These
24-
// symbols have to be emitted somewhere once in the module. Check if each of
25-
// the symbols has already been created, i.e., we have at least one 'throw' or
26-
// 'catch' instruction with the symbol in the module, and emit the symbol only
27-
// if so.
28-
//
29-
// But in dynamic linking, it is in general not possible to come up with a
30-
// module instantiating order in which tag-defining modules are loaded before
31-
// the importing modules. So we make them undefined symbols here, define tags
32-
// in the JS side, and feed them to each importing module.
33-
if (!Asm->isPositionIndependent()) {
34-
for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
35-
SmallString<60> NameStr;
36-
Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
37-
if (Asm->OutContext.lookupSymbol(NameStr)) {
38-
MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
39-
Asm->OutStreamer->emitLabel(ExceptionSym);
40-
}
41-
}
42-
}
43-
}
44-
4522
void WasmException::endFunction(const MachineFunction *MF) {
4623
bool ShouldEmitExceptionTable = false;
4724
for (const LandingPadInfo &Info : MF->getLandingPads()) {

llvm/lib/CodeGen/AsmPrinter/WasmException.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LLVM_LIBRARY_VISIBILITY WasmException : public EHStreamer {
2626
public:
2727
WasmException(AsmPrinter *A) : EHStreamer(A) {}
2828

29-
void endModule() override;
29+
void endModule() override {}
3030
void beginFunction(const MachineFunction *MF) override {}
3131
void endFunction(const MachineFunction *MF) override;
3232

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,6 @@ MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
249249
SmallVector<wasm::ValType, 4> Params;
250250
if (Name == "__cpp_exception" || Name == "__c_longjmp") {
251251
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
252-
// In static linking we define tag symbols in WasmException::endModule().
253-
// But we may have multiple objects to be linked together, each of which
254-
// defines the tag symbols. To resolve them, we declare them as weak. In
255-
// dynamic linking we make tag symbols undefined in the backend, define it
256-
// in JS, and feed them to each importing module.
257-
if (!isPositionIndependent())
258-
WasmSym->setWeak(true);
259252
WasmSym->setExternal(true);
260253

261254
// Currently both C++ exceptions and C longjmps have a single pointer type

llvm/test/CodeGen/WebAssembly/exception-legacy.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,4 +573,5 @@ attributes #0 = { nounwind }
573573
attributes #1 = { noreturn }
574574
attributes #2 = { noreturn nounwind }
575575

576-
; CHECK: __cpp_exception:
576+
;; The exception tag should not be defined locally
577+
; CHECK-NOT: __cpp_exception:

llvm/test/CodeGen/WebAssembly/exception.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,5 @@ attributes #0 = { nounwind }
670670
attributes #1 = { noreturn }
671671
attributes #2 = { noreturn nounwind }
672672

673-
; CHECK: __cpp_exception:
673+
;; The exception tag should not be defined locally
674+
; CHECK-NOT: __cpp_exception:

0 commit comments

Comments
 (0)