[17341d4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2006 Sergey Bondari
|
---|
| 3 | * Copyright (c) 2008 Jiri Svoboda
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup generic
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | * @brief ELF loader structures and public functions.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #ifndef ELF_MOD_H_
|
---|
| 38 | #define ELF_MOD_H_
|
---|
| 39 |
|
---|
| 40 | #include <elf/elf.h>
|
---|
[8d2dd7f2] | 41 | #include <stddef.h>
|
---|
| 42 | #include <stdint.h>
|
---|
[17341d4] | 43 | #include <loader/pcb.h>
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * ELF error return codes
|
---|
| 47 | */
|
---|
| 48 | #define EE_OK 0 /* No error */
|
---|
| 49 | #define EE_INVALID 1 /* Invalid ELF image */
|
---|
| 50 | #define EE_MEMORY 2 /* Cannot allocate address space */
|
---|
| 51 | #define EE_INCOMPATIBLE 3 /* ELF image is not compatible with current architecture */
|
---|
| 52 | #define EE_UNSUPPORTED 4 /* Non-supported ELF (e.g. dynamic ELFs) */
|
---|
| 53 | #define EE_LOADER 5 /* The image is actually a program loader. */
|
---|
| 54 | #define EE_IRRECOVERABLE 6
|
---|
[1afa94d] | 55 | #define EE_IO 7 /* Could not read file. */
|
---|
[17341d4] | 56 |
|
---|
| 57 | typedef enum {
|
---|
| 58 | /** Leave all segments in RW access mode. */
|
---|
| 59 | ELDF_RW = 1
|
---|
| 60 | } eld_flags_t;
|
---|
| 61 |
|
---|
[6adb775f] | 62 | /** TLS info for a module */
|
---|
| 63 | typedef struct {
|
---|
| 64 | /** tdata section image */
|
---|
| 65 | void *tdata;
|
---|
| 66 | /** Size of tdata section image in bytes */
|
---|
| 67 | size_t tdata_size;
|
---|
| 68 | /** Size of tbss section */
|
---|
| 69 | size_t tbss_size;
|
---|
[29405ac] | 70 | /** Alignment of TLS initialization image */
|
---|
| 71 | size_t tls_align;
|
---|
[6adb775f] | 72 | } elf_tls_info_t;
|
---|
| 73 |
|
---|
[17341d4] | 74 | /**
|
---|
| 75 | * Some data extracted from the headers are stored here
|
---|
| 76 | */
|
---|
| 77 | typedef struct {
|
---|
| 78 | /** Entry point */
|
---|
| 79 | entry_point_t entry;
|
---|
| 80 |
|
---|
| 81 | /** ELF interpreter name or NULL if statically-linked */
|
---|
| 82 | const char *interp;
|
---|
| 83 |
|
---|
| 84 | /** Pointer to the dynamic section */
|
---|
| 85 | void *dynamic;
|
---|
[6adb775f] | 86 |
|
---|
| 87 | /** TLS info */
|
---|
| 88 | elf_tls_info_t tls;
|
---|
[17341d4] | 89 | } elf_finfo_t;
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Holds information about an ELF binary being loaded.
|
---|
| 93 | */
|
---|
| 94 | typedef struct {
|
---|
| 95 | /** Filedescriptor of the file from which we are loading */
|
---|
| 96 | int fd;
|
---|
| 97 |
|
---|
| 98 | /** Difference between run-time addresses and link-time addresses */
|
---|
| 99 | uintptr_t bias;
|
---|
| 100 |
|
---|
| 101 | /** Flags passed to the ELF loader. */
|
---|
| 102 | eld_flags_t flags;
|
---|
| 103 |
|
---|
| 104 | /** A copy of the ELF file header */
|
---|
| 105 | elf_header_t *header;
|
---|
| 106 |
|
---|
| 107 | /** Store extracted info here */
|
---|
| 108 | elf_finfo_t *info;
|
---|
| 109 | } elf_ld_t;
|
---|
| 110 |
|
---|
| 111 | extern const char *elf_error(unsigned int);
|
---|
[bb9ec2d] | 112 | extern int elf_load_file(int, size_t, eld_flags_t, elf_finfo_t *);
|
---|
| 113 | extern int elf_load_file_name(const char *, size_t, eld_flags_t, elf_finfo_t *);
|
---|
[17341d4] | 114 |
|
---|
| 115 | #endif
|
---|
| 116 |
|
---|
| 117 | /** @}
|
---|
| 118 | */
|
---|