From 9cd203e04bec254728cd8b6709f529991e06bba7 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 19 Aug 2024 19:16:23 +0900 Subject: [PATCH 1/3] Add crt2-command.o to provide __main_void cf. https://github.com/WebAssembly/wasi-libc/issues/485 cf. https://github.com/llvm/llvm-project/pull/104755 --- libc-bottom-half/crt/crt2-command.c | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 libc-bottom-half/crt/crt2-command.c diff --git a/libc-bottom-half/crt/crt2-command.c b/libc-bottom-half/crt/crt2-command.c new file mode 100644 index 000000000..cba22efa0 --- /dev/null +++ b/libc-bottom-half/crt/crt2-command.c @@ -0,0 +1,54 @@ +#include +#include +#include + +// The user's `main` function, expecting arguments. +int __main_argc_argv(int argc, char *argv[]); + +// If the user's `main` function expects arguments, the compiler will rename +// it to `__main_argc_argv`, and this version will get linked in, which +// initializes the argument data and calls `__main_argc_argv`. +__attribute__((__weak__, nodebug)) +int __main_void(void) { + __wasi_errno_t err; + + // Get the sizes of the arrays we'll have to create to copy in the args. + size_t argv_buf_size; + size_t argc; + err = __wasi_args_sizes_get(&argc, &argv_buf_size); + if (err != __WASI_ERRNO_SUCCESS) { + _Exit(EX_OSERR); + } + + // Add 1 for the NULL pointer to mark the end, and check for overflow. + size_t num_ptrs = argc + 1; + if (num_ptrs == 0) { + _Exit(EX_SOFTWARE); + } + + // Allocate memory for storing the argument chars. + char *argv_buf = malloc(argv_buf_size); + if (argv_buf == NULL) { + _Exit(EX_SOFTWARE); + } + + // Allocate memory for the array of pointers. This uses `calloc` both to + // handle overflow and to initialize the NULL pointer at the end. + char **argv = calloc(num_ptrs, sizeof(char *)); + if (argv == NULL) { + free(argv_buf); + _Exit(EX_SOFTWARE); + } + + // Fill the argument chars, and the argv array with pointers into those chars. + // TODO: Remove the casts on `argv_ptrs` and `argv_buf` once the witx is updated with char8 support. + err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf); + if (err != __WASI_ERRNO_SUCCESS) { + free(argv_buf); + free(argv); + _Exit(EX_OSERR); + } + + // Call `__main_argc_argv` with the arguments! + return __main_argc_argv(argc, argv); +} From bf737160fd2f472fd521fc3bc42457a1fddb52ee Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 19 Aug 2024 19:19:41 +0900 Subject: [PATCH 2/3] Add a comment --- libc-bottom-half/sources/__main_void.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libc-bottom-half/sources/__main_void.c b/libc-bottom-half/sources/__main_void.c index 9f46419bd..ef10108be 100644 --- a/libc-bottom-half/sources/__main_void.c +++ b/libc-bottom-half/sources/__main_void.c @@ -2,6 +2,9 @@ #include #include +// TODO: This file can be dropped when we drop the support of LLVM +// versions w/o crt2-command.o support. + // The user's `main` function, expecting arguments. // // Note that we make this a weak symbol so that it will have a From 7aedc3e60344c48dd7d5b82b0098e69879c89ed5 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 19 Aug 2024 19:38:43 +0900 Subject: [PATCH 3/3] Add __main_argc_argv to undefined-symbols.txt --- expected/wasm32-wasip1-threads/undefined-symbols.txt | 1 + expected/wasm32-wasip1/undefined-symbols.txt | 1 + expected/wasm32-wasip2/undefined-symbols.txt | 1 + 3 files changed, 3 insertions(+) diff --git a/expected/wasm32-wasip1-threads/undefined-symbols.txt b/expected/wasm32-wasip1-threads/undefined-symbols.txt index 83babb761..08df2de2a 100644 --- a/expected/wasm32-wasip1-threads/undefined-symbols.txt +++ b/expected/wasm32-wasip1-threads/undefined-symbols.txt @@ -62,6 +62,7 @@ __imported_wasi_snapshot_preview1_sock_shutdown __imported_wasi_thread_spawn __letf2 __lttf2 +__main_argc_argv __netf2 __stack_pointer __subtf3 diff --git a/expected/wasm32-wasip1/undefined-symbols.txt b/expected/wasm32-wasip1/undefined-symbols.txt index bdcb0c786..6d3b2b7d6 100644 --- a/expected/wasm32-wasip1/undefined-symbols.txt +++ b/expected/wasm32-wasip1/undefined-symbols.txt @@ -59,6 +59,7 @@ __imported_wasi_snapshot_preview1_sock_send __imported_wasi_snapshot_preview1_sock_shutdown __letf2 __lttf2 +__main_argc_argv __netf2 __stack_pointer __subtf3 diff --git a/expected/wasm32-wasip2/undefined-symbols.txt b/expected/wasm32-wasip2/undefined-symbols.txt index b98dc7113..a1e35efac 100644 --- a/expected/wasm32-wasip2/undefined-symbols.txt +++ b/expected/wasm32-wasip2/undefined-symbols.txt @@ -59,6 +59,7 @@ __imported_wasi_snapshot_preview1_sock_send __imported_wasi_snapshot_preview1_sock_shutdown __letf2 __lttf2 +__main_argc_argv __netf2 __stack_pointer __subtf3