aboutsummaryrefslogtreecommitdiff
path: root/type_elf.h
blob: 2a522b19ff3023b9d2c85e4a38a9559544b069b0 (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
282
283
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_ZUCCHINI_TYPE_ELF_H_
#define COMPONENTS_ZUCCHINI_TYPE_ELF_H_

#include <stdint.h>

namespace zucchini {

// Structures and constants taken from linux/elf.h and following identical
// layout. This is used for parsing of Executable and Linkable Format (ELF).
namespace elf {
// Supported by MSVC, g++, and clang++. Ensures no gaps in packing.
#pragma pack(push, 1)

// This header defines various types from the ELF file spec, but no code
// related to using them.

typedef uint32_t Elf32_Addr;  // Unsigned program address.
typedef uint16_t Elf32_Half;  // Unsigned medium integer.
typedef uint32_t Elf32_Off;   // Unsigned file offset.
typedef int32_t Elf32_Sword;  // Signed large integer.
typedef uint32_t Elf32_Word;  // Unsigned large integer.

typedef uint64_t Elf64_Addr;   // Unsigned program address.
typedef uint16_t Elf64_Half;   // Unsigned medium integer.
typedef uint64_t Elf64_Off;    // Unsigned file offset.
typedef int32_t Elf64_Sword;   // Signed large integer.
typedef uint32_t Elf64_Word;   // Unsigned large integer.
typedef int64_t Elf64_Sxword;  // Signed extra large integer.
typedef uint64_t Elf64_Xword;  // Unsigned extra large integer.

// The header at the top of the file.
struct Elf32_Ehdr {
  unsigned char e_ident[16];
  Elf32_Half e_type;
  Elf32_Half e_machine;
  Elf32_Word e_version;
  Elf32_Addr e_entry;
  Elf32_Off e_phoff;
  Elf32_Off e_shoff;
  Elf32_Word e_flags;
  Elf32_Half e_ehsize;
  Elf32_Half e_phentsize;
  Elf32_Half e_phnum;
  Elf32_Half e_shentsize;
  Elf32_Half e_shnum;
  Elf32_Half e_shstrndx;
};

struct Elf64_Ehdr {
  unsigned char e_ident[16];
  Elf64_Half e_type;
  Elf64_Half e_machine;
  Elf64_Word e_version;
  Elf64_Addr e_entry;
  Elf64_Off e_phoff;
  Elf64_Off e_shoff;
  Elf64_Word e_flags;
  Elf64_Half e_ehsize;
  Elf64_Half e_phentsize;
  Elf64_Half e_phnum;
  Elf64_Half e_shentsize;
  Elf64_Half e_shnum;
  Elf64_Half e_shstrndx;
};

// Identification Indexes in header->e_ident.
enum IdentificationIndex {
  EI_MAG0 = 0,        // File identification.
  EI_MAG1 = 1,        // File identification.
  EI_MAG2 = 2,        // File identification.
  EI_MAG3 = 3,        // File identification.
  EI_CLASS = 4,       // File class.
  EI_DATA = 5,        // Data encoding.
  EI_VERSION = 6,     // File version.
  EI_OSABI = 7,       // Operating system/ABI identification.
  EI_ABIVERSION = 8,  // ABI version.
  EI_PAD = 9,         // Start of padding bytes.
  EI_NIDENT = 16      // Size of e_ident[].
};

// Values for header->e_ident[EI_CLASS].
enum FileClass {
  ELFCLASSNONE = 0,  // Invalid class.
  ELFCLASS32 = 1,    // 32-bit objects.
  ELFCLASS64 = 2     // 64-bit objects.
};

// Values for header->e_type.
enum FileType {
  ET_NONE = 0,         // No file type
  ET_REL = 1,          // Relocatable file
  ET_EXEC = 2,         // Executable file
  ET_DYN = 3,          // Shared object file
  ET_CORE = 4,         // Core file
  ET_LOPROC = 0xFF00,  // Processor-specific
  ET_HIPROC = 0xFFFF   // Processor-specific
};

// Values for header->e_machine.
enum MachineArchitecture {
  EM_NONE = 0,       // No machine.
  EM_386 = 3,        // Intel Architecture.
  EM_ARM = 40,       // ARM Architecture.
  EM_X86_64 = 62,    // Intel x86-64 Architecture.
  EM_AARCH64 = 183,  // ARM Architecture, 64-bit.
  // Other values skipped.
};

// A section header in the section header table.
struct Elf32_Shdr {
  Elf32_Word sh_name;
  Elf32_Word sh_type;
  Elf32_Word sh_flags;
  Elf32_Addr sh_addr;
  Elf32_Off sh_offset;
  Elf32_Word sh_size;
  Elf32_Word sh_link;
  Elf32_Word sh_info;
  Elf32_Word sh_addralign;
  Elf32_Word sh_entsize;
};

struct Elf64_Shdr {
  Elf64_Word sh_name;
  Elf64_Word sh_type;
  Elf64_Xword sh_flags;
  Elf64_Addr sh_addr;
  Elf64_Off sh_offset;
  Elf64_Xword sh_size;
  Elf64_Word sh_link;
  Elf64_Word sh_info;
  Elf64_Xword sh_addralign;
  Elf64_Xword sh_entsize;
};

// Values for the section type field in a section header.
enum sh_type_values {
  SHT_NULL = 0,
  SHT_PROGBITS = 1,
  SHT_SYMTAB = 2,
  SHT_STRTAB = 3,
  SHT_RELA = 4,
  SHT_HASH = 5,
  SHT_DYNAMIC = 6,
  SHT_NOTE = 7,
  SHT_NOBITS = 8,
  SHT_REL = 9,
  SHT_SHLIB = 10,
  SHT_DYNSYM = 11,
  SHT_INIT_ARRAY = 14,
  SHT_FINI_ARRAY = 15,
  SHT_LOPROC = 0x70000000,
  SHT_HIPROC = 0x7FFFFFFF,
  SHT_LOUSER = 0x80000000,
  SHT_HIUSER = 0xFFFFFFFF
};

enum sh_flag_masks {
  SHF_WRITE = 1 << 0,
  SHF_ALLOC = 1 << 1,
  SHF_EXECINSTR = 1 << 2,
  // 1 << 3 is reserved.
  SHF_MERGE = 1 << 4,
  SHF_STRINGS = 1 << 5,
  SHF_INFO_LINK = 1 << 6,
  SHF_LINK_ORDER = 1 << 7,
  SHF_OS_NONCONFORMING = 1 << 8,
  SHF_GROUP = 1 << 9,
  SHF_TLS = 1 << 10,
  SHF_COMPRESSED = 1 << 11,
};

struct Elf32_Phdr {
  Elf32_Word p_type;
  Elf32_Off p_offset;
  Elf32_Addr p_vaddr;
  Elf32_Addr p_paddr;
  Elf32_Word p_filesz;
  Elf32_Word p_memsz;
  Elf32_Word p_flags;
  Elf32_Word p_align;
};

struct Elf64_Phdr {
  Elf64_Word p_type;
  Elf64_Word p_flags;
  Elf64_Off p_offset;
  Elf64_Addr p_vaddr;
  Elf64_Addr p_paddr;
  Elf64_Xword p_filesz;
  Elf64_Xword p_memsz;
  Elf64_Xword p_align;
};

// Values for the segment type field in a program segment header.
enum ph_type_values {
  PT_NULL = 0,
  PT_LOAD = 1,
  PT_DYNAMIC = 2,
  PT_INTERP = 3,
  PT_NOTE = 4,
  PT_SHLIB = 5,
  PT_PHDR = 6,
  PT_LOPROC = 0x70000000,
  PT_HIPROC = 0x7FFFFFFF
};

struct Elf32_Rel {
  Elf32_Addr r_offset;
  Elf32_Word r_info;
};

struct Elf64_Rel {
  Elf64_Addr r_offset;
  Elf64_Xword r_info;
};

struct Elf32_Rela {
  Elf32_Addr r_offset;
  Elf32_Word r_info;
  Elf32_Sword r_addend;
};

struct Elf64_Rela {
  Elf64_Addr r_offset;
  Elf64_Xword r_info;
  Elf64_Sxword r_addend;
};

enum elf32_rel_386_type_values {
  R_386_NONE = 0,
  R_386_32 = 1,
  R_386_PC32 = 2,
  R_386_GOT32 = 3,
  R_386_PLT32 = 4,
  R_386_COPY = 5,
  R_386_GLOB_DAT = 6,
  R_386_JMP_SLOT = 7,
  R_386_RELATIVE = 8,
  R_386_GOTOFF = 9,
  R_386_GOTPC = 10,
  R_386_TLS_TPOFF = 14,
};

enum elf32_rel_x86_64_type_values {
  R_X86_64_NONE = 0,
  R_X86_64_64 = 1,
  R_X86_64_PC32 = 2,
  R_X86_64_GOT32 = 3,
  R_X86_64_PLT32 = 4,
  R_X86_64_COPY = 5,
  R_X86_64_GLOB_DAT = 6,
  R_X86_64_JUMP_SLOT = 7,
  R_X86_64_RELATIVE = 8,
  R_X86_64_GOTPCREL = 9,
  R_X86_64_32 = 10,
  R_X86_64_32S = 11,
  R_X86_64_16 = 12,
  R_X86_64_PC16 = 13,
  R_X86_64_8 = 14,
  R_X86_64_PC8 = 15,
};

enum elf32_rel_arm_type_values {
  R_ARM_RELATIVE = 23,
};

enum elf64_rel_aarch64_type_values {
  R_AARCH64_GLOB_DAT = 0x401,
  R_AARCH64_JUMP_SLOT = 0x402,
  R_AARCH64_RELATIVE = 0x403,
};

#pragma pack(pop)

}  // namespace elf
}  // namespace zucchini

#endif  // COMPONENTS_ZUCCHINI_TYPE_ELF_H_