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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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