aboutsummaryrefslogtreecommitdiffstats
path: root/elff/elff_elf.h
blob: ea192330996a71b4d99c73f34d76a5bbb6f38d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Copyright (C) 2007-2010 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/

/*
 * Contains declarations of types, constants and structures
 * describing ELF file format.
 */

#ifndef ELFF_ELH_H_
#define ELFF_ELH_H_

#include <stdint.h>
#include "elff-common.h"

//=============================================================================
// ELF file definitions
//=============================================================================

/*
 * ELF format documentation uses Elf##_Xxx notation for data types, where
 * ## stands for CPU architecture (32, or 64 bit), and Xxx stands for a
 * specific type. For the sake of compliance, we will follow doc's notation
 * when defining types used in ELF file descriptors. However, for the sake of
 * code simplicity, we will drop CPU architecture index from the types that
 * have equal sizes on both, 32 and 64 bit architectures.
 */

/*
 * Architecture independent types.
 */

typedef uint8_t   Elf_Byte;
typedef int8_t    Elf_Sbyte;

typedef uint16_t  Elf_Half;
typedef int16_t   Elf_Shalf;

typedef uint32_t  Elf_Word;
typedef int32_t   Elf_Sword;

typedef uint64_t  Elf_Xword;
typedef int64_t   Elf_Sxword;

/*
 * Architecture dependent types.
 */

/* 32-bit ELF address. */
typedef uint32_t  Elf32_Addr;
/* 32-bit ELF offset. */
typedef uint32_t  Elf32_Off;

/* 64-bit ELF address. */
typedef uint64_t  Elf64_Addr;
/* 64-bit ELF offset. */
typedef uint64_t  Elf64_Off;

//=============================================================================
// ELF file header
//=============================================================================

/* Byte size of the fixed portion of ELF header. */
#define EI_NIDENT	16

/* Common (architecture independent portion of) ELF file header,
 * that starts at offset 0 in ELF file.
 */
typedef struct Elf_CommonHdr {
  union {
    struct {
      /* ei_mag0 - ei_mag3 contain ELF header signature. See ELFMAGx bellow. */
      Elf_Byte  ei_mag0;
      Elf_Byte  ei_mag1;
      Elf_Byte  ei_mag2;
      Elf_Byte  ei_mag3;

      /* File class (32, or 64 bits). See ELFCLASSxxx bellow. */
      Elf_Byte  ei_class;

      /* Data encoding (endianness). See ELFDATAxxx bellow. */
      Elf_Byte  ei_data;

      /* ELF header version number. */
      Elf_Byte  ei_version;
    } ei_info;
    unsigned char e_ident[EI_NIDENT];
  };

  /* File type (executable, shared object, etc.) */
  Elf_Half      e_type;

  /* Processor type. */
  Elf_Half      e_machine;

  /* File version. */
  Elf_Word      e_version;
} Elf_CommonHdr;


/* ELF header signature. */
#define ELFMAG0		0x7f
#define ELFMAG1		'E'
#define ELFMAG2		'L'
#define ELFMAG3		'F'
#define ELFMAG		"\177ELF"
#define SELFMAG		4

/*
 * Possible ei_class values.
 */

/* Invalid. */
#define ELFCLASSNONE  0
/* It's 32-bit ELF file. */
#define ELFCLASS32    1
/* It's 64-bit ELF file. */
#define ELFCLASS64    2

/*
 * Possible ei_data values.
 */

/* Invalid. */
#define ELFDATANONE   0
/* ELF data is formatted in little-endian. */
#define ELFDATA2LSB   1
/* ELF data is formatted in big-endian. */
#define ELFDATA2MSB   2

/* Tempated (architecture dependent) ELF file header.
 * Template param:
 *  Elf_Addr - Actual type for address encoding (Elf32_Addr, or Elf64_Addr).
 *  Elf_Off - Actual type for offset encoding (Elf32_Off, or Elf64_Off).
 */
template <typename Elf_Addr, typename Elf_Off>
struct Elf_FHdr {
  /* Common header. */
  Elf_CommonHdr common;

  /* Module entry point. */
  Elf_Addr      e_entry;

  /* Programm header table offset (in bytes) from the beginning of the file.
   * Zero if there is no programm header in this file.
   */
  Elf_Off       e_phoff;

  /* Section header table offset (in bytes) from the beginning of the file.
   * Zero if there is no section header in this file.
   */
  Elf_Off       e_shoff;

  /* Processor-specific flags. */
  Elf_Word      e_flags;

  /* This header size in bytes. */
  Elf_Half      e_ehsize;

  /* Byte size of an entry in programm header table. All entries
   * in the table are the same size.
   */
  Elf_Half      e_phentsize;

  /* Number of entries in programm header table. */
  Elf_Half      e_phnum;

  /* Byte size of an entry in section header table. All entries
   * in the table are the same size.
   */
  Elf_Half      e_shentsize;

  /* Number of entries in section header table. */
  Elf_Half      e_shnum;

  /* Zero-based index of an entry for name string table section in the section
   * header table. If no such section exists in the file this field contains
   * SHN_UNDEF value.
   */
  Elf_Half      e_shstrndx;
};
/* 32-bit ELF header. */
typedef Elf_FHdr<Elf32_Addr, Elf32_Off> Elf32_FHdr;
/* 64-bit ELF header. */
typedef Elf_FHdr<Elf64_Addr, Elf64_Off> Elf64_FHdr;

//=============================================================================
// ELF section header
//=============================================================================

/* Templated (architecture dependent) section header for ELF file.
 * Template param:
 *  Elf_Addr - Actual type for address encoding (Elf32_Addr, or Elf64_Addr).
 *  Elf_Off - Actual type for offset encoding (Elf32_Off, or Elf64_Off).
 */
template <typename Elf_Addr, typename Elf_Off>
struct Elf_SHdr {
  /* Index (byte offset) of section name in the name string table section. */
  Elf_Word    sh_name;

  /* Section type and semantics. */
  Elf_Word    sh_type;

  /* Section flags and attributes. */
  Elf_Word    sh_flags;

  /* Section address in the memory image of the process. */
  Elf_Addr    sh_addr;

  /* Byte offset from the beginning of the ELF file to the first
   * byte in the section.
   */
  Elf_Off     sh_offset;

  /* Section size in bytes. */
  Elf_Word    sh_size;

  /* Section header table index link. Depends on section type. */
  Elf_Word    sh_link;

  /* Extra section information, depending on the section type. */
  Elf_Word    sh_info;

  /* Address alignment constrains. 0 and 1 means that section has no
   * alignment constrains.
   */
  Elf_Word    sh_addralign;

  /* Entry size for sections that hold some kind of a table. */
  Elf_Word    sh_entsize;
};
/* 32-bit section header. */
typedef Elf_SHdr<Elf32_Addr, Elf32_Off> Elf32_SHdr;
/* 64-bit section header. */
typedef Elf_SHdr<Elf64_Addr, Elf64_Off> Elf64_SHdr;

/*
 * Special section indices
 */
#define SHN_UNDEF       0
#define SHN_LORESERVE   0xff00
#define SHN_LOPROC      0xff00
#define SHN_HIPROC      0xff1f
#define SHN_LOOS        0xff20
#define SHN_HIOS        0xff3f
#define SHN_ABS         0xfff1
#define SHN_COMMON      0xfff2
#define SHN_XINDEX      0xffff
#define SHN_HIRESERVE   0xffff

/*
 * Values for sh_type
 */
#define SHT_NULL            0
#define SHT_PROGBITS        1
#define SHT_SYMTAB          2
#define SHT_STRTAB          3
#define SHT_RELA            4
#define SHT_HASH            5
#define SHT_DYNAMIC         6
#define SHT_NOTE            7
#define SHT_NOBITS          8
#define SHT_REL             9
#define SHT_SHLIB           10
#define SHT_DYNSYM          11
#define SHT_INIT_ARRAY      14
#define SHT_FINI_ARRAY      15
#define SHT_PREINIT_ARRAY   16
#define SHT_GROUP           17
#define SHT_SYMTAB_SHNDX    18
#define SHT_NUM             19

#endif  // ELFF_ELH_H_