source: mainline/uspace/lib/c/generic/rtld/module.c@ 4b63316

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4b63316 was 4b63316, checked in by Jakub Jermar <jakub@…>, 12 years ago

Fix build when dynamic linking is enabled.

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