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

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

c+cpp: added support for global static constructors destructors

  • Property mode set to 100644
File size: 13.0 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 elf->info->cpp_data.ctors_count = 0;
203 elf->info->cpp_data.dtors_count = 0;
204
[742fc98e]205 if (phdr_len > sizeof(phdr)) {
206 DPRINTF("more than %d program headers\n", phdr_cap);
207 return EE_UNSUPPORTED;
208 }
209
210 pos = header->e_phoff;
211 rc = vfs_read(elf->fd, &pos, phdr, phdr_len, &nr);
212 if (rc != EOK || nr != phdr_len) {
213 DPRINTF("Read error.\n");
214 return EE_IO;
215 }
216
217 uintptr_t module_base = UINTPTR_MAX;
218 uintptr_t module_top = 0;
219 uintptr_t base_offset = UINTPTR_MAX;
220
221 /* Walk through PT_LOAD headers, to find out the size of the module. */
222 for (i = 0; i < header->e_phnum; i++) {
223 if (phdr[i].p_type != PT_LOAD)
224 continue;
225
226 if (module_base > phdr[i].p_vaddr) {
227 module_base = phdr[i].p_vaddr;
228 base_offset = phdr[i].p_offset;
229 }
230 module_top = max(module_top, phdr[i].p_vaddr + phdr[i].p_memsz);
231 }
232
233 if (base_offset != 0) {
234 DPRINTF("ELF headers not present in the text segment.\n");
235 return EE_INVALID;
236 }
237
[17341d4]238 /* Shared objects can be loaded with a bias */
[742fc98e]239 if (header->e_type != ET_DYN) {
[17341d4]240 elf->bias = 0;
[742fc98e]241 } else {
242 if (module_base != 0) {
243 DPRINTF("Unexpected shared object format.\n");
244 return EE_INVALID;
245 }
246
[7c3fb9b]247 /*
248 * Attempt to allocate a span of memory large enough for the
[742fc98e]249 * shared object.
250 */
251 // FIXME: This is not reliable when we're running
252 // multi-threaded. Even if this part succeeds, later
253 // allocation can fail because another thread took the
254 // space in the meantime. This is only relevant for
255 // dlopen() though.
256 void *area = as_area_create(AS_AREA_ANY, module_top,
257 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE |
258 AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
259
260 if (area == AS_MAP_FAILED) {
261 DPRINTF("Can't find suitable memory area.\n");
262 return EE_MEMORY;
263 }
264
265 elf->bias = (uintptr_t) area;
266 as_area_destroy(area);
267 }
268
269 /* Load all loadable segments. */
270 for (i = 0; i < header->e_phnum; i++) {
271 if (phdr[i].p_type != PT_LOAD)
272 continue;
273
274 ret = load_segment(elf, &phdr[i]);
275 if (ret != EE_OK)
276 return ret;
277 }
278
279 void *base = (void *) module_base + elf->bias;
280 elf->info->base = base;
[17341d4]281
[4c5f04f]282 /* Ensure valid TLS info even if there is no TLS header. */
283 elf->info->tls.tdata = NULL;
284 elf->info->tls.tdata_size = 0;
285 elf->info->tls.tbss_size = 0;
286 elf->info->tls.tls_align = 1;
287
[17341d4]288 elf->info->interp = NULL;
289 elf->info->dynamic = NULL;
290
291 /* Walk through all segment headers and process them. */
292 for (i = 0; i < header->e_phnum; i++) {
[742fc98e]293 if (phdr[i].p_type == PT_LOAD)
294 continue;
[17341d4]295
[742fc98e]296 ret = segment_header(elf, &phdr[i]);
[1afa94d]297 if (ret != EE_OK)
298 return ret;
[17341d4]299 }
300
301 elf->info->entry =
302 (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
303
304 DPRINTF("Done.\n");
305
306 return EE_OK;
307}
308
309/** Print error message according to error code.
310 *
311 * @param rc Return code returned by elf_load().
312 *
313 * @return NULL terminated description of error.
314 */
315const char *elf_error(unsigned int rc)
316{
317 assert(rc < sizeof(error_codes) / sizeof(char *));
318
319 return error_codes[rc];
320}
321
[6adb775f]322/** Process TLS program header.
323 *
324 * @param elf Pointer to loader state buffer.
325 * @param hdr TLS program header
326 * @param info Place to store TLS info
327 */
328static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
329 elf_tls_info_t *info)
330{
331 info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
332 info->tdata_size = hdr->p_filesz;
333 info->tbss_size = hdr->p_memsz - hdr->p_filesz;
[29405ac]334 info->tls_align = hdr->p_align;
[6adb775f]335}
336
[17341d4]337/** Process segment header.
338 *
[6adb775f]339 * @param elf Pointer to loader state buffer.
[17341d4]340 * @param entry Segment header.
341 *
342 * @return EE_OK on success, error code otherwise.
343 */
344static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
345{
346 switch (entry->p_type) {
347 case PT_NULL:
348 case PT_PHDR:
349 case PT_NOTE:
350 break;
[888a2c6]351 case PT_GNU_EH_FRAME:
352 case PT_GNU_STACK:
353 case PT_GNU_RELRO:
354 /* Ignore GNU headers, if present. */
355 break;
[17341d4]356 case PT_INTERP:
[57d44dd]357 elf->info->interp =
358 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
359
360 if (elf->info->interp[entry->p_filesz - 1] != '\0') {
361 DPRINTF("Unterminated ELF interp string.\n");
362 return EE_INVALID;
363 }
364 DPRINTF("interpreter: \"%s\"\n", elf->info->interp);
[17341d4]365 break;
366 case PT_DYNAMIC:
367 /* Record pointer to dynamic section into info structure */
368 elf->info->dynamic =
369 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
370 DPRINTF("dynamic section found at %p\n",
[3bacee1]371 (void *)elf->info->dynamic);
[17341d4]372 break;
373 case 0x70000000:
[e634684]374 case 0x70000001:
375 case 0x70000002:
376 case 0x70000003:
377 // FIXME: Architecture-specific headers.
378 /* PT_MIPS_REGINFO, PT_MIPS_ABIFLAGS, PT_ARM_UNWIND, ... */
[17341d4]379 break;
[6adb775f]380 case PT_TLS:
381 /* Parse TLS program header */
382 tls_program_header(elf, entry, &elf->info->tls);
383 DPRINTF("TLS header found at %p\n",
384 (void *)((uint8_t *)entry->p_vaddr + elf->bias));
385 break;
[17341d4]386 case PT_SHLIB:
387 default:
388 DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
389 return EE_UNSUPPORTED;
390 break;
391 }
392 return EE_OK;
393}
394
395/** Load segment described by program header entry.
396 *
397 * @param elf Loader state.
398 * @param entry Program header entry describing segment to be loaded.
399 *
400 * @return EE_OK on success, error code otherwise.
401 */
402int load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
403{
404 void *a;
405 int flags = 0;
406 uintptr_t bias;
407 uintptr_t base;
408 void *seg_ptr;
409 uintptr_t seg_addr;
410 size_t mem_sz;
[58898d1d]411 aoff64_t pos;
[b7fd2a0]412 errno_t rc;
[8e3498b]413 size_t nr;
[17341d4]414
415 bias = elf->bias;
416
417 seg_addr = entry->p_vaddr + bias;
418 seg_ptr = (void *) seg_addr;
419
420 DPRINTF("Load segment at addr %p, size 0x%zx\n", (void *) seg_addr,
[3bacee1]421 entry->p_memsz);
[17341d4]422
423 if (entry->p_align > 1) {
424 if ((entry->p_offset % entry->p_align) !=
425 (seg_addr % entry->p_align)) {
426 DPRINTF("Align check 1 failed offset%%align=0x%zx, "
427 "vaddr%%align=0x%zx\n",
428 entry->p_offset % entry->p_align,
429 seg_addr % entry->p_align);
430 return EE_INVALID;
431 }
432 }
433
434 /* Final flags that will be set for the memory area */
435
436 if (entry->p_flags & PF_X)
437 flags |= AS_AREA_EXEC;
438 if (entry->p_flags & PF_W)
439 flags |= AS_AREA_WRITE;
440 if (entry->p_flags & PF_R)
441 flags |= AS_AREA_READ;
442 flags |= AS_AREA_CACHEABLE;
[a35b458]443
[17341d4]444 base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
445 mem_sz = entry->p_memsz + (entry->p_vaddr - base);
446
447 DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
448 (void *) (entry->p_vaddr + bias +
449 ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
450
451 /*
452 * For the course of loading, the area needs to be readable
453 * and writeable.
454 */
455 a = as_area_create((uint8_t *) base + bias, mem_sz,
[6aeca0d]456 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
457 AS_AREA_UNPAGED);
[17341d4]458 if (a == AS_MAP_FAILED) {
459 DPRINTF("memory mapping failed (%p, %zu)\n",
460 (void *) (base + bias), mem_sz);
461 return EE_MEMORY;
462 }
463
464 DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
465 (void *) (base + bias), mem_sz, flags, (void *) a);
466
467 /*
468 * Load segment data
469 */
[58898d1d]470 pos = entry->p_offset;
[8e3498b]471 rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
472 if (rc != EOK || nr != entry->p_filesz) {
[48178b56]473 DPRINTF("read error\n");
[1afa94d]474 return EE_IO;
[17341d4]475 }
476
477 /*
478 * The caller wants to modify the segments first. He will then
479 * need to set the right access mode and ensure SMC coherence.
480 */
[3bacee1]481 if ((elf->flags & ELDF_RW) != 0)
482 return EE_OK;
[17341d4]483
484 rc = as_area_change_flags(seg_ptr, flags);
[a53ed3a]485 if (rc != EOK) {
[17341d4]486 DPRINTF("Failed to set memory area flags.\n");
487 return EE_MEMORY;
488 }
489
490 if (flags & AS_AREA_EXEC) {
491 /* Enforce SMC coherence for the segment */
492 if (smc_coherence(seg_ptr, entry->p_filesz))
493 return EE_MEMORY;
494 }
495
496 return EE_OK;
497}
498
499/** @}
500 */
Note: See TracBrowser for help on using the repository browser.