source: mainline/uspace/lib/c/generic/rtld/rtld.c@ a2eb85d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a2eb85d was 4f205248, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 8 years ago

Honor TLS alignment.

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