Skip to content

Commit

Permalink
More cleanups.
Browse files Browse the repository at this point in the history
Make reactor_setup a weak external, and factor out common code between
the crt1.c implementations into a shared header.

This renames basics/include to basics/headers/public, which is more
consistent with the directory structures in libc-bottom-half and
libc-top-half, and which allows us to have basics/headers/private, for
the new crt1.h.
  • Loading branch information
sunfishcode committed Jun 5, 2019
1 parent 414e725 commit 1bd89e0
Show file tree
Hide file tree
Showing 33 changed files with 62 additions and 59 deletions.
11 changes: 7 additions & 4 deletions Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 \
Expand Down
38 changes: 12 additions & 26 deletions 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);
}
40 changes: 40 additions & 0 deletions 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
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion expected/wasm32-wasi/undefined-symbols.txt
Expand Up @@ -68,4 +68,3 @@ __wasi_sock_send
__wasi_sock_shutdown
__wasm_call_ctors
main
reactor_setup
31 changes: 3 additions & 28 deletions libc-bottom-half/crt/crt1.c
@@ -1,18 +1,9 @@
#include "crt1.h"
#include <stdlib.h>
#include <sysexits.h>
#include <wasi/core.h>
#include <wasi/libc.h>

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;
Expand Down Expand Up @@ -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);
}

0 comments on commit 1bd89e0

Please sign in to comment.