source: mainline/uspace/lib/c/generic/rtld/module.c@ 6adb775f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6adb775f was 6adb775f, checked in by Jiri Svoboda <jiri@…>, 9 years ago

TLS for dynamically linked executables and initially loaded DSOs (but must not call dlopen or there will be trouble).

  • Property mode set to 100644
File size: 7.1 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 <adt/list.h>
38#include <elf/elf_load.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <loader/pcb.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <unistd.h>
45
46#include <rtld/rtld.h>
47#include <rtld/rtld_debug.h>
48#include <rtld/dynamic.h>
49#include <rtld/rtld_arch.h>
50#include <rtld/module.h>
51
52/** (Eagerly) process all relocation tables in a module.
53 *
54 * Currently works as if LD_BIND_NOW was specified.
55 */
56void module_process_relocs(module_t *m)
57{
58 DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
59
60 /* Do not relocate twice. */
61 if (m->relocated) return;
62
63 module_process_pre_arch(m);
64
65 if (m->dyn.plt_rel == DT_REL) {
66 DPRINTF("table type DT_REL\n");
67 if (m->dyn.rel != NULL) {
68 DPRINTF("non-empty\n");
69 rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
70 }
71 /* FIXME: this seems wrong */
72 if (m->dyn.jmp_rel != NULL) {
73 DPRINTF("table type jmp-rel\n");
74 DPRINTF("non-empty\n");
75 rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
76 }
77 } else { /* (m->dyn.plt_rel == DT_RELA) */
78 DPRINTF("table type DT_RELA\n");
79 if (m->dyn.rela != NULL) {
80 DPRINTF("non-empty\n");
81 rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
82 }
83 }
84
85 m->relocated = true;
86}
87
88/** Find module structure by soname/pathname.
89 *
90 * Used primarily to see if a module has already been loaded.
91 * Modules are compared according to their soname, i.e. possible
92 * path components are ignored.
93 */
94module_t *module_find(rtld_t *rtld, const char *name)
95{
96 const char *p, *soname;
97
98 DPRINTF("module_find('%s')\n", name);
99
100 /*
101 * If name contains slashes, treat it as a pathname and
102 * construct soname by chopping off the path. Otherwise
103 * treat it as soname.
104 */
105 p = str_rchr(name, '/');
106 soname = p ? (p + 1) : name;
107
108 /* Traverse list of all modules. Not extremely fast, but simple */
109 list_foreach(rtld->modules, modules_link, module_t, m) {
110 DPRINTF("m = %p\n", m);
111 if (str_cmp(m->dyn.soname, soname) == 0) {
112 return m; /* Found */
113 }
114 }
115
116 return NULL; /* Not found */
117}
118
119#define NAME_BUF_SIZE 64
120
121/** Load a module.
122 *
123 * Currently this trivially tries to load '/<name>'.
124 */
125module_t *module_load(rtld_t *rtld, const char *name)
126{
127 elf_finfo_t info;
128 char name_buf[NAME_BUF_SIZE];
129 module_t *m;
130 int rc;
131
132 m = calloc(1, sizeof(module_t));
133 if (m == NULL) {
134 printf("malloc failed\n");
135 exit(1);
136 }
137
138 m->rtld = rtld;
139 m->id = rtld_get_next_id(rtld);
140
141 if (str_size(name) > NAME_BUF_SIZE - 2) {
142 printf("soname too long. increase NAME_BUF_SIZE\n");
143 exit(1);
144 }
145
146 /* Prepend soname with '/lib/' */
147 str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
148 str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
149
150 /* FIXME: need to real allocation of address space */
151 m->bias = rtld->next_bias;
152 rtld->next_bias += 0x100000;
153
154 DPRINTF("filename:'%s'\n", name_buf);
155 DPRINTF("load '%s' at 0x%x\n", name_buf, m->bias);
156
157 rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info);
158 if (rc != EE_OK) {
159 printf("Failed to load '%s'\n", name_buf);
160 exit(1);
161 }
162
163 if (info.dynamic == NULL) {
164 printf("Error: '%s' is not a dynamically-linked object.\n",
165 name_buf);
166 exit(1);
167 }
168
169 /* Pending relocation. */
170 m->relocated = false;
171
172 DPRINTF("parse dynamic section\n");
173 /* Parse ELF .dynamic section. Store info to m->dyn. */
174 dynamic_parse(info.dynamic, m->bias, &m->dyn);
175
176 /* Insert into the list of loaded modules */
177 list_append(&m->modules_link, &rtld->modules);
178
179 /* Copy TLS info */
180 m->tdata = info.tls.tdata;
181 m->tdata_size = info.tls.tdata_size;
182 m->tbss_size = info.tls.tbss_size;
183
184 printf("tdata at %p size %zu, tbss size %zu\n",
185 m->tdata, m->tdata_size, m->tbss_size);
186
187 return m;
188}
189
190/** Load all modules on which m (transitively) depends.
191 */
192void module_load_deps(module_t *m)
193{
194 elf_dyn_t *dp;
195 char *dep_name;
196 module_t *dm;
197 size_t n, i;
198
199 DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
200
201 /* Count direct dependencies */
202
203 dp = m->dyn.dynamic;
204 n = 0;
205
206 while (dp->d_tag != DT_NULL) {
207 if (dp->d_tag == DT_NEEDED) ++n;
208 ++dp;
209 }
210
211 /* Create an array of pointers to direct dependencies */
212
213 m->n_deps = n;
214
215 if (n == 0) {
216 /* There are no dependencies, so we are done. */
217 m->deps = NULL;
218 return;
219 }
220
221 m->deps = malloc(n * sizeof(module_t *));
222 if (!m->deps) {
223 printf("malloc failed\n");
224 exit(1);
225 }
226
227 i = 0; /* Current dependency index */
228 dp = m->dyn.dynamic;
229
230 while (dp->d_tag != DT_NULL) {
231 if (dp->d_tag == DT_NEEDED) {
232 dep_name = m->dyn.str_tab + dp->d_un.d_val;
233
234 DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
235 dm = module_find(m->rtld, dep_name);
236 if (!dm) {
237 dm = module_load(m->rtld, dep_name);
238 module_load_deps(dm);
239 }
240
241 /* Save into deps table */
242 m->deps[i++] = dm;
243 }
244 ++dp;
245 }
246}
247
248/** Find module structure by ID. */
249module_t *module_by_id(rtld_t *rtld, unsigned long id)
250{
251 list_foreach(rtld->modules, modules_link, module_t, m) {
252 if (m->id == id)
253 return m;
254 }
255
256 return NULL;
257}
258
259/** Process relocations in modules.
260 *
261 * Processes relocations in @a start and all its dependencies.
262 * Modules that have already been relocated are unaffected.
263 *
264 * @param start The module where to start from.
265 */
266void modules_process_relocs(rtld_t *rtld, module_t *start)
267{
268 list_foreach(rtld->modules, modules_link, module_t, m) {
269 /* Skip rtld module, since it has already been processed */
270 if (m != &rtld->rtld) {
271 module_process_relocs(m);
272 }
273 }
274}
275
276void modules_process_tls(rtld_t *rtld)
277{
278 list_foreach(rtld->modules, modules_link, module_t, m) {
279 m->ioffs = rtld->tls_size;
280 rtld->tls_size += m->tdata_size + m->tbss_size;
281 }
282}
283
284/** Clear BFS tags of all modules.
285 */
286void modules_untag(rtld_t *rtld)
287{
288 list_foreach(rtld->modules, modules_link, module_t, m) {
289 m->bfs_tag = false;
290 }
291}
292
293/** @}
294 */
Note: See TracBrowser for help on using the repository browser.