-
Notifications
You must be signed in to change notification settings - Fork 2
/
elf.h
181 lines (152 loc) · 5.03 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
#ifndef KERNAUX_INCLUDED_ELF
#define KERNAUX_INCLUDED_ELF
#ifdef __cplusplus
extern "C" {
#endif
#include <kernaux/macro.h>
#include <stdint.h>
#include <kernaux/macro/packing_start.run>
struct KernAux_ELF_Ident {
uint8_t magic_0x7f;
uint8_t magic_E;
uint8_t magic_L;
uint8_t magic_F;
uint8_t class_;
uint8_t data;
uint8_t version;
uint8_t unused[9];
}
KERNAUX_PACKED;
KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Ident, 16);
// KernAux_ELF_Ident.class_
#define KERNAUX_ELF_CLASS_NONE 0 // Invalid class
#define KERNAUX_ELF_CLASS_32 1 // 32-bit objects
#define KERNAUX_ELF_CLASS_64 2 // 64-bit objects
// KernAux_ELF_Ident.data
#define KERNAUX_ELF_DATA_NONE 0 // Invalid data encoding
#define KERNAUX_ELF_DATA_2LSB 1 // 0x01020304 == [0x04, 0x03, 0x02, 0x01]
#define KERNAUX_ELF_DATA_2MSB 2 // 0x01020304 == [0x01, 0x02, 0x03, 0x04]
struct KernAux_ELF_Header {
struct KernAux_ELF_Ident ident;
uint16_t type;
uint16_t machine;
uint32_t version;
uint32_t entry;
uint32_t phoff;
uint32_t shoff;
uint32_t flags;
uint16_t ehsize;
uint16_t phentsize;
uint16_t phnum;
uint16_t shentsize;
uint16_t shnum;
uint16_t shstrndx;
}
KERNAUX_PACKED;
KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Header, 52);
// KernAux_ELF_Header.type
#define KERNAUX_ELF_TYPE_NONE 0 // No file type
#define KERNAUX_ELF_TYPE_REL 1 // Relocatable file
#define KERNAUX_ELF_TYPE_EXEC 2 // Executable file
#define KERNAUX_ELF_TYPE_DYN 3 // Shared object file
#define KERNAUX_ELF_TYPE_CORE 4 // Core file
#define KERNAUX_ELF_TYPE_LOPROC 0xff00 // Processor-specific
#define KERNAUX_ELF_TYPE_HIPROC 0xffff // Processor-specific
// KernAux_ELF_Header.machine
#define KERNAUX_ELF_MACHINE_NONE 0 // No machine
#define KERNAUX_ELF_MACHINE_M32 1 // AT&T WE 32100
#define KERNAUX_ELF_MACHINE_SPARC 2 // SPARC
#define KERNAUX_ELF_MACHINE_386 3 // Intel 80386
#define KERNAUX_ELF_MACHINE_68K 4 // Motorola 68000
#define KERNAUX_ELF_MACHINE_88K 5 // Motorola 88000
#define KERNAUX_ELF_MACHINE_860 7 // Intel 80860
#define KERNAUX_ELF_MACHINE_MIPS 8 // MIPS RS3000
// KernAux_ELF_Header.version
#define KERNAUX_ELF_VERSION_NONE 0 // Invalid version
#define KERNAUX_ELF_VERSION_CURRENT 1 // Current version
struct KernAux_ELF_Section {
uint32_t name;
uint32_t type;
uint32_t flags;
uint32_t addr;
uint32_t offset;
uint32_t size;
uint32_t link;
uint32_t info;
uint32_t addralign;
uint32_t entsize;
}
KERNAUX_PACKED;
KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Section, 40);
#define KERNAUX_ELF_SECT_TYPE_NULL 0
#define KERNAUX_ELF_SECT_TYPE_PROGBITS 1
#define KERNAUX_ELF_SECT_TYPE_SYMTAB 2
#define KERNAUX_ELF_SECT_TYPE_STRTAB 3
#define KERNAUX_ELF_SECT_TYPE_RELA 4
#define KERNAUX_ELF_SECT_TYPE_HASH 5
#define KERNAUX_ELF_SECT_TYPE_DYNAMIC 6
#define KERNAUX_ELF_SECT_TYPE_NOTE 7
#define KERNAUX_ELF_SECT_TYPE_NOBITS 8
#define KERNAUX_ELF_SECT_TYPE_REL 9
#define KERNAUX_ELF_SECT_TYPE_SHLIB 10
#define KERNAUX_ELF_SECT_TYPE_DYNSYM 11
#define KERNAUX_ELF_SECT_TYPE_LOPROC 0x70000000
#define KERNAUX_ELF_SECT_TYPE_HIPROC 0x7fffffff
#define KERNAUX_ELF_SECT_TYPE_LOUSER 0x80000000
#define KERNAUX_ELF_SECT_TYPE_HIUSER 0xffffffff
#define KERNAUX_ELF_SECT_FLAGS_WRITE 0x1
#define KERNAUX_ELF_SECT_FLAGS_ALLOC 0x2
#define KERNAUX_ELF_SECT_FLAGS_EXECINSTR 0x4
#define KERNAUX_ELF_SECT_FLAGS_MASKPROC 0xf0000000
const char *KernAux_ELF_Section_Type_to_str(uint32_t type);
struct KernAux_ELF_Symbol {
uint32_t name;
uint32_t value;
uint32_t size;
uint8_t info;
uint8_t other;
uint16_t shndx;
}
KERNAUX_PACKED;
KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Symbol, 16);
#define KERNAUX_ELF_SYM_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
#define KERNAUX_ELF_SYM_BIND(info) ((info) >> 4)
#define KERNAUX_ELF_SYM_TYPE(info) ((into) & 0xf)
#define KERNAUX_ELF_SYM_BIND_LOCAL 0
#define KERNAUX_ELF_SYM_BIND_GLOBAL 1
#define KERNAUX_ELF_SYM_BIND_WEAK 2
#define KERNAUX_ELF_SYM_BIND_LOPROC 13
#define KERNAUX_ELF_SYM_BIND_HIPROC 15
#define KERNAUX_ELF_SYM_TYPE_NOTYPE 0
#define KERNAUX_ELF_SYM_TYPE_OBJECT 1
#define KERNAUX_ELF_SYM_TYPE_FUNC 2
#define KERNAUX_ELF_SYM_TYPE_SECTION 3
#define KERNAUX_ELF_SYM_TYPE_FILE 4
#define KERNAUX_ELF_SYM_TYPE_LOPROC 13
#define KERNAUX_ELF_SYM_TYPE_HIPROC 15
struct KernAux_ELF_Program {
uint32_t type;
uint32_t offset;
uint32_t vaddr;
uint32_t paddr;
uint32_t filesz;
uint32_t memsz;
uint32_t flags;
uint32_t align;
}
KERNAUX_PACKED;
KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Program, 32);
#define KERNAUX_ELF_PROG_TYPE_NULL 0
#define KERNAUX_ELF_PROG_TYPE_LOAD 1
#define KERNAUX_ELF_PROG_TYPE_DYNAMIC 2
#define KERNAUX_ELF_PROG_TYPE_INTERP 3
#define KERNAUX_ELF_PROG_TYPE_NOTE 4
#define KERNAUX_ELF_PROG_TYPE_SHLIB 5
#define KERNAUX_ELF_PROG_TYPE_PHDR 6
#define KERNAUX_ELF_PROG_TYPE_LOPROC 0x70000000
#define KERNAUX_ELF_PROG_TYPE_HIPROC 0x7fffffff
#include <kernaux/macro/packing_end.run>
#ifdef __cplusplus
}
#endif
#endif