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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b83c5e4 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
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 <stdalign.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <str.h>
46#include <macros.h>
47
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>
53#include <libarch/rtld/module.h>
54
55#include "../private/libc.h"
56
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 */
63errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
64{
65 module_t *module;
66
67 module = calloc(1, sizeof(module_t));
68 if (module == NULL) {
69 DPRINTF("malloc failed\n");
70 return ENOMEM;
71 }
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
80 const elf_segment_header_t *tls =
81 elf_get_phdr(__progsymbols.elfstart, PT_TLS);
82
83 if (tls) {
84 uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
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 }
95
96 list_append(&module->modules_link, &rtld->modules);
97
98 if (rmodule != NULL)
99 *rmodule = module;
100 return EOK;
101}
102
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. */
112 if (m->relocated)
113 return;
114
115 module_process_pre_arch(m);
116
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");
122 rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
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);
127 }
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);
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 */
151module_t *module_find(rtld_t *rtld, const char *name)
152{
153 const char *p, *soname;
154
155 DPRINTF("module_find('%s')\n", name);
156
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 */
166 list_foreach(rtld->modules, modules_link, module_t, m) {
167 DPRINTF("m = %p\n", m);
168 if (str_cmp(m->dyn.soname, soname) == 0) {
169 return m; /* Found */
170 }
171 }
172
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 */
182module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
183{
184 elf_finfo_t info;
185 char name_buf[NAME_BUF_SIZE];
186 module_t *m;
187 errno_t rc;
188
189 m = calloc(1, sizeof(module_t));
190 if (m == NULL) {
191 DPRINTF("malloc failed\n");
192 goto error;
193 }
194
195 m->rtld = rtld;
196 m->id = rtld_get_next_id(rtld);
197
198 if ((flags & mlf_local) != 0)
199 m->local = true;
200
201 if (str_size(name) > NAME_BUF_SIZE - 2) {
202 DPRINTF("soname too long. increase NAME_BUF_SIZE\n");
203 goto error;
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
212 rc = elf_load_file_name(name_buf, RTLD_MODULE_LDF, &info);
213 if (rc != EOK) {
214 DPRINTF("Failed to load '%s'\n", name_buf);
215 goto error;
216 }
217
218 m->bias = elf_get_bias(info.base);
219
220 DPRINTF("loaded '%s' at 0x%zx\n", name_buf, m->bias);
221
222 if (info.dynamic == NULL) {
223 DPRINTF("Error: '%s' is not a dynamically-linked object.\n",
224 name_buf);
225 goto error;
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 */
236 list_append(&m->modules_link, &rtld->modules);
237
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;
242 m->tls_align = info.tls.tls_align;
243
244 DPRINTF("tdata at %p size %zu, tbss size %zu\n",
245 m->tdata, m->tdata_size, m->tbss_size);
246
247 return m;
248
249error:
250 if (m)
251 free(m);
252
253 return NULL;
254}
255
256/** Load all modules on which m (transitively) depends.
257 */
258errno_t module_load_deps(module_t *m, mlflags_t flags)
259{
260 elf_dyn_t *dp;
261 char *dep_name;
262 module_t *dm;
263 size_t n, i;
264
265 DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
266
267 /* Count direct dependencies */
268
269 dp = m->dyn.dynamic;
270 n = 0;
271
272 while (dp->d_tag != DT_NULL) {
273 if (dp->d_tag == DT_NEEDED)
274 ++n;
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;
285 return EOK;
286 }
287
288 m->deps = malloc(n * sizeof(module_t *));
289 if (!m->deps) {
290 DPRINTF("malloc failed\n");
291 return ENOMEM;
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);
302 dm = module_find(m->rtld, dep_name);
303 if (!dm) {
304 dm = module_load(m->rtld, dep_name, flags);
305 if (!dm) {
306 return EINVAL;
307 }
308
309 errno_t rc = module_load_deps(dm, flags);
310 if (rc != EOK) {
311 return rc;
312 }
313 }
314
315 /* Save into deps table */
316 m->deps[i++] = dm;
317 }
318 ++dp;
319 }
320
321 return EOK;
322}
323
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
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 */
342void modules_process_relocs(rtld_t *rtld, module_t *start)
343{
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) {
347 module_process_relocs(m);
348 }
349 }
350}
351
352void modules_process_tls(rtld_t *rtld)
353{
354#ifdef CONFIG_TLS_VARIANT_1
355 rtld->tls_size = sizeof(tcb_t);
356 rtld->tls_align = alignof(tcb_t);
357
358 list_foreach(rtld->modules, modules_link, module_t, m) {
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;
364 rtld->tls_size += m->tdata_size + m->tbss_size;
365 }
366
367#else
368 rtld->tls_size = 0;
369 rtld->tls_align = alignof(tcb_t);
370
371 list_foreach(rtld->modules, modules_link, module_t, m) {
372 list_append(&m->imodules_link, &rtld->imodules);
373 rtld->tls_align = max(rtld->tls_align, m->tls_align);
374
375 /*
376 * We are allocating spans "backwards", here,
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;
382 }
383
384 /*
385 * We are in negative offsets. In order for the alignments to
386 * be correct, "zero" offset (i.e. the total size) must be aligned
387 * to the strictest alignment present.
388 * Note that the padding is actually in front of the TLS data,
389 * not after it.
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);
395#endif
396}
397
398/** Clear BFS tags of all modules.
399 */
400void modules_untag(rtld_t *rtld)
401{
402 list_foreach(rtld->modules, modules_link, module_t, m) {
403 m->bfs_tag = false;
404 }
405}
406
407/** @}
408 */
Note: See TracBrowser for help on using the repository browser.