elf/elf.h File Reference
Detailed Description
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.
|
Functions |
| int | elf_checkFile (void *elfFile) |
| unsigned | elf_getNumSections (void *elfFile) |
| uint16_t | elf_getNumProgramHeaders (void *elfFile) |
| uint64_t | elf_getProgramHeaderPaddr (void *elfFile, uint16_t ph) |
| uint64_t | elf_getProgramHeaderVaddr (void *elfFile, uint16_t ph) |
| uint64_t | elf_getProgramHeaderMemorySize (void *elfFile, uint16_t ph) |
| uint64_t | elf_getProgramHeaderFileSize (void *elfFile, uint16_t ph) |
| uint64_t | elf_getProgramHeaderOffset (void *elfFile, uint16_t ph) |
| uint32_t | elf_getProgramHeaderFlags (void *elfFile, uint16_t ph) |
| uint32_t | elf_getProgramHeaderType (void *elfFile, uint16_t ph) |
| uint64_t | elf_vtopProgramHeader (void *elfFile, uint16_t ph, uint64_t vaddr) |
| bool | elf_vaddrInProgramHeader (void *elfFile, uint16_t ph, uint64_t vaddr) |
| bool | elf_getMemoryBounds (void *elfFile, bool phys, uint64_t *min, uint64_t *max) |
| uint64_t | elf_getEntryPoint (void *elfFile) |
| bool | elf_loadFile (void *elfFile, bool phys) |
| uint32_t | elf_getSectionFlags (void *elfFile, int i) |
| uint32_t | elf_getSectionType (void *elfFile, int i) |
| void | elf_fprintf (FILE *f, void *elfFile, int size, const char *name, int flags) |
| Elf32_Shdr * | elf_getSectionTable (void *elfFile) |
Function Documentation
| int elf_checkFile |
( |
void * |
elfFile |
) |
|
|
|
|
Checks that elfFile points to a valid elf file.
- Parameters:
-
| elfFile | Potential ELF file to check |
- Returns:
- 0 on success. -1 if not and elf, -2 if not 32 bit.
|
| void elf_fprintf |
( |
FILE * |
f, |
|
|
void * |
elfFile, |
|
|
int |
size, |
|
|
const char * |
name, |
|
|
int |
flags |
|
) |
|
|
|
|
output the details of an ELF file to the stream f |
| uint64_t elf_getEntryPoint |
( |
void * |
elfFile |
) |
|
|
|
|
Find the entry point of an ELF file.
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
- Returns:
- The entry point address as a 64-bit integer.
|
| bool elf_getMemoryBounds |
( |
void * |
elfFile, |
|
|
bool |
phys, |
|
|
uint64_t * |
min, |
|
|
uint64_t * |
max |
|
) |
|
|
|
|
Determine the memory bounds of an ELF file
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| phys | If true return bounds of physical memory, otherwise return bounds of virtual memory |
| min | Pointer to return value of the minimum |
| max | Pointer to return value of the maximum |
- Returns:
- true on success. false on failure, if for example, it is an invalid ELF file
|
| uint16_t elf_getNumProgramHeaders |
( |
void * |
elfFile |
) |
|
|
|
|
Determine number of program headers in an ELF file.
- Parameters:
-
| elfFile | Pointer to a valid ELF header. |
- Returns:
- Number of program headers in the ELF file.
|
| unsigned elf_getNumSections |
( |
void * |
elfFile |
) |
|
|
|
|
Determine number of sections in an ELF file.
- Parameters:
-
| elfFile | Pointer to a valid ELF header. |
- Returns:
- Number of sections in the ELF file.
|
| uint64_t elf_getProgramHeaderFileSize |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the file size of a given program header in an ELF file
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The file size of the specified program header
|
| uint32_t elf_getProgramHeaderFlags |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the flags for a given program header
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The flags of a given program header
|
| uint64_t elf_getProgramHeaderMemorySize |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the memory size of a given program header in an ELF file
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The memory size of the specified program header
|
| uint64_t elf_getProgramHeaderOffset |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the start offset of he file
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The offset of this program header with relation to the start of the elfFile.
|
| uint64_t elf_getProgramHeaderPaddr |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the base physical address of given program header in an ELF file
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The memory size of the specified program header
|
| uint32_t elf_getProgramHeaderType |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the type for a given program header
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The type of a given program header
|
| uint64_t elf_getProgramHeaderVaddr |
( |
void * |
elfFile, |
|
|
uint16_t |
ph |
|
) |
|
|
|
|
Return the base virtual address of given program header in an ELF file
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| ph | Index of the program header |
- Returns:
- The memory size of the specified program header
|
| uint32_t elf_getSectionFlags |
( |
void * |
elfFile, |
|
|
int |
i |
|
) |
|
|
|
|
Return the flags for a given sections
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| i | Index of the sections |
- Returns:
- The flags of a given section
|
| struct Elf32_Shdr* elf_getSectionTable |
( |
void * |
elfFile |
) |
|
|
|
|
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. |
| uint32_t elf_getSectionType |
( |
void * |
elfFile, |
|
|
int |
i |
|
) |
|
|
|
|
Return the type for a given sections
- Parameters:
-
| elfFile | Pointer to a valid ELF header |
| i | Index of the sections |
- Returns:
- The type of a given section
|
| bool elf_loadFile |
( |
void * |
elfFile, |
|
|
bool |
phys |
|
) |
|
|
|
|
Load an ELF file into memory
- Parameters:
-
| elfFile | Pointer to a valid ELF file |
| phys | If true load using the physical address, otherwise using the virtual addresses |
- Returns:
- 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 ale to me 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 address are within the first 4GB. |
| bool elf_vaddrInProgramHeader |
( |
void * |
elfFile, |
|
|
uint16_t |
ph, |
|
|
uint64_t |
vaddr |
|
) |
|
|
|
|
- Returns:
- true if the address in in this program header
|
| uint64_t elf_vtopProgramHeader |
( |
void * |
elfFile, |
|
|
uint16_t |
ph, |
|
|
uint64_t |
vaddr |
|
) |
|
|
|
|
Return the physical translation of a physical address, with respect to a given program header |