Changeset acb14ce in mainline
- Timestamp:
- 2025-02-02T22:06:07Z (4 weeks ago)
- Children:
- c095552
- Parents:
- bdcf71e
- git-author:
- Matěj Volf <git@…> (2025-02-02 21:59:42)
- git-committer:
- Matěj Volf <git@…> (2025-02-02 22:06:07)
- Location:
- uspace/lib/c
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/elf/elf_load.c
rbdcf71e racb14ce 69 69 } 70 70 71 #ifdef CONFIG_RTLD 72 DPRINTF("- prog dynamic: %p\n", finfo->dynamic); 73 rc = rtld_prog_process(finfo, &env); 74 if (rc != EOK) { 75 DPRINTF("Failed to process executable '%s'.\n", file_name); 76 return rc; 77 } 78 info->env = env; 79 return EOK; 80 #else 71 81 if (info->finfo.dynamic == NULL) { 72 /* Statically linked program */73 DPRINTF("Binary is statically linked.\n");74 82 info->env = NULL; 75 #ifdef CONFIG_RTLD76 rc = rtld_init_static(finfo, &env);77 info->env = env;78 #endif79 83 return EOK; 80 84 } 81 85 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); 86 info->env = env; 87 #else 88 rc = ENOTSUP; 86 DPRINTF("Error: trying to run a dynamically-linked executable with CONFIG_RTLD disabled.\n"); 87 return ENOTSUP; 89 88 #endif 90 return rc;91 89 } 92 90 -
uspace/lib/c/generic/rtld/module.c
rbdcf71e racb14ce 55 55 #include "../private/libc.h" 56 56 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 59 60 * @param rtld Run-time dynamic linker 60 61 * @param rmodule Place to store pointer to new module or @c NULL 61 62 * @return EOK on success, ENOMEM if out of memory 62 63 */ 63 errno_t module_create_ static_exec(const void *elf, rtld_t *rtld)64 errno_t module_create_entrypoint(elf_finfo_t *p_info, rtld_t *rtld, module_t **rmodule) 64 65 { 65 66 module_t *module; 67 bool is_dynamic = p_info->dynamic != NULL; 68 DPRINTF("module_create_entrypoint\n"); 66 69 67 70 module = calloc(1, sizeof(module_t)); 68 if (module == NULL) { 69 DPRINTF("malloc failed\n"); 71 if (module == NULL) 70 72 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; 73 88 module->id = rtld_get_next_id(rtld); 74 89 module->dyn.soname = "[program]"; … … 76 91 module->rtld = rtld; 77 92 module->exec = true; 78 module->local = true; 79 80 const elf_segment_header_t *tls = 81 elf_get_phdr(elf, PT_TLS); 82 83 if (tls) { 84 uintptr_t bias = elf_get_bias(elf); 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); 95 102 96 103 list_append(&module->modules_link, &rtld->modules); 104 105 if (rmodule != NULL) 106 *rmodule = module; 97 107 return EOK; 98 108 } -
uspace/lib/c/generic/rtld/rtld.c
rbdcf71e racb14ce 44 44 rtld_t *runtime_env; 45 45 46 /** Initialize the runtime linker for use in a statically-linked executable. */ 47 errno_t rtld_init_static(elf_finfo_t *finfo, rtld_t **rre) 48 { 49 rtld_t *env; 50 errno_t rc; 51 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); 62 if (rc != EOK) 63 return rc; 64 65 modules_process_tls(env); 66 67 *rre = env; 68 return EOK; 69 } 70 71 /** Initialize and process a dynamically linked executable. 46 /** Initialize and process an executable. 72 47 * 73 48 * @param p_info Program info … … 77 52 { 78 53 rtld_t *env; 79 module_t *prog; 80 81 DPRINTF("Load dynamically linked program.\n"); 54 bool is_dynamic = p_info->dynamic != NULL; 55 DPRINTF("rtld_prog_process\n"); 82 56 83 57 /* Allocate new RTLD environment to pass to the loaded program */ … … 86 60 return ENOMEM; 87 61 88 env->next_id = 1;89 90 prog = calloc(1, sizeof(module_t));91 if (prog == NULL) {92 free(env);93 return ENOMEM;94 }95 96 /*97 * First we need to process dynamic sections of the executable98 * program and insert it into the module graph.99 */100 101 DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);102 dynamic_parse(p_info->dynamic, 0, &prog->dyn);103 prog->bias = 0;104 prog->dyn.soname = "[program]";105 prog->rtld = env;106 prog->id = rtld_get_next_id(env);107 prog->exec = true;108 prog->local = false;109 110 prog->tdata = p_info->tls.tdata;111 prog->tdata_size = p_info->tls.tdata_size;112 prog->tbss_size = p_info->tls.tbss_size;113 prog->tls_align = p_info->tls.tls_align;114 115 DPRINTF("prog tdata at %p size %zu, tbss size %zu\n",116 prog->tdata, prog->tdata_size, prog->tbss_size);117 118 /* Initialize list of loaded modules */119 62 list_initialize(&env->modules); 120 63 list_initialize(&env->imodules); 121 list_append(&prog->modules_link, &env->modules); 122 123 /* Pointer to program module. Used as root of the module graph. */ 124 env->program = prog; 125 126 /* 127 * Now we can continue with loading all other modules. 128 */ 129 130 DPRINTF("Load all program dependencies\n"); 131 errno_t rc = module_load_deps(prog, 0); 64 env->next_id = 1; 65 66 module_t *module; 67 errno_t rc = module_create_entrypoint(p_info, env, &module); 132 68 if (rc != EOK) { 133 free(prog);134 69 free(env); 135 70 return rc; 136 71 } 137 72 73 /* Pointer to program module. Used as root of the module graph. */ 74 env->program = module; 75 76 /* 77 * Now we can continue with loading all other modules. 78 */ 79 80 if (is_dynamic) { 81 DPRINTF("Load all program dependencies\n"); 82 rc = module_load_deps(module, 0); 83 if (rc != EOK) { 84 free(module); 85 free(env); 86 return rc; 87 } 88 } 89 138 90 /* Compute static TLS size */ 139 91 modules_process_tls(env); … … 143 95 */ 144 96 145 /* Process relocations in all modules */ 146 DPRINTF("Relocate all modules\n"); 147 modules_process_relocs(env, prog); 148 149 *rre = env; 97 if (is_dynamic) { 98 /* Process relocations in all modules */ 99 DPRINTF("Relocate all modules\n"); 100 modules_process_relocs(env, module); 101 } 102 103 if (rre != NULL) 104 *rre = env; 150 105 return EOK; 151 106 } -
uspace/lib/c/include/rtld/module.h
rbdcf71e racb14ce 41 41 #include <types/rtld/rtld.h> 42 42 43 extern errno_t module_create_ static_exec(const void *, rtld_t*);43 extern errno_t module_create_entrypoint(elf_finfo_t *, rtld_t *, module_t **); 44 44 extern void module_process_relocs(module_t *); 45 45 extern module_t *module_find(rtld_t *, const char *); -
uspace/lib/c/include/rtld/rtld.h
rbdcf71e racb14ce 45 45 extern rtld_t *runtime_env; 46 46 47 extern errno_t rtld_init_static(elf_finfo_t *, rtld_t **);48 47 extern errno_t rtld_prog_process(elf_finfo_t *, rtld_t **); 49 48 extern tcb_t *rtld_tls_make(rtld_t *);
Note:
See TracChangeset
for help on using the changeset viewer.