Fixes static binary init issue (XCC)
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.14.1-ros / sysdeps / ros / start.c
1 #include <abort-instr.h>
2 #include <string.h>
3 #include <vcore.h>
4 #include <stdio.h>
5 #include <ros/syscall.h>
6 #include <ros/procinfo.h>
7 #include <unistd.h>
8 #include <tls.h>
9
10 void** __vcore_thread_control_blocks = NULL;
11 weak_alias(__vcore_thread_control_blocks,vcore_thread_control_blocks)
12
13 __thread int __vcoreid = 0;
14 __thread bool __vcore_context = FALSE;
15
16 void __uthread_vcore_entry(void)
17 {
18         fputs("Define a uthread_vcore_entry() or a vcore_entry(), foo!\n", stderr);
19         abort();
20 }
21 weak_alias(__uthread_vcore_entry, uthread_vcore_entry)
22
23 void __vcore_entry(void)
24 {
25         uthread_vcore_entry();
26 }
27 weak_alias(__vcore_entry, vcore_entry)
28
29 void __vcore_event_init(void)
30 {
31         fputs("Build your application with -lparlib\n", stderr);
32         abort();
33 }
34 weak_alias(__vcore_event_init, vcore_event_init)
35
36 #define failmsg(str) write(2,str"\n",sizeof(str"\n")-1)
37
38 void __ros_libc_csu_init(int argc, char **argv, char **envp)
39 {
40         __libc_csu_init(argc, argv, envp);
41         vcore_event_init();
42 }
43
44 void
45 _start(void)
46 {
47         // WARNING: __vcore_self_on_entry must be read before
48         // anything is register-allocated!
49         int id = __vcore_id_on_entry;
50         static int init = 0;
51         // For dynamically-linked programs, the first time through,
52         // __vcore_self_on_entry could be clobbered (on x86), because
53         // the linker will have overwritten eax.  Happily, the first
54         // time through, we know we are vcore 0.  Subsequent entries
55         // into this routine do not have this problem.
56         if(init == 0)
57                 id = 0;
58         
59         // vcore0 when it comes up again, and all threads besides thread 0 must
60         // acquire a TCB.
61         if(init || (id != 0))
62         {
63                 set_tls_desc(__vcore_thread_control_blocks[id],id);
64                 __vcoreid = id;
65                 __vcore_context = TRUE;
66                 vcore_entry();
67                 failmsg("why did vcore_entry() return?");
68                 goto diediedie;
69         }
70
71         init = 1;
72
73         extern int main(int,char**,char**);
74         extern void __libc_csu_init(int,char**,char**);
75         extern void __libc_csu_fini(void);
76         extern void __libc_start_main(typeof(&main),int,char**,
77                       typeof(&__libc_csu_init),
78                       typeof(&__libc_csu_fini),
79                       void*,void*);
80
81         char** argv = (char**)alloca(sizeof(__procinfo.argp));
82         memcpy(argv,__procinfo.argp,sizeof(__procinfo.argp));
83
84         char* argbuf = (char*)alloca(sizeof(__procinfo.argbuf));
85         memcpy(argbuf,__procinfo.argbuf,sizeof(__procinfo.argbuf));
86
87         // touch up pointers, but don't mess up auxp!
88         for(int i = 0, zeros = 0; i < PROCINFO_MAX_ARGP; i++)
89         {
90                 if(argv[i])
91                         argv[i] += argbuf - __procinfo.argbuf;
92                 else if(++zeros == 2)
93                         break;
94         }
95
96         int argc = 0;
97         while(argv[argc])
98                 argc++;
99
100         extern char** _environ;
101         _environ = argv+argc+1;
102
103         __libc_start_main(&main, argc, argv, &__ros_libc_csu_init, &__libc_csu_fini,
104                           0, 0);
105
106         failmsg("why did main() return?");
107
108 diediedie:
109         abort();
110         #ifdef ABORT_INSTRUCTION
111         ABORT_INSTRUCTION;
112         #endif
113         while(1);
114 }