source: mainline/uspace/lib/c/generic/rtld/rtld.c@ 2112a79

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2112a79 was 9c07c3d, checked in by Jiri Svoboda <jiri@…>, 9 years ago

Create TLS blocks for non-initial modules when needed.

  • Property mode set to 100644
File size: 6.8 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
29/** @addtogroup rtld rtld
30 * @brief
31 * @{
32 */
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>
[1ea99cc]42
[17341d4]43rtld_t *runtime_env;
44static rtld_t rt_env_static;
[1ea99cc]45
[17341d4]46/** Initialize the runtime linker for use in a statically-linked executable. */
[1ea99cc]47void rtld_init_static(void)
48{
49 runtime_env = &rt_env_static;
[bfdb5af1]50 list_initialize(&runtime_env->modules);
[bab0f42]51 list_initialize(&runtime_env->imodules);
[1ea99cc]52 runtime_env->next_bias = 0x2000000;
53 runtime_env->program = NULL;
54}
55
[17341d4]56/** Initialize and process a dynamically linked executable.
57 *
58 * @param p_info Program info
59 * @return EOK on success or non-zero error code
60 */
61int rtld_prog_process(elf_finfo_t *p_info, rtld_t **rre)
62{
63 rtld_t *env;
[6adb775f]64 module_t *prog;
[17341d4]65
66 DPRINTF("Load dynamically linked program.\n");
67
68 /* Allocate new RTLD environment to pass to the loaded program */
69 env = calloc(1, sizeof(rtld_t));
70 if (env == NULL)
71 return ENOMEM;
72
[6adb775f]73 env->next_id = 1;
74
75 prog = calloc(1, sizeof(module_t));
76 if (prog == NULL) {
77 free(env);
78 return ENOMEM;
79 }
80
[17341d4]81 /*
82 * First we need to process dynamic sections of the executable
83 * program and insert it into the module graph.
84 */
85
86 DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
[6adb775f]87 dynamic_parse(p_info->dynamic, 0, &prog->dyn);
88 prog->bias = 0;
89 prog->dyn.soname = "[program]";
90 prog->rtld = env;
91 prog->id = rtld_get_next_id(env);
[32573ff]92 prog->exec = true;
93 prog->local = false;
[6adb775f]94
95 prog->tdata = p_info->tls.tdata;
96 prog->tdata_size = p_info->tls.tdata_size;
97 prog->tbss_size = p_info->tls.tbss_size;
[29405ac]98 prog->tls_align = p_info->tls.tls_align;
[6adb775f]99
100 printf("prog tdata at %p size %zu, tbss size %zu\n",
101 prog->tdata, prog->tdata_size, prog->tbss_size);
[17341d4]102
103 /* Initialize list of loaded modules */
104 list_initialize(&env->modules);
[bab0f42]105 list_initialize(&env->imodules);
[6adb775f]106 list_append(&prog->modules_link, &env->modules);
[17341d4]107
108 /* Pointer to program module. Used as root of the module graph. */
[6adb775f]109 env->program = prog;
[17341d4]110
111 /* Work around non-existent memory space allocation. */
112 env->next_bias = 0x1000000;
113
114 /*
115 * Now we can continue with loading all other modules.
116 */
117
118 DPRINTF("Load all program dependencies\n");
[32573ff]119 module_load_deps(prog, 0);
[17341d4]120
[29405ac]121 /* Compute static TLS size */
122 modules_process_tls(env);
123
[17341d4]124 /*
125 * Now relocate/link all modules together.
126 */
127
128 /* Process relocations in all modules */
129 DPRINTF("Relocate all modules\n");
[6adb775f]130 modules_process_relocs(env, prog);
131
[17341d4]132 *rre = env;
133 return EOK;
134}
135
[6adb775f]136/** Create TLS (Thread Local Storage) data structures.
137 *
138 * @return Pointer to TCB.
139 */
140tcb_t *rtld_tls_make(rtld_t *rtld)
141{
142 void *data;
143 tcb_t *tcb;
144 size_t offset;
[d2bb25e7]145 void **dtv;
146 size_t nmods;
147 size_t i;
[6adb775f]148
149 tcb = tls_alloc_arch(&data, rtld->tls_size);
150 if (tcb == NULL)
151 return NULL;
152
[d2bb25e7]153 /** Allocate dynamic thread vector */
154 nmods = list_count(&rtld->imodules);
155 dtv = malloc((nmods + 1) * sizeof(void *));
156 if (dtv == NULL) {
157 tls_free(tcb);
158 return NULL;
159 }
160
161 /*
162 * We define generation number to be equal to vector length.
163 * We start with a vector covering the initially loaded modules.
164 */
165 DTV_GN(dtv) = nmods;
166
[6adb775f]167 /*
[d2bb25e7]168 * Copy thread local data from the initialization images of initial
169 * modules. Zero out thread-local uninitialized data.
[6adb775f]170 */
171
[29405ac]172#ifdef CONFIG_TLS_VARIANT_1
173 /*
174 * Ascending addresses
175 */
[d2bb25e7]176 offset = 0; i = 1;
[bab0f42]177 list_foreach(rtld->imodules, imodules_link, module_t, m) {
[d2bb25e7]178 assert(i == m->id);
[91e4567]179 assert(offset + m->tdata_size + m->tbss_size <= rtld->tls_size);
[d2bb25e7]180 dtv[i++] = data + offset;
[6adb775f]181 memcpy(data + offset, m->tdata, m->tdata_size);
182 offset += m->tdata_size;
183 memset(data + offset, 0, m->tbss_size);
184 offset += m->tbss_size;
185 }
[29405ac]186#else /* CONFIG_TLS_VARIANT_2 */
187 /*
188 * Descending addresses
189 */
[d2bb25e7]190 offset = 0; i = 1;
[bab0f42]191 list_foreach(rtld->imodules, imodules_link, module_t, m) {
[d2bb25e7]192 assert(i == m->id);
[29405ac]193 assert(offset + m->tdata_size + m->tbss_size <= rtld->tls_size);
194 offset += m->tbss_size;
195 memset(data + rtld->tls_size - offset, 0, m->tbss_size);
196 offset += m->tdata_size;
197 memcpy(data + rtld->tls_size - offset, m->tdata, m->tdata_size);
[d2bb25e7]198 dtv[i++] = data + rtld->tls_size - offset;
[29405ac]199 }
200#endif
[6adb775f]201
[d2bb25e7]202 tcb->dtv = dtv;
[6adb775f]203 return tcb;
204}
205
206unsigned long rtld_get_next_id(rtld_t *rtld)
207{
208 return rtld->next_id++;
209}
210
[d2bb25e7]211/** Get address of thread-local variable.
212 *
213 * @param rtld RTLD instance
214 * @param tcb TCB of the thread whose instance to return
215 * @param mod_id Module ID
216 * @param offset Offset within TLS block of the module
217 *
218 * @return Address of thread-local variable
219 */
220void *rtld_tls_get_addr(rtld_t *rtld, tcb_t *tcb, unsigned long mod_id,
[6adb775f]221 unsigned long offset)
222{
[9c07c3d]223 module_t *m;
224 size_t dtv_len;
225 void *tls_block;
226
227 dtv_len = DTV_GN(tcb->dtv);
228 if (dtv_len < mod_id) {
229 /* Vector is short */
230
231 tcb->dtv = realloc(tcb->dtv, (1 + mod_id) * sizeof(void *));
232 /* XXX This can fail if OOM */
233 assert(tcb->dtv != NULL);
234 /* Zero out new part of vector */
235 memset(tcb->dtv + (1 + dtv_len), 0, (mod_id - dtv_len) *
236 sizeof(void *));
237 }
238
239 if (tcb->dtv[mod_id] == NULL) {
240 /* TLS block is not allocated */
241
242 m = module_by_id(rtld, mod_id);
243 assert(m != NULL);
244 /* Should not be initial module, those have TLS pre-allocated */
245 assert(!link_used(&m->imodules_link));
246
247 tls_block = malloc(m->tdata_size + m->tbss_size);
248 /* XXX This can fail if OOM */
249 assert(tls_block != NULL);
250
251 /* Copy tdata */
252 memcpy(tls_block, m->tdata, m->tdata_size);
253 /* Zero out tbss */
254 memset(tls_block + m->tdata_size, 0, m->tbss_size);
255
256 tcb->dtv[mod_id] = tls_block;
[e2f26002]257 }
258
[d2bb25e7]259 return (uint8_t *)(tcb->dtv[mod_id]) + offset;
[6adb775f]260}
261
[1ea99cc]262/** @}
263 */
Note: See TracBrowser for help on using the repository browser.