module.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * File : module.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-01-09 Bernard first version
  13. * 2010-04-09 yi.qiu implement based on first version
  14. */
  15. #include <rtm.h>
  16. #include <rtthread.h>
  17. #include "module.h"
  18. #include "kservice.h"
  19. /* #define RT_MODULE_DEBUG */
  20. #define elf_module ((Elf32_Ehdr *)module_ptr)
  21. #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
  22. #define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
  23. #define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
  24. #define IS_REL(s) (s.sh_type == SHT_REL)
  25. #define IS_RELA(s) (s.sh_type == SHT_RELA)
  26. #define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
  27. #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
  28. #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
  29. #ifdef RT_USING_MODULE
  30. rt_list_t rt_module_symbol_list;
  31. struct rt_module* rt_current_module;
  32. struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
  33. void rt_system_module_init()
  34. {
  35. #ifdef __CC_ARM
  36. extern int RTMSymTab$$Base;
  37. extern int RTMSymTab$$Limit;
  38. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  39. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  40. #elif defined(__GNUC__)
  41. extern int __rtmsymtab_start;
  42. extern int __rtmsymtab_end;
  43. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  44. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  45. #endif
  46. rt_list_init(&rt_module_symbol_list);
  47. }
  48. rt_uint32_t rt_module_symbol_find(const rt_uint8_t* sym_str)
  49. {
  50. /* find in kernel symbol table */
  51. struct rt_module_symtab* index;
  52. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  53. {
  54. if (strcmp(index->name, (const char*)sym_str) == 0)
  55. return index->addr;
  56. }
  57. return 0;
  58. }
  59. int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf32_Addr sym_val, rt_uint32_t module_addr)
  60. {
  61. Elf32_Addr *where, tmp;
  62. Elf32_Sword addend;
  63. where = (Elf32_Addr *)((rt_uint8_t*)module->module_space + rel->r_offset - module_addr);
  64. switch (ELF32_R_TYPE(rel->r_info))
  65. {
  66. case R_ARM_NONE:
  67. break;
  68. case R_ARM_ABS32:
  69. *where += (Elf32_Addr)sym_val;
  70. #ifdef RT_MODULE_DEBUG
  71. rt_kprintf("R_ARM_ABS32: %x -> %x\n", where, *where);
  72. #endif
  73. break;
  74. case R_ARM_PC24:
  75. case R_ARM_PLT32:
  76. case R_ARM_CALL:
  77. case R_ARM_JUMP24:
  78. addend = *where & 0x00ffffff;
  79. if (addend & 0x00800000)
  80. addend |= 0xff000000;
  81. tmp = sym_val - (Elf32_Addr)where + (addend << 2);
  82. tmp >>= 2;
  83. *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
  84. #ifdef RT_MODULE_DEBUG
  85. rt_kprintf("R_ARM_PC24: %x -> %x\n", where, *where);
  86. #endif
  87. break;
  88. default:
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. static void rt_module_init_object_container(struct rt_module* module)
  94. {
  95. RT_ASSERT(module != RT_NULL);
  96. /* init object container - thread */
  97. rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
  98. module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
  99. module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;
  100. #ifdef RT_USING_SEMAPHORE
  101. /* init object container - semaphore */
  102. rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
  103. module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
  104. module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
  105. #endif
  106. #ifdef RT_USING_MUTEX
  107. /* init object container - mutex */
  108. rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
  109. module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
  110. module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
  111. #endif
  112. #ifdef RT_USING_FASTEVENT
  113. /* init object container - fast event */
  114. rt_list_init(&(module->module_object[RT_Object_Class_FastEvent].object_list));
  115. module->module_object[RT_Object_Class_FastEvent].object_size = sizeof(struct rt_fast_event);
  116. module->module_object[RT_Object_Class_FastEvent].type = RT_Object_Class_FastEvent;
  117. #endif
  118. #ifdef RT_USING_EVENT
  119. /* init object container - event */
  120. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  121. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  122. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  123. #endif
  124. #ifdef RT_USING_MAILBOX
  125. /* init object container - mailbox */
  126. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  127. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  128. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  129. #endif
  130. #ifdef RT_USING_MESSAGEQUEUE
  131. /* init object container - message queue */
  132. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  133. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  134. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  135. #endif
  136. #ifdef RT_USING_MEMPOOL
  137. /* init object container - memory pool */
  138. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  139. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  140. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  141. #endif
  142. #ifdef RT_USING_DEVICE
  143. /* init object container - device */
  144. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  145. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  146. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  147. #endif
  148. /* init object container - timer */
  149. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  150. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  151. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  152. }
  153. struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name)
  154. {
  155. rt_uint32_t index;
  156. rt_uint32_t module_addr = 0, module_size = 0;
  157. struct rt_module* module = RT_NULL;
  158. rt_uint8_t *ptr, *strtab, *shstrab;
  159. /* check ELF header */
  160. if (rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0 ||
  161. elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  162. return RT_NULL;
  163. /* get the ELF image size */
  164. for (index = 0; index < elf_module->e_shnum; index++)
  165. {
  166. /* text */
  167. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  168. {
  169. module_size += shdr[index].sh_size;
  170. module_addr = shdr[index].sh_addr;
  171. }
  172. /* rodata */
  173. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  174. {
  175. module_size += shdr[index].sh_size;
  176. }
  177. /* data */
  178. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  179. {
  180. module_size += shdr[index].sh_size;
  181. }
  182. /* bss */
  183. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  184. {
  185. module_size += shdr[index].sh_size;
  186. }
  187. }
  188. /* no text, data and bss on image */
  189. if (module_size == 0) return module;
  190. /* allocate module */
  191. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char*)name);
  192. if (module == RT_NULL) return module;
  193. /* allocate module space */
  194. module->module_space = rt_malloc(module_size);
  195. if (module->module_space == RT_NULL)
  196. {
  197. rt_object_delete(&(module->parent));
  198. return RT_NULL;
  199. }
  200. /* zero all space */
  201. ptr = module->module_space;
  202. rt_memset(ptr, 0, module_size);
  203. /* load text and data section */
  204. for (index = 0; index < elf_module->e_shnum; index++)
  205. {
  206. /* load text and rodata section */
  207. if (IS_PROG(shdr[index]) && (IS_AX(shdr[index])||IS_ALLOC(shdr[index])))
  208. {
  209. rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
  210. ptr += shdr[index].sh_size;
  211. }
  212. /* load data section */
  213. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  214. {
  215. module->module_data = (rt_uint32_t)ptr;
  216. rt_memset(ptr, 0, shdr[index].sh_size);
  217. ptr += shdr[index].sh_size;
  218. }
  219. }
  220. /* set module entry */
  221. module->module_entry = (rt_uint8_t*)module->module_space + elf_module->e_entry - module_addr;
  222. /* handle relocation section */
  223. for (index = 0; index < elf_module->e_shnum; index ++)
  224. {
  225. if (IS_REL(shdr[index]))
  226. {
  227. rt_uint32_t i, nr_reloc;
  228. Elf32_Sym *symtab;
  229. Elf32_Rel *rel;
  230. /* get relocate item */
  231. rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);
  232. /* locate .dynsym and .dynstr */
  233. symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset);
  234. strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  235. shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
  236. nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
  237. /* relocate every items */
  238. for (i = 0; i < nr_reloc; i ++)
  239. {
  240. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  241. #ifdef RT_MODULE_DEBUG
  242. rt_kprintf("relocate symbol: %s\n", strtab + sym->st_name);
  243. #endif
  244. if (sym->st_shndx != STN_UNDEF)
  245. {
  246. if(ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  247. {
  248. if (strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_RODATA, 8) == 0)
  249. {
  250. /* relocate rodata section, fix me, module_ptr should be freed */
  251. rt_module_arm_relocate(module, rel,
  252. (Elf32_Addr)((rt_uint8_t*)module_ptr + shdr[sym->st_shndx].sh_offset),
  253. module_addr);
  254. }
  255. else if(strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_BSS, 5) == 0)
  256. {
  257. /* relocate bss section */
  258. rt_module_arm_relocate(module, rel, (Elf32_Addr)ptr, module_addr);
  259. }
  260. }
  261. else if(ELF_ST_TYPE(sym->st_info) == STT_FUNC )
  262. {
  263. /* relocate function */
  264. rt_module_arm_relocate(module, rel,
  265. (Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value),
  266. module_addr);
  267. }
  268. else if(ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  269. {
  270. /* relocate object, fix me, module_ptr should be freed */
  271. rt_module_arm_relocate(module, rel,
  272. (Elf32_Addr)((rt_uint8_t*)module_ptr + shdr[sym->st_shndx].sh_offset + sym->st_value),
  273. module_addr);
  274. }
  275. }
  276. else
  277. {
  278. #ifdef RT_MODULE_DEBUG
  279. rt_kprintf("unresolved relocate symbol: %s\n", strtab + sym->st_name);
  280. #endif
  281. /* need to resolve symbol in kernel symbol table */
  282. Elf32_Addr addr = rt_module_symbol_find(strtab + sym->st_name);
  283. if (addr != (Elf32_Addr)RT_NULL)
  284. rt_module_arm_relocate(module, rel, addr, module_addr);
  285. else rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
  286. }
  287. rel ++;
  288. }
  289. }
  290. }
  291. /* init module object container */
  292. rt_module_init_object_container(module);
  293. /* create module main thread */
  294. module->module_thread = rt_thread_create((const char*)name,
  295. module->module_entry, RT_NULL,
  296. 512, 90, 10);
  297. module->module_thread->module_parent = module;
  298. rt_thread_startup(module->module_thread);
  299. return module;
  300. }
  301. void rt_module_unload(struct rt_module* module)
  302. {
  303. struct rt_object* object;
  304. /* suspend module main thread */
  305. if (module->module_thread->stat == RT_THREAD_READY)
  306. rt_thread_suspend(module->module_thread);
  307. /* delete all module object */
  308. /* release module memory */
  309. }
  310. rt_module_t rt_module_find(char* name)
  311. {
  312. struct rt_module* module;
  313. module = (struct rt_module*)rt_object_find(RT_Object_Class_Module, name);
  314. return module;
  315. }
  316. #endif