0,0 → 1,251 |
/* |
* 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 <elf_dyn.h> |
#include <rtld.h> |
#include <pcb.h> |
|
void __main(void); |
void __io_init(void); |
void __exit(void); |
|
static void kputint(unsigned i) |
{ |
unsigned dummy; |
asm volatile ( |
"movl $31, %%eax;" |
"int $0x30" |
: "=d" (dummy) /* output - %edx clobbered */ |
: "d" (i) /* input */ |
: "%eax","%ecx" /* all scratch registers clobbered */ |
); |
} |
|
void __bootstrap(void) |
{ |
unsigned bias; |
unsigned *got; |
elf_dyn_t *dynamic; |
void *dptr; |
unsigned dval; |
int i; |
|
size_t rel_entries; |
size_t r_offset; |
elf_word r_info; |
unsigned rel_type; |
elf_word sym_idx; |
uintptr_t sym_addr; |
|
elf_symbol_t *sym_table; |
elf_rel_t *rel_table; |
elf_rel_t *jmp_rel_table; |
size_t jmp_rel_entries; |
pcb_t *pcb; |
|
pcb = __pcb_get(); |
|
/* The program loader (iloader) kindly provided us with these */ |
dynamic = pcb->rtld_dynamic; |
bias = pcb->rtld_bias; |
/* |
asm volatile ( |
// Calculate the bias into %0 |
// Generates "fake" R_386_RELATIVE run-time relocation |
" call .L0;" |
".L0: pop %0;" |
" subl $.L0, %0;" |
|
// Calculate run-time address of _DYNAMIC into %1 |
// Generates "fake" R_386_RELATIVE run-time relocation |
" movl $_DYNAMIC, %1;" // Again, at link time 0-based VMA gets in |
" addl %0, %1;" // Add bias to compute run-time address |
|
: "=r" (bias), "=r" (dynamic) |
); |
*/ |
kputint(bias); |
kputint((unsigned)dynamic); |
|
/* parse DYNAMIC */ |
got = 0; |
sym_table = 0; |
rel_table = 0; |
rel_entries = 0; |
jmp_rel_table = 0; |
jmp_rel_entries = 0; |
|
i = 0; |
while (dynamic[i].d_tag != 0) { |
dptr = (void *)(dynamic[i].d_un.d_val + bias); |
dval = dynamic[i].d_un.d_val; |
|
switch (dynamic[i].d_tag) { |
case DT_PLTRELSZ: jmp_rel_entries = dval/8; break; |
case DT_JMPREL: jmp_rel_table = dptr; break; |
case DT_PLTGOT: |
/* GOT address */ |
got = dptr; break; |
case DT_SYMTAB: sym_table = dptr; break; |
case DT_REL: rel_table = dptr; break; |
case DT_RELSZ: rel_entries = dval / 8; break; |
default: break; |
} |
|
++i; |
} |
|
kputint(1); |
kputint((unsigned)sym_table); |
kputint((unsigned)rel_table); |
kputint((unsigned)rel_entries); |
|
/* Now relocate all our dynsyms */ |
kputint(-1); |
|
for (i=0; i<rel_entries; i++) { |
kputint(i); |
r_offset = rel_table[i].r_offset; |
r_info = rel_table[i].r_info; |
|
rel_type = ELF32_R_TYPE(r_info); |
|
kputint(rel_type); |
kputint(r_offset); |
|
switch (rel_type) { |
case R_386_GLOB_DAT: |
case R_386_JUMP_SLOT: |
kputint(16); |
sym_idx = ELF32_R_SYM(r_info); |
|
sym_addr = sym_table[sym_idx].st_value + bias; |
kputint(sym_idx); |
kputint(sym_addr); |
|
*(unsigned *)(r_offset+bias) = sym_addr; |
break; |
|
case R_386_32: |
kputint(16); |
sym_idx = ELF32_R_SYM(r_info); |
|
sym_addr = sym_table[sym_idx].st_value + bias; |
kputint(sym_idx); |
kputint(sym_addr); |
|
*(unsigned *)(r_offset+bias) += sym_addr; |
break; |
|
case R_386_RELATIVE: |
kputint(16); |
*(unsigned *)(r_offset+bias) += bias; |
break; |
} |
} |
|
kputint(-1); |
|
for (i=0; i<jmp_rel_entries; i++) { |
kputint(i); |
r_offset = jmp_rel_table[i].r_offset; |
r_info = jmp_rel_table[i].r_info; |
|
rel_type = ELF32_R_TYPE(r_info); |
|
kputint(rel_type); |
kputint(r_offset); |
|
switch (rel_type) { |
case R_386_GLOB_DAT: |
case R_386_JUMP_SLOT: |
kputint(16); |
sym_idx = ELF32_R_SYM(r_info); |
|
sym_addr = sym_table[sym_idx].st_value + bias; |
kputint(sym_idx); |
kputint(sym_addr); |
|
*(unsigned *)(r_offset+bias) = sym_addr; |
break; |
|
case R_386_32: |
kputint(16); |
sym_idx = ELF32_R_SYM(r_info); |
|
sym_addr = sym_table[sym_idx].st_value + bias; |
kputint(sym_idx); |
kputint(sym_addr); |
|
*(unsigned *)(r_offset+bias) += sym_addr; |
break; |
|
case R_386_RELATIVE: |
kputint(16); |
*(unsigned *)(r_offset+bias) += bias; |
break; |
} |
} |
|
kputint(-1); |
|
/* This will come in handy */ |
runtime_env.rtld_dynamic = dynamic; |
runtime_env.rtld.bias = bias; |
|
/* Init libc and run rtld main */ |
__main(); |
|
kputint(33); |
__io_init(); |
kputint(34); |
_rtld_main(); |
kputint(35); |
__exit(); |
|
kputint(36); |
|
asm ( |
"movl $250, %%eax;" |
"int $0x30" |
: /* output */ |
: /* input */ |
: "%eax","%ecx","%edx" /* all scratch registers clobbered */ |
); |
} |
|
/** @} |
*/ |