/*
* 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 <unistd.h>
#include <fcntl.h>
#include <rtld.h>
#include <dynamic.h>
#include <pcb.h>
#include <elf_load.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) {
rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
}
}
}
void _rtld_main(void)
{
pcb_t *pcb;
elf_info_t lib_info;
static module_t prog;
static module_t lib;
module_t *rtld;
int rc;
printf("Hello, world! (from rtld)\n");
// 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();
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) {
printf("failed to load library\n");
return;
}
dynamic_parse(lib_info.dynamic, 0x20000, &lib.dyn);
lib.bias = 0x20000;
printf("lib.bias=0x%x\n", lib.
bias);
runtime_env.program = &prog;
runtime_env.libc = &lib;
/* Parse program's relocation tables */
printf("Resolve references in program\n");
module_process_relocs(&prog);
/* Parse lib's relocation tables */
printf("Resolve references in library\n");
module_process_relocs(&lib);
printf("lib.bias=0x%x\n", lib.
bias);
printf("Run program.. (at 0x%x)\n", (uintptr_t)pcb
->entry
);
pcb->entry();
}
/** @}
*/