source: mainline/uspace/lib/c/generic/rtld/rtld.c@ 1d6dd2a

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

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 7.0 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 void *data;
155 tcb_t *tcb;
156 size_t offset;
157 void **dtv;
158 size_t nmods;
159 size_t i;
160
161 tcb = tls_alloc_arch(&data, rtld->tls_size);
162 if (tcb == NULL)
163 return NULL;
164
165 /** Allocate dynamic thread vector */
166 nmods = list_count(&rtld->imodules);
167 dtv = malloc((nmods + 1) * sizeof(void *));
168 if (dtv == NULL) {
169 tls_free(tcb);
170 return NULL;
171 }
172
173 /*
174 * We define generation number to be equal to vector length.
175 * We start with a vector covering the initially loaded modules.
176 */
177 DTV_GN(dtv) = nmods;
178
179 /*
180 * Copy thread local data from the initialization images of initial
181 * modules. Zero out thread-local uninitialized data.
182 */
183
184#ifdef CONFIG_TLS_VARIANT_1
185 /*
186 * Ascending addresses
187 */
188 offset = 0; i = 1;
189 list_foreach(rtld->imodules, imodules_link, module_t, m) {
190 assert(i == m->id);
191 assert(offset + m->tdata_size + m->tbss_size <= rtld->tls_size);
192 dtv[i++] = data + offset;
193 memcpy(data + offset, m->tdata, m->tdata_size);
194 offset += m->tdata_size;
195 memset(data + offset, 0, m->tbss_size);
196 offset += m->tbss_size;
197 }
198#else /* CONFIG_TLS_VARIANT_2 */
199 /*
200 * Descending addresses
201 */
202 offset = 0; i = 1;
203 list_foreach(rtld->imodules, imodules_link, module_t, m) {
204 assert(i == m->id);
205 assert(offset + m->tdata_size + m->tbss_size <= rtld->tls_size);
206 offset += m->tbss_size;
207 memset(data + rtld->tls_size - offset, 0, m->tbss_size);
208 offset += m->tdata_size;
209 memcpy(data + rtld->tls_size - offset, m->tdata, m->tdata_size);
210 dtv[i++] = data + rtld->tls_size - offset;
211 }
212#endif
213
214 tcb->dtv = dtv;
215 return tcb;
216}
217
218unsigned long rtld_get_next_id(rtld_t *rtld)
219{
220 return rtld->next_id++;
221}
222
223/** Get address of thread-local variable.
224 *
225 * @param rtld RTLD instance
226 * @param tcb TCB of the thread whose instance to return
227 * @param mod_id Module ID
228 * @param offset Offset within TLS block of the module
229 *
230 * @return Address of thread-local variable
231 */
232void *rtld_tls_get_addr(rtld_t *rtld, tcb_t *tcb, unsigned long mod_id,
233 unsigned long offset)
234{
235 module_t *m;
236 size_t dtv_len;
237 void *tls_block;
238
239 dtv_len = DTV_GN(tcb->dtv);
240 if (dtv_len < mod_id) {
241 /* Vector is short */
242
243 tcb->dtv = realloc(tcb->dtv, (1 + mod_id) * sizeof(void *));
244 /* XXX This can fail if OOM */
245 assert(tcb->dtv != NULL);
246 /* Zero out new part of vector */
247 memset(tcb->dtv + (1 + dtv_len), 0, (mod_id - dtv_len) *
248 sizeof(void *));
249 }
250
251 if (tcb->dtv[mod_id] == NULL) {
252 /* TLS block is not allocated */
253
254 m = module_by_id(rtld, mod_id);
255 assert(m != NULL);
256 /* Should not be initial module, those have TLS pre-allocated */
257 assert(!link_used(&m->imodules_link));
258
259 tls_block = malloc(m->tdata_size + m->tbss_size);
260 /* XXX This can fail if OOM */
261 assert(tls_block != NULL);
262
263 /* Copy tdata */
264 memcpy(tls_block, m->tdata, m->tdata_size);
265 /* Zero out tbss */
266 memset(tls_block + m->tdata_size, 0, m->tbss_size);
267
268 tcb->dtv[mod_id] = tls_block;
269 }
270
271 return (uint8_t *)(tcb->dtv[mod_id]) + offset;
272}
273
274/** @}
275 */
Note: See TracBrowser for help on using the repository browser.