elf.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include <common/glib.h>
  3. // Reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-43405.html#scrolltoc
  4. // ====== ELF32 Header中的数据类型定义 ====
  5. typedef uint32_t Elf32_Addr;
  6. typedef uint16_t Elf32_Half;
  7. typedef uint32_t Elf32_Off;
  8. typedef uint32_t Elf32_SWord;
  9. typedef uint32_t Elf32_Word;
  10. // ====== ELF64 Header中的数据类型定义 ====
  11. typedef uint64_t Elf64_Addr;
  12. typedef uint16_t Elf64_Half;
  13. typedef uint64_t Elf64_Off;
  14. typedef uint32_t Elf64_Sword;
  15. typedef uint32_t Elf64_Word;
  16. typedef uint64_t Elf64_Xword;
  17. typedef uint64_t Elf64_Sxword;
  18. // ELF Header中的最大段entry数量
  19. #define EI_NIDENT 16
  20. // ELF e_type的类型定义
  21. #define ET_NONE 0 // No file type
  22. #define ET_REL 1 // Relocatable file
  23. #define ET_EXEC 2 // Executable file
  24. #define ET_DYN 3 // Shared object file
  25. #define ET_CORE 4 // Core file
  26. #define ET_LOPROC 0xff00 // Processor-specific
  27. #define ET_HIPROC 0xffff // Processor-specific
  28. // e_machine的类型定义
  29. #define EM_NONE 0 // No machine
  30. #define EM_SPARC 2 // SPARC
  31. #define EM_386 3 // Intel 80386
  32. #define EM_SPARC32PLUS 18 // Sun SPARC 32+
  33. #define EM_SPARCV9 43 // SPARC V9
  34. #define EM_AMD64 62 // AMD 64
  35. // e_version的类型定义
  36. #define EV_NONE 0 // Invalid Version
  37. // EV_CURRENT: Value>=1 means current version
  38. // e_flags 定义
  39. // e_flags for SPARC
  40. #define EF_SPARC_EXT_MASK 0xffff00 // Vendor Extension mask
  41. #define EF_SPARC_32PLUS 0x000100 // Generic V8+ features
  42. #define EF_SPARC_SUN_US1 0x000200 // Sun UltraSPARC 1 Extensions
  43. #define EF_SPARC_HAL_R1 0x000400 // HAL R1 Extensions
  44. #define EF_SPARC_SUN_US3 0x000800 // Sun UltraSPARC 3 Extensions
  45. #define EF_SPARCV9_MM 0x3 // Mask for Memory Model
  46. #define EF_SPARCV9_TSO 0x0 // Total Store Ordering
  47. #define EF_SPARCV9_PSO 0x1 // Partial Store Ordering
  48. #define EF_SPARCV9_RMO 0x2 // Relaxed Memory Ordering
  49. #define PN_XNUM 0xffff
  50. #define SHN_LORESERVE 0xff00
  51. #define SHN_XINDEX 0xffff
  52. typedef struct
  53. {
  54. unsigned char e_ident[EI_NIDENT];
  55. Elf32_Half e_type;
  56. Elf32_Half e_machine;
  57. Elf32_Word e_version;
  58. Elf32_Addr e_entry;
  59. Elf32_Off e_phoff;
  60. Elf32_Off e_shoff;
  61. Elf32_Word e_flags;
  62. Elf32_Half e_ehsize;
  63. Elf32_Half e_phentsize;
  64. Elf32_Half e_phnum;
  65. Elf32_Half e_shentsize;
  66. Elf32_Half e_shnum;
  67. Elf32_Half e_shstrndx;
  68. } Elf32_Ehdr;
  69. typedef struct
  70. {
  71. unsigned char e_ident[EI_NIDENT];
  72. Elf64_Half e_type; // 文件类型标志符
  73. Elf64_Half e_machine; // 该文件依赖的处理器架构类型
  74. Elf64_Word e_version; // 对象文件的版本
  75. Elf64_Addr e_entry; // 进程的虚拟地址入点,使用字节偏移量表示。如果没有entry point,则该值为0
  76. Elf64_Off e_phoff; // The program header table's file offset in bytes. 若没有,则为0
  77. Elf64_Off e_shoff; // The section header table's file offset in bytes. 若没有,则为0
  78. Elf64_Word e_flags; // 与处理器相关联的flags。格式为: EF_machine_flag 如果是x86架构,那么该值为0
  79. Elf64_Half e_ehsize; // ELF Header的大小(单位:字节)
  80. Elf64_Half e_phentsize; // 程序的program header table中的一个entry的大小(所有的entry大小相同)
  81. Elf64_Half e_phnum; // program header table的entry数量
  82. // e_phentsize*e_phnum=program header table的大小
  83. // 如果没有program header table,该值为0
  84. // 如果entry num>=PN_XNUM(0xffff), 那么该值为0xffff,且真实的pht的entry数量存储在section header的sh_info中(index=0)
  85. // 其他情况下,第一个section header entry的sh_info的值为0
  86. Elf64_Half e_shentsize; // 每个section header的大小(字节
  87. // 每个section header是section header table的一个entry
  88. Elf64_Half e_shnum; // section header table的entry数量
  89. // e_shentsize*e_shnum=section header table的大小
  90. // 如果没有section header table,那么该值为0
  91. // 如果section的数量>=SHN_LORESERVE(0xff00),那么该值为0,且真实的section数量存储在
  92. // section header at index 0的sh_size变量中,否则第一个sh_size为0
  93. Elf64_Half e_shstrndx; // 与section name string表相关联的section header table的entry的索引下标
  94. // 如果没有name string table,那么该值等于SHN_UNDEF
  95. // 如果对应的index>=SHN_LORESERVE(0xff00), 那么该变量值为SHN_XINDEX(0xffff)
  96. // 且真正的section name string table的index被存放在section header的index=0处的sh_link变量中
  97. // 否则初始section header entry的sh_link变量为0
  98. } Elf64_Ehdr;