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

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

DTPMOD relocations with null symbol name should return the current module index. Implement TPOFF relocations. dltest -n to not run dlfcn tests. Now dltest -n works like a charm. Place TLS images in descending order for variant II just to be safe. Propagate TLS alignment info.

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