source: mainline/uspace/lib/c/generic/rtld/rtld.c@ 31ea2a7

Last change on this file since 31ea2a7 was 31ea2a7, checked in by Matěj Volf <git@…>, 5 months ago

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

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[1ea99cc]1/*
2 * Copyright (c) 2008 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[b1834a01]29/** @addtogroup rtld
[1ea99cc]30 * @brief
31 * @{
[1b20da0]32 */
[1ea99cc]33/**
34 * @file
35 */
36
[17341d4]37#include <errno.h>
38#include <rtld/module.h>
[8a1fb09]39#include <rtld/rtld.h>
[17341d4]40#include <rtld/rtld_debug.h>
41#include <stdlib.h>
[1d6dd2a]42#include <str.h>
[1ea99cc]43
[17341d4]44rtld_t *runtime_env;
45static rtld_t rt_env_static;
[1ea99cc]46
[31ea2a7]47/** Initialize a minimal runtime linker environment for use in executables loaded directly by kernel. */
[b7fd2a0]48errno_t rtld_init_static(void)
[1ea99cc]49{
[b7fd2a0]50 errno_t rc;
[153c7a29]51
[1ea99cc]52 runtime_env = &rt_env_static;
[bfdb5af1]53 list_initialize(&runtime_env->modules);
[bab0f42]54 list_initialize(&runtime_env->imodules);
[1ea99cc]55 runtime_env->program = NULL;
[153c7a29]56 runtime_env->next_id = 1;
57
58 rc = module_create_static_exec(runtime_env, NULL);
59 if (rc != EOK)
60 return rc;
61
62 modules_process_tls(runtime_env);
63
64 return EOK;
[1ea99cc]65}
66
[31ea2a7]67/** Initialize and process an executable, static or dynamic.
[17341d4]68 *
69 * @param p_info Program info
70 * @return EOK on success or non-zero error code
71 */
[b7fd2a0]72errno_t rtld_prog_process(elf_finfo_t *p_info, rtld_t **rre)
[17341d4]73{
74 rtld_t *env;
[6adb775f]75 module_t *prog;
[17341d4]76
[31ea2a7]77 DPRINTF("Load program with rtld.\n");
[17341d4]78
79 /* Allocate new RTLD environment to pass to the loaded program */
80 env = calloc(1, sizeof(rtld_t));
81 if (env == NULL)
82 return ENOMEM;
83
[6adb775f]84 env->next_id = 1;
85
86 prog = calloc(1, sizeof(module_t));
87 if (prog == NULL) {
88 free(env);
89 return ENOMEM;
90 }
91
[17341d4]92 /*
93 * First we need to process dynamic sections of the executable
94 * program and insert it into the module graph.
95 */
96
[31ea2a7]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 }
[6adb775f]103 prog->bias = 0;
104 prog->dyn.soname = "[program]";
105 prog->rtld = env;
106 prog->id = rtld_get_next_id(env);
[32573ff]107 prog->exec = true;
108 prog->local = false;
[6adb775f]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;
[29405ac]113 prog->tls_align = p_info->tls.tls_align;
[6adb775f]114
[153c7a29]115 DPRINTF("prog tdata at %p size %zu, tbss size %zu\n",
[6adb775f]116 prog->tdata, prog->tdata_size, prog->tbss_size);
[17341d4]117
118 /* Initialize list of loaded modules */
119 list_initialize(&env->modules);
[bab0f42]120 list_initialize(&env->imodules);
[6adb775f]121 list_append(&prog->modules_link, &env->modules);
[17341d4]122
123 /* Pointer to program module. Used as root of the module graph. */
[6adb775f]124 env->program = prog;
[17341d4]125
126 /*
127 * Now we can continue with loading all other modules.
128 */
129
[31ea2a7]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 }
[967e7a1]138 }
[17341d4]139
[29405ac]140 /* Compute static TLS size */
141 modules_process_tls(env);
142
[17341d4]143 /*
144 * Now relocate/link all modules together.
145 */
146
147 /* Process relocations in all modules */
148 DPRINTF("Relocate all modules\n");
[6adb775f]149 modules_process_relocs(env, prog);
150
[17341d4]151 *rre = env;
152 return EOK;
153}
154
[6adb775f]155/** Create TLS (Thread Local Storage) data structures.
156 *
157 * @return Pointer to TCB.
158 */
159tcb_t *rtld_tls_make(rtld_t *rtld)
160{
161 tcb_t *tcb;
[d2bb25e7]162 void **dtv;
163 size_t nmods;
164 size_t i;
[6adb775f]165
[4f205248]166 tcb = tls_alloc_arch(rtld->tls_size, rtld->tls_align);
[6adb775f]167 if (tcb == NULL)
168 return NULL;
169
[d2bb25e7]170 /** Allocate dynamic thread vector */
171 nmods = list_count(&rtld->imodules);
172 dtv = malloc((nmods + 1) * sizeof(void *));
173 if (dtv == NULL) {
174 tls_free(tcb);
175 return NULL;
176 }
177
178 /*
179 * We define generation number to be equal to vector length.
180 * We start with a vector covering the initially loaded modules.
181 */
182 DTV_GN(dtv) = nmods;
183
[6adb775f]184 /*
[d2bb25e7]185 * Copy thread local data from the initialization images of initial
186 * modules. Zero out thread-local uninitialized data.
[6adb775f]187 */
188
[1433ecda]189 i = 1;
[bab0f42]190 list_foreach(rtld->imodules, imodules_link, module_t, m) {
[4f205248]191 assert(i++ == m->id);
192
193 dtv[m->id] = (void *) tcb + m->tpoff;
194
195 assert(((uintptr_t) dtv[m->id]) % m->tls_align == 0);
196
[b83c5e4]197 if (m->tdata)
198 memcpy(dtv[m->id], m->tdata, m->tdata_size);
199
[4f205248]200 memset(dtv[m->id] + m->tdata_size, 0, m->tbss_size);
[29405ac]201 }
[6adb775f]202
[d2bb25e7]203 tcb->dtv = dtv;
[6adb775f]204 return tcb;
205}
206
207unsigned long rtld_get_next_id(rtld_t *rtld)
208{
209 return rtld->next_id++;
210}
211
[d2bb25e7]212/** Get address of thread-local variable.
213 *
214 * @param rtld RTLD instance
215 * @param tcb TCB of the thread whose instance to return
216 * @param mod_id Module ID
217 * @param offset Offset within TLS block of the module
218 *
219 * @return Address of thread-local variable
220 */
221void *rtld_tls_get_addr(rtld_t *rtld, tcb_t *tcb, unsigned long mod_id,
[6adb775f]222 unsigned long offset)
223{
[9c07c3d]224 module_t *m;
225 size_t dtv_len;
226 void *tls_block;
227
228 dtv_len = DTV_GN(tcb->dtv);
229 if (dtv_len < mod_id) {
230 /* Vector is short */
231
232 tcb->dtv = realloc(tcb->dtv, (1 + mod_id) * sizeof(void *));
233 /* XXX This can fail if OOM */
234 assert(tcb->dtv != NULL);
235 /* Zero out new part of vector */
236 memset(tcb->dtv + (1 + dtv_len), 0, (mod_id - dtv_len) *
237 sizeof(void *));
238 }
239
240 if (tcb->dtv[mod_id] == NULL) {
241 /* TLS block is not allocated */
242
243 m = module_by_id(rtld, mod_id);
244 assert(m != NULL);
245 /* Should not be initial module, those have TLS pre-allocated */
246 assert(!link_used(&m->imodules_link));
247
[4f205248]248 tls_block = memalign(m->tls_align, m->tdata_size + m->tbss_size);
[9c07c3d]249 /* XXX This can fail if OOM */
250 assert(tls_block != NULL);
251
252 /* Copy tdata */
253 memcpy(tls_block, m->tdata, m->tdata_size);
254 /* Zero out tbss */
255 memset(tls_block + m->tdata_size, 0, m->tbss_size);
256
257 tcb->dtv[mod_id] = tls_block;
[e2f26002]258 }
259
[d2bb25e7]260 return (uint8_t *)(tcb->dtv[mod_id]) + offset;
[6adb775f]261}
262
[1ea99cc]263/** @}
264 */
Note: See TracBrowser for help on using the repository browser.