elf.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include <errno.h>
  3. #include <kernel/interrupt.h>
  4. #include <kernel/process.hpp>
  5. #include <kernel/vfs.hpp>
  6. #include <stdint.h>
  7. #include <types/size.h>
  8. #include <types/status.h>
  9. namespace types::elf {
  10. using elf32_addr_t = uint32_t;
  11. using elf32_off_t = uint32_t;
  12. using elf_addr_t = elf32_addr_t;
  13. using elf_off_t = elf32_off_t;
  14. constexpr elf32_addr_t ELF_STACK_BOTTOM = 0xbffff000;
  15. constexpr elf32_off_t ELF_STACK_SIZE = 8 * 1024 * 1024;
  16. constexpr elf32_addr_t ELF_STACK_TOP = ELF_STACK_BOTTOM - ELF_STACK_SIZE;
  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. uint32_t flags;
  102. // 0 and 1 for no alignment, otherwise power of 2
  103. uint32_t align;
  104. };
  105. struct PACKED elf32_section_header_entry {
  106. elf32_off_t sh_name;
  107. enum : uint32_t {
  108. SHT_NULL = 0x00,
  109. SHT_PROGBITS = 0x01,
  110. SHT_RELA = 0x04,
  111. SHT_DYNAMIC = 0x06,
  112. SHT_NOTE = 0x07,
  113. SHT_NOBITS = 0x08,
  114. SHT_REL = 0x09,
  115. SHT_DYNSYM = 0x0b,
  116. SHT_INIT_ARRAY = 0x0e,
  117. SHT_FINI_ARRAY = 0x0f,
  118. SHT_PREINIT_ARRAY = 0x0f,
  119. } sh_type;
  120. enum : uint32_t {
  121. SHF_WRITE = 0x01,
  122. SHF_ALLOC = 0x02,
  123. SHF_EXECINSTR = 0x04,
  124. } sh_flags;
  125. elf32_addr_t sh_addr;
  126. elf32_off_t sh_offset;
  127. uint32_t sh_size;
  128. char _[16];
  129. };
  130. struct elf32_load_data {
  131. const fs::dentry* exec_dent;
  132. const char* const* argv;
  133. const char* const* envp;
  134. int errcode;
  135. void* eip;
  136. uint32_t* sp;
  137. bool system;
  138. };
  139. // TODO: environment variables
  140. int elf32_load(elf32_load_data* data);
  141. } // namespace types::elf