Skip to content

Commit 1176e2f

Browse files
committed
Move the new crt1.c into libc-bottom-half.
The new crt1.c depends on WASI core, so move it into libc-bottom-half.
1 parent 40d2d70 commit 1176e2f

3 files changed

Lines changed: 103 additions & 76 deletions

File tree

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
5555
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c)
5656
LIBWASI_EMULATED_MMAN_SOURCES = \
5757
$(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c)
58+
LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
5859
LIBC_TOP_HALF_DIR = $(CURDIR)/libc-top-half
5960
LIBC_TOP_HALF_MUSL_DIR = $(LIBC_TOP_HALF_DIR)/musl
6061
LIBC_TOP_HALF_MUSL_SRC_DIR = $(LIBC_TOP_HALF_MUSL_DIR)/src
@@ -366,13 +367,19 @@ $(SYSROOT_INC):
366367
"$(SYSROOT_INC)/wordexp.h" \
367368
"$(SYSROOT_INC)/spawn.h"
368369

370+
ifeq ($(BUILD_LIBC_BOTTOM_HALF),no)
371+
override CRT_SOURCES = $(BASICS_CRT_SOURCES)
372+
else
373+
override CRT_SOURCES = $(LIBC_BOTTOM_HALF_CRT_SOURCES)
374+
endif
375+
369376
startup_files: $(SYSROOT_INC)
370377
#
371378
# Build the startup files.
372379
#
373380
@mkdir -p "$(OBJDIR)"
374381
cd "$(OBJDIR)" && \
375-
"$(WASM_CC)" $(WASM_CFLAGS) -c $(BASICS_CRT_SOURCES) -MD -MP && \
382+
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT_SOURCES) -MD -MP && \
376383
mkdir -p "$(SYSROOT_LIB)" && \
377384
mv *.o "$(SYSROOT_LIB)"
378385

basics/libc/crt1.c

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,14 @@
1-
#include <stdlib.h>
2-
#include <sysexits.h>
3-
#include <wasi/core.h>
4-
5-
extern char **__environ;
61
extern void __wasm_call_ctors(void);
72
extern int main(int, char *[]);
83
extern void __prepare_for_exit(void);
94
void _Exit(int) __attribute__((noreturn));
105

11-
static __wasi_errno_t populate_args(size_t *argc, char ***argv) {
12-
__wasi_errno_t err;
13-
14-
/* Get the sizes of the arrays we'll have to create to copy in the args. */
15-
size_t argv_buf_size;
16-
err = __wasi_args_sizes_get(argc, &argv_buf_size);
17-
if (err != __WASI_ESUCCESS) {
18-
return err;
19-
}
20-
if (*argc == 0) {
21-
return __WASI_ESUCCESS;
22-
}
23-
24-
/* Allocate memory for the array of pointers. */
25-
*argv = malloc(sizeof(char *) * *argc);
26-
/* Allocate memory for storing the argument chars. */
27-
char *argv_buf = malloc(sizeof(char) * argv_buf_size);
28-
if (*argv == NULL || argv_buf == NULL) {
29-
return __WASI_ENOMEM;
30-
}
31-
32-
/* Fill the argument chars, and the argv array with pointers into those chars. */
33-
return __wasi_args_get(*argv, argv_buf);
34-
}
35-
36-
static __wasi_errno_t populate_environ() {
37-
__wasi_errno_t err;
38-
39-
/* Get the sizes of the arrays we'll have to create to copy in the environment. */
40-
size_t environ_count;
41-
size_t environ_buf_size;
42-
err = __wasi_environ_sizes_get(&environ_count, &environ_buf_size);
43-
if (err != __WASI_ESUCCESS) {
44-
return err;
45-
}
46-
/* If there's no environment pairs, make sure environ is null and return. */
47-
if (environ_count == 0) {
48-
__environ = NULL;
49-
return __WASI_ESUCCESS;
50-
}
51-
52-
/* Allocate memory for the array of pointers, plus one terminating null pointer. */
53-
__environ = malloc(sizeof(char *) * (environ_count + 1));
54-
/* Allocate memory for storing the environment chars. */
55-
char *environ_buf = malloc(sizeof(char) * environ_buf_size);
56-
if (__environ == NULL || environ_buf == NULL) {
57-
return __WASI_ENOMEM;
58-
}
59-
60-
/* Make sure the last pointer in the array is NULL. */
61-
__environ[environ_count] = NULL;
62-
63-
/* Fill the environment chars, and the __environ array with pointers into those chars. */
64-
return __wasi_environ_get(__environ, environ_buf);
65-
}
66-
676
void _start(void) {
687
/* The linker synthesizes this to call constructors. */
698
__wasm_call_ctors();
709

71-
/* Fill in the environment from WASI syscalls. */
72-
if (populate_environ() != __WASI_ESUCCESS) {
73-
_Exit(EX_OSERR);
74-
}
75-
76-
/* Fill in the arguments from WASI syscalls. */
77-
size_t argc;
78-
char **argv;
79-
if (populate_args(&argc, &argv) != __WASI_ESUCCESS) {
80-
_Exit(EX_OSERR);
81-
}
82-
83-
/* Call main with the arguments. */
84-
int r = main(argc, argv);
10+
/* Call main with no arguments. */
11+
int r = main(0, 0);
8512

8613
/* Call atexit functions, destructors, stdio cleanup, etc. */
8714
__prepare_for_exit();

libc-bottom-half/crt/crt1.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdlib.h>
2+
#include <sysexits.h>
3+
#include <wasi/core.h>
4+
5+
extern char **__environ;
6+
extern void __wasm_call_ctors(void);
7+
extern int main(int, char *[]);
8+
extern void __prepare_for_exit(void);
9+
void _Exit(int) __attribute__((noreturn));
10+
11+
static __wasi_errno_t populate_args(size_t *argc, char ***argv) {
12+
__wasi_errno_t err;
13+
14+
/* Get the sizes of the arrays we'll have to create to copy in the args. */
15+
size_t argv_buf_size;
16+
err = __wasi_args_sizes_get(argc, &argv_buf_size);
17+
if (err != __WASI_ESUCCESS) {
18+
return err;
19+
}
20+
if (*argc == 0) {
21+
return __WASI_ESUCCESS;
22+
}
23+
24+
/* Allocate memory for the array of pointers. */
25+
*argv = malloc(sizeof(char *) * *argc);
26+
/* Allocate memory for storing the argument chars. */
27+
char *argv_buf = malloc(sizeof(char) * argv_buf_size);
28+
if (*argv == NULL || argv_buf == NULL) {
29+
return __WASI_ENOMEM;
30+
}
31+
32+
/* Fill the argument chars, and the argv array with pointers into those chars. */
33+
return __wasi_args_get(*argv, argv_buf);
34+
}
35+
36+
static __wasi_errno_t populate_environ() {
37+
__wasi_errno_t err;
38+
39+
/* Get the sizes of the arrays we'll have to create to copy in the environment. */
40+
size_t environ_count;
41+
size_t environ_buf_size;
42+
err = __wasi_environ_sizes_get(&environ_count, &environ_buf_size);
43+
if (err != __WASI_ESUCCESS) {
44+
return err;
45+
}
46+
/* If there's no environment pairs, make sure environ is null and return. */
47+
if (environ_count == 0) {
48+
__environ = NULL;
49+
return __WASI_ESUCCESS;
50+
}
51+
52+
/* Allocate memory for the array of pointers, plus one terminating null pointer. */
53+
__environ = malloc(sizeof(char *) * (environ_count + 1));
54+
/* Allocate memory for storing the environment chars. */
55+
char *environ_buf = malloc(sizeof(char) * environ_buf_size);
56+
if (__environ == NULL || environ_buf == NULL) {
57+
return __WASI_ENOMEM;
58+
}
59+
60+
/* Make sure the last pointer in the array is NULL. */
61+
__environ[environ_count] = NULL;
62+
63+
/* Fill the environment chars, and the __environ array with pointers into those chars. */
64+
return __wasi_environ_get(__environ, environ_buf);
65+
}
66+
67+
void _start(void) {
68+
/* The linker synthesizes this to call constructors. */
69+
__wasm_call_ctors();
70+
71+
/* Fill in the environment from WASI syscalls. */
72+
if (populate_environ() != __WASI_ESUCCESS) {
73+
_Exit(EX_OSERR);
74+
}
75+
76+
/* Fill in the arguments from WASI syscalls. */
77+
size_t argc;
78+
char **argv;
79+
if (populate_args(&argc, &argv) != __WASI_ESUCCESS) {
80+
_Exit(EX_OSERR);
81+
}
82+
83+
/* Call main with the arguments. */
84+
int r = main(argc, argv);
85+
86+
/* Call atexit functions, destructors, stdio cleanup, etc. */
87+
__prepare_for_exit();
88+
89+
/* If main exited successfully, just return, otherwise call _Exit. */
90+
if (r != 0) {
91+
_Exit(r);
92+
}
93+
}

0 commit comments

Comments
 (0)