dicej created this revision. Herald added subscribers: pmatos, asb, delcypher, sunfish, hiraditya, jgravelle-google, sbc100, dschuff. Herald added a project: All. dicej requested review of this revision. Herald added subscribers: llvm-commits, cfe-commits, MaskRay, aheejin. Herald added projects: clang, LLVM.
This adds support for Emscripten-style shared libraries [1] to the `wasm32-wasi` target. Previously, only static linking was supported, and the `-shared` and `-fPIC` flags were simply ignored. Now both flags are honored. Since WASI runtimes do not necessarily include JavaScript support, we cannot rely on the JS-based Emscripten linker to link shared libraries. Instead, we link them using the Component Model proposal [2]. We have prototyped shared library support in `wasi-sdk` [3] and put together a demo [4] which uses a patched version of `wit-component` [5] to link libraries using the Component Model. We plan to submit the required changes upstream to the respective repositories in the next week or two. [1] https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md [2] https://github.com/WebAssembly/component-model/blob/main/design/mvp/examples/SharedEverythingDynamicLinking.md [3] https://github.com/dicej/wasi-sdk/tree/dynamic-linking [4] https://github.com/dicej/component-linking-demo [5] https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-component Signed-off-by: Joel Dice <joel.d...@fermyon.com> Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D153293 Files: clang/docs/ReleaseNotes.rst clang/lib/Driver/ToolChains/WebAssembly.cpp clang/test/Driver/wasm-toolchain.c llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp llvm/utils/lit/lit/llvm/config.py
Index: llvm/utils/lit/lit/llvm/config.py =================================================================== --- llvm/utils/lit/lit/llvm/config.py +++ llvm/utils/lit/lit/llvm/config.py @@ -328,6 +328,9 @@ return False def make_itanium_abi_triple(self, triple): + if triple == "wasm32-wasi": + return triple + m = re.match(r"(\w+)-(\w+)-(\w+)", triple) if not m: self.lit_config.fatal( @@ -339,6 +342,9 @@ return m.group(1) + "-" + m.group(2) + "-" + m.group(3) + "-gnu" def make_msabi_triple(self, triple): + if triple == "wasm32-wasi": + return triple + m = re.match(r"(\w+)-(\w+)-(\w+)", triple) if not m: self.lit_config.fatal("Could not turn '%s' into MS ABI triple" % triple) Index: llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp +++ llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp @@ -99,13 +99,6 @@ return Reloc::Static; } - if (!TT.isOSEmscripten()) { - // Relocation modes other than static are currently implemented in a way - // that only works for Emscripten, so disable them if we aren't targeting - // Emscripten. - return Reloc::Static; - } - return *RM; } Index: clang/test/Driver/wasm-toolchain.c =================================================================== --- clang/test/Driver/wasm-toolchain.c +++ clang/test/Driver/wasm-toolchain.c @@ -33,6 +33,13 @@ // LINK_KNOWN: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]" // LINK_KNOWN: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out" +// -shared should be passed through to `wasm-ld` and not include crt1.o + +// RUN: %clang -### -shared --target=wasm32-wasi --sysroot=/foo %s 2>&1 \ +// RUN: | FileCheck -check-prefix=LINK_KNOWN_SHARED %s +// LINK_KNOWN_SHARED: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]" +// LINK_KNOWN_SHARED: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi" "-shared" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out" + // A basic C link command-line with optimization with known OS. // RUN: %clang -### -O2 --target=wasm32-wasi --sysroot=/foo %s 2>&1 \ @@ -46,6 +53,12 @@ // RUN: | FileCheck -check-prefix=COMPILE %s // COMPILE: "-cc1" {{.*}} "-internal-isystem" "/foo/include/wasm32-wasi" "-internal-isystem" "/foo/include" +// -fPIC should work for WASI + +// RUN: %clang -### -fPIC --target=wasm32-wasi --sysroot=/foo %s 2>&1 \ +// RUN: | FileCheck -check-prefix=COMPILE_PIC %s +// COMPILE_PIC: "-cc1" {{.*}} "-mrelocation-model" "pic" "-pic-level" "2" {{.*}} "-internal-isystem" "/foo/include/wasm32-wasi" "-internal-isystem" "/foo/include" + // Thread-related command line tests. // '-pthread' sets +atomics, +bulk-memory, +mutable-globals, +sign-ext, and --shared-memory Index: clang/lib/Driver/ToolChains/WebAssembly.cpp =================================================================== --- clang/lib/Driver/ToolChains/WebAssembly.cpp +++ clang/lib/Driver/ToolChains/WebAssembly.cpp @@ -101,13 +101,16 @@ << CM << A->getOption().getName(); } } - if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) + if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, options::OPT_shared)) CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1))); if (Entry) { CmdArgs.push_back(Args.MakeArgString("--entry")); CmdArgs.push_back(Args.MakeArgString(Entry)); } + if (Args.hasArg(options::OPT_shared)) + CmdArgs.push_back(Args.MakeArgString("-shared")); + AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -701,6 +701,8 @@ WebAssembly Support ^^^^^^^^^^^^^^^^^^^ +- The `wasm32-wasi` target now supports `Emscripten-style shared libraries + <https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md>`_. AVR Support ^^^^^^^^^^^
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits