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

Last change on this file since 32254d6 was 32254d6, checked in by GitHub <noreply@…>, 5 months ago

init RTLD runtime at load time even for statically linked binaries (#242)

init RTLD runtime at load time even for statically linked binaries

  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[1ea99cc]1/*
[c576800]2 * Copyright (c) 2024 Jiri Svoboda
[1ea99cc]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
[b1834a01]29/** @addtogroup rtld
[1ea99cc]30 * @brief
31 * @{
[1b20da0]32 */
[1ea99cc]33/**
34 * @file
35 */
36
[4f205248]37#include <align.h>
[bfdb5af1]38#include <adt/list.h>
39#include <elf/elf_load.h>
[6adb775f]40#include <errno.h>
[bfdb5af1]41#include <loader/pcb.h>
[ffccdff0]42#include <stdalign.h>
[1ea99cc]43#include <stdio.h>
44#include <stdlib.h>
[1d6dd2a]45#include <str.h>
[4f205248]46#include <macros.h>
[1ea99cc]47
[8a1fb09]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>
[1567471]53#include <libarch/rtld/module.h>
[1ea99cc]54
[2eadda9]55#include "../private/libc.h"
56
[32254d6]57/** Create the "entrypoint" module, of the program executable
[153c7a29]58 *
[32254d6]59 * @param p_info Program ELF file info
[153c7a29]60 * @param rtld Run-time dynamic linker
61 * @param rmodule Place to store pointer to new module or @c NULL
62 * @return EOK on success, ENOMEM if out of memory
63 */
[32254d6]64errno_t module_create_entrypoint(elf_finfo_t *p_info, rtld_t *rtld, module_t **rmodule)
[153c7a29]65{
66 module_t *module;
[32254d6]67 bool is_dynamic = p_info->dynamic != NULL;
68 DPRINTF("module_create_entrypoint\n");
[153c7a29]69
70 module = calloc(1, sizeof(module_t));
[32254d6]71 if (module == NULL)
[153c7a29]72 return ENOMEM;
[32254d6]73
74 uintptr_t bias = elf_get_bias(p_info->base);
75
76 /*
77 * First we need to process dynamic sections of the executable
78 * program and insert it into the module graph.
79 */
80 if (is_dynamic) {
81 DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
82 dynamic_parse(p_info->dynamic, bias, &module->dyn);
83 } else {
84 DPRINTF("Executable is not dynamically linked\n");
[bdca26a]85 }
[153c7a29]86
[32254d6]87 module->bias = bias;
[153c7a29]88 module->id = rtld_get_next_id(rtld);
89 module->dyn.soname = "[program]";
90
91 module->rtld = rtld;
92 module->exec = true;
[32254d6]93 module->local = !is_dynamic;
[153c7a29]94
[32254d6]95 module->tdata = p_info->tls.tdata;
96 module->tdata_size = p_info->tls.tdata_size;
97 module->tbss_size = p_info->tls.tbss_size;
98 module->tls_align = p_info->tls.tls_align;
[2c4e1cc]99
[32254d6]100 DPRINTF("prog tdata at %p size %zu, tbss size %zu\n",
101 module->tdata, module->tdata_size, module->tbss_size);
[153c7a29]102
103 list_append(&module->modules_link, &rtld->modules);
104
105 if (rmodule != NULL)
106 *rmodule = module;
107 return EOK;
108}
109
[1ea99cc]110/** (Eagerly) process all relocation tables in a module.
111 *
112 * Currently works as if LD_BIND_NOW was specified.
113 */
114void module_process_relocs(module_t *m)
115{
116 DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
117
118 /* Do not relocate twice. */
[1433ecda]119 if (m->relocated)
120 return;
[1ea99cc]121
122 module_process_pre_arch(m);
123
[634e020]124 /* jmp_rel table */
125 if (m->dyn.jmp_rel != NULL) {
126 DPRINTF("jmp_rel table\n");
127 if (m->dyn.plt_rel == DT_REL) {
128 DPRINTF("jmp_rel table type DT_REL\n");
[1ea99cc]129 rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
[634e020]130 } else {
131 assert(m->dyn.plt_rel == DT_RELA);
132 DPRINTF("jmp_rel table type DT_RELA\n");
133 rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
[1ea99cc]134 }
[634e020]135 }
136
137 /* rel table */
138 if (m->dyn.rel != NULL) {
139 DPRINTF("rel table\n");
140 rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
141 }
142
143 /* rela table */
144 if (m->dyn.rela != NULL) {
145 DPRINTF("rela table\n");
146 rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
[1ea99cc]147 }
148
149 m->relocated = true;
150}
151
152/** Find module structure by soname/pathname.
153 *
154 * Used primarily to see if a module has already been loaded.
155 * Modules are compared according to their soname, i.e. possible
156 * path components are ignored.
157 */
[17341d4]158module_t *module_find(rtld_t *rtld, const char *name)
[1ea99cc]159{
[04803bf]160 const char *p, *soname;
[1ea99cc]161
[a6dffb8]162 DPRINTF("module_find('%s')\n", name);
163
[1ea99cc]164 /*
165 * If name contains slashes, treat it as a pathname and
166 * construct soname by chopping off the path. Otherwise
167 * treat it as soname.
168 */
169 p = str_rchr(name, '/');
170 soname = p ? (p + 1) : name;
171
172 /* Traverse list of all modules. Not extremely fast, but simple */
[17341d4]173 list_foreach(rtld->modules, modules_link, module_t, m) {
[4b63316]174 DPRINTF("m = %p\n", m);
[1ea99cc]175 if (str_cmp(m->dyn.soname, soname) == 0) {
176 return m; /* Found */
177 }
178 }
[a35b458]179
[1ea99cc]180 return NULL; /* Not found */
181}
182
183#define NAME_BUF_SIZE 64
184
185/** Load a module.
186 *
187 * Currently this trivially tries to load '/<name>'.
188 */
[5035ba05]189module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
[1ea99cc]190{
[17341d4]191 elf_finfo_t info;
[1ea99cc]192 char name_buf[NAME_BUF_SIZE];
193 module_t *m;
[bdca26a]194 errno_t rc;
[a35b458]195
[6adb775f]196 m = calloc(1, sizeof(module_t));
197 if (m == NULL) {
[bdca26a]198 DPRINTF("malloc failed\n");
[967e7a1]199 goto error;
[1ea99cc]200 }
[a35b458]201
[17341d4]202 m->rtld = rtld;
[6adb775f]203 m->id = rtld_get_next_id(rtld);
[17341d4]204
[5035ba05]205 if ((flags & mlf_local) != 0)
206 m->local = true;
[17341d4]207
[1ea99cc]208 if (str_size(name) > NAME_BUF_SIZE - 2) {
[bdca26a]209 DPRINTF("soname too long. increase NAME_BUF_SIZE\n");
[967e7a1]210 goto error;
[1ea99cc]211 }
212
213 /* Prepend soname with '/lib/' */
214 str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
215 str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
216
217 DPRINTF("filename:'%s'\n", name_buf);
218
[1567471]219 rc = elf_load_file_name(name_buf, RTLD_MODULE_LDF, &info);
[bdca26a]220 if (rc != EOK) {
221 DPRINTF("Failed to load '%s'\n", name_buf);
[967e7a1]222 goto error;
[1ea99cc]223 }
224
[742fc98e]225 m->bias = elf_get_bias(info.base);
226
227 DPRINTF("loaded '%s' at 0x%zx\n", name_buf, m->bias);
228
[1ea99cc]229 if (info.dynamic == NULL) {
[bdca26a]230 DPRINTF("Error: '%s' is not a dynamically-linked object.\n",
[1ea99cc]231 name_buf);
[967e7a1]232 goto error;
[1ea99cc]233 }
234
235 /* Pending relocation. */
236 m->relocated = false;
237
238 DPRINTF("parse dynamic section\n");
239 /* Parse ELF .dynamic section. Store info to m->dyn. */
240 dynamic_parse(info.dynamic, m->bias, &m->dyn);
241
242 /* Insert into the list of loaded modules */
[17341d4]243 list_append(&m->modules_link, &rtld->modules);
[a35b458]244
[6adb775f]245 /* Copy TLS info */
246 m->tdata = info.tls.tdata;
247 m->tdata_size = info.tls.tdata_size;
248 m->tbss_size = info.tls.tbss_size;
[29405ac]249 m->tls_align = info.tls.tls_align;
[a35b458]250
[153c7a29]251 DPRINTF("tdata at %p size %zu, tbss size %zu\n",
[6adb775f]252 m->tdata, m->tdata_size, m->tbss_size);
253
[1ea99cc]254 return m;
[967e7a1]255
256error:
257 if (m)
258 free(m);
259
260 return NULL;
[1ea99cc]261}
262
263/** Load all modules on which m (transitively) depends.
264 */
[967e7a1]265errno_t module_load_deps(module_t *m, mlflags_t flags)
[1ea99cc]266{
267 elf_dyn_t *dp;
268 char *dep_name;
269 module_t *dm;
270 size_t n, i;
271
[a6dffb8]272 DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
273
[1ea99cc]274 /* Count direct dependencies */
[a35b458]275
[1ea99cc]276 dp = m->dyn.dynamic;
277 n = 0;
278
279 while (dp->d_tag != DT_NULL) {
[1433ecda]280 if (dp->d_tag == DT_NEEDED)
281 ++n;
[1ea99cc]282 ++dp;
283 }
284
285 /* Create an array of pointers to direct dependencies */
286
287 m->n_deps = n;
288
289 if (n == 0) {
290 /* There are no dependencies, so we are done. */
291 m->deps = NULL;
[967e7a1]292 return EOK;
[1ea99cc]293 }
294
295 m->deps = malloc(n * sizeof(module_t *));
296 if (!m->deps) {
[bdca26a]297 DPRINTF("malloc failed\n");
[967e7a1]298 return ENOMEM;
[1ea99cc]299 }
300
301 i = 0; /* Current dependency index */
302 dp = m->dyn.dynamic;
303
304 while (dp->d_tag != DT_NULL) {
305 if (dp->d_tag == DT_NEEDED) {
306 dep_name = m->dyn.str_tab + dp->d_un.d_val;
307
308 DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
[17341d4]309 dm = module_find(m->rtld, dep_name);
[1ea99cc]310 if (!dm) {
[5035ba05]311 dm = module_load(m->rtld, dep_name, flags);
[a0e2f9c]312 if (!dm) {
313 return EINVAL;
314 }
315
[967e7a1]316 errno_t rc = module_load_deps(dm, flags);
317 if (rc != EOK) {
318 return rc;
319 }
[1ea99cc]320 }
321
322 /* Save into deps table */
323 m->deps[i++] = dm;
324 }
325 ++dp;
326 }
[967e7a1]327
328 return EOK;
[1ea99cc]329}
330
[6adb775f]331/** Find module structure by ID. */
332module_t *module_by_id(rtld_t *rtld, unsigned long id)
333{
334 list_foreach(rtld->modules, modules_link, module_t, m) {
335 if (m->id == id)
336 return m;
337 }
338
339 return NULL;
340}
341
[1ea99cc]342/** Process relocations in modules.
343 *
344 * Processes relocations in @a start and all its dependencies.
345 * Modules that have already been relocated are unaffected.
346 *
347 * @param start The module where to start from.
348 */
[17341d4]349void modules_process_relocs(rtld_t *rtld, module_t *start)
[1ea99cc]350{
[17341d4]351 list_foreach(rtld->modules, modules_link, module_t, m) {
[c576800]352 /*
353 * Skip rtld module, since it has already been processed.
354 * Skip start / main program -- leave it for later
355 */
356 if (m != &rtld->rtld && m != start) {
[1ea99cc]357 module_process_relocs(m);
358 }
359 }
[c576800]360
361 /*
362 * Now that shared libraries have been processed and their variables
363 * are thus initialized, we can process the main program,
364 * which may contain COPY relocations that copy value from shared
365 * library variables to instances of those variables defined
366 * in the main program.
367 */
368 module_process_relocs(start);
[1ea99cc]369}
370
[6adb775f]371void modules_process_tls(rtld_t *rtld)
372{
[29405ac]373#ifdef CONFIG_TLS_VARIANT_1
[4f205248]374 rtld->tls_size = sizeof(tcb_t);
[ffccdff0]375 rtld->tls_align = alignof(tcb_t);
[29405ac]376
377 list_foreach(rtld->modules, modules_link, module_t, m) {
[4f205248]378 list_append(&m->imodules_link, &rtld->imodules);
379 rtld->tls_align = max(rtld->tls_align, m->tls_align);
380
381 rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
382 m->tpoff = rtld->tls_size;
[29405ac]383 rtld->tls_size += m->tdata_size + m->tbss_size;
384 }
385
[4f205248]386#else
387 rtld->tls_size = 0;
[ffccdff0]388 rtld->tls_align = alignof(tcb_t);
[4f205248]389
[29405ac]390 list_foreach(rtld->modules, modules_link, module_t, m) {
[bab0f42]391 list_append(&m->imodules_link, &rtld->imodules);
[4f205248]392 rtld->tls_align = max(rtld->tls_align, m->tls_align);
393
[7c3fb9b]394 /*
395 * We are allocating spans "backwards", here,
[4f205248]396 * as described in U. Drepper's paper.
397 */
398 rtld->tls_size += m->tdata_size + m->tbss_size;
399 rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
400 m->tpoff = -(ptrdiff_t) rtld->tls_size;
[29405ac]401 }
[4f205248]402
[7c3fb9b]403 /*
404 * We are in negative offsets. In order for the alignments to
[4f205248]405 * be correct, "zero" offset (i.e. the total size) must be aligned
406 * to the strictest alignment present.
407 */
408 rtld->tls_size = ALIGN_UP(rtld->tls_size, rtld->tls_align);
409
[b27ae65a]410 /*
411 * Space for the TCB.
412 * Later, the TLS zero offset is equal to the pointer to tcb_t, so
413 * adding the sizeof(tcb_t) block AFTER we calculated the alignment
414 * of the remainder above is correct.
415 */
[4f205248]416 rtld->tls_size += sizeof(tcb_t);
[29405ac]417#endif
[6adb775f]418}
419
[1ea99cc]420/** Clear BFS tags of all modules.
421 */
[17341d4]422void modules_untag(rtld_t *rtld)
[1ea99cc]423{
[17341d4]424 list_foreach(rtld->modules, modules_link, module_t, m) {
[1ea99cc]425 m->bfs_tag = false;
426 }
427}
428
429/** @}
430 */
Note: See TracBrowser for help on using the repository browser.