Rev 3553 | Rev 3686 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2999 | svoboda | 1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
||
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 rtld rtld |
||
30 | * @brief |
||
31 | * @{ |
||
32 | */ |
||
33 | /** |
||
34 | * @file |
||
35 | */ |
||
36 | |||
37 | #include <stdio.h> |
||
38 | #include <stdlib.h> |
||
39 | #include <unistd.h> |
||
40 | #include <fcntl.h> |
||
41 | #include <libadt/list.h> |
||
3400 | svoboda | 42 | #include <loader/pcb.h> |
2999 | svoboda | 43 | |
44 | #include <rtld.h> |
||
45 | #include <dynamic.h> |
||
46 | #include <elf_load.h> |
||
3400 | svoboda | 47 | #include <rtld_arch.h> |
2999 | svoboda | 48 | #include <module.h> |
49 | |||
50 | /** (Eagerly) process all relocation tables in a module. |
||
51 | * |
||
52 | * Currently works as if LD_BIND_NOW was specified. |
||
53 | */ |
||
54 | void module_process_relocs(module_t *m) |
||
55 | { |
||
3562 | svoboda | 56 | DPRINTF("module_process_relocs('%s')\n", m->dyn.soname); |
2999 | svoboda | 57 | if (m->dyn.plt_rel == DT_REL) { |
58 | if (m->dyn.rel != NULL) |
||
59 | rel_table_process(m, m->dyn.rel, m->dyn.rel_sz); |
||
60 | /* FIXME: this seems wrong */ |
||
61 | if (m->dyn.jmp_rel != NULL) |
||
62 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz); |
||
63 | } else { /* (m->dyn.plt_rel == DT_RELA) */ |
||
3562 | svoboda | 64 | DPRINTF("table type DT_RELA\n"); |
2999 | svoboda | 65 | if (m->dyn.rela != NULL) { |
3562 | svoboda | 66 | DPRINTF("non-empty\n"); |
2999 | svoboda | 67 | rela_table_process(m, m->dyn.rela, m->dyn.rela_sz); |
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
3000 | svoboda | 72 | /** Find module structure by soname/pathname. |
2999 | svoboda | 73 | * |
74 | * Used primarily to see if a module has already been loaded. |
||
3000 | svoboda | 75 | * Modules are compared according to their soname, i.e. possible |
76 | * path components are ignored. |
||
2999 | svoboda | 77 | */ |
3000 | svoboda | 78 | module_t *module_find(char *name) |
2999 | svoboda | 79 | { |
80 | link_t *head = &runtime_env.modules_head; |
||
81 | |||
82 | link_t *cur; |
||
83 | module_t *m; |
||
3000 | svoboda | 84 | char *p, *soname; |
2999 | svoboda | 85 | |
3000 | svoboda | 86 | /* |
87 | * If name contains slashes, treat it as a pathname and |
||
88 | * construct soname by chopping off the path. Otherwise |
||
89 | * treat it as soname. |
||
90 | */ |
||
91 | p = strrchr(name, '/'); |
||
92 | soname = p ? (p + 1) : name; |
||
93 | |||
94 | /* Traverse list of all modules. Not extremely fast, but simple */ |
||
2999 | svoboda | 95 | for (cur = head->next; cur != head; cur = cur->next) { |
96 | m = list_get_instance(cur, module_t, modules_link); |
||
97 | if (strcmp(m->dyn.soname, soname) == 0) { |
||
98 | return m; /* Found */ |
||
99 | } |
||
100 | } |
||
101 | |||
102 | return NULL; /* Not found */ |
||
103 | } |
||
104 | |||
105 | #define NAME_BUF_SIZE 64 |
||
106 | |||
107 | /** Load a module. |
||
108 | * |
||
3000 | svoboda | 109 | * Currently this trivially tries to load '/<name>'. |
2999 | svoboda | 110 | */ |
3000 | svoboda | 111 | module_t *module_load(char *name) |
2999 | svoboda | 112 | { |
113 | elf_info_t info; |
||
114 | char name_buf[NAME_BUF_SIZE]; |
||
115 | module_t *m; |
||
116 | int rc; |
||
117 | |||
118 | m = malloc(sizeof(module_t)); |
||
119 | if (!m) { |
||
120 | printf("malloc failed\n"); |
||
121 | exit(1); |
||
122 | } |
||
123 | |||
3000 | svoboda | 124 | if (strlen(name) > NAME_BUF_SIZE - 2) { |
2999 | svoboda | 125 | printf("soname too long. increase NAME_BUF_SIZE\n"); |
126 | exit(1); |
||
127 | } |
||
128 | |||
3400 | svoboda | 129 | /* Prepend soname with '/lib/' */ |
2999 | svoboda | 130 | name_buf[0] = '/'; |
3400 | svoboda | 131 | strcpy(name_buf, "/lib/"); |
132 | strcpy(name_buf + 5, name); |
||
2999 | svoboda | 133 | |
134 | /* FIXME: need to vary bias / allocate address space */ |
||
135 | m->bias = 0x20000; |
||
3562 | svoboda | 136 | DPRINTF("filename:'%s'\n", name_buf); |
2999 | svoboda | 137 | |
3552 | svoboda | 138 | rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info); |
2999 | svoboda | 139 | if (rc < 0) { |
3400 | svoboda | 140 | printf("Failed to load '%s'\n", name_buf); |
2999 | svoboda | 141 | exit(1); |
142 | } |
||
143 | |||
3562 | svoboda | 144 | DPRINTF("parse dynamic section\n"); |
2999 | svoboda | 145 | /* Parse ELF .dynamic section. Store info to m->dyn. */ |
146 | dynamic_parse(info.dynamic, m->bias, &m->dyn); |
||
147 | |||
148 | /* Insert into the list of loaded modules */ |
||
149 | list_append(&m->modules_link, &runtime_env.modules_head); |
||
150 | |||
151 | return m; |
||
152 | } |
||
153 | |||
154 | /** Load all modules on which m (transitively) depends. |
||
155 | */ |
||
156 | void module_load_deps(module_t *m) |
||
157 | { |
||
158 | elf_dyn_t *dp; |
||
3000 | svoboda | 159 | char *dep_name; |
2999 | svoboda | 160 | module_t *dm; |
161 | size_t n, i; |
||
162 | |||
163 | /* Count direct dependencies */ |
||
164 | |||
165 | dp = m->dyn.dynamic; |
||
166 | n = 0; |
||
167 | |||
168 | while (dp->d_tag != DT_NULL) { |
||
169 | if (dp->d_tag == DT_NEEDED) ++n; |
||
170 | ++dp; |
||
171 | } |
||
172 | |||
173 | /* Create an array of pointers to direct dependencies */ |
||
174 | |||
175 | m->n_deps = n; |
||
3553 | svoboda | 176 | |
177 | if (n == 0) { |
||
178 | /* There are no dependencies, so we are done. */ |
||
2999 | svoboda | 179 | m->deps = NULL; |
3553 | svoboda | 180 | return; |
181 | } |
||
2999 | svoboda | 182 | |
3553 | svoboda | 183 | m->deps = malloc(n * sizeof(module_t *)); |
2999 | svoboda | 184 | if (!m->deps) { |
185 | printf("malloc failed\n"); |
||
186 | exit(1); |
||
187 | } |
||
188 | |||
189 | i = 0; /* Current dependency index */ |
||
190 | dp = m->dyn.dynamic; |
||
191 | |||
192 | while (dp->d_tag != DT_NULL) { |
||
193 | if (dp->d_tag == DT_NEEDED) { |
||
3000 | svoboda | 194 | dep_name = m->dyn.str_tab + dp->d_un.d_val; |
2999 | svoboda | 195 | |
3562 | svoboda | 196 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name); |
3000 | svoboda | 197 | dm = module_find(dep_name); |
2999 | svoboda | 198 | if (!dm) { |
3000 | svoboda | 199 | dm = module_load(dep_name); |
2999 | svoboda | 200 | module_load_deps(dm); |
201 | } |
||
202 | |||
203 | /* Save into deps table */ |
||
204 | m->deps[i++] = dm; |
||
205 | } |
||
206 | ++dp; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | void modules_process_relocs(void) |
||
211 | { |
||
212 | link_t *head = &runtime_env.modules_head; |
||
213 | |||
214 | link_t *cur; |
||
215 | module_t *m; |
||
216 | |||
217 | for (cur = head->next; cur != head; cur = cur->next) { |
||
218 | m = list_get_instance(cur, module_t, modules_link); |
||
219 | |||
220 | /* Skip rtld, since it has already been processed */ |
||
221 | if (m != &runtime_env.rtld) { |
||
222 | module_process_relocs(m); |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
3000 | svoboda | 227 | /** Clear BFS tags of all modules. |
228 | */ |
||
229 | void modules_untag(void) |
||
230 | { |
||
231 | link_t *head = &runtime_env.modules_head; |
||
232 | |||
233 | link_t *cur; |
||
234 | module_t *m; |
||
235 | |||
236 | for (cur = head->next; cur != head; cur = cur->next) { |
||
237 | m = list_get_instance(cur, module_t, modules_link); |
||
238 | m->bfs_tag = false; |
||
239 | } |
||
240 | } |
||
241 | |||
2999 | svoboda | 242 | /** @} |
243 | */ |