-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.h
369 lines (320 loc) · 8.36 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
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 1999-2004 University of New South Wales
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
Authors: Luke Deller, Ben Leslie
Created: 24/Sep/1999
*/
/**
\file
\brief Generic ELF library
The ELF library is designed to make the task of parsing and getting information
out of an ELF file easier.
It provides function to obtain the various different fields in the ELF header, and
the program and segment information.
Also importantly, it provides a function elf_loadFile which will load a given
ELF file into memory.
*/
#pragma once
#include <sys/types.h>
#include "printf.h"
#include "elf32.h"
#include "elf64.h"
#define ISELF32(elfFile) ( ((struct Elf32_Header*)elfFile)->e_ident[EI_CLASS] == ELFCLASS32 )
#define ISELF64(elfFile) ( ((struct Elf64_Header*)elfFile)->e_ident[EI_CLASS] == ELFCLASS64 )
/*
* constants for Elf32_Phdr.p_flags
*/
#define PF_R 4 /* readable segment */
#define PF_W 2 /* writeable segment */
#define PF_X 1 /* executable segment */
/*
* constants for indexing into Elf64_Header_t.e_ident
*/
#define EI_MAG0 0
#define EI_MAG1 1
#define EI_MAG2 2
#define EI_MAG3 3
#define EI_CLASS 4
#define EI_DATA 5
#define EI_VERSION 6
#define ELFMAG0 '\177'
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
#define ELFCLASS32 1
#define ELFCLASS64 2
#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
#define PT_TLS 7
#define PT_NUM 8
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
/* Section Header type bits */
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2
#define SHT_NOBITS 8
#define SHT_REL 9
/* Section Header flag bits */
#define SHF_WRITE 1
#define SHF_ALLOC 2
#define SHF_EXECINSTR 4
/* Dynamic section entry types */
#define DT_NULL 0
#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_REL 17
#define DT_RELSZ 18
#define DT_RELENT 19
/**/
#define ELF_PRINT_PROGRAM_HEADERS 1
#define ELF_PRINT_SECTIONS 2
#define ELF_PRINT_ALL (ELF_PRINT_PROGRAM_HEADERS | ELF_PRINT_SECTIONS)
/**
* Checks that elfFile points to a valid elf file.
*
* @param elfFile Potential ELF file to check
*
* \return 0 on success. -1 if not and elf, -2 if not 32 bit.
*/
int elf_checkFile(
void const *elfFile);
/**
* Determine number of sections in an ELF file.
*
* @param elfFile Pointer to a valid ELF header.
*
* \return Number of sections in the ELF file.
*/
unsigned elf_getNumSections(
void const *elfFile);
/**
* Determine number of program headers in an ELF file.
*
* @param elfFile Pointer to a valid ELF header.
*
* \return Number of program headers in the ELF file.
*/
uint16_t elf_getNumProgramHeaders(
void const *elfFile);
/**
* Return the base physical address of given program header in an ELF file
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The memory size of the specified program header
*/
uint64_t elf_getProgramHeaderPaddr(
void const *elfFile,
uint16_t ph);
/**
* Return the base virtual address of given program header in an ELF file
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The memory size of the specified program header
*/
uint64_t elf_getProgramHeaderVaddr(
void const *elfFile,
uint16_t ph);
/**
* Return the memory size of a given program header in an ELF file
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The memory size of the specified program header
*/
uint64_t elf_getProgramHeaderMemorySize(
void const *elfFile,
uint16_t ph);
/**
* Return the file size of a given program header in an ELF file
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The file size of the specified program header
*/
uint64_t elf_getProgramHeaderFileSize(
void const *elfFile,
uint16_t ph);
/**
* Return the start offset of he file
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The offset of this program header with relation to the start
* of the elfFile.
*/
uint64_t elf_getProgramHeaderOffset(
void const *elfFile,
uint16_t ph);
/**
* Return the flags for a given program header
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The flags of a given program header
*/
uint32_t elf_getProgramHeaderFlags(
void const *elfFile,
uint16_t ph);
/**
* Return the type for a given program header
*
* @param elfFile Pointer to a valid ELF header
* @param ph Index of the program header
*
* \return The type of a given program header
*/
uint32_t elf_getProgramHeaderType(
void const *elfFile,
uint16_t ph);
/**
* Return the physical translation of a physical address, with respect
* to a given program header
*
*/
uint64_t elf_vtopProgramHeader(
void const *elfFile,
uint16_t ph,
uint64_t vaddr);
/**
*
* \return true if the address in in this program header
*/
int elf_vaddrInProgramHeader(
void const *elfFile,
uint16_t ph,
uint64_t vaddr);
/**
* Determine the memory bounds of an ELF file
*
* @param elfFile Pointer to a valid ELF header
* @param phys If true return bounds of physical memory, otherwise return
* bounds of virtual memory
* @param min Pointer to return value of the minimum
* @param max Pointer to return value of the maximum
*
* \return true on success. false on failure, if for example, it is an invalid ELF file
*/
int elf_getMemoryBounds(
void const *elfFile,
int phys,
uint64_t *min,
uint64_t *max);
/**
* Find the entry point of an ELF file.
*
* @param elfFile Pointer to a valid ELF header
*
* \return The entry point address as a 64-bit integer.
*/
uint64_t elf_getEntryPoint(
void const *elfFile);
/**
* Load an ELF file into memory
*
* @param elfFile Pointer to a valid ELF file
* @param phys If true load using the physical address, otherwise using the virtual addresses
*
* \return true on success, false on failure.
*
* The function assumes that the ELF file is loaded in memory at some
* address different to the target address at which it will be loaded.
* It also assumes direct access to the source and destination address, i.e:
* Memory must be able to be loaded with a simple memcpy.
*
* Obviously this also means that if we are loading a 64bit ELF on a 32bit
* platform, we assume that any memory addresses are within the first 4GB.
*
*/
int elf_loadFile(
void const *elfFile,
int phys);
char const *elf_getStringTable(
void const *elfFile,
int string_segment);
char const *elf_getSegmentStringTable(
void const *elfFile);
void const *elf_getSectionNamed(
void const *elfFile,
char const *str);
char const *elf_getSectionName(
void const *elfFile,
int i);
uint64_t elf_getSectionSize(
void const *elfFile,
int i);
uint64_t elf_getSectionAddr(
void const *elfFile,
int i);
/**
* Return the flags for a given sections
*
* @param elfFile Pointer to a valid ELF header
* @param i Index of the sections
*
* \return The flags of a given section
*/
uint32_t elf_getSectionFlags(
void const *elfFile,
int i);
/**
* Return the type for a given sections
*
* @param elfFile Pointer to a valid ELF header
* @param i Index of the sections
*
* \return The type of a given section
*/
uint32_t elf_getSectionType(
void const *elfFile,
int i);
void const *elf_getSection(
void const *elfFile,
int i);
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);
#if 0
/*
* Returns a pointer to the program segment table, which is an array of
* ELF64_Phdr_t structs. The size of the array can be found by calling
* getNumProgramSegments.
*/
struct Elf32_Phdr const *elf_getProgramSegmentTable(
void const *elfFile);
#endif
#if 0
/**
* Returns a pointer to the program segment table, which is an array of
* ELF64_Phdr_t structs. The size of the array can be found by calling
* getNumProgramSegments.
*/
struct Elf32_Shdr const *elf_getSectionTable(
void const *elfFile);
#endif