module.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * File : module.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-01-09 Bernard first version
  23. * 2010-04-09 yi.qiu implement based on first version
  24. */
  25. #ifndef __MODULE_H__
  26. #define __MODULE_H__
  27. #include <rtdef.h>
  28. typedef rt_uint8_t Elf_Byte;
  29. typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */
  30. typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */
  31. typedef rt_int32_t Elf32_Sword; /* Signed large integer */
  32. typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */
  33. typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
  34. /* e_ident[] magic number */
  35. #define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
  36. #define ELFMAG1 'E' /* e_ident[EI_MAG1] */
  37. #define ELFMAG2 'L' /* e_ident[EI_MAG2] */
  38. #define ELFMAG3 'F' /* e_ident[EI_MAG3] */
  39. #define RTMMAG "\177RTM" /* magic */
  40. #define ELFMAG "\177ELF" /* magic */
  41. #define SELFMAG 4 /* size of magic */
  42. #define EI_CLASS 4 /* file class */
  43. #define EI_NIDENT 16 /* Size of e_ident[] */
  44. /* e_ident[] file class */
  45. #define ELFCLASSNONE 0 /* invalid */
  46. #define ELFCLASS32 1 /* 32-bit objs */
  47. #define ELFCLASS64 2 /* 64-bit objs */
  48. #define ELFCLASSNUM 3 /* number of classes */
  49. /* e_ident[] data encoding */
  50. #define ELFDATANONE 0 /* invalid */
  51. #define ELFDATA2LSB 1 /* Little-Endian */
  52. #define ELFDATA2MSB 2 /* Big-Endian */
  53. #define ELFDATANUM 3 /* number of data encode defines */
  54. /* e_ident */
  55. #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
  56. (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
  57. (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
  58. (ehdr).e_ident[EI_MAG3] == ELFMAG3)
  59. #define ET_NONE 0 /* No file type */
  60. #define ET_REL 1 /* Relocatable file */
  61. #define ET_EXEC 2 /* Executable file */
  62. #define ET_DYN 3 /* Shared object file */
  63. #define ET_CORE 4 /* Core file */
  64. /* ELF Header */
  65. typedef struct elfhdr
  66. {
  67. unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
  68. Elf32_Half e_type; /* object file type */
  69. Elf32_Half e_machine; /* machine */
  70. Elf32_Word e_version; /* object file version */
  71. Elf32_Addr e_entry; /* virtual entry point */
  72. Elf32_Off e_phoff; /* program header table offset */
  73. Elf32_Off e_shoff; /* section header table offset */
  74. Elf32_Word e_flags; /* processor-specific flags */
  75. Elf32_Half e_ehsize; /* ELF header size */
  76. Elf32_Half e_phentsize; /* program header entry size */
  77. Elf32_Half e_phnum; /* number of program header entries */
  78. Elf32_Half e_shentsize; /* section header entry size */
  79. Elf32_Half e_shnum; /* number of section header entries */
  80. Elf32_Half e_shstrndx; /* section header table's "section
  81. header string table" entry offset */
  82. } Elf32_Ehdr;
  83. /* Section Header */
  84. typedef struct
  85. {
  86. Elf32_Word sh_name; /* name - index into section header
  87. string table section */
  88. Elf32_Word sh_type; /* type */
  89. Elf32_Word sh_flags; /* flags */
  90. Elf32_Addr sh_addr; /* address */
  91. Elf32_Off sh_offset; /* file offset */
  92. Elf32_Word sh_size; /* section size */
  93. Elf32_Word sh_link; /* section header table index link */
  94. Elf32_Word sh_info; /* extra information */
  95. Elf32_Word sh_addralign; /* address alignment */
  96. Elf32_Word sh_entsize; /* section entry size */
  97. } Elf32_Shdr;
  98. /* Section names */
  99. #define ELF_BSS ".bss" /* uninitialized data */
  100. #define ELF_DATA ".data" /* initialized data */
  101. #define ELF_DEBUG ".debug" /* debug */
  102. #define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
  103. #define ELF_DYNSTR ".dynstr" /* dynamic string table */
  104. #define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
  105. #define ELF_FINI ".fini" /* termination code */
  106. #define ELF_GOT ".got" /* global offset table */
  107. #define ELF_HASH ".hash" /* symbol hash table */
  108. #define ELF_INIT ".init" /* initialization code */
  109. #define ELF_REL_DATA ".rel.data" /* relocation data */
  110. #define ELF_REL_FINI ".rel.fini" /* relocation termination code */
  111. #define ELF_REL_INIT ".rel.init" /* relocation initialization code */
  112. #define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
  113. #define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
  114. #define ELF_REL_TEXT ".rel.text" /* relocation code */
  115. #define ELF_RODATA ".rodata" /* read-only data */
  116. #define ELF_SHSTRTAB ".shstrtab" /* section header string table */
  117. #define ELF_STRTAB ".strtab" /* string table */
  118. #define ELF_SYMTAB ".symtab" /* symbol table */
  119. #define ELF_TEXT ".text" /* code */
  120. #define ELF_RTMSYMTAB "RTMSymTab"
  121. /* Symbol Table Entry */
  122. typedef struct elf32_sym
  123. {
  124. Elf32_Word st_name; /* name - index into string table */
  125. Elf32_Addr st_value; /* symbol value */
  126. Elf32_Word st_size; /* symbol size */
  127. unsigned char st_info; /* type and binding */
  128. unsigned char st_other; /* 0 - no defined meaning */
  129. Elf32_Half st_shndx; /* section header index */
  130. } Elf32_Sym;
  131. #define STB_LOCAL 0 /* BIND */
  132. #define STB_GLOBAL 1
  133. #define STB_WEAK 2
  134. #define STB_NUM 3
  135. #define STB_LOPROC 13 /* processor specific range */
  136. #define STB_HIPROC 15
  137. #define STT_NOTYPE 0 /* symbol type is unspecified */
  138. #define STT_OBJECT 1 /* data object */
  139. #define STT_FUNC 2 /* code object */
  140. #define STT_SECTION 3 /* symbol identifies an ELF section */
  141. #define STT_FILE 4 /* symbol's name is file name */
  142. #define STT_COMMON 5 /* common data object */
  143. #define STT_TLS 6 /* thread-local data object */
  144. #define STT_NUM 7 /* # defined types in generic range */
  145. #define STT_LOOS 10 /* OS specific range */
  146. #define STT_HIOS 12
  147. #define STT_LOPROC 13 /* processor specific range */
  148. #define STT_HIPROC 15
  149. #define STN_UNDEF 0 /* undefined */
  150. #define ELF_ST_BIND(info) ((info) >> 4)
  151. #define ELF_ST_TYPE(info) ((info) & 0xf)
  152. #define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf))
  153. /* Relocation entry with implicit addend */
  154. typedef struct
  155. {
  156. Elf32_Addr r_offset; /* offset of relocation */
  157. Elf32_Word r_info; /* symbol table index and type */
  158. } Elf32_Rel;
  159. /* Relocation entry with explicit addend */
  160. typedef struct
  161. {
  162. Elf32_Addr r_offset; /* offset of relocation */
  163. Elf32_Word r_info; /* symbol table index and type */
  164. Elf32_Sword r_addend;
  165. } Elf32_Rela;
  166. /* Extract relocation info - r_info */
  167. #define ELF32_R_SYM(i) ((i) >> 8)
  168. #define ELF32_R_TYPE(i) ((unsigned char) (i))
  169. #define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
  170. /*
  171. * Relocation type for arm
  172. */
  173. #define R_ARM_NONE 0
  174. #define R_ARM_PC24 1
  175. #define R_ARM_ABS32 2
  176. #define R_ARM_REL32 3
  177. #define R_ARM_THM_CALL 10
  178. #define R_ARM_GLOB_DAT 21
  179. #define R_ARM_JUMP_SLOT 22
  180. #define R_ARM_RELATIVE 23
  181. #define R_ARM_GOT_BREL 26
  182. #define R_ARM_PLT32 27
  183. #define R_ARM_CALL 28
  184. #define R_ARM_JUMP24 29
  185. #define R_ARM_THM_JUMP24 30
  186. #define R_ARM_V4BX 40
  187. /* Program Header */
  188. typedef struct
  189. {
  190. Elf32_Word p_type; /* segment type */
  191. Elf32_Off p_offset; /* segment offset */
  192. Elf32_Addr p_vaddr; /* virtual address of segment */
  193. Elf32_Addr p_paddr; /* physical address - ignored? */
  194. Elf32_Word p_filesz; /* number of bytes in file for seg. */
  195. Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
  196. Elf32_Word p_flags; /* flags */
  197. Elf32_Word p_align; /* memory alignment */
  198. } Elf32_Phdr;
  199. /* p_type */
  200. #define PT_LOAD 1
  201. /* p_flags */
  202. #define PF_X 1
  203. #define PF_W 2
  204. #define PF_R 4
  205. /* sh_type */
  206. #define SHT_NULL 0 /* inactive */
  207. #define SHT_PROGBITS 1 /* program defined information */
  208. #define SHT_SYMTAB 2 /* symbol table section */
  209. #define SHT_STRTAB 3 /* string table section */
  210. #define SHT_RELA 4 /* relocation section with addends*/
  211. #define SHT_HASH 5 /* symbol hash table section */
  212. #define SHT_DYNAMIC 6 /* dynamic section */
  213. #define SHT_NOTE 7 /* note section */
  214. #define SHT_NOBITS 8 /* no space section */
  215. #define SHT_REL 9 /* relation section without addends */
  216. #define SHT_SHLIB 10 /* reserved - purpose unknown */
  217. #define SHT_DYNSYM 11 /* dynamic symbol table section */
  218. #define SHT_NUM 12 /* number of section types */
  219. #define SHT_LOPROC 0x70000000 /* reserved range for processor */
  220. #define SHT_HIPROC 0x7fffffff /* specific section header types */
  221. #define SHT_LOUSER 0x80000000 /* reserved range for application */
  222. #define SHT_HIUSER 0xffffffff /* specific indexes */
  223. /* Section Attribute Flags - sh_flags */
  224. #define SHF_WRITE 0x1 /* Writable */
  225. #define SHF_ALLOC 0x2 /* occupies memory */
  226. #define SHF_EXECINSTR 0x4 /* executable */
  227. #define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
  228. /* specific section attributes */
  229. #endif