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

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

Removing printf when failing from lib/rtld

If rtld failed a message was printed with printf.
This has been replaced with DPRINTF which
gives control to the developer over the message

  • 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 errno_t rc = module_load_deps(dm, flags);
305 if (rc != EOK) {
306 return rc;
307 }
308 }
309
310 /* Save into deps table */
311 m->deps[i++] = dm;
312 }
313 ++dp;
314 }
315
316 return EOK;
317}
318
319/** Find module structure by ID. */
320module_t *module_by_id(rtld_t *rtld, unsigned long id)
321{
322 list_foreach(rtld->modules, modules_link, module_t, m) {
323 if (m->id == id)
324 return m;
325 }
326
327 return NULL;
328}
329
330/** Process relocations in modules.
331 *
332 * Processes relocations in @a start and all its dependencies.
333 * Modules that have already been relocated are unaffected.
334 *
335 * @param start The module where to start from.
336 */
337void modules_process_relocs(rtld_t *rtld, module_t *start)
338{
339 list_foreach(rtld->modules, modules_link, module_t, m) {
340 /* Skip rtld module, since it has already been processed */
341 if (m != &rtld->rtld) {
342 module_process_relocs(m);
343 }
344 }
345}
346
347void modules_process_tls(rtld_t *rtld)
348{
349#ifdef CONFIG_TLS_VARIANT_1
350 rtld->tls_size = sizeof(tcb_t);
351 rtld->tls_align = _Alignof(tcb_t);
352
353 list_foreach(rtld->modules, modules_link, module_t, m) {
354 list_append(&m->imodules_link, &rtld->imodules);
355 rtld->tls_align = max(rtld->tls_align, m->tls_align);
356
357 rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
358 m->tpoff = rtld->tls_size;
359 rtld->tls_size += m->tdata_size + m->tbss_size;
360 }
361
362#else
363 rtld->tls_size = 0;
364 rtld->tls_align = _Alignof(tcb_t);
365
366 list_foreach(rtld->modules, modules_link, module_t, m) {
367 list_append(&m->imodules_link, &rtld->imodules);
368 rtld->tls_align = max(rtld->tls_align, m->tls_align);
369
370 /*
371 * We are allocating spans "backwards", here,
372 * as described in U. Drepper's paper.
373 */
374 rtld->tls_size += m->tdata_size + m->tbss_size;
375 rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
376 m->tpoff = -(ptrdiff_t) rtld->tls_size;
377 }
378
379 /*
380 * We are in negative offsets. In order for the alignments to
381 * be correct, "zero" offset (i.e. the total size) must be aligned
382 * to the strictest alignment present.
383 * Note that the padding is actually in front of the TLS data,
384 * not after it.
385 */
386 rtld->tls_size = ALIGN_UP(rtld->tls_size, rtld->tls_align);
387
388 /* Space for the TCB. */
389 rtld->tls_size += sizeof(tcb_t);
390#endif
391}
392
393/** Clear BFS tags of all modules.
394 */
395void modules_untag(rtld_t *rtld)
396{
397 list_foreach(rtld->modules, modules_link, module_t, m) {
398 m->bfs_tag = false;
399 }
400}
401
402/** @}
403 */
Note: See TracBrowser for help on using the repository browser.