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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8e3498b was 8e3498b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

vfs_read/write() should return error code separately from number of bytes transferred.

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