dlmodule.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/08/29 Bernard first version
  9. */
  10. #include <rthw.h>
  11. #include "dlfcn.h"
  12. #include "dlmodule.h"
  13. #include "dlelf.h"
  14. #if defined(RT_USING_POSIX)
  15. #include <unistd.h>
  16. #include <sys/stat.h>
  17. #include <sys/statfs.h>
  18. #endif
  19. #define DBG_TAG "DLMD"
  20. #define DBG_LVL DBG_INFO
  21. #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
  22. static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
  23. static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
  24. #if defined(__IAR_SYSTEMS_ICC__) /* for IAR compiler */
  25. #pragma section="RTMSymTab"
  26. #endif
  27. /* set the name of module */
  28. static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
  29. {
  30. int size;
  31. struct rt_object *object;
  32. const char *first, *end, *ptr;
  33. object = &(module->parent);
  34. ptr = first = (char *)path;
  35. end = path + rt_strlen(path);
  36. while (*ptr != '\0')
  37. {
  38. if (*ptr == '/')
  39. first = ptr + 1;
  40. if (*ptr == '.')
  41. end = ptr - 1;
  42. ptr ++;
  43. }
  44. size = end - first + 1;
  45. if (size > RT_NAME_MAX) size = RT_NAME_MAX;
  46. rt_strncpy(object->name, first, size);
  47. object->name[size] = '\0';
  48. }
  49. #define RT_MODULE_ARG_MAX 8
  50. static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[])
  51. {
  52. int argc = 0;
  53. char *ptr = cmd;
  54. while ((ptr - cmd) < length)
  55. {
  56. /* strip bank and tab */
  57. while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length)
  58. *ptr++ = '\0';
  59. /* check whether it's the end of line */
  60. if ((ptr - cmd) >= length) break;
  61. /* handle string with quote */
  62. if (*ptr == '"')
  63. {
  64. argv[argc++] = ++ptr;
  65. /* skip this string */
  66. while (*ptr != '"' && (ptr - cmd) < length)
  67. if (*ptr ++ == '\\') ptr ++;
  68. if ((ptr - cmd) >= length) break;
  69. /* skip '"' */
  70. *ptr ++ = '\0';
  71. }
  72. else
  73. {
  74. argv[argc++] = ptr;
  75. while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
  76. ptr ++;
  77. }
  78. if (argc >= RT_MODULE_ARG_MAX) break;
  79. }
  80. return argc;
  81. }
  82. /* invoked by main thread for exit */
  83. static void _dlmodule_exit(void)
  84. {
  85. struct rt_dlmodule *module;
  86. module = dlmodule_self();
  87. if (!module) return; /* not a module thread */
  88. rt_enter_critical();
  89. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  90. {
  91. struct rt_object *object = RT_NULL;
  92. struct rt_list_node *node = RT_NULL;
  93. /* set stat to closing */
  94. module->stat = RT_DLMODULE_STAT_CLOSING;
  95. /* suspend all threads in this module */
  96. for (node = module->object_list.next; node != &(module->object_list); node = node->next)
  97. {
  98. object = rt_list_entry(node, struct rt_object, list);
  99. if ((object->type & ~RT_Object_Class_Static) == RT_Object_Class_Thread)
  100. {
  101. rt_thread_t thread = (rt_thread_t)object;
  102. /* stop timer and suspend thread*/
  103. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  104. (thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  105. {
  106. rt_timer_stop(&(thread->thread_timer));
  107. rt_thread_suspend(thread);
  108. }
  109. }
  110. }
  111. }
  112. rt_exit_critical();
  113. return;
  114. }
  115. static void _dlmodule_thread_entry(void* parameter)
  116. {
  117. int argc = 0;
  118. char *argv[RT_MODULE_ARG_MAX];
  119. struct rt_dlmodule *module = (struct rt_dlmodule*)parameter;
  120. if (module == RT_NULL || module->cmd_line == RT_NULL)
  121. /* malloc for module_cmd_line failed. */
  122. return;
  123. if (module->cmd_line)
  124. {
  125. rt_memset(argv, 0x00, sizeof(argv));
  126. argc = _rt_module_split_arg((char *)module->cmd_line, rt_strlen(module->cmd_line), argv);
  127. if (argc == 0) goto __exit;
  128. }
  129. /* set status of module */
  130. module->stat = RT_DLMODULE_STAT_RUNNING;
  131. LOG_D("run main entry: 0x%p with %s",
  132. module->entry_addr,
  133. module->cmd_line);
  134. if (module->entry_addr)
  135. module->entry_addr(argc, argv);
  136. __exit:
  137. _dlmodule_exit();
  138. return ;
  139. }
  140. struct rt_dlmodule *dlmodule_create(void)
  141. {
  142. struct rt_dlmodule *module = RT_NULL;
  143. module = (struct rt_dlmodule*) rt_object_allocate(RT_Object_Class_Module, "module");
  144. if (module)
  145. {
  146. module->stat = RT_DLMODULE_STAT_INIT;
  147. /* set initial priority and stack size */
  148. module->priority = RT_THREAD_PRIORITY_MAX - 1;
  149. module->stack_size = 2048;
  150. rt_list_init(&(module->object_list));
  151. }
  152. return module;
  153. }
  154. void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
  155. {
  156. RT_ASSERT(thread->module_id == module);
  157. /* lock scheduler to prevent scheduling in cleanup function. */
  158. rt_enter_critical();
  159. /* remove thread from thread_list (ready or defunct thread list) */
  160. rt_list_remove(&(thread->tlist));
  161. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  162. (thread->thread_timer.parent.type == (RT_Object_Class_Static | RT_Object_Class_Timer)))
  163. {
  164. /* release thread timer */
  165. rt_timer_detach(&(thread->thread_timer));
  166. }
  167. /* change stat */
  168. thread->stat = RT_THREAD_CLOSE;
  169. /* invoke thread cleanup */
  170. if (thread->cleanup != RT_NULL)
  171. thread->cleanup(thread);
  172. rt_exit_critical();
  173. #ifdef RT_USING_SIGNALS
  174. rt_thread_free_sig(thread);
  175. #endif
  176. if (thread->type & RT_Object_Class_Static)
  177. {
  178. /* detach object */
  179. rt_object_detach((rt_object_t)thread);
  180. }
  181. #ifdef RT_USING_HEAP
  182. else
  183. {
  184. /* release thread's stack */
  185. RT_KERNEL_FREE(thread->stack_addr);
  186. /* delete thread object */
  187. rt_object_delete((rt_object_t)thread);
  188. }
  189. #endif
  190. }
  191. rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
  192. {
  193. int i;
  194. RT_DEBUG_NOT_IN_INTERRUPT;
  195. /* check parameter */
  196. if (module == RT_NULL)
  197. return -RT_ERROR;
  198. /* can not destroy a running module */
  199. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  200. return -RT_EBUSY;
  201. /* do module cleanup */
  202. if (module->cleanup_func)
  203. {
  204. rt_enter_critical();
  205. module->cleanup_func(module);
  206. rt_exit_critical();
  207. }
  208. // list_object(&(module->object_list));
  209. /* cleanup for all kernel objects inside module*/
  210. {
  211. struct rt_object *object = RT_NULL;
  212. struct rt_list_node *node = RT_NULL;
  213. /* detach/delete all threads in this module */
  214. for (node = module->object_list.next; node != &(module->object_list); )
  215. {
  216. int object_type;
  217. object = rt_list_entry(node, struct rt_object, list);
  218. object_type = object->type & ~RT_Object_Class_Static;
  219. /* to next node */
  220. node = node->next;
  221. if (object->type & RT_Object_Class_Static)
  222. {
  223. switch (object_type)
  224. {
  225. case RT_Object_Class_Thread:
  226. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  227. break;
  228. #ifdef RT_USING_SEMAPHORE
  229. case RT_Object_Class_Semaphore:
  230. rt_sem_detach((rt_sem_t)object);
  231. break;
  232. #endif
  233. #ifdef RT_USING_MUTEX
  234. case RT_Object_Class_Mutex:
  235. rt_mutex_detach((rt_mutex_t)object);
  236. break;
  237. #endif
  238. #ifdef RT_USING_EVENT
  239. case RT_Object_Class_Event:
  240. rt_event_detach((rt_event_t)object);
  241. break;
  242. #endif
  243. #ifdef RT_USING_MAILBOX
  244. case RT_Object_Class_MailBox:
  245. rt_mb_detach((rt_mailbox_t)object);
  246. break;
  247. #endif
  248. #ifdef RT_USING_MESSAGEQUEUE
  249. case RT_Object_Class_MessageQueue:
  250. rt_mq_detach((rt_mq_t)object);
  251. break;
  252. #endif
  253. #ifdef RT_USING_MEMHEAP
  254. case RT_Object_Class_MemHeap:
  255. rt_memheap_detach((struct rt_memheap*)object);
  256. break;
  257. #endif
  258. #ifdef RT_USING_MEMPOOL
  259. case RT_Object_Class_MemPool:
  260. rt_mp_detach((struct rt_mempool*)object);
  261. break;
  262. #endif
  263. case RT_Object_Class_Timer:
  264. rt_timer_detach((rt_timer_t)object);
  265. break;
  266. default:
  267. LOG_E("Unsupported oject type in module.");
  268. break;
  269. }
  270. }
  271. else
  272. {
  273. switch (object_type)
  274. {
  275. case RT_Object_Class_Thread:
  276. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  277. break;
  278. #ifdef RT_USING_SEMAPHORE
  279. case RT_Object_Class_Semaphore:
  280. rt_sem_delete((rt_sem_t)object);
  281. break;
  282. #endif
  283. #ifdef RT_USING_MUTEX
  284. case RT_Object_Class_Mutex:
  285. rt_mutex_delete((rt_mutex_t)object);
  286. break;
  287. #endif
  288. #ifdef RT_USING_EVENT
  289. case RT_Object_Class_Event:
  290. rt_event_delete((rt_event_t)object);
  291. break;
  292. #endif
  293. #ifdef RT_USING_MAILBOX
  294. case RT_Object_Class_MailBox:
  295. rt_mb_delete((rt_mailbox_t)object);
  296. break;
  297. #endif
  298. #ifdef RT_USING_MESSAGEQUEUE
  299. case RT_Object_Class_MessageQueue:
  300. rt_mq_delete((rt_mq_t)object);
  301. break;
  302. #endif
  303. #ifdef RT_USING_MEMHEAP
  304. /* no delete operation */
  305. #endif
  306. #ifdef RT_USING_MEMPOOL
  307. case RT_Object_Class_MemPool:
  308. rt_mp_delete((struct rt_mempool*)object);
  309. break;
  310. #endif
  311. case RT_Object_Class_Timer:
  312. rt_timer_delete((rt_timer_t)object);
  313. break;
  314. default:
  315. LOG_E("Unsupported oject type in module.");
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. if (module->cmd_line) rt_free(module->cmd_line);
  322. /* release module symbol table */
  323. for (i = 0; i < module->nsym; i ++)
  324. {
  325. rt_free((void *)module->symtab[i].name);
  326. }
  327. if (module->symtab != RT_NULL)
  328. {
  329. rt_free(module->symtab);
  330. }
  331. /* destory module */
  332. rt_free(module->mem_space);
  333. /* delete module object */
  334. rt_object_delete((rt_object_t)module);
  335. return RT_EOK;
  336. }
  337. struct rt_dlmodule *dlmodule_self(void)
  338. {
  339. rt_thread_t tid;
  340. struct rt_dlmodule *ret = RT_NULL;
  341. tid = rt_thread_self();
  342. if (tid)
  343. {
  344. ret = (struct rt_dlmodule*) tid->module_id;
  345. }
  346. return ret;
  347. }
  348. /*
  349. * Compatible with old API
  350. */
  351. struct rt_dlmodule *rt_module_self(void)
  352. {
  353. return dlmodule_self();
  354. }
  355. struct rt_dlmodule* dlmodule_load(const char* filename)
  356. {
  357. #if defined(RT_USING_POSIX)
  358. int fd = -1, length = 0;
  359. #endif
  360. rt_err_t ret = RT_EOK;
  361. rt_uint8_t *module_ptr = RT_NULL;
  362. struct rt_dlmodule *module = RT_NULL;
  363. #if defined(RT_USING_POSIX)
  364. fd = open(filename, O_RDONLY, 0);
  365. if (fd >= 0)
  366. {
  367. length = lseek(fd, 0, SEEK_END);
  368. lseek(fd, 0, SEEK_SET);
  369. if (length == 0) goto __exit;
  370. module_ptr = (uint8_t*) rt_malloc (length);
  371. if (!module_ptr) goto __exit;
  372. if (read(fd, module_ptr, length) != length)
  373. goto __exit;
  374. /* close file and release fd */
  375. close(fd);
  376. fd = -1;
  377. }
  378. else
  379. {
  380. goto __exit;
  381. }
  382. #endif
  383. if (!module_ptr) goto __exit;
  384. /* check ELF header */
  385. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  386. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  387. {
  388. rt_kprintf("Module: magic error\n");
  389. goto __exit;
  390. }
  391. /* check ELF class */
  392. if ((elf_module->e_ident[EI_CLASS] != ELFCLASS32)&&(elf_module->e_ident[EI_CLASS] != ELFCLASS64))
  393. {
  394. rt_kprintf("Module: ELF class error\n");
  395. goto __exit;
  396. }
  397. module = dlmodule_create();
  398. if (!module) goto __exit;
  399. /* set the name of module */
  400. _dlmodule_set_name(module, filename);
  401. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  402. if (elf_module->e_type == ET_REL)
  403. {
  404. ret = dlmodule_load_relocated_object(module, module_ptr);
  405. }
  406. else if (elf_module->e_type == ET_DYN)
  407. {
  408. ret = dlmodule_load_shared_object(module, module_ptr);
  409. }
  410. else
  411. {
  412. rt_kprintf("Module: unsupported elf type\n");
  413. goto __exit;
  414. }
  415. /* check return value */
  416. if (ret != RT_EOK) goto __exit;
  417. /* release module data */
  418. rt_free(module_ptr);
  419. /* increase module reference count */
  420. module->nref ++;
  421. /* deal with cache */
  422. #ifdef RT_USING_CACHE
  423. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  424. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  425. #endif
  426. /* set module initialization and cleanup function */
  427. module->init_func = dlsym(module, "module_init");
  428. module->cleanup_func = dlsym(module, "module_cleanup");
  429. module->stat = RT_DLMODULE_STAT_INIT;
  430. /* do module initialization */
  431. if (module->init_func)
  432. {
  433. module->init_func(module);
  434. }
  435. return module;
  436. __exit:
  437. #if defined(RT_USING_POSIX)
  438. if (fd >= 0) close(fd);
  439. #endif
  440. if (module_ptr) rt_free(module_ptr);
  441. if (module) dlmodule_destroy(module);
  442. return RT_NULL;
  443. }
  444. struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
  445. {
  446. struct rt_dlmodule *module = RT_NULL;
  447. module = dlmodule_load(pgname);
  448. if (module)
  449. {
  450. if (module->entry_addr)
  451. {
  452. /* exec this module */
  453. rt_thread_t tid;
  454. module->cmd_line = rt_strdup(cmd);
  455. /* check stack size and priority */
  456. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  457. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  458. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  459. module->stack_size, module->priority, 10);
  460. if (tid)
  461. {
  462. tid->module_id = module;
  463. module->main_thread = tid;
  464. rt_thread_startup(tid);
  465. }
  466. else
  467. {
  468. /* destory dl module */
  469. dlmodule_destroy(module);
  470. module = RT_NULL;
  471. }
  472. }
  473. }
  474. return module;
  475. }
  476. #if defined(RT_USING_CUSTOM_DLMODULE)
  477. struct rt_dlmodule* dlmodule_load_custom(const char* filename, struct rt_dlmodule_ops* ops)
  478. {
  479. #if defined(RT_USING_POSIX)
  480. int fd = -1, length = 0;
  481. #endif
  482. rt_err_t ret = RT_EOK;
  483. rt_uint8_t *module_ptr = RT_NULL;
  484. struct rt_dlmodule *module = RT_NULL;
  485. if (ops)
  486. {
  487. RT_ASSERT(ops->load);
  488. RT_ASSERT(ops->unload);
  489. module_ptr = ops->load(filename);
  490. }
  491. #if defined(RT_USING_POSIX)
  492. else
  493. {
  494. fd = open(filename, O_RDONLY, 0);
  495. if (fd >= 0)
  496. {
  497. length = lseek(fd, 0, SEEK_END);
  498. lseek(fd, 0, SEEK_SET);
  499. if (length == 0) goto __exit;
  500. module_ptr = (uint8_t*) rt_malloc (length);
  501. if (!module_ptr) goto __exit;
  502. if (read(fd, module_ptr, length) != length)
  503. goto __exit;
  504. /* close file and release fd */
  505. close(fd);
  506. fd = -1;
  507. }
  508. else
  509. {
  510. goto __exit;
  511. }
  512. }
  513. #endif
  514. if (!module_ptr) goto __exit;
  515. /* check ELF header */
  516. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  517. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  518. {
  519. rt_kprintf("Module: magic error\n");
  520. goto __exit;
  521. }
  522. /* check ELF class */
  523. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  524. {
  525. rt_kprintf("Module: ELF class error\n");
  526. goto __exit;
  527. }
  528. module = dlmodule_create();
  529. if (!module) goto __exit;
  530. /* set the name of module */
  531. _dlmodule_set_name(module, filename);
  532. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  533. if (elf_module->e_type == ET_REL)
  534. {
  535. ret = dlmodule_load_relocated_object(module, module_ptr);
  536. }
  537. else if (elf_module->e_type == ET_DYN)
  538. {
  539. ret = dlmodule_load_shared_object(module, module_ptr);
  540. }
  541. else
  542. {
  543. rt_kprintf("Module: unsupported elf type\n");
  544. goto __exit;
  545. }
  546. /* check return value */
  547. if (ret != RT_EOK) goto __exit;
  548. /* release module data */
  549. if (ops)
  550. {
  551. ops->unload(module_ptr);
  552. }
  553. else
  554. {
  555. rt_free(module_ptr);
  556. }
  557. /* increase module reference count */
  558. module->nref ++;
  559. /* deal with cache */
  560. #ifdef RT_USING_CACHE
  561. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  562. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  563. #endif
  564. /* set module initialization and cleanup function */
  565. module->init_func = dlsym(module, "module_init");
  566. module->cleanup_func = dlsym(module, "module_cleanup");
  567. module->stat = RT_DLMODULE_STAT_INIT;
  568. /* do module initialization */
  569. if (module->init_func)
  570. {
  571. module->init_func(module);
  572. }
  573. return module;
  574. __exit:
  575. #if defined(RT_USING_POSIX)
  576. if (fd >= 0) close(fd);
  577. #endif
  578. if (module_ptr)
  579. {
  580. if (ops)
  581. {
  582. ops->unload(module_ptr);
  583. }
  584. else
  585. {
  586. rt_free(module_ptr);
  587. }
  588. }
  589. if (module) dlmodule_destroy(module);
  590. return RT_NULL;
  591. }
  592. struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, int cmd_size, struct rt_dlmodule_ops* ops)
  593. {
  594. struct rt_dlmodule *module = RT_NULL;
  595. module = dlmodule_load_custom(pgname, ops);
  596. if (module)
  597. {
  598. if (module->entry_addr)
  599. {
  600. /* exec this module */
  601. rt_thread_t tid;
  602. module->cmd_line = rt_strdup(cmd);
  603. /* check stack size and priority */
  604. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  605. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  606. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  607. module->stack_size, module->priority, 10);
  608. if (tid)
  609. {
  610. tid->module_id = module;
  611. module->main_thread = tid;
  612. rt_thread_startup(tid);
  613. }
  614. else
  615. {
  616. /* destory dl module */
  617. dlmodule_destroy(module);
  618. module = RT_NULL;
  619. }
  620. }
  621. }
  622. return module;
  623. }
  624. #endif
  625. void dlmodule_exit(int ret_code)
  626. {
  627. rt_thread_t thread;
  628. struct rt_dlmodule *module;
  629. module = dlmodule_self();
  630. if (!module) return;
  631. /* disable scheduling */
  632. rt_enter_critical();
  633. /* module is not running */
  634. if (module->stat != RT_DLMODULE_STAT_RUNNING)
  635. {
  636. /* restore scheduling */
  637. rt_exit_critical();
  638. return;
  639. }
  640. /* set return code */
  641. module->ret_code = ret_code;
  642. /* do exit for this module */
  643. _dlmodule_exit();
  644. /* the stat of module was changed to CLOSING in _dlmodule_exit */
  645. thread = module->main_thread;
  646. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  647. {
  648. /* main thread already closed */
  649. rt_exit_critical();
  650. return ;
  651. }
  652. /* delete thread: insert to defunct thread list */
  653. rt_thread_delete(thread);
  654. /* enable scheduling */
  655. rt_exit_critical();
  656. }
  657. rt_uint32_t dlmodule_symbol_find(const char *sym_str)
  658. {
  659. /* find in kernel symbol table */
  660. struct rt_module_symtab *index;
  661. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  662. {
  663. if (rt_strcmp(index->name, sym_str) == 0)
  664. return (rt_uint32_t)index->addr;
  665. }
  666. return 0;
  667. }
  668. int rt_system_dlmodule_init(void)
  669. {
  670. #if defined(__GNUC__) && !defined(__CC_ARM)
  671. extern int __rtmsymtab_start;
  672. extern int __rtmsymtab_end;
  673. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  674. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  675. #elif defined (__CC_ARM)
  676. extern int RTMSymTab$$Base;
  677. extern int RTMSymTab$$Limit;
  678. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  679. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  680. #elif defined (__IAR_SYSTEMS_ICC__)
  681. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  682. _rt_module_symtab_end = __section_end("RTMSymTab");
  683. #endif
  684. return 0;
  685. }
  686. INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
  687. /**
  688. * This function will find the specified module.
  689. *
  690. * @param name the name of module finding
  691. *
  692. * @return the module
  693. */
  694. struct rt_dlmodule *dlmodule_find(const char *name)
  695. {
  696. rt_object_t object;
  697. struct rt_dlmodule *ret = RT_NULL;
  698. object = rt_object_find(name, RT_Object_Class_Module);
  699. if (object)
  700. {
  701. ret = (struct rt_dlmodule*) object;
  702. }
  703. return ret;
  704. }
  705. RTM_EXPORT(dlmodule_find);
  706. int list_symbols(void)
  707. {
  708. extern int __rtmsymtab_start;
  709. extern int __rtmsymtab_end;
  710. /* find in kernel symbol table */
  711. struct rt_module_symtab *index;
  712. for (index = _rt_module_symtab_begin;
  713. index != _rt_module_symtab_end;
  714. index ++)
  715. {
  716. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  717. }
  718. return 0;
  719. }
  720. MSH_CMD_EXPORT(list_symbols, list symbols information);
  721. int list_module(void)
  722. {
  723. struct rt_dlmodule *module;
  724. struct rt_list_node *list, *node;
  725. struct rt_object_information *info;
  726. info = rt_object_get_information(RT_Object_Class_Module);
  727. list = &info->object_list;
  728. rt_kprintf("module ref address \n");
  729. rt_kprintf("-------- -------- ------------\n");
  730. for (node = list->next; node != list; node = node->next)
  731. {
  732. module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
  733. rt_kprintf("%-*.*s %-04d 0x%08x\n",
  734. RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
  735. }
  736. return 0;
  737. }
  738. MSH_CMD_EXPORT(list_module, list modules in system);