-
Notifications
You must be signed in to change notification settings - Fork 3
/
entry.c
63 lines (51 loc) · 861 Bytes
/
entry.c
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
//entry.c
#include "minicrt.h"
#ifndef DEBUG
#define DEBUG 0
#endif
extern int main(int argc,char*argv[]);
void exit(int);
static void crt_fatal_error(const char*msg)
{
printf("fatal error : %s\n",msg);
exit(1);
}
void mini_crt_entry(void)
{
int ret;
int argc;
char **argv;
char *ebp_reg=0;
int k=0;
asm(
"movl %%ebp,%0 \n"
:"=r"(ebp_reg)
);
argc=*((int*)(ebp_reg+4));
argv=(char**)(ebp_reg+8);
if(DEBUG){
printf("argc:[%d]\n", argc);
while(k<argc)
printf("argv[%d]: [%s]\n", k, argv[k++]);
}
if(!mini_crt_heap_init())
{
crt_fatal_error("heap init failed!\n");
}
if(!mini_crt_io_init())
{
crt_fatal_error("io init failed!\n");
}
ret=main(argc,argv);
exit(ret);
}
void exit(int exitcode)
{
asm(
"movl %0,%%ebx \n\t"
"movl $1,%%eax \n\t"
"int $0x80 \n\t"
"hlt \n\t"
::"m"(exitcode)
);
}