Skip to content

Commit

Permalink
New-style command support. (#203)
Browse files Browse the repository at this point in the history
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 <sunfish@mozilla.com>
  • Loading branch information
sunfishcode and sunfishcode committed Oct 3, 2020
1 parent d2482b7 commit 614d783
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ _exit
_flushlbf
_initialize
_start
_start
a64l
abort
abs
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/cloudlibc/src/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
18 changes: 18 additions & 0 deletions libc-bottom-half/crt/crt1-command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <wasi/api.h>
#include <stdlib.h>
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);
}
}

0 comments on commit 614d783

Please sign in to comment.