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