source: mainline/uspace/lib/c/generic/elf/elf_mod.c@ 199b6d8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 199b6d8 was c735afb, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: fix problems caused by new HelenOS changes (and leftowers from rebase)

  • Property mode set to 100644
File size: 12.9 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
31/** @addtogroup generic
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
66static const char *error_codes[] = {
67 "no error",
68 "invalid image",
69 "address space error",
70 "incompatible image",
71 "unsupported image type",
[1afa94d]72 "irrecoverable error",
73 "file io error"
[17341d4]74};
75
[742fc98e]76static unsigned int elf_load_module(elf_ld_t *elf);
[17341d4]77static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
78static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
79
80/** Load ELF binary from a file.
81 *
82 * Load an ELF binary from the specified file. If the file is
83 * an executable program, it is loaded unbiased. If it is a shared
84 * object, it is loaded with the bias @a so_bias. Some information
85 * extracted from the binary is stored in a elf_info_t structure
86 * pointed to by @a info.
87 *
[e796dc8]88 * @param file ELF file.
[17341d4]89 * @param info Pointer to a structure for storing information
90 * extracted from the binary.
91 *
[1afa94d]92 * @return EE_OK on success or EE_xx error code.
[17341d4]93 *
94 */
[742fc98e]95int elf_load_file(int file, eld_flags_t flags, elf_finfo_t *info)
[17341d4]96{
97 elf_ld_t elf;
98
[f77c1c9]99 int ofile;
[b7fd2a0]100 errno_t rc = vfs_clone(file, -1, true, &ofile);
[f77c1c9]101 if (rc == EOK) {
102 rc = vfs_open(ofile, MODE_READ);
103 }
[e796dc8]104 if (rc != EOK) {
[1afa94d]105 return EE_IO;
[17341d4]106 }
107
[e796dc8]108 elf.fd = ofile;
[17341d4]109 elf.info = info;
110 elf.flags = flags;
111
[742fc98e]112 int ret = elf_load_module(&elf);
[17341d4]113
[9c4cf0d]114 vfs_put(ofile);
[1afa94d]115 return ret;
[e796dc8]116}
[17341d4]117
[742fc98e]118int elf_load_file_name(const char *path, eld_flags_t flags, elf_finfo_t *info)
[e796dc8]119{
[f77c1c9]120 int file;
[b7fd2a0]121 errno_t rc = vfs_lookup(path, 0, &file);
[f77c1c9]122 if (rc == EOK) {
[742fc98e]123 int ret = elf_load_file(file, flags, info);
[f77c1c9]124 vfs_put(file);
[1afa94d]125 return ret;
126 } else {
127 return EE_IO;
[f77c1c9]128 }
[17341d4]129}
130
131/** Load an ELF binary.
132 *
133 * The @a elf structure contains the loader state, including
134 * an open file, from which the binary will be loaded,
135 * a pointer to the @c info structure etc.
136 *
137 * @param elf Pointer to loader state buffer.
138 * @return EE_OK on success or EE_xx error code.
139 */
[742fc98e]140static unsigned int elf_load_module(elf_ld_t *elf)
[17341d4]141{
142 elf_header_t header_buf;
143 elf_header_t *header = &header_buf;
[58898d1d]144 aoff64_t pos = 0;
[8e3498b]145 size_t nr;
[1afa94d]146 int i, ret;
[b7fd2a0]147 errno_t rc;
[17341d4]148
[8e3498b]149 rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t), &nr);
150 if (rc != EOK || nr != sizeof(elf_header_t)) {
[1b20da0]151 DPRINTF("Read error.\n");
[1afa94d]152 return EE_IO;
[17341d4]153 }
154
155 /* Identify ELF */
156 if (header->e_ident[EI_MAG0] != ELFMAG0 ||
[1b20da0]157 header->e_ident[EI_MAG1] != ELFMAG1 ||
[17341d4]158 header->e_ident[EI_MAG2] != ELFMAG2 ||
159 header->e_ident[EI_MAG3] != ELFMAG3) {
160 DPRINTF("Invalid header.\n");
161 return EE_INVALID;
162 }
[a35b458]163
[17341d4]164 /* Identify ELF compatibility */
165 if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
[1b20da0]166 header->e_machine != ELF_MACHINE ||
[17341d4]167 header->e_ident[EI_VERSION] != EV_CURRENT ||
168 header->e_version != EV_CURRENT ||
169 header->e_ident[EI_CLASS] != ELF_CLASS) {
170 DPRINTF("Incompatible data/version/class.\n");
171 return EE_INCOMPATIBLE;
172 }
173
174 if (header->e_phentsize != sizeof(elf_segment_header_t)) {
175 DPRINTF("e_phentsize: %u != %zu\n", header->e_phentsize,
176 sizeof(elf_segment_header_t));
177 return EE_INCOMPATIBLE;
178 }
179
180 /* Check if the object type is supported. */
181 if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
182 DPRINTF("Object type %d is not supported\n", header->e_type);
183 return EE_UNSUPPORTED;
184 }
185
[742fc98e]186 if (header->e_phoff == 0) {
187 DPRINTF("Program header table is not present!\n");
188 return EE_UNSUPPORTED;
189 }
190
[7c3fb9b]191 /*
192 * Read program header table.
[742fc98e]193 * Normally, there are very few program headers, so don't bother
194 * with allocating memory dynamically.
195 */
196 const int phdr_cap = 16;
197 elf_segment_header_t phdr[phdr_cap];
198 size_t phdr_len = header->e_phnum * header->e_phentsize;
199
[c4049e6]200 elf->info->interp = NULL;
201 elf->info->dynamic = NULL;
202
[742fc98e]203 if (phdr_len > sizeof(phdr)) {
204 DPRINTF("more than %d program headers\n", phdr_cap);
205 return EE_UNSUPPORTED;
206 }
207
208 pos = header->e_phoff;
209 rc = vfs_read(elf->fd, &pos, phdr, phdr_len, &nr);
210 if (rc != EOK || nr != phdr_len) {
211 DPRINTF("Read error.\n");
212 return EE_IO;
213 }
214
215 uintptr_t module_base = UINTPTR_MAX;
216 uintptr_t module_top = 0;
217 uintptr_t base_offset = UINTPTR_MAX;
218
219 /* Walk through PT_LOAD headers, to find out the size of the module. */
220 for (i = 0; i < header->e_phnum; i++) {
221 if (phdr[i].p_type != PT_LOAD)
222 continue;
223
224 if (module_base > phdr[i].p_vaddr) {
225 module_base = phdr[i].p_vaddr;
226 base_offset = phdr[i].p_offset;
227 }
228 module_top = max(module_top, phdr[i].p_vaddr + phdr[i].p_memsz);
229 }
230
231 if (base_offset != 0) {
232 DPRINTF("ELF headers not present in the text segment.\n");
233 return EE_INVALID;
234 }
235
[17341d4]236 /* Shared objects can be loaded with a bias */
[742fc98e]237 if (header->e_type != ET_DYN) {
[17341d4]238 elf->bias = 0;
[742fc98e]239 } else {
240 if (module_base != 0) {
241 DPRINTF("Unexpected shared object format.\n");
242 return EE_INVALID;
243 }
244
[7c3fb9b]245 /*
246 * Attempt to allocate a span of memory large enough for the
[742fc98e]247 * shared object.
248 */
249 // FIXME: This is not reliable when we're running
250 // multi-threaded. Even if this part succeeds, later
251 // allocation can fail because another thread took the
252 // space in the meantime. This is only relevant for
253 // dlopen() though.
254 void *area = as_area_create(AS_AREA_ANY, module_top,
255 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE |
256 AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
257
258 if (area == AS_MAP_FAILED) {
259 DPRINTF("Can't find suitable memory area.\n");
260 return EE_MEMORY;
261 }
262
263 elf->bias = (uintptr_t) area;
264 as_area_destroy(area);
265 }
266
267 /* Load all loadable segments. */
268 for (i = 0; i < header->e_phnum; i++) {
269 if (phdr[i].p_type != PT_LOAD)
270 continue;
271
272 ret = load_segment(elf, &phdr[i]);
273 if (ret != EE_OK)
274 return ret;
275 }
276
277 void *base = (void *) module_base + elf->bias;
278 elf->info->base = base;
[17341d4]279
[4c5f04f]280 /* Ensure valid TLS info even if there is no TLS header. */
281 elf->info->tls.tdata = NULL;
282 elf->info->tls.tdata_size = 0;
283 elf->info->tls.tbss_size = 0;
284 elf->info->tls.tls_align = 1;
285
[17341d4]286 elf->info->interp = NULL;
287 elf->info->dynamic = NULL;
288
289 /* Walk through all segment headers and process them. */
290 for (i = 0; i < header->e_phnum; i++) {
[742fc98e]291 if (phdr[i].p_type == PT_LOAD)
292 continue;
[17341d4]293
[742fc98e]294 ret = segment_header(elf, &phdr[i]);
[1afa94d]295 if (ret != EE_OK)
296 return ret;
[17341d4]297 }
298
299 elf->info->entry =
300 (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
301
302 DPRINTF("Done.\n");
303
304 return EE_OK;
305}
306
307/** Print error message according to error code.
308 *
309 * @param rc Return code returned by elf_load().
310 *
311 * @return NULL terminated description of error.
312 */
313const char *elf_error(unsigned int rc)
314{
315 assert(rc < sizeof(error_codes) / sizeof(char *));
316
317 return error_codes[rc];
318}
319
[6adb775f]320/** Process TLS program header.
321 *
322 * @param elf Pointer to loader state buffer.
323 * @param hdr TLS program header
324 * @param info Place to store TLS info
325 */
326static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
327 elf_tls_info_t *info)
328{
329 info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
330 info->tdata_size = hdr->p_filesz;
331 info->tbss_size = hdr->p_memsz - hdr->p_filesz;
[29405ac]332 info->tls_align = hdr->p_align;
[6adb775f]333}
334
[17341d4]335/** Process segment header.
336 *
[6adb775f]337 * @param elf Pointer to loader state buffer.
[17341d4]338 * @param entry Segment header.
339 *
340 * @return EE_OK on success, error code otherwise.
341 */
342static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
343{
344 switch (entry->p_type) {
345 case PT_NULL:
346 case PT_PHDR:
347 case PT_NOTE:
348 break;
[888a2c6]349 case PT_GNU_EH_FRAME:
350 case PT_GNU_STACK:
351 case PT_GNU_RELRO:
352 /* Ignore GNU headers, if present. */
353 break;
[17341d4]354 case PT_INTERP:
[57d44dd]355 elf->info->interp =
356 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
357
358 if (elf->info->interp[entry->p_filesz - 1] != '\0') {
359 DPRINTF("Unterminated ELF interp string.\n");
360 return EE_INVALID;
361 }
362 DPRINTF("interpreter: \"%s\"\n", elf->info->interp);
[17341d4]363 break;
364 case PT_DYNAMIC:
365 /* Record pointer to dynamic section into info structure */
366 elf->info->dynamic =
367 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
368 DPRINTF("dynamic section found at %p\n",
[3bacee1]369 (void *)elf->info->dynamic);
[17341d4]370 break;
371 case 0x70000000:
[e634684]372 case 0x70000001:
373 case 0x70000002:
374 case 0x70000003:
375 // FIXME: Architecture-specific headers.
376 /* PT_MIPS_REGINFO, PT_MIPS_ABIFLAGS, PT_ARM_UNWIND, ... */
[17341d4]377 break;
[6adb775f]378 case PT_TLS:
379 /* Parse TLS program header */
380 tls_program_header(elf, entry, &elf->info->tls);
381 DPRINTF("TLS header found at %p\n",
382 (void *)((uint8_t *)entry->p_vaddr + elf->bias));
383 break;
[17341d4]384 case PT_SHLIB:
385 default:
386 DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
387 return EE_UNSUPPORTED;
388 break;
389 }
390 return EE_OK;
391}
392
393/** Load segment described by program header entry.
394 *
395 * @param elf Loader state.
396 * @param entry Program header entry describing segment to be loaded.
397 *
398 * @return EE_OK on success, error code otherwise.
399 */
400int load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
401{
402 void *a;
403 int flags = 0;
404 uintptr_t bias;
405 uintptr_t base;
406 void *seg_ptr;
407 uintptr_t seg_addr;
408 size_t mem_sz;
[58898d1d]409 aoff64_t pos;
[b7fd2a0]410 errno_t rc;
[8e3498b]411 size_t nr;
[17341d4]412
413 bias = elf->bias;
414
415 seg_addr = entry->p_vaddr + bias;
416 seg_ptr = (void *) seg_addr;
417
418 DPRINTF("Load segment at addr %p, size 0x%zx\n", (void *) seg_addr,
[3bacee1]419 entry->p_memsz);
[17341d4]420
421 if (entry->p_align > 1) {
422 if ((entry->p_offset % entry->p_align) !=
423 (seg_addr % entry->p_align)) {
424 DPRINTF("Align check 1 failed offset%%align=0x%zx, "
425 "vaddr%%align=0x%zx\n",
426 entry->p_offset % entry->p_align,
427 seg_addr % entry->p_align);
428 return EE_INVALID;
429 }
430 }
431
432 /* Final flags that will be set for the memory area */
433
434 if (entry->p_flags & PF_X)
435 flags |= AS_AREA_EXEC;
436 if (entry->p_flags & PF_W)
437 flags |= AS_AREA_WRITE;
438 if (entry->p_flags & PF_R)
439 flags |= AS_AREA_READ;
440 flags |= AS_AREA_CACHEABLE;
[a35b458]441
[17341d4]442 base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
443 mem_sz = entry->p_memsz + (entry->p_vaddr - base);
444
445 DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
446 (void *) (entry->p_vaddr + bias +
447 ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
448
449 /*
450 * For the course of loading, the area needs to be readable
451 * and writeable.
452 */
453 a = as_area_create((uint8_t *) base + bias, mem_sz,
[6aeca0d]454 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
455 AS_AREA_UNPAGED);
[17341d4]456 if (a == AS_MAP_FAILED) {
457 DPRINTF("memory mapping failed (%p, %zu)\n",
458 (void *) (base + bias), mem_sz);
459 return EE_MEMORY;
460 }
461
462 DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
463 (void *) (base + bias), mem_sz, flags, (void *) a);
464
465 /*
466 * Load segment data
467 */
[58898d1d]468 pos = entry->p_offset;
[8e3498b]469 rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
470 if (rc != EOK || nr != entry->p_filesz) {
[48178b56]471 DPRINTF("read error\n");
[1afa94d]472 return EE_IO;
[17341d4]473 }
474
475 /*
476 * The caller wants to modify the segments first. He will then
477 * need to set the right access mode and ensure SMC coherence.
478 */
[3bacee1]479 if ((elf->flags & ELDF_RW) != 0)
480 return EE_OK;
[17341d4]481
482 rc = as_area_change_flags(seg_ptr, flags);
[a53ed3a]483 if (rc != EOK) {
[17341d4]484 DPRINTF("Failed to set memory area flags.\n");
485 return EE_MEMORY;
486 }
487
488 if (flags & AS_AREA_EXEC) {
489 /* Enforce SMC coherence for the segment */
490 if (smc_coherence(seg_ptr, entry->p_filesz))
491 return EE_MEMORY;
492 }
493
494 return EE_OK;
495}
496
497/** @}
498 */
Note: See TracBrowser for help on using the repository browser.