2
* Australian Public Licence B (OZPLB)
6
* Copyright (c) 1999-2004 University of New South Wales
10
* Developed by: Operating Systems and Distributed Systems Group (DiSy)
11
* University of New South Wales
12
* http://www.disy.cse.unsw.edu.au
14
* Permission is granted by University of New South Wales, free of charge, to
15
* any person obtaining a copy of this software and any associated
16
* documentation files (the "Software") to deal with the Software without
17
* restriction, including (without limitation) the rights to use, copy,
18
* modify, adapt, merge, publish, distribute, communicate to the public,
19
* sublicense, and/or sell, lend or rent out copies of the Software, and
20
* to permit persons to whom the Software is furnished to do so, subject
21
* to the following conditions:
23
* * Redistributions of source code must retain the above copyright
24
* notice, this list of conditions and the following disclaimers.
26
* * Redistributions in binary form must reproduce the above
27
* copyright notice, this list of conditions and the following
28
* disclaimers in the documentation and/or other materials provided
29
* with the distribution.
31
* * Neither the name of University of New South Wales, nor the names of its
32
* contributors, may be used to endorse or promote products derived
33
* from this Software without specific prior written permission.
35
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
36
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
37
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
38
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
39
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
40
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
41
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
42
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
43
* ERRORS, WHETHER OR NOT DISCOVERABLE.
45
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
46
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
47
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
48
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
49
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
50
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
51
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
52
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
53
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
54
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
55
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
56
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
57
* DAMAGES OR OTHER LIABILITY.
59
* If applicable legislation implies representations, warranties, or
60
* conditions, or imposes obligations or liability on University of New South
61
* Wales or one of its contributors in respect of the Software that
62
* cannot be wholly or partly excluded, restricted or modified, the
63
* liability of University of New South Wales or the contributor is limited, to
64
* the full extent permitted by the applicable legislation, at its
66
* a. in the case of goods, any one or more of the following:
67
* i. the replacement of the goods or the supply of equivalent goods;
68
* ii. the repair of the goods;
69
* iii. the payment of the cost of replacing the goods or of acquiring
71
* iv. the payment of the cost of having the goods repaired; or
72
* b. in the case of services:
73
* i. the supplying of the services again; or
74
* ii. the payment of the cost of having the services supplied again.
76
* The construction, validity and performance of this licence is governed
77
* by the laws in force in New South Wales, Australia.
81
Authors: Luke Deller, Ben Leslie
88
\brief Generic ELF library
90
The ELF library is designed to make the task of parsing and getting information
91
out of an ELF file easier.
93
It provides function to obtain the various different fields in the ELF header, and
94
the program and segment information.
96
Also importantly, it provides a function elf_loadFile which will load a given
101
#ifndef __ELF_ELF_H__
102
#define __ELF_ELF_H__
111
uint32_t st_name; // Symbol name (string table index)
112
uint32_t st_value; // Symbol value
113
uint32_t st_size; // Symbol size
114
uint8_t st_info; // Symbol type and binding
115
uint8_t st_other; // No defined meaning, 0
116
uint16_t st_shndx; // Section index
120
* constants for Elf32_Phdr.p_flags
122
#define PF_X 1 /* readable segment */
123
#define PF_W 2 /* writeable segment */
124
#define PF_R 4 /* executable segment */
127
* constants for indexing into Elf64_Header_t.e_ident
137
#define ELFMAG0 '\177'
151
#define ELFDATA2LSB 1
152
#define ELFDATA2MSB 2
154
/* Section Header type bits */
155
#define SHT_PROGBITS 1
160
/* Section Header flag bits */
163
#define SHF_EXECINSTR 4
166
#define ELF_PRINT_PROGRAM_HEADERS 1
167
#define ELF_PRINT_SECTIONS 2
168
#define ELF_PRINT_ALL (ELF_PRINT_PROGRAM_HEADERS | ELF_PRINT_SECTIONS)
171
* Checks that elfFile points to a valid elf file.
173
* @param elfFile Potential ELF file to check
175
* \return 0 on success. -1 if not and elf, -2 if not 32 bit.
177
int elf_checkFile(void *elfFile);
180
* Determine number of sections in an ELF file.
182
* @param elfFile Pointer to a valid ELF header.
184
* \return Number of sections in the ELF file.
186
unsigned elf_getNumSections(void *elfFile);
189
* Determine number of program headers in an ELF file.
191
* @param elfFile Pointer to a valid ELF header.
193
* \return Number of program headers in the ELF file.
195
uint16_t elf_getNumProgramHeaders(void *elfFile);
198
* Return the base physical address of given program header in an ELF file
200
* @param elfFile Pointer to a valid ELF header
201
* @param ph Index of the program header
203
* \return The memory size of the specified program header
205
uint64_t elf_getProgramHeaderPaddr(void *elfFile, uint16_t ph);
208
* Return the base virtual address of given program header in an ELF file
210
* @param elfFile Pointer to a valid ELF header
211
* @param ph Index of the program header
213
* \return The memory size of the specified program header
215
uint64_t elf_getProgramHeaderVaddr(void *elfFile, uint16_t ph);
218
* Return the memory size of a given program header in an ELF file
220
* @param elfFile Pointer to a valid ELF header
221
* @param ph Index of the program header
223
* \return The memory size of the specified program header
225
uint64_t elf_getProgramHeaderMemorySize(void *elfFile, uint16_t ph);
228
* Return the file size of a given program header in an ELF file
230
* @param elfFile Pointer to a valid ELF header
231
* @param ph Index of the program header
233
* \return The file size of the specified program header
235
uint64_t elf_getProgramHeaderFileSize(void *elfFile, uint16_t ph);
238
* Return the start offset of he file
240
* @param elfFile Pointer to a valid ELF header
241
* @param ph Index of the program header
243
* \return The offset of this program header with relation to the start
246
uint64_t elf_getProgramHeaderOffset(void *elfFile, uint16_t ph);
249
* Return the flags for a given program header
251
* @param elfFile Pointer to a valid ELF header
252
* @param ph Index of the program header
254
* \return The flags of a given program header
256
uint32_t elf_getProgramHeaderFlags(void *elfFile, uint16_t ph);
259
* Return the type for a given program header
261
* @param elfFile Pointer to a valid ELF header
262
* @param ph Index of the program header
264
* \return The type of a given program header
266
uint32_t elf_getProgramHeaderType(void *elfFile, uint16_t ph);
269
* Return the physical translation of a physical address, with respect
270
* to a given program header
273
uint64_t elf_vtopProgramHeader(void *elfFile, uint16_t ph, uint64_t vaddr);
278
* \return true if the address in in this program header
280
bool elf_vaddrInProgramHeader(void *elfFile, uint16_t ph, uint64_t vaddr);
283
* Determine the memory bounds of an ELF file
285
* @param elfFile Pointer to a valid ELF header
286
* @param phys If true return bounds of physical memory, otherwise return
287
* bounds of virtual memory
288
* @param min Pointer to return value of the minimum
289
* @param max Pointer to return value of the maximum
291
* \return true on success. false on failure, if for example, it is an invalid ELF file
293
bool elf_getMemoryBounds(void *elfFile, bool phys, uint64_t *min, uint64_t *max);
296
* Find the entry point of an ELF file.
298
* @param elfFile Pointer to a valid ELF header
300
* \return The entry point address as a 64-bit integer.
302
uint64_t elf_getEntryPoint(void *elfFile);
305
* Load an ELF file into memory
307
* @param elfFile Pointer to a valid ELF file
308
* @param phys If true load using the physical address, otherwise using the virtual addresses
310
* \return true on success, false on failure.
312
* The function assumes that the ELF file is loaded in memory at some
313
* address different to the target address at which it will be loaded.
314
* It also assumes direct access to the source and destination address, i.e:
315
* Memory must be ale to me loaded with a simple memcpy.
317
* Obviously this also means that if we are loading a 64bit ELF on a 32bit
318
* platform, we assume that any memory address are within the first 4GB.
321
bool elf_loadFile(void *elfFile, bool phys);
323
char *elf_getStringTable(void *elfFile, int string_segment);
324
char *elf_getSegmentStringTable(void *elfFile);
325
void *elf_getSectionNamed(void *elfFile, char *str);
326
char *elf_getSectionName(void *elfFile, int i);
327
uint64_t elf_getSectionSize(void *elfFile, int i);
328
uint64_t elf_getSectionLink(void *elfFile, int i);
329
uint64_t elf_getSectionAddr(void *elfFile, int i);
332
* Return the flags for a given sections
334
* @param elfFile Pointer to a valid ELF header
335
* @param i Index of the sections
337
* \return The flags of a given section
339
uint32_t elf_getSectionFlags(void *elfFile, int i);
342
* Return the type for a given sections
344
* @param elfFile Pointer to a valid ELF header
345
* @param i Index of the sections
347
* \return The type of a given section
349
uint32_t elf_getSectionType(void *elfFile, int i);
351
void *elf_getSection(void *elfFile, int i);
352
void elf_getProgramHeaderInfo(void *elfFile, uint16_t ph, uint64_t *p_vaddr,
353
uint64_t *p_paddr, uint64_t *p_filesz,
354
uint64_t *p_offset, uint64_t *p_memsz);
358
* output the details of an ELF file to the stream f
360
void elf_fprintf(FILE *f, void *elfFile, int size, const char *name, int flags);
362
#define SHT_DYNSYM 0xB
365
* Returns a pointer to the program segment table, which is an array of
366
* ELF64_Phdr_t structs. The size of the array can be found by calling
367
* getNumProgramSegments.
369
struct Elf32_Phdr *elf_getProgramSegmentTable(void *elfFile);
373
* Returns a pointer to the program segment table, which is an array of
374
* ELF64_Phdr_t structs. The size of the array can be found by calling
375
* getNumProgramSegments.
377
struct Elf32_Shdr *elf_getSectionTable(void *elfFile);