source: mainline/uspace/lib/c/generic/elf/elf_mod.c@ 8aea932

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8aea932 was bdca26a, checked in by Jakub Jermář <jakub@…>, 6 years ago

Removing printf when failing from lib/rtld

If rtld failed a message was printed with printf.
This has been replaced with DPRINTF which
gives control to the developer over the message

  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[17341d4]1/*
2 * Copyright (c) 2006 Sergey Bondari
3 * Copyright (c) 2006 Jakub Jermar
4 * Copyright (c) 2011 Jiri Svoboda
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
[174156fd]31/** @addtogroup libc
[17341d4]32 * @{
33 */
34
35/**
36 * @file
37 * @brief Userspace ELF module loader.
38 *
39 * This module allows loading ELF binaries (both executables and
40 * shared objects) from VFS. The current implementation allocates
41 * anonymous memory, fills it with segment data and then adjusts
42 * the memory areas' flags to the final value. In the future,
43 * the segments will be mapped directly from the file.
44 */
45
[e796dc8]46#include <errno.h>
[17341d4]47#include <stdio.h>
[e796dc8]48#include <vfs/vfs.h>
[8d2dd7f2]49#include <stddef.h>
50#include <stdint.h>
[17341d4]51#include <align.h>
52#include <assert.h>
53#include <as.h>
54#include <elf/elf.h>
55#include <smc.h>
56#include <loader/pcb.h>
57#include <entry_point.h>
[e796dc8]58#include <str_error.h>
59#include <stdlib.h>
[742fc98e]60#include <macros.h>
[17341d4]61
62#include <elf/elf_load.h>
63
64#define DPRINTF(...)
65
[bdca26a]66static errno_t elf_load_module(elf_ld_t *elf);
67static errno_t segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
68static errno_t load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
[17341d4]69
70/** Load ELF binary from a file.
71 *
72 * Load an ELF binary from the specified file. If the file is
73 * an executable program, it is loaded unbiased. If it is a shared
74 * object, it is loaded with the bias @a so_bias. Some information
75 * extracted from the binary is stored in a elf_info_t structure
76 * pointed to by @a info.
77 *
[e796dc8]78 * @param file ELF file.
[17341d4]79 * @param info Pointer to a structure for storing information
80 * extracted from the binary.
81 *
[bdca26a]82 * @return EOK on success or an error code.
[17341d4]83 *
84 */
[bdca26a]85errno_t elf_load_file(int file, eld_flags_t flags, elf_finfo_t *info)
[17341d4]86{
87 elf_ld_t elf;
88
[f77c1c9]89 int ofile;
[b7fd2a0]90 errno_t rc = vfs_clone(file, -1, true, &ofile);
[f77c1c9]91 if (rc == EOK) {
92 rc = vfs_open(ofile, MODE_READ);
93 }
[e796dc8]94 if (rc != EOK) {
[bdca26a]95 return rc;
[17341d4]96 }
97
[e796dc8]98 elf.fd = ofile;
[17341d4]99 elf.info = info;
100 elf.flags = flags;
101
[bdca26a]102 rc = elf_load_module(&elf);
[17341d4]103
[9c4cf0d]104 vfs_put(ofile);
[bdca26a]105 return rc;
[e796dc8]106}
[17341d4]107
[bdca26a]108errno_t elf_load_file_name(const char *path, eld_flags_t flags, elf_finfo_t *info)
[e796dc8]109{
[f77c1c9]110 int file;
[b7fd2a0]111 errno_t rc = vfs_lookup(path, 0, &file);
[f77c1c9]112 if (rc == EOK) {
[bdca26a]113 rc = elf_load_file(file, flags, info);
[f77c1c9]114 vfs_put(file);
[bdca26a]115 return rc;
[1afa94d]116 } else {
[bdca26a]117 return EIO;
[f77c1c9]118 }
[17341d4]119}
120
121/** Load an ELF binary.
122 *
123 * The @a elf structure contains the loader state, including
124 * an open file, from which the binary will be loaded,
125 * a pointer to the @c info structure etc.
126 *
127 * @param elf Pointer to loader state buffer.
[bdca26a]128 * @return EOK on success or an error code.
[17341d4]129 */
[bdca26a]130static errno_t elf_load_module(elf_ld_t *elf)
[17341d4]131{
132 elf_header_t header_buf;
133 elf_header_t *header = &header_buf;
[58898d1d]134 aoff64_t pos = 0;
[8e3498b]135 size_t nr;
[bdca26a]136 int i;
[b7fd2a0]137 errno_t rc;
[17341d4]138
[8e3498b]139 rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t), &nr);
140 if (rc != EOK || nr != sizeof(elf_header_t)) {
[1b20da0]141 DPRINTF("Read error.\n");
[bdca26a]142 return EIO;
[17341d4]143 }
144
145 /* Identify ELF */
146 if (header->e_ident[EI_MAG0] != ELFMAG0 ||
[1b20da0]147 header->e_ident[EI_MAG1] != ELFMAG1 ||
[17341d4]148 header->e_ident[EI_MAG2] != ELFMAG2 ||
149 header->e_ident[EI_MAG3] != ELFMAG3) {
150 DPRINTF("Invalid header.\n");
[bdca26a]151 return EINVAL;
[17341d4]152 }
[a35b458]153
[17341d4]154 /* Identify ELF compatibility */
155 if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
[1b20da0]156 header->e_machine != ELF_MACHINE ||
[17341d4]157 header->e_ident[EI_VERSION] != EV_CURRENT ||
158 header->e_version != EV_CURRENT ||
159 header->e_ident[EI_CLASS] != ELF_CLASS) {
160 DPRINTF("Incompatible data/version/class.\n");
[bdca26a]161 return EINVAL;
[17341d4]162 }
163
164 if (header->e_phentsize != sizeof(elf_segment_header_t)) {
165 DPRINTF("e_phentsize: %u != %zu\n", header->e_phentsize,
166 sizeof(elf_segment_header_t));
[bdca26a]167 return EINVAL;
[17341d4]168 }
169
170 /* Check if the object type is supported. */
171 if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
172 DPRINTF("Object type %d is not supported\n", header->e_type);
[bdca26a]173 return ENOTSUP;
[17341d4]174 }
175
[742fc98e]176 if (header->e_phoff == 0) {
177 DPRINTF("Program header table is not present!\n");
[bdca26a]178 return ENOTSUP;
[742fc98e]179 }
180
[7c3fb9b]181 /*
182 * Read program header table.
[742fc98e]183 * Normally, there are very few program headers, so don't bother
184 * with allocating memory dynamically.
185 */
186 const int phdr_cap = 16;
187 elf_segment_header_t phdr[phdr_cap];
188 size_t phdr_len = header->e_phnum * header->e_phentsize;
189
[c4049e6]190 elf->info->interp = NULL;
191 elf->info->dynamic = NULL;
192
[742fc98e]193 if (phdr_len > sizeof(phdr)) {
194 DPRINTF("more than %d program headers\n", phdr_cap);
[bdca26a]195 return ENOTSUP;
[742fc98e]196 }
197
198 pos = header->e_phoff;
199 rc = vfs_read(elf->fd, &pos, phdr, phdr_len, &nr);
200 if (rc != EOK || nr != phdr_len) {
201 DPRINTF("Read error.\n");
[bdca26a]202 return EIO;
[742fc98e]203 }
204
205 uintptr_t module_base = UINTPTR_MAX;
206 uintptr_t module_top = 0;
207 uintptr_t base_offset = UINTPTR_MAX;
208
209 /* Walk through PT_LOAD headers, to find out the size of the module. */
210 for (i = 0; i < header->e_phnum; i++) {
211 if (phdr[i].p_type != PT_LOAD)
212 continue;
213
214 if (module_base > phdr[i].p_vaddr) {
215 module_base = phdr[i].p_vaddr;
216 base_offset = phdr[i].p_offset;
217 }
218 module_top = max(module_top, phdr[i].p_vaddr + phdr[i].p_memsz);
219 }
220
221 if (base_offset != 0) {
222 DPRINTF("ELF headers not present in the text segment.\n");
[bdca26a]223 return EINVAL;
[742fc98e]224 }
225
[17341d4]226 /* Shared objects can be loaded with a bias */
[742fc98e]227 if (header->e_type != ET_DYN) {
[17341d4]228 elf->bias = 0;
[742fc98e]229 } else {
230 if (module_base != 0) {
231 DPRINTF("Unexpected shared object format.\n");
[bdca26a]232 return EINVAL;
[742fc98e]233 }
234
[7c3fb9b]235 /*
236 * Attempt to allocate a span of memory large enough for the
[742fc98e]237 * shared object.
238 */
239 // FIXME: This is not reliable when we're running
240 // multi-threaded. Even if this part succeeds, later
241 // allocation can fail because another thread took the
242 // space in the meantime. This is only relevant for
243 // dlopen() though.
244 void *area = as_area_create(AS_AREA_ANY, module_top,
245 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE |
246 AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
247
248 if (area == AS_MAP_FAILED) {
249 DPRINTF("Can't find suitable memory area.\n");
[bdca26a]250 return ENOMEM;
[742fc98e]251 }
252
253 elf->bias = (uintptr_t) area;
254 as_area_destroy(area);
255 }
256
257 /* Load all loadable segments. */
258 for (i = 0; i < header->e_phnum; i++) {
259 if (phdr[i].p_type != PT_LOAD)
260 continue;
261
[bdca26a]262 rc = load_segment(elf, &phdr[i]);
263 if (rc != EOK)
264 return rc;
[742fc98e]265 }
266
267 void *base = (void *) module_base + elf->bias;
268 elf->info->base = base;
[17341d4]269
[4c5f04f]270 /* Ensure valid TLS info even if there is no TLS header. */
271 elf->info->tls.tdata = NULL;
272 elf->info->tls.tdata_size = 0;
273 elf->info->tls.tbss_size = 0;
274 elf->info->tls.tls_align = 1;
275
[17341d4]276 elf->info->interp = NULL;
277 elf->info->dynamic = NULL;
278
279 /* Walk through all segment headers and process them. */
280 for (i = 0; i < header->e_phnum; i++) {
[742fc98e]281 if (phdr[i].p_type == PT_LOAD)
282 continue;
[17341d4]283
[bdca26a]284 rc = segment_header(elf, &phdr[i]);
285 if (rc != EOK)
286 return rc;
[17341d4]287 }
288
289 elf->info->entry =
290 (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
291
292 DPRINTF("Done.\n");
293
[bdca26a]294 return EOK;
[17341d4]295}
296
[6adb775f]297/** Process TLS program header.
298 *
299 * @param elf Pointer to loader state buffer.
300 * @param hdr TLS program header
301 * @param info Place to store TLS info
302 */
303static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
304 elf_tls_info_t *info)
305{
306 info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
307 info->tdata_size = hdr->p_filesz;
308 info->tbss_size = hdr->p_memsz - hdr->p_filesz;
[29405ac]309 info->tls_align = hdr->p_align;
[6adb775f]310}
311
[17341d4]312/** Process segment header.
313 *
[6adb775f]314 * @param elf Pointer to loader state buffer.
[17341d4]315 * @param entry Segment header.
316 *
[bdca26a]317 * @return EOK on success, error code otherwise.
[17341d4]318 */
[bdca26a]319static errno_t segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
[17341d4]320{
321 switch (entry->p_type) {
322 case PT_NULL:
323 case PT_PHDR:
324 case PT_NOTE:
325 break;
[888a2c6]326 case PT_GNU_EH_FRAME:
327 case PT_GNU_STACK:
328 case PT_GNU_RELRO:
329 /* Ignore GNU headers, if present. */
330 break;
[17341d4]331 case PT_INTERP:
[57d44dd]332 elf->info->interp =
333 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
334
[2eadda9]335 if (entry->p_filesz == 0) {
336 DPRINTF("Zero-sized ELF interp string.\n");
[bdca26a]337 return EINVAL;
[2eadda9]338 }
[57d44dd]339 if (elf->info->interp[entry->p_filesz - 1] != '\0') {
340 DPRINTF("Unterminated ELF interp string.\n");
[bdca26a]341 return EINVAL;
[57d44dd]342 }
343 DPRINTF("interpreter: \"%s\"\n", elf->info->interp);
[17341d4]344 break;
345 case PT_DYNAMIC:
346 /* Record pointer to dynamic section into info structure */
347 elf->info->dynamic =
348 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
349 DPRINTF("dynamic section found at %p\n",
[3bacee1]350 (void *)elf->info->dynamic);
[17341d4]351 break;
352 case 0x70000000:
[e634684]353 case 0x70000001:
354 case 0x70000002:
355 case 0x70000003:
356 // FIXME: Architecture-specific headers.
357 /* PT_MIPS_REGINFO, PT_MIPS_ABIFLAGS, PT_ARM_UNWIND, ... */
[17341d4]358 break;
[6adb775f]359 case PT_TLS:
360 /* Parse TLS program header */
361 tls_program_header(elf, entry, &elf->info->tls);
362 DPRINTF("TLS header found at %p\n",
363 (void *)((uint8_t *)entry->p_vaddr + elf->bias));
364 break;
[17341d4]365 case PT_SHLIB:
366 default:
367 DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
[bdca26a]368 return ENOTSUP;
[17341d4]369 break;
370 }
[bdca26a]371 return EOK;
[17341d4]372}
373
374/** Load segment described by program header entry.
375 *
376 * @param elf Loader state.
377 * @param entry Program header entry describing segment to be loaded.
378 *
[bdca26a]379 * @return EOK on success, error code otherwise.
[17341d4]380 */
[bdca26a]381errno_t load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
[17341d4]382{
383 void *a;
384 int flags = 0;
385 uintptr_t bias;
386 uintptr_t base;
387 void *seg_ptr;
388 uintptr_t seg_addr;
389 size_t mem_sz;
[58898d1d]390 aoff64_t pos;
[b7fd2a0]391 errno_t rc;
[8e3498b]392 size_t nr;
[17341d4]393
394 bias = elf->bias;
395
396 seg_addr = entry->p_vaddr + bias;
397 seg_ptr = (void *) seg_addr;
398
[1c3c287]399 DPRINTF("Load segment v_addr=0x%zx at addr %p, size 0x%zx, flags %c%c%c\n",
400 entry->p_vaddr,
401 (void *) seg_addr,
[1567471]402 entry->p_memsz,
403 (entry->p_flags & PF_R) ? 'r' : '-',
404 (entry->p_flags & PF_W) ? 'w' : '-',
405 (entry->p_flags & PF_X) ? 'x' : '-');
[17341d4]406
407 if (entry->p_align > 1) {
408 if ((entry->p_offset % entry->p_align) !=
409 (seg_addr % entry->p_align)) {
410 DPRINTF("Align check 1 failed offset%%align=0x%zx, "
[1c3c287]411 "vaddr%%align=0x%zx align=0x%zx\n",
[17341d4]412 entry->p_offset % entry->p_align,
[1c3c287]413 seg_addr % entry->p_align, entry->p_align);
[bdca26a]414 return EINVAL;
[17341d4]415 }
416 }
417
418 /* Final flags that will be set for the memory area */
419
420 if (entry->p_flags & PF_X)
421 flags |= AS_AREA_EXEC;
422 if (entry->p_flags & PF_W)
423 flags |= AS_AREA_WRITE;
424 if (entry->p_flags & PF_R)
425 flags |= AS_AREA_READ;
426 flags |= AS_AREA_CACHEABLE;
[a35b458]427
[17341d4]428 base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
429 mem_sz = entry->p_memsz + (entry->p_vaddr - base);
430
431 DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
432 (void *) (entry->p_vaddr + bias +
433 ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
434
435 /*
436 * For the course of loading, the area needs to be readable
437 * and writeable.
438 */
439 a = as_area_create((uint8_t *) base + bias, mem_sz,
[6aeca0d]440 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
441 AS_AREA_UNPAGED);
[17341d4]442 if (a == AS_MAP_FAILED) {
443 DPRINTF("memory mapping failed (%p, %zu)\n",
444 (void *) (base + bias), mem_sz);
[bdca26a]445 return ENOMEM;
[17341d4]446 }
447
448 DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
449 (void *) (base + bias), mem_sz, flags, (void *) a);
450
451 /*
452 * Load segment data
453 */
[58898d1d]454 pos = entry->p_offset;
[8e3498b]455 rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
456 if (rc != EOK || nr != entry->p_filesz) {
[48178b56]457 DPRINTF("read error\n");
[bdca26a]458 return EIO;
[17341d4]459 }
460
461 /*
462 * The caller wants to modify the segments first. He will then
463 * need to set the right access mode and ensure SMC coherence.
464 */
[3bacee1]465 if ((elf->flags & ELDF_RW) != 0)
[bdca26a]466 return EOK;
[17341d4]467
[1567471]468 DPRINTF("as_area_change_flags(%p, %x)\n",
469 (uint8_t *) base + bias, flags);
470 rc = as_area_change_flags((uint8_t *) base + bias, flags);
[a53ed3a]471 if (rc != EOK) {
[17341d4]472 DPRINTF("Failed to set memory area flags.\n");
[bdca26a]473 return ENOMEM;
[17341d4]474 }
475
476 if (flags & AS_AREA_EXEC) {
477 /* Enforce SMC coherence for the segment */
478 if (smc_coherence(seg_ptr, entry->p_filesz))
[bdca26a]479 return ENOMEM;
[17341d4]480 }
481
[bdca26a]482 return EOK;
[17341d4]483}
484
485/** @}
486 */
Note: See TracBrowser for help on using the repository browser.