1 /* This file defines standard ELF types, structures, and macros.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ian Lance Taylor <ian@cygnus.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 /* Standard ELF types. */
30 /* Type for a 16-bit quantity. */
31 typedef uint16_t Elf32_Half;
32 typedef uint16_t Elf64_Half;
34 /* Types for signed and unsigned 32-bit quantities. */
35 typedef uint32_t Elf32_Word;
36 typedef int32_t Elf32_Sword;
37 typedef uint32_t Elf64_Word;
38 typedef int32_t Elf64_Sword;
40 /* Types for signed and unsigned 64-bit quantities. */
41 typedef uint64_t Elf32_Xword;
42 typedef int64_t Elf32_Sxword;
43 typedef uint64_t Elf64_Xword;
44 typedef int64_t Elf64_Sxword;
46 /* Type of addresses. */
47 typedef uint32_t Elf32_Addr;
48 typedef uint64_t Elf64_Addr;
50 /* Type of file offsets. */
51 typedef uint32_t Elf32_Off;
52 typedef uint64_t Elf64_Off;
54 /* Type for section indices, which are 16-bit quantities. */
55 typedef uint16_t Elf32_Section;
56 typedef uint16_t Elf64_Section;
58 /* Type of symbol indices. */
59 typedef uint32_t Elf32_Symndx;
60 typedef uint64_t Elf64_Symndx;
63 /* The ELF file header. This appears at the start of every ELF file. */
65 #define EI_NIDENT (16)
69 unsigned char e_ident[EI_NIDENT]; /** Magic number and other info */
70 Elf32_Half e_type; /** Object file type */
71 Elf32_Half e_machine; /** Architecture */
72 Elf32_Word e_version; /** Object file version */
73 Elf32_Addr e_entry; /** Entry point virtual address */
74 Elf32_Off e_phoff; /** Program header table file offset */
75 Elf32_Off e_shoff; /** Section header table file offset */
76 Elf32_Word e_flags; /** Processor-specific flags */
77 Elf32_Half e_ehsize; /** ELF header size in bytes */
78 Elf32_Half e_phentsize; /** Program header table entry size */
79 Elf32_Half e_phnum; /** Program header table entry count */
80 Elf32_Half e_shentsize; /** Section header table entry size */
81 Elf32_Half e_shnum; /** Section header table entry count */
82 Elf32_Half e_shstrndx; /** Section header string table index */
85 /* Fields in the e_ident array. The EI_* macros are indices into the
86 array. The macros under each EI_* macro are the values the byte
89 #define EI_MAG0 0 /* File identification byte 0 index */
90 #define ELFMAG0 0x7f /* Magic number byte 0 */
92 #define EI_MAG1 1 /* File identification byte 1 index */
93 #define ELFMAG1 'E' /* Magic number byte 1 */
95 #define EI_MAG2 2 /* File identification byte 2 index */
96 #define ELFMAG2 'L' /* Magic number byte 2 */
98 #define EI_MAG3 3 /* File identification byte 3 index */
99 #define ELFMAG3 'F' /* Magic number byte 3 */
101 /* Conglomeration of the identification bytes, for easy testing as a word. */
102 #define ELFMAG "\177ELF"
105 #define EI_CLASS 4 /* File class byte index */
106 #define ELFCLASSNONE 0 /* Invalid class */
107 #define ELFCLASS32 1 /* 32-bit objects */
108 #define ELFCLASS64 2 /* 64-bit objects */
109 #define ELFCLASSNUM 3
111 #define EI_DATA 5 /* Data encoding byte index */
112 #define ELFDATANONE 0 /* Invalid data encoding */
113 #define ELFDATA2LSB 1 /* 2's complement, little endian */
114 #define ELFDATA2MSB 2 /* 2's complement, big endian */
117 #define EI_VERSION 6 /* File version byte index */
118 /* Value must be EV_CURRENT */
120 #define EI_OSABI 7 /* OS ABI identification */
121 #define ELFOSABI_SYSV 0 /* UNIX System V ABI */
122 #define ELFOSABI_HPUX 1 /* HP-UX */
123 #define ELFOSABI_FREEBSD 9 /* Free BSD */
124 #define ELFOSABI_ARM 97 /* ARM */
125 #define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
127 #define EI_ABIVERSION 8 /* ABI version */
129 #define EI_PAD 9 /* Byte index of padding bytes */
131 /* Legal values for e_type (object file type). */
133 #define ET_NONE 0 /* No file type */
134 #define ET_REL 1 /* Relocatable file */
135 #define ET_EXEC 2 /* Executable file */
136 #define ET_DYN 3 /* Shared object file */
137 #define ET_CORE 4 /* Core file */
138 #define ET_NUM 5 /* Number of defined types */
139 #define ET_LOPROC 0xff00 /* Processor-specific */
140 #define ET_HIPROC 0xffff /* Processor-specific */
142 /* Legal values for e_machine (architecture). */
144 #define EM_NONE 0 /* No machine */
145 #define EM_M32 1 /* AT&T WE 32100 */
146 #define EM_SPARC 2 /* SUN SPARC */
147 #define EM_386 3 /* Intel 80386 */
148 #define EM_68K 4 /* Motorola m68k family */
149 #define EM_88K 5 /* Motorola m88k family */
150 #define EM_486 6 /* Intel 80486 */
151 #define EM_860 7 /* Intel 80860 */
152 #define EM_MIPS 8 /* MIPS R3000 big-endian */
153 #define EM_S370 9 /* Amdahl */
154 #define EM_MIPS_RS4_BE 10 /* MIPS R4000 big-endian */
155 #define EM_RS6000 11 /* RS6000 */
157 #define EM_PARISC 15 /* HPPA */
158 #define EM_nCUBE 16 /* nCUBE */
159 #define EM_VPP500 17 /* Fujitsu VPP500 */
160 #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
161 #define EM_960 19 /* Intel 80960 */
162 #define EM_PPC 20 /* PowerPC */
164 #define EM_V800 36 /* NEC V800 series */
165 #define EM_FR20 37 /* Fujitsu FR20 */
166 #define EM_RH32 38 /* TRW RH32 */
167 #define EM_MMA 39 /* Fujitsu MMA */
168 #define EM_ARM 40 /* ARM */
169 #define EM_FAKE_ALPHA 41 /* Digital Alpha */
170 #define EM_SH 42 /* Hitachi SH */
171 #define EM_SPARCV9 43 /* SPARC v9 64-bit */
172 #define EM_TRICORE 44 /* Siemens Tricore */
173 #define EM_ARC 45 /* Argonaut RISC Core */
174 #define EM_H8_300 46 /* Hitachi H8/300 */
175 #define EM_H8_300H 47 /* Hitachi H8/300H */
176 #define EM_H8S 48 /* Hitachi H8S */
177 #define EM_H8_500 49 /* Hitachi H8/500 */
178 #define EM_IA_64 50 /* Intel Merced */
179 #define EM_MIPS_X 51 /* Stanford MIPS-X */
180 #define EM_COLDFIRE 52 /* Motorola Coldfire */
181 #define EM_68HC12 53 /* Motorola M68HC12 */
184 /* If it is necessary to assign new unofficial EM_* values, please
185 pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
186 chances of collision with official or non-GNU unofficial values. */
188 #define EM_ALPHA 0x9026
190 /* Legal values for e_version (version). */
192 #define EV_NONE 0 /* Invalid ELF version */
193 #define EV_CURRENT 1 /* Current version */
196 /* Section header. */
200 Elf32_Word sh_name; /** Section name (string tbl index) */
201 Elf32_Word sh_type; /** Section type */
202 Elf32_Word sh_flags; /** Section flags */
203 Elf32_Addr sh_addr; /** Section virtual addr at execution */
204 Elf32_Off sh_offset; /** Section file offset */
205 Elf32_Word sh_size; /** Section size in bytes */
206 Elf32_Word sh_link; /** Link to another section */
207 Elf32_Word sh_info; /** Additional section information */
208 Elf32_Word sh_addralign; /** Section alignment */
209 Elf32_Word sh_entsize; /** Entry size if section holds table */
212 /* Special section indices. */
214 #define SHN_UNDEF 0 /* Undefined section */
215 #define SHN_LORESERVE 0xff00 /* Start of reserved indices */
216 #define SHN_LOPROC 0xff00 /* Start of processor-specific */
217 #define SHN_HIPROC 0xff1f /* End of processor-specific */
218 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
219 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
220 #define SHN_HIRESERVE 0xffff /* End of reserved indices */
222 /* Legal values for sh_type (section type). */
224 #define SHT_NULL 0 /* Section header table entry unused */
225 #define SHT_PROGBITS 1 /* Program data */
226 #define SHT_SYMTAB 2 /* Symbol table */
227 #define SHT_STRTAB 3 /* String table */
228 #define SHT_RELA 4 /* Relocation entries with addends */
229 #define SHT_HASH 5 /* Symbol hash table */
230 #define SHT_DYNAMIC 6 /* Dynamic linking information */
231 #define SHT_NOTE 7 /* Notes */
232 #define SHT_NOBITS 8 /* Program space with no data (bss) */
233 #define SHT_REL 9 /* Relocation entries, no addends */
234 #define SHT_SHLIB 10 /* Reserved */
235 #define SHT_DYNSYM 11 /* Dynamic linker symbol table */
236 #define SHT_NUM 12 /* Number of defined types. */
237 #define SHT_LOOS 0x60000000 /* Start OS-specific */
238 #define SHT_LOSUNW 0x6ffffffb /* Sun-specific low bound. */
239 #define SHT_SUNW_COMDAT 0x6ffffffb
240 #define SHT_SUNW_syminfo 0x6ffffffc
241 #define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */
242 #define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */
243 #define SHT_GNU_versym 0x6fffffff /* Version symbol table. */
244 #define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */
245 #define SHT_HIOS 0x6fffffff /* End OS-specific type */
246 #define SHT_LOPROC 0x70000000 /* Start of processor-specific */
247 #define SHT_HIPROC 0x7fffffff /* End of processor-specific */
248 #define SHT_LOUSER 0x80000000 /* Start of application-specific */
249 #define SHT_HIUSER 0x8fffffff /* End of application-specific */
251 /* Legal values for sh_flags (section flags). */
253 #define SHF_WRITE (1 << 0) /* Writable */
254 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
255 #define SHF_EXECINSTR (1 << 2) /* Executable */
256 #define SHF_MASKPROC 0xf0000000 /* Processor-specific */
258 /* --- Simplified ELF header --- */
259 typedef struct simple_elf {
260 const char * e_fname; /* filename of binary */
261 unsigned long e_entry; /* entry point virtual address */
262 unsigned long e_txtoff; /* offset of text segment in file */
263 unsigned long e_txtlen; /* length of text segment in bytes */
264 unsigned long e_txtstart; /* start of text segment virtual address */
265 unsigned long e_datoff; /* offset of data segment in file */
266 unsigned long e_datlen; /* length of data segment in bytes */
267 unsigned long e_datstart; /* start of data segment in virtual memory */
268 unsigned long e_rodatoff; /* offset of rodata segment in file */
269 unsigned long e_rodatlen; /* length of rodata segment in bytes */
270 unsigned long e_rodatstart; /* start of rodata segment in virtual memory*/
271 unsigned long e_bsslen; /* length of bss segment in bytes */
274 /* --- Defines --- */
275 #define NMAGIC 0410 /* 0x108 */
276 #define N_MAGIC(exec) ((exec).a_midmag & 0xffff)
277 #define N_BADMAG(x) (N_MAGIC(x) != NMAGIC)
279 #define ELF_NOTELF -1
280 #define ELF_SUCCESS 0
281 #define NOT_PRESENT -2
284 * elf function prototypes
286 extern int getbytes( const char *filename, int offset, int size, char *buf);
287 extern int elf_load_helper( simple_elf_t *se_hdr, const char *fname );
288 extern int elf_check_header( const char *fname );