Changeset 31ea2a7 in mainline


Ignore:
Timestamp:
2025-01-26T17:53:49Z (3 weeks ago)
Author:
Matěj Volf <git@…>
Children:
f215c6ef
Parents:
eff458d
git-author:
Matěj Volf <git@…> (2025-01-25 21:36:13)
git-committer:
Matěj Volf <git@…> (2025-01-26 17:53:49)
Message:

init RTLD runtime at load time even for statically linked binaries

before this, main_fibril of a statically linked binary had wrong thread-local storage

please see PR description for an elaborate explanation of what and why was wrong

Location:
uspace
Files:
5 edited

Legend:

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

    reff458d r31ea2a7  
    5757errno_t elf_load(int file, elf_info_t *info)
    5858{
    59 #ifdef CONFIG_RTLD
    60         rtld_t *env;
    61 #endif
    62         errno_t rc;
     59        errno_t rc = EOK;
    6360
    6461        rc = elf_load_file(file, 0, &info->finfo);
     
    6865        }
    6966
    70         if (info->finfo.dynamic == NULL) {
    71                 /* Statically linked program */
    72                 DPRINTF("Binary is statically linked.\n");
    73                 info->env = NULL;
    74                 return EOK;
    75         }
    76 
    77         DPRINTF("Binary is dynamically linked.\n");
    7867#ifdef CONFIG_RTLD
    79         DPRINTF("- prog dynamic: %p\n", info->finfo.dynamic);
    80 
     68        rtld_t *env;
    8169        rc = rtld_prog_process(&info->finfo, &env);
    8270        info->env = env;
  • uspace/lib/c/generic/libc.c

    reff458d r31ea2a7  
    9696
    9797#ifdef CONFIG_RTLD
    98         if (__pcb != NULL && __pcb->rtld_runtime != NULL) {
     98        if (__pcb == NULL) {
     99                /*
     100                 * A binary loaded by kernel, not the loader.
     101                 * Init some rudimentary rtld runtime environment.
     102                 */
     103                errno_t rtld_init_result = rtld_init_static();
     104                assert(rtld_init_result == EOK);
     105        } else {
     106                assert(__pcb->rtld_runtime != NULL);
    99107                runtime_env = (rtld_t *) __pcb->rtld_runtime;
    100         } else {
    101                 if (rtld_init_static() != EOK)
    102                         abort();
    103108        }
    104109#endif
  • uspace/lib/c/generic/rtld/rtld.c

    reff458d r31ea2a7  
    4545static rtld_t rt_env_static;
    4646
    47 /** Initialize the runtime linker for use in a statically-linked executable. */
     47/** Initialize a minimal runtime linker environment for use in executables loaded directly by kernel. */
    4848errno_t rtld_init_static(void)
    4949{
     
    6565}
    6666
    67 /** Initialize and process a dynamically linked executable.
     67/** Initialize and process an executable, static or dynamic.
    6868 *
    6969 * @param p_info Program info
     
    7575        module_t *prog;
    7676
    77         DPRINTF("Load dynamically linked program.\n");
     77        DPRINTF("Load program with rtld.\n");
    7878
    7979        /* Allocate new RTLD environment to pass to the loaded program */
     
    9595         */
    9696
    97         DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
    98         dynamic_parse(p_info->dynamic, 0, &prog->dyn);
     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        }
    99103        prog->bias = 0;
    100104        prog->dyn.soname = "[program]";
     
    124128         */
    125129
    126         DPRINTF("Load all program dependencies\n");
    127         errno_t rc = module_load_deps(prog, 0);
    128         if (rc != EOK) {
    129                 return rc;
     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                }
    130138        }
    131139
  • uspace/lib/c/generic/thread/tls.c

    reff458d r31ea2a7  
    7474}
    7575
    76 /** Get address of static TLS block */
     76/** Get address of static TLS block - only when RTLD is not initialized  */
    7777void *tls_get(void)
    7878{
  • uspace/srv/loader/main.c

    reff458d r31ea2a7  
    300300
    301301#ifdef CONFIG_RTLD
    302         if (prog_info.env) {
    303                 pcb.tcb = rtld_tls_make(prog_info.env);
    304         } else {
    305                 pcb.tcb = tls_make(prog_info.finfo.base);
    306         }
     302        assert(prog_info.env != NULL);
     303        pcb.tcb = rtld_tls_make(prog_info.env);
    307304#else
    308305        pcb.tcb = tls_make(prog_info.finfo.base);
Note: See TracChangeset for help on using the changeset viewer.