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
Line 
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
37#include <errno.h>
38#include <rtld/module.h>
39#include <rtld/rtld.h>
40#include <rtld/rtld_debug.h>
41#include <stdlib.h>
42
43rtld_t *runtime_env;
44static rtld_t rt_env_static;
45
46/** Initialize the runtime linker for use in a statically-linked executable. */
47void rtld_init_static(void)
48{
49 runtime_env = &rt_env_static;
50 list_initialize(&runtime_env->modules);
51 list_initialize(&runtime_env->imodules);
52 runtime_env->next_bias = 0x2000000;
53 runtime_env->program = NULL;
54}
55
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;
64 module_t *prog;
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
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
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);
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);
92 prog->exec = true;
93 prog->local = false;
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;
98 prog->tls_align = p_info->tls.tls_align;
99
100 printf("prog tdata at %p size %zu, tbss size %zu\n",
101 prog->tdata, prog->tdata_size, prog->tbss_size);
102
103 /* Initialize list of loaded modules */
104 list_initialize(&env->modules);
105 list_initialize(&env->imodules);
106 list_append(&prog->modules_link, &env->modules);
107
108 /* Pointer to program module. Used as root of the module graph. */
109 env->program = prog;
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");
119 module_load_deps(prog, 0);
120
121 /* Compute static TLS size */
122 modules_process_tls(env);
123
124 /*
125 * Now relocate/link all modules together.
126 */
127
128 /* Process relocations in all modules */
129 DPRINTF("Relocate all modules\n");
130 modules_process_relocs(env, prog);
131
132 *rre = env;
133 return EOK;
134}
135
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;
145 void **dtv;
146 size_t nmods;
147 size_t i;
148
149 tcb = tls_alloc_arch(&data, rtld->tls_size);
150 if (tcb == NULL)
151 return NULL;
152
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
167 /*
168 * Copy thread local data from the initialization images of initial
169 * modules. Zero out thread-local uninitialized data.
170 */
171
172#ifdef CONFIG_TLS_VARIANT_1
173 /*
174 * Ascending addresses
175 */
176 offset = 0; i = 1;
177 list_foreach(rtld->imodules, imodules_link, module_t, m) {
178 assert(i == m->id);
179 assert(offset + m->tdata_size + m->tbss_size <= rtld->tls_size);
180 dtv[i++] = data + offset;
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 }
186#else /* CONFIG_TLS_VARIANT_2 */
187 /*
188 * Descending addresses
189 */
190 offset = 0; i = 1;
191 list_foreach(rtld->imodules, imodules_link, module_t, m) {
192 assert(i == m->id);
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);
198 dtv[i++] = data + rtld->tls_size - offset;
199 }
200#endif
201
202 tcb->dtv = dtv;
203 return tcb;
204}
205
206unsigned long rtld_get_next_id(rtld_t *rtld)
207{
208 return rtld->next_id++;
209}
210
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,
221 unsigned long offset)
222{
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;
257 }
258
259 return (uint8_t *)(tcb->dtv[mod_id]) + offset;
260}
261
262/** @}
263 */
Note: See TracBrowser for help on using the repository browser.