Skip to content

Commit bb66d8f

Browse files
yamttru
authored andcommitted
[clang][WebAssembly] Link crt1 even in case of -shared
This allows -mexec-model=reactor -shared produces a library module with _initialize entrypoint, which is preferrable over __wasm_call_ctors. This partially reverts https://reviews.llvm.org/D153293 Discussion: dicej/component-linking-demo#3 Reviewed By: sbc100 Differential Revision: https://reviews.llvm.org/D156205 (cherry picked from commit 989ce06)
1 parent 98bfdac commit bb66d8f

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

clang/lib/Driver/ToolChains/WebAssembly.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,43 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
7777
Args.AddAllArgs(CmdArgs, options::OPT_u);
7878
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
7979

80-
const char *Crt1 = "crt1.o";
80+
bool IsCommand = true;
81+
const char *Crt1;
8182
const char *Entry = nullptr;
8283

83-
// If crt1-command.o exists, it supports new-style commands, so use it.
84-
// Otherwise, use the old crt1.o. This is a temporary transition measure.
85-
// Once WASI libc no longer needs to support LLVM versions which lack
86-
// support for new-style command, it can make crt1.o the same as
87-
// crt1-command.o. And once LLVM no longer needs to support WASI libc
88-
// versions before that, it can switch to using crt1-command.o.
89-
if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
90-
Crt1 = "crt1-command.o";
84+
// When -shared is specified, use the reactor exec model unless
85+
// specified otherwise.
86+
if (Args.hasArg(options::OPT_shared))
87+
IsCommand = false;
9188

9289
if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
9390
StringRef CM = A->getValue();
9491
if (CM == "command") {
95-
// Use default values.
92+
IsCommand = true;
9693
} else if (CM == "reactor") {
97-
Crt1 = "crt1-reactor.o";
98-
Entry = "_initialize";
94+
IsCommand = false;
9995
} else {
10096
ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
10197
<< CM << A->getOption().getName();
10298
}
10399
}
104-
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, options::OPT_shared))
100+
101+
if (IsCommand) {
102+
// If crt1-command.o exists, it supports new-style commands, so use it.
103+
// Otherwise, use the old crt1.o. This is a temporary transition measure.
104+
// Once WASI libc no longer needs to support LLVM versions which lack
105+
// support for new-style command, it can make crt1.o the same as
106+
// crt1-command.o. And once LLVM no longer needs to support WASI libc
107+
// versions before that, it can switch to using crt1-command.o.
108+
Crt1 = "crt1.o";
109+
if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
110+
Crt1 = "crt1-command.o";
111+
} else {
112+
Crt1 = "crt1-reactor.o";
113+
Entry = "_initialize";
114+
}
115+
116+
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
105117
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
106118
if (Entry) {
107119
CmdArgs.push_back(Args.MakeArgString("--entry"));

clang/test/Driver/wasm-toolchain.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@
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.
36+
// -shared should be passed through to `wasm-ld` and include crt1-reactor.o with a known OS.
3737

38-
// RUN: %clang -### -shared --target=wasm32-wasi --sysroot=/foo %s 2>&1 \
38+
// RUN: %clang -### -shared -mexec-model=reactor --target=wasm32-wasi --sysroot=/foo %s 2>&1 \
3939
// RUN: | FileCheck -check-prefix=LINK_KNOWN_SHARED %s
4040
// 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"
41+
// LINK_KNOWN_SHARED: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi" "crt1-reactor.o" "--entry" "_initialize" "-shared" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
4242

43-
// -shared should be passed through to `wasm-ld` and not include crt1.o with an unknown OS.
43+
// -shared should be passed through to `wasm-ld` and include crt1-reactor.o with an unknown OS.
4444

45-
// RUN: %clang -### -shared --target=wasm32-unknown-unknown --sysroot=/foo %s 2>&1 \
45+
// RUN: %clang -### -shared -mexec-model=reactor --target=wasm32-unknown-unknown --sysroot=/foo %s 2>&1 \
4646
// RUN: | FileCheck -check-prefix=LINK_UNKNOWN_SHARED %s
4747
// LINK_UNKNOWN_SHARED: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
48-
// LINK_UNKNOWN_SHARED: wasm-ld{{.*}}" "-shared" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
48+
// LINK_UNKNOWN_SHARED: wasm-ld{{.*}}" "crt1-reactor.o" "--entry" "_initialize" "-shared" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
4949

5050
// A basic C link command-line with optimization with known OS.
5151

0 commit comments

Comments
 (0)