0,0 → 1,195 |
/* |
* 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 |
*/ |
|
void _rtld_main(void); |
|
#define ELF32_R_SYM(i) ((i)>>8) |
#define ELF32_R_TYPE(i) ((unsigned char)(i)) |
|
typedef struct { |
int d_tag; |
union { |
unsigned d_val; |
unsigned *d_ptr; |
} d_un; |
} elf32_dyn; |
|
typedef struct { |
unsigned r_offset; |
unsigned r_info; |
} elf32_rel; |
|
typedef struct { |
unsigned st_name; |
unsigned st_value; |
unsigned st_size; |
unsigned char st_info; |
unsigned char st_other; |
unsigned short st_shndx; |
} elf32_sym; |
|
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 */ |
); |
} |
|
void __bootstrap(void) |
{ |
unsigned bias; |
unsigned *got; |
elf32_dyn *dynamic; |
unsigned *dptr; |
unsigned dval; |
int i; |
|
unsigned rel_entries; |
unsigned r_offset; |
unsigned r_info; |
unsigned rel_type; |
unsigned sym_idx; |
unsigned sym_addr; |
|
elf32_sym *sym_table; |
elf32_rel *rel_table; |
|
asm volatile ( |
"movl $30, %eax;" |
"int $0x30" |
); |
|
/* Copied from libc/arch/ia32/entry.s */ |
/*asm volatile ( |
"mov %ss, %ax;" |
"mov %ax, %ds;" |
"mov %ax, %es;" |
"mov %ax, %fs;" |
);*/ |
|
|
asm volatile ( |
/* Calculate the bias into %0 */ |
" call .L0;" |
".L0: pop %0;" |
" subl $.L0, %0;" |
|
/* Calculate run-time address of _DYNAMIC into %1 */ |
" 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; |
|
i = 0; |
while (dynamic[i].d_tag != 0) { |
dptr = (unsigned *)(dynamic[i].d_un.d_val + bias); |
dval = dynamic[i].d_un.d_val; |
|
switch (dynamic[i].d_tag) { |
case 3 /* DT_PLTGOT */: |
/* GOT address */ |
got = dptr; break; |
case 6 /* DT_SYMTAB */ : sym_table = dptr; break; |
case 17 /* DT_REL */ : rel_table = dptr; break; |
case 18 /* 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); |
|
if (rel_type == 7 /* 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; |
} |
} |
|
kputint(-1); |
|
kputint(32); |
|
_rtld_main(); |
|
asm ( |
"movl $250, %%eax;" |
"int $0x30" |
: /* output */ |
: /* input */ |
: "%eax","%ecx","%edx" /* all scratch registers clobbered */ |
); |
} |
|
/** @} |
*/ |