-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.c
301 lines (263 loc) · 6.96 KB
/
elf.c
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
/*
* Copyright (c) 1999-2004 University of New South Wales
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "elf.h"
#include "mem.h"
/*
* Checks that elfFile points to a valid elf file. Returns 0 if the elf
* file is valid, < 0 if invalid.
*/
int elf_checkFile(
void const *elfFile)
{
return ISELF32(elfFile)
? elf32_checkFile(elfFile)
: elf64_checkFile(elfFile);
}
/* Program Headers Access functions */
uint16_t elf_getNumProgramHeaders(
void const *elfFile)
{
return ISELF32(elfFile)
? elf32_getNumProgramHeaders(elfFile)
: elf64_getNumProgramHeaders(elfFile);
}
uint32_t elf_getProgramHeaderFlags(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderFlags(elfFile, ph)
: elf64_getProgramHeaderFlags(elfFile, ph);
}
uint32_t elf_getProgramHeaderType(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderType(elfFile, ph)
: elf64_getProgramHeaderType(elfFile, ph);
}
uint64_t elf_getProgramHeaderPaddr(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderPaddr(elfFile, ph)
: elf64_getProgramHeaderPaddr(elfFile, ph);
}
uint64_t elf_getProgramHeaderVaddr(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderVaddr(elfFile, ph)
: elf64_getProgramHeaderVaddr(elfFile, ph);
}
uint64_t elf_getProgramHeaderMemorySize(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderMemorySize(elfFile, ph)
: elf64_getProgramHeaderMemorySize(elfFile, ph);
}
uint64_t elf_getProgramHeaderFileSize(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderFileSize(elfFile, ph)
: elf64_getProgramHeaderFileSize(elfFile, ph);
}
uint64_t elf_getProgramHeaderOffset(
void const *elfFile,
uint16_t ph)
{
return ISELF32(elfFile)
? elf32_getProgramHeaderOffset(elfFile, ph)
: elf64_getProgramHeaderOffset(elfFile, ph);
}
char const *elf_getSegmentStringTable(
void const *elfFile)
{
return ISELF32(elfFile)
? elf32_getSegmentStringTable(elfFile)
: elf64_getSegmentStringTable(elfFile);
}
char const *elf_getStringTable(
void const *elfFile,
int string_segment)
{
return ISELF32(elfFile)
? elf32_getStringTable(elfFile)
: elf64_getStringTable(elfFile, string_segment);
}
unsigned elf_getNumSections(
void const *elfFile)
{
return ISELF32(elfFile)
? elf32_getNumSections(elfFile)
: elf64_getNumSections(elfFile);
}
char const *elf_getSectionName(
void const *elfFile,
int i)
{
return ISELF32(elfFile)
? elf32_getSectionName(elfFile, i)
: elf64_getSectionName(elfFile, i);
}
uint32_t elf_getSectionFlags(
void const *elfFile,
int i)
{
return ISELF32(elfFile)
? elf32_getSectionFlags(elfFile, i)
: elf64_getSectionFlags(elfFile, i);
}
uint32_t elf_getSectionType(
void const *elfFile,
int i)
{
return ISELF32(elfFile)
? elf32_getSectionType(elfFile, i)
: elf64_getSectionType(elfFile, i);
}
uint64_t elf_getSectionSize(
void const *elfFile,
int i)
{
return ISELF32(elfFile)
? elf32_getSectionSize(elfFile, i)
: elf64_getSectionSize(elfFile, i);
}
uint64_t elf_getSectionAddr(
void const *elfFile,
int i)
{
return ISELF32(elfFile)
? elf32_getSectionAddr(elfFile, i)
: elf64_getSectionAddr(elfFile, i);
}
void const *elf_getSection(
void const *elfFile,
int i)
{
return ISELF32(elfFile)
? elf32_getSection(elfFile, i)
: elf64_getSection(elfFile, i);
}
void const *elf_getSectionNamed(
void const *elfFile,
char const *_str)
{
return ISELF32(elfFile)
? elf32_getSectionNamed(elfFile, _str)
: elf64_getSectionNamed(elfFile, _str);
}
void elf_getProgramHeaderInfo(
void const *elfFile,
uint16_t ph,
uint64_t *p_vaddr,
uint64_t *p_paddr,
uint64_t *p_filesz,
uint64_t *p_offset,
uint64_t *p_memsz)
{
*p_vaddr = elf_getProgramHeaderVaddr(elfFile, ph);
*p_paddr = elf_getProgramHeaderPaddr(elfFile, ph);
*p_filesz = elf_getProgramHeaderFileSize(elfFile, ph);
*p_offset = elf_getProgramHeaderOffset(elfFile, ph);
*p_memsz = elf_getProgramHeaderMemorySize(elfFile, ph);
}
uint64_t elf_getEntryPoint(
void const *elfFile)
{
return ISELF32(elfFile)
? elf32_getEntryPoint (elfFile)
: elf64_getEntryPoint (elfFile);
}
int elf_getMemoryBounds(
void const *elfFile,
int phys,
uint64_t *min,
uint64_t *max)
{
uint64_t mem_min = UINT64_MAX;
uint64_t mem_max = 0;
if (elf_checkFile(elfFile) != 0) {
return 0;
}
for (unsigned int i = 0; i < elf_getNumProgramHeaders(elfFile); i++) {
uint64_t sect_min, sect_max;
if (elf_getProgramHeaderMemorySize(elfFile, i) == 0) {
continue;
}
if (phys) {
sect_min = elf_getProgramHeaderPaddr(elfFile, i);
} else {
sect_min = elf_getProgramHeaderVaddr(elfFile, i);
}
sect_max = sect_min + elf_getProgramHeaderMemorySize(elfFile, i);
if (sect_max > mem_max) {
mem_max = sect_max;
}
if (sect_min < mem_min) {
mem_min = sect_min;
}
}
*min = mem_min;
*max = mem_max;
return 1;
}
int elf_vaddrInProgramHeader(
void const *elfFile,
uint16_t ph,
uint64_t vaddr)
{
uint64_t min = elf_getProgramHeaderVaddr(elfFile, ph);
uint64_t max = min + elf_getProgramHeaderMemorySize(elfFile, ph);
if (vaddr >= min && vaddr < max) {
return 1;
} else {
return 0;
}
}
uint64_t elf_vtopProgramHeader(
void const *elfFile,
uint16_t ph,
uint64_t vaddr)
{
uint64_t ph_phys = elf_getProgramHeaderPaddr(elfFile, ph);
uint64_t ph_virt = elf_getProgramHeaderVaddr(elfFile, ph);
uint64_t paddr;
paddr = vaddr - ph_virt + ph_phys;
return paddr;
}
int elf_loadFile(
void const *elfFile,
int phys)
{
if (elf_checkFile(elfFile) != 0) {
return 0;
}
for (unsigned int i = 0; i < elf_getNumProgramHeaders(elfFile); i++) {
/* Load that section */
uint64_t dest, src;
size_t len;
if (phys) {
dest = elf_getProgramHeaderPaddr(elfFile, i);
} else {
dest = elf_getProgramHeaderVaddr(elfFile, i);
}
len = elf_getProgramHeaderFileSize(elfFile, i);
src = (uint64_t) (uintptr_t) elfFile + elf_getProgramHeaderOffset(elfFile, i);
memcpy((void*) (uintptr_t) dest, (void*) (uintptr_t) src, len);
dest += len;
memset((void*) (uintptr_t) dest, 0, elf_getProgramHeaderMemorySize(elfFile, i) - len);
}
return 1;
}