source: mainline/uspace/lib/c/generic/rtld/module.c@ 859d006

Last change on this file since 859d006 was 859d006, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Correcting return handling of rtld/module_load()

Because of the previous commit 985e0f158464dec99e0760a13f626453991d6c60
the function can now return NULL in case of a failure. This
commit add a missing NULL test to module_load_deps()

  • Property mode set to 100644
File size: 9.6 KB
Line 
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
30 * @brief
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <align.h>
38#include <adt/list.h>
39#include <elf/elf_load.h>
40#include <errno.h>
41#include <loader/pcb.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <str.h>
45#include <macros.h>
46
47#include <rtld/rtld.h>
48#include <rtld/rtld_debug.h>
49#include <rtld/dynamic.h>
50#include <rtld/rtld_arch.h>
51#include <rtld/module.h>
52#include <libarch/rtld/module.h>
53
54#include "../private/libc.h"
55
56/** Create module for static executable.
57 *
58 * @param rtld Run-time dynamic linker
59 * @param rmodule Place to store pointer to new module or @c NULL
60 * @return EOK on success, ENOMEM if out of memory
61 */
62errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
63{
64 module_t *module;
65
66 module = calloc(1, sizeof(module_t));
67 if (module == NULL) {
68 DPRINTF("malloc failed\n");
69 return ENOMEM;
70 }
71
72 module->id = rtld_get_next_id(rtld);
73 module->dyn.soname = "[program]";
74
75 module->rtld = rtld;
76 module->exec = true;
77 module->local = true;
78
79 const elf_segment_header_t *tls =
80 elf_get_phdr(__progsymbols.elfstart, PT_TLS);
81
82 if (tls) {
83 uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
84 module->tdata = (void *) (tls->p_vaddr + bias);
85 module->tdata_size = tls->p_filesz;
86 module->tbss_size = tls->p_memsz - tls->p_filesz;
87 module->tls_align = tls->p_align;
88 } else {
89 module->tdata = NULL;
90 module->tdata_size = 0;
91 module->tbss_size = 0;
92 module->tls_align = 1;
93 }
94
95 list_append(&module->modules_link, &rtld->modules);
96
97 if (rmodule != NULL)
98 *rmodule = module;
99 return EOK;
100}
101
102/** (Eagerly) process all relocation tables in a module.
103 *
104 * Currently works as if LD_BIND_NOW was specified.
105 */
106void module_process_relocs(module_t *m)
107{
108 DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
109
110 /* Do not relocate twice. */
111 if (m->relocated)
112 return;
113
114 module_process_pre_arch(m);
115
116 /* jmp_rel table */
117 if (m->dyn.jmp_rel != NULL) {
118 DPRINTF("jmp_rel table\n");
119 if (m->dyn.plt_rel == DT_REL) {
120 DPRINTF("jmp_rel table type DT_REL\n");
121 rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
122 } else {
123 assert(m->dyn.plt_rel == DT_RELA);
124 DPRINTF("jmp_rel table type DT_RELA\n");
125 rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
126 }
127 }
128
129 /* rel table */
130 if (m->dyn.rel != NULL) {
131 DPRINTF("rel table\n");
132 rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
133 }
134
135 /* rela table */
136 if (m->dyn.rela != NULL) {
137 DPRINTF("rela table\n");
138 rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
139 }
140
141 m->relocated = true;
142}
143
144/** Find module structure by soname/pathname.
145 *
146 * Used primarily to see if a module has already been loaded.
147 * Modules are compared according to their soname, i.e. possible
148 * path components are ignored.
149 */
150module_t *module_find(rtld_t *rtld, const char *name)
151{
152 const char *p, *soname;
153
154 DPRINTF("module_find('%s')\n", name);
155
156 /*
157 * If name contains slashes, treat it as a pathname and
158 * construct soname by chopping off the path. Otherwise
159 * treat it as soname.
160 */
161 p = str_rchr(name, '/');
162 soname = p ? (p + 1) : name;
163
164 /* Traverse list of all modules. Not extremely fast, but simple */
165 list_foreach(rtld->modules, modules_link, module_t, m) {
166 DPRINTF("m = %p\n", m);
167 if (str_cmp(m->dyn.soname, soname) == 0) {
168 return m; /* Found */
169 }
170 }
171
172 return NULL; /* Not found */
173}
174
175#define NAME_BUF_SIZE 64
176
177/** Load a module.
178 *
179 * Currently this trivially tries to load '/<name>'.
180 */
181module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
182{
183 elf_finfo_t info;
184 char name_buf[NAME_BUF_SIZE];
185 module_t *m;
186 errno_t rc;
187
188 m = calloc(1, sizeof(module_t));
189 if (m == NULL) {
190 DPRINTF("malloc failed\n");
191 goto error;
192 }
193
194 m->rtld = rtld;
195 m->id = rtld_get_next_id(rtld);
196
197 if ((flags & mlf_local) != 0)
198 m->local = true;
199
200 if (str_size(name) > NAME_BUF_SIZE - 2) {
201 DPRINTF("soname too long. increase NAME_BUF_SIZE\n");
202 goto error;
203 }
204
205 /* Prepend soname with '/lib/' */
206 str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
207 str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
208
209 DPRINTF("filename:'%s'\n", name_buf);
210
211 rc = elf_load_file_name(name_buf, RTLD_MODULE_LDF, &info);
212 if (rc != EOK) {
213 DPRINTF("Failed to load '%s'\n", name_buf);
214 goto error;
215 }
216
217 m->bias = elf_get_bias(info.base);
218
219 DPRINTF("loaded '%s' at 0x%zx\n", name_buf, m->bias);
220
221 if (info.dynamic == NULL) {
222 DPRINTF("Error: '%s' is not a dynamically-linked object.\n",
223 name_buf);
224 goto error;
225 }
226
227 /* Pending relocation. */
228 m->relocated = false;
229
230 DPRINTF("parse dynamic section\n");
231 /* Parse ELF .dynamic section. Store info to m->dyn. */
232 dynamic_parse(info.dynamic, m->bias, &m->dyn);
233
234 /* Insert into the list of loaded modules */
235 list_append(&m->modules_link, &rtld->modules);
236
237 /* Copy TLS info */
238 m->tdata = info.tls.tdata;
239 m->tdata_size = info.tls.tdata_size;
240 m->tbss_size = info.tls.tbss_size;
241 m->tls_align = info.tls.tls_align;
242
243 DPRINTF("tdata at %p size %zu, tbss size %zu\n",
244 m->tdata, m->tdata_size, m->tbss_size);
245
246 return m;
247
248error:
249 if (m)
250 free(m);
251
252 return NULL;
253}
254
255/** Load all modules on which m (transitively) depends.
256 */
257errno_t module_load_deps(module_t *m, mlflags_t flags)
258{
259 elf_dyn_t *dp;
260 char *dep_name;
261 module_t *dm;
262 size_t n, i;
263
264 DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
265
266 /* Count direct dependencies */
267
268 dp = m->dyn.dynamic;
269 n = 0;
270
271 while (dp->d_tag != DT_NULL) {
272 if (dp->d_tag == DT_NEEDED)
273 ++n;
274 ++dp;
275 }
276
277 /* Create an array of pointers to direct dependencies */
278
279 m->n_deps = n;
280
281 if (n == 0) {
282 /* There are no dependencies, so we are done. */
283 m->deps = NULL;
284 return EOK;
285 }
286
287 m->deps = malloc(n * sizeof(module_t *));
288 if (!m->deps) {
289 DPRINTF("malloc failed\n");
290 return ENOMEM;
291 }
292
293 i = 0; /* Current dependency index */
294 dp = m->dyn.dynamic;
295
296 while (dp->d_tag != DT_NULL) {
297 if (dp->d_tag == DT_NEEDED) {
298 dep_name = m->dyn.str_tab + dp->d_un.d_val;
299
300 DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
301 dm = module_find(m->rtld, dep_name);
302 if (!dm) {
303 dm = module_load(m->rtld, dep_name, flags);
304 if (!dm) {
305 return EINVAL;
306 }
307
308 errno_t rc = module_load_deps(dm, flags);
309 if (rc != EOK) {
310 return rc;
311 }
312 }
313
314 /* Save into deps table */
315 m->deps[i++] = dm;
316 }
317 ++dp;
318 }
319
320 return EOK;
321}
322
323/** Find module structure by ID. */
324module_t *module_by_id(rtld_t *rtld, unsigned long id)
325{
326 list_foreach(rtld->modules, modules_link, module_t, m) {
327 if (m->id == id)
328 return m;
329 }
330
331 return NULL;
332}
333
334/** Process relocations in modules.
335 *
336 * Processes relocations in @a start and all its dependencies.
337 * Modules that have already been relocated are unaffected.
338 *
339 * @param start The module where to start from.
340 */
341void modules_process_relocs(rtld_t *rtld, module_t *start)
342{
343 list_foreach(rtld->modules, modules_link, module_t, m) {
344 /* Skip rtld module, since it has already been processed */
345 if (m != &rtld->rtld) {
346 module_process_relocs(m);
347 }
348 }
349}
350
351void modules_process_tls(rtld_t *rtld)
352{
353#ifdef CONFIG_TLS_VARIANT_1
354 rtld->tls_size = sizeof(tcb_t);
355 rtld->tls_align = _Alignof(tcb_t);
356
357 list_foreach(rtld->modules, modules_link, module_t, m) {
358 list_append(&m->imodules_link, &rtld->imodules);
359 rtld->tls_align = max(rtld->tls_align, m->tls_align);
360
361 rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
362 m->tpoff = rtld->tls_size;
363 rtld->tls_size += m->tdata_size + m->tbss_size;
364 }
365
366#else
367 rtld->tls_size = 0;
368 rtld->tls_align = _Alignof(tcb_t);
369
370 list_foreach(rtld->modules, modules_link, module_t, m) {
371 list_append(&m->imodules_link, &rtld->imodules);
372 rtld->tls_align = max(rtld->tls_align, m->tls_align);
373
374 /*
375 * We are allocating spans "backwards", here,
376 * as described in U. Drepper's paper.
377 */
378 rtld->tls_size += m->tdata_size + m->tbss_size;
379 rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
380 m->tpoff = -(ptrdiff_t) rtld->tls_size;
381 }
382
383 /*
384 * We are in negative offsets. In order for the alignments to
385 * be correct, "zero" offset (i.e. the total size) must be aligned
386 * to the strictest alignment present.
387 * Note that the padding is actually in front of the TLS data,
388 * not after it.
389 */
390 rtld->tls_size = ALIGN_UP(rtld->tls_size, rtld->tls_align);
391
392 /* Space for the TCB. */
393 rtld->tls_size += sizeof(tcb_t);
394#endif
395}
396
397/** Clear BFS tags of all modules.
398 */
399void modules_untag(rtld_t *rtld)
400{
401 list_foreach(rtld->modules, modules_link, module_t, m) {
402 m->bfs_tag = false;
403 }
404}
405
406/** @}
407 */
Note: See TracBrowser for help on using the repository browser.