Skip to content

Commit 614d783

Browse files
authored
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 <[email protected]>
1 parent d2482b7 commit 614d783

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

expected/wasm32-wasi/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ _exit
276276
_flushlbf
277277
_initialize
278278
_start
279+
_start
279280
a64l
280281
abort
281282
abs

libc-bottom-half/cloudlibc/src/include/stdlib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ void *calloc(size_t, size_t);
139139
div_t div(int, int) __pure2;
140140
double drand48(void);
141141
double erand48(__uint16_t *);
142-
_Noreturn void exit(int);
143142
#endif
143+
_Noreturn void exit(int);
144144
void free(void *);
145145
#ifdef __wasilibc_unmodified_upstream
146146
char *getenv(const char *);

libc-bottom-half/crt/crt1-command.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <wasi/api.h>
2+
#include <stdlib.h>
3+
extern void __wasm_call_ctors(void);
4+
extern int __original_main(void);
5+
extern void __wasm_call_dtors(void);
6+
7+
__attribute__((export_name("_start")))
8+
void _start(void) {
9+
// Call `__original_main` which will either be the application's zero-argument
10+
// `__original_main` function or a libc routine which calls `__main_void`.
11+
// TODO: Call `main` directly once we no longer have to support old compilers.
12+
int r = __original_main();
13+
14+
// If main exited successfully, just return, otherwise call `exit`.
15+
if (r != 0) {
16+
exit(r);
17+
}
18+
}

0 commit comments

Comments
 (0)