Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/standalone/__main_void.c
6174 views
1
/*
2
* Copyright 2019 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include <alloca.h>
9
#include <wasi/api.h>
10
#include <sysexits.h>
11
12
// Much of this code comes from:
13
// https://github.com/CraneStation/wasi-libc/blob/master/libc-bottom-half/crt/crt1.c
14
// Converted malloc() calls to alloca() to avoid including malloc in all programs.
15
16
int __main_argc_argv(int argc, char *argv[]);
17
18
// If the application's `main` does not uses argc/argv, then it will be defined
19
// (by llvm) with an __main_void alias, and therefore this function will not
20
// be included, and `_start` will call the application's `__main_void` directly.
21
//
22
// If the application's `main` does use argc/argv, then _start will call this
23
// function which loads argv values and sends them to the
24
// application's `main` which gets mangled to `__main_argc_argv` by llvm.
25
__attribute__((weak))
26
int __main_void(void) {
27
/* Fill in the arguments from WASI syscalls. */
28
size_t argc;
29
char **argv = NULL;
30
size_t argv_buf_size;
31
__wasi_errno_t err;
32
33
/* Get the sizes of the arrays we'll have to create to copy in the args. */
34
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
35
if (err != __WASI_ERRNO_SUCCESS) {
36
__wasi_proc_exit(EX_OSERR);
37
}
38
39
if (argc) {
40
/* Allocate memory for the array of pointers, adding null terminator. */
41
argv = alloca(sizeof(char *) * (argc + 1));
42
/* Allocate memory for storing the argument chars. */
43
uint8_t *argv_buf = alloca(sizeof(char) * argv_buf_size);
44
/* Make sure the last pointer in the array is NULL. */
45
argv[argc] = NULL;
46
/* Fill the argument chars, and the argv array with pointers into those chars. */
47
err = __wasi_args_get((uint8_t**)argv, argv_buf);
48
if (err != __WASI_ERRNO_SUCCESS) {
49
__wasi_proc_exit(EX_OSERR);
50
}
51
}
52
53
return __main_argc_argv(argc, argv);
54
}
55
56