[1ea99cc] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup rtld rtld
|
---|
| 30 | * @brief
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[17341d4] | 37 | #include <errno.h>
|
---|
| 38 | #include <rtld/module.h>
|
---|
[8a1fb09] | 39 | #include <rtld/rtld.h>
|
---|
[17341d4] | 40 | #include <rtld/rtld_debug.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
[1ea99cc] | 42 |
|
---|
[17341d4] | 43 | rtld_t *runtime_env;
|
---|
| 44 | static rtld_t rt_env_static;
|
---|
| 45 | static module_t prog_mod;
|
---|
[1ea99cc] | 46 |
|
---|
[17341d4] | 47 | /** Initialize the runtime linker for use in a statically-linked executable. */
|
---|
[1ea99cc] | 48 | void rtld_init_static(void)
|
---|
| 49 | {
|
---|
| 50 | runtime_env = &rt_env_static;
|
---|
[bfdb5af1] | 51 | list_initialize(&runtime_env->modules);
|
---|
[1ea99cc] | 52 | runtime_env->next_bias = 0x2000000;
|
---|
| 53 | runtime_env->program = NULL;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[17341d4] | 56 | /** Initialize and process a dynamically linked executable.
|
---|
| 57 | *
|
---|
| 58 | * @param p_info Program info
|
---|
| 59 | * @return EOK on success or non-zero error code
|
---|
| 60 | */
|
---|
| 61 | int rtld_prog_process(elf_finfo_t *p_info, rtld_t **rre)
|
---|
| 62 | {
|
---|
| 63 | rtld_t *env;
|
---|
| 64 |
|
---|
| 65 | DPRINTF("Load dynamically linked program.\n");
|
---|
| 66 |
|
---|
| 67 | /* Allocate new RTLD environment to pass to the loaded program */
|
---|
| 68 | env = calloc(1, sizeof(rtld_t));
|
---|
| 69 | if (env == NULL)
|
---|
| 70 | return ENOMEM;
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * First we need to process dynamic sections of the executable
|
---|
| 74 | * program and insert it into the module graph.
|
---|
| 75 | */
|
---|
| 76 |
|
---|
| 77 | DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
|
---|
| 78 | dynamic_parse(p_info->dynamic, 0, &prog_mod.dyn);
|
---|
| 79 | prog_mod.bias = 0;
|
---|
| 80 | prog_mod.dyn.soname = "[program]";
|
---|
| 81 | prog_mod.rtld = env;
|
---|
| 82 |
|
---|
| 83 | /* Initialize list of loaded modules */
|
---|
| 84 | list_initialize(&env->modules);
|
---|
| 85 | list_append(&prog_mod.modules_link, &env->modules);
|
---|
| 86 |
|
---|
| 87 | /* Pointer to program module. Used as root of the module graph. */
|
---|
| 88 | env->program = &prog_mod;
|
---|
| 89 |
|
---|
| 90 | /* Work around non-existent memory space allocation. */
|
---|
| 91 | env->next_bias = 0x1000000;
|
---|
| 92 |
|
---|
| 93 | /*
|
---|
| 94 | * Now we can continue with loading all other modules.
|
---|
| 95 | */
|
---|
| 96 |
|
---|
| 97 | DPRINTF("Load all program dependencies\n");
|
---|
| 98 | module_load_deps(&prog_mod);
|
---|
| 99 |
|
---|
| 100 | /*
|
---|
| 101 | * Now relocate/link all modules together.
|
---|
| 102 | */
|
---|
| 103 |
|
---|
| 104 | /* Process relocations in all modules */
|
---|
| 105 | DPRINTF("Relocate all modules\n");
|
---|
| 106 | modules_process_relocs(env, &prog_mod);
|
---|
| 107 |
|
---|
| 108 | *rre = env;
|
---|
| 109 | return EOK;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[1ea99cc] | 112 | /** @}
|
---|
| 113 | */
|
---|