-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build and install an LTO version of libc.a and the startup files, alongside the non-LTO versions. This also adds support for the proposed `__main_argc_argv` convention, supporting compilers both with and without that change.
- Loading branch information
1 parent
a280fea
commit 041d027
Showing
7 changed files
with
378 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <wasi/api.h> | ||
#include <wasi/libc.h> | ||
#include <stdlib.h> | ||
#include <sysexits.h> | ||
|
||
// New compilers define `__main_argc_argv`. If that doesn't exist, we | ||
// may get called here. Old compilers define `main` expecting an | ||
// argv/argc, so call that. | ||
// TODO: Remove this layer when we no longer have to support old compilers. | ||
int __wasilibc_main(int argc, char *argv[]) asm("main"); | ||
|
||
__attribute__((weak, nodebug)) | ||
int __main_argc_argv(int argc, char *argv[]) { | ||
return __wasilibc_main(argc, argv); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <wasi/api.h> | ||
#include <wasi/libc.h> | ||
#include <stdlib.h> | ||
#include <sysexits.h> | ||
|
||
// The user's `main` function, expecting arguments. | ||
int __main_argc_argv(int argc, char *argv[]); | ||
|
||
// If the user's `main` function expects arguments, the compiler will rename | ||
// it to `__main_argc_argv`, and this version will get linked in, which | ||
// initializes the argument data and calls `__main_argc_argv`. | ||
__attribute__((weak, nodebug)) | ||
int __main_void(void) { | ||
__wasi_errno_t err; | ||
|
||
// Get the sizes of the arrays we'll have to create to copy in the args. | ||
size_t argv_buf_size; | ||
size_t argc; | ||
err = __wasi_args_sizes_get(&argc, &argv_buf_size); | ||
if (err != __WASI_ERRNO_SUCCESS) { | ||
_Exit(EX_OSERR); | ||
} | ||
|
||
// Add 1 for the NULL pointer to mark the end, and check for overflow. | ||
size_t num_ptrs = argc + 1; | ||
if (num_ptrs == 0) { | ||
_Exit(EX_SOFTWARE); | ||
} | ||
|
||
// Allocate memory for storing the argument chars. | ||
char *argv_buf = malloc(argv_buf_size); | ||
if (argv_buf == NULL) { | ||
_Exit(EX_SOFTWARE); | ||
} | ||
|
||
// Allocate memory for the array of pointers. This uses `calloc` both to | ||
// handle overflow and to initialize the NULL pointer at the end. | ||
char **argv = calloc(num_ptrs, sizeof(char *)); | ||
if (argv == NULL) { | ||
free(argv_buf); | ||
_Exit(EX_SOFTWARE); | ||
} | ||
|
||
// Fill the argument chars, and the argv array with pointers into those chars. | ||
// TODO: Remove the casts on `argv_ptrs` and `argv_buf` once the witx is updated with char8 support. | ||
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf); | ||
if (err != __WASI_ERRNO_SUCCESS) { | ||
free(argv_buf); | ||
free(argv); | ||
_Exit(EX_OSERR); | ||
} | ||
|
||
// Call `__main_argc_argv` with the arguments! | ||
return __main_argc_argv(argc, argv); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters