1/* Copyright (C) 2007-2010 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10** GNU General Public License for more details.
11*/
12
13/*
14 * Contains declarations of types, constants and structures
15 * describing ELF file format.
16 */
17
18#ifndef ELFF_ELH_H_
19#define ELFF_ELH_H_
20
21#include <stdint.h>
22#include "elff-common.h"
23
24//=============================================================================
25// ELF file definitions
26//=============================================================================
27
28/*
29 * ELF format documentation uses Elf##_Xxx notation for data types, where
30 * ## stands for CPU architecture (32, or 64 bit), and Xxx stands for a
31 * specific type. For the sake of compliance, we will follow doc's notation
32 * when defining types used in ELF file descriptors. However, for the sake of
33 * code simplicity, we will drop CPU architecture index from the types that
34 * have equal sizes on both, 32 and 64 bit architectures.
35 */
36
37/*
38 * Architecture independent types.
39 */
40
41typedef uint8_t   Elf_Byte;
42typedef int8_t    Elf_Sbyte;
43
44typedef uint16_t  Elf_Half;
45typedef int16_t   Elf_Shalf;
46
47typedef uint32_t  Elf_Word;
48typedef int32_t   Elf_Sword;
49
50typedef uint64_t  Elf_Xword;
51typedef int64_t   Elf_Sxword;
52
53/*
54 * Architecture dependent types.
55 */
56
57/* 32-bit ELF address. */
58typedef uint32_t  Elf32_Addr;
59/* 32-bit ELF offset. */
60typedef uint32_t  Elf32_Off;
61
62/* 64-bit ELF address. */
63typedef uint64_t  Elf64_Addr;
64/* 64-bit ELF offset. */
65typedef uint64_t  Elf64_Off;
66
67//=============================================================================
68// ELF file header
69//=============================================================================
70
71/* Byte size of the fixed portion of ELF header. */
72#define EI_NIDENT	16
73
74/* Common (architecture independent portion of) ELF file header,
75 * that starts at offset 0 in ELF file.
76 */
77typedef struct Elf_CommonHdr {
78  union {
79    struct {
80      /* ei_mag0 - ei_mag3 contain ELF header signature. See ELFMAGx bellow. */
81      Elf_Byte  ei_mag0;
82      Elf_Byte  ei_mag1;
83      Elf_Byte  ei_mag2;
84      Elf_Byte  ei_mag3;
85
86      /* File class (32, or 64 bits). See ELFCLASSxxx bellow. */
87      Elf_Byte  ei_class;
88
89      /* Data encoding (endianness). See ELFDATAxxx bellow. */
90      Elf_Byte  ei_data;
91
92      /* ELF header version number. */
93      Elf_Byte  ei_version;
94    } ei_info;
95    unsigned char e_ident[EI_NIDENT];
96  };
97
98  /* File type (executable, shared object, etc.) */
99  Elf_Half      e_type;
100
101  /* Processor type. */
102  Elf_Half      e_machine;
103
104  /* File version. */
105  Elf_Word      e_version;
106} Elf_CommonHdr;
107
108
109/* ELF header signature. */
110#define ELFMAG0		0x7f
111#define ELFMAG1		'E'
112#define ELFMAG2		'L'
113#define ELFMAG3		'F'
114#define ELFMAG		"\177ELF"
115#define SELFMAG		4
116
117/*
118 * Possible ei_class values.
119 */
120
121/* Invalid. */
122#define ELFCLASSNONE  0
123/* It's 32-bit ELF file. */
124#define ELFCLASS32    1
125/* It's 64-bit ELF file. */
126#define ELFCLASS64    2
127
128/*
129 * Possible ei_data values.
130 */
131
132/* Invalid. */
133#define ELFDATANONE   0
134/* ELF data is formatted in little-endian. */
135#define ELFDATA2LSB   1
136/* ELF data is formatted in big-endian. */
137#define ELFDATA2MSB   2
138
139/* Tempated (architecture dependent) ELF file header.
140 * Template param:
141 *  Elf_Addr - Actual type for address encoding (Elf32_Addr, or Elf64_Addr).
142 *  Elf_Off - Actual type for offset encoding (Elf32_Off, or Elf64_Off).
143 */
144template <typename Elf_Addr, typename Elf_Off>
145struct Elf_FHdr {
146  /* Common header. */
147  Elf_CommonHdr common;
148
149  /* Module entry point. */
150  Elf_Addr      e_entry;
151
152  /* Programm header table offset (in bytes) from the beginning of the file.
153   * Zero if there is no programm header in this file.
154   */
155  Elf_Off       e_phoff;
156
157  /* Section header table offset (in bytes) from the beginning of the file.
158   * Zero if there is no section header in this file.
159   */
160  Elf_Off       e_shoff;
161
162  /* Processor-specific flags. */
163  Elf_Word      e_flags;
164
165  /* This header size in bytes. */
166  Elf_Half      e_ehsize;
167
168  /* Byte size of an entry in programm header table. All entries
169   * in the table are the same size.
170   */
171  Elf_Half      e_phentsize;
172
173  /* Number of entries in programm header table. */
174  Elf_Half      e_phnum;
175
176  /* Byte size of an entry in section header table. All entries
177   * in the table are the same size.
178   */
179  Elf_Half      e_shentsize;
180
181  /* Number of entries in section header table. */
182  Elf_Half      e_shnum;
183
184  /* Zero-based index of an entry for name string table section in the section
185   * header table. If no such section exists in the file this field contains
186   * SHN_UNDEF value.
187   */
188  Elf_Half      e_shstrndx;
189};
190/* 32-bit ELF header. */
191typedef Elf_FHdr<Elf32_Addr, Elf32_Off> Elf32_FHdr;
192/* 64-bit ELF header. */
193typedef Elf_FHdr<Elf64_Addr, Elf64_Off> Elf64_FHdr;
194
195//=============================================================================
196// ELF section header
197//=============================================================================
198
199/* Templated (architecture dependent) section header for ELF file.
200 * Template param:
201 *  Elf_Addr - Actual type for address encoding (Elf32_Addr, or Elf64_Addr).
202 *  Elf_Off - Actual type for offset encoding (Elf32_Off, or Elf64_Off).
203 */
204template <typename Elf_Addr, typename Elf_Off>
205struct Elf_SHdr {
206  /* Index (byte offset) of section name in the name string table section. */
207  Elf_Word    sh_name;
208
209  /* Section type and semantics. */
210  Elf_Word    sh_type;
211
212  /* Section flags and attributes. */
213  Elf_Word    sh_flags;
214
215  /* Section address in the memory image of the process. */
216  Elf_Addr    sh_addr;
217
218  /* Byte offset from the beginning of the ELF file to the first
219   * byte in the section.
220   */
221  Elf_Off     sh_offset;
222
223  /* Section size in bytes. */
224  Elf_Word    sh_size;
225
226  /* Section header table index link. Depends on section type. */
227  Elf_Word    sh_link;
228
229  /* Extra section information, depending on the section type. */
230  Elf_Word    sh_info;
231
232  /* Address alignment constrains. 0 and 1 means that section has no
233   * alignment constrains.
234   */
235  Elf_Word    sh_addralign;
236
237  /* Entry size for sections that hold some kind of a table. */
238  Elf_Word    sh_entsize;
239};
240/* 32-bit section header. */
241typedef Elf_SHdr<Elf32_Addr, Elf32_Off> Elf32_SHdr;
242/* 64-bit section header. */
243typedef Elf_SHdr<Elf64_Addr, Elf64_Off> Elf64_SHdr;
244
245/*
246 * Special section indices
247 */
248#define SHN_UNDEF       0
249#define SHN_LORESERVE   0xff00
250#define SHN_LOPROC      0xff00
251#define SHN_HIPROC      0xff1f
252#define SHN_LOOS        0xff20
253#define SHN_HIOS        0xff3f
254#define SHN_ABS         0xfff1
255#define SHN_COMMON      0xfff2
256#define SHN_XINDEX      0xffff
257#define SHN_HIRESERVE   0xffff
258
259/*
260 * Values for sh_type
261 */
262#define SHT_NULL            0
263#define SHT_PROGBITS        1
264#define SHT_SYMTAB          2
265#define SHT_STRTAB          3
266#define SHT_RELA            4
267#define SHT_HASH            5
268#define SHT_DYNAMIC         6
269#define SHT_NOTE            7
270#define SHT_NOBITS          8
271#define SHT_REL             9
272#define SHT_SHLIB           10
273#define SHT_DYNSYM          11
274#define SHT_INIT_ARRAY      14
275#define SHT_FINI_ARRAY      15
276#define SHT_PREINIT_ARRAY   16
277#define SHT_GROUP           17
278#define SHT_SYMTAB_SHNDX    18
279#define SHT_NUM             19
280
281#endif  // ELFF_ELH_H_
282