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

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

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revision 1946

Original commit messages:

1946: Jiri Zarevucky 2013-08-06 Relativize mount, add root handle to libc and remove root from VFS server. This wraps up the "relativization" phase.

Breakage:

  • Dynamic builds broken
  • Mount table lookups by name
  • 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 <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 int i, rc;
138
139 rc = read(elf->fd, header, sizeof(elf_header_t));
140 if (rc != sizeof(elf_header_t)) {
141 DPRINTF("Read error.\n");
142 return EE_INVALID;
143 }
144
145 elf->header = header;
146
147 /* Identify ELF */
148 if (header->e_ident[EI_MAG0] != ELFMAG0 ||
149 header->e_ident[EI_MAG1] != ELFMAG1 ||
150 header->e_ident[EI_MAG2] != ELFMAG2 ||
151 header->e_ident[EI_MAG3] != ELFMAG3) {
152 DPRINTF("Invalid header.\n");
153 return EE_INVALID;
154 }
155
156 /* Identify ELF compatibility */
157 if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
158 header->e_machine != ELF_MACHINE ||
159 header->e_ident[EI_VERSION] != EV_CURRENT ||
160 header->e_version != EV_CURRENT ||
161 header->e_ident[EI_CLASS] != ELF_CLASS) {
162 DPRINTF("Incompatible data/version/class.\n");
163 return EE_INCOMPATIBLE;
164 }
165
166 if (header->e_phentsize != sizeof(elf_segment_header_t)) {
167 DPRINTF("e_phentsize: %u != %zu\n", header->e_phentsize,
168 sizeof(elf_segment_header_t));
169 return EE_INCOMPATIBLE;
170 }
171
172 if (header->e_shentsize != sizeof(elf_section_header_t)) {
173 DPRINTF("e_shentsize: %u != %zu\n", header->e_shentsize,
174 sizeof(elf_section_header_t));
175 return EE_INCOMPATIBLE;
176 }
177
178 /* Check if the object type is supported. */
179 if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
180 DPRINTF("Object type %d is not supported\n", header->e_type);
181 return EE_UNSUPPORTED;
182 }
183
184 /* Shared objects can be loaded with a bias */
185 if (header->e_type == ET_DYN)
186 elf->bias = so_bias;
187 else
188 elf->bias = 0;
189
190 elf->info->interp = NULL;
191 elf->info->dynamic = NULL;
192
193 /* Walk through all segment headers and process them. */
194 for (i = 0; i < header->e_phnum; i++) {
195 elf_segment_header_t segment_hdr;
196
197 /* Seek to start of segment header */
198 lseek(elf->fd, header->e_phoff
199 + i * sizeof(elf_segment_header_t), SEEK_SET);
200
201 rc = read(elf->fd, &segment_hdr,
202 sizeof(elf_segment_header_t));
203 if (rc != 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 /* Seek to start of section header */
220 lseek(elf->fd, header->e_shoff
221 + i * sizeof(elf_section_header_t), SEEK_SET);
222
223 rc = read(elf->fd, &section_hdr,
224 sizeof(elf_section_header_t));
225 if (rc != sizeof(elf_section_header_t)) {
226 DPRINTF("Read error.\n");
227 return EE_INVALID;
228 }
229
230 rc = section_header(elf, &section_hdr);
231 if (rc != EE_OK)
232 return rc;
233 }
234
235 elf->info->entry =
236 (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
237
238 DPRINTF("Done.\n");
239
240 return EE_OK;
241}
242
243/** Print error message according to error code.
244 *
245 * @param rc Return code returned by elf_load().
246 *
247 * @return NULL terminated description of error.
248 */
249const char *elf_error(unsigned int rc)
250{
251 assert(rc < sizeof(error_codes) / sizeof(char *));
252
253 return error_codes[rc];
254}
255
256/** Process TLS program header.
257 *
258 * @param elf Pointer to loader state buffer.
259 * @param hdr TLS program header
260 * @param info Place to store TLS info
261 */
262static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
263 elf_tls_info_t *info)
264{
265 info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
266 info->tdata_size = hdr->p_filesz;
267 info->tbss_size = hdr->p_memsz - hdr->p_filesz;
268 info->tls_align = hdr->p_align;
269}
270
271/** Process segment header.
272 *
273 * @param elf Pointer to loader state buffer.
274 * @param entry Segment header.
275 *
276 * @return EE_OK on success, error code otherwise.
277 */
278static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
279{
280 switch (entry->p_type) {
281 case PT_NULL:
282 case PT_PHDR:
283 case PT_NOTE:
284 break;
285 case PT_LOAD:
286 return load_segment(elf, entry);
287 break;
288 case PT_INTERP:
289 /* Assume silently interp == "/app/dload" */
290 elf->info->interp = "/app/dload";
291 break;
292 case PT_DYNAMIC:
293 /* Record pointer to dynamic section into info structure */
294 elf->info->dynamic =
295 (void *)((uint8_t *)entry->p_vaddr + elf->bias);
296 DPRINTF("dynamic section found at %p\n",
297 (void *)elf->info->dynamic);
298 break;
299 case 0x70000000:
300 /* FIXME: MIPS reginfo */
301 break;
302 case PT_TLS:
303 /* Parse TLS program header */
304 tls_program_header(elf, entry, &elf->info->tls);
305 DPRINTF("TLS header found at %p\n",
306 (void *)((uint8_t *)entry->p_vaddr + elf->bias));
307 break;
308 case PT_SHLIB:
309// case PT_LOPROC:
310// case PT_HIPROC:
311 default:
312 DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
313 return EE_UNSUPPORTED;
314 break;
315 }
316 return EE_OK;
317}
318
319/** Load segment described by program header entry.
320 *
321 * @param elf Loader state.
322 * @param entry Program header entry describing segment to be loaded.
323 *
324 * @return EE_OK on success, error code otherwise.
325 */
326int load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
327{
328 void *a;
329 int flags = 0;
330 uintptr_t bias;
331 uintptr_t base;
332 void *seg_ptr;
333 uintptr_t seg_addr;
334 size_t mem_sz;
335 ssize_t rc;
336
337 bias = elf->bias;
338
339 seg_addr = entry->p_vaddr + bias;
340 seg_ptr = (void *) seg_addr;
341
342 DPRINTF("Load segment at addr %p, size 0x%zx\n", (void *) seg_addr,
343 entry->p_memsz);
344
345 if (entry->p_align > 1) {
346 if ((entry->p_offset % entry->p_align) !=
347 (seg_addr % entry->p_align)) {
348 DPRINTF("Align check 1 failed offset%%align=0x%zx, "
349 "vaddr%%align=0x%zx\n",
350 entry->p_offset % entry->p_align,
351 seg_addr % entry->p_align);
352 return EE_INVALID;
353 }
354 }
355
356 /* Final flags that will be set for the memory area */
357
358 if (entry->p_flags & PF_X)
359 flags |= AS_AREA_EXEC;
360 if (entry->p_flags & PF_W)
361 flags |= AS_AREA_WRITE;
362 if (entry->p_flags & PF_R)
363 flags |= AS_AREA_READ;
364 flags |= AS_AREA_CACHEABLE;
365
366 base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
367 mem_sz = entry->p_memsz + (entry->p_vaddr - base);
368
369 DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
370 (void *) (entry->p_vaddr + bias +
371 ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
372
373 /*
374 * For the course of loading, the area needs to be readable
375 * and writeable.
376 */
377 a = as_area_create((uint8_t *) base + bias, mem_sz,
378 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
379 AS_AREA_UNPAGED);
380 if (a == AS_MAP_FAILED) {
381 DPRINTF("memory mapping failed (%p, %zu)\n",
382 (void *) (base + bias), mem_sz);
383 return EE_MEMORY;
384 }
385
386 DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
387 (void *) (base + bias), mem_sz, flags, (void *) a);
388
389 /*
390 * Load segment data
391 */
392 rc = lseek(elf->fd, entry->p_offset, SEEK_SET);
393 if (rc < 0) {
394 printf("seek error\n");
395 return EE_INVALID;
396 }
397
398 rc = read(elf->fd, seg_ptr, entry->p_filesz);
399 if (rc < 0) {
400 DPRINTF("read error\n");
401 return EE_INVALID;
402 }
403
404 /*
405 * The caller wants to modify the segments first. He will then
406 * need to set the right access mode and ensure SMC coherence.
407 */
408 if ((elf->flags & ELDF_RW) != 0) return EE_OK;
409
410// printf("set area flags to %d\n", flags);
411 rc = as_area_change_flags(seg_ptr, flags);
412 if (rc != 0) {
413 DPRINTF("Failed to set memory area flags.\n");
414 return EE_MEMORY;
415 }
416
417 if (flags & AS_AREA_EXEC) {
418 /* Enforce SMC coherence for the segment */
419 if (smc_coherence(seg_ptr, entry->p_filesz))
420 return EE_MEMORY;
421 }
422
423 return EE_OK;
424}
425
426/** Process section header.
427 *
428 * @param elf Loader state.
429 * @param entry Segment header.
430 *
431 * @return EE_OK on success, error code otherwise.
432 */
433static int section_header(elf_ld_t *elf, elf_section_header_t *entry)
434{
435 switch (entry->sh_type) {
436 case SHT_PROGBITS:
437 if (entry->sh_flags & SHF_TLS) {
438 /* .tdata */
439 }
440 break;
441 case SHT_NOBITS:
442 if (entry->sh_flags & SHF_TLS) {
443 /* .tbss */
444 }
445 break;
446 case SHT_DYNAMIC:
447 /* Record pointer to dynamic section into info structure */
448 elf->info->dynamic =
449 (void *)((uint8_t *)entry->sh_addr + elf->bias);
450 DPRINTF("Dynamic section found at %p.\n",
451 (void *) elf->info->dynamic);
452 break;
453 default:
454 break;
455 }
456
457 return EE_OK;
458}
459
460/** @}
461 */
Note: See TracBrowser for help on using the repository browser.