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