1 /**
2 * \file
3 * \brief ELF definition
4 *
5 * \date 08/18/2000
6 * \author Frank Mehnert <fm3@os.inf.tu-dresden.de>
7 * Alexander Warg <aw11@os.inf.tu-dresden.de>
8 *
9 * Many structs from
10 * "Executable and Linkable Format (ELF)",
11 * Portable Formats Specification, Version 1.1
12 * and
13 * "System V Application Binary Interface - DRAFT - April 29, 1998"
14 * The Santa Cruz Operation, Inc.
15 * (see http://www.sco.com/developer/gabi/contents.html)
16 *
17 * \ingroup l4util_elf
18 */
19 /*
20 * (c) 2008-2009 Author(s)
21 * economic rights: Technische Universität Dresden (Germany)
22 * This file is part of TUD:OS and distributed under the terms of the
23 * GNU Lesser General Public License 2.1.
24 * Please see the COPYING-LGPL-2.1 file for details.
25 */
26
27 /* (c) 2003-2006 Technische Universitaet Dresden
28 * This file is part of the exec package, which is distributed under
29 * the terms of the GNU General Public License 2. Please see the
30 * COPYING file for details. */
31
32 #ifndef _L4_EXEC_ELF_H
33 #define _L4_EXEC_ELF_H
34
35 #include <l4/sys/l4int.h>
36
37 /**
38 * \defgroup l4util_elf ELF binary format
39 * \ingroup l4util_api
40 * \brief Functions and types related to ELF binaries.
41 */
42
43 /** \name ELF types
44 * \ingroup l4util_elf
45 */
46 /*@{*/
47 typedef l4_uint32_t Elf32_Addr; /**< size 4 align 4 \ingroup l4util_elf*/
48 typedef l4_uint32_t Elf32_Off; /**< size 4 align 4 \ingroup l4util_elf*/
49 typedef l4_uint16_t Elf32_Half; /**< size 2 align 2 \ingroup l4util_elf*/
50 typedef l4_uint32_t Elf32_Word; /**< size 4 align 4 \ingroup l4util_elf*/
51 typedef l4_int32_t Elf32_Sword; /**< size 4 align 4 \ingroup l4util_elf*/
52 typedef l4_uint64_t Elf64_Addr; /**< size 8 align 8 \ingroup l4util_elf*/
53 typedef l4_uint64_t Elf64_Off; /**< size 8 align 8 \ingroup l4util_elf*/
54 typedef l4_uint16_t Elf64_Half; /**< size 2 align 2 \ingroup l4util_elf*/
55 typedef l4_uint32_t Elf64_Word; /**< size 4 align 4 \ingroup l4util_elf*/
56 typedef l4_int32_t Elf64_Sword; /**< size 4 align 4 \ingroup l4util_elf*/
57 typedef l4_uint64_t Elf64_Xword; /**< size 8 align 8 \ingroup l4util_elf*/
58 typedef l4_int64_t Elf64_Sxword; /**< size 8 align 8 \ingroup l4util_elf*/
59 /*@}*/
60
61 #if L4_MWORD_BITS == 64
62 /**
63 * \brief Use 64 or 32 bits types depending on the target architecture.
64 * \ingroup l4util_elf
65 */
66 #define ElfW(type) _ElfW(Elf, 64, type)
67 #else
68 #define ElfW(type) _ElfW(Elf, 32, type)
69 #endif
70 #define _ElfW(e,w,t) __ElfW(e, w, _##t)
71 #define __ElfW(e,w,t) e##w##t
72
73 #if defined(ARCH_x86)
74 #define L4_ARCH_EI_DATA ELFDATA2LSB
75 #define L4_ARCH_E_MACHINE EM_386
76 #define L4_ARCH_EI_CLASS ELFCLASS32
77 #elif defined(ARCH_amd64)
78 #define L4_ARCH_EI_DATA ELFDATA2LSB
79 #define L4_ARCH_E_MACHINE EM_X86_64
80 #define L4_ARCH_EI_CLASS ELFCLASS64
81 #elif defined(ARCH_arm)
82 #define L4_ARCH_EI_DATA ELFDATA2LSB
83 #define L4_ARCH_E_MACHINE EM_ARM
84 #define L4_ARCH_EI_CLASS ELFCLASS32
85 #elif defined(ARCH_arm64)
86 #define L4_ARCH_EI_DATA ELFDATA2LSB
87 #define L4_ARCH_E_MACHINE EM_AARCH64
88 #define L4_ARCH_EI_CLASS ELFCLASS64
89 #elif defined(ARCH_ppc32)
90 #define L4_ARCH_EI_DATA ELFDATA2MSB
91 #define L4_ARCH_E_MACHINE EM_PPC
92 #define L4_ARCH_EI_CLASS ELFCLASS32
93 #elif defined(ARCH_sparc)
94 #define L4_ARCH_EI_DATA ELFDATA2MSB
95 #define L4_ARCH_E_MACHINE EM_SPARC
96 #define L4_ARCH_EI_CLASS ELFCLASS32
97 #elif defined(ARCH_mips)
98 #define L4_ARCH_EI_DATA ELFDATA2LSB
99 #define L4_ARCH_E_MACHINE EM_MIPS
100 #ifdef __mips64
101 #define L4_ARCH_EI_CLASS ELFCLASS64
102 #else
103 #define L4_ARCH_EI_CLASS ELFCLASS32
104 #endif
105 #else
106 #warning elf.h: Unsupported build architecture!
107 #endif
108
109
110 /*************************************/
111 /* ELF Header - figure 1-3, page 1-3 */
112 /*************************************/
113
114 /** \addtogroup l4util_elf */
115
116 /*@{*/
117
118 #define EI_NIDENT 16 /**< \brief number of characters */
119 /**
120 * \brief ELF32 header.
121 */
122 typedef struct {
123 unsigned char e_ident[EI_NIDENT];
124 Elf32_Half e_type; /**< type of ELF file */
125 Elf32_Half e_machine; /**< required architecture */
126 Elf32_Word e_version; /**< file version */
127 Elf32_Addr e_entry; /**< initial eip */
128 Elf32_Off e_phoff; /**< offset of program header table */
129 Elf32_Off e_shoff; /**< offset of file header table */
130 Elf32_Word e_flags; /**< processor-specific flags */
131 Elf32_Half e_ehsize; /**< size of ELF header */
132 Elf32_Half e_phentsize; /**< size of program header entry */
133 Elf32_Half e_phnum; /**< # of entries in prog. head. tab. */
134 Elf32_Half e_shentsize; /**< size of section header entry */
135 Elf32_Half e_shnum; /**< # of entries in sect. head. tab. */
136 Elf32_Half e_shstrndx; /**< sect.head.tab.idx of strtab */
137 } Elf32_Ehdr;
138
139 /**
140 * \brief ELF64 header.
141 */
142 typedef struct {
143 unsigned char e_ident[EI_NIDENT];
144 Elf64_Half e_type; /**< \brief type of ELF file */
145 Elf64_Half e_machine; /**< \brief required architecture */
146 Elf64_Word e_version; /**< \brief file version */
147 Elf64_Addr e_entry; /**< \brief initial eip */
148 Elf64_Off e_phoff; /**< \brief offset of program header table */
149 Elf64_Off e_shoff; /**< \brief offset of file header table */
150 Elf64_Word e_flags; /**< \brief processor-specific flags */
151 Elf64_Half e_ehsize; /**< \brief size of ELF header */
152 Elf64_Half e_phentsize; /**< \brief size of program header entry */
153 Elf64_Half e_phnum; /**< \brief # of entries in prog. head. tab. */
154 Elf64_Half e_shentsize; /**< \brief size of section header entry */
155 Elf64_Half e_shnum; /**< \brief # of entries in sect. head. tab. */
156 Elf64_Half e_shstrndx; /**< \brief sect.head.tab.idx of strtab */
157 } Elf64_Ehdr;
158
159 #define EI_CLASS 4 /**< \brief ELF class byte index */
160 #define ELFCLASSNONE 0 /**< \brief Invalid ELF class */
161 #define ELFCLASS32 1 /**< \brief 32-bit objects */
162 #define ELFCLASS64 2 /**< \brief 64-bit objects */
163 #define ELFCLASSNUM 3 /**< \brief Mask for 32-bit or 64-bit class */
164
165 #define EI_DATA 5 /**< Data encoding byte index */
166 #define ELFDATANONE 0 /**< Invalid data encoding */
167 #define ELFDATA2LSB 1 /**< 2's complement, little endian */
168 #define ELFDATA2MSB 2 /**< 2's complement, big endian */
169 #define ELFDATANUM 3
170
171 #define EI_VERSION 6 /**< File version byte index */
172 /**< Value must be EV_CURRENT */
173
174 #define EI_OSABI 7 /**< OS ABI identification */
175 #define ELFOSABI_NONE 0 /**< UNIX System V ABI */
176 #define ELFOSABI_SYSV 0 /**< Alias. */
177 #define ELFOSABI_HPUX 1 /**< HP-UX */
178 #define ELFOSABI_NETBSD 2 /**< NetBSD. */
179 #define ELFOSABI_LINUX 3 /**< Linux. */
180 #define ELFOSABI_SOLARIS 6 /**< Sun Solaris. */
181 #define ELFOSABI_AIX 7 /**< IBM AIX. */
182 #define ELFOSABI_IRIX 8 /**< SGI Irix. */
183 #define ELFOSABI_FREEBSD 9 /**< FreeBSD. */
184 #define ELFOSABI_TRU64 10 /**< Compaq TRU64 UNIX. */
185 #define ELFOSABI_MODESTO 11 /**< Novell Modesto. */
186 #define ELFOSABI_OPENBSD 12 /**< OpenBSD. */
187 #define ELFOSABI_ARM 97 /**< ARM */
188 #define ELFOSABI_STANDALONE 255 /**< Standalone (embedded) application */
189
190 #define EI_ABIVERSION 8 /**< ABI version */
191
192 #define EI_PAD 9 /**< Byte index of padding bytes */
193
194 /* object file type - page 1-3 (e_type) */
195
196 #define ET_NONE 0 /**< no file type */
197 #define ET_REL 1 /**< relocatable file */
198 #define ET_EXEC 2 /**< executable file */
199 #define ET_DYN 3 /**< shared object file */
200 #define ET_CORE 4 /**< core file */
201 #define ET_LOPROC 0xff00 /**< processor-specific */
202 #define ET_HIPROC 0xffff /**< processor-specific */
203
204 /* required architecture - page 1-4 (e_machine) */
205
206 #define EM_NONE 0 /**< no machine */
207 #define EM_M32 1 /**< AT&T WE 32100 */
208 #define EM_SPARC 2 /**< SPARC */
209 #define EM_386 3 /**< Intel 80386 */
210 #define EM_68K 4 /**< Motorola 68000 */
211 #define EM_88K 5 /**< Motorola 88000 */
212 #define EM_860 7 /**< Intel 80860 */
213 #define EM_MIPS 8 /**< MIPS RS3000 big-endian */
214 #define EM_MIPS_RS4_BE 10 /**< MIPS RS4000 big-endian */
215 #define EM_SPARC64 11 /**< SPARC 64-bit */
216 #define EM_PARISC 15 /**< HP PA-RISC */
217 #define EM_VPP500 17 /**< Fujitsu VPP500 */
218 #define EM_SPARC32PLUS 18 /**< Sun's V8plus */
219 #define EM_960 19 /**< Intel 80960 */
220 #define EM_PPC 20 /**< PowerPC */
221 #define EM_V800 36 /**< NEC V800 */
222 #define EM_FR20 37 /**< Fujitsu FR20 */
223 #define EM_RH32 38 /**< TRW RH-32 */
224 #define EM_RCE 39 /**< Motorola RCE */
225 #define EM_ARM 40 /**< Advanced RISC Machines ARM */
226 #define EM_ALPHA 41 /**< Digital Alpha */
227 #define EM_SH 42 /**< Hitachi SuperH */
228 #define EM_SPARCV9 43 /**< SPARC v9 64-bit */
229 #define EM_TRICORE 44 /**< Siemens Tricore embedded processor */
230 #define EM_ARC 45 /**< Argonaut RISC Core, Argonaut Techn Inc. */
231 #define EM_H8_300 46 /**< Hitachi H8/300 */
232 #define EM_H8_300H 47 /**< Hitachi H8/300H */
233 #define EM_H8S 48 /**< Hitachi H8/S */
234 #define EM_H8_500 49 /**< Hitachi H8/500 */
235 #define EM_IA_64 50 /**< HP/Intel IA-64 */
236 #define EM_MIPS_X 51 /**< Stanford MIPS-X */
237 #define EM_COLDFIRE 52 /**< Motorola Coldfire */
238 #define EM_68HC12 53 /**< Motorola M68HC12 */
239 #define EM_X86_64 62 /**< Advanced Micro Devices x86-64 */
240 #define EM_PDSP 63 /**< Sony DSP Processor */
241 #define EM_FX66 66 /**< Siemens FX66 microcontroller */
242 #define EM_ST9PLUS 67 /**< STMicroelectronics ST9+ 8/16 mc */
243 #define EM_ST7 68 /**< STmicroelectronics ST7 8 bit mc */
244 #define EM_68HC16 69 /**< Motorola MC68HC16 microcontroller */
245 #define EM_68HC11 70 /**< Motorola MC68HC11 microcontroller */
246 #define EM_68HC08 71 /**< Motorola MC68HC08 microcontroller */
247 #define EM_68HC05 72 /**< Motorola MC68HC05 microcontroller */
248 #define EM_SVX 73 /**< Silicon Graphics SVx */
249 #define EM_ST19 74 /**< STMicroelectronics ST19 8 bit mc */
250 #define EM_VAX 75 /**< Digital VAX */
251 #define EM_CRIS 76 /**< Axis Communications 32-bit embedded processor */
252 #define EM_JAVELIN 77 /**< Infineon Technologies 32-bit embedded processor */
253 #define EM_FIREPATH 78 /**< Element 14 64-bit DSP Processor */
254 #define EM_ZSP 79 /**< LSI Logic 16-bit DSP Processor */
255 #define EM_MMIX 80 /**< Donald Knuth's educational 64-bit processor */
256 #define EM_HUANY 81 /**< Harvard University machine-independent object files */
257 #define EM_PRISM 82 /**< SiTera Prism */
258 #define EM_AVR 83 /**< Atmel AVR 8-bit microcontroller */
259 #define EM_FR30 84 /**< Fujitsu FR30 */
260 #define EM_D10V 85 /**< Mitsubishi D10V */
261 #define EM_D30V 86 /**< Mitsubishi D30V */
262 #define EM_V850 87 /**< NEC v850 */
263 #define EM_M32R 88 /**< Mitsubishi M32R */
264 #define EM_MN10300 89 /**< Matsushita MN10300 */
265 #define EM_MN10200 90 /**< Matsushita MN10200 */
266 #define EM_PJ 91 /**< picoJava */
267 #define EM_OPENRISC 92 /**< OpenRISC 32-bit embedded processor */
268 #define EM_ARC_A5 93 /**< ARC Cores Tangent-A5 */
269 #define EM_XTENSA 94 /**< Tensilica Xtensa Architecture */
270 #define EM_ALTERA_NIOS2 113 /**< Altera Nios II */
271 #define EM_AARCH64 183 /**< ARM AARCH64 */
272 #define EM_TILEPRO 188 /**< Tilera TILEPro */
273 #define EM_MICROBLAZE 189 /**< Xilinx MicroBlaze */
274 #define EM_TILEGX 191 /**< Tilera TILE-Gx */
275 #define EM_NUM 192
276
277 #if 0
278 #define EM_ALPHA 0x9026 /* interium value used by Linux until the
279 committee comes up with a final number */
280 #define EM_S390 0xA390 /* interium value used for IBM S390 */
281 #endif
282
283 /* object file version - page 1-4 (e_version) */
284
285 #define EV_NONE 0 /**< Invalid version */
286 #define EV_CURRENT 1 /**< Current version */
287
288 /* e_ident[] Identification Indexes - figure 1-4, page 1-5 */
289
290 #define EI_MAG0 0 /**< file id */
291 #define EI_MAG1 1 /**< file id */
292 #define EI_MAG2 2 /**< file id */
293 #define EI_MAG3 3 /**< file id */
294 #define EI_CLASS 4 /**< file class */
295 #define EI_DATA 5 /**< data encoding */
296 #define EI_VERSION 6 /**< file version */
297 #define EI_OSABI 7 /**< Operating system / ABI identification */
298 #define EI_ABIVERSION 8 /**< ABI version */
299 #define EI_PAD 9 /**< start of padding bytes */
300
301 /* magic number - page 1-5 */
302
303 #define ELFMAG0 0x7f /**< e_ident[EI_MAG0] */
304 #define ELFMAG1 'E' /**< e_ident[EI_MAG1] */
305 #define ELFMAG2 'L' /**< e_ident[EI_MAG2] */
306 #define ELFMAG3 'F' /**< e_ident[EI_MAG3] */
307
308 /* file class or capacity - page 1-6 */
309
310 #define ELFCLASSNONE 0 /**< Invalid class */
311 #define ELFCLASSS32 1 /**< 32-bit object */
312 #define ELFCLASSS64 2 /**< 64-bit object */
313
314 /* data encoding - page 1-6 */
315
316 #define ELFDATANONE 0 /**< invalid data encoding */
317 #define ELFDATA2LSB 1 /**< 0x01020304 => [ 0x04|0x03|0x02|0x01 ] */
318 #define ELFDATA2MSB 2 /**< 0x01020304 => [ 0x01|0x02|0x03|0x04 ] */
319
320 /* Identify operating system and ABI to which the object is targeted */
321
322 #define ELFOSABI_SYSV 0 /**< UNIX System V ABI (this specification) */
323 #define ELFOSABI_HPUX 1 /**< HP-UX operating system */
324 #define ELFOSABI_STANDALONE 255 /**< Standalone (embedded) application */
325
326
327 /***********************/
328 /* Sections - page 1-8 */
329 /***********************/
330
331 /* special section indexes */
332
333 #define SHN_UNDEF 0 /**< undefined section header entry */
334 #define SHN_LORESERVE 0xff00 /**< lower bound of reserved indexes */
335 #define SHN_LOPROC 0xff00 /**< lower bound of proc spec entr */
336 #define SHN_HIPROC 0xff1f /**< upper bound of proc spec entr */
337 #define SHN_ABS 0xfff1 /**< absolute values for ref */
338 #define SHN_COMMON 0xfff2 /**< common symbols */
339 #define SHN_HIRESERVE 0xffff /**< upper bound of reserved indexes */
340
341 /** ELF32 section header - figure 1-9, page 1-9 */
342 typedef struct {
343 Elf32_Word sh_name; /**< name of sect (idx into strtab) */
344 Elf32_Word sh_type; /**< section's type */
345 Elf32_Word sh_flags; /**< section's flags */
346 Elf32_Addr sh_addr; /**< memory address of section */
347 Elf32_Off sh_offset; /**< file offset of section */
348 Elf32_Word sh_size; /**< file size of section */
349 Elf32_Word sh_link; /**< idx to associated header section */
350 Elf32_Word sh_info; /**< extra info of header section */
351 Elf32_Word sh_addralign; /**< address alignment constraints */
352 Elf32_Word sh_entsize; /**< size of entry if sect is table */
353 } Elf32_Shdr;
354
355 /** ELF64 section header */
356 typedef struct {
357 Elf64_Word sh_name; /**< name of sect (idx into strtab) */
358 Elf64_Word sh_type; /**< section's type */
359 Elf64_Xword sh_flags; /**< section's flags */
360 Elf64_Addr sh_addr; /**< memory address of section */
361 Elf64_Off sh_offset; /**< file offset of section */
362 Elf64_Xword sh_size; /**< file size of section */
363 Elf64_Word sh_link; /**< idx to associated header section */
364 Elf64_Word sh_info; /**< extra info of header section */
365 Elf64_Xword sh_addralign; /**< address alignment constraints */
366 Elf64_Xword sh_entsize; /**< size of entry if sect is table */
367 } Elf64_Shdr;
368
369 /* section type - figure 1-10, page 1-10 */
370
371 #define SHT_NULL 0
372 #define SHT_PROGBITS 1
373 #define SHT_SYMTAB 2
374 #define SHT_STRTAB 3
375 #define SHT_RELA 4
376 #define SHT_HASH 5
377 #define SHT_DYNAMIC 6
378 #define SHT_NOTE 7
379 #define SHT_NOBITS 8
380 #define SHT_REL 9
381 #define SHT_SHLIB 10
382 #define SHT_DYNSYM 11
383 #define SHT_INIT_ARRAY 14 /**< Array of constructors */
384 #define SHT_FINI_ARRAY 15 /**< Array of destructors */
385 #define SHT_PREINIT_ARRAY 16 /**< Array of pre-constructors */
386 #define SHT_GROUP 17 /**< Section group */
387 #define SHT_SYMTAB_SHNDX 18 /**< Extended section indeces */
388 #define SHT_NUM 19 /**< Number of defined types. */
389 #define SHT_LOOS 0x60000000
390 #define SHT_HIOS 0x6fffffff
391 #define SHT_LOPROC 0x70000000
392 #define SHT_HIPROC 0x7fffffff
393 #define SHT_LOUSER 0x80000000
394 #define SHT_HIUSER 0xffffffff
395
396 /* section attribute flags - page 1-12, figure 1-12 */
397
398 #define SHF_WRITE 0x1 /**< writeable during execution */
399 #define SHF_ALLOC 0x2 /**< section occupies virt memory */
400 #define SHF_EXECINSTR 0x4 /**< code section */
401 #define SHF_MERGE 0x10 /**< Might be merged */
402 #define SHF_STRINGS 0x20 /**< Contains nul-terminated strings */
403 #define SHF_INFO_LINK 0x40 /**< `sh_info' contains SHT index */
404 #define SHF_LINK_ORDER 0x80 /**< Preserve order after combining */
405 #define SHF_OS_NONCONFORMING 0x100 /**< Non-standard OS specific handling
406 required */
407 #define SHF_GROUP 0x200 /**< Section is member of a group. */
408 #define SHF_TLS 0x400 /**< Section hold thread-local data. */
409 #define SHF_MASKOS 0x0ff00000 /**< OS-specific. */
410 #define SHF_MASKPROC 0xf0000000 /**< proc spec mask */
411
412
413 /*****************************************/
414 /* Program Header - figure 2-1, page 2-2 */
415 /*****************************************/
416
417 /** ELF32 program header */
418 typedef struct {
419 Elf32_Word p_type; /**< type of program section */
420 Elf32_Off p_offset; /**< file offset of program section */
421 Elf32_Addr p_vaddr; /**< memory address of prog section */
422 Elf32_Addr p_paddr; /**< physical address (ignored) */
423 Elf32_Word p_filesz; /**< file size of program section */
424 Elf32_Word p_memsz; /**< memory size of program section */
425 Elf32_Word p_flags; /**< flags */
426 Elf32_Word p_align; /**< alignment of section */
427 } Elf32_Phdr;
428
429 /** ELF64 program header */
430 typedef struct {
431 Elf64_Word p_type; /**< type of program section */
432 Elf64_Word p_flags; /**< flags */
433 Elf64_Off p_offset; /**< file offset of program section */
434 Elf64_Addr p_vaddr; /**< memory address of prog section */
435 Elf64_Addr p_paddr; /**< physical address (ignored) */
436 Elf64_Xword p_filesz; /**< file size of program section */
437 Elf64_Xword p_memsz; /**< memory size of program section */
438 Elf64_Xword p_align; /**< alignment of section */
439 } Elf64_Phdr;
440
441 /* segment types - figure 2-2, page 2-3 */
442
443 #define PT_NULL 0 /**< array is unused */
444 #define PT_LOAD 1 /**< loadable */
445 #define PT_DYNAMIC 2 /**< dynamic linking information */
446 #define PT_INTERP 3 /**< path to interpreter */
447 #define PT_NOTE 4 /**< auxiliary information */
448 #define PT_SHLIB 5 /**< reserved */
449 #define PT_PHDR 6 /**< location of the pht itself */
450 #define PT_TLS 7 /**< Thread-local storage segment */
451 #define PT_NUM 8 /**< Number of defined types */
452 #define PT_LOOS 0x60000000 /**< os spec. */
453 #define PT_HIOS 0x6fffffff /**< os spec. */
454 #define PT_LOPROC 0x70000000 /**< processor spec. */
455 #define PT_HIPROC 0x7fffffff /**< processor spec. */
456
457 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550) /**< EH frame information. */
458 #define PT_GNU_STACK (PT_LOOS + 0x474e551) /**< Flags for stack. */
459 #define PT_GNU_RELRO (PT_LOOS + 0x474e552) /**< Read only after reloc. */
460
461 #define PT_L4_STACK (PT_LOOS + 0x12) /**< Address of the stack. */
462 #define PT_L4_KIP (PT_LOOS + 0x13) /**< Address of the KIP. */
463 #define PT_L4_AUX (PT_LOOS + 0x14) /**< Address of the AUX strcutures. */
464
465 /* segment permissions - page 2-3 */
466
467 #define PF_X 0x1
468 #define PF_W 0x2
469 #define PF_R 0x4
470 #define PF_MASKOS 0x0ff00000
471 #define PF_MASKPROC 0x7fffffff
472
473 /* Legal values for note segment descriptor types for core files. */
474
475 #define NT_PRSTATUS 1 /**< Contains copy of prstatus struct */
476 #define NT_FPREGSET 2 /**< Contains copy of fpregset struct */
477 #define NT_PRPSINFO 3 /**< Contains copy of prpsinfo struct */
478 #define NT_PRXREG 4 /**< Contains copy of prxregset struct */
479 #define NT_TASKSTRUCT 4 /**< Contains copy of task structure */
480 #define NT_PLATFORM 5 /**< String from sysinfo(SI_PLATFORM) */
481 #define NT_AUXV 6 /**< Contains copy of auxv array */
482 #define NT_GWINDOWS 7 /**< Contains copy of gwindows struct */
483 #define NT_ASRS 8 /**< Contains copy of asrset struct */
484 #define NT_PSTATUS 10 /**< Contains copy of pstatus struct */
485 #define NT_PSINFO 13 /**< Contains copy of psinfo struct */
486 #define NT_PRCRED 14 /**< Contains copy of prcred struct */
487 #define NT_UTSNAME 15 /**< Contains copy of utsname struct */
488 #define NT_LWPSTATUS 16 /**< Contains copy of lwpstatus struct */
489 #define NT_LWPSINFO 17 /**< Contains copy of lwpinfo struct */
490 #define NT_PRFPXREG 20 /**< Contains copy of fprxregset struct*/
491
492 /* Legal values for the note segment descriptor types for object files. */
493
494 #define NT_VERSION 1 /**< Contains a version string. */
495
496 /* Dynamic structure - figure 2-9, page 2-12 */
497
498 /** ELF32 dynamic entry */
499 typedef struct {
500 Elf32_Sword d_tag; /**< see DT_ values */
501 union {
502 Elf32_Word d_val; /**< integer values with various interpret. */
503 Elf32_Addr d_ptr; /**< program virtual addresses */
504 } d_un;
505 } Elf32_Dyn;
506
507 /** ELF64 dynamic entry */
508 typedef struct {
509 Elf64_Sxword d_tag; /**< see DT_ values */
510 union {
511 Elf64_Xword d_val; /**< integer values with various interpret. */
512 Elf64_Addr d_ptr; /**< program virtual addresses */
513 } d_un;
514 } Elf64_Dyn;
515
516 /** Dynamic Array Tags, d_tag - figure 2-10, page 2-12 */
517
518 #define DT_NULL 0 /**< end of _DYNAMIC array */
519 #define DT_NEEDED 1 /**< name of a needed library */
520 #define DT_PLTRELSZ 2 /**< total size of relocation entry */
521 #define DT_PLTGOT 3 /**< address assoc with prog link table */
522 #define DT_HASH 4 /**< address of symbol hash table */
523 #define DT_STRTAB 5 /**< address of string table */
524 #define DT_SYMTAB 6 /**< address of symbol table */
525 #define DT_RELA 7 /**< address of relocation table */
526 #define DT_RELASZ 8 /**< total size of relocation table */
527 #define DT_RELAENT 9 /**< size of DT_RELA relocation entry */
528 #define DT_STRSZ 10 /**< size of the string table */
529 #define DT_SYMENT 11 /**< size of a symbol table entry */
530 #define DT_INIT 12 /**< address of initialization function */
531 #define DT_FINI 13 /**< address of termination function */
532 #define DT_SONAME 14 /**< name of the shared object */
533 #define DT_RPATH 15 /**< search library path */
534 #define DT_SYMBOLIC 16 /**< alter symbol resolution algorithm */
535 #define DT_REL 17 /**< address of relocation table */
536 #define DT_RELSZ 18 /**< total size of DT_REL relocation table */
537 #define DT_RELENT 19 /**< size of the DT_REL relocation entry */
538 #define DT_PTRREL 20 /**< type of relocation entry */
539 #define DT_DEBUG 21 /**< for debugging purposes */
540 #define DT_TEXTREL 22 /**< at least on entry changes r/o section */
541 #define DT_JMPREL 23 /**< address of relocation entries */
542 #define DT_BIND_NOW 24 /**< Process relocations of object */
543 #define DT_INIT_ARRAY 25 /**< Array with addresses of init fct */
544 #define DT_FINI_ARRAY 26 /**< Array with addresses of fini fct */
545 #define DT_INIT_ARRAYSZ 27 /**< Size in bytes of DT_INIT_ARRAY */
546 #define DT_FINI_ARRAYSZ 28 /**< Size in bytes of DT_FINI_ARRAY */
547 #define DT_RUNPATH 29 /**< Library search path */
548 #define DT_FLAGS 30 /**< Flags for the object being loaded */
549 #define DT_ENCODING 32 /**< Start of encoded range */
550 #define DT_PREINIT_ARRAY 32 /**< Array with addresses of preinit fct*/
551 #define DT_PREINIT_ARRAYSZ 33 /**< size in bytes of DT_PREINIT_ARRAY */
552 #define DT_NUM 34 /**< Number used */
553 #define DT_LOOS 0x6000000d /**< Start of OS-specific */
554 #define DT_HIOS 0x6ffff000 /**< End of OS-specific */
555 #define DT_LOPROC 0x70000000 /**< processor spec. */
556 #define DT_HIPROC 0x7fffffff /**< processor spec. */
557
558 /* Values of `d_un.d_val' in the DT_FLAGS entry. */
559 #define DF_ORIGIN 0x00000001 /**< Object may use DF_ORIGIN */
560 #define DF_SYMBOLIC 0x00000002 /**< Symbol resolutions starts here */
561 #define DF_TEXTREL 0x00000004 /**< Object contains text relocations */
562 #define DF_BIND_NOW 0x00000008 /**< No lazy binding for this object */
563 #define DF_STATIC_TLS 0x00000010 /**< Module uses the static TLS model */
564
565 /* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1
566 entry in the dynamic section. */
567 #define DF_1_NOW 0x00000001 /**< Set RTLD_NOW for this object. */
568 #define DF_1_GLOBAL 0x00000002 /**< Set RTLD_GLOBAL for this object. */
569 #define DF_1_GROUP 0x00000004 /**< Set RTLD_GROUP for this object. */
570 #define DF_1_NODELETE 0x00000008 /**< Set RTLD_NODELETE for this object.*/
571 #define DF_1_LOADFLTR 0x00000010 /**< Trigger filtee loading at runtime.*/
572 #define DF_1_INITFIRST 0x00000020 /**< Set RTLD_INITFIRST for this object*/
573 #define DF_1_NOOPEN 0x00000040 /**< Set RTLD_NOOPEN for this object. */
574 #define DF_1_ORIGIN 0x00000080 /**< $ORIGIN must be handled. */
575 #define DF_1_DIRECT 0x00000100 /**< Direct binding enabled. */
576 #define DF_1_TRANS 0x00000200
577 #define DF_1_INTERPOSE 0x00000400 /**< Object is used to interpose. */
578 #define DF_1_NODEFLIB 0x00000800 /**< Ignore default lib search path. */
579 #define DF_1_NODUMP 0x00001000 /**< Object can't be dldump'ed. */
580 #define DF_1_CONFALT 0x00002000 /**< Configuration alternative created.*/
581 #define DF_1_ENDFILTEE 0x00004000 /**< Filtee terminates filters search. */
582 #define DF_1_DISPRELDNE 0x00008000 /**< Disp reloc applied at build time. */
583 #define DF_1_DISPRELPND 0x00010000 /**< Disp reloc applied at run-time. */
584
585 /* Flags for the feature selection in DT_FEATURE_1. */
586 #define DTF_1_PARINIT 0x00000001
587 #define DTF_1_CONFEXP 0x00000002
588
589 /* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */
590 #define DF_P1_LAZYLOAD 0x00000001 /**< Lazyload following object. */
591 #define DF_P1_GROUPPERM 0x00000002 /**< Symbols from next object are not
592 generally available. */
593
594 /* Relocation - page 1-21, figure 1-20 */
595
596 typedef struct {
597 Elf32_Addr r_offset;
598 Elf32_Word r_info;
599 } Elf32_Rel;
600
601 typedef struct {
602 Elf32_Addr r_offset;
603 Elf32_Word r_info;
604 Elf32_Sword r_addend;
605 } Elf32_Rela;
606
607 typedef struct {
608 Elf64_Addr r_offset;
609 Elf64_Xword r_info;
610 } Elf64_Rel;
611
612 typedef struct {
613 Elf64_Addr r_offset;
614 Elf64_Xword r_info;
615 Elf64_Sxword r_addend;
616 } Elf64_Rela;
617
618 #define ELF32_R_SYM(i) ((i)>>8)
619 #define ELF32_R_TYPE(i) ((unsigned char)(i))
620 #define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t))
621
622 #define ELF64_R_SYM(i) ((i)>>32)
623 #define ELF64_R_TYPE(i) ((i)&0xffffffffL)
624 #define ELF64_R_INFO(s,t) (((s)<<32)+(t)&0xffffffffL)
625
626 /* Relocation types (processor specific) - page 1-23, figure 1-22 */
627
628 #define R_386_NONE 0 /**< none */
629 #define R_386_32 1 /**< S + A */
630 #define R_386_PC32 2 /**< S + A - P */
631 #define R_386_GOT32 3 /**< G + A - P */
632 #define R_386_PLT32 4 /**< L + A - P */
633 #define R_386_COPY 5 /**< none */
634 #define R_386_GLOB_DAT 6 /**< S */
635 #define R_386_JMP_SLOT 7 /**< S */
636 #define R_386_RELATIVE 8 /**< B + A */
637 #define R_386_GOTOFF 9 /**< S + A - GOT */
638 #define R_386_GOTPC 10 /**< GOT + A - P */
639 #define R_386_32PLT 11
640 #define R_386_TLS_TPOFF 14 /* Offset in static TLS block */
641 #define R_386_TLS_IE 15 /* Address of GOT entry for static TLS
642 block offset */
643 #define R_386_TLS_GOTIE 16 /* GOT entry for static TLS block
644 offset */
645 #define R_386_TLS_LE 17 /* Offset relative to static TLS
646 block */
647 #define R_386_TLS_GD 18 /* Direct 32 bit for GNU version of
648 general dynamic thread local data */
649 #define R_386_TLS_LDM 19 /* Direct 32 bit for GNU version of
650 local dynamic thread local data
651 in LE code */
652 #define R_386_16 20
653 #define R_386_PC16 21
654 #define R_386_8 22
655 #define R_386_PC8 23
656 #define R_386_TLS_GD_32 24 /* Direct 32 bit for general dynamic
657 thread local data */
658 #define R_386_TLS_GD_PUSH 25 /* Tag for pushl in GD TLS code */
659 #define R_386_TLS_GD_CALL 26 /* Relocation for call to
660 __tls_get_addr() */
661 #define R_386_TLS_GD_POP 27 /* Tag for popl in GD TLS code */
662 #define R_386_TLS_LDM_32 28 /* Direct 32 bit for local dynamic
663 thread local data in LE code */
664 #define R_386_TLS_LDM_PUSH 29 /* Tag for pushl in LDM TLS code */
665 #define R_386_TLS_LDM_CALL 30 /* Relocation for call to
666 __tls_get_addr() in LDM code */
667 #define R_386_TLS_LDM_POP 31 /* Tag for popl in LDM TLS code */
668 #define R_386_TLS_LDO_32 32 /* Offset relative to TLS block */
669 #define R_386_TLS_IE_32 33 /* GOT entry for negated static TLS
670 block offset */
671 #define R_386_TLS_LE_32 34 /* Negated offset relative to static
672 TLS block */
673 #define R_386_TLS_DTPMOD32 35 /* ID of module containing symbol */
674 #define R_386_TLS_DTPOFF32 36 /* Offset in TLS block */
675 #define R_386_TLS_TPOFF32 37 /* Negated offset in static TLS block */
676 /* Keep this the last entry. */
677 #define R_386_NUM 38
678
679 /* ARM specific declarations */
680
681 /* Processor specific flags for the ELF header e_flags field. */
682 #define EF_ARM_RELEXEC 0x01
683 #define EF_ARM_HASENTRY 0x02
684 #define EF_ARM_INTERWORK 0x04
685 #define EF_ARM_APCS_26 0x08
686 #define EF_ARM_APCS_FLOAT 0x10
687 #define EF_ARM_PIC 0x20
688 #define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */
689 #define EF_ARM_NEW_ABI 0x80
690 #define EF_ARM_OLD_ABI 0x100
691
692 /* Other constants defined in the ARM ELF spec. version B-01. */
693 /* NB. These conflict with values defined above. */
694 #define EF_ARM_SYMSARESORTED 0x04
695 #define EF_ARM_DYNSYMSUSESEGIDX 0x08
696 #define EF_ARM_MAPSYMSFIRST 0x10
697 #define EF_ARM_EABIMASK 0XFF000000
698
699 #define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK)
700 #define EF_ARM_EABI_UNKNOWN 0x00000000
701 #define EF_ARM_EABI_VER1 0x01000000
702 #define EF_ARM_EABI_VER2 0x02000000
703
704 /* Additional symbol types for Thumb */
705 #define STT_ARM_TFUNC 0xd
706
707 /* ARM-specific values for sh_flags */
708 #define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */
709 #define SHF_ARM_COMDEF 0x80000000 /* Section may be multiply defined
710 in the input to a link step */
711
712 /* ARM-specific program header flags */
713 #define PF_ARM_SB 0x10000000 /* Segment contains the location
714 addressed by the static base */
715
716 /* ARM relocs. */
717 #define R_ARM_NONE 0 /* No reloc */
718 #define R_ARM_PC24 1 /* PC relative 26 bit branch */
719 #define R_ARM_ABS32 2 /* Direct 32 bit */
720 #define R_ARM_REL32 3 /* PC relative 32 bit */
721 #define R_ARM_PC13 4
722 #define R_ARM_ABS16 5 /* Direct 16 bit */
723 #define R_ARM_ABS12 6 /* Direct 12 bit */
724 #define R_ARM_THM_ABS5 7
725 #define R_ARM_ABS8 8 /* Direct 8 bit */
726 #define R_ARM_SBREL32 9
727 #define R_ARM_THM_PC22 10
728 #define R_ARM_THM_PC8 11
729 #define R_ARM_AMP_VCALL9 12
730 #define R_ARM_SWI24 13
731 #define R_ARM_THM_SWI8 14
732 #define R_ARM_XPC25 15
733 #define R_ARM_THM_XPC22 16
734 #define R_ARM_COPY 20 /* Copy symbol at runtime */
735 #define R_ARM_GLOB_DAT 21 /* Create GOT entry */
736 #define R_ARM_JUMP_SLOT 22 /* Create PLT entry */
737 #define R_ARM_RELATIVE 23 /* Adjust by program base */
738 #define R_ARM_GOTOFF 24 /* 32 bit offset to GOT */
739 #define R_ARM_GOTPC 25 /* 32 bit PC relative offset to GOT */
740 #define R_ARM_GOT32 26 /* 32 bit GOT entry */
741 #define R_ARM_PLT32 27 /* 32 bit PLT address */
742 #define R_ARM_ALU_PCREL_7_0 32
743 #define R_ARM_ALU_PCREL_15_8 33
744 #define R_ARM_ALU_PCREL_23_15 34
745 #define R_ARM_LDR_SBREL_11_0 35
746 #define R_ARM_ALU_SBREL_19_12 36
747 #define R_ARM_ALU_SBREL_27_20 37
748 #define R_ARM_GNU_VTENTRY 100
749 #define R_ARM_GNU_VTINHERIT 101
750 #define R_ARM_THM_PC11 102 /* thumb unconditional branch */
751 #define R_ARM_THM_PC9 103 /* thumb conditional branch */
752 #define R_ARM_RXPC25 249
753 #define R_ARM_RSBREL32 250
754 #define R_ARM_THM_RPC22 251
755 #define R_ARM_RREL32 252
756 #define R_ARM_RABS22 253
757 #define R_ARM_RPC24 254
758 #define R_ARM_RBASE 255
759 /* Keep this the last entry. */
760 #define R_ARM_NUM 256
761
762 /* AARCH64 relocations. */
763
764 #define R_AARCH64_NONE 0 /* No reloc */
765 #define R_AARCH64_RELATIVE 1027
766
767 /* AMD x86-64 relocations. */
768 #define R_X86_64_NONE 0 /* No reloc */
769 #define R_X86_64_64 1 /* Direct 64 bit */
770 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
771 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
772 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
773 #define R_X86_64_COPY 5 /* Copy symbol at runtime */
774 #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
775 #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
776 #define R_X86_64_RELATIVE 8 /* Adjust by program base */
777 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
778 offset to GOT */
779 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
780 #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
781 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
782 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
783 #define R_X86_64_8 14 /* Direct 8 bit sign extended */
784 #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
785 #define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
786 #define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
787 #define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
788 #define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
789 to two GOT entries for GD symbol */
790 #define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
791 to two GOT entries for LD symbol */
792 #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
793 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
794 to GOT entry for IE symbol */
795 #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
796
797 #define R_X86_64_NUM 24
798
799 /* Symbol Table Entry - page 1-17, figure 1-16 */
800
801 #define STN_UNDEF 0
802
803 /** ELF32 symbol table entry */
804 typedef struct {
805 Elf32_Word st_name; /**< name of symbol (idx symstrtab) */
806 Elf32_Addr st_value; /**< value of associated symbol */
807 Elf32_Word st_size; /**< size of associated symbol */
808 unsigned char st_info; /**< type and binding info */
809 unsigned char st_other; /**< undefined */
810 Elf32_Half st_shndx; /**< associated section header */
811 } Elf32_Sym;
812
813 /** ELF64 symbol table entry */
814 typedef struct {
815 Elf64_Word st_name; /**< name of symbol (idx symstrtab) */
816 unsigned char st_info; /**< type and binding info */
817 unsigned char st_other; /**< undefined */
818 Elf64_Half st_shndx; /**< associated section header */
819 Elf64_Addr st_value; /**< value of associated symbol */
820 Elf64_Xword st_size; /**< size of associated symbol */
821 } Elf64_Sym;
822
823 #define ELF32_ST_BIND(i) ((i)>>4)
824 #define ELF32_ST_TYPE(i) ((i)&0xf)
825 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
826
827 #define ELF64_ST_BIND(i) ((i)>>4)
828 #define ELF64_ST_TYPE(i) ((i)&0xf)
829 #define ELF64_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
830
831 /* Symbol Binding - page 1-18, figure 1-17 */
832
833 #define STB_LOCAL 0 /**< not visible outside object file */
834 #define STB_GLOBAL 1 /**< visible to all objects beeing combined */
835 #define STB_WEAK 2 /**< resemble global symbols */
836 #define STB_LOOS 10 /**< os specific */
837 #define STB_HIOS 12 /**< os specific */
838 #define STB_LOPROC 13 /**< proc specific */
839 #define STB_HIPROC 15 /**< proc specific */
840
841 /* Symbol Types - page 1-19, figure 1-18 */
842
843 #define STT_NOTYPE 0 /**< symbol's type not specified */
844 #define STT_OBJECT 1 /**< associated with a data object */
845 #define STT_FUNC 2 /**< associated with a function or other code */
846 #define STT_SECTION 3 /**< associated with a section */
847 #define STT_FILE 4 /**< source file name associated with object */
848 #define STT_LOOS 10 /**< os specific */
849 #define STT_HIOS 12 /**< os specific */
850 #define STT_LOPROC 13 /**< proc specific */
851 #define STT_HIPROC 15 /**< proc specific */
852
853 enum Elf_ATs
854 {
855 AT_NULL = 0,
856 AT_IGNORE = 1,
857 AT_EXECFD = 2,
858 AT_PHDR = 3,
859 AT_PHENT = 4,
860 AT_PHNUM = 5,
861 AT_PAGESZ = 6,
862 AT_BASE = 7,
863 AT_FLAGS = 8,
864 AT_ENTRY = 9,
865 AT_NOTELF = 10,
866 AT_UID = 11,
867 AT_EUID = 12,
868 AT_GID = 13,
869 AT_EGID = 14,
870
871 AT_L4_AUX = 0xf0,
872 AT_L4_ENV = 0xf1,
873 };
874
875 typedef struct Elf32_Auxv
876 {
877 Elf32_Word atype;
878 Elf32_Word avalue;
879 } Elf32_Auxv;
880
881 typedef struct Elf64_Auxv
882 {
883 Elf64_Word atype;
884 Elf64_Word avalue;
885 } Elf64_Auxv;
886
887 /* Some helpers */
888 static inline int l4util_elf_check_magic(ElfW(Ehdr) const *hdr);
889 static inline int l4util_elf_check_arch(ElfW(Ehdr) const *hdr);
890 static inline ElfW(Phdr) *l4util_elf_phdr(ElfW(Ehdr) const *hdr);
891
892
893 /* Implemeantions */
894 static inline
l4util_elf_check_magic(ElfW (Ehdr)const * hdr)895 int l4util_elf_check_magic(ElfW(Ehdr) const *hdr)
896 {
897 return hdr->e_ident[EI_MAG0] == ELFMAG0
898 && hdr->e_ident[EI_MAG1] == ELFMAG1
899 && hdr->e_ident[EI_MAG2] == ELFMAG2
900 && hdr->e_ident[EI_MAG3] == ELFMAG3;
901 }
902
903 static inline
l4util_elf_check_arch(ElfW (Ehdr)const * hdr)904 int l4util_elf_check_arch(ElfW(Ehdr) const *hdr)
905 {
906 return hdr->e_ident[EI_CLASS] == L4_ARCH_EI_CLASS
907 && hdr->e_ident[EI_DATA] == L4_ARCH_EI_DATA
908 && hdr->e_machine == L4_ARCH_E_MACHINE;
909 }
910
911 static inline
ElfW(Phdr)912 ElfW(Phdr) *l4util_elf_phdr(ElfW(Ehdr) const *hdr)
913 {
914 return (ElfW(Phdr) *)((char *)hdr + hdr->e_phoff);
915 }
916 /*@}*/
917
918 #endif /* _L4_EXEC_ELF_H */
919