/*
* 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 <module.h>
#include <arch.h>
runtime_env_t runtime_env;
void _rtld_main(void)
{
pcb_t *pcb;
static module_t prog;
module_t *rtld;
printf("Hello, world! (from rtld)\n");
/*
* 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);
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]";
/* 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);
/* Pointer to program module. Used as root of the dependency graph */
runtime_env.program = &prog;
/*
* Now we can continue with loading all other modules.
*/
printf("Load all program dependencies\n");
module_load_deps(&prog);
/*
* 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
);
pcb->entry();
}
/** @}
*/