This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Make EH work with dynamic linking
ClosedPublic

Authored by aheejin on Oct 8 2021, 1:08 AM.

Details

Summary

This makes Wasm EH work with dynamic linking. So far we were only able
to handle destructors, which do not use any tags or LSDA info.

  1. This uses TargetExternalSymbol for GCC_except_tableN symbols, which points to the address of per-function LSDA info. It is more convenient to use than MCSymbol because it can take additional target flags.
  1. When lowering wasm_lsda intrinsic, if PIC is enabled, make the symbol relative to __memory_base and generate the add node. If PIC is disabled, continue to use the absolute address.
  1. Make tag symbols (__cpp_exception and __c_longjmp) undefined in the backend, because it is hard to make it work with dynamic linking's loading order. Instead, we make all tag symbols undefined in the LLVM backend and import it from JS.
  1. Add support for undefined tags to the linker.

Companion patches:

Diff Detail

Event Timeline

aheejin created this revision.Oct 8 2021, 1:08 AM
aheejin requested review of this revision.Oct 8 2021, 1:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 8 2021, 1:08 AM
aheejin added inline comments.Oct 8 2021, 1:12 AM
llvm/lib/Object/WasmObjectFile.cpp
1127–1129

This was deleted by mistake in D111086. It was fine with that patch because until now we don't have any case of tag imports.

llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
281–284

Now we are using LSDA symbol with target flags, for this I removed this comment and assert.

287–293

I tried to remove this MO_MCSymbol case altogether, but it turned out this case is used by __funcref_call_table and __indirect_call_table. So I only deleted the (already incorrect) comment.

aheejin edited the summary of this revision. (Show Details)Oct 8 2021, 1:15 AM
aheejin added inline comments.
lld/test/wasm/tag-section.ll
14

I'm not very sure what each of --experimental-pic and -pie does... Without --experimental-pic this doesn't work and without -pie it emits a warning, so I ended up putting both.

aheejin edited the summary of this revision. (Show Details)Oct 8 2021, 1:19 AM
sbc100 added inline comments.Oct 8 2021, 7:21 AM
lld/test/wasm/tag-section.ll
14

Yes, you need them both today. -pie is experimental so you need to opt into this feature. -pie outputs a position independent executable, which is what emscripten used for MAIN_MODULE today.

52

Maybe add ; PIC-NOT: - Type: TAG ?

lld/wasm/InputFiles.cpp
670

Maybe remove of rephrase this comment.. It conceivable that other folks could build other object (either via .s or some other mechanism that could contain undefined tags). Or remove the limitation?

I'm curious why the compiler wasn't complaining before if this enum value was missing from this swtich?

lld/wasm/Symbols.h
70

Maybe follow the pattern of one comparison per line here?

llvm/test/CodeGen/WebAssembly/eh-lsda.ll
4

It seems like a shame to use coverage of eh-lsda for the non-emscripten targets? cna you remove the target tripple completely here and instead set it from the command line. Then you can use emscripten for the PIC test and unknown for the non-PIC test?

aheejin updated this revision to Diff 378759.Oct 11 2021, 12:15 PM
aheejin marked 3 inline comments as done.

Address comments + improve test

lld/wasm/InputFiles.cpp
670

Maybe remove of rephrase this comment.. It conceivable that other folks could build other object (either via .s or some other mechanism that could contain undefined tags). Or remove the limitation?

Removed the limitation.

I'm curious why the compiler wasn't complaining before if this enum value was missing from this swtich?

I think it's because WasmSymbolInfo.Kind's type is not enum WasmSymbolType but uint32_t: https://github.com/llvm/llvm-project/blob/63aab4065b452ae5693481dfd12fed3c184261c7/llvm/include/llvm/BinaryFormat/Wasm.h#L192

lld/wasm/Symbols.h
70

This is what clang-format insists. Currently our wasm lld code conforms to clang-format nearly 99%, so I'm not sure if this is worth making an exception..?

llvm/test/CodeGen/WebAssembly/eh-lsda.ll
4

For EH/LSDA we don't do anything differently for emscripten and non-emscripten targets, but yeah, will change to a command line argument, and also will add wasm64 tests. So this will test 6 combinations:

  1. wasm32-unknown-unknown
  2. wasm64-unknown-unknown
  3. wasm32-unknown-emscripten
  4. wasm64-unknown-emscripten
  5. wasm32-unknown-emscripten + PIC
  6. wasm64-unknown-emscripten + PIC
sbc100 accepted this revision.Oct 12 2021, 3:18 PM
sbc100 added inline comments.
lld/wasm/Symbols.h
70

Clang-format is the boss I guess.

This revision is now accepted and ready to land.Oct 12 2021, 3:18 PM
This revision was automatically updated to reflect the committed changes.