From 4f84e641176d6ac693e9ea0d6a7f9b9383d9ae20 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 3 May 2023 14:51:44 -0700 Subject: [PATCH] Revert #131, renaming `main` back to `run`. (#165) Changing LLVM and/or Rust to avoid special handling of `main` is a fair amount of work, and there could be other toolchains with similar special rules for functions named `main`, so rename the command entrypoint back to `run`. We could potentially re-evaluate this in the future, such as in a preview3 timeframe, but for now, let's go with the simplest thing that works. --- build.rs | 6 ------ src/lib.rs | 17 ++++++++++++++--- src/main.s | 27 --------------------------- 3 files changed, 14 insertions(+), 36 deletions(-) delete mode 100644 src/main.s diff --git a/build.rs b/build.rs index f3514d64c4..fcd7b7c6da 100644 --- a/build.rs +++ b/build.rs @@ -14,12 +14,6 @@ fn main() { out_dir.to_str().unwrap() ); - // LLVM has special handling for `main` so use a .s file to define the main - // function for the adapter. See the comments in src/main.s for details. - if env::var("CARGO_FEATURE_COMMAND").is_ok() { - println!("cargo:rustc-link-arg=src/main.o"); - } - // Some specific flags to `wasm-ld` to inform the shape of this adapter. // Notably we're importing memory from the main module and additionally our // own module has no stack at all since it's specifically allocated at diff --git a/src/lib.rs b/src/lib.rs index 1696d29e0b..1198dd3d8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ pub mod bindings { raw_strings, // The generated definition of command will pull in std, so we are defining it // manually below instead - skip: ["main", "get-directories", "get-environment"], + skip: ["run", "get-directories", "get-environment"], }); #[cfg(feature = "reactor")] @@ -43,6 +43,17 @@ pub mod bindings { }); } +#[no_mangle] +#[cfg(feature = "command")] +pub unsafe extern "C" fn run() -> u32 { + #[link(wasm_import_module = "__main_module__")] + extern "C" { + fn _start(); + } + _start(); + 0 +} + // The unwrap/expect methods in std pull panic when they fail, which pulls // in unwinding machinery that we can't use in the adapter. Instead, use this // extension trait to get postfixed upwrap on Option and Result. @@ -197,7 +208,7 @@ impl ImportAlloc { } } -/// This allocator is only used for the `main` entrypoint. +/// This allocator is only used for the `run` entrypoint. /// /// The implementation here is a bump allocator into `State::long_lived_arena` which /// traps when it runs out of data. This means that the total size of @@ -2092,7 +2103,7 @@ struct State { /// Long-lived bump allocated memory arena. /// /// This is used for the cabi_export_realloc to allocate data passed to the - /// `main` entrypoint. Allocations in this arena are safe to use for + /// `run` entrypoint. Allocations in this arena are safe to use for /// the lifetime of the State struct. It may also be used for import allocations /// which need to be long-lived, by using `import_alloc.with_arena`. long_lived_arena: BumpArena, diff --git a/src/main.s b/src/main.s deleted file mode 100644 index 4a252e3f89..0000000000 --- a/src/main.s +++ /dev/null @@ -1,27 +0,0 @@ -# The component model CLI world exports its entrypoint under the name -# "main". However, LLVM has special handling for functions named `main` -# in order to handle the `main(void)` vs `main(int argc, char **argv)` -# difference on Wasm where the caller needs to know the exact signature. -# To avoid this, define a function with a different name and export it -# as `main`. -# -# To generate the `main.o` file from this `main.s` file, compile with -# `clang --target=wasm32-wasi -c main.s` - - .text - .functype main () -> (i32) - .export_name main, main - .functype _start () -> () - .import_name _start, _start - .import_module _start, __main_module__ - .section .text.main,"",@ - .hidden main - .globl main - .type main,@function -main: - .functype main () -> (i32) - call _start - i32.const 0 - return - end_function - .no_dead_strip main