1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <libc.h>
|
---|
38 | #include <stdbool.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <tls.h>
|
---|
41 | #include <fibril.h>
|
---|
42 | #include <task.h>
|
---|
43 | #include <loader/pcb.h>
|
---|
44 | #include <vfs/vfs.h>
|
---|
45 | #include <vfs/inbox.h>
|
---|
46 | #include "private/libc.h"
|
---|
47 | #include "private/async.h"
|
---|
48 | #include "private/malloc.h"
|
---|
49 | #include "private/io.h"
|
---|
50 | #include "private/fibril.h"
|
---|
51 |
|
---|
52 | #ifdef CONFIG_RTLD
|
---|
53 | #include <rtld/rtld.h>
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | progsymbols_t __progsymbols;
|
---|
57 |
|
---|
58 | static bool env_setup;
|
---|
59 | static fibril_t main_fibril;
|
---|
60 |
|
---|
61 | void __libc_main(void *pcb_ptr)
|
---|
62 | {
|
---|
63 | assert(!__tcb_is_set());
|
---|
64 |
|
---|
65 | __pcb = (pcb_t *) pcb_ptr;
|
---|
66 |
|
---|
67 | if (__pcb) {
|
---|
68 | main_fibril.tcb = __pcb->tcb;
|
---|
69 | } else {
|
---|
70 | /*
|
---|
71 | * Loaded by kernel, not the loader.
|
---|
72 | * Kernel only supports loading fully static binaries,
|
---|
73 | * so we can do basic initialization without worrying about
|
---|
74 | * dynamic libraries.
|
---|
75 | */
|
---|
76 |
|
---|
77 | main_fibril.tcb = tls_make_initial(__progsymbols.elfstart);
|
---|
78 | }
|
---|
79 |
|
---|
80 | assert(main_fibril.tcb);
|
---|
81 |
|
---|
82 | __fibrils_init();
|
---|
83 |
|
---|
84 | /* Initialize the fibril. */
|
---|
85 | main_fibril.tcb->fibril_data = &main_fibril;
|
---|
86 | __tcb_set(main_fibril.tcb);
|
---|
87 | fibril_setup(&main_fibril);
|
---|
88 |
|
---|
89 | /* Initialize user task run-time environment */
|
---|
90 | __malloc_init();
|
---|
91 |
|
---|
92 | #ifdef CONFIG_RTLD
|
---|
93 | if (__pcb != NULL && __pcb->rtld_runtime != NULL) {
|
---|
94 | runtime_env = (rtld_t *) __pcb->rtld_runtime;
|
---|
95 | } else {
|
---|
96 | if (rtld_init_static() != EOK)
|
---|
97 | abort();
|
---|
98 | }
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | __async_server_init();
|
---|
102 | __async_client_init();
|
---|
103 | __async_ports_init();
|
---|
104 |
|
---|
105 | /* The basic run-time environment is setup */
|
---|
106 | env_setup = true;
|
---|
107 |
|
---|
108 | int argc;
|
---|
109 | char **argv;
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Get command line arguments and initialize
|
---|
113 | * standard input and output
|
---|
114 | */
|
---|
115 | if (__pcb == NULL) {
|
---|
116 | argc = 0;
|
---|
117 | argv = NULL;
|
---|
118 | __stdio_init();
|
---|
119 | } else {
|
---|
120 | argc = __pcb->argc;
|
---|
121 | argv = __pcb->argv;
|
---|
122 | __inbox_init(__pcb->inbox, __pcb->inbox_entries);
|
---|
123 | __stdio_init();
|
---|
124 | vfs_root_set(inbox_get("root"));
|
---|
125 | (void) vfs_cwd_set(__pcb->cwd);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * C++ Static constructor calls.
|
---|
130 | */
|
---|
131 |
|
---|
132 | if (__progsymbols.preinit_array) {
|
---|
133 | for (int i = __progsymbols.preinit_array_len - 1; i >= 0; --i)
|
---|
134 | __progsymbols.preinit_array[i]();
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (__progsymbols.init_array) {
|
---|
138 | for (int i = __progsymbols.init_array_len - 1; i >= 0; --i)
|
---|
139 | __progsymbols.init_array[i]();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Run main() and set task return value
|
---|
144 | * according the result
|
---|
145 | */
|
---|
146 | int retval = __progsymbols.main(argc, argv);
|
---|
147 | exit(retval);
|
---|
148 | }
|
---|
149 |
|
---|
150 | void __libc_exit(int status)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * GCC extension __attribute__((destructor)),
|
---|
154 | * C++ destructors are added to __cxa_finalize call
|
---|
155 | * when the respective constructor is called.
|
---|
156 | */
|
---|
157 |
|
---|
158 | for (int i = 0; i < __progsymbols.fini_array_len; ++i)
|
---|
159 | __progsymbols.fini_array[i]();
|
---|
160 |
|
---|
161 | if (env_setup) {
|
---|
162 | __stdio_done();
|
---|
163 | task_retval(status);
|
---|
164 | }
|
---|
165 |
|
---|
166 | __SYSCALL1(SYS_TASK_EXIT, false);
|
---|
167 | __builtin_unreachable();
|
---|
168 | }
|
---|
169 |
|
---|
170 | void __libc_abort(void)
|
---|
171 | {
|
---|
172 | __SYSCALL1(SYS_TASK_EXIT, true);
|
---|
173 | __builtin_unreachable();
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** @}
|
---|
177 | */
|
---|