-
Notifications
You must be signed in to change notification settings - Fork 1
/
elf.h
356 lines (325 loc) · 12.2 KB
/
elf.h
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifndef __ELF_H
#define __ELF_H
#include <stdint.h>
#define ELFCLASSNONE 0
#define ELFCLASS32 1
#define ELFCLASS64 2
#define ELFDATANONE 0
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
#define ELFOSABI_NONE 0 /* Unix System V */
#define ELFOSABI_SYSV 0 /* Alias. */
#define ELFOSABI_GNU 3
#define ELFOSABI_LINUX ELFOSABI_GNU
#define ELFOSABI_HURD 4 /* GNU/Hurd */
#define ELFOSABI_SOLARIS 6
#define ELFOSABI_FREEBSD 9 /* FreeBSD. */
#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */
#define ELFOSABI_ARM 97 /* ARM */
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
#define EM_386 3 /* Intel 80386 */
#define EM_MIPS 8 /* */
#define EM_PPC 20 /* PowerPC */
#define EM_PPC64 21 /* 64-bit PowerPC */
#define EM_ARM 40 /* ARM */
#define EM_X86_64 62 /* AMD x86-64 */
#define EM_AARCH64 183 /* ARM AARCH64 */
#define EM_AVR32 185 /* Amtel 32-bit microprocessor */
#define EM_STM8 186 /* STMicroelectronics STM8 */
#define EM_MCHP_PIC 204 /* Microchip 8-bit PIC(r) */
#define EM_AMDGPU 224 /* AMD GPU */
#define EM_RISCV 243
#define EM_BPF 247 /* Linux BPF -- in-kernel virtual machine */
#define EM_NUM 253
#define ET_NONE 0 //!< No file type
#define ET_REL 1 //!< Relocatable file
#define ET_EXEC 2 //!< Executable file
#define ET_DYN 3 //!< Shared object file
#define ET_CORE 4 //!< Core file
#define ET_LOPROC 0xff00 //!< Processor-specific
#define ET_HIPROC 0xffff //!< Processor-specific
typedef uint32_t Elf32_Word;
typedef int32_t Elf32_Sword;
typedef uint32_t Elf32_Addr;
typedef uint32_t Elf32_Off; //!< смещение
typedef uint16_t Elf32_Half;
typedef struct _Elf32_Ehdr Elf32_Ehdr;
typedef struct _Elf32_Shdr Elf32_Shdr;
#define EI_NIDENT 16
struct _Elf32_Ehdr {
unsigned char e_ident[EI_NIDENT];
/* struct {
uint32_t magic; // 0x7f 0x45 0x4c 0x46
uint8_t class; // 1- 32 bit, 2- 64 bit
uint8_t data; // 1- little endian, 2- big endian
uint8_t version;// 1- current
uint8_t os_abi;
uint8_t abi_version;
uint8_t pad[7];
} e_ident; */
Elf32_Half e_type; //!< 1 - перемещаемый, 2 - исполняемый, 3 - разделяемый, 4 - ядро,
Elf32_Half e_machine; //!< 40 - ARM, 62 - x86-64
Elf32_Word e_version; //!< 1 - current
Elf32_Addr e_entry; //!< точка входа
Elf32_Off e_phoff; //!< смещение таблицы заголовков программы
Elf32_Off e_shoff; //!< смещение таблицы заголовков секций
Elf32_Word e_flags;
Elf32_Half e_ehsize; //!< размер заголовка 56 или 64
Elf32_Half e_phentsize; //!< размер одного заголовка программы 32 или 56
Elf32_Half e_phnum; //!< число заголовков программы
Elf32_Half e_shentsize; //!< размер одного заголовка секции 40 или 64
Elf32_Half e_shnum; //!< число заголовков секций
Elf32_Half e_shstrndx; //!< индекс записи в талице заголовков, названия секций .shstrtab или 0.
};
/*! Таблица заголовков разделов .shsymtab */
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;
};
/*! Таблица символов .symtab */
typedef struct _Elf32_Sym Elf32_Sym;
struct _Elf32_Sym {
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
};
/*! Таблица перемещений .rel */
typedef struct _Elf32_Rel Elf32_Rel;
struct _Elf32_Rel {
Elf32_Addr r_offset;
Elf32_Word r_info;
};
/*! Таблица перемещений .rela
Elf32_Rela entries contain an explicit addend. Entries of type
Elf32_Rel store an implicit addend in the location to be modified
*/
typedef struct _Elf32_Rela Elf32_Rela;
struct _Elf32_Rela {
Elf32_Addr r_offset;
Elf32_Word r_info;
Elf32_Sword r_addend;//!< constant addend
};
/*! Таблица программ */
typedef struct _Elf32_Phdr Elf32_Phdr;
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;
};
/*! Таблица динамических параметров */
typedef struct _Elf32_Dyn Elf32_Dyn;
struct _Elf32_Dyn {
Elf32_Sword d_tag;
union {
Elf32_Word d_val;
Elf32_Addr d_ptr;
} d_un;
};
typedef struct _Elf32_Chdr Elf32_Chdr;
struct _Elf32_Chdr {
Elf32_Word ch_type; /* Compression format. */
Elf32_Word ch_size; /* Uncompressed data size. */
Elf32_Word ch_addralign; /* Uncompressed data alignment. */
};
#define ELFCOMPRESS_ZLIB 1 /* ZLIB/DEFLATE algorithm. */
#define ELF32_ST_BIND(i) ((i)>>4)
#define STB_LOCAL 0
#define STB_GLOBAL 1
#define STB_WEAK 2
#define STB_LOPROC 13
#define STB_HIPROC 15
#define ELF32_ST_TYPE(i) ((i)&0xF)
#define STT_NOTYPE 0
#define STT_OBJECT 1
#define STT_FUNC 2
#define STT_SECTION 3
#define STT_FILE 4
#define STT_COMMON 5
#define STT_LOPROC 13
#define STT_HIPROC 15
/* Additional symbol types for Thumb. */
#define STT_ARM_TFUNC STT_LOPROC /* A Thumb function. */
#define STT_ARM_16BIT STT_HIPROC /* A Thumb label. */
/* ARM-specific program header flags */
#define PF_ARM_SB 0x10000000 /* Segment contains the location addressed by the static base. */
#define PF_ARM_PI 0x20000000 /* Position-independent segment. */
#define PF_ARM_ABS 0x40000000 /* Absolute segment. */
/*
Обозначения флагов:
W (запись), A (назнач), X (исполняемый), M (слияние), S (строки),
I (инфо), L (порядок ссылок), O (требуется дополнительная работа ОС),
G (группа), T (TLS), C (сжат), x (неизвестно), o (специфич. для ОС),
E (исключён), y (чистый код), p (processor specific)
*/
#define SHF_WRITE 0x01 /* Writable */
#define SHF_ALLOC 0x02 /* Occupies memory during execution */
#define SHF_EXECINSTR 0x04 /* Executable */
#define SHF_MERGE 0x10 /* Might be merged */
#define SHF_STRINGS 0x20 /* Contains nul-terminated strings */
#define SHF_INFO_LINK 0x40 /* 'sh_info' contains SHT index */
#define SHF_LINK_ORDER 0x80 /* Preserve order after combining */
#define SHF_OS_NONCONFORMING 0x100 /* Non-standard OS specific handling required */
#define SHF_GROUP 0x200 /* Section is member of a group. */
#define SHF_TLS 0x400 /* Section hold thread-local data. */
#define SHF_COMPRESSED 0x800 /* Section with compressed data. */
#define SHF_MASKOS 0x0FF00000 /* OS-specific. */
#define SHF_MASKPROC 0xF0000000 /* Processor-specific */
#define SHF_ORDERED 0x40000000
#define SHF_EXCLUDE 0x80000000
#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xF))
#define ELF32_R_SYM(i) ((i)>>8)
#define ELF32_R_TYPE(i) ((unsigned char)(i))
#define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t))
#define SHN_UNDEF 0
#define SHN_ABS 0xFFF1
#define SHN_COMMON 0xFFF2
#define PF_R 4
#define PF_W 2
#define PF_X 1
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#define PT_NOTE 4
#define PT_SHLIB 5
#define PT_PHDR 6// PT_SHDR
#define PT_TLS 7 /* Thread local storage segment */
#define PT_ARM_ARCHEXT 0x70000000
#define PT_ARM_EXIDX 0x70000001
#define DT_NULL 0
#define DT_NEEDED 1
#define DT_PLTRELSZ 2
#define DT_PLTGOT 3
#define DT_HASH 4
#define DT_STRTAB 5
#define DT_SYMTAB 6
#define DT_RELA 7
#define DT_RELASZ 8
#define DT_RELAENT 9
#define DT_STRSZ 10
#define DT_SYMENT 11
#define DT_INIT 12
#define DT_FINI 13
#define DT_SONAME 14
#define DT_RPATH 15
#define DT_SYMBOLIC 16
#define DT_REL 17
#define DT_RELSZ 18
#define DT_RELENT 19
#define DT_PLTREL 20
#define DT_DEBUG 21
#define DT_TEXTREL 22
#define DT_JMPREL 23
#define DT_BIND_NOW 24
#define DT_INIT_ARRAY 25
#define DT_FINI_ARRAY 26
#define DT_INIT_ARRAYSZ 27
#define DT_FINI_ARRAYSZ 28
#define DT_RUNPATH 29
#define DT_FLAGS 30
#define DT_ENCODING 32
#define DT_PREINIT_ARRAY 32
#define DT_PREINIT_ARRAYSZ 33
#define DT_NUM 34
#define DT_VERSYM 0x6ffffff0
#define DT_RELACOUNT 0x6ffffff9
#define DT_RELCOUNT 0x6ffffffa
#define DT_FLAGS_1 0x6ffffffb
#define DT_VERDEF 0x6ffffffc
#define DT_VERDEFNUM 0x6ffffffd
#define DT_VERNEED 0x6ffffffe
#define DT_VERNEEDNUM 0x6fffffff
// Section Types, 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_NUM 12
#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_RELR 19 // Relocation entries; only offsets.
#define SHT_LOOS 0x60000000 // Lowest operating system-specific type.
// Android packed relocation section types.
// https://android.googlesource.com/platform/bionic/+/6f12bfece5dcc01325e0abba56a46b1bcf991c69/tools/relocation_packer/src/elf_file.cc#37
#define SHT_ANDROID_REL 0x60000001
#define SHT_ANDROID_RELA 0x60000002
#define SHT_LLVM_ODRTAB 0x6fff4c00 // LLVM ODR table.
#define SHT_LLVM_LINKER_OPTIONS 0x6fff4c01 // LLVM Linker Options.
#define SHT_LLVM_CALL_GRAPH_PROFILE 0x6fff4c02 // LLVM Call Graph Profile.
#define SHT_LLVM_ADDRSIG 0x6fff4c03 // List of address-significant symbols
#define SHT_LLVM_DEPENDENT_LIBRARIES 0x6fff4c04 // LLVM Dependent Library Specifiers.
#define SHT_LLVM_SYMPART 0x6fff4c05 // Symbol partition specification.
#define SHT_LLVM_PART_EHDR 0x6fff4c06 // ELF header for loadable partition.
#define SHT_LLVM_PART_PHDR 0x6fff4c07 // Phdrs for loadable partition.
// Android's experimental support for SHT_RELR sections.
#define SHT_GNU_ATTRIBUTES 0x6ffffff5 // Object attributes.
#define SHT_GNU_HASH 0x6ffffff6 // GNU-style hash table.
#define SHT_GNU_LIBLIST 0x6ffffff7
#define SHT_GNU_verdef 0x6ffffffd // GNU version definitions.
#define SHT_GNU_verneed 0x6ffffffe // GNU version references.
#define SHT_GNU_versym 0x6fffffff // GNU symbol versions table.
#define SHT_HIOS 0x6fffffff // Highest operating system-specific type.
#define SHT_LOPROC 0x70000000
#define SHT_ARM_EXIDX 0x70000001
#define SHT_ARM_PREEMPTMAP 0x70000002
#define SHT_ARM_ATTRIBUTES 0x70000003 // ARM: Object file compatibility attributes
#define SHT_ARM_DEBUGOVERLAY 0x70000004
#define SHT_ARM_OVERLAYSECTION 0x70000005
#define SHT_HIPROC 0x7FFFFFFF
/*! Спецификация типов перемещений для ARM */
enum {
R_ARM_NONE =0,
R_ARM_ABS32 =2,
R_ARM_REL32 =3,
R_ARM_THM_PC22 =10,// R_ARM_THM_PC22
R_ARM_THM_CALL =10,// R_ARM_THM_PC22
// Dynamic relocations
R_ARM_COPY =20,
R_ARM_GLOB_DAT =21,/* Create GOT entry */
R_ARM_JUMP_SLOT =22,/* Create PLT entry */
R_ARM_RELATIVE =23,/* Adjust by program base */
// Position Relative
R_ARM_GOTOFF32 =24,/* 32 bit offset to GOT */
R_ARM_BASE_PREL =25,/* R_ARM_GOTPC 32 bit PC relative offset to GOT */
R_ARM_GOT_BREL =26,/* R_ARM_GOT32 32 bit GOT entry */
R_ARM_THM_JUMP24 =30,
R_ARM_BASE_ABS =31,
R_ARM_TARGET1 =38,
R_ARM_PREL31 =42,
R_ARM_THM_MOVW_ABS_NC=47,
R_ARM_THM_MOVT_ABS =48,
R_ARM_THM_MOVW_PREL_NC =49,
R_ARM_THM_MOVT_PREL =50,
R_ARM_THM_JUMP19 =51,
R_ARM_THM_MOVW_BREL_NC = 87,
R_ARM_THM_MOVT_BREL = 88,
R_ARM_THM_MOVW_BREL = 89,
};
#endif // __ELF_H