42,27 → 42,11 |
#include <dynamic.h> |
#include <pcb.h> |
#include <elf_load.h> |
#include <module.h> |
#include <arch.h> |
|
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; |
73,13 → 57,17 |
int rc; |
|
printf("Hello, world! (from rtld)\n"); |
// getchar(); |
|
/* |
* First we need to process dynamic sections of the two modules |
* that have been already loaded, that is, of ourselves and of |
* the executable program. |
*/ |
|
/* 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(); |
|
pcb = __pcb_get(); |
printf("Parse program .dynamic section at 0x%x\n", pcb->dynamic); |
87,34 → 75,33 |
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) { |
printf("failed to load library\n"); |
return; |
} |
/* Initialize list of loaded modules */ |
list_initialize(&runtime_env.modules_head); |
list_append(&prog.modules_link, &runtime_env.modules_head); |
list_append(&rtld->modules_link, &runtime_env.modules_head); |
|
dynamic_parse(lib_info.dynamic, 0x20000, &lib.dyn); |
lib.bias = 0x20000; |
printf("lib.bias=0x%x\n", lib.bias); |
|
/* Pointer to program module. Used as root of the dependency graph */ |
runtime_env.program = &prog; |
runtime_env.libc = &lib; |
|
/* Parse program's relocation tables */ |
printf("Resolve references in program\n"); |
module_process_relocs(&prog); |
/* |
* Now we can continue with loading all other modules. |
*/ |
|
/* Parse lib's relocation tables */ |
printf("Resolve references in library\n"); |
module_process_relocs(&lib); |
printf("Load all program dependencies\n"); |
module_load_deps(&prog); |
|
printf("lib.bias=0x%x\n", lib.bias); |
/* |
* Now relocate/link all modules together. |
*/ |
|
/* Process relocations in all modules */ |
printf("Relocate all modules\n"); |
modules_process_relocs(); |
|
/* |
* Finally, run the main program. |
*/ |
printf("Run program.. (at 0x%x)\n", (uintptr_t)pcb->entry); |
getchar(); |
pcb->entry(); |
} |
|