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