elf.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #define ELFMAG0 0x7f
  3. #define ELFMAG1 'E'
  4. #define ELFMAG2 'L'
  5. #define ELFMAG3 'F'
  6. #define EI_NIDENT 16
  7. typedef uint32_t Elf32_Word;
  8. typedef uint32_t Elf32_Addr;
  9. typedef uint32_t Elf32_Off;
  10. typedef uint32_t Elf32_Sword;
  11. typedef uint16_t Elf32_Half;
  12. /*
  13. * ELF Header
  14. */
  15. typedef struct {
  16. unsigned char e_ident[EI_NIDENT];
  17. Elf32_Half e_type;
  18. Elf32_Half e_machine;
  19. Elf32_Word e_version;
  20. Elf32_Addr e_entry;
  21. Elf32_Off e_phoff;
  22. Elf32_Off e_shoff;
  23. Elf32_Word e_flags;
  24. Elf32_Half e_ehsize;
  25. Elf32_Half e_phentsize;
  26. Elf32_Half e_phnum;
  27. Elf32_Half e_shentsize;
  28. Elf32_Half e_shnum;
  29. Elf32_Half e_shstrndx;
  30. } Elf32_Header;
  31. /*
  32. * e_type
  33. */
  34. #define ET_NONE 0 /* No file type */
  35. #define ET_REL 1 /* Relocatable file */
  36. #define ET_EXEC 2 /* Executable file */
  37. #define ET_DYN 3 /* Shared object file */
  38. #define ET_CORE 4 /* Core file */
  39. #define ET_LOPROC 0xff0 /* [Processor Specific] */
  40. #define ET_HIPROC 0xfff /* [Processor Specific] */
  41. /*
  42. * Machine types
  43. */
  44. #define EM_NONE 0
  45. #define EM_386 3
  46. #define EV_NONE 0
  47. #define EV_CURRENT 1
  48. /** Program Header */
  49. typedef struct {
  50. Elf32_Word p_type;
  51. Elf32_Off p_offset;
  52. Elf32_Addr p_vaddr;
  53. Elf32_Addr p_paddr;
  54. Elf32_Word p_filesz;
  55. Elf32_Word p_memsz;
  56. Elf32_Word p_flags;
  57. Elf32_Word p_align;
  58. } Elf32_Phdr;
  59. /* p_type values */
  60. #define PT_NULL 0 /* Unused, skip me */
  61. #define PT_LOAD 1 /* Loadable segment */
  62. #define PT_DYNAMIC 2 /* Dynamic linking information */
  63. #define PT_INTERP 3 /* Interpreter (null-terminated string, pathname) */
  64. #define PT_NOTE 4 /* Auxillary information */
  65. #define PT_SHLIB 5 /* Reserved. */
  66. #define PT_PHDR 6 /* Oh, it's me. Hello! Back-reference to the header table itself */
  67. #define PT_LOPROC 0x70000000
  68. #define PT_HIPROC 0x7FFFFFFF
  69. /** Section Header */
  70. typedef struct {
  71. Elf32_Word sh_name;
  72. Elf32_Word sh_type;
  73. Elf32_Word sh_flags;
  74. Elf32_Addr sh_addr;
  75. Elf32_Off sh_offset;
  76. Elf32_Word sh_size;
  77. Elf32_Word sh_link;
  78. Elf32_Word sh_info;
  79. Elf32_Word sh_addralign;
  80. Elf32_Word sh_entsize;
  81. } Elf32_Shdr;
  82. typedef struct {
  83. uint32_t id;
  84. uintptr_t ptr;
  85. } Elf32_auxv;
  86. typedef struct {
  87. Elf32_Word st_name;
  88. Elf32_Addr st_value;
  89. Elf32_Word st_size;
  90. unsigned char st_info;
  91. unsigned char st_other;
  92. Elf32_Half st_shndx;
  93. } Elf32_Sym;
  94. typedef struct {
  95. Elf32_Addr r_offset;
  96. Elf32_Word r_info;
  97. } Elf32_Rel;
  98. typedef struct {
  99. Elf32_Sword d_tag;
  100. union {
  101. Elf32_Word d_val;
  102. Elf32_Addr d_ptr;
  103. Elf32_Off d_off;
  104. } d_un;
  105. } Elf32_Dyn;
  106. /* sh_type values */
  107. #define SHT_NONE 0
  108. #define SHT_PROGBITS 1
  109. #define SHT_SYMTAB 2
  110. #define SHT_STRTAB 3
  111. #define SHT_NOBITS 8
  112. #define SHT_REL 9
  113. #define ELF32_R_SYM(i) ((i) >> 8)
  114. #define ELF32_R_TYPE(i) ((unsigned char)(i))
  115. #define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
  116. #define ELF32_ST_BIND(i) ((i) >> 4)
  117. #define ELF32_ST_TYPE(i) ((i) & 0xf)
  118. #define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
  119. #define STB_LOCAL 0
  120. #define STB_GLOBAL 1
  121. #define STB_WEAK 2
  122. #define STB_NUM 3
  123. #define STB_LOPROC 13
  124. #define STB_HIPROC 15
  125. #define STT_NOTYPE 0
  126. #define STT_OBJECT 1
  127. #define STT_FUNC 2
  128. #define STT_SECTION 3
  129. #define STT_FILE 4
  130. #define STT_COMMON 5
  131. #define STT_TLS 6
  132. #define STT_NUM 7
  133. #define STT_LOPROC 13
  134. #define STT_HIPROC 15