forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrt0_standalone.cpp
More file actions
62 lines (52 loc) · 1.43 KB
/
crt0_standalone.cpp
File metadata and controls
62 lines (52 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Types.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/internals.h>
#include <unistd.h>
extern "C" {
int main(int, char**, char**);
// Tell the compiler that this may be called from somewhere else.
int _entry(int argc, char** argv) __attribute__((used));
void _start(int, char**, char**) __attribute__((used));
NAKED void _start(int, char**, char**)
{
#if ARCH(AARCH64)
asm(
"mov x29, 0\n"
"mov x30, 0\n"
"bl _entry\n");
#elif ARCH(RISCV64)
asm(
"li fp, 0\n"
"li ra, 0\n"
"tail _entry@plt\n");
#elif ARCH(X86_64)
asm(
"push $0\n"
"jmp _entry@plt\n");
#else
# error "Unknown architecture"
#endif
}
int _entry(int argc, char** argv)
{
// In the original crt0.cpp, we didn't need this line, but we don't
// run with libc as **defined** dependency (i.e. shared object),
// therefore the expected initialization of the dynamic libc.so that
// should done by the DynamicLinker is never actually done,
// specifically at DynamicLinker::initialize_libc method.
// Thus, let's initialize our own "copy" of libc in this binary so
// everything could work as expected.
__libc_init();
__begin_atexit_locking();
int status = main(argc, argv, environ);
exit(status);
return 20150614;
}
}