/branches/dynload/uspace/lib/rtld/include/arch.h |
---|
37,7 → 37,10 |
#include <rtld.h> |
void module_process_pre_arch(module_t *m); |
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size); |
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size); |
#endif |
/branches/dynload/uspace/lib/rtld/rtld.c |
---|
46,6 → 46,23 |
runtime_env_t runtime_env; |
void module_process_relocs(module_t *m) |
{ |
if (m->dyn.plt_rel == DT_REL) { |
if (m->dyn.rel != NULL) |
rel_table_process(m, m->dyn.rel, m->dyn.rel_sz); |
/* FIXME: this seems wrong */ |
if (m->dyn.jmp_rel != NULL) |
rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz); |
} else { /* (m->dyn.plt_rel == DT_RELA) */ |
printf("table type DT_RELA\n"); |
if (m->dyn.rela != NULL) { |
printf("non-empty\n"); |
rela_table_process(m, m->dyn.rela, m->dyn.rela_sz); |
} |
} |
} |
void _rtld_main(void) |
{ |
pcb_t *pcb; |
56,19 → 73,22 |
int rc; |
printf("Hello, world! (from rtld)\n"); |
getchar(); |
// getchar(); |
/* rtld_dynamic and rtld->bias were filled out by the bootstrap code */ |
rtld = &runtime_env.rtld; |
printf("Parse rtld .dynamic section at 0x%x\n", runtime_env.rtld_dynamic); |
dynamic_parse(runtime_env.rtld_dynamic, rtld->bias, &rtld->dyn); |
// getchar(); |
printf("Parse .dynamic section\n"); |
pcb = __pcb_get(); |
printf("Parse program .dynamic section at 0x%x\n", pcb->dynamic); |
dynamic_parse(pcb->dynamic, 0, &prog.dyn); |
prog.bias = 0; |
prog.dyn.soname = "[program]"; |
printf("Program requested library '%s'\n", prog.dyn.needed); |
// getchar(); |
rc = elf_load_file("/libc.so.0", 0x20000, &lib_info); |
if (rc < 0) { |
85,21 → 105,16 |
/* Parse program's relocation tables */ |
printf("Resolve references in program\n"); |
if (prog.dyn.rel != NULL) |
rel_table_process(&prog, prog.dyn.rel, prog.dyn.rel_sz); |
if (prog.dyn.jmp_rel != NULL) |
rel_table_process(&prog, prog.dyn.jmp_rel, prog.dyn.plt_rel_sz); |
module_process_relocs(&prog); |
/* Parse lib's relocation tables */ |
printf("Resolve references in library\n"); |
if (lib.dyn.rel != NULL) |
rel_table_process(&lib, lib.dyn.rel, lib.dyn.rel_sz); |
if (lib.dyn.jmp_rel != NULL) |
rel_table_process(&lib, lib.dyn.jmp_rel, lib.dyn.plt_rel_sz); |
module_process_relocs(&lib); |
printf("lib.bias=0x%x\n", lib.bias); |
printf("Run program..\n"); |
printf("Run program.. (at 0x%x)\n", (uintptr_t)pcb->entry); |
getchar(); |
pcb->entry(); |
} |
/branches/dynload/uspace/lib/rtld/Makefile |
---|
38,7 → 38,7 |
include arch/$(ARCH)/Makefile.inc |
CFLAGS += -I../../app/iloader/include -Iinclude -fPIC -O0 -ggdb |
LFLAGS = -shared --no-undefined |
LFLAGS += -shared --no-undefined -Bsymbolic |
LIBS = $(LIBC_PREFIX)/libc.pic.a $(SOFTINT_PREFIX)/libsoftint.pic.a |
DEFS += -DRELEASE=\"$(RELEASE)\" |
/branches/dynload/uspace/lib/rtld/arch/ppc32/Makefile.inc |
---|
31,6 → 31,6 |
ARCH_SOURCES := \ |
arch/$(ARCH)/src/bootstrap.c \ |
arch/$(ARCH)/src/runtime.c |
# arch/$(ARCH)/src/reloc.c |
arch/$(ARCH)/src/runtime.c \ |
arch/$(ARCH)/src/reloc.c |
/branches/dynload/uspace/lib/rtld/arch/ppc32/src/reloc.c |
---|
0,0 → 1,238 |
/* |
* Copyright (c) 2008 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup rtld rtld |
* @brief |
* @{ |
*/ |
/** |
* @file |
*/ |
#include <stdio.h> |
#include <arch.h> |
#include <elf_dyn.h> |
#include <symbol.h> |
#include <rtld.h> |
#define __L(ptr) ((uint32_t)(ptr) & 0x0000ffff) |
#define __HA(ptr) ((uint32_t)(ptr) >> 16) |
// ldis r11, .PLTtable@ha |
static inline uint32_t _ldis(unsigned rD, uint16_t imm16) |
{ |
/* Special case of addis: ldis rD,SIMM == addis rD,0,SIMM */ |
return 0x3C000000 | (rD << 21) | imm16; |
} |
static inline uint32_t _lwz(unsigned rD, uint16_t disp16, unsigned rA) |
{ |
return 0x80000000 | (rD << 21) | (rA << 16) | disp16; |
} |
static inline uint32_t _mtctr(unsigned rS) |
{ |
/* mtctr rD == mtspr 9, rD */ |
return 0x7c0003a6 | (rS << 21) | (9/*CTR*/ << 16); |
} |
static inline uint32_t _bctr() |
{ |
/* bcctr 0x1f, 0 */ |
return 0x4c000420 | (0x1f/*always*/ << 21); |
} |
/* branch */ |
static inline uint32_t _b(uint32_t *addr, uint32_t *location) |
{ |
uint32_t raddr = ((uint32_t)addr - (uint32_t)location) & 0x03fffffc; |
return 0x48000000 | raddr; |
} |
/* |
* Fill in PLT |
*/ |
void module_process_pre_arch(module_t *m) |
{ |
uint32_t *plt; |
uint32_t *_plt_ent; |
int i; |
plt = m->dyn.plt_got; |
if (!plt) { |
/* Module has no PLT */ |
return; |
} |
// PLT entries start here. However, each occupies 2 words |
_plt_ent = plt + 18; |
// By definition of the ppc ABI, there's 1:1 correspondence |
// between JMPREL entries and PLT entries |
unsigned plt_n = m->dyn.plt_rel_sz / sizeof(elf_rela_t); |
uint32_t *_plt_table; |
uint32_t *_plt_call; |
uint32_t *_plt_resolve; |
_plt_resolve = plt; |
_plt_call = plt + 6; |
_plt_table = plt + 18 + plt_n; |
/* .PLTcall: */ |
plt[6] = _ldis(11, __HA(_plt_table)); // ldis r11, .PLTtable@ha |
plt[7] = _lwz(11, __L(_plt_table), 11); // lwz r11, .PLTtable@l(r11) |
plt[8] = _mtctr(11); // mtctr r11 |
plt[9] = _bctr(); |
/* .PLTi, i = 0..N-1 */ |
// kputint(-4); |
for (i = 0; i < plt_n; ++i) { |
//_plt_table[i] == function address; |
plt[18+i] = _b(_plt_call, &plt[18+i]); // b .PLTcall |
} |
} |
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size) |
{ |
/* Unused */ |
(void)m; (void)rt; (void)rt_size; |
} |
/** |
* Process (fixup) all relocations in a relocation table. |
*/ |
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size) |
{ |
int i; |
size_t rt_entries; |
size_t r_offset; |
elf_word r_info; |
unsigned rel_type; |
elf_word sym_idx; |
uintptr_t sym_addr; |
uintptr_t r_addend; |
elf_symbol_t *sym_table; |
elf_symbol_t *sym; |
uint32_t *r_ptr; |
char *str_tab; |
elf_symbol_t *sym_def; |
module_t *dest; |
uint32_t *plt; |
uint32_t *_plt_table; |
uint32_t *_plt_ent; |
uint32_t plt_n; |
uint32_t pidx; |
plt = m->dyn.plt_got; |
plt_n = m->dyn.plt_rel_sz / sizeof(elf_rela_t); |
_plt_ent = plt+ 18; |
_plt_table = plt + 18 + plt_n; |
printf("parse relocation table\n"); |
sym_table = m->dyn.sym_tab; |
rt_entries = rt_size / sizeof(elf_rela_t); |
str_tab = m->dyn.str_tab; |
printf("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries); |
for (i = 0; i < rt_entries; ++i) { |
printf("symbol %d: ", i); |
r_offset = rt[i].r_offset; |
r_info = rt[i].r_info; |
r_addend = rt[i].r_addend; |
sym_idx = ELF32_R_SYM(r_info); |
sym = &sym_table[sym_idx]; |
printf("name '%s', value 0x%x, size 0x%x\n", |
str_tab + sym->st_name, |
sym->st_value, |
sym->st_size); |
rel_type = ELF32_R_TYPE(r_info); |
r_ptr = (uint32_t *)(r_offset + m->bias); |
if (sym->st_name != 0) { |
printf("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset); |
sym_def = symbol_def_find(str_tab + sym->st_name, &dest); |
printf("dest name: '%s'\n", dest->dyn.soname); |
printf("dest bias: 0x%x\n", dest->bias); |
if (sym_def) { |
if (sym_def->st_shndx == SHN_ABS) { |
/* Do not add bias to absolute symbols */ |
sym_addr = sym_def->st_value; |
} else { |
sym_addr = sym_def->st_value + dest->bias; |
} |
printf("symbol definition found, addr=0x%x\n", sym_addr); |
} else { |
printf("symbol definition not found\n"); |
continue; |
} |
} |
switch (rel_type) { |
case R_PPC_JMP_SLOT: |
printf("fixup R_PPC_JMP_SLOT (b+v)\n"); |
pidx = (r_ptr - _plt_ent) / 2; |
if (pidx >= plt_n) { |
printf("error: proc index out of range\n"); |
//kputint(0xee00ee0ee00); |
while(1); |
} |
_plt_table[pidx] = sym_addr; |
break; |
case R_PPC_ADDR32: |
printf("fixup R_PPC_ADDR32 (b+v+a)\n"); |
*r_ptr = r_addend + sym_addr; |
break; |
case R_PPC_RELATIVE: |
printf("fixup R_PPC_RELATIVE (b+a)\n"); |
*r_ptr = r_addend + m->bias; |
break; |
case R_PPC_REL24: |
printf("ignore R_PPC_RELATIVE\n"); |
/*TODO*/ |
break; |
} |
} |
} |
/** @} |
*/ |
/branches/dynload/uspace/lib/rtld/arch/ppc32/src/bootstrap.c |
---|
160,7 → 160,7 |
* -fno-jump-tables. |
*/ |
switch (tag) { |
case DT_PLTRELSZ: jmp_rel_entries = dval/8; break; |
case DT_PLTRELSZ: jmp_rel_entries = dval/sizeof(elf_rela_t); break; |
case DT_JMPREL: jmp_rel_table = dptr; break; |
case DT_PLTGOT: |
/* PLT address */ |
167,7 → 167,7 |
plt = dptr; break; |
case DT_SYMTAB: sym_table = dptr; break; |
case DT_RELA: rel_table = dptr; break; |
case DT_RELASZ: rel_entries = dval / 8; break; |
case DT_RELASZ: rel_entries = dval / sizeof(elf_rela_t); break; |
default: break; |
} |
291,13 → 291,13 |
} |
kputint(-5); |
kputint(_plt_table[0]); |
} |
/* This will come in handy */ |
// runtime_env.rtld_dynamic = dynamic; |
// runtime_env.rtld.bias = bias; |
runtime_env.rtld_dynamic = dynamic; |
runtime_env.rtld.bias = bias; |
kputint(_plt_table[0]); |
} |
// volatile int ff=1; |
// while(ff); |
test_func(); |
311,15 → 311,15 |
__io_init(); |
kputint(-1); |
kputint(0x52); |
printf("Hello, world! (from ppc rtld)\n"); |
// printf("Hello, world! (from ppc rtld)\n"); |
kputint(0x62); |
while(1); |
// kputint(34); |
// _rtld_main(); |
// kputint(35); |
// __exit(); |
// while(1); |
kputint(34); |
_rtld_main(); |
kputint(35); |
__exit(); |
// kputint(36); |
kputint(36); |
} |
/** @} |
/branches/dynload/uspace/lib/rtld/arch/ia32/src/reloc.c |
---|
41,6 → 41,12 |
#include <symbol.h> |
#include <rtld.h> |
void module_process_pre_arch(module_t *m) |
{ |
/* Unused */ |
} |
/** |
* Process (fixup) all relocations in a relocation table. |
*/ |
127,5 → 133,11 |
} |
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size) |
{ |
/* Unused */ |
(void)m; (void)rt; (void)rt_size; |
} |
/** @} |
*/ |
/branches/dynload/uspace/lib/libtest/libtest.c |
---|
38,14 → 38,14 |
static void kputint(unsigned i) |
{ |
unsigned dummy; |
asm volatile ( |
"movl $30, %%eax;" |
"int $0x30" |
: "=d" (dummy) /* output - %edx clobbered */ |
: "d" (i) /* input */ |
: "%eax","%ecx" /* all scratch registers clobbered */ |
); |
// unsigned dummy; |
// asm volatile ( |
// "movl $30, %%eax;" |
// "int $0x30" |
// : "=d" (dummy) /* output - %edx clobbered */ |
// : "d" (i) /* input */ |
// : "%eax","%ecx" /* all scratch registers clobbered */ |
// ); |
} |
void test_func(void) |
/branches/dynload/uspace/lib/libc/arch/mips32/Makefile.inc |
---|
31,7 → 31,7 |
TARGET = mipsel-linux-gnu |
TOOLCHAIN_DIR = /usr/local/mipsel/bin |
CFLAGS += -mips3 |
CFLAGS += -mips32 |
-include ../../Makefile.config |
ifeq ($(CONFIG_MIPS_FPU),y) |
/branches/dynload/uspace/lib/libc-shared/_link.ld.in |
---|
File deleted |
/branches/dynload/uspace/lib/libc-shared/Makefile |
---|
67,11 → 67,11 |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm $(OUTPUT).sections Makefile.depend *.o arch/$(ARCH)/_link.ld include/arch _link.ld |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm $(OUTPUT).sections Makefile.depend *.o arch/$(ARCH)/_link.ld include/arch |
find arch/$(ARCH)/ -name '*.o' -follow -exec rm \{\} \; |
$(OUTPUT): $(LIBS) _link.ld |
$(LD) -T _link.ld $(LFLAGS) $(LIBS) -o $@ -Map $(OUTPUT).map |
$(OUTPUT): $(LIBS) arch/$(ARCH)/_link.ld |
$(LD) -T arch/$(ARCH)/_link.ld $(LFLAGS) $(LIBS) -o $@ -Map $(OUTPUT).map |
_link.ld: _link.ld.in |
arch/$(ARCH)/_link.ld: arch/$(ARCH)/_link.ld.in |
$(CC) $(DEFS) $(CFLAGS) -DLIBC_PREFIX=$(LIBC_PREFIX) -E -x c $< | grep -v "^\#" > $@ |
/branches/dynload/uspace/lib/libc-shared/arch/ppc32/_link.ld.in |
---|
0,0 → 1,82 |
ENTRY(__entry) |
PHDRS { |
text PT_LOAD FLAGS(5); |
data PT_LOAD FLAGS(6); |
} |
SECTIONS { |
. = 0x1000; |
.init ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.init); |
} :text |
.text : { |
*(.text); |
*(.text.*); |
*(.rodata*); |
} :text |
.rel.plt ALIGN(0x1000) : { |
*(.rel.plt); |
} |
/* |
*.rel.dyn MUST FOLLOW IMMEDIATELY after .rel.plt |
* without alignment gap or DT_REL will be broken |
*/ |
.rel.dyn : { |
*(.rel.*); |
} :text |
.dynamic ALIGN(0x1000) : { |
*(.dynamic); |
} :text |
.dynsym ALIGN(0x1000) : { |
*(.dynsym); |
} :text |
.dynstr ALIGN(0x1000) : { |
*(.dynstr); |
} :text |
.data ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.data); |
*(.sdata); |
} :data |
.data.rel ALIGN(0x1000) : { |
*(.data.rel); |
} :data |
.got ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.got); |
} :data |
.got.plt ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.got.plt); |
} :data |
.tdata : { |
_tdata_start = .; |
*(.tdata); |
_tdata_end = .; |
} :data |
.tbss : { |
_tbss_start = .; |
*(.tbss); |
_tbss_end = .; |
} :data |
_tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)); |
.bss : { |
*(.sbss); |
*(COMMON); |
*(.bss); |
} :data |
.plt ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.plt); |
} :data |
. = ALIGN(0x1000); |
_heap = .; |
} |
/branches/dynload/uspace/lib/libc-shared/arch/ia32/_link.ld.in |
---|
0,0 → 1,81 |
ENTRY(__entry) |
PHDRS { |
text PT_LOAD FLAGS(5); |
data PT_LOAD FLAGS(6); |
} |
SECTIONS { |
. = 0x1000; |
.init ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.init); |
} :text |
.text : { |
*(.text); |
*(.text.*); |
*(.rodata*); |
} :text |
.rel.plt ALIGN(0x1000) : { |
*(.rel.plt); |
} |
/* |
*.rel.dyn MUST FOLLOW IMMEDIATELY after .rel.plt |
* without alignment gap or DT_REL will be broken |
*/ |
.rel.dyn : { |
*(.rel.*); |
} :text |
.plt ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.plt); |
} :text |
.dynamic ALIGN(0x1000) : { |
*(.dynamic); |
} :text |
.dynsym ALIGN(0x1000) : { |
*(.dynsym); |
} :text |
.dynstr ALIGN(0x1000) : { |
*(.dynstr); |
} :text |
.data ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.data); |
} :data |
.data.rel ALIGN(0x1000) : { |
*(.data.rel); |
} :data |
.got ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.got); |
} :data |
.got.plt ALIGN(0x1000) : SUBALIGN(0x1000) { |
*(.got.plt); |
} :data |
.bss : { |
*(COMMON); |
*(.bss); |
} :data |
.tdata : { |
_tdata_start = .; |
*(.tdata); |
_tdata_end = .; |
} :data |
.tbss : { |
_tbss_start = .; |
*(.tbss); |
_tbss_end = .; |
} :data |
_tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)); |
. = ALIGN(0x1000); |
_heap = .; |
} |