Rev 3690 | Rev 4348 | 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); |
3689 | svoboda | 57 | |
58 | /* Do not relocate twice. */ |
||
59 | if (m->relocated) return; |
||
60 | |||
3869 | svoboda | 61 | module_process_pre_arch(m); |
62 | |||
2999 | svoboda | 63 | if (m->dyn.plt_rel == DT_REL) { |
3869 | svoboda | 64 | DPRINTF("table type DT_REL\n"); |
65 | if (m->dyn.rel != NULL) { |
||
66 | DPRINTF("non-empty\n"); |
||
2999 | svoboda | 67 | rel_table_process(m, m->dyn.rel, m->dyn.rel_sz); |
3869 | svoboda | 68 | } |
2999 | svoboda | 69 | /* FIXME: this seems wrong */ |
3869 | svoboda | 70 | if (m->dyn.jmp_rel != NULL) { |
71 | DPRINTF("table type jmp-rel\n"); |
||
72 | DPRINTF("non-empty\n"); |
||
2999 | svoboda | 73 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz); |
3869 | svoboda | 74 | } |
2999 | svoboda | 75 | } else { /* (m->dyn.plt_rel == DT_RELA) */ |
3562 | svoboda | 76 | DPRINTF("table type DT_RELA\n"); |
2999 | svoboda | 77 | if (m->dyn.rela != NULL) { |
3562 | svoboda | 78 | DPRINTF("non-empty\n"); |
2999 | svoboda | 79 | rela_table_process(m, m->dyn.rela, m->dyn.rela_sz); |
80 | } |
||
81 | } |
||
3689 | svoboda | 82 | |
83 | m->relocated = true; |
||
2999 | svoboda | 84 | } |
85 | |||
3000 | svoboda | 86 | /** Find module structure by soname/pathname. |
2999 | svoboda | 87 | * |
88 | * Used primarily to see if a module has already been loaded. |
||
3000 | svoboda | 89 | * Modules are compared according to their soname, i.e. possible |
90 | * path components are ignored. |
||
2999 | svoboda | 91 | */ |
3000 | svoboda | 92 | module_t *module_find(char *name) |
2999 | svoboda | 93 | { |
3686 | svoboda | 94 | link_t *head = &runtime_env->modules_head; |
2999 | svoboda | 95 | |
96 | link_t *cur; |
||
97 | module_t *m; |
||
3000 | svoboda | 98 | char *p, *soname; |
2999 | svoboda | 99 | |
3000 | svoboda | 100 | /* |
101 | * If name contains slashes, treat it as a pathname and |
||
102 | * construct soname by chopping off the path. Otherwise |
||
103 | * treat it as soname. |
||
104 | */ |
||
105 | p = strrchr(name, '/'); |
||
106 | soname = p ? (p + 1) : name; |
||
3686 | svoboda | 107 | |
3000 | svoboda | 108 | /* Traverse list of all modules. Not extremely fast, but simple */ |
2999 | svoboda | 109 | for (cur = head->next; cur != head; cur = cur->next) { |
110 | m = list_get_instance(cur, module_t, modules_link); |
||
111 | if (strcmp(m->dyn.soname, soname) == 0) { |
||
112 | return m; /* Found */ |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return NULL; /* Not found */ |
||
117 | } |
||
118 | |||
119 | #define NAME_BUF_SIZE 64 |
||
120 | |||
121 | /** Load a module. |
||
122 | * |
||
3000 | svoboda | 123 | * Currently this trivially tries to load '/<name>'. |
2999 | svoboda | 124 | */ |
3000 | svoboda | 125 | module_t *module_load(char *name) |
2999 | svoboda | 126 | { |
127 | elf_info_t info; |
||
128 | char name_buf[NAME_BUF_SIZE]; |
||
129 | module_t *m; |
||
130 | int rc; |
||
131 | |||
132 | m = malloc(sizeof(module_t)); |
||
133 | if (!m) { |
||
134 | printf("malloc failed\n"); |
||
135 | exit(1); |
||
136 | } |
||
137 | |||
3000 | svoboda | 138 | if (strlen(name) > NAME_BUF_SIZE - 2) { |
2999 | svoboda | 139 | printf("soname too long. increase NAME_BUF_SIZE\n"); |
140 | exit(1); |
||
141 | } |
||
142 | |||
3400 | svoboda | 143 | /* Prepend soname with '/lib/' */ |
2999 | svoboda | 144 | name_buf[0] = '/'; |
3400 | svoboda | 145 | strcpy(name_buf, "/lib/"); |
146 | strcpy(name_buf + 5, name); |
||
2999 | svoboda | 147 | |
3689 | svoboda | 148 | /* FIXME: need to real allocation of address space */ |
149 | m->bias = runtime_env->next_bias; |
||
150 | runtime_env->next_bias += 0x100000; |
||
151 | |||
3562 | svoboda | 152 | DPRINTF("filename:'%s'\n", name_buf); |
3869 | svoboda | 153 | printf("load '%s' at 0x%x\n", name_buf, m->bias); |
2999 | svoboda | 154 | |
3552 | svoboda | 155 | rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info); |
3689 | svoboda | 156 | if (rc != EE_OK) { |
3400 | svoboda | 157 | printf("Failed to load '%s'\n", name_buf); |
2999 | svoboda | 158 | exit(1); |
159 | } |
||
160 | |||
3690 | svoboda | 161 | if (info.dynamic == NULL) { |
162 | printf("Error: '%s' is not a dynamically-linked object.\n", |
||
163 | name_buf); |
||
164 | exit(1); |
||
165 | } |
||
166 | |||
3689 | svoboda | 167 | /* Pending relocation. */ |
168 | m->relocated = false; |
||
169 | |||
3562 | svoboda | 170 | DPRINTF("parse dynamic section\n"); |
2999 | svoboda | 171 | /* Parse ELF .dynamic section. Store info to m->dyn. */ |
172 | dynamic_parse(info.dynamic, m->bias, &m->dyn); |
||
173 | |||
174 | /* Insert into the list of loaded modules */ |
||
3686 | svoboda | 175 | list_append(&m->modules_link, &runtime_env->modules_head); |
2999 | svoboda | 176 | |
177 | return m; |
||
178 | } |
||
179 | |||
180 | /** Load all modules on which m (transitively) depends. |
||
181 | */ |
||
182 | void module_load_deps(module_t *m) |
||
183 | { |
||
184 | elf_dyn_t *dp; |
||
3000 | svoboda | 185 | char *dep_name; |
2999 | svoboda | 186 | module_t *dm; |
187 | size_t n, i; |
||
188 | |||
189 | /* Count direct dependencies */ |
||
190 | |||
191 | dp = m->dyn.dynamic; |
||
192 | n = 0; |
||
193 | |||
194 | while (dp->d_tag != DT_NULL) { |
||
195 | if (dp->d_tag == DT_NEEDED) ++n; |
||
196 | ++dp; |
||
197 | } |
||
198 | |||
199 | /* Create an array of pointers to direct dependencies */ |
||
200 | |||
201 | m->n_deps = n; |
||
3553 | svoboda | 202 | |
203 | if (n == 0) { |
||
204 | /* There are no dependencies, so we are done. */ |
||
2999 | svoboda | 205 | m->deps = NULL; |
3553 | svoboda | 206 | return; |
207 | } |
||
2999 | svoboda | 208 | |
3553 | svoboda | 209 | m->deps = malloc(n * sizeof(module_t *)); |
2999 | svoboda | 210 | if (!m->deps) { |
211 | printf("malloc failed\n"); |
||
212 | exit(1); |
||
213 | } |
||
214 | |||
215 | i = 0; /* Current dependency index */ |
||
216 | dp = m->dyn.dynamic; |
||
217 | |||
218 | while (dp->d_tag != DT_NULL) { |
||
219 | if (dp->d_tag == DT_NEEDED) { |
||
3000 | svoboda | 220 | dep_name = m->dyn.str_tab + dp->d_un.d_val; |
2999 | svoboda | 221 | |
3562 | svoboda | 222 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name); |
3000 | svoboda | 223 | dm = module_find(dep_name); |
2999 | svoboda | 224 | if (!dm) { |
3000 | svoboda | 225 | dm = module_load(dep_name); |
2999 | svoboda | 226 | module_load_deps(dm); |
227 | } |
||
228 | |||
229 | /* Save into deps table */ |
||
230 | m->deps[i++] = dm; |
||
231 | } |
||
232 | ++dp; |
||
233 | } |
||
234 | } |
||
235 | |||
3689 | svoboda | 236 | /** Process relocations in modules. |
237 | * |
||
238 | * Processes relocations in @a start and all its dependencies. |
||
239 | * Modules that have already been relocated are unaffected. |
||
240 | * |
||
241 | * @param start The module where to start from. |
||
242 | */ |
||
243 | void modules_process_relocs(module_t *start) |
||
2999 | svoboda | 244 | { |
3686 | svoboda | 245 | link_t *head = &runtime_env->modules_head; |
2999 | svoboda | 246 | |
247 | link_t *cur; |
||
248 | module_t *m; |
||
249 | |||
250 | for (cur = head->next; cur != head; cur = cur->next) { |
||
251 | m = list_get_instance(cur, module_t, modules_link); |
||
252 | |||
253 | /* Skip rtld, since it has already been processed */ |
||
3686 | svoboda | 254 | if (m != &runtime_env->rtld) { |
2999 | svoboda | 255 | module_process_relocs(m); |
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
3000 | svoboda | 260 | /** Clear BFS tags of all modules. |
261 | */ |
||
262 | void modules_untag(void) |
||
263 | { |
||
3686 | svoboda | 264 | link_t *head = &runtime_env->modules_head; |
3000 | svoboda | 265 | |
266 | link_t *cur; |
||
267 | module_t *m; |
||
268 | |||
269 | for (cur = head->next; cur != head; cur = cur->next) { |
||
270 | m = list_get_instance(cur, module_t, modules_link); |
||
271 | m->bfs_tag = false; |
||
272 | } |
||
273 | } |
||
274 | |||
2999 | svoboda | 275 | /** @} |
276 | */ |