elf.hpp 6.8 KB

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