elf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * elf.c
  3. *
  4. * Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _ELF_H_
  20. #define _ELF_H_
  21. #include <sdk/defs.h>
  22. #define ELF_MAGIC 0x464C457F
  23. #define ELF_CLASS_32BIT 1
  24. #define ELF_SEGMENT_NULL 0
  25. #define ELF_SEGMENT_LOAD 1
  26. #define ELF_SEGMENT_DYNAMIC 2
  27. #define ELF_SEGMENT_INTERP 3
  28. #define ELF_SEGMENT_NOTE 4
  29. #define ELF_SEGMENT_SHLIB 5
  30. #define ELF_SEGMENT_PHDR 6
  31. #define ELF_SEGMENT_TLS 7
  32. #define ELF_SEGMENT_EXEC (1 << 0)
  33. #define ELF_SEGMENT_WRITE (1 << 1)
  34. #define ELF_SEGMENT_READ (1 << 2)
  35. #define ELF_SECTION_FLAG_WRITE (1 << 0)
  36. #define ELF_SECTION_FLAG_ALLOC (1 << 1)
  37. #define ELF_SECTION_FLAG_EXECINSTR (1 << 2)
  38. #define ELF_SYMBOL_UNDEF 0
  39. #define ELF_R_SYM(x) ((x) >> 8)
  40. #define ELF_R_TYPE(x) ((x) & 0xFF)
  41. enum
  42. {
  43. ELF_DYNAMIC_TAG_NULL = 0,
  44. ELF_DYNAMIC_TAG_NEEDED,
  45. ELF_DYNAMIC_TAG_PLTRELSZ,
  46. ELF_DYNAMIC_TAG_PLTGOT,
  47. ELF_DYNAMIC_TAG_HASH,
  48. ELF_DYNAMIC_TAG_STRTAB,
  49. ELF_DYNAMIC_TAG_SYMTAB,
  50. ELF_DYNAMIC_TAG_RELA,
  51. ELF_DYNAMIC_TAG_RELASZ,
  52. ELF_DYNAMIC_TAG_RELAENT,
  53. ELF_DYNAMIC_TAG_STRSZ,
  54. ELF_DYNAMIC_TAG_SYMENT,
  55. ELF_DYNAMIC_TAG_INIT,
  56. ELF_DYNAMIC_TAG_FINI,
  57. ELF_DYNAMIC_TAG_SONAME,
  58. ELF_DYNAMIC_TAG_RPATH,
  59. ELF_DYNAMIC_TAG_SYMBOLIC,
  60. ELF_DYNAMIC_TAG_REL,
  61. ELF_DYNAMIC_TAG_RELSZ,
  62. ELF_DYNAMIC_TAG_RELENT,
  63. ELF_DYNAMIC_TAG_PLTREL,
  64. ELF_DYNAMIC_TAG_DEBUG,
  65. ELF_DYNAMIC_TAG_TEXTREL,
  66. ELF_DYNAMIC_TAG_JMPREL,
  67. ELF_DYNAMIC_TAG_BIND_NOW,
  68. ELF_DYNAMIC_TAG_INIT_ARRAY,
  69. ELF_DYNAMIC_TAG_FINI_ARRAY,
  70. ELF_DYNAMIC_TAG_INIT_ARRAYSZ,
  71. ELF_DYNAMIC_TAG_FINI_ARRAYSZ,
  72. ELF_DYNAMIC_TAG_RUNPATH,
  73. ELF_DYNAMIC_TAG_FLAGS,
  74. ELF_DYNAMIC_TAG_UNUSED,
  75. ELF_DYNAMIC_TAG_ENCODING,
  76. ELF_DYNAMIC_TAG_PREINIT_ARRAY,
  77. ELF_DYNAMIC_TAG_PREINIT_ARRAYSZ,
  78. ELF_DYNAMIC_TAG_SYMTAB_SHNDX,
  79. };
  80. enum
  81. {
  82. ELF_SECTION_TYPE_NULL,
  83. ELF_SECTION_TYPE_PROGBITS,
  84. ELF_SECTION_TYPE_SYMTAB,
  85. ELF_SECTION_TYPE_STRTAB,
  86. ELF_SECTION_TYPE_RELA,
  87. ELF_SECTION_TYPE_HASH,
  88. ELF_SECTION_TYPE_DYNAMIC,
  89. ELF_SECTION_TYPE_NOTE,
  90. ELF_SECTION_TYPE_NOBITS,
  91. ELF_SECTION_TYPE_REL,
  92. ELF_SECTION_TYPE_SHLIB,
  93. ELF_SECTION_TYPE_DYNSYM,
  94. };
  95. enum
  96. {
  97. ELF_R_386_NONE = 0,
  98. ELF_R_386_32,
  99. ELF_R_386_PC32,
  100. ELF_R_386_GOT32,
  101. ELF_R_386_PLT32,
  102. ELF_R_386_COPY,
  103. ELF_R_386_GLOB_DAT,
  104. ELF_R_386_JMP_SLOT,
  105. ELF_R_386_RELATIVE,
  106. ELF_R_386_GOTOFF,
  107. ELF_R_386_GOTPC,
  108. ELF_R_386_32PLT,
  109. ELF_R_386_16 = 20,
  110. ELF_R_386_PC16,
  111. ELF_R_386_8,
  112. ELF_R_386_PC8,
  113. };
  114. #pragma pack(push, 1)
  115. typedef struct
  116. {
  117. dword_t magic;
  118. byte_t class;
  119. byte_t encoding;
  120. byte_t version;
  121. byte_t system_abi;
  122. byte_t abi_version;
  123. } elf_ident_t;
  124. typedef struct
  125. {
  126. word_t type;
  127. word_t machine;
  128. dword_t version;
  129. dword_t entry;
  130. dword_t segment_offset;
  131. dword_t section_offset;
  132. dword_t flags;
  133. word_t header_size;
  134. word_t segment_entry_size;
  135. word_t segment_count;
  136. word_t section_entry_size;
  137. word_t section_count;
  138. word_t section_string_index;
  139. } elf32_header_t;
  140. typedef struct
  141. {
  142. dword_t type;
  143. dword_t offset;
  144. dword_t virtual_address;
  145. dword_t physical_address;
  146. dword_t file_size;
  147. dword_t mem_size;
  148. dword_t flags;
  149. dword_t alignment;
  150. } elf32_segment_t;
  151. typedef struct
  152. {
  153. dword_t sh_name;
  154. dword_t sh_type;
  155. dword_t sh_flags;
  156. dword_t sh_addr;
  157. dword_t sh_offset;
  158. dword_t sh_size;
  159. dword_t sh_link;
  160. dword_t sh_info;
  161. dword_t sh_addralign;
  162. dword_t sh_entsize;
  163. } elf32_section_t;
  164. typedef struct
  165. {
  166. int32_t d_tag;
  167. union
  168. {
  169. dword_t d_val;
  170. dword_t d_ptr;
  171. };
  172. } elf32_dynamic_t;
  173. typedef struct
  174. {
  175. dword_t st_name;
  176. dword_t st_value;
  177. dword_t st_size;
  178. byte_t st_info;
  179. byte_t st_other;
  180. word_t st_shndx;
  181. } elf32_symbol_t;
  182. typedef struct
  183. {
  184. dword_t r_offset;
  185. dword_t r_info;
  186. } elf32_rel_t;
  187. typedef struct
  188. {
  189. dword_t r_offset;
  190. dword_t r_info;
  191. int32_t r_addend;
  192. } elf32_rela_t;
  193. #pragma pack(pop)
  194. #endif