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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

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