module.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef __MODULE_H__
  2. #define __MODULE_H__
  3. #include <rtdef.h>
  4. typedef rt_uint8_t Elf_Byte;
  5. typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */
  6. typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */
  7. typedef rt_int32_t Elf32_Sword; /* Signed large integer */
  8. typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */
  9. typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
  10. /* e_ident[] magic number */
  11. #define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
  12. #define ELFMAG1 'E' /* e_ident[EI_MAG1] */
  13. #define ELFMAG2 'L' /* e_ident[EI_MAG2] */
  14. #define ELFMAG3 'F' /* e_ident[EI_MAG3] */
  15. #define ELFMAG "\177ELF" /* magic */
  16. #define SELFMAG 4 /* size of magic */
  17. #define EI_CLASS 4 /* file class */
  18. #define EI_NIDENT 16 /* Size of e_ident[] */
  19. /* e_ident[] file class */
  20. #define ELFCLASSNONE 0 /* invalid */
  21. #define ELFCLASS32 1 /* 32-bit objs */
  22. #define ELFCLASS64 2 /* 64-bit objs */
  23. #define ELFCLASSNUM 3 /* number of classes */
  24. /* e_ident[] data encoding */
  25. #define ELFDATANONE 0 /* invalid */
  26. #define ELFDATA2LSB 1 /* Little-Endian */
  27. #define ELFDATA2MSB 2 /* Big-Endian */
  28. #define ELFDATANUM 3 /* number of data encode defines */
  29. /* e_ident */
  30. #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
  31. (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
  32. (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
  33. (ehdr).e_ident[EI_MAG3] == ELFMAG3)
  34. /* ELF Header */
  35. typedef struct elfhdr {
  36. unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
  37. Elf32_Half e_type; /* object file type */
  38. Elf32_Half e_machine; /* machine */
  39. Elf32_Word e_version; /* object file version */
  40. Elf32_Addr e_entry; /* virtual entry point */
  41. Elf32_Off e_phoff; /* program header table offset */
  42. Elf32_Off e_shoff; /* section header table offset */
  43. Elf32_Word e_flags; /* processor-specific flags */
  44. Elf32_Half e_ehsize; /* ELF header size */
  45. Elf32_Half e_phentsize; /* program header entry size */
  46. Elf32_Half e_phnum; /* number of program header entries */
  47. Elf32_Half e_shentsize; /* section header entry size */
  48. Elf32_Half e_shnum; /* number of section header entries */
  49. Elf32_Half e_shstrndx; /* section header table's "section
  50. header string table" entry offset */
  51. } Elf32_Ehdr;
  52. /* Section Header */
  53. typedef struct {
  54. Elf32_Word sh_name; /* name - index into section header
  55. string table section */
  56. Elf32_Word sh_type; /* type */
  57. Elf32_Word sh_flags; /* flags */
  58. Elf32_Addr sh_addr; /* address */
  59. Elf32_Off sh_offset; /* file offset */
  60. Elf32_Word sh_size; /* section size */
  61. Elf32_Word sh_link; /* section header table index link */
  62. Elf32_Word sh_info; /* extra information */
  63. Elf32_Word sh_addralign; /* address alignment */
  64. Elf32_Word sh_entsize; /* section entry size */
  65. } Elf32_Shdr;
  66. /* Section names */
  67. #define ELF_BSS ".bss" /* uninitialized data */
  68. #define ELF_DATA ".data" /* initialized data */
  69. #define ELF_DEBUG ".debug" /* debug */
  70. #define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
  71. #define ELF_DYNSTR ".dynstr" /* dynamic string table */
  72. #define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
  73. #define ELF_FINI ".fini" /* termination code */
  74. #define ELF_GOT ".got" /* global offset table */
  75. #define ELF_HASH ".hash" /* symbol hash table */
  76. #define ELF_INIT ".init" /* initialization code */
  77. #define ELF_REL_DATA ".rel.data" /* relocation data */
  78. #define ELF_REL_FINI ".rel.fini" /* relocation termination code */
  79. #define ELF_REL_INIT ".rel.init" /* relocation initialization code */
  80. #define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
  81. #define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
  82. #define ELF_REL_TEXT ".rel.text" /* relocation code */
  83. #define ELF_RODATA ".rodata" /* read-only data */
  84. #define ELF_SHSTRTAB ".shstrtab" /* section header string table */
  85. #define ELF_STRTAB ".strtab" /* string table */
  86. #define ELF_SYMTAB ".symtab" /* symbol table */
  87. #define ELF_TEXT ".text" /* code */
  88. /* Symbol Table Entry */
  89. typedef struct elf32_sym {
  90. Elf32_Word st_name; /* name - index into string table */
  91. Elf32_Addr st_value; /* symbol value */
  92. Elf32_Word st_size; /* symbol size */
  93. unsigned char st_info; /* type and binding */
  94. unsigned char st_other; /* 0 - no defined meaning */
  95. Elf32_Half st_shndx; /* section header index */
  96. } Elf32_Sym;
  97. #define STB_LOCAL 0 /* BIND */
  98. #define STB_GLOBAL 1
  99. #define STB_WEAK 2
  100. #define STB_NUM 3
  101. #define STB_LOPROC 13 /* processor specific range */
  102. #define STB_HIPROC 15
  103. #define STT_NOTYPE 0 /* symbol type is unspecified */
  104. #define STT_OBJECT 1 /* data object */
  105. #define STT_FUNC 2 /* code object */
  106. #define STT_SECTION 3 /* symbol identifies an ELF section */
  107. #define STT_FILE 4 /* symbol's name is file name */
  108. #define STT_COMMON 5 /* common data object */
  109. #define STT_TLS 6 /* thread-local data object */
  110. #define STT_NUM 7 /* # defined types in generic range */
  111. #define STT_LOOS 10 /* OS specific range */
  112. #define STT_HIOS 12
  113. #define STT_LOPROC 13 /* processor specific range */
  114. #define STT_HIPROC 15
  115. #define ELF_ST_BIND(info) ((info) >> 4)
  116. #define ELF_ST_TYPE(info) ((info) & 0xf)
  117. #define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf))
  118. /* Relocation entry with implicit addend */
  119. typedef struct {
  120. Elf32_Addr r_offset; /* offset of relocation */
  121. Elf32_Word r_info; /* symbol table index and type */
  122. } Elf32_Rel;
  123. /* Relocation entry with explicit addend */
  124. typedef struct {
  125. Elf32_Addr r_offset; /* offset of relocation */
  126. Elf32_Word r_info; /* symbol table index and type */
  127. Elf32_Sword r_addend;
  128. } Elf32_Rela;
  129. /* Extract relocation info - r_info */
  130. #define ELF32_R_SYM(i) ((i) >> 8)
  131. #define ELF32_R_TYPE(i) ((unsigned char) (i))
  132. #define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
  133. /*
  134. * Relocation type for arm
  135. */
  136. #define R_ARM_NONE 0
  137. #define R_ARM_PC24 1
  138. #define R_ARM_ABS32 2
  139. #define R_ARM_PLT32 27
  140. #define R_ARM_CALL 28
  141. #define R_ARM_JUMP24 29
  142. /* Program Header */
  143. typedef struct {
  144. Elf32_Word p_type; /* segment type */
  145. Elf32_Off p_offset; /* segment offset */
  146. Elf32_Addr p_vaddr; /* virtual address of segment */
  147. Elf32_Addr p_paddr; /* physical address - ignored? */
  148. Elf32_Word p_filesz; /* number of bytes in file for seg. */
  149. Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
  150. Elf32_Word p_flags; /* flags */
  151. Elf32_Word p_align; /* memory alignment */
  152. } Elf32_Phdr;
  153. /* sh_type */
  154. #define SHT_NULL 0 /* inactive */
  155. #define SHT_PROGBITS 1 /* program defined information */
  156. #define SHT_SYMTAB 2 /* symbol table section */
  157. #define SHT_STRTAB 3 /* string table section */
  158. #define SHT_RELA 4 /* relocation section with addends*/
  159. #define SHT_HASH 5 /* symbol hash table section */
  160. #define SHT_DYNAMIC 6 /* dynamic section */
  161. #define SHT_NOTE 7 /* note section */
  162. #define SHT_NOBITS 8 /* no space section */
  163. #define SHT_REL 9 /* relation section without addends */
  164. #define SHT_SHLIB 10 /* reserved - purpose unknown */
  165. #define SHT_DYNSYM 11 /* dynamic symbol table section */
  166. #define SHT_NUM 12 /* number of section types */
  167. #define SHT_LOPROC 0x70000000 /* reserved range for processor */
  168. #define SHT_HIPROC 0x7fffffff /* specific section header types */
  169. #define SHT_LOUSER 0x80000000 /* reserved range for application */
  170. #define SHT_HIUSER 0xffffffff /* specific indexes */
  171. /* Section Attribute Flags - sh_flags */
  172. #define SHF_WRITE 0x1 /* Writable */
  173. #define SHF_ALLOC 0x2 /* occupies memory */
  174. #define SHF_EXECINSTR 0x4 /* executable */
  175. #define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
  176. /* specific section attributes */
  177. /* Symbol table index */
  178. #define STN_UNDEF 0 /* undefined */
  179. #endif