Skip to content

Commit a7d11c4

Browse files
dicejcuviper
authored andcommitted
[clang][WebAssembly] Support wasm32-wasi shared libraries
This adds support for Emscripten-style shared libraries [1] to non-emscripten targets, such as `wasm32-wasi`. 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 <[email protected]> Reviewed By: sbc100 Differential Revision: https://reviews.llvm.org/D153293 Signed-off-by: Joel Dice <[email protected]>
1 parent 06248fa commit a7d11c4

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

clang/docs/ReleaseNotes.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,12 @@ WebAssembly Support
11031103
^^^^^^^^^^^^^^^^^^^
11041104
- The -mcpu=generic configuration now enables sign-ext and mutable-globals. These
11051105
proposals are standardized and available in all major engines.
1106+
- Shared library support (and PIC code generation) for WebAssembly is no longer
1107+
limited to the Emscripten target OS and now works with other targets such as
1108+
wasm32-wasi. Note that the `format
1109+
<https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md>`_
1110+
is not yet stable and may change between LLVM versions. Also, WASI does not
1111+
yet have facilities to load dynamic libraries.
11061112

11071113
DWARF Support in Clang
11081114
----------------------

clang/lib/Driver/ToolChains/WebAssembly.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
101101
<< CM << A->getOption().getName();
102102
}
103103
}
104-
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
104+
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, options::OPT_shared))
105105
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
106106
if (Entry) {
107107
CmdArgs.push_back(Args.MakeArgString("--entry"));
108108
CmdArgs.push_back(Args.MakeArgString(Entry));
109109
}
110110

111+
if (Args.hasArg(options::OPT_shared))
112+
CmdArgs.push_back(Args.MakeArgString("-shared"));
113+
111114
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
112115

113116
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {

clang/test/Driver/wasm-toolchain.c

+26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@
3333
// LINK_KNOWN: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
3434
// LINK_KNOWN: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
3535

36+
// -shared should be passed through to `wasm-ld` and not include crt1.o with a known OS.
37+
38+
// RUN: %clang -### -shared --target=wasm32-wasi --sysroot=/foo %s 2>&1 \
39+
// RUN: | FileCheck -check-prefix=LINK_KNOWN_SHARED %s
40+
// LINK_KNOWN_SHARED: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
41+
// LINK_KNOWN_SHARED: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi" "-shared" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
42+
43+
// -shared should be passed through to `wasm-ld` and not include crt1.o with an unknown OS.
44+
45+
// RUN: %clang -### -shared --target=wasm32-unknown-unknown --sysroot=/foo %s 2>&1 \
46+
// RUN: | FileCheck -check-prefix=LINK_UNKNOWN_SHARED %s
47+
// LINK_UNKNOWN_SHARED: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
48+
// LINK_UNKNOWN_SHARED: wasm-ld{{.*}}" "-shared" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
49+
3650
// A basic C link command-line with optimization with known OS.
3751

3852
// RUN: %clang -### -O2 --target=wasm32-wasi --sysroot=/foo %s 2>&1 \
@@ -46,6 +60,18 @@
4660
// RUN: | FileCheck -check-prefix=COMPILE %s
4761
// COMPILE: "-cc1" {{.*}} "-internal-isystem" "/foo/include/wasm32-wasi" "-internal-isystem" "/foo/include"
4862

63+
// -fPIC should work on a known OS
64+
65+
// RUN: %clang -### -fPIC --target=wasm32-wasi --sysroot=/foo %s 2>&1 \
66+
// RUN: | FileCheck -check-prefix=COMPILE_KNOWN_PIC %s
67+
// COMPILE_KNOWN_PIC: "-cc1" {{.*}} "-mrelocation-model" "pic" "-pic-level" "2" {{.*}} "-internal-isystem" "/foo/include/wasm32-wasi" "-internal-isystem" "/foo/include"
68+
69+
// -fPIC should work on an unknown OS
70+
71+
// RUN: %clang -### -fPIC --target=wasm32-unknown-unknown --sysroot=/foo %s 2>&1 \
72+
// RUN: | FileCheck -check-prefix=COMPILE_UNKNOWN_PIC %s
73+
// COMPILE_UNKNOWN_PIC: "-cc1" {{.*}} "-mrelocation-model" "pic" "-pic-level" "2"
74+
4975
// Thread-related command line tests.
5076

5177
// '-pthread' sets +atomics, +bulk-memory, +mutable-globals, +sign-ext, and --shared-memory

llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM,
9898
return Reloc::Static;
9999
}
100100

101-
if (!TT.isOSEmscripten()) {
102-
// Relocation modes other than static are currently implemented in a way
103-
// that only works for Emscripten, so disable them if we aren't targeting
104-
// Emscripten.
105-
return Reloc::Static;
106-
}
107-
108101
return *RM;
109102
}
110103

0 commit comments

Comments
 (0)