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 <adt/list.h>
|
---|
38 | #include <elf/elf_load.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <loader/pcb.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str.h>
|
---|
44 |
|
---|
45 | #include <rtld/rtld.h>
|
---|
46 | #include <rtld/rtld_debug.h>
|
---|
47 | #include <rtld/dynamic.h>
|
---|
48 | #include <rtld/rtld_arch.h>
|
---|
49 | #include <rtld/module.h>
|
---|
50 |
|
---|
51 | /** Create module for static executable.
|
---|
52 | *
|
---|
53 | * @param rtld Run-time dynamic linker
|
---|
54 | * @param rmodule Place to store pointer to new module or @c NULL
|
---|
55 | * @return EOK on success, ENOMEM if out of memory
|
---|
56 | */
|
---|
57 | errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
|
---|
58 | {
|
---|
59 | module_t *module;
|
---|
60 |
|
---|
61 | module = calloc(1, sizeof(module_t));
|
---|
62 | if (module == NULL)
|
---|
63 | return ENOMEM;
|
---|
64 |
|
---|
65 | module->id = rtld_get_next_id(rtld);
|
---|
66 | module->dyn.soname = "[program]";
|
---|
67 |
|
---|
68 | module->rtld = rtld;
|
---|
69 | module->exec = true;
|
---|
70 | module->local = true;
|
---|
71 |
|
---|
72 | module->tdata = &_tdata_start;
|
---|
73 | module->tdata_size = &_tdata_end - &_tdata_start;
|
---|
74 | module->tbss_size = &_tbss_end - &_tbss_start;
|
---|
75 | module->tls_align = (uintptr_t)&_tls_alignment;
|
---|
76 |
|
---|
77 | list_append(&module->modules_link, &rtld->modules);
|
---|
78 |
|
---|
79 | if (rmodule != NULL)
|
---|
80 | *rmodule = module;
|
---|
81 | return EOK;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** (Eagerly) process all relocation tables in a module.
|
---|
85 | *
|
---|
86 | * Currently works as if LD_BIND_NOW was specified.
|
---|
87 | */
|
---|
88 | void module_process_relocs(module_t *m)
|
---|
89 | {
|
---|
90 | DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
|
---|
91 |
|
---|
92 | /* Do not relocate twice. */
|
---|
93 | if (m->relocated)
|
---|
94 | return;
|
---|
95 |
|
---|
96 | module_process_pre_arch(m);
|
---|
97 |
|
---|
98 | /* jmp_rel table */
|
---|
99 | if (m->dyn.jmp_rel != NULL) {
|
---|
100 | DPRINTF("jmp_rel table\n");
|
---|
101 | if (m->dyn.plt_rel == DT_REL) {
|
---|
102 | DPRINTF("jmp_rel table type DT_REL\n");
|
---|
103 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
104 | } else {
|
---|
105 | assert(m->dyn.plt_rel == DT_RELA);
|
---|
106 | DPRINTF("jmp_rel table type DT_RELA\n");
|
---|
107 | rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* rel table */
|
---|
112 | if (m->dyn.rel != NULL) {
|
---|
113 | DPRINTF("rel table\n");
|
---|
114 | rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* rela table */
|
---|
118 | if (m->dyn.rela != NULL) {
|
---|
119 | DPRINTF("rela table\n");
|
---|
120 | rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
|
---|
121 | }
|
---|
122 |
|
---|
123 | m->relocated = true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Find module structure by soname/pathname.
|
---|
127 | *
|
---|
128 | * Used primarily to see if a module has already been loaded.
|
---|
129 | * Modules are compared according to their soname, i.e. possible
|
---|
130 | * path components are ignored.
|
---|
131 | */
|
---|
132 | module_t *module_find(rtld_t *rtld, const char *name)
|
---|
133 | {
|
---|
134 | const char *p, *soname;
|
---|
135 |
|
---|
136 | DPRINTF("module_find('%s')\n", name);
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * If name contains slashes, treat it as a pathname and
|
---|
140 | * construct soname by chopping off the path. Otherwise
|
---|
141 | * treat it as soname.
|
---|
142 | */
|
---|
143 | p = str_rchr(name, '/');
|
---|
144 | soname = p ? (p + 1) : name;
|
---|
145 |
|
---|
146 | /* Traverse list of all modules. Not extremely fast, but simple */
|
---|
147 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
148 | DPRINTF("m = %p\n", m);
|
---|
149 | if (str_cmp(m->dyn.soname, soname) == 0) {
|
---|
150 | return m; /* Found */
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | return NULL; /* Not found */
|
---|
155 | }
|
---|
156 |
|
---|
157 | #define NAME_BUF_SIZE 64
|
---|
158 |
|
---|
159 | /** Load a module.
|
---|
160 | *
|
---|
161 | * Currently this trivially tries to load '/<name>'.
|
---|
162 | */
|
---|
163 | module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
|
---|
164 | {
|
---|
165 | elf_finfo_t info;
|
---|
166 | char name_buf[NAME_BUF_SIZE];
|
---|
167 | module_t *m;
|
---|
168 | int rc;
|
---|
169 |
|
---|
170 | m = calloc(1, sizeof(module_t));
|
---|
171 | if (m == NULL) {
|
---|
172 | printf("malloc failed\n");
|
---|
173 | exit(1);
|
---|
174 | }
|
---|
175 |
|
---|
176 | m->rtld = rtld;
|
---|
177 | m->id = rtld_get_next_id(rtld);
|
---|
178 |
|
---|
179 | if ((flags & mlf_local) != 0)
|
---|
180 | m->local = true;
|
---|
181 |
|
---|
182 | if (str_size(name) > NAME_BUF_SIZE - 2) {
|
---|
183 | printf("soname too long. increase NAME_BUF_SIZE\n");
|
---|
184 | exit(1);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* Prepend soname with '/lib/' */
|
---|
188 | str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
|
---|
189 | str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
|
---|
190 |
|
---|
191 | /* FIXME: need to real allocation of address space */
|
---|
192 | m->bias = rtld->next_bias;
|
---|
193 | rtld->next_bias += 0x100000;
|
---|
194 |
|
---|
195 | DPRINTF("filename:'%s'\n", name_buf);
|
---|
196 | DPRINTF("load '%s' at 0x%zx\n", name_buf, m->bias);
|
---|
197 |
|
---|
198 | rc = elf_load_file_name(name_buf, m->bias, ELDF_RW, &info);
|
---|
199 | if (rc != EE_OK) {
|
---|
200 | printf("Failed to load '%s'\n", name_buf);
|
---|
201 | exit(1);
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (info.dynamic == NULL) {
|
---|
205 | printf("Error: '%s' is not a dynamically-linked object.\n",
|
---|
206 | name_buf);
|
---|
207 | exit(1);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Pending relocation. */
|
---|
211 | m->relocated = false;
|
---|
212 |
|
---|
213 | DPRINTF("parse dynamic section\n");
|
---|
214 | /* Parse ELF .dynamic section. Store info to m->dyn. */
|
---|
215 | dynamic_parse(info.dynamic, m->bias, &m->dyn);
|
---|
216 |
|
---|
217 | /* Insert into the list of loaded modules */
|
---|
218 | list_append(&m->modules_link, &rtld->modules);
|
---|
219 |
|
---|
220 | /* Copy TLS info */
|
---|
221 | m->tdata = info.tls.tdata;
|
---|
222 | m->tdata_size = info.tls.tdata_size;
|
---|
223 | m->tbss_size = info.tls.tbss_size;
|
---|
224 | m->tls_align = info.tls.tls_align;
|
---|
225 |
|
---|
226 | DPRINTF("tdata at %p size %zu, tbss size %zu\n",
|
---|
227 | m->tdata, m->tdata_size, m->tbss_size);
|
---|
228 |
|
---|
229 | return m;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Load all modules on which m (transitively) depends.
|
---|
233 | */
|
---|
234 | void module_load_deps(module_t *m, mlflags_t flags)
|
---|
235 | {
|
---|
236 | elf_dyn_t *dp;
|
---|
237 | char *dep_name;
|
---|
238 | module_t *dm;
|
---|
239 | size_t n, i;
|
---|
240 |
|
---|
241 | DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
|
---|
242 |
|
---|
243 | /* Count direct dependencies */
|
---|
244 |
|
---|
245 | dp = m->dyn.dynamic;
|
---|
246 | n = 0;
|
---|
247 |
|
---|
248 | while (dp->d_tag != DT_NULL) {
|
---|
249 | if (dp->d_tag == DT_NEEDED)
|
---|
250 | ++n;
|
---|
251 | ++dp;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /* Create an array of pointers to direct dependencies */
|
---|
255 |
|
---|
256 | m->n_deps = n;
|
---|
257 |
|
---|
258 | if (n == 0) {
|
---|
259 | /* There are no dependencies, so we are done. */
|
---|
260 | m->deps = NULL;
|
---|
261 | return;
|
---|
262 | }
|
---|
263 |
|
---|
264 | m->deps = malloc(n * sizeof(module_t *));
|
---|
265 | if (!m->deps) {
|
---|
266 | printf("malloc failed\n");
|
---|
267 | exit(1);
|
---|
268 | }
|
---|
269 |
|
---|
270 | i = 0; /* Current dependency index */
|
---|
271 | dp = m->dyn.dynamic;
|
---|
272 |
|
---|
273 | while (dp->d_tag != DT_NULL) {
|
---|
274 | if (dp->d_tag == DT_NEEDED) {
|
---|
275 | dep_name = m->dyn.str_tab + dp->d_un.d_val;
|
---|
276 |
|
---|
277 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
|
---|
278 | dm = module_find(m->rtld, dep_name);
|
---|
279 | if (!dm) {
|
---|
280 | dm = module_load(m->rtld, dep_name, flags);
|
---|
281 | module_load_deps(dm, flags);
|
---|
282 | }
|
---|
283 |
|
---|
284 | /* Save into deps table */
|
---|
285 | m->deps[i++] = dm;
|
---|
286 | }
|
---|
287 | ++dp;
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** Find module structure by ID. */
|
---|
292 | module_t *module_by_id(rtld_t *rtld, unsigned long id)
|
---|
293 | {
|
---|
294 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
295 | if (m->id == id)
|
---|
296 | return m;
|
---|
297 | }
|
---|
298 |
|
---|
299 | return NULL;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /** Process relocations in modules.
|
---|
303 | *
|
---|
304 | * Processes relocations in @a start and all its dependencies.
|
---|
305 | * Modules that have already been relocated are unaffected.
|
---|
306 | *
|
---|
307 | * @param start The module where to start from.
|
---|
308 | */
|
---|
309 | void modules_process_relocs(rtld_t *rtld, module_t *start)
|
---|
310 | {
|
---|
311 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
312 | /* Skip rtld module, since it has already been processed */
|
---|
313 | if (m != &rtld->rtld) {
|
---|
314 | module_process_relocs(m);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | void modules_process_tls(rtld_t *rtld)
|
---|
320 | {
|
---|
321 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
322 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
323 | m->ioffs = rtld->tls_size;
|
---|
324 | list_append(&m->imodules_link, &rtmd->imodules);
|
---|
325 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
326 | }
|
---|
327 | #else /* CONFIG_TLS_VARIANT_2 */
|
---|
328 | size_t offs;
|
---|
329 |
|
---|
330 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
331 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
332 | }
|
---|
333 |
|
---|
334 | offs = 0;
|
---|
335 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
336 | offs += m->tdata_size + m->tbss_size;
|
---|
337 | m->ioffs = rtld->tls_size - offs;
|
---|
338 | list_append(&m->imodules_link, &rtld->imodules);
|
---|
339 | }
|
---|
340 | #endif
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** Clear BFS tags of all modules.
|
---|
344 | */
|
---|
345 | void modules_untag(rtld_t *rtld)
|
---|
346 | {
|
---|
347 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
348 | m->bfs_tag = false;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** @}
|
---|
353 | */
|
---|