Rev 4347 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4347 | Rev 4691 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup rtld rtld |
29 | /** @addtogroup rtld rtld |
30 | * @brief |
30 | * @brief |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <stdio.h> |
37 | #include <stdio.h> |
38 | #include <unistd.h> |
38 | #include <unistd.h> |
39 | #include <fcntl.h> |
39 | #include <fcntl.h> |
40 | #include <loader/pcb.h> |
40 | #include <loader/pcb.h> |
- | 41 | #include <ipc/ipc.h> |
|
41 | #include <console.h> |
42 | #include <vfs/vfs.h> |
42 | 43 | ||
43 | /* from librtld */ |
44 | /* from librtld */ |
44 | #include <rtld.h> |
45 | #include <rtld.h> |
45 | #include <dynamic.h> |
46 | #include <dynamic.h> |
46 | #include <elf_load.h> |
47 | #include <elf_load.h> |
47 | #include <module.h> |
48 | #include <module.h> |
48 | 49 | ||
49 | void program_run(void *entry, pcb_t *pcb); |
50 | void program_run(void *entry, pcb_t *pcb); |
50 | 51 | ||
51 | runtime_env_t dload_re; |
52 | runtime_env_t dload_re; |
52 | 53 | ||
53 | int main(int argc, char *argv[]) |
54 | int main(int argc, char *argv[]) |
54 | { |
55 | { |
55 | static module_t prog; |
56 | static module_t prog; |
56 | 57 | ||
57 | runtime_env = &dload_re; |
58 | runtime_env = &dload_re; |
58 | 59 | ||
59 | DPRINTF("Hello, world! (from dload)\n"); |
60 | DPRINTF("Hello, world! (from dload)\n"); |
60 | if (__pcb->dynamic == NULL) { |
61 | if (__pcb->dynamic == NULL) { |
61 | printf("This is the dynamic loader. It is not supposed " |
62 | printf("This is the dynamic loader. It is not supposed " |
62 | "to be executed directly.\n"); |
63 | "to be executed directly.\n"); |
63 | return -1; |
64 | return -1; |
64 | } |
65 | } |
65 | 66 | ||
66 | /* |
67 | /* |
67 | * First we need to process dynamic sections of the executable |
68 | * First we need to process dynamic sections of the executable |
68 | * program and insert it into the module graph. |
69 | * program and insert it into the module graph. |
69 | */ |
70 | */ |
70 | 71 | ||
71 | DPRINTF("Parse program .dynamic section at 0x%x\n", __pcb->dynamic); |
72 | DPRINTF("Parse program .dynamic section at 0x%x\n", __pcb->dynamic); |
72 | dynamic_parse(__pcb->dynamic, 0, &prog.dyn); |
73 | dynamic_parse(__pcb->dynamic, 0, &prog.dyn); |
73 | prog.bias = 0; |
74 | prog.bias = 0; |
74 | prog.dyn.soname = "[program]"; |
75 | prog.dyn.soname = "[program]"; |
75 | 76 | ||
76 | /* Initialize list of loaded modules */ |
77 | /* Initialize list of loaded modules */ |
77 | list_initialize(&runtime_env->modules_head); |
78 | list_initialize(&runtime_env->modules_head); |
78 | list_append(&prog.modules_link, &runtime_env->modules_head); |
79 | list_append(&prog.modules_link, &runtime_env->modules_head); |
79 | 80 | ||
80 | /* Pointer to program module. Used as root of the module graph. */ |
81 | /* Pointer to program module. Used as root of the module graph. */ |
81 | runtime_env->program = &prog; |
82 | runtime_env->program = &prog; |
82 | 83 | ||
83 | /* Work around non-existent memory space allocation. */ |
84 | /* Work around non-existent memory space allocation. */ |
84 | runtime_env->next_bias = 0x1000000; |
85 | runtime_env->next_bias = 0x1000000; |
85 | 86 | ||
86 | /* |
87 | /* |
87 | * Now we can continue with loading all other modules. |
88 | * Now we can continue with loading all other modules. |
88 | */ |
89 | */ |
89 | 90 | ||
90 | DPRINTF("Load all program dependencies\n"); |
91 | DPRINTF("Load all program dependencies\n"); |
91 | module_load_deps(&prog); |
92 | module_load_deps(&prog); |
92 | 93 | ||
93 | /* |
94 | /* |
94 | * Now relocate/link all modules together. |
95 | * Now relocate/link all modules together. |
95 | */ |
96 | */ |
96 | 97 | ||
97 | /* Process relocations in all modules */ |
98 | /* Process relocations in all modules */ |
98 | DPRINTF("Relocate all modules\n"); |
99 | DPRINTF("Relocate all modules\n"); |
99 | modules_process_relocs(&prog); |
100 | modules_process_relocs(&prog); |
100 | 101 | ||
101 | /* Pass runtime evironment pointer through PCB. */ |
102 | /* Pass runtime evironment pointer through PCB. */ |
102 | __pcb->rtld_runtime = (void *) runtime_env; |
103 | __pcb->rtld_runtime = (void *) runtime_env; |
103 | 104 | ||
104 | /* |
105 | /* |
105 | * Finally, run the main program. |
106 | * Finally, run the main program. |
106 | */ |
107 | */ |
107 | DPRINTF("Run program.. (at 0x%lx)\n", (uintptr_t)__pcb->entry); |
108 | DPRINTF("Run program.. (at 0x%lx)\n", (uintptr_t)__pcb->entry); |
108 | 109 | ||
109 | #ifndef RTLD_DEBUG |
110 | #ifndef RTLD_DEBUG |
110 | console_close(); |
111 | ipc_hangup(fphone(stdout)); |
111 | #endif |
112 | #endif |
112 | program_run(__pcb->entry, __pcb); |
113 | program_run(__pcb->entry, __pcb); |
113 | 114 | ||
114 | /* not reached */ |
115 | /* not reached */ |
115 | return 0; |
116 | return 0; |
116 | } |
117 | } |
117 | 118 | ||
118 | /** @} |
119 | /** @} |
119 | */ |
120 | */ |
120 | 121 |