[3eddaff] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Martin Decky
|
---|
[3eddaff] | 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.
|
---|
[b2951e2] | 27 | */
|
---|
[a46da63] | 28 |
|
---|
[4122410] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
[433131d] | 32 |
|
---|
[b2951e2] | 33 | /** @file
|
---|
[433131d] | 34 | */
|
---|
[3eddaff] | 35 |
|
---|
[153c7a29] | 36 | #include <errno.h>
|
---|
[3eddaff] | 37 | #include <libc.h>
|
---|
[76d0981d] | 38 | #include <stdbool.h>
|
---|
[47b7006] | 39 | #include <stdlib.h>
|
---|
[fa23560] | 40 | #include <tls.h>
|
---|
[bc1f1c2] | 41 | #include <fibril.h>
|
---|
[47b7006] | 42 | #include <task.h>
|
---|
[c98e6ee] | 43 | #include <loader/pcb.h>
|
---|
[5126f80] | 44 | #include <vfs/vfs.h>
|
---|
| 45 | #include <vfs/inbox.h>
|
---|
[e26a4633] | 46 | #include "private/libc.h"
|
---|
[fc5f7a8] | 47 | #include "private/async.h"
|
---|
[47b7006] | 48 | #include "private/malloc.h"
|
---|
| 49 | #include "private/io.h"
|
---|
[d73d992] | 50 | #include "private/fibril.h"
|
---|
[d54b303] | 51 |
|
---|
[7fb3f1c] | 52 | #ifdef CONFIG_RTLD
|
---|
[8a1fb09] | 53 | #include <rtld/rtld.h>
|
---|
[7fb3f1c] | 54 | #endif
|
---|
[1ea99cc] | 55 |
|
---|
[2eadda9] | 56 | progsymbols_t __progsymbols;
|
---|
| 57 |
|
---|
[40abf56] | 58 | static bool env_setup;
|
---|
| 59 | static fibril_t main_fibril;
|
---|
[350514c] | 60 |
|
---|
[7148abf] | 61 | void __libc_main(void *pcb_ptr)
|
---|
[a46da63] | 62 | {
|
---|
[40abf56] | 63 | assert(!__tcb_is_set());
|
---|
[a35b458] | 64 |
|
---|
[91e4567] | 65 | __pcb = (pcb_t *) pcb_ptr;
|
---|
[a35b458] | 66 |
|
---|
[40abf56] | 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 |
|
---|
[514d561] | 82 | __fibrils_init();
|
---|
| 83 |
|
---|
[40abf56] | 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 |
|
---|
[91e4567] | 92 | #ifdef CONFIG_RTLD
|
---|
| 93 | if (__pcb != NULL && __pcb->rtld_runtime != NULL) {
|
---|
| 94 | runtime_env = (rtld_t *) __pcb->rtld_runtime;
|
---|
[153c7a29] | 95 | } else {
|
---|
| 96 | if (rtld_init_static() != EOK)
|
---|
| 97 | abort();
|
---|
[91e4567] | 98 | }
|
---|
| 99 | #endif
|
---|
[a35b458] | 100 |
|
---|
[49a796f1] | 101 | __async_server_init();
|
---|
| 102 | __async_client_init();
|
---|
| 103 | __async_ports_init();
|
---|
[a35b458] | 104 |
|
---|
[47b7006] | 105 | /* The basic run-time environment is setup */
|
---|
| 106 | env_setup = true;
|
---|
[a35b458] | 107 |
|
---|
[433131d] | 108 | int argc;
|
---|
| 109 | char **argv;
|
---|
[a35b458] | 110 |
|
---|
[47b7006] | 111 | /*
|
---|
| 112 | * Get command line arguments and initialize
|
---|
| 113 | * standard input and output
|
---|
| 114 | */
|
---|
[c98e6ee] | 115 | if (__pcb == NULL) {
|
---|
| 116 | argc = 0;
|
---|
| 117 | argv = NULL;
|
---|
[bb9ec2d] | 118 | __stdio_init();
|
---|
[c98e6ee] | 119 | } else {
|
---|
| 120 | argc = __pcb->argc;
|
---|
| 121 | argv = __pcb->argv;
|
---|
[bb9ec2d] | 122 | __inbox_init(__pcb->inbox, __pcb->inbox_entries);
|
---|
| 123 | __stdio_init();
|
---|
[5126f80] | 124 | vfs_root_set(inbox_get("root"));
|
---|
[d96d9bc] | 125 | (void) vfs_cwd_set(__pcb->cwd);
|
---|
[c98e6ee] | 126 | }
|
---|
[a35b458] | 127 |
|
---|
[c4049e6] | 128 | /*
|
---|
| 129 | * C++ Static constructor calls.
|
---|
| 130 | */
|
---|
| 131 |
|
---|
[2eadda9] | 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 | }
|
---|
[c4049e6] | 141 |
|
---|
[47b7006] | 142 | /*
|
---|
| 143 | * Run main() and set task return value
|
---|
| 144 | * according the result
|
---|
| 145 | */
|
---|
[2eadda9] | 146 | int retval = __progsymbols.main(argc, argv);
|
---|
[47b7006] | 147 | exit(retval);
|
---|
| 148 | }
|
---|
[7114d83] | 149 |
|
---|
[099c834] | 150 | void __libc_exit(int status)
|
---|
[47b7006] | 151 | {
|
---|
[c4049e6] | 152 | /*
|
---|
| 153 | * GCC extension __attribute__((destructor)),
|
---|
| 154 | * C++ destructors are added to __cxa_finalize call
|
---|
| 155 | * when the respective constructor is called.
|
---|
| 156 | */
|
---|
| 157 |
|
---|
[2eadda9] | 158 | for (int i = 0; i < __progsymbols.fini_array_len; ++i)
|
---|
| 159 | __progsymbols.fini_array[i]();
|
---|
[c4049e6] | 160 |
|
---|
[47b7006] | 161 | if (env_setup) {
|
---|
| 162 | __stdio_done();
|
---|
| 163 | task_retval(status);
|
---|
| 164 | }
|
---|
[a35b458] | 165 |
|
---|
[47b7006] | 166 | __SYSCALL1(SYS_TASK_EXIT, false);
|
---|
[2eadda9] | 167 | __builtin_unreachable();
|
---|
[3eddaff] | 168 | }
|
---|
| 169 |
|
---|
[099c834] | 170 | void __libc_abort(void)
|
---|
[a46da63] | 171 | {
|
---|
[47b7006] | 172 | __SYSCALL1(SYS_TASK_EXIT, true);
|
---|
[2eadda9] | 173 | __builtin_unreachable();
|
---|
[3eddaff] | 174 | }
|
---|
[b2951e2] | 175 |
|
---|
[a46da63] | 176 | /** @}
|
---|
[b2951e2] | 177 | */
|
---|