HelenOS sources
This source file includes following definitions.
- module_process_pre_arch
- rel_table_process
- rela_table_process
- fill_plt_entry_generic
- func_get_addr
#include <bitops.h>
#include <mem.h>
#include <smc.h>
#include <stdio.h>
#include <stdlib.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>
static void fill_plt_entry_generic(uint32_t *, uintptr_t);
void module_process_pre_arch(module_t *m)
{
}
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
{
DPRINTF("rel table address: 0x%zx, size: %zd\n", (uintptr_t)rt, rt_size);
(void)m;
(void)rt;
(void)rt_size;
}
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
{
unsigned i;
size_t rt_entries;
size_t r_offset;
size_t r_addend;
elf_xword r_info;
unsigned rel_type;
elf_word sym_idx;
uintptr_t sym_addr;
elf_symbol_t *sym_table;
elf_symbol_t *sym;
uintptr_t *r_ptr;
uintptr_t sym_size;
char *str_tab;
elf_symbol_t *sym_def;
module_t *dest;
uint32_t *plt;
DPRINTF("parse relocation table\n");
sym_table = m->dyn.sym_tab;
rt_entries = rt_size / sizeof(elf_rela_t);
str_tab = m->dyn.str_tab;
plt = (uint32_t *)m->dyn.plt_got;
DPRINTF("rel table address: 0x%zx, 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;
r_addend = rt[i].r_addend;
sym_idx = ELF64_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 = ELF64_R_TYPE(r_info);
r_ptr = (uintptr_t *)(r_offset + m->bias);
if (sym->st_name != 0) {
DPRINTF("rel_type: %x, rel_offset: 0x%zx\n", rel_type, r_offset);
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%zx\n", dest->bias);
if (sym_def) {
sym_addr = (uintptr_t)
symbol_get_addr(sym_def, dest, NULL);
DPRINTF("symbol definition found, value=0x%zx addr=0x%zx\n", sym_def->st_value, sym_addr);
} 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_SPARC_COPY:
DPRINTF("fixup R_SPARC_COPY (s)\n");
sym_def = symbol_def_find(str_tab + sym->st_name,
m, ssf_noexec, &dest);
if (sym_def) {
sym_addr = (uintptr_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);
DPRINTF("OK\n");
break;
case R_SPARC_GLOB_DAT:
DPRINTF("fixup R_SPARC_GLOB_DAT (S+A)\n");
DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_addr);
*r_ptr = sym_addr + r_addend;
DPRINTF("OK\n");
break;
case R_SPARC_JMP_SLOT:
DPRINTF("fixup R_SPARC_JMP_SLOT (S)\n");
DPRINTF("r_offset=0x%zx r_addend=0x%zx\n",
r_offset, r_addend);
sym_def = symbol_def_find(str_tab + sym->st_name,
m, ssf_noexec, &dest);
if (sym_def) {
sym_addr = (uintptr_t)
symbol_get_addr(sym_def, dest, NULL);
} else {
printf("Source definition of '%s' not found.\n",
str_tab + sym->st_name);
continue;
}
DPRINTF("sym_addr = 0x%zx\n", sym_addr);
DPRINTF("r_offset=0x%zx\n", r_offset);
assert((uint32_t *)r_ptr - plt < 32768 * 8);
fill_plt_entry_generic((uint32_t *)r_ptr, sym_addr);
smc_coherence(r_ptr, 32);
DPRINTF("OK\n");
break;
case R_SPARC_RELATIVE:
DPRINTF("fixup R_SPARC_RELATIVE (B+A)\n");
DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, m->bias + r_addend);
*r_ptr = m->bias + r_addend;
DPRINTF("OK\n");
break;
case R_SPARC_64:
DPRINTF("fixup R_SPARC_64 (S+A)\n");
DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_addr);
*r_ptr = sym_addr + r_addend;
DPRINTF("OK\n");
break;
case R_SPARC_TLS_DTPMOD64:
DPRINTF("fixup R_SPARC_TLS_DTPMOD64\n");
DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, (size_t)dest->id);
*r_ptr = dest->id;
DPRINTF("OK\n");
break;
case R_SPARC_TLS_DTPOFF64:
DPRINTF("fixup R_SPARC_TLS_DTPOFF64\n");
DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_def->st_value);
*r_ptr = sym_def->st_value;
DPRINTF("OK\n");
break;
case R_SPARC_TLS_TPOFF64:
DPRINTF("fixup R_SPARC_TLS_TPOFF64\n");
*r_ptr = sym_def->st_value + dest->tpoff;
break;
default:
printf("Error: Unknown relocation type %d\n",
rel_type);
exit(1);
}
}
}
static void fill_plt_entry_generic(uint32_t *plte, uintptr_t ta)
{
uint32_t hh, lm, hm, lo;
hh = BIT_RANGE_EXTRACT(uintptr_t, 63, 42, ta);
hm = BIT_RANGE_EXTRACT(uintptr_t, 41, 32, ta);
lm = BIT_RANGE_EXTRACT(uintptr_t, 31, 10, ta);
lo = BIT_RANGE_EXTRACT(uintptr_t, 9, 0, ta);
plte[0] = 0x01000000;
plte[1] = 0x03000000 | hh;
plte[2] = 0x0b000000 | lm;
plte[3] = 0x82106000 | hm;
plte[4] = 0x83287020;
plte[5] = 0x8a104005;
plte[6] = 0x81c16000 | lo;
plte[7] = 0x01000000;
DPRINTF("Fill PTL entry at %p (target=0x%zx)\n",
plte, ta);
for (unsigned i = 0; i < 8; i++) {
DPRINTF(" - [%d] = 0x%08x\n", i, plte[i]);
}
}
void *func_get_addr(elf_symbol_t *sym, module_t *m)
{
return symbol_get_addr(sym, m, __tcb_get());
}
HelenOS homepage, sources at GitHub