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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 742fc98e was 742fc98e, checked in by GitHub <noreply@…>, 7 years ago

Make some effort to allocate the memory area for shared objects. (#33)

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