Skip to content

Commit 84baf06

Browse files
committed
Support whole-program LTO
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.
1 parent dd010be commit 84baf06

File tree

9 files changed

+378
-305
lines changed

9 files changed

+378
-305
lines changed

Makefile

+294-250
Large diffs are not rendered by default.

basics/crt/crt1.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ void _start(void) {
77
// The linker synthesizes this to call constructors.
88
__wasm_call_ctors();
99

10-
// Call `__original_main` which will either be the application's
11-
// zero-argument `main` function (renamed by the compiler) or a libc
12-
// routine which populates `argv` and `argc` and calls the application's
13-
// two-argument `main`.
10+
// Call `__original_main` which will either be the application's zero-argument
11+
// `__original_main` function or a libc routine which calls `__main_void`.
12+
// TODO: Call `main` directly once we no longer have to support old compilers.
1413
int r = __original_main();
1514

1615
// Call atexit functions, destructors, stdio cleanup, etc.
File renamed without changes.
File renamed without changes.

expected/wasm32-wasi/defined-symbols.txt

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ __log2_data
139139
__log2f_data
140140
__log_data
141141
__logf_data
142+
__main_argc_argv
143+
__main_void
142144
__math_divzero
143145
__math_divzerof
144146
__math_invalid

libc-bottom-half/crt/crt1.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <wasi/api.h>
2-
#include <wasi/libc.h>
32
extern void __wasm_call_ctors(void);
43
extern int __original_main(void);
54
extern void __prepare_for_exit(void);
@@ -8,10 +7,9 @@ void _start(void) {
87
// The linker synthesizes this to call constructors.
98
__wasm_call_ctors();
109

11-
// Call `__original_main` which will either be the application's
12-
// zero-argument `main` function (renamed by the compiler) or a libc
13-
// routine which populates `argv` and `argc` and calls the application's
14-
// two-argument `main`.
10+
// Call `__original_main` which will either be the application's zero-argument
11+
// `__original_main` function or a libc routine which calls `__main_void`.
12+
// TODO: Call `main` directly once we no longer have to support old compilers.
1513
int r = __original_main();
1614

1715
// Call atexit functions, destructors, stdio cleanup, etc.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <wasi/api.h>
2+
#include <wasi/libc.h>
3+
#include <stdlib.h>
4+
#include <sysexits.h>
5+
6+
// New compilers define `__main_argc_argv`. If that doesn't exist, we
7+
// may get called here. Old compilers define `main` expecting an
8+
// argv/argc, so call that.
9+
// TODO: Remove this layer when we no longer have to support old compilers.
10+
int __wasilibc_main(int argc, char *argv[]) asm("main");
11+
12+
__attribute__((weak, nodebug))
13+
int __main_argc_argv(int argc, char *argv[]) {
14+
return __wasilibc_main(argc, argv);
15+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <wasi/api.h>
2+
#include <wasi/libc.h>
3+
#include <stdlib.h>
4+
#include <sysexits.h>
5+
6+
// The user's `main` function, expecting arguments.
7+
int __main_argc_argv(int argc, char *argv[]);
8+
9+
// If the user's `main` function expects arguments, the compiler will rename
10+
// it to `__main_argc_argv`, and this version will get linked in, which
11+
// initializes the argument data and calls `__main_argc_argv`.
12+
__attribute__((weak, nodebug))
13+
int __main_void(void) {
14+
__wasi_errno_t err;
15+
16+
// Get the sizes of the arrays we'll have to create to copy in the args.
17+
size_t argv_buf_size;
18+
size_t argc;
19+
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
20+
if (err != __WASI_ERRNO_SUCCESS) {
21+
_Exit(EX_OSERR);
22+
}
23+
24+
// Add 1 for the NULL pointer to mark the end, and check for overflow.
25+
size_t num_ptrs = argc + 1;
26+
if (num_ptrs == 0) {
27+
_Exit(EX_SOFTWARE);
28+
}
29+
30+
// Allocate memory for storing the argument chars.
31+
char *argv_buf = malloc(argv_buf_size);
32+
if (argv_buf == NULL) {
33+
_Exit(EX_SOFTWARE);
34+
}
35+
36+
// Allocate memory for the array of pointers. This uses `calloc` both to
37+
// handle overflow and to initialize the NULL pointer at the end.
38+
char **argv = calloc(num_ptrs, sizeof(char *));
39+
if (argv == NULL) {
40+
free(argv_buf);
41+
_Exit(EX_SOFTWARE);
42+
}
43+
44+
// Fill the argument chars, and the argv array with pointers into those chars.
45+
// TODO: Remove the casts on `argv_ptrs` and `argv_buf` once the witx is updated with char8 support.
46+
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf);
47+
if (err != __WASI_ERRNO_SUCCESS) {
48+
free(argv_buf);
49+
free(argv);
50+
_Exit(EX_OSERR);
51+
}
52+
53+
// Call `__main_argc_argv` with the arguments!
54+
return __main_argc_argv(argc, argv);
55+
}

libc-bottom-half/sources/__original_main.c

+6-46
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,13 @@
33
#include <stdlib.h>
44
#include <sysexits.h>
55

6-
// The user's `main` function, expecting arguments.
7-
int main(int argc, char *argv[]);
6+
// Old compilers define `__original_main`. If that doesn't exist, we
7+
// get called here. New compilers define `__main_void`. If that doesn't
8+
// exist, we'll try something else.
9+
// TODO: Remove this layer when we no longer have to support old compilers.
10+
int __main_void(void);
811

9-
// If the user's `main` function expects arguments, the compiler won't emit
10-
// an `__original_main` function so this version will get linked in, which
11-
// initializes the argument data and calls `main`.
1212
__attribute__((weak))
1313
int __original_main(void) {
14-
__wasi_errno_t err;
15-
16-
// Get the sizes of the arrays we'll have to create to copy in the args.
17-
size_t argv_buf_size;
18-
size_t argc;
19-
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
20-
if (err != __WASI_ERRNO_SUCCESS) {
21-
_Exit(EX_OSERR);
22-
}
23-
24-
// Add 1 for the NULL pointer to mark the end, and check for overflow.
25-
size_t num_ptrs = argc + 1;
26-
if (num_ptrs == 0) {
27-
_Exit(EX_SOFTWARE);
28-
}
29-
30-
// Allocate memory for storing the argument chars.
31-
char *argv_buf = malloc(argv_buf_size);
32-
if (argv_buf == NULL) {
33-
_Exit(EX_SOFTWARE);
34-
}
35-
36-
// Allocate memory for the array of pointers. This uses `calloc` both to
37-
// handle overflow and to initialize the NULL pointer at the end.
38-
char **argv = calloc(num_ptrs, sizeof(char *));
39-
if (argv == NULL) {
40-
free(argv_buf);
41-
_Exit(EX_SOFTWARE);
42-
}
43-
44-
// Fill the argument chars, and the argv array with pointers into those chars.
45-
// TODO: Remove the casts on `argv_ptrs` and `argv_buf` once the witx is updated with char8 support.
46-
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf);
47-
if (err != __WASI_ERRNO_SUCCESS) {
48-
free(argv_buf);
49-
free(argv);
50-
_Exit(EX_OSERR);
51-
}
52-
53-
// Call main with the arguments!
54-
return main(argc, argv);
14+
return __main_void();
5515
}

0 commit comments

Comments
 (0)