source: mainline/uspace/lib/c/generic/rtld/module.c@ 0aa5c8a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0aa5c8a was a0e2f9c, checked in by Jakub Jermář <jakub@…>, 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
RevLine 
[1ea99cc]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
[b1834a01]29/** @addtogroup rtld
[1ea99cc]30 * @brief
31 * @{
[1b20da0]32 */
[1ea99cc]33/**
34 * @file
35 */
36
[4f205248]37#include <align.h>
[bfdb5af1]38#include <adt/list.h>
39#include <elf/elf_load.h>
[6adb775f]40#include <errno.h>
[bfdb5af1]41#include <loader/pcb.h>
[1ea99cc]42#include <stdio.h>
43#include <stdlib.h>
[1d6dd2a]44#include <str.h>
[4f205248]45#include <macros.h>
[1ea99cc]46
[8a1fb09]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>
[1567471]52#include <libarch/rtld/module.h>
[1ea99cc]53
[2eadda9]54#include "../private/libc.h"
55
[153c7a29]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 */
[b7fd2a0]62errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
[153c7a29]63{
64 module_t *module;
65
66 module = calloc(1, sizeof(module_t));
[bdca26a]67 if (module == NULL) {
68 DPRINTF("malloc failed\n");
[153c7a29]69 return ENOMEM;
[bdca26a]70 }
[153c7a29]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
[2c4e1cc]79 const elf_segment_header_t *tls =
[2eadda9]80 elf_get_phdr(__progsymbols.elfstart, PT_TLS);
[2c4e1cc]81
[4f205248]82 if (tls) {
[2eadda9]83 uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
[4f205248]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 }
[153c7a29]94
95 list_append(&module->modules_link, &rtld->modules);
96
97 if (rmodule != NULL)
98 *rmodule = module;
99 return EOK;
100}
101
[1ea99cc]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. */
[1433ecda]111 if (m->relocated)
112 return;
[1ea99cc]113
114 module_process_pre_arch(m);
115
[634e020]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");
[1ea99cc]121 rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
[634e020]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);
[1ea99cc]126 }
[634e020]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);
[1ea99cc]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 */
[17341d4]150module_t *module_find(rtld_t *rtld, const char *name)
[1ea99cc]151{
[04803bf]152 const char *p, *soname;
[1ea99cc]153
[a6dffb8]154 DPRINTF("module_find('%s')\n", name);
155
[1ea99cc]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 */
[17341d4]165 list_foreach(rtld->modules, modules_link, module_t, m) {
[4b63316]166 DPRINTF("m = %p\n", m);
[1ea99cc]167 if (str_cmp(m->dyn.soname, soname) == 0) {
168 return m; /* Found */
169 }
170 }
[a35b458]171
[1ea99cc]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 */
[5035ba05]181module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
[1ea99cc]182{
[17341d4]183 elf_finfo_t info;
[1ea99cc]184 char name_buf[NAME_BUF_SIZE];
185 module_t *m;
[bdca26a]186 errno_t rc;
[a35b458]187
[6adb775f]188 m = calloc(1, sizeof(module_t));
189 if (m == NULL) {
[bdca26a]190 DPRINTF("malloc failed\n");
[967e7a1]191 goto error;
[1ea99cc]192 }
[a35b458]193
[17341d4]194 m->rtld = rtld;
[6adb775f]195 m->id = rtld_get_next_id(rtld);
[17341d4]196
[5035ba05]197 if ((flags & mlf_local) != 0)
198 m->local = true;
[17341d4]199
[1ea99cc]200 if (str_size(name) > NAME_BUF_SIZE - 2) {
[bdca26a]201 DPRINTF("soname too long. increase NAME_BUF_SIZE\n");
[967e7a1]202 goto error;
[1ea99cc]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
[1567471]211 rc = elf_load_file_name(name_buf, RTLD_MODULE_LDF, &info);
[bdca26a]212 if (rc != EOK) {
213 DPRINTF("Failed to load '%s'\n", name_buf);
[967e7a1]214 goto error;
[1ea99cc]215 }
216
[742fc98e]217 m->bias = elf_get_bias(info.base);
218
219 DPRINTF("loaded '%s' at 0x%zx\n", name_buf, m->bias);
220
[1ea99cc]221 if (info.dynamic == NULL) {
[bdca26a]222 DPRINTF("Error: '%s' is not a dynamically-linked object.\n",
[1ea99cc]223 name_buf);
[967e7a1]224 goto error;
[1ea99cc]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 */
[17341d4]235 list_append(&m->modules_link, &rtld->modules);
[a35b458]236
[6adb775f]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;
[29405ac]241 m->tls_align = info.tls.tls_align;
[a35b458]242
[153c7a29]243 DPRINTF("tdata at %p size %zu, tbss size %zu\n",
[6adb775f]244 m->tdata, m->tdata_size, m->tbss_size);
245
[1ea99cc]246 return m;
[967e7a1]247
248error:
249 if (m)
250 free(m);
251
252 return NULL;
[1ea99cc]253}
254
255/** Load all modules on which m (transitively) depends.
256 */
[967e7a1]257errno_t module_load_deps(module_t *m, mlflags_t flags)
[1ea99cc]258{
259 elf_dyn_t *dp;
260 char *dep_name;
261 module_t *dm;
262 size_t n, i;
263
[a6dffb8]264 DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
265
[1ea99cc]266 /* Count direct dependencies */
[a35b458]267
[1ea99cc]268 dp = m->dyn.dynamic;
269 n = 0;
270
271 while (dp->d_tag != DT_NULL) {
[1433ecda]272 if (dp->d_tag == DT_NEEDED)
273 ++n;
[1ea99cc]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;
[967e7a1]284 return EOK;
[1ea99cc]285 }
286
287 m->deps = malloc(n * sizeof(module_t *));
288 if (!m->deps) {
[bdca26a]289 DPRINTF("malloc failed\n");
[967e7a1]290 return ENOMEM;
[1ea99cc]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);
[17341d4]301 dm = module_find(m->rtld, dep_name);
[1ea99cc]302 if (!dm) {
[5035ba05]303 dm = module_load(m->rtld, dep_name, flags);
[a0e2f9c]304 if (!dm) {
305 return EINVAL;
306 }
307
[967e7a1]308 errno_t rc = module_load_deps(dm, flags);
309 if (rc != EOK) {
310 return rc;
311 }
[1ea99cc]312 }
313
314 /* Save into deps table */
315 m->deps[i++] = dm;
316 }
317 ++dp;
318 }
[967e7a1]319
320 return EOK;
[1ea99cc]321}
322
[6adb775f]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
[1ea99cc]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 */
[17341d4]341void modules_process_relocs(rtld_t *rtld, module_t *start)
[1ea99cc]342{
[17341d4]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) {
[1ea99cc]346 module_process_relocs(m);
347 }
348 }
349}
350
[6adb775f]351void modules_process_tls(rtld_t *rtld)
352{
[29405ac]353#ifdef CONFIG_TLS_VARIANT_1
[4f205248]354 rtld->tls_size = sizeof(tcb_t);
355 rtld->tls_align = _Alignof(tcb_t);
[29405ac]356
357 list_foreach(rtld->modules, modules_link, module_t, m) {
[4f205248]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;
[29405ac]363 rtld->tls_size += m->tdata_size + m->tbss_size;
364 }
365
[4f205248]366#else
367 rtld->tls_size = 0;
368 rtld->tls_align = _Alignof(tcb_t);
369
[29405ac]370 list_foreach(rtld->modules, modules_link, module_t, m) {
[bab0f42]371 list_append(&m->imodules_link, &rtld->imodules);
[4f205248]372 rtld->tls_align = max(rtld->tls_align, m->tls_align);
373
[7c3fb9b]374 /*
375 * We are allocating spans "backwards", here,
[4f205248]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;
[29405ac]381 }
[4f205248]382
[7c3fb9b]383 /*
384 * We are in negative offsets. In order for the alignments to
[4f205248]385 * be correct, "zero" offset (i.e. the total size) must be aligned
386 * to the strictest alignment present.
[40abf56]387 * Note that the padding is actually in front of the TLS data,
388 * not after it.
[4f205248]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);
[29405ac]394#endif
[6adb775f]395}
396
[1ea99cc]397/** Clear BFS tags of all modules.
398 */
[17341d4]399void modules_untag(rtld_t *rtld)
[1ea99cc]400{
[17341d4]401 list_foreach(rtld->modules, modules_link, module_t, m) {
[1ea99cc]402 m->bfs_tag = false;
403 }
404}
405
406/** @}
407 */
Note: See TracBrowser for help on using the repository browser.