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 <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <fcntl.h>
|
---|
41 | #include <adt/list.h>
|
---|
42 | #include <loader/pcb.h>
|
---|
43 |
|
---|
44 | #include <rtld.h>
|
---|
45 | #include <rtld_debug.h>
|
---|
46 | #include <dynamic.h>
|
---|
47 | #include <elf_load.h>
|
---|
48 | #include <rtld_arch.h>
|
---|
49 | #include <module.h>
|
---|
50 |
|
---|
51 | /** (Eagerly) process all relocation tables in a module.
|
---|
52 | *
|
---|
53 | * Currently works as if LD_BIND_NOW was specified.
|
---|
54 | */
|
---|
55 | void 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 | */
|
---|
93 | module_t *module_find(const char *name)
|
---|
94 | {
|
---|
95 | link_t *head = &runtime_env->modules_head;
|
---|
96 |
|
---|
97 | link_t *cur;
|
---|
98 | module_t *m;
|
---|
99 | const char *p, *soname;
|
---|
100 |
|
---|
101 | DPRINTF("module_find('%s')\n", name);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * If name contains slashes, treat it as a pathname and
|
---|
105 | * construct soname by chopping off the path. Otherwise
|
---|
106 | * treat it as soname.
|
---|
107 | */
|
---|
108 | p = str_rchr(name, '/');
|
---|
109 | soname = p ? (p + 1) : name;
|
---|
110 |
|
---|
111 | /* Traverse list of all modules. Not extremely fast, but simple */
|
---|
112 | DPRINTF("head = %p\n", head);
|
---|
113 | for (cur = head->next; cur != head; cur = cur->next) {
|
---|
114 | DPRINTF("cur = %p\n", cur);
|
---|
115 | m = list_get_instance(cur, module_t, modules_link);
|
---|
116 | if (str_cmp(m->dyn.soname, soname) == 0) {
|
---|
117 | return m; /* Found */
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | return NULL; /* Not found */
|
---|
122 | }
|
---|
123 |
|
---|
124 | #define NAME_BUF_SIZE 64
|
---|
125 |
|
---|
126 | /** Load a module.
|
---|
127 | *
|
---|
128 | * Currently this trivially tries to load '/<name>'.
|
---|
129 | */
|
---|
130 | module_t *module_load(const char *name)
|
---|
131 | {
|
---|
132 | elf_info_t info;
|
---|
133 | char name_buf[NAME_BUF_SIZE];
|
---|
134 | module_t *m;
|
---|
135 | int rc;
|
---|
136 |
|
---|
137 | m = malloc(sizeof(module_t));
|
---|
138 | if (!m) {
|
---|
139 | printf("malloc failed\n");
|
---|
140 | exit(1);
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (str_size(name) > NAME_BUF_SIZE - 2) {
|
---|
144 | printf("soname too long. increase NAME_BUF_SIZE\n");
|
---|
145 | exit(1);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* Prepend soname with '/lib/' */
|
---|
149 | str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
|
---|
150 | str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
|
---|
151 |
|
---|
152 | /* FIXME: need to real allocation of address space */
|
---|
153 | m->bias = runtime_env->next_bias;
|
---|
154 | runtime_env->next_bias += 0x100000;
|
---|
155 |
|
---|
156 | DPRINTF("filename:'%s'\n", name_buf);
|
---|
157 | DPRINTF("load '%s' at 0x%x\n", name_buf, m->bias);
|
---|
158 |
|
---|
159 | rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info);
|
---|
160 | if (rc != EE_OK) {
|
---|
161 | printf("Failed to load '%s'\n", name_buf);
|
---|
162 | exit(1);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (info.dynamic == NULL) {
|
---|
166 | printf("Error: '%s' is not a dynamically-linked object.\n",
|
---|
167 | name_buf);
|
---|
168 | exit(1);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Pending relocation. */
|
---|
172 | m->relocated = false;
|
---|
173 |
|
---|
174 | DPRINTF("parse dynamic section\n");
|
---|
175 | /* Parse ELF .dynamic section. Store info to m->dyn. */
|
---|
176 | dynamic_parse(info.dynamic, m->bias, &m->dyn);
|
---|
177 |
|
---|
178 | /* Insert into the list of loaded modules */
|
---|
179 | list_append(&m->modules_link, &runtime_env->modules_head);
|
---|
180 |
|
---|
181 | return m;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** Load all modules on which m (transitively) depends.
|
---|
185 | */
|
---|
186 | void module_load_deps(module_t *m)
|
---|
187 | {
|
---|
188 | elf_dyn_t *dp;
|
---|
189 | char *dep_name;
|
---|
190 | module_t *dm;
|
---|
191 | size_t n, i;
|
---|
192 |
|
---|
193 | DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
|
---|
194 |
|
---|
195 | /* Count direct dependencies */
|
---|
196 |
|
---|
197 | dp = m->dyn.dynamic;
|
---|
198 | n = 0;
|
---|
199 |
|
---|
200 | while (dp->d_tag != DT_NULL) {
|
---|
201 | if (dp->d_tag == DT_NEEDED) ++n;
|
---|
202 | ++dp;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Create an array of pointers to direct dependencies */
|
---|
206 |
|
---|
207 | m->n_deps = n;
|
---|
208 |
|
---|
209 | if (n == 0) {
|
---|
210 | /* There are no dependencies, so we are done. */
|
---|
211 | m->deps = NULL;
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | m->deps = malloc(n * sizeof(module_t *));
|
---|
216 | if (!m->deps) {
|
---|
217 | printf("malloc failed\n");
|
---|
218 | exit(1);
|
---|
219 | }
|
---|
220 |
|
---|
221 | i = 0; /* Current dependency index */
|
---|
222 | dp = m->dyn.dynamic;
|
---|
223 |
|
---|
224 | while (dp->d_tag != DT_NULL) {
|
---|
225 | if (dp->d_tag == DT_NEEDED) {
|
---|
226 | dep_name = m->dyn.str_tab + dp->d_un.d_val;
|
---|
227 |
|
---|
228 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
|
---|
229 | dm = module_find(dep_name);
|
---|
230 | if (!dm) {
|
---|
231 | dm = module_load(dep_name);
|
---|
232 | module_load_deps(dm);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /* Save into deps table */
|
---|
236 | m->deps[i++] = dm;
|
---|
237 | }
|
---|
238 | ++dp;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** Process relocations in modules.
|
---|
243 | *
|
---|
244 | * Processes relocations in @a start and all its dependencies.
|
---|
245 | * Modules that have already been relocated are unaffected.
|
---|
246 | *
|
---|
247 | * @param start The module where to start from.
|
---|
248 | */
|
---|
249 | void modules_process_relocs(module_t *start)
|
---|
250 | {
|
---|
251 | link_t *head = &runtime_env->modules_head;
|
---|
252 |
|
---|
253 | link_t *cur;
|
---|
254 | module_t *m;
|
---|
255 |
|
---|
256 | for (cur = head->next; cur != head; cur = cur->next) {
|
---|
257 | m = list_get_instance(cur, module_t, modules_link);
|
---|
258 |
|
---|
259 | /* Skip rtld, since it has already been processed */
|
---|
260 | if (m != &runtime_env->rtld) {
|
---|
261 | module_process_relocs(m);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | /** Clear BFS tags of all modules.
|
---|
267 | */
|
---|
268 | void modules_untag(void)
|
---|
269 | {
|
---|
270 | link_t *head = &runtime_env->modules_head;
|
---|
271 |
|
---|
272 | link_t *cur;
|
---|
273 | module_t *m;
|
---|
274 |
|
---|
275 | for (cur = head->next; cur != head; cur = cur->next) {
|
---|
276 | m = list_get_instance(cur, module_t, modules_link);
|
---|
277 | m->bfs_tag = false;
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** @}
|
---|
282 | */
|
---|