HelenOS sources
This source file includes following definitions.
- module_create_static_exec
- module_process_relocs
- module_find
- module_load
- module_load_deps
- module_by_id
- modules_process_relocs
- modules_process_tls
- modules_untag
#include <align.h>
#include <adt/list.h>
#include <elf/elf_load.h>
#include <errno.h>
#include <loader/pcb.h>
#include <stdalign.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include <macros.h>
#include <rtld/rtld.h>
#include <rtld/rtld_debug.h>
#include <rtld/dynamic.h>
#include <rtld/rtld_arch.h>
#include <rtld/module.h>
#include <libarch/rtld/module.h>
#include "../private/libc.h"
errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
{
module_t *module;
module = calloc(1, sizeof(module_t));
if (module == NULL) {
DPRINTF("malloc failed\n");
return ENOMEM;
}
module->id = rtld_get_next_id(rtld);
module->dyn.soname = "[program]";
module->rtld = rtld;
module->exec = true;
module->local = true;
const elf_segment_header_t *tls =
elf_get_phdr(__progsymbols.elfstart, PT_TLS);
if (tls) {
uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
module->tdata = (void *) (tls->p_vaddr + bias);
module->tdata_size = tls->p_filesz;
module->tbss_size = tls->p_memsz - tls->p_filesz;
module->tls_align = tls->p_align;
} else {
module->tdata = NULL;
module->tdata_size = 0;
module->tbss_size = 0;
module->tls_align = 1;
}
list_append(&module->modules_link, &rtld->modules);
if (rmodule != NULL)
*rmodule = module;
return EOK;
}
void module_process_relocs(module_t *m)
{
DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
if (m->relocated)
return;
module_process_pre_arch(m);
if (m->dyn.jmp_rel != NULL) {
DPRINTF("jmp_rel table\n");
if (m->dyn.plt_rel == DT_REL) {
DPRINTF("jmp_rel table type DT_REL\n");
rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
} else {
assert(m->dyn.plt_rel == DT_RELA);
DPRINTF("jmp_rel table type DT_RELA\n");
rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
}
}
if (m->dyn.rel != NULL) {
DPRINTF("rel table\n");
rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
}
if (m->dyn.rela != NULL) {
DPRINTF("rela table\n");
rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
}
m->relocated = true;
}
module_t *module_find(rtld_t *rtld, const char *name)
{
const char *p, *soname;
DPRINTF("module_find('%s')\n", name);
p = str_rchr(name, '/');
soname = p ? (p + 1) : name;
list_foreach(rtld->modules, modules_link, module_t, m) {
DPRINTF("m = %p\n", m);
if (str_cmp(m->dyn.soname, soname) == 0) {
return m;
}
}
return NULL;
}
#define NAME_BUF_SIZE 64
module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
{
elf_finfo_t info;
char name_buf[NAME_BUF_SIZE];
module_t *m;
errno_t rc;
m = calloc(1, sizeof(module_t));
if (m == NULL) {
DPRINTF("malloc failed\n");
goto error;
}
m->rtld = rtld;
m->id = rtld_get_next_id(rtld);
if ((flags & mlf_local) != 0)
m->local = true;
if (str_size(name) > NAME_BUF_SIZE - 2) {
DPRINTF("soname too long. increase NAME_BUF_SIZE\n");
goto error;
}
str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
DPRINTF("filename:'%s'\n", name_buf);
rc = elf_load_file_name(name_buf, RTLD_MODULE_LDF, &info);
if (rc != EOK) {
DPRINTF("Failed to load '%s'\n", name_buf);
goto error;
}
m->bias = elf_get_bias(info.base);
DPRINTF("loaded '%s' at 0x%zx\n", name_buf, m->bias);
if (info.dynamic == NULL) {
DPRINTF("Error: '%s' is not a dynamically-linked object.\n",
name_buf);
goto error;
}
m->relocated = false;
DPRINTF("parse dynamic section\n");
dynamic_parse(info.dynamic, m->bias, &m->dyn);
list_append(&m->modules_link, &rtld->modules);
m->tdata = info.tls.tdata;
m->tdata_size = info.tls.tdata_size;
m->tbss_size = info.tls.tbss_size;
m->tls_align = info.tls.tls_align;
DPRINTF("tdata at %p size %zu, tbss size %zu\n",
m->tdata, m->tdata_size, m->tbss_size);
return m;
error:
if (m)
free(m);
return NULL;
}
errno_t module_load_deps(module_t *m, mlflags_t flags)
{
elf_dyn_t *dp;
char *dep_name;
module_t *dm;
size_t n, i;
DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
dp = m->dyn.dynamic;
n = 0;
while (dp->d_tag != DT_NULL) {
if (dp->d_tag == DT_NEEDED)
++n;
++dp;
}
m->n_deps = n;
if (n == 0) {
m->deps = NULL;
return EOK;
}
m->deps = malloc(n * sizeof(module_t *));
if (!m->deps) {
DPRINTF("malloc failed\n");
return ENOMEM;
}
i = 0;
dp = m->dyn.dynamic;
while (dp->d_tag != DT_NULL) {
if (dp->d_tag == DT_NEEDED) {
dep_name = m->dyn.str_tab + dp->d_un.d_val;
DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
dm = module_find(m->rtld, dep_name);
if (!dm) {
dm = module_load(m->rtld, dep_name, flags);
if (!dm) {
return EINVAL;
}
errno_t rc = module_load_deps(dm, flags);
if (rc != EOK) {
return rc;
}
}
m->deps[i++] = dm;
}
++dp;
}
return EOK;
}
module_t *module_by_id(rtld_t *rtld, unsigned long id)
{
list_foreach(rtld->modules, modules_link, module_t, m) {
if (m->id == id)
return m;
}
return NULL;
}
void modules_process_relocs(rtld_t *rtld, module_t *start)
{
list_foreach(rtld->modules, modules_link, module_t, m) {
if (m != &rtld->rtld && m != start) {
module_process_relocs(m);
}
}
module_process_relocs(start);
}
void modules_process_tls(rtld_t *rtld)
{
#ifdef CONFIG_TLS_VARIANT_1
rtld->tls_size = sizeof(tcb_t);
rtld->tls_align = alignof(tcb_t);
list_foreach(rtld->modules, modules_link, module_t, m) {
list_append(&m->imodules_link, &rtld->imodules);
rtld->tls_align = max(rtld->tls_align, m->tls_align);
rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
m->tpoff = rtld->tls_size;
rtld->tls_size += m->tdata_size + m->tbss_size;
}
#else
rtld->tls_size = 0;
rtld->tls_align = alignof(tcb_t);
list_foreach(rtld->modules, modules_link, module_t, m) {
list_append(&m->imodules_link, &rtld->imodules);
rtld->tls_align = max(rtld->tls_align, m->tls_align);
rtld->tls_size += m->tdata_size + m->tbss_size;
rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
m->tpoff = -(ptrdiff_t) rtld->tls_size;
}
rtld->tls_size = ALIGN_UP(rtld->tls_size, rtld->tls_align);
rtld->tls_size += sizeof(tcb_t);
#endif
}
void modules_untag(rtld_t *rtld)
{
list_foreach(rtld->modules, modules_link, module_t, m) {
m->bfs_tag = false;
}
}
HelenOS homepage, sources at GitHub