Advertisement
Metalhead33

elf.h

Sep 8th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.84 KB | None | 0 0
  1. #ifndef ELF_H
  2. #define ELF_H
  3.  
  4. #ifdef __cplusplus
  5. #include <cstdint>
  6. #else
  7. #include <stdint.h>
  8. #endif
  9.  
  10. // Check windows
  11. #if _WIN32 || _WIN64
  12. #if _WIN64
  13. #define ENV64BIT
  14. #else
  15. #define ENV32BIT
  16. #endif
  17. #endif
  18.  
  19. // Check GCC
  20. #if __GNUC__
  21. #if __x86_64__ || __ppc64__
  22. #define ENV64BIT
  23. #else
  24. #define ENV32BIT
  25. #endif
  26. #endif
  27.  
  28. // Define the ABIs
  29. #define ABI_SYSTEM_V 0x00
  30. #define ABI_HP-UX 0x01
  31. #define ABI_NETBSD 0x02
  32. #define ABI_LINUX 0x03
  33. #define ABI_GNU_HURD 0x04
  34. #define ABI_SOLARIS 0x06
  35. #define ABI_AIX 0x07
  36. #define ABI_IRIX 0x08
  37. #define ABI_FREEBSD 0x09
  38. #define ABI_TRU64 0x0A
  39. #define ABI_NOVELL_MODESTO 0x0B
  40. #define ABI_OPENBSD 0x0C
  41. #define ABI_OPENVMS 0x0D
  42. #define ABI_NONSTOP_KERNEL 0x0E
  43. #define ABI_AROS 0x0F
  44. #define ABI_FENIX_OS 0x10
  45. #define ABI_CLOUDABI 0x11
  46. #define ABI_SORTIX 0x53
  47.  
  48. typedef struct _ELF_IDENT {
  49.     uint32_t EI_MAG; // 0x00 - always supposed to be 0x7F 0x45 0x4C 0x46
  50.     uint8_t EI_CLASS; // 0x04
  51.     uint8_t EI_DATA; // 0x05
  52.     uint8_t EI_VERSION; // 0x06
  53.     uint8_t EI_OSABI; // 0x07
  54.     uint8_t EI_ABIVERSION; // 0x08
  55.     char EI_PAD[7]; // 0x09
  56. } ELF_IDENT;
  57.  
  58. #define ARCH_NOMACHINE 0x00
  59. #define ARCH_SPARC 0x02
  60. #define ARCH_X86 0x03
  61. #define ARCH_MIPS 0x08
  62. #define ARCH_POWERPC 0x14
  63. #define ARCH_S390 0x16
  64. #define ARCH_ARM 0x28
  65. #define ARCH_SUPERH 0x2A
  66. #define ARCH_IA-64 0x32
  67. #define ARCH_X86-64 0x3E
  68. #define ARCH_AARCH64 0xB7
  69. #define ARCH_RISC-V 0xF3
  70.  
  71. typedef struct _ELF32_HEADER {
  72.     ELF_IDENT e_ident; // 0x09
  73.     uint16_t e_type; // 0x10
  74.     uint16_t e_machine; // 0x12
  75.     uint32_t e_version; // 0x14
  76.     uint32_t e_entry; // 0x18
  77.     uint32_t e_phoff; // 0x1c
  78.     uint32_t e_shoff; // 0x20
  79.     uint32_t e_flags; // 0x24
  80.     uint16_t e_ehsize; // 0x28
  81.     uint16_t e_phentsize; // 0x2A
  82.     uint16_t e_phnum; // 0x2C
  83.     uint16_t e_shentsize; // 0x2E
  84.     uint16_t e_shnum; // 0x30
  85.     uint16_t e_shstrndx; // 0x32
  86. } ELF32_HEADER;
  87.  
  88. typedef struct _ELF64_HEADER {
  89.     ELF_IDENT e_ident; // 0x09
  90.     uint16_t e_type; // 0x10
  91.     uint16_t e_machine; // 0x12
  92.     uint32_t e_version; // 0x14
  93.     uint64_t e_entry; // 0x18
  94.     uint64_t e_phoff; // 0x20
  95.     uint64_t e_shoff; // 0x28
  96.     uint32_t e_flags; // 0x30
  97.     uint16_t e_ehsize; // 0x34
  98.     uint16_t e_phentsize; // 0x36
  99.     uint16_t e_phnum; // 0x38
  100.     uint16_t e_shentsize; // 0x3A
  101.     uint16_t e_shnum; // 0x3C
  102.     uint16_t e_shstrndx; // 0x3E
  103. } ELF64_HEADER;
  104.  
  105. #define PT_NULL 0x00000000
  106. #define PT_LOAD 0x00000001
  107. #define PT_DYNAMIC 0x00000002
  108. #define PT_INTERP 0x00000003
  109. #define PT_NOTE 0x00000004
  110. #define PT_SHLIB 0x00000005
  111. #define PT_PHDR 0x00000006
  112. #define PT_LOOS 0x60000000
  113. #define PT_HIOS 0x6FFFFFFF
  114. #define PT_LOPROC 0x70000000
  115. #define PT_HIPROC 0x7FFFFFFF
  116.  
  117. typedef struct _PROGRAM_HEADER_32 {
  118.     uint32_t p_type; // 0x00
  119.     uint32_t p_offset; // 0x04
  120.     uint32_t p_vaddr; // 0x08
  121.     uint32_t p_paddr; // 0x0C
  122.     uint32_t p_filesz; // 0x10
  123.     uint32_t p_memsz; // 0x14
  124.     uint32_t p_flags; // 0x18
  125.     uint32_t p_align; // 0x1C
  126. } PROGRAM_HEADER_32;
  127.  
  128. typedef struct _PROGRAM_HEADER_64 {
  129.     uint32_t p_type; // 0x00
  130.     uint32_t p_flags; // 0x04
  131.     uint64_t p_offset; // 0x08
  132.     uint64_t p_vaddr; // 0x10
  133.     uint64_t p_paddr; // 0x18
  134.     uint64_t p_filesz; // 0x20
  135.     uint64_t p_memsz; // 0x28
  136.     uint64_t p_align; // 0x30
  137. } PROGRAM_HEADER_64;
  138.  
  139. #define SHT_NULL 0x0
  140. #define SHT_PROGBITS 0x1
  141. #define SHT_SYMTAB 0x2
  142. #define SHT_STRTAB 0x3
  143. #define SHT_RELA 0x4
  144. #define SHT_HASH 0x5
  145. #define SHT_DYNAMIC 0x6
  146. #define SHT_NOTE 0x7
  147. #define SHT_NOBITS 0x8
  148. #define SHT_REL 0x9
  149. #define SHT_SHLIB 0x0A
  150. #define SHT_DYNSYM 0x0B
  151. #define SHT_INIT_ARRAY 0x0E
  152. #define SHT_FINI_ARRAY 0x0F
  153. #define SHT_PREINIT_ARRAY 0x10
  154. #define SHT_GROUP 0x11
  155. #define SHT_SYMTAB_SHNDX 0x12
  156. #define SHT_NUM 0x13
  157. #define SHT_LOOS 0x60000000
  158.  
  159. typedef struct _SECTION_HEADER_32 {
  160.     uint32_t sh_name; // 0x00
  161.     uint32_t sh_type; // 0x04
  162.     uint32_t sh_flags; // 0x08
  163.     uint32_t sh_addr; // 0x0C
  164.     uint32_t sh_offset; // 0x10
  165.     uint32_t sh_size; // 0x14
  166.     uint32_t sh_link; // 0x18
  167.     uint32_t sh_info; // 0x1C
  168.     uint32_t sh_addralign; // 0x20
  169.     uint32_t sh_entsize; // 0x24
  170. } SECTION_HEADER_32; // 0x28
  171.  
  172. typedef struct _SECTION_HEADER_64 {
  173.     uint32_t sh_name; // 0x00
  174.     uint32_t sh_type; // 0x04
  175.     uint64_t sh_flags; // 0x08
  176.     uint64_t sh_addr; // 0x10
  177.     uint64_t sh_offset; // 0x18
  178.     uint64_t sh_size; // 0x20
  179.     uint32_t sh_link; // 0x28
  180.     uint32_t sh_info; // 0x2C
  181.     uint64_t sh_addralign; // 0x30
  182.     uint64_t sh_entsize; // 0x38
  183. } SECTION_HEADER_64; // 0x40
  184.  
  185. #if defined(ENV64BIT)  
  186. typedef ELF64_HEADER ElfHeader;
  187. typedef PROGRAM_HEADER_64 ProgramHeader;
  188. typedef SECTION_HEADER_64 SectionHeader;
  189. #elif defined(ENV32BIT)  
  190. typedef ELF32_HEADER ElfHeader;
  191. typedef PROGRAM_HEADER_32 ProgramHeader;
  192. typedef SECTION_HEADER_32 SectionHeader;
  193. #else  
  194. #error "Your system is not supported, as it seems to be neither 32-bit nor 64-bit."
  195. #endif
  196. typedef ElfHeader* pElfHeader;
  197. typedef ProgramHeader* pProgramHeader;
  198. typedef SectionHeader* pSectionHeader;
  199.  
  200. #endif /* ELF_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement