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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5754c31e was 2c4e1cc, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 8 years ago

Define TLS consistently in linker scripts, and remove nonstandard symbols.

Read the program header to find TLS instead.

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