| 1 |
/* elf.h - ELF64 loader for Pactor64 */ |
| 2 |
#ifndef _ELF_H |
| 3 |
#define _ELF_H |
| 4 |
|
| 5 |
#include "pactor64.h" |
| 6 |
|
| 7 |
/* ELF magic bytes */ |
| 8 |
#define ELFMAG0 0x7F |
| 9 |
#define ELFMAG1 'E' |
| 10 |
#define ELFMAG2 'L' |
| 11 |
#define ELFMAG3 'F' |
| 12 |
#define ELFCLASS64 2 |
| 13 |
#define ELFDATA2LSB 1 |
| 14 |
#define ET_EXEC 2 |
| 15 |
#define EM_X86_64 62 |
| 16 |
#define PT_LOAD 1 |
| 17 |
#define PF_X 0x1 |
| 18 |
#define PF_W 0x2 |
| 19 |
#define PF_R 0x4 |
| 20 |
|
| 21 |
/* 64-bit ELF header */ |
| 22 |
typedef struct { |
| 23 |
uint8_t e_ident[16]; |
| 24 |
uint16_t e_type; |
| 25 |
uint16_t e_machine; |
| 26 |
uint32_t e_version; |
| 27 |
uint64_t e_entry; |
| 28 |
uint64_t e_phoff; |
| 29 |
uint64_t e_shoff; |
| 30 |
uint32_t e_flags; |
| 31 |
uint16_t e_ehsize; |
| 32 |
uint16_t e_phentsize; |
| 33 |
uint16_t e_phnum; |
| 34 |
uint16_t e_shentsize; |
| 35 |
uint16_t e_shnum; |
| 36 |
uint16_t e_shstrndx; |
| 37 |
} __attribute__((packed)) elf64_ehdr_t; |
| 38 |
|
| 39 |
/* 64-bit Program header */ |
| 40 |
typedef struct { |
| 41 |
uint32_t p_type; |
| 42 |
uint32_t p_flags; |
| 43 |
uint64_t p_offset; |
| 44 |
uint64_t p_vaddr; |
| 45 |
uint64_t p_paddr; |
| 46 |
uint64_t p_filesz; |
| 47 |
uint64_t p_memsz; |
| 48 |
uint64_t p_align; |
| 49 |
} __attribute__((packed)) elf64_phdr_t; |
| 50 |
|
| 51 |
/* ELF load result */ |
| 52 |
typedef struct { |
| 53 |
uint64_t entry; |
| 54 |
uint64_t load_base; |
| 55 |
uint64_t load_end; |
| 56 |
int valid; |
| 57 |
} elf64_load_result_t; |
| 58 |
|
| 59 |
/* Validate and load an ELF64 executable */ |
| 60 |
elf64_load_result_t elf64_load(const void *file_data, uint32_t file_size); |
| 61 |
|
| 62 |
/* Validate ELF header without loading */ |
| 63 |
int elf64_validate(const void *file_data, uint32_t file_size); |
| 64 |
|
| 65 |
/* Get entry point from ELF header */ |
| 66 |
uint64_t elf64_get_entry(const void *file_data); |
| 67 |
|
| 68 |
#endif /* _ELF_H */ |
| 69 |
|