Rev 3799 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3799 | Rev 3805 | ||
---|---|---|---|
1 | #include <_ansi.h> |
1 | #include <_ansi.h> |
2 | #include |
2 | #include |
3 | #include |
3 | #include |
4 | #include |
4 | #include |
5 | #include |
5 | #include |
6 | #include |
6 | #include |
7 | #include |
7 | #include |
8 | #include |
8 | #include |
9 | #include |
9 | #include |
10 | 10 | ||
11 | struct app_hdr |
11 | struct app_hdr |
12 | { |
12 | { |
13 | char banner[8]; |
13 | char banner[8]; |
14 | int version; |
14 | int version; |
15 | int start; |
15 | int start; |
16 | int iend; |
16 | int iend; |
17 | int memsize; |
17 | int memsize; |
18 | int stacktop; |
18 | int stacktop; |
19 | char *cmdline; |
19 | char *cmdline; |
20 | char *path; |
20 | char *path; |
21 | }; |
21 | int reserved; |
- | 22 | void *__idata_start; |
|
- | 23 | void *__idata_end; |
|
- | 24 | void (*main)(int argc, char **argv, char **envp); |
|
- | 25 | }; |
|
22 | 26 | ||
23 | int _argc; |
27 | int _argc; |
24 | char **_argv; |
28 | char **_argv; |
25 | 29 | ||
26 | 30 | ||
27 | void __fastcall init_loader(void *libc_image); |
- | |
28 | void* __fastcall create_image(void *raw); |
- | |
29 | int __fastcall link_image(void *img_base); |
- | |
30 | void* get_entry_point(void *raw); |
31 | void* get_entry_point(void *raw); |
31 | int (*entry)(int, char **, char **); |
32 | int (*entry)(int, char **, char **); |
32 | 33 | ||
- | 34 | void init_loader(void *libc_image); |
|
- | 35 | ||
33 | 36 | ||
34 | void init_reent(); |
37 | void init_reent(); |
35 | 38 | ||
36 | jmp_buf loader_env; |
39 | jmp_buf loader_env; |
37 | 40 | ||
38 | void __attribute__((noreturn)) |
41 | void __attribute__((noreturn)) |
39 | __thread_startup (int (*entry)(void*), void *param, |
42 | __thread_startup (int (*entry)(void*), void *param, |
40 | void *stacklow, void *stackhigh) |
43 | void *stacklow, void *stackhigh) |
41 | { |
44 | { |
42 | int retval; |
45 | int retval; |
43 | 46 | ||
44 | asm volatile ( "xchgw %bx, %bx"); |
47 | asm volatile ( "xchgw %bx, %bx"); |
45 | 48 | ||
46 | __asm__ __volatile__( // save stack limits |
49 | __asm__ __volatile__( // save stack limits |
47 | "movl %0, %%fs:4 \n\t" // use TLS |
50 | "movl %0, %%fs:8 \n\t" // use TLS |
48 | "movl %1, %%fs:8 \n\t" |
51 | "movl %1, %%fs:12 \n\t" |
49 | ::"r"(stacklow), "r"(stackhigh)); |
52 | ::"r"(stacklow), "r"(stackhigh)); |
50 | 53 | ||
51 | init_reent(); // initialize thread reentry structure |
54 | init_reent(); // initialize thread reentry structure |
52 | 55 | ||
53 | retval = entry(param); // call user thread function |
56 | retval = entry(param); // call user thread function |
54 | 57 | ||
55 | _exit(retval); |
58 | _exit(retval); |
56 | }; |
59 | }; |
57 | 60 | ||
58 | char * __libc_getenv(const char *name) |
61 | char * __libc_getenv(const char *name) |
59 | { |
62 | { |
60 | return NULL; |
63 | return NULL; |
61 | } |
64 | } |
62 | 65 | ||
- | 66 | void _pei386_runtime_relocator (void); |
|
- | 67 | int link_app(); |
|
- | 68 | ||
63 | char __appcwd[1024]; |
69 | char __appcwd[1024]; |
64 | int __appcwdlen; |
70 | int __appcwdlen; |
65 | char* __appenv; |
71 | char* __appenv; |
66 | int __appenv_size; |
72 | int __appenv_size; |
67 | 73 | ||
- | 74 | static char *arg[2]; |
|
- | 75 | ||
- | 76 | extern char _tls_map[128]; |
|
- | 77 | ||
68 | void __attribute__((noreturn)) |
78 | void __attribute__((noreturn)) |
69 | crt_startup (void *libc_base, void *obj_base, uint32_t *params) |
79 | libc_crt_startup (void *libc_base) |
70 | { |
80 | { |
71 | struct app_hdr *header; |
81 | struct app_hdr *header = NULL; |
72 | char *arg[2]; |
- | |
73 | 82 | ||
74 | int len; |
83 | int len; |
75 | char *p; |
84 | char *p; |
76 | 85 | ||
77 | void *my_app; |
86 | void *my_app; |
78 | int retval = 0; |
87 | int retval = 0; |
79 | 88 | ||
80 | // user_free(obj_base); |
89 | _pei386_runtime_relocator(); |
- | 90 | ||
- | 91 | memset(_tls_map, 0xFF, 32*4); |
|
81 | 92 | _tls_map[0] = 0xE0; |
|
82 | init_reent(); |
93 | init_reent(); |
- | 94 | __initPOSIXHandles(); |
|
83 | __initPOSIXHandles(); |
95 | |
84 | // __appenv = load_file("/sys/system.env", &__appenv_size); |
96 | // __appenv = load_file("/sys/system.env", &__appenv_size); |
85 | 97 | ||
86 | init_loader(libc_base); |
98 | init_loader(libc_base); |
87 | 99 | ||
88 | my_app = create_image((void*)(params[0])); |
- | |
89 | - | ||
90 | if( link_image(my_app)==0) |
100 | if( link_app() == 0) |
91 | goto done; |
101 | goto done; |
92 | - | ||
93 | header = (struct app_hdr*)NULL; |
- | |
94 | 102 | ||
95 | __appcwdlen = strrchr(header->path, '/') - header->path; |
103 | __appcwdlen = strrchr(header->path, '/') - header->path; |
96 | __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen; |
104 | __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen; |
97 | memcpy(__appcwd, header->path, __appcwdlen); |
105 | memcpy(__appcwd, header->path, __appcwdlen); |
98 | set_cwd(__appcwd); |
106 | set_cwd(__appcwd); |
99 | 107 | ||
100 | #ifdef BRAVE_NEW_WORLD |
- | |
101 | len = strlen(header->path); |
- | |
102 | p = alloca(len+1); |
- | |
103 | memcpy(p, header->path, len); |
- | |
104 | p[len]=0; |
- | |
105 | - | ||
106 | arg[0] = p; |
- | |
107 | #else |
- | |
108 | arg[0] = header->path; |
108 | arg[0] = header->path; |
109 | #endif |
- | |
110 | - | ||
111 | _argc = 1; |
- | |
112 | 109 | ||
113 | if( header->cmdline != 0) |
110 | if( header->cmdline[0] != 0) |
114 | { |
- | |
115 | #ifdef BRAVE_NEW_WORLD |
- | |
116 | len = strlen(header->cmdline); |
- | |
117 | if(len) |
- | |
118 | { |
- | |
119 | p = alloca(len+1); |
- | |
120 | memcpy(p, header->cmdline, len); |
- | |
121 | p[len]=0; |
- | |
122 | _argc = 2; |
- | |
123 | arg[1] = p; |
- | |
124 | }; |
- | |
125 | #else |
111 | { |
126 | _argc = 2; |
112 | _argc = 2; |
127 | arg[1] = header->cmdline; |
- | |
128 | #endif |
113 | arg[1] = header->cmdline; |
- | 114 | } |
|
129 | }; |
115 | else _argc = 1; |
130 | 116 | ||
131 | _argv = arg; |
117 | _argv = arg; |
132 | 118 | ||
133 | entry = get_entry_point(my_app); |
- | |
134 | - | ||
135 | // __asm__ __volatile__("int3"); |
- | |
136 | - | ||
137 | retval = entry(_argc, _argv, NULL); |
119 | header->main(_argc, _argv, NULL); |
138 | - | ||
139 | done: |
120 | done: |
140 | exit (retval); |
121 | _exit (retval); |
141 | } |
122 | } |