Ignore:
File:
1 edited

Legend:

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

    rffccdff0 r32254d6  
    11/*
    2  * Copyright (c) 2008 Jiri Svoboda
     2 * Copyright (c) 2024 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5555#include "../private/libc.h"
    5656
    57 /** Create module for static executable.
    58  *
     57/** Create the "entrypoint" module, of the program executable
     58 *
     59 * @param p_info Program ELF file info
    5960 * @param rtld Run-time dynamic linker
    6061 * @param rmodule Place to store pointer to new module or @c NULL
    6162 * @return EOK on success, ENOMEM if out of memory
    6263 */
    63 errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
     64errno_t module_create_entrypoint(elf_finfo_t *p_info, rtld_t *rtld, module_t **rmodule)
    6465{
    6566        module_t *module;
     67        bool is_dynamic = p_info->dynamic != NULL;
     68        DPRINTF("module_create_entrypoint\n");
    6669
    6770        module = calloc(1, sizeof(module_t));
    68         if (module == NULL) {
    69                 DPRINTF("malloc failed\n");
     71        if (module == NULL)
    7072                return ENOMEM;
    71         }
    72 
     73
     74        uintptr_t bias = elf_get_bias(p_info->base);
     75
     76        /*
     77         * First we need to process dynamic sections of the executable
     78         * program and insert it into the module graph.
     79         */
     80        if (is_dynamic) {
     81                DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
     82                dynamic_parse(p_info->dynamic, bias, &module->dyn);
     83        } else {
     84                DPRINTF("Executable is not dynamically linked\n");
     85        }
     86
     87        module->bias = bias;
    7388        module->id = rtld_get_next_id(rtld);
    7489        module->dyn.soname = "[program]";
     
    7691        module->rtld = rtld;
    7792        module->exec = true;
    78         module->local = true;
    79 
    80         const elf_segment_header_t *tls =
    81             elf_get_phdr(__progsymbols.elfstart, PT_TLS);
    82 
    83         if (tls) {
    84                 uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
    85                 module->tdata = (void *) (tls->p_vaddr + bias);
    86                 module->tdata_size = tls->p_filesz;
    87                 module->tbss_size = tls->p_memsz - tls->p_filesz;
    88                 module->tls_align = tls->p_align;
    89         } else {
    90                 module->tdata = NULL;
    91                 module->tdata_size = 0;
    92                 module->tbss_size = 0;
    93                 module->tls_align = 1;
    94         }
     93        module->local = !is_dynamic;
     94
     95        module->tdata = p_info->tls.tdata;
     96        module->tdata_size = p_info->tls.tdata_size;
     97        module->tbss_size = p_info->tls.tbss_size;
     98        module->tls_align = p_info->tls.tls_align;
     99
     100        DPRINTF("prog tdata at %p size %zu, tbss size %zu\n",
     101        module->tdata, module->tdata_size, module->tbss_size);
    95102
    96103        list_append(&module->modules_link, &rtld->modules);
     
    343350{
    344351        list_foreach(rtld->modules, modules_link, module_t, m) {
    345                 /* Skip rtld module, since it has already been processed */
    346                 if (m != &rtld->rtld) {
     352                /*
     353                 * Skip rtld module, since it has already been processed.
     354                 * Skip start / main program -- leave it for later
     355                 */
     356                if (m != &rtld->rtld && m != start) {
    347357                        module_process_relocs(m);
    348358                }
    349359        }
     360
     361        /*
     362         * Now that shared libraries have been processed and their variables
     363         * are thus initialized, we can process the main program,
     364         * which may contain COPY relocations that copy value from shared
     365         * library variables to instances of those variables defined
     366         * in the main program.
     367         */
     368        module_process_relocs(start);
    350369}
    351370
     
    386405         * be correct, "zero" offset (i.e. the total size) must be aligned
    387406         * to the strictest alignment present.
    388          * Note that the padding is actually in front of the TLS data,
    389          * not after it.
    390407         */
    391408        rtld->tls_size = ALIGN_UP(rtld->tls_size, rtld->tls_align);
    392409
    393         /* Space for the TCB. */
     410        /*
     411         * Space for the TCB.
     412         * Later, the TLS zero offset is equal to the pointer to tcb_t, so
     413         * adding the sizeof(tcb_t) block AFTER we calculated the alignment
     414         * of the remainder above is correct.
     415         */
    394416        rtld->tls_size += sizeof(tcb_t);
    395417#endif
Note: See TracChangeset for help on using the changeset viewer.