From 614d783e927261e503bc144f7c7d1a9dc02565ba Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 3 Oct 2020 14:18:39 -0700 Subject: [PATCH] New-style command support. (#203) This adds a new crt1-command.c startup file, which uses [new-style command support]. Instead of calling `__wasm_call_ctors` and `__wasm_call_dtors` directly, this lets wasm-ld automatically call them. This preserves the existing crt1.c, so that the same wasi-libc build can support old-style and new-style commands, for compatibility during the transition. [new-style command support]: https://reviews.llvm.org/D81689 Co-authored-by: Dan Gohman --- expected/wasm32-wasi/defined-symbols.txt | 1 + .../cloudlibc/src/include/stdlib.h | 2 +- libc-bottom-half/crt/crt1-command.c | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 libc-bottom-half/crt/crt1-command.c diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt index 7cab06cb8..84c508d0c 100644 --- a/expected/wasm32-wasi/defined-symbols.txt +++ b/expected/wasm32-wasi/defined-symbols.txt @@ -276,6 +276,7 @@ _exit _flushlbf _initialize _start +_start a64l abort abs diff --git a/libc-bottom-half/cloudlibc/src/include/stdlib.h b/libc-bottom-half/cloudlibc/src/include/stdlib.h index 134a0ba34..e30b8de0f 100644 --- a/libc-bottom-half/cloudlibc/src/include/stdlib.h +++ b/libc-bottom-half/cloudlibc/src/include/stdlib.h @@ -139,8 +139,8 @@ void *calloc(size_t, size_t); div_t div(int, int) __pure2; double drand48(void); double erand48(__uint16_t *); -_Noreturn void exit(int); #endif +_Noreturn void exit(int); void free(void *); #ifdef __wasilibc_unmodified_upstream char *getenv(const char *); diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c new file mode 100644 index 000000000..93279fbed --- /dev/null +++ b/libc-bottom-half/crt/crt1-command.c @@ -0,0 +1,18 @@ +#include +#include +extern void __wasm_call_ctors(void); +extern int __original_main(void); +extern void __wasm_call_dtors(void); + +__attribute__((export_name("_start"))) +void _start(void) { + // Call `__original_main` which will either be the application's zero-argument + // `__original_main` function or a libc routine which calls `__main_void`. + // TODO: Call `main` directly once we no longer have to support old compilers. + int r = __original_main(); + + // If main exited successfully, just return, otherwise call `exit`. + if (r != 0) { + exit(r); + } +}