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

Last change on this file was 32254d6, checked in by GitHub <noreply@…>, 5 months ago

init RTLD runtime at load time even for statically linked binaries (#242)

init RTLD runtime at load time even for statically linked binaries

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[1ea99cc]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
[b1834a01]29/** @addtogroup rtld
[1ea99cc]30 * @brief
31 * @{
[1b20da0]32 */
[1ea99cc]33/**
34 * @file
35 */
36
[17341d4]37#include <errno.h>
38#include <rtld/module.h>
[8a1fb09]39#include <rtld/rtld.h>
[17341d4]40#include <rtld/rtld_debug.h>
41#include <stdlib.h>
[1d6dd2a]42#include <str.h>
[1ea99cc]43
[17341d4]44rtld_t *runtime_env;
[1ea99cc]45
[32254d6]46/** Initialize and process an executable.
[17341d4]47 *
48 * @param p_info Program info
49 * @return EOK on success or non-zero error code
50 */
[b7fd2a0]51errno_t rtld_prog_process(elf_finfo_t *p_info, rtld_t **rre)
[17341d4]52{
53 rtld_t *env;
[32254d6]54 bool is_dynamic = p_info->dynamic != NULL;
55 DPRINTF("rtld_prog_process\n");
[17341d4]56
57 /* Allocate new RTLD environment to pass to the loaded program */
58 env = calloc(1, sizeof(rtld_t));
59 if (env == NULL)
60 return ENOMEM;
61
[32254d6]62 list_initialize(&env->modules);
63 list_initialize(&env->imodules);
[6adb775f]64 env->next_id = 1;
65
[32254d6]66 module_t *module;
67 errno_t rc = module_create_entrypoint(p_info, env, &module);
68 if (rc != EOK) {
[6adb775f]69 free(env);
[32254d6]70 return rc;
[6adb775f]71 }
72
[17341d4]73 /* Pointer to program module. Used as root of the module graph. */
[32254d6]74 env->program = module;
[17341d4]75
76 /*
77 * Now we can continue with loading all other modules.
78 */
79
[32254d6]80 if (is_dynamic) {
81 DPRINTF("Load all program dependencies\n");
82 rc = module_load_deps(module, 0);
83 if (rc != EOK) {
84 free(module);
85 free(env);
86 return rc;
87 }
[967e7a1]88 }
[17341d4]89
[29405ac]90 /* Compute static TLS size */
91 modules_process_tls(env);
92
[17341d4]93 /*
94 * Now relocate/link all modules together.
95 */
96
[32254d6]97 if (is_dynamic) {
98 /* Process relocations in all modules */
99 DPRINTF("Relocate all modules\n");
100 modules_process_relocs(env, module);
101 }
[6adb775f]102
[32254d6]103 if (rre != NULL)
104 *rre = env;
[17341d4]105 return EOK;
106}
107
[6adb775f]108/** Create TLS (Thread Local Storage) data structures.
109 *
110 * @return Pointer to TCB.
111 */
112tcb_t *rtld_tls_make(rtld_t *rtld)
113{
114 tcb_t *tcb;
[d2bb25e7]115 void **dtv;
116 size_t nmods;
117 size_t i;
[6adb775f]118
[4f205248]119 tcb = tls_alloc_arch(rtld->tls_size, rtld->tls_align);
[6adb775f]120 if (tcb == NULL)
121 return NULL;
122
[d2bb25e7]123 /** Allocate dynamic thread vector */
124 nmods = list_count(&rtld->imodules);
125 dtv = malloc((nmods + 1) * sizeof(void *));
126 if (dtv == NULL) {
127 tls_free(tcb);
128 return NULL;
129 }
130
131 /*
132 * We define generation number to be equal to vector length.
133 * We start with a vector covering the initially loaded modules.
134 */
135 DTV_GN(dtv) = nmods;
136
[6adb775f]137 /*
[d2bb25e7]138 * Copy thread local data from the initialization images of initial
139 * modules. Zero out thread-local uninitialized data.
[6adb775f]140 */
141
[1433ecda]142 i = 1;
[bab0f42]143 list_foreach(rtld->imodules, imodules_link, module_t, m) {
[4f205248]144 assert(i++ == m->id);
145
146 dtv[m->id] = (void *) tcb + m->tpoff;
147
148 assert(((uintptr_t) dtv[m->id]) % m->tls_align == 0);
149
[b83c5e4]150 if (m->tdata)
151 memcpy(dtv[m->id], m->tdata, m->tdata_size);
152
[4f205248]153 memset(dtv[m->id] + m->tdata_size, 0, m->tbss_size);
[29405ac]154 }
[6adb775f]155
[d2bb25e7]156 tcb->dtv = dtv;
[6adb775f]157 return tcb;
158}
159
160unsigned long rtld_get_next_id(rtld_t *rtld)
161{
162 return rtld->next_id++;
163}
164
[d2bb25e7]165/** Get address of thread-local variable.
166 *
167 * @param rtld RTLD instance
168 * @param tcb TCB of the thread whose instance to return
169 * @param mod_id Module ID
170 * @param offset Offset within TLS block of the module
171 *
172 * @return Address of thread-local variable
173 */
174void *rtld_tls_get_addr(rtld_t *rtld, tcb_t *tcb, unsigned long mod_id,
[6adb775f]175 unsigned long offset)
176{
[9c07c3d]177 module_t *m;
178 size_t dtv_len;
179 void *tls_block;
180
181 dtv_len = DTV_GN(tcb->dtv);
182 if (dtv_len < mod_id) {
183 /* Vector is short */
184
185 tcb->dtv = realloc(tcb->dtv, (1 + mod_id) * sizeof(void *));
186 /* XXX This can fail if OOM */
187 assert(tcb->dtv != NULL);
188 /* Zero out new part of vector */
189 memset(tcb->dtv + (1 + dtv_len), 0, (mod_id - dtv_len) *
190 sizeof(void *));
191 }
192
193 if (tcb->dtv[mod_id] == NULL) {
194 /* TLS block is not allocated */
195
196 m = module_by_id(rtld, mod_id);
197 assert(m != NULL);
198 /* Should not be initial module, those have TLS pre-allocated */
199 assert(!link_used(&m->imodules_link));
200
[4f205248]201 tls_block = memalign(m->tls_align, m->tdata_size + m->tbss_size);
[9c07c3d]202 /* XXX This can fail if OOM */
203 assert(tls_block != NULL);
204
205 /* Copy tdata */
206 memcpy(tls_block, m->tdata, m->tdata_size);
207 /* Zero out tbss */
208 memset(tls_block + m->tdata_size, 0, m->tbss_size);
209
210 tcb->dtv[mod_id] = tls_block;
[e2f26002]211 }
212
[d2bb25e7]213 return (uint8_t *)(tcb->dtv[mod_id]) + offset;
[6adb775f]214}
215
[1ea99cc]216/** @}
217 */
Note: See TracBrowser for help on using the repository browser.