Subversion Repositories HelenOS

Rev

Rev 2983 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

/*
 * 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 _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;
    dynamic_parse(runtime_env.rtld_dynamic, rtld->bias, &rtld->dyn);

    printf("Parse .dynamic section\n");
    pcb = __pcb_get();
    dynamic_parse(pcb->dynamic, 0, &prog.dyn);
    prog.bias = 0;
    prog.dyn.soname = "[program]";

    printf("Program requested library '%s'\n", prog.dyn.needed);
    
    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");
    if (prog.dyn.rel != NULL)
        rel_table_process(&prog, prog.dyn.rel, prog.dyn.rel_sz);
    if (prog.dyn.jmp_rel != NULL)
        rel_table_process(&prog, prog.dyn.jmp_rel, prog.dyn.plt_rel_sz);

    /* Parse lib's relocation tables */
    printf("Resolve references in library\n");
    if (lib.dyn.rel != NULL)
        rel_table_process(&lib, lib.dyn.rel, lib.dyn.rel_sz);
    if (lib.dyn.jmp_rel != NULL)
        rel_table_process(&lib, lib.dyn.jmp_rel, lib.dyn.plt_rel_sz);

    printf("lib.bias=0x%x\n", lib.bias);

    printf("Run program..\n");
    pcb->entry();
}

/** @}
 */