elf.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <kernel/interrupt.hpp>
  5. #include <kernel/process.hpp>
  6. #include <kernel/vfs.hpp>
  7. namespace types::elf {
  8. using elf32_addr_t = uint32_t;
  9. using elf32_off_t = uint32_t;
  10. using elf64_addr_t = uint64_t;
  11. using elf64_off_t = uint64_t;
  12. constexpr elf32_addr_t ELF32_STACK_BOTTOM = 0xbffff000;
  13. constexpr elf32_off_t ELF32_STACK_SIZE = 8 * 1024 * 1024;
  14. constexpr elf32_addr_t ELF32_STACK_TOP = ELF32_STACK_BOTTOM - ELF32_STACK_SIZE;
  15. struct PACKED elf32_header {
  16. // 0x7f, "ELF"
  17. char magic[4];
  18. enum : uint8_t {
  19. FORMAT_32 = 1,
  20. FORMAT_64 = 2,
  21. } format;
  22. enum : uint8_t {
  23. ENDIAN_LITTLE = 1,
  24. ENDIAN_BIG = 2,
  25. } endian;
  26. // should be 1
  27. uint8_t _version1;
  28. enum : uint8_t {
  29. ABI_SYSTEM_V = 0x00,
  30. // TODO:
  31. ABI_LINUX = 0x03,
  32. } abi;
  33. uint8_t abi_version;
  34. uint8_t _reserved[7];
  35. enum : uint16_t {
  36. ET_NONE = 0x00,
  37. ET_REL = 0x01,
  38. ET_EXEC = 0x02,
  39. ET_DYN = 0x03,
  40. ET_CORE = 0x04,
  41. ET_LOOS = 0xfe00,
  42. ET_HIOS = 0xfeff,
  43. ET_LOPROC = 0xff00,
  44. ET_HIPROC = 0xffff,
  45. } type;
  46. enum : uint16_t {
  47. ARCH_NONE = 0x00,
  48. ARCH_X86 = 0x03,
  49. ARCH_ARM = 0x28,
  50. ARCH_IA64 = 0x32,
  51. ARCH_X86_64 = 0x3e,
  52. ARCH_ARM64 = 0xb7,
  53. ARCH_RISCV = 0xf3,
  54. } arch;
  55. // should be 1
  56. uint32_t _version2;
  57. // entry address
  58. elf32_addr_t entry;
  59. // program header table offset
  60. elf32_off_t phoff;
  61. // section header table offset
  62. elf32_off_t shoff;
  63. // architecture dependent flags
  64. uint32_t flags;
  65. // elf header size
  66. uint16_t ehsize;
  67. // program header table entry size
  68. uint16_t phentsize;
  69. // program header table entries number
  70. uint16_t phnum;
  71. // section header table entry size
  72. uint16_t shentsize;
  73. // section header table entries number
  74. uint16_t shnum;
  75. // section header table entry index that contains section names
  76. uint16_t shstrndx;
  77. };
  78. struct PACKED elf32_program_header_entry {
  79. enum : uint32_t {
  80. PT_NULL = 0x00,
  81. PT_LOAD = 0x01,
  82. PT_DYNAMIC = 0x02,
  83. PT_INTERP = 0x03,
  84. PT_NOTE = 0x04,
  85. PT_SHLIB = 0x05,
  86. PT_PHDR = 0x06,
  87. PT_TLS = 0x07,
  88. PT_LOOS = 0x60000000,
  89. PT_HIOS = 0x6fffffff,
  90. PT_LIPROC = 0x70000000,
  91. PT_HIPROC = 0x7fffffff,
  92. } type;
  93. elf32_off_t offset;
  94. elf32_addr_t vaddr;
  95. elf32_addr_t paddr;
  96. elf32_off_t filesz;
  97. elf32_off_t memsz;
  98. // segment dependent
  99. enum : uint32_t {
  100. PF_X = 0x1,
  101. PF_W = 0x2,
  102. PF_R = 0x4,
  103. } flags;
  104. // 0 and 1 for no alignment, otherwise power of 2
  105. uint32_t align;
  106. };
  107. struct PACKED elf32_section_header_entry {
  108. elf32_off_t sh_name;
  109. enum : uint32_t {
  110. SHT_NULL = 0x00,
  111. SHT_PROGBITS = 0x01,
  112. SHT_RELA = 0x04,
  113. SHT_DYNAMIC = 0x06,
  114. SHT_NOTE = 0x07,
  115. SHT_NOBITS = 0x08,
  116. SHT_REL = 0x09,
  117. SHT_DYNSYM = 0x0b,
  118. SHT_INIT_ARRAY = 0x0e,
  119. SHT_FINI_ARRAY = 0x0f,
  120. SHT_PREINIT_ARRAY = 0x0f,
  121. } sh_type;
  122. enum : uint32_t {
  123. SHF_WRITE = 0x01,
  124. SHF_ALLOC = 0x02,
  125. SHF_EXECINSTR = 0x04,
  126. } sh_flags;
  127. elf32_addr_t sh_addr;
  128. elf32_off_t sh_offset;
  129. elf32_off_t sh_size;
  130. uint32_t sh_link;
  131. uint32_t sh_info;
  132. elf32_off_t sh_addralign;
  133. elf32_off_t sh_entsize;
  134. };
  135. struct elf32_load_data {
  136. const fs::dentry* exec_dent;
  137. std::vector<std::string> argv;
  138. std::vector<std::string> envp;
  139. uintptr_t ip;
  140. uintptr_t sp;
  141. };
  142. // TODO: environment variables
  143. int elf32_load(elf32_load_data& data);
  144. struct PACKED elf64_header {
  145. // 0x7f, "ELF"
  146. char magic[4];
  147. enum : uint8_t {
  148. FORMAT_32 = 1,
  149. FORMAT_64 = 2,
  150. } format;
  151. enum : uint8_t {
  152. ENDIAN_LITTLE = 1,
  153. ENDIAN_BIG = 2,
  154. } endian;
  155. // should be 1
  156. uint8_t _version1;
  157. enum : uint8_t {
  158. ABI_SYSTEM_V = 0x00,
  159. // TODO:
  160. ABI_LINUX = 0x03,
  161. } abi;
  162. uint8_t abi_version;
  163. uint8_t _reserved[7];
  164. enum : uint16_t {
  165. ET_NONE = 0x00,
  166. ET_REL = 0x01,
  167. ET_EXEC = 0x02,
  168. ET_DYN = 0x03,
  169. ET_CORE = 0x04,
  170. ET_LOOS = 0xfe00,
  171. ET_HIOS = 0xfeff,
  172. ET_LOPROC = 0xff00,
  173. ET_HIPROC = 0xffff,
  174. } type;
  175. enum : uint16_t {
  176. ARCH_NONE = 0x00,
  177. ARCH_X86 = 0x03,
  178. ARCH_ARM = 0x28,
  179. ARCH_IA64 = 0x32,
  180. ARCH_X86_64 = 0x3e,
  181. ARCH_ARM64 = 0xb7,
  182. ARCH_RISCV = 0xf3,
  183. } arch;
  184. // should be 1
  185. uint32_t _version2;
  186. // entry address
  187. elf64_addr_t entry;
  188. // program header table offset
  189. elf64_off_t phoff;
  190. // section header table offset
  191. elf64_off_t shoff;
  192. // architecture dependent flags
  193. uint32_t flags;
  194. // elf header size
  195. uint16_t ehsize;
  196. // program header table entry size
  197. uint16_t phentsize;
  198. // program header table entries number
  199. uint16_t phnum;
  200. // section header table entry size
  201. uint16_t shentsize;
  202. // section header table entries number
  203. uint16_t shnum;
  204. // section header table entry index that contains section names
  205. uint16_t shstrndx;
  206. };
  207. struct PACKED elf64_program_header_entry {
  208. enum : uint32_t {
  209. PT_NULL = 0x00,
  210. PT_LOAD = 0x01,
  211. PT_DYNAMIC = 0x02,
  212. PT_INTERP = 0x03,
  213. PT_NOTE = 0x04,
  214. PT_SHLIB = 0x05,
  215. PT_PHDR = 0x06,
  216. PT_TLS = 0x07,
  217. PT_LOOS = 0x60000000,
  218. PT_HIOS = 0x6fffffff,
  219. PT_LIPROC = 0x70000000,
  220. PT_HIPROC = 0x7fffffff,
  221. } type;
  222. // segment dependent
  223. enum : uint32_t {
  224. PF_X = 0x1,
  225. PF_W = 0x2,
  226. PF_R = 0x4,
  227. } flags;
  228. elf64_off_t offset;
  229. elf64_addr_t vaddr;
  230. elf64_addr_t paddr;
  231. elf64_off_t filesz;
  232. elf64_off_t memsz;
  233. // 0 and 1 for no alignment, otherwise power of 2
  234. uint64_t align;
  235. };
  236. struct PACKED elf64_section_header_entry {
  237. uint32_t sh_name;
  238. enum : uint32_t {
  239. SHT_NULL = 0x00,
  240. SHT_PROGBITS = 0x01,
  241. SHT_RELA = 0x04,
  242. SHT_DYNAMIC = 0x06,
  243. SHT_NOTE = 0x07,
  244. SHT_NOBITS = 0x08,
  245. SHT_REL = 0x09,
  246. SHT_DYNSYM = 0x0b,
  247. SHT_INIT_ARRAY = 0x0e,
  248. SHT_FINI_ARRAY = 0x0f,
  249. SHT_PREINIT_ARRAY = 0x0f,
  250. } sh_type;
  251. enum : uint64_t {
  252. SHF_WRITE = 0x01,
  253. SHF_ALLOC = 0x02,
  254. SHF_EXECINSTR = 0x04,
  255. } sh_flags;
  256. elf64_addr_t sh_addr;
  257. elf64_off_t sh_offset;
  258. elf64_off_t sh_size;
  259. uint32_t sh_link;
  260. uint32_t sh_info;
  261. elf64_off_t sh_addralign;
  262. elf64_off_t sh_entsize;
  263. };
  264. struct elf64_load_data {
  265. const fs::dentry* exec_dent;
  266. std::vector<std::string> argv;
  267. std::vector<std::string> envp;
  268. unsigned long ip;
  269. unsigned long sp;
  270. };
  271. } // namespace types::elf