Changeset 17341d4 in mainline for uspace/lib/c/generic/rtld


Ignore:
Timestamp:
2016-04-20T17:25:48Z (9 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
dc0d8b52
Parents:
13dfa3f9
Message:

Move rtld internals out of loader. Stop misusing rtld instance from current environment for loading dynamically linked executables.

Location:
uspace/lib/c/generic/rtld
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/rtld/module.c

    r13dfa3f9 r17341d4  
    9191 * path components are ignored.
    9292 */
    93 module_t *module_find(const char *name)
     93module_t *module_find(rtld_t *rtld, const char *name)
    9494{
    9595        const char *p, *soname;
     
    106106
    107107        /* Traverse list of all modules. Not extremely fast, but simple */
    108         list_foreach(runtime_env->modules, modules_link, module_t, m) {
     108        list_foreach(rtld->modules, modules_link, module_t, m) {
    109109                DPRINTF("m = %p\n", m);
    110110                if (str_cmp(m->dyn.soname, soname) == 0) {
     
    122122 * Currently this trivially tries to load '/<name>'.
    123123 */
    124 module_t *module_load(const char *name)
    125 {
    126         elf_info_t info;
     124module_t *module_load(rtld_t *rtld, const char *name)
     125{
     126        elf_finfo_t info;
    127127        char name_buf[NAME_BUF_SIZE];
    128128        module_t *m;
     
    135135        }
    136136
     137        m->rtld = rtld;
     138
    137139        if (str_size(name) > NAME_BUF_SIZE - 2) {
    138140                printf("soname too long. increase NAME_BUF_SIZE\n");
     
    145147
    146148        /* FIXME: need to real allocation of address space */
    147         m->bias = runtime_env->next_bias;
    148         runtime_env->next_bias += 0x100000;
     149        m->bias = rtld->next_bias;
     150        rtld->next_bias += 0x100000;
    149151
    150152        DPRINTF("filename:'%s'\n", name_buf);
     
    171173
    172174        /* Insert into the list of loaded modules */
    173         list_append(&m->modules_link, &runtime_env->modules);
     175        list_append(&m->modules_link, &rtld->modules);
    174176
    175177        return m;
     
    221223
    222224                        DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
    223                         dm = module_find(dep_name);
     225                        dm = module_find(m->rtld, dep_name);
    224226                        if (!dm) {
    225                                 dm = module_load(dep_name);
     227                                dm = module_load(m->rtld, dep_name);
    226228                                module_load_deps(dm);
    227229                        }
     
    241243 * @param       start   The module where to start from.
    242244 */
    243 void modules_process_relocs(module_t *start)
    244 {
    245         list_foreach(runtime_env->modules, modules_link, module_t, m) {
    246                 /* Skip rtld, since it has already been processed */
    247                 if (m != &runtime_env->rtld) {
     245void modules_process_relocs(rtld_t *rtld, module_t *start)
     246{
     247        list_foreach(rtld->modules, modules_link, module_t, m) {
     248                /* Skip rtld module, since it has already been processed */
     249                if (m != &rtld->rtld) {
    248250                        module_process_relocs(m);
    249251                }
     
    253255/** Clear BFS tags of all modules.
    254256 */
    255 void modules_untag(void)
    256 {
    257         list_foreach(runtime_env->modules, modules_link, module_t, m) {
     257void modules_untag(rtld_t *rtld)
     258{
     259        list_foreach(rtld->modules, modules_link, module_t, m) {
    258260                m->bfs_tag = false;
    259261        }
  • uspace/lib/c/generic/rtld/rtld.c

    r13dfa3f9 r17341d4  
    3535 */
    3636
     37#include <errno.h>
     38#include <rtld/module.h>
    3739#include <rtld/rtld.h>
     40#include <rtld/rtld_debug.h>
     41#include <stdlib.h>
    3842
    39 runtime_env_t *runtime_env;
    40 static runtime_env_t rt_env_static;
     43rtld_t *runtime_env;
     44static rtld_t rt_env_static;
     45static module_t prog_mod;
    4146
    42 /** Initialize the loder for use in a statically-linked binary. */
     47/** Initialize the runtime linker for use in a statically-linked executable. */
    4348void rtld_init_static(void)
    4449{
     
    4954}
    5055
     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 */
     61int 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
    51112/** @}
    52113 */
  • uspace/lib/c/generic/rtld/symbol.c

    r13dfa3f9 r17341d4  
    3939
    4040#include <elf/elf.h>
     41#include <rtld/module.h>
    4142#include <rtld/rtld.h>
    4243#include <rtld/rtld_debug.h>
     
    132133
    133134        /* Mark all vertices (modules) as unvisited */ 
    134         modules_untag();
     135        modules_untag(start->rtld);
    135136
    136137        /* Insert root (the program) into the queue and tag it */
     
    219220        /* Not DT_SYMBOLIC or no match. Now try other locations. */
    220221
    221         if (runtime_env->program) {
     222        if (origin->rtld->program) {
    222223                /* Program is dynamic -- start with program as root. */
    223                 return symbol_bfs_find(name, runtime_env->program, flags, mod);
     224                return symbol_bfs_find(name, origin->rtld->program, flags, mod);
    224225        } else {
    225226                /* Program is static -- start with @a origin as root. */
Note: See TracChangeset for help on using the changeset viewer.