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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ffccdff0 was ffccdff0, checked in by Martin Decky <martin@…>, 5 years ago

Unify alignment handling

Use the C11 alignof() operator. Make sure the allocation alignment is
sufficient.

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