Skip to content

Commit

Permalink
[clang][WebAssembly] Link crt1 even in case of -shared
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yamt authored and sbc100 committed Aug 14, 2023
1 parent aacaf3d commit 989ce06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
38 changes: 25 additions & 13 deletions clang/lib/Driver/ToolChains/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,43 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.AddAllArgs(CmdArgs, options::OPT_u);
ToolChain.AddFilePathLibArgs(Args, CmdArgs);

const char *Crt1 = "crt1.o";
bool IsCommand = true;
const char *Crt1;
const char *Entry = nullptr;

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

if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
StringRef CM = A->getValue();
if (CM == "command") {
// Use default values.
IsCommand = true;
} else if (CM == "reactor") {
Crt1 = "crt1-reactor.o";
Entry = "_initialize";
IsCommand = false;
} else {
ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
<< CM << A->getOption().getName();
}
}
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, options::OPT_shared))

if (IsCommand) {
// If crt1-command.o exists, it supports new-style commands, so use it.
// Otherwise, use the old crt1.o. This is a temporary transition measure.
// Once WASI libc no longer needs to support LLVM versions which lack
// support for new-style command, it can make crt1.o the same as
// crt1-command.o. And once LLVM no longer needs to support WASI libc
// versions before that, it can switch to using crt1-command.o.
Crt1 = "crt1.o";
if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
Crt1 = "crt1-command.o";
} else {
Crt1 = "crt1-reactor.o";
Entry = "_initialize";
}

if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
if (Entry) {
CmdArgs.push_back(Args.MakeArgString("--entry"));
Expand Down
12 changes: 6 additions & 6 deletions clang/test/Driver/wasm-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
// 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 with a known OS.
// -shared should be passed through to `wasm-ld` and include crt1-reactor.o with a known OS.

// RUN: %clang -### -shared --target=wasm32-wasi --sysroot=/foo %s 2>&1 \
// RUN: %clang -### -shared -mexec-model=reactor --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"
// 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"

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

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

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

Expand Down

0 comments on commit 989ce06

Please sign in to comment.