module.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "string.h"
  18. #include "kservice.h"
  19. #define RT_MODULE_DEBUG
  20. #ifdef RT_USING_MODULE
  21. #include "module.h"
  22. #define elf_module ((Elf32_Ehdr *)module_ptr)
  23. #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
  24. #define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
  25. #define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
  26. #define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
  27. #define IS_REL(s) (s.sh_type == SHT_REL)
  28. #define IS_RELA(s) (s.sh_type == SHT_RELA)
  29. #define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
  30. #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
  31. #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
  32. struct rt_module* rt_current_module;
  33. int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf32_Addr sym_val)
  34. {
  35. Elf32_Addr *where, tmp;
  36. Elf32_Sword addend;
  37. where = (Elf32_Addr *)((rt_uint8_t*)module->module_space + rel->r_offset);
  38. switch (ELF32_R_TYPE(rel->r_info))
  39. {
  40. case R_ARM_NONE:
  41. break;
  42. case R_ARM_ABS32:
  43. *where += (Elf32_Addr)sym_val;
  44. #ifdef RT_MODULE_DEBUG
  45. rt_kprintf("R_ARM_ABS32: %x -> %x\n", where, *where);
  46. #endif
  47. break;
  48. case R_ARM_PC24:
  49. case R_ARM_PLT32:
  50. case R_ARM_CALL:
  51. case R_ARM_JUMP24:
  52. addend = *where & 0x00ffffff;
  53. if (addend & 0x00800000)
  54. addend |= 0xff000000;
  55. tmp = sym_val - (Elf32_Addr)where + (addend << 2);
  56. tmp >>= 2;
  57. *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
  58. #ifdef RT_MODULE_DEBUG
  59. rt_kprintf("R_ARM_PC24: %x -> %x\n", where, *where);
  60. #endif
  61. break;
  62. case R_ARM_V4BX:
  63. *where &= 0xf000000f;
  64. *where |= 0x01a0f000;
  65. break;
  66. case R_ARM_GLOB_DAT:
  67. *where += (Elf32_Addr)sym_val;
  68. break;
  69. case R_ARM_JUMP_SLOT:
  70. break;
  71. default:
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static void rt_module_init_object_container(struct rt_module* module)
  77. {
  78. RT_ASSERT(module != RT_NULL);
  79. /* init object container - thread */
  80. rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
  81. module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
  82. module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;
  83. #ifdef RT_USING_SEMAPHORE
  84. /* init object container - semaphore */
  85. rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
  86. module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
  87. module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
  88. #endif
  89. #ifdef RT_USING_MUTEX
  90. /* init object container - mutex */
  91. rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
  92. module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
  93. module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
  94. #endif
  95. #ifdef RT_USING_EVENT
  96. /* init object container - event */
  97. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  98. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  99. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  100. #endif
  101. #ifdef RT_USING_MAILBOX
  102. /* init object container - mailbox */
  103. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  104. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  105. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  106. #endif
  107. #ifdef RT_USING_MESSAGEQUEUE
  108. /* init object container - message queue */
  109. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  110. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  111. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  112. #endif
  113. #ifdef RT_USING_MEMPOOL
  114. /* init object container - memory pool */
  115. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  116. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  117. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  118. #endif
  119. #ifdef RT_USING_DEVICE
  120. /* init object container - device */
  121. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  122. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  123. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  124. #endif
  125. /* init object container - timer */
  126. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  127. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  128. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  129. }
  130. struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name)
  131. {
  132. rt_uint32_t index;
  133. rt_uint32_t module_size = 0;
  134. struct rt_module* module = RT_NULL;
  135. rt_uint8_t *ptr, *strtab, *shstrab;
  136. #ifdef RT_MODULE_DEBUG
  137. rt_kprintf("rt_module_load: %s\n", name);
  138. #endif
  139. /* check ELF header */
  140. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 ||
  141. elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  142. {
  143. rt_kprintf(" wrong magic\n");
  144. return RT_NULL;
  145. }
  146. /* get the ELF image size */
  147. for (index = 0; index < elf_module->e_phnum; index++)
  148. {
  149. if(phdr[index].p_type == PT_LOAD)
  150. module_size += phdr[index].p_memsz;
  151. }
  152. if (module_size == 0) return module;
  153. /* allocate module */
  154. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char*)name);
  155. if (module == RT_NULL) return RT_NULL;
  156. /* allocate module space */
  157. module->module_space = rt_malloc(module_size);
  158. if (module->module_space == RT_NULL)
  159. {
  160. rt_object_delete(&(module->parent));
  161. return RT_NULL;
  162. }
  163. /* zero all space */
  164. ptr = module->module_space;
  165. rt_memset(ptr, 0, module_size);
  166. for (index = 0; index < elf_module->e_phnum; index++)
  167. {
  168. if(phdr[index].p_type == PT_LOAD)
  169. {
  170. rt_memcpy(ptr, (rt_uint8_t*)elf_module + phdr[index].p_offset, phdr[index].p_filesz);
  171. ptr += phdr[index].p_memsz;
  172. }
  173. }
  174. /* set module entry */
  175. module->module_entry = (rt_uint8_t*)module->module_space + elf_module->e_entry;
  176. /* handle relocation section */
  177. for (index = 0; index < elf_module->e_shnum; index ++)
  178. {
  179. if (IS_REL(shdr[index]))
  180. {
  181. rt_uint32_t i, nr_reloc;
  182. Elf32_Sym *symtab;
  183. Elf32_Rel *rel;
  184. /* get relocate item */
  185. rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);
  186. /* locate .rel.plt and .rel.dyn */
  187. symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset);
  188. strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  189. nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
  190. /* relocate every items */
  191. for (i = 0; i < nr_reloc; i ++)
  192. {
  193. Elf32_Addr addr = 0;
  194. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  195. #ifdef RT_MODULE_DEBUG
  196. rt_kprintf("relocate symbol %s\n", strtab + sym->st_name);
  197. #endif
  198. rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t*)module->module_space + sym->st_value));
  199. rel ++;
  200. }
  201. }
  202. }
  203. /* init module object container */
  204. rt_module_init_object_container(module);
  205. module->module_thread = rt_thread_create(name,
  206. module->module_entry, RT_NULL,
  207. 512, 90, 10);
  208. module->module_thread->module_parent = module;
  209. rt_thread_startup(module->module_thread);
  210. rt_free(module_ptr);
  211. return module;
  212. }
  213. void rt_module_unload(struct rt_module* module)
  214. {
  215. int i;
  216. struct rt_object* object;
  217. struct rt_list_node *list, *node;
  218. /* check parameter */
  219. RT_ASSERT(module != RT_NULL);
  220. /* suspend module main thread */
  221. if (module->module_thread->stat == RT_THREAD_READY)
  222. rt_thread_suspend(module->module_thread);
  223. /* delete all module object */
  224. for(i = RT_Object_Class_Thread; i < RT_Object_Class_Module; i++)
  225. {
  226. list = &module->module_object[i].object_list;
  227. for (node = list->next; node != list; node = node->next)
  228. {
  229. object = rt_list_entry(node, struct rt_object, list);
  230. if (rt_object_is_systemobject(object) == RT_EOK)
  231. {
  232. /* detach static objcet */
  233. rt_object_detach(object);
  234. }
  235. else
  236. {
  237. /* delete dynamic object */
  238. rt_object_delete(object);
  239. }
  240. }
  241. }
  242. /* release module memory */
  243. rt_free(module->module_space);
  244. rt_object_delete((struct rt_object *)module);
  245. }
  246. rt_module_t rt_module_find(char* name)
  247. {
  248. struct rt_object_information *information;
  249. struct rt_object* object;
  250. struct rt_list_node* node;
  251. extern struct rt_object_information rt_object_container[];
  252. /* enter critical */
  253. rt_enter_critical();
  254. /* try to find device object */
  255. information = &rt_object_container[RT_Object_Class_Thread];
  256. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  257. {
  258. object = rt_list_entry(node, struct rt_object, list);
  259. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  260. {
  261. /* leave critical */
  262. rt_exit_critical();
  263. return (rt_module_t)object;
  264. }
  265. }
  266. /* leave critical */
  267. rt_exit_critical();
  268. /* not found */
  269. return RT_NULL;
  270. }
  271. #endif