1
0

dlmodule.c 22 KB

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