dlmodule.c 22 KB

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