HelenOS sources
This source file includes following definitions.
- module_process_pre_arch
- rel_table_process
- rela_table_process
- func_get_addr
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <str.h>
#include <libarch/rtld/elf_dyn.h>
#include <rtld/symbol.h>
#include <rtld/rtld.h>
#include <rtld/rtld_debug.h>
#include <rtld/rtld_arch.h>
void module_process_pre_arch(module_t *m)
{
}
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
{
unsigned i;
size_t rt_entries;
size_t r_offset;
elf_word r_info;
unsigned rel_type;
elf_word sym_idx;
uint32_t sym_addr;
elf_symbol_t *sym_table;
elf_symbol_t *sym;
uint32_t *r_ptr;
uint32_t sym_size;
char *str_tab;
elf_symbol_t *sym_def;
module_t *dest;
DPRINTF("parse relocation table\n");
sym_table = m->dyn.sym_tab;
rt_entries = rt_size / sizeof(elf_rel_t);
str_tab = m->dyn.str_tab;
DPRINTF("address: 0x%" PRIxPTR ", entries: %zd\n", (uintptr_t)rt, rt_entries);
for (i = 0; i < rt_entries; ++i) {
#if 0
DPRINTF("symbol %d: ", i);
#endif
r_offset = rt[i].r_offset;
r_info = rt[i].r_info;
sym_idx = ELF32_R_SYM(r_info);
sym = &sym_table[sym_idx];
#if 0
DPRINTF("name '%s', value 0x%x, size 0x%x\n",
str_tab + sym->st_name, sym->st_value, sym->st_size);
#endif
rel_type = ELF32_R_TYPE(r_info);
r_ptr = (uint32_t *)(r_offset + m->bias);
if (sym->st_name != 0) {
#if 0
DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
#endif
sym_def = symbol_def_find(str_tab + sym->st_name,
m, ssf_none, &dest);
DPRINTF("dest name: '%s'\n", dest->dyn.soname);
DPRINTF("dest bias: 0x%x\n", dest->bias);
if (sym_def) {
sym_addr = (uint32_t)
symbol_get_addr(sym_def, dest, NULL);
#if 0
DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
#endif
} else {
printf("Definition of '%s' not found.\n",
str_tab + sym->st_name);
continue;
}
} else {
sym_addr = 0;
sym_def = NULL;
dest = m;
}
switch (rel_type) {
case R_386_GLOB_DAT:
case R_386_JUMP_SLOT:
DPRINTF("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n");
*r_ptr = sym_addr;
break;
case R_386_32:
DPRINTF("fixup R_386_32 (b+v+a)\n");
*r_ptr += sym_addr;
break;
case R_386_PC32:
DPRINTF("fixup R_386_PC32 (b+v+a-p)\n");
*r_ptr += sym_addr - (uint32_t) r_ptr;
break;
case R_386_COPY:
DPRINTF("fixup R_386_COPY (s)\n");
sym_def = symbol_def_find(str_tab + sym->st_name,
m, ssf_noexec, &dest);
if (sym_def) {
sym_addr = (uint32_t)
symbol_get_addr(sym_def, dest, NULL);
} else {
printf("Source definition of '%s' not found.\n",
str_tab + sym->st_name);
continue;
}
sym_size = sym->st_size;
if (sym_size != sym_def->st_size) {
printf("Warning: Mismatched symbol sizes.\n");
if (sym_size > sym_def->st_size)
sym_size = sym_def->st_size;
}
memcpy(r_ptr, (const void *)sym_addr, sym_size);
break;
case R_386_RELATIVE:
DPRINTF("fixup R_386_RELATIVE (b+a)\n");
*r_ptr += m->bias;
break;
case R_386_TLS_TPOFF:
DPRINTF("fixup R_386_TLS_TPOFF\n");
*r_ptr = sym_def->st_value + dest->tpoff;
break;
case R_386_TLS_DTPOFF32:
DPRINTF("fixup R_386_TLS_DTPOFF32\n");
*r_ptr = sym_def->st_value;
break;
case R_386_TLS_DTPMOD32:
DPRINTF("fixup R_386_TLS_DTPMOD32\n");
*r_ptr = dest->id;
break;
default:
printf("Error: Unknown relocation type %d\n",
rel_type);
exit(1);
}
}
}
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
{
(void)m;
(void)rt;
(void)rt_size;
}
void *func_get_addr(elf_symbol_t *sym, module_t *m)
{
return symbol_get_addr(sym, m, __tcb_get());
}
HelenOS homepage, sources at GitHub