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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 58898d1d was 58898d1d, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove VFS_IN_SEEK from VFS

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