2
* Copyright (c) 2006 Sergey Bondari
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup generic
39
#include <arch/types.h>
49
#define ET_NONE 0 /* No type */
50
#define ET_REL 1 /* Relocatable file */
51
#define ET_EXEC 2 /* Executable */
52
#define ET_DYN 3 /* Shared object */
53
#define ET_CORE 4 /* Core */
54
#define ET_LOPROC 0xff00 /* Processor specific */
55
#define ET_HIPROC 0xffff /* Processor specific */
60
#define EM_NO 0 /* No machine */
61
#define EM_SPARC 2 /* SPARC */
62
#define EM_386 3 /* i386 */
63
#define EM_MIPS 8 /* MIPS RS3000 */
64
#define EM_MIPS_RS3_LE 10 /* MIPS RS3000 LE */
65
#define EM_PPC 20 /* PPC32 */
66
#define EM_PPC64 21 /* PPC64 */
67
#define EM_ARM 40 /* ARM */
68
#define EM_SPARCV9 43 /* SPARC64 */
69
#define EM_IA_64 50 /* IA-64 */
70
#define EM_X86_64 62 /* AMD64/EMT64 */
73
* ELF identification indexes
79
#define EI_CLASS 4 /* File class */
80
#define EI_DATA 5 /* Data encoding */
81
#define EI_VERSION 6 /* File version */
83
#define EI_ABIVERSION 8
84
#define EI_PAD 9 /* Start of padding bytes */
85
#define EI_NIDENT 16 /* ELF identification table size */
98
#define ELFCLASSNONE 0
103
* ELF data encoding types
105
#define ELFDATANONE 0
106
#define ELFDATA2LSB 1 /* Least significant byte first (little endian) */
107
#define ELFDATA2MSB 2 /* Most signigicant byte first (big endian) */
110
* ELF error return codes
112
#define EE_OK 0 /* No error */
113
#define EE_INVALID 1 /* Invalid ELF image */
114
#define EE_MEMORY 2 /* Cannot allocate address space */
115
#define EE_INCOMPATIBLE 3 /* ELF image is not compatible with current architecture */
116
#define EE_UNSUPPORTED 4 /* Non-supported ELF (e.g. dynamic ELFs) */
117
#define EE_LOADER 5 /* The image is actually a program loader */
118
#define EE_IRRECOVERABLE 6
124
#define SHT_PROGBITS 1
129
#define SHT_DYNAMIC 6
134
#define SHT_DYNSYM 11
135
#define SHT_LOOS 0x60000000
136
#define SHT_HIOS 0x6fffffff
137
#define SHT_LOPROC 0x70000000
138
#define SHT_HIPROC 0x7fffffff
139
#define SHT_LOUSER 0x80000000
140
#define SHT_HIUSER 0xffffffff
145
#define SHF_WRITE 0x1
146
#define SHF_ALLOC 0x2
147
#define SHF_EXECINSTR 0x4
148
#define SHF_TLS 0x400
149
#define SHF_MASKPROC 0xf0000000
157
#define STB_LOPROC 13
158
#define STB_HIPROC 15
166
#define STT_SECTION 3
168
#define STT_LOPROC 13
169
#define STT_HIPROC 15
172
* Program segment types
181
#define PT_LOPROC 0x70000000
182
#define PT_HIPROC 0x7fffffff
185
* Program segment attributes.
194
* These types are found to be identical in both 32-bit and 64-bit
195
* ELF object file specifications. They are the only types used
198
typedef uint64_t elf_xword;
199
typedef int64_t elf_sxword;
200
typedef uint32_t elf_word;
201
typedef int32_t elf_sword;
202
typedef uint16_t elf_half;
205
* 32-bit ELF data types.
207
* These types are specific for 32-bit format.
209
typedef uint32_t elf32_addr;
210
typedef uint32_t elf32_off;
213
* 64-bit ELF data types.
215
* These types are specific for 64-bit format.
217
typedef uint64_t elf64_addr;
218
typedef uint64_t elf64_off;
221
struct elf32_header {
222
uint8_t e_ident[EI_NIDENT];
231
elf_half e_phentsize;
233
elf_half e_shentsize;
237
struct elf64_header {
238
uint8_t e_ident[EI_NIDENT];
247
elf_half e_phentsize;
249
elf_half e_shentsize;
255
* ELF segment header.
256
* Segments headers are also known as program headers.
258
struct elf32_segment_header {
268
struct elf64_segment_header {
282
struct elf32_section_header {
291
elf_word sh_addralign;
294
struct elf64_section_header {
303
elf_xword sh_addralign;
304
elf_xword sh_entsize;
308
* ELF symbol table entry
310
struct elf32_symbol {
318
struct elf64_symbol {
328
typedef struct elf32_header elf_header_t;
329
typedef struct elf32_segment_header elf_segment_header_t;
330
typedef struct elf32_section_header elf_section_header_t;
331
typedef struct elf32_symbol elf_symbol_t;
334
typedef struct elf64_header elf_header_t;
335
typedef struct elf64_segment_header elf_segment_header_t;
336
typedef struct elf64_section_header elf_section_header_t;
337
typedef struct elf64_symbol elf_symbol_t;
340
extern char *elf_error(unsigned int rc);
342
/* Interpreter string used to recognize the program loader */
343
#define ELF_INTERP_ZSTR "kernel"
344
#define ELF_INTERP_ZLEN sizeof(ELF_INTERP_ZSTR)