module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_EVENT
  113. /* init object container - event */
  114. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  115. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  116. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  117. #endif
  118. #ifdef RT_USING_MAILBOX
  119. /* init object container - mailbox */
  120. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  121. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  122. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  123. #endif
  124. #ifdef RT_USING_MESSAGEQUEUE
  125. /* init object container - message queue */
  126. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  127. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  128. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  129. #endif
  130. #ifdef RT_USING_MEMPOOL
  131. /* init object container - memory pool */
  132. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  133. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  134. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  135. #endif
  136. #ifdef RT_USING_DEVICE
  137. /* init object container - device */
  138. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  139. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  140. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  141. #endif
  142. /* init object container - timer */
  143. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  144. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  145. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  146. }
  147. struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name)
  148. {
  149. rt_uint32_t index;
  150. rt_uint32_t module_addr = 0, module_size = 0, rodata_addr = 0;
  151. struct rt_module* module = RT_NULL;
  152. rt_uint8_t *ptr, *strtab, *shstrab;
  153. #ifdef RT_MODULE_DEBUG
  154. rt_kprintf("rt_module_load: %s\n", name);
  155. #endif
  156. /* check ELF header */
  157. if (rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0 ||
  158. elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  159. return RT_NULL;
  160. /* get the ELF image size */
  161. for (index = 0; index < elf_module->e_shnum; index++)
  162. {
  163. /* text */
  164. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  165. {
  166. module_size += shdr[index].sh_size;
  167. module_addr = shdr[index].sh_addr;
  168. }
  169. /* rodata */
  170. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  171. {
  172. module_size += shdr[index].sh_size;
  173. }
  174. /* data */
  175. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  176. {
  177. module_size += shdr[index].sh_size;
  178. }
  179. /* bss */
  180. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  181. {
  182. module_size += shdr[index].sh_size;
  183. }
  184. }
  185. /* no text, data and bss on image */
  186. if (module_size == 0) return module;
  187. /* allocate module */
  188. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char*)name);
  189. if (module == RT_NULL) return module;
  190. /* allocate module space */
  191. module->module_space = rt_malloc(module_size);
  192. if (module->module_space == RT_NULL)
  193. {
  194. rt_object_delete(&(module->parent));
  195. return RT_NULL;
  196. }
  197. /* zero all space */
  198. ptr = module->module_space;
  199. rt_memset(ptr, 0, module_size);
  200. /* load text and data section */
  201. for (index = 0; index < elf_module->e_shnum; index++)
  202. {
  203. /* load text section */
  204. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  205. {
  206. rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
  207. ptr += shdr[index].sh_size;
  208. }
  209. /* load rodata section */
  210. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  211. {
  212. rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
  213. rodata_addr = (rt_uint32_t)ptr;
  214. ptr += shdr[index].sh_size;
  215. }
  216. /* load data section */
  217. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  218. {
  219. module->module_data = (rt_uint32_t)ptr;
  220. rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
  221. ptr += shdr[index].sh_size;
  222. }
  223. /* load bss section */
  224. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  225. {
  226. rt_memset(ptr, 0, shdr[index].sh_size);
  227. }
  228. }
  229. /* set module entry */
  230. module->module_entry = (rt_uint8_t*)module->module_space + elf_module->e_entry - module_addr;
  231. /* handle relocation section */
  232. for (index = 0; index < elf_module->e_shnum; index ++)
  233. {
  234. if (IS_REL(shdr[index]))
  235. {
  236. rt_uint32_t i, nr_reloc;
  237. Elf32_Sym *symtab;
  238. Elf32_Rel *rel;
  239. /* get relocate item */
  240. rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);
  241. /* locate .dynsym and .dynstr */
  242. symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset);
  243. strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  244. shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
  245. nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
  246. /* relocate every items */
  247. for (i = 0; i < nr_reloc; i ++)
  248. {
  249. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  250. #ifdef RT_MODULE_DEBUG
  251. rt_kprintf("relocate symbol: %s\n", strtab + sym->st_name);
  252. #endif
  253. if (sym->st_shndx != STN_UNDEF)
  254. {
  255. if(ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  256. {
  257. if (strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_RODATA, 8) == 0)
  258. {
  259. /* relocate rodata section */
  260. rt_module_arm_relocate(module, rel,
  261. (Elf32_Addr)(rodata_addr),
  262. module_addr);
  263. }
  264. else if(strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_BSS, 5) == 0)
  265. {
  266. /* relocate bss section */
  267. rt_module_arm_relocate(module, rel, (Elf32_Addr)ptr, module_addr);
  268. }
  269. }
  270. else if(ELF_ST_TYPE(sym->st_info) == STT_FUNC )
  271. {
  272. /* relocate function */
  273. rt_module_arm_relocate(module, rel,
  274. (Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value),
  275. module_addr);
  276. }
  277. else if(ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  278. {
  279. /* relocate object in data section */
  280. rt_module_arm_relocate(module, rel,
  281. (Elf32_Addr)(module->module_data + sym->st_value),
  282. module_addr);
  283. }
  284. }
  285. else
  286. {
  287. #ifdef RT_MODULE_DEBUG
  288. rt_kprintf("unresolved relocate symbol: %s\n", strtab + sym->st_name);
  289. #endif
  290. /* need to resolve symbol in kernel symbol table */
  291. Elf32_Addr addr = rt_module_symbol_find(strtab + sym->st_name);
  292. if (addr != (Elf32_Addr)RT_NULL)
  293. rt_module_arm_relocate(module, rel, addr, module_addr);
  294. else rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
  295. }
  296. rel ++;
  297. }
  298. }
  299. }
  300. /* init module object container */
  301. rt_module_init_object_container(module);
  302. /* create module main thread */
  303. module->module_thread = rt_thread_create((const char*)name,
  304. module->module_entry, RT_NULL,
  305. 512, 90, 10);
  306. module->module_thread->module_parent = module;
  307. rt_thread_startup(module->module_thread);
  308. return module;
  309. }
  310. void rt_module_unload(struct rt_module* module)
  311. {
  312. int i;
  313. struct rt_object* object;
  314. struct rt_timer *timer;
  315. struct rt_list_node *list, *node;
  316. /* suspend module main thread */
  317. if (module->module_thread->stat == RT_THREAD_READY)
  318. rt_thread_suspend(module->module_thread);
  319. /* delete all module object */
  320. for(i = RT_Object_Class_Thread; i < RT_Object_Class_Module; i++)
  321. {
  322. list = &module->module_object[i].object_list;
  323. for (node = list->next; node != list; node = node->next)
  324. {
  325. object = rt_list_entry(node, struct rt_object, list);
  326. rt_object_delete(object);
  327. }
  328. }
  329. /* release module memory */
  330. rt_free(module->module_space);
  331. rt_free((void *)module);
  332. }
  333. rt_module_t rt_module_find(char* name)
  334. {
  335. struct rt_module* module;
  336. module = (struct rt_module*)rt_object_find(RT_Object_Class_Module, name);
  337. return module;
  338. }
  339. #endif