source: mainline/uspace/lib/c/generic/elf/elf_load.c@ 24cf31f1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 24cf31f1 was 24cf31f1, checked in by Martin Decky <martin@…>, 14 years ago

cstyle

  • Property mode set to 100644
File size: 11.8 KB
RevLine 
[c98e6ee]1/*
2 * Copyright (c) 2006 Sergey Bondari
3 * Copyright (c) 2006 Jakub Jermar
[bae7bdc]4 * Copyright (c) 2011 Jiri Svoboda
[c98e6ee]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
[bfdb5af1]31/** @addtogroup generic
[c98e6ee]32 * @{
33 */
34
35/**
36 * @file
37 * @brief Userspace ELF 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
46#include <stdio.h>
47#include <sys/types.h>
48#include <align.h>
49#include <assert.h>
50#include <as.h>
[90b8d58]51#include <elf/elf.h>
[c98e6ee]52#include <unistd.h>
53#include <fcntl.h>
54#include <smc.h>
55#include <loader/pcb.h>
[bae7bdc]56#include <entry_point.h>
[c98e6ee]57
[bfdb5af1]58#include <elf/elf_load.h>
[c98e6ee]59
[06b2b7f]60#define DPRINTF(...)
61
[a000878c]62static const char *error_codes[] = {
[c98e6ee]63 "no error",
64 "invalid image",
65 "address space error",
66 "incompatible image",
67 "unsupported image type",
68 "irrecoverable error"
69};
70
71static unsigned int elf_load(elf_ld_t *elf, size_t so_bias);
72static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
73static int section_header(elf_ld_t *elf, elf_section_header_t *entry);
74static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
75
76/** Load ELF binary from a file.
77 *
78 * Load an ELF binary from the specified file. If the file is
79 * an executable program, it is loaded unbiased. If it is a shared
80 * object, it is loaded with the bias @a so_bias. Some information
81 * extracted from the binary is stored in a elf_info_t structure
82 * pointed to by @a info.
83 *
[a000878c]84 * @param file_name Path to the ELF file.
85 * @param so_bias Bias to use if the file is a shared object.
86 * @param info Pointer to a structure for storing information
87 * extracted from the binary.
[c98e6ee]88 *
89 * @return EOK on success or negative error code.
[a000878c]90 *
[c98e6ee]91 */
[04803bf]92int elf_load_file(const char *file_name, size_t so_bias, eld_flags_t flags,
[1ea99cc]93 elf_info_t *info)
[c98e6ee]94{
95 elf_ld_t elf;
96
97 int fd;
98 int rc;
[d88218b]99
[c98e6ee]100 fd = open(file_name, O_RDONLY);
101 if (fd < 0) {
[06b2b7f]102 DPRINTF("failed opening file\n");
[c98e6ee]103 return -1;
104 }
105
106 elf.fd = fd;
107 elf.info = info;
[1ea99cc]108 elf.flags = flags;
[c98e6ee]109
110 rc = elf_load(&elf, so_bias);
111
112 close(fd);
113
114 return rc;
115}
116
117/** Create the program control block (PCB).
118 *
119 * Fills the program control block @a pcb with information from
120 * @a info.
121 *
122 * @param info Program info structure
123 * @return EOK on success or negative error code
124 */
125void elf_create_pcb(elf_info_t *info, pcb_t *pcb)
126{
127 pcb->entry = info->entry;
128 pcb->dynamic = info->dynamic;
[1ea99cc]129 pcb->rtld_runtime = NULL;
[c98e6ee]130}
131
132
133/** Load an ELF binary.
134 *
135 * The @a elf structure contains the loader state, including
136 * an open file, from which the binary will be loaded,
137 * a pointer to the @c info structure etc.
138 *
139 * @param elf Pointer to loader state buffer.
140 * @param so_bias Bias to use if the file is a shared object.
141 * @return EE_OK on success or EE_xx error code.
142 */
143static unsigned int elf_load(elf_ld_t *elf, size_t so_bias)
144{
145 elf_header_t header_buf;
146 elf_header_t *header = &header_buf;
147 int i, rc;
148
[c566782]149 rc = read_all(elf->fd, header, sizeof(elf_header_t));
[aa865ee]150 if (rc != sizeof(elf_header_t)) {
[06b2b7f]151 DPRINTF("Read error.\n");
[c98e6ee]152 return EE_INVALID;
153 }
154
155 elf->header = header;
156
157 /* Identify ELF */
158 if (header->e_ident[EI_MAG0] != ELFMAG0 ||
159 header->e_ident[EI_MAG1] != ELFMAG1 ||
160 header->e_ident[EI_MAG2] != ELFMAG2 ||
161 header->e_ident[EI_MAG3] != ELFMAG3) {
[06b2b7f]162 DPRINTF("Invalid header.\n");
[c98e6ee]163 return EE_INVALID;
164 }
165
166 /* Identify ELF compatibility */
167 if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
168 header->e_machine != ELF_MACHINE ||
169 header->e_ident[EI_VERSION] != EV_CURRENT ||
170 header->e_version != EV_CURRENT ||
171 header->e_ident[EI_CLASS] != ELF_CLASS) {
[06b2b7f]172 DPRINTF("Incompatible data/version/class.\n");
[c98e6ee]173 return EE_INCOMPATIBLE;
174 }
175
176 if (header->e_phentsize != sizeof(elf_segment_header_t)) {
[06b2b7f]177 DPRINTF("e_phentsize:%d != %d\n", header->e_phentsize,
[c98e6ee]178 sizeof(elf_segment_header_t));
179 return EE_INCOMPATIBLE;
180 }
181
182 if (header->e_shentsize != sizeof(elf_section_header_t)) {
[06b2b7f]183 DPRINTF("e_shentsize:%d != %d\n", header->e_shentsize,
[c98e6ee]184 sizeof(elf_section_header_t));
185 return EE_INCOMPATIBLE;
186 }
187
188 /* Check if the object type is supported. */
189 if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
[06b2b7f]190 DPRINTF("Object type %d is not supported\n", header->e_type);
[c98e6ee]191 return EE_UNSUPPORTED;
192 }
193
194 /* Shared objects can be loaded with a bias */
195 if (header->e_type == ET_DYN)
196 elf->bias = so_bias;
197 else
198 elf->bias = 0;
199
200 elf->info->interp = NULL;
201 elf->info->dynamic = NULL;
202
203 /* Walk through all segment headers and process them. */
204 for (i = 0; i < header->e_phnum; i++) {
205 elf_segment_header_t segment_hdr;
206
207 /* Seek to start of segment header */
208 lseek(elf->fd, header->e_phoff
209 + i * sizeof(elf_segment_header_t), SEEK_SET);
210
[c566782]211 rc = read_all(elf->fd, &segment_hdr,
[874a0e8]212 sizeof(elf_segment_header_t));
[aa865ee]213 if (rc != sizeof(elf_segment_header_t)) {
[06b2b7f]214 DPRINTF("Read error.\n");
[f93f168]215 return EE_INVALID;
216 }
[c98e6ee]217
218 rc = segment_header(elf, &segment_hdr);
219 if (rc != EE_OK)
220 return rc;
221 }
222
[06b2b7f]223 DPRINTF("Parse sections.\n");
[c98e6ee]224
225 /* Inspect all section headers and proccess them. */
226 for (i = 0; i < header->e_shnum; i++) {
227 elf_section_header_t section_hdr;
228
229 /* Seek to start of section header */
230 lseek(elf->fd, header->e_shoff
231 + i * sizeof(elf_section_header_t), SEEK_SET);
232
[c566782]233 rc = read_all(elf->fd, &section_hdr,
[874a0e8]234 sizeof(elf_section_header_t));
[aa865ee]235 if (rc != sizeof(elf_section_header_t)) {
[06b2b7f]236 DPRINTF("Read error.\n");
[f93f168]237 return EE_INVALID;
238 }
[c98e6ee]239
240 rc = section_header(elf, &section_hdr);
241 if (rc != EE_OK)
242 return rc;
243 }
244
245 elf->info->entry =
246 (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
247
[06b2b7f]248 DPRINTF("Done.\n");
[c98e6ee]249
250 return EE_OK;
251}
252
253/** Print error message according to error code.
254 *
255 * @param rc Return code returned by elf_load().
256 *
257 * @return NULL terminated description of error.
258 */
[a000878c]259const char *elf_error(unsigned int rc)
[c98e6ee]260{
261 assert(rc < sizeof(error_codes) / sizeof(char *));
262
263 return error_codes[rc];
264}
265
266/** Process segment header.
267 *
268 * @param entry Segment header.
269 *
270 * @return EE_OK on success, error code otherwise.
271 */
272static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
273{
274 switch (entry->p_type) {
275 case PT_NULL:
276 case PT_PHDR:
[1e00216]277 case PT_NOTE:
[c98e6ee]278 break;
279 case PT_LOAD:
280 return load_segment(elf, entry);
281 break;
282 case PT_INTERP:
[1ea99cc]283 /* Assume silently interp == "/app/dload" */
284 elf->info->interp = "/app/dload";
[c98e6ee]285 break;
286 case PT_DYNAMIC:
[1ea99cc]287 /* Record pointer to dynamic section into info structure */
288 elf->info->dynamic =
289 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
290 DPRINTF("dynamic section found at 0x%x\n",
291 (uintptr_t)elf->info->dynamic);
292 break;
293 case 0x70000000:
294 /* FIXME: MIPS reginfo */
295 break;
[c98e6ee]296 case PT_SHLIB:
[1ea99cc]297// case PT_LOPROC:
298// case PT_HIPROC:
[c98e6ee]299 default:
[06b2b7f]300 DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
[c98e6ee]301 return EE_UNSUPPORTED;
302 break;
303 }
304 return EE_OK;
305}
306
307/** Load segment described by program header entry.
308 *
309 * @param elf Loader state.
310 * @param entry Program header entry describing segment to be loaded.
311 *
312 * @return EE_OK on success, error code otherwise.
313 */
314int load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
315{
316 void *a;
317 int flags = 0;
318 uintptr_t bias;
319 uintptr_t base;
[08c9f7d5]320 void *seg_ptr;
321 uintptr_t seg_addr;
[c98e6ee]322 size_t mem_sz;
[aa865ee]323 ssize_t rc;
[c98e6ee]324
325 bias = elf->bias;
326
[08c9f7d5]327 seg_addr = entry->p_vaddr + bias;
328 seg_ptr = (void *) seg_addr;
329
[d88218b]330 DPRINTF("Load segment at addr %p, size 0x%x\n", (void *) seg_addr,
[fb6f1a5]331 entry->p_memsz);
[08c9f7d5]332
[c98e6ee]333 if (entry->p_align > 1) {
334 if ((entry->p_offset % entry->p_align) !=
[08c9f7d5]335 (seg_addr % entry->p_align)) {
[06b2b7f]336 DPRINTF("Align check 1 failed offset%%align=%d, "
[f93f168]337 "vaddr%%align=%d\n",
338 entry->p_offset % entry->p_align,
[08c9f7d5]339 seg_addr % entry->p_align
[c98e6ee]340 );
341 return EE_INVALID;
342 }
343 }
344
345 /* Final flags that will be set for the memory area */
346
347 if (entry->p_flags & PF_X)
348 flags |= AS_AREA_EXEC;
349 if (entry->p_flags & PF_W)
350 flags |= AS_AREA_WRITE;
351 if (entry->p_flags & PF_R)
352 flags |= AS_AREA_READ;
353 flags |= AS_AREA_CACHEABLE;
354
355 base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
356 mem_sz = entry->p_memsz + (entry->p_vaddr - base);
357
[d88218b]358 DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
359 (void *) (entry->p_vaddr + bias +
360 ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
[c98e6ee]361
362 /*
363 * For the course of loading, the area needs to be readable
364 * and writeable.
365 */
[8873c01d]366 a = as_area_create((uint8_t *) base + bias, mem_sz,
[874a0e8]367 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
[24cf31f1]368 if (a == (void *) -1) {
[1ea99cc]369 DPRINTF("memory mapping failed (0x%x, %d)\n",
[24cf31f1]370 base + bias, mem_sz);
[c98e6ee]371 return EE_MEMORY;
372 }
373
[d88218b]374 DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
375 (void *) (base + bias), mem_sz, flags, (void *) a);
[c98e6ee]376
377 /*
378 * Load segment data
379 */
380 rc = lseek(elf->fd, entry->p_offset, SEEK_SET);
[f93f168]381 if (rc < 0) {
382 printf("seek error\n");
383 return EE_INVALID;
384 }
[c98e6ee]385
386/* rc = read(fd, (void *)(entry->p_vaddr + bias), entry->p_filesz);
387 if (rc < 0) { printf("read error\n"); return EE_INVALID; }*/
388
[06b2b7f]389 /* Long reads are not possible yet. Load segment piecewise. */
[c98e6ee]390
391 unsigned left, now;
392 uint8_t *dp;
393
394 left = entry->p_filesz;
[08c9f7d5]395 dp = seg_ptr;
[c98e6ee]396
397 while (left > 0) {
398 now = 16384;
399 if (now > left) now = left;
400
[c566782]401 rc = read_all(elf->fd, dp, now);
[c98e6ee]402
[aa865ee]403 if (rc != (ssize_t) now) {
[06b2b7f]404 DPRINTF("Read error.\n");
[f93f168]405 return EE_INVALID;
406 }
[c98e6ee]407
408 left -= now;
409 dp += now;
410 }
411
[1ea99cc]412 /*
413 * The caller wants to modify the segments first. He will then
414 * need to set the right access mode and ensure SMC coherence.
415 */
416 if ((elf->flags & ELDF_RW) != 0) return EE_OK;
417
418// printf("set area flags to %d\n", flags);
[08c9f7d5]419 rc = as_area_change_flags(seg_ptr, flags);
[c98e6ee]420 if (rc != 0) {
[06b2b7f]421 DPRINTF("Failed to set memory area flags.\n");
[c98e6ee]422 return EE_MEMORY;
423 }
424
425 if (flags & AS_AREA_EXEC) {
426 /* Enforce SMC coherence for the segment */
[08c9f7d5]427 if (smc_coherence(seg_ptr, entry->p_filesz))
[c98e6ee]428 return EE_MEMORY;
429 }
430
431 return EE_OK;
432}
433
434/** Process section header.
435 *
436 * @param elf Loader state.
437 * @param entry Segment header.
438 *
439 * @return EE_OK on success, error code otherwise.
440 */
441static int section_header(elf_ld_t *elf, elf_section_header_t *entry)
442{
443 switch (entry->sh_type) {
444 case SHT_PROGBITS:
445 if (entry->sh_flags & SHF_TLS) {
446 /* .tdata */
447 }
448 break;
449 case SHT_NOBITS:
450 if (entry->sh_flags & SHF_TLS) {
451 /* .tbss */
452 }
453 break;
454 case SHT_DYNAMIC:
455 /* Record pointer to dynamic section into info structure */
456 elf->info->dynamic =
457 (void *)((uint8_t *)entry->sh_addr + elf->bias);
[fb6f1a5]458 DPRINTF("Dynamic section found at %p.\n",
[d88218b]459 (void *) elf->info->dynamic);
[c98e6ee]460 break;
461 default:
462 break;
463 }
464
465 return EE_OK;
466}
467
468/** @}
469 */
Note: See TracBrowser for help on using the repository browser.