Changeset f215c6ef in mainline


Ignore:
Timestamp:
2025-01-26T21:33:13Z (3 weeks ago)
Author:
Matěj Volf <git@…>
Children:
ea77c2d
Parents:
31ea2a7
git-author:
Matěj Volf <git@…> (2025-01-26 21:04:16)
git-committer:
Matěj Volf <git@…> (2025-01-26 21:33:13)
Message:

properly initialize RTLD runtime for static binaries

this is a better approach than what I did of previous commit

see https://github.com/HelenOS/helenos/pull/242 for a lot more context

Location:
uspace/lib/c
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/elf/elf_load.c

    r31ea2a7 rf215c6ef  
    5757errno_t elf_load(int file, elf_info_t *info)
    5858{
     59#ifdef CONFIG_RTLD
     60        rtld_t *env;
     61#endif
    5962        errno_t rc = EOK;
     63        elf_finfo_t *finfo = &info->finfo;
    6064
    61         rc = elf_load_file(file, 0, &info->finfo);
     65        rc = elf_load_file(file, 0, finfo);
    6266        if (rc != EOK) {
    6367                DPRINTF("Failed to load executable '%s'.\n", file_name);
     
    6569        }
    6670
     71        if (info->finfo.dynamic == NULL) {
     72                /* Statically linked program */
     73                DPRINTF("Binary is statically linked.\n");
     74                info->env = NULL;
    6775#ifdef CONFIG_RTLD
    68         rtld_t *env;
    69         rc = rtld_prog_process(&info->finfo, &env);
     76                rc = rtld_init_static(finfo, &env);
     77                info->env = env;
     78#endif
     79                return EOK;
     80        }
     81
     82        DPRINTF("Binary is dynamically linked.\n");
     83#ifdef CONFIG_RTLD
     84        DPRINTF("- prog dynamic: %p\n", finfo->dynamic);
     85        rc = rtld_prog_process(finfo, &env);
    7086        info->env = env;
    7187#else
  • uspace/lib/c/generic/libc.c

    r31ea2a7 rf215c6ef  
    9999                /*
    100100                 * A binary loaded by kernel, not the loader.
    101                  * Init some rudimentary rtld runtime environment.
     101                 * Noop - code loaded by kernel doesn't need RTLD.
    102102                 */
    103                 errno_t rtld_init_result = rtld_init_static();
    104                 assert(rtld_init_result == EOK);
     103                // errno_t rtld_init_result = rtld_init_static();
     104                // assert(rtld_init_result == EOK);
    105105        } else {
    106106                assert(__pcb->rtld_runtime != NULL);
  • uspace/lib/c/generic/rtld/module.c

    r31ea2a7 rf215c6ef  
    6161 * @return EOK on success, ENOMEM if out of memory
    6262 */
    63 errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
     63errno_t module_create_static_exec(const void *elf, rtld_t *rtld)
    6464{
    6565        module_t *module;
     
    7979
    8080        const elf_segment_header_t *tls =
    81             elf_get_phdr(__progsymbols.elfstart, PT_TLS);
     81            elf_get_phdr(elf, PT_TLS);
    8282
    8383        if (tls) {
    84                 uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
     84                uintptr_t bias = elf_get_bias(elf);
    8585                module->tdata = (void *) (tls->p_vaddr + bias);
    8686                module->tdata_size = tls->p_filesz;
     
    9595
    9696        list_append(&module->modules_link, &rtld->modules);
    97 
    98         if (rmodule != NULL)
    99                 *rmodule = module;
    10097        return EOK;
    10198}
  • uspace/lib/c/generic/rtld/rtld.c

    r31ea2a7 rf215c6ef  
    4343
    4444rtld_t *runtime_env;
    45 static rtld_t rt_env_static;
    46 
    47 /** Initialize a minimal runtime linker environment for use in executables loaded directly by kernel. */
    48 errno_t rtld_init_static(void)
    49 {
     45
     46/** Initialize the runtime linker for use in a statically-linked executable. */
     47errno_t rtld_init_static(elf_finfo_t *finfo, rtld_t **rre)
     48{
     49        rtld_t *env;
    5050        errno_t rc;
    5151
    52         runtime_env = &rt_env_static;
    53         list_initialize(&runtime_env->modules);
    54         list_initialize(&runtime_env->imodules);
    55         runtime_env->program = NULL;
    56         runtime_env->next_id = 1;
    57 
    58         rc = module_create_static_exec(runtime_env, NULL);
     52        env = calloc(1, sizeof(rtld_t));
     53        if (env == NULL)
     54                return ENOMEM;
     55
     56        list_initialize(&env->modules);
     57        list_initialize(&env->imodules);
     58        env->program = NULL;
     59        env->next_id = 1;
     60
     61        rc = module_create_static_exec(finfo->base, env);
    5962        if (rc != EOK)
    6063                return rc;
    6164
    62         modules_process_tls(runtime_env);
    63 
     65        modules_process_tls(env);
     66
     67        *rre = env;
    6468        return EOK;
    6569}
    6670
    67 /** Initialize and process an executable, static or dynamic.
     71/** Initialize and process a dynamically linked executable.
    6872 *
    6973 * @param p_info Program info
     
    7579        module_t *prog;
    7680
    77         DPRINTF("Load program with rtld.\n");
     81        DPRINTF("Load dynamically linked program.\n");
    7882
    7983        /* Allocate new RTLD environment to pass to the loaded program */
     
    9599         */
    96100
    97         if (p_info->dynamic) {
    98                 DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
    99                 dynamic_parse(p_info->dynamic, 0, &prog->dyn);
    100         } else {
    101                 DPRINTF("Program is statically linked\n");
    102         }
     101        DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
     102        dynamic_parse(p_info->dynamic, 0, &prog->dyn);
    103103        prog->bias = 0;
    104104        prog->dyn.soname = "[program]";
     
    128128         */
    129129
    130         if (p_info->dynamic) {
    131                 DPRINTF("Load all program dependencies\n");
    132                 errno_t rc = module_load_deps(prog, 0);
    133                 if (rc != EOK) {
    134                         free(prog);
    135                         free(env);
    136                         return rc;
    137                 }
     130        DPRINTF("Load all program dependencies\n");
     131        errno_t rc = module_load_deps(prog, 0);
     132        if (rc != EOK) {
     133                free(prog);
     134                free(env);
     135                return rc;
    138136        }
    139137
  • uspace/lib/c/include/rtld/module.h

    r31ea2a7 rf215c6ef  
    4141#include <types/rtld/rtld.h>
    4242
    43 extern errno_t module_create_static_exec(rtld_t *, module_t **);
     43extern errno_t module_create_static_exec(const void *, rtld_t *);
    4444extern void module_process_relocs(module_t *);
    4545extern module_t *module_find(rtld_t *, const char *);
  • uspace/lib/c/include/rtld/rtld.h

    r31ea2a7 rf215c6ef  
    4545extern rtld_t *runtime_env;
    4646
    47 extern errno_t rtld_init_static(void);
     47extern errno_t rtld_init_static(elf_finfo_t *, rtld_t **);
    4848extern errno_t rtld_prog_process(elf_finfo_t *, rtld_t **);
    4949extern tcb_t *rtld_tls_make(rtld_t *);
Note: See TracChangeset for help on using the changeset viewer.