diff --git a/Makefile b/Makefile index aeed4bc59..e502082a3 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,8 @@ endif # These variables describe the locations of various files and # directories in the source tree. BASICS_DIR = $(CURDIR)/basics -BASICS_INC = $(BASICS_DIR)/include +BASICS_HEADERS_PUBLIC = $(BASICS_DIR)/headers/public +BASICS_HEADERS_PRIVATE = $(BASICS_DIR)/headers/private BASICS_CRT1_SOURCE = $(BASICS_DIR)/crt/crt1.c BASICS_SOURCES = $(wildcard $(BASICS_DIR)/sources/*.c) DLMALLOC_DIR = $(CURDIR)/dlmalloc @@ -300,7 +301,7 @@ include_dirs: # Install the include files. # mkdir -p "$(SYSROOT_INC)" - cp -r "$(BASICS_INC)" "$(SYSROOT)" + cp -r "$(BASICS_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)" cp -r "$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)" # Generate musl's bits/alltypes.h header. @@ -417,8 +418,10 @@ startup_files: include_dirs # Build the startup files. # mkdir -p "$(SYSROOT_LIB)" - "$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT1_SOURCE) -MD -MP -o $(SYSROOT_LIB)/crt1.o - "$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT1_SOURCE) -MD -MP -DREACTOR_RUNTIME -o $(SYSROOT_LIB)/reactor-crt1.o + "$(WASM_CC)" $(WASM_CFLAGS) -I$(BASICS_HEADERS_PRIVATE) -c \ + $(CRT1_SOURCE) -MD -MP -o $(SYSROOT_LIB)/crt1.o + "$(WASM_CC)" $(WASM_CFLAGS) -I$(BASICS_HEADERS_PRIVATE) -c \ + $(CRT1_SOURCE) -MD -MP -DREACTOR_RUNTIME -o $(SYSROOT_LIB)/reactor-crt1.o libc: include_dirs \ $(SYSROOT_LIB)/libc.a \ diff --git a/basics/crt/crt1.c b/basics/crt/crt1.c index fb3fdb444..c228381fe 100644 --- a/basics/crt/crt1.c +++ b/basics/crt/crt1.c @@ -1,34 +1,20 @@ -extern void __wasm_call_ctors(void); -void _Exit(int) __attribute__((noreturn)); - -#ifdef REACTOR_RUNTIME -extern void reactor_setup(int, char *[]); -#else -extern int main(int, char *[]); -extern void __prepare_for_exit(void); -#endif +#include "crt1.h" #ifdef REACTOR_RUNTIME void __wasi_unstable_reactor_start(void) { #else void _start(void) { #endif - /* The linker synthesizes this to call constructors. */ - __wasm_call_ctors(); + /* + * In the basics directory, we don't have a way to pass in command-line + * parameters, so we just use some simple placeholder arguments. For real + * command-line argument handling, see the crt1.c in libc-bottom-half. + */ + static char *argv[] = { + "program", + NULL + }; -#ifdef REACTOR_RUNTIME - /* Call reactor_setup with the arguments. */ - reactor_setup(argc, argv); -#else - /* Call main with no arguments. */ - int r = main(0, 0); - - /* Call atexit functions, destructors, stdio cleanup, etc. */ - __prepare_for_exit(); - - /* If main exited successfully, just return, otherwise call _Exit. */ - if (r != 0) { - _Exit(r); - } -#endif + /* Start the program! */ + start_program(1, argv); } diff --git a/basics/headers/private/crt1.h b/basics/headers/private/crt1.h new file mode 100644 index 000000000..8105816c5 --- /dev/null +++ b/basics/headers/private/crt1.h @@ -0,0 +1,40 @@ +/* + * Common code for defining the crt1.o object, which defines the program + * entrypoint. + */ + +extern void __wasm_call_ctors(void); +void _Exit(int) __attribute__((noreturn)); + +#ifdef REACTOR_RUNTIME +extern void reactor_setup(int, char *[]) __attribute__((weak)); +#else +extern int main(int, char *[]); +extern void __prepare_for_exit(void); +#endif + +static inline void start_program(int argc, char *argv[]) { + /* The linker synthesizes this to call constructors. */ + __wasm_call_ctors(); + +#ifdef REACTOR_RUNTIME + /* + * Call reactor_setup with the arguments. It's a weak external, so we + * can skip calling it if it's not defined. + */ + if (reactor_setup) { + reactor_setup(argc, argv); + } +#else + /* Call main with no arguments. */ + int r = main(0, 0); + + /* Call atexit functions, destructors, stdio cleanup, etc. */ + __prepare_for_exit(); + + /* If main exited successfully, just return, otherwise call _Exit. */ + if (r != 0) { + _Exit(r); + } +#endif +} diff --git a/basics/include/__errno.h b/basics/headers/public/__errno.h similarity index 100% rename from basics/include/__errno.h rename to basics/headers/public/__errno.h diff --git a/basics/include/__functions_malloc.h b/basics/headers/public/__functions_malloc.h similarity index 100% rename from basics/include/__functions_malloc.h rename to basics/headers/public/__functions_malloc.h diff --git a/basics/include/__functions_memcpy.h b/basics/headers/public/__functions_memcpy.h similarity index 100% rename from basics/include/__functions_memcpy.h rename to basics/headers/public/__functions_memcpy.h diff --git a/basics/include/__header_inttypes.h b/basics/headers/public/__header_inttypes.h similarity index 100% rename from basics/include/__header_inttypes.h rename to basics/headers/public/__header_inttypes.h diff --git a/basics/include/__macro_PAGESIZE.h b/basics/headers/public/__macro_PAGESIZE.h similarity index 100% rename from basics/include/__macro_PAGESIZE.h rename to basics/headers/public/__macro_PAGESIZE.h diff --git a/basics/include/__struct_stat.h b/basics/headers/public/__struct_stat.h similarity index 100% rename from basics/include/__struct_stat.h rename to basics/headers/public/__struct_stat.h diff --git a/basics/include/__struct_timespec.h b/basics/headers/public/__struct_timespec.h similarity index 100% rename from basics/include/__struct_timespec.h rename to basics/headers/public/__struct_timespec.h diff --git a/basics/include/__typedef_blkcnt_t.h b/basics/headers/public/__typedef_blkcnt_t.h similarity index 100% rename from basics/include/__typedef_blkcnt_t.h rename to basics/headers/public/__typedef_blkcnt_t.h diff --git a/basics/include/__typedef_blksize_t.h b/basics/headers/public/__typedef_blksize_t.h similarity index 100% rename from basics/include/__typedef_blksize_t.h rename to basics/headers/public/__typedef_blksize_t.h diff --git a/basics/include/__typedef_clock_t.h b/basics/headers/public/__typedef_clock_t.h similarity index 100% rename from basics/include/__typedef_clock_t.h rename to basics/headers/public/__typedef_clock_t.h diff --git a/basics/include/__typedef_dev_t.h b/basics/headers/public/__typedef_dev_t.h similarity index 100% rename from basics/include/__typedef_dev_t.h rename to basics/headers/public/__typedef_dev_t.h diff --git a/basics/include/__typedef_gid_t.h b/basics/headers/public/__typedef_gid_t.h similarity index 100% rename from basics/include/__typedef_gid_t.h rename to basics/headers/public/__typedef_gid_t.h diff --git a/basics/include/__typedef_ino_t.h b/basics/headers/public/__typedef_ino_t.h similarity index 100% rename from basics/include/__typedef_ino_t.h rename to basics/headers/public/__typedef_ino_t.h diff --git a/basics/include/__typedef_mode_t.h b/basics/headers/public/__typedef_mode_t.h similarity index 100% rename from basics/include/__typedef_mode_t.h rename to basics/headers/public/__typedef_mode_t.h diff --git a/basics/include/__typedef_nlink_t.h b/basics/headers/public/__typedef_nlink_t.h similarity index 100% rename from basics/include/__typedef_nlink_t.h rename to basics/headers/public/__typedef_nlink_t.h diff --git a/basics/include/__typedef_off_t.h b/basics/headers/public/__typedef_off_t.h similarity index 100% rename from basics/include/__typedef_off_t.h rename to basics/headers/public/__typedef_off_t.h diff --git a/basics/include/__typedef_ssize_t.h b/basics/headers/public/__typedef_ssize_t.h similarity index 100% rename from basics/include/__typedef_ssize_t.h rename to basics/headers/public/__typedef_ssize_t.h diff --git a/basics/include/__typedef_suseconds_t.h b/basics/headers/public/__typedef_suseconds_t.h similarity index 100% rename from basics/include/__typedef_suseconds_t.h rename to basics/headers/public/__typedef_suseconds_t.h diff --git a/basics/include/__typedef_time_t.h b/basics/headers/public/__typedef_time_t.h similarity index 100% rename from basics/include/__typedef_time_t.h rename to basics/headers/public/__typedef_time_t.h diff --git a/basics/include/__typedef_uid_t.h b/basics/headers/public/__typedef_uid_t.h similarity index 100% rename from basics/include/__typedef_uid_t.h rename to basics/headers/public/__typedef_uid_t.h diff --git a/basics/include/errno.h b/basics/headers/public/errno.h similarity index 100% rename from basics/include/errno.h rename to basics/headers/public/errno.h diff --git a/basics/include/inttypes.h b/basics/headers/public/inttypes.h similarity index 100% rename from basics/include/inttypes.h rename to basics/headers/public/inttypes.h diff --git a/basics/include/stdlib.h b/basics/headers/public/stdlib.h similarity index 100% rename from basics/include/stdlib.h rename to basics/headers/public/stdlib.h diff --git a/basics/include/string.h b/basics/headers/public/string.h similarity index 100% rename from basics/include/string.h rename to basics/headers/public/string.h diff --git a/basics/include/sys/stat.h b/basics/headers/public/sys/stat.h similarity index 100% rename from basics/include/sys/stat.h rename to basics/headers/public/sys/stat.h diff --git a/basics/include/sys/types.h b/basics/headers/public/sys/types.h similarity index 100% rename from basics/include/sys/types.h rename to basics/headers/public/sys/types.h diff --git a/basics/include/time.h b/basics/headers/public/time.h similarity index 100% rename from basics/include/time.h rename to basics/headers/public/time.h diff --git a/basics/include/wchar.h b/basics/headers/public/wchar.h similarity index 100% rename from basics/include/wchar.h rename to basics/headers/public/wchar.h diff --git a/expected/wasm32-wasi/undefined-symbols.txt b/expected/wasm32-wasi/undefined-symbols.txt index 33c9ec3b3..75bee0304 100644 --- a/expected/wasm32-wasi/undefined-symbols.txt +++ b/expected/wasm32-wasi/undefined-symbols.txt @@ -68,4 +68,3 @@ __wasi_sock_send __wasi_sock_shutdown __wasm_call_ctors main -reactor_setup diff --git a/libc-bottom-half/crt/crt1.c b/libc-bottom-half/crt/crt1.c index 686380184..f99ad8923 100644 --- a/libc-bottom-half/crt/crt1.c +++ b/libc-bottom-half/crt/crt1.c @@ -1,18 +1,9 @@ +#include "crt1.h" #include #include #include #include - extern char **__environ; -extern void __wasm_call_ctors(void); -void _Exit(int) __attribute__((noreturn)); - -#ifdef REACTOR_RUNTIME -extern void reactor_setup(int, char *[]); -#else -extern int main(int, char *[]); -extern void __prepare_for_exit(void); -#endif static __wasi_errno_t populate_args(size_t *argc, char ***argv) { __wasi_errno_t err; @@ -131,22 +122,6 @@ void _start(void) { _Exit(EX_OSERR); } - /* The linker synthesizes this to call constructors. */ - __wasm_call_ctors(); - -#ifdef REACTOR_RUNTIME - /* Call reactor_setup with the arguments. */ - reactor_setup(argc, argv); -#else - /* Call main with the arguments. */ - int r = main(argc, argv); - - /* Call atexit functions, destructors, stdio cleanup, etc. */ - __prepare_for_exit(); - - /* If main exited successfully, just return, otherwise call _Exit. */ - if (r != 0) { - _Exit(r); - } -#endif + /* Start the program! */ + start_program(argc, argv); }