source: mainline/uspace/lib/c/generic/rtld/module.c@ 5035ba05

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

Default symbol search vs. dlsym's BFS search.

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