1
0

dlmodule.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  106. (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. /* remove thread from thread_list (ready or defunct thread list) */
  162. rt_list_remove(&(thread->tlist));
  163. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  164. (thread->thread_timer.parent.type == (RT_Object_Class_Static | RT_Object_Class_Timer)))
  165. {
  166. /* release thread timer */
  167. rt_timer_detach(&(thread->thread_timer));
  168. }
  169. /* change stat */
  170. thread->stat = RT_THREAD_CLOSE;
  171. /* invoke thread cleanup */
  172. if (thread->cleanup != RT_NULL)
  173. thread->cleanup(thread);
  174. rt_exit_critical();
  175. #ifdef RT_USING_SIGNALS
  176. rt_thread_free_sig(thread);
  177. #endif
  178. if (thread->parent.type & RT_Object_Class_Static)
  179. {
  180. /* detach object */
  181. rt_object_detach((rt_object_t)thread);
  182. }
  183. #ifdef RT_USING_HEAP
  184. else
  185. {
  186. /* release thread's stack */
  187. RT_KERNEL_FREE(thread->stack_addr);
  188. /* delete thread object */
  189. rt_object_delete((rt_object_t)thread);
  190. }
  191. #endif
  192. }
  193. rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
  194. {
  195. int i;
  196. RT_DEBUG_NOT_IN_INTERRUPT;
  197. /* check parameter */
  198. if (module == RT_NULL)
  199. return -RT_ERROR;
  200. /* can not destroy a running module */
  201. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  202. return -RT_EBUSY;
  203. /* do module cleanup */
  204. if (module->cleanup_func)
  205. {
  206. rt_enter_critical();
  207. module->cleanup_func(module);
  208. rt_exit_critical();
  209. }
  210. // list_object(&(module->object_list));
  211. /* cleanup for all kernel objects inside module*/
  212. {
  213. struct rt_object *object = RT_NULL;
  214. struct rt_list_node *node = RT_NULL;
  215. /* detach/delete all threads in this module */
  216. for (node = module->object_list.next; node != &(module->object_list); )
  217. {
  218. int object_type;
  219. object = rt_list_entry(node, struct rt_object, list);
  220. object_type = object->type & ~RT_Object_Class_Static;
  221. /* to next node */
  222. node = node->next;
  223. if (object->type & RT_Object_Class_Static)
  224. {
  225. switch (object_type)
  226. {
  227. case RT_Object_Class_Thread:
  228. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  229. break;
  230. #ifdef RT_USING_SEMAPHORE
  231. case RT_Object_Class_Semaphore:
  232. rt_sem_detach((rt_sem_t)object);
  233. break;
  234. #endif
  235. #ifdef RT_USING_MUTEX
  236. case RT_Object_Class_Mutex:
  237. rt_mutex_detach((rt_mutex_t)object);
  238. break;
  239. #endif
  240. #ifdef RT_USING_EVENT
  241. case RT_Object_Class_Event:
  242. rt_event_detach((rt_event_t)object);
  243. break;
  244. #endif
  245. #ifdef RT_USING_MAILBOX
  246. case RT_Object_Class_MailBox:
  247. rt_mb_detach((rt_mailbox_t)object);
  248. break;
  249. #endif
  250. #ifdef RT_USING_MESSAGEQUEUE
  251. case RT_Object_Class_MessageQueue:
  252. rt_mq_detach((rt_mq_t)object);
  253. break;
  254. #endif
  255. #ifdef RT_USING_MEMHEAP
  256. case RT_Object_Class_MemHeap:
  257. rt_memheap_detach((struct rt_memheap*)object);
  258. break;
  259. #endif
  260. #ifdef RT_USING_MEMPOOL
  261. case RT_Object_Class_MemPool:
  262. rt_mp_detach((struct rt_mempool*)object);
  263. break;
  264. #endif
  265. case RT_Object_Class_Timer:
  266. rt_timer_detach((rt_timer_t)object);
  267. break;
  268. default:
  269. LOG_E("Unsupported oject type in module.");
  270. break;
  271. }
  272. }
  273. else
  274. {
  275. switch (object_type)
  276. {
  277. case RT_Object_Class_Thread:
  278. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  279. break;
  280. #ifdef RT_USING_SEMAPHORE
  281. case RT_Object_Class_Semaphore:
  282. rt_sem_delete((rt_sem_t)object);
  283. break;
  284. #endif
  285. #ifdef RT_USING_MUTEX
  286. case RT_Object_Class_Mutex:
  287. rt_mutex_delete((rt_mutex_t)object);
  288. break;
  289. #endif
  290. #ifdef RT_USING_EVENT
  291. case RT_Object_Class_Event:
  292. rt_event_delete((rt_event_t)object);
  293. break;
  294. #endif
  295. #ifdef RT_USING_MAILBOX
  296. case RT_Object_Class_MailBox:
  297. rt_mb_delete((rt_mailbox_t)object);
  298. break;
  299. #endif
  300. #ifdef RT_USING_MESSAGEQUEUE
  301. case RT_Object_Class_MessageQueue:
  302. rt_mq_delete((rt_mq_t)object);
  303. break;
  304. #endif
  305. #ifdef RT_USING_MEMHEAP
  306. /* no delete operation */
  307. #endif
  308. #ifdef RT_USING_MEMPOOL
  309. case RT_Object_Class_MemPool:
  310. rt_mp_delete((struct rt_mempool*)object);
  311. break;
  312. #endif
  313. case RT_Object_Class_Timer:
  314. rt_timer_delete((rt_timer_t)object);
  315. break;
  316. default:
  317. LOG_E("Unsupported oject type in module.");
  318. break;
  319. }
  320. }
  321. }
  322. }
  323. if (module->cmd_line) rt_free(module->cmd_line);
  324. /* release module symbol table */
  325. for (i = 0; i < module->nsym; i ++)
  326. {
  327. rt_free((void *)module->symtab[i].name);
  328. }
  329. if (module->symtab != RT_NULL)
  330. {
  331. rt_free(module->symtab);
  332. }
  333. /* destory module */
  334. rt_free(module->mem_space);
  335. /* delete module object */
  336. rt_object_delete((rt_object_t)module);
  337. return RT_EOK;
  338. }
  339. struct rt_dlmodule *dlmodule_self(void)
  340. {
  341. rt_thread_t tid;
  342. struct rt_dlmodule *ret = RT_NULL;
  343. tid = rt_thread_self();
  344. if (tid)
  345. {
  346. ret = (struct rt_dlmodule*) tid->parent.module_id;
  347. }
  348. return ret;
  349. }
  350. /*
  351. * Compatible with old API
  352. */
  353. struct rt_dlmodule *rt_module_self(void)
  354. {
  355. return dlmodule_self();
  356. }
  357. struct rt_dlmodule* dlmodule_load(const char* filename)
  358. {
  359. #ifdef RT_USING_POSIX_FS
  360. int fd = -1, length = 0;
  361. #endif
  362. rt_err_t ret = RT_EOK;
  363. rt_uint8_t *module_ptr = RT_NULL;
  364. struct rt_dlmodule *module = RT_NULL;
  365. #ifdef RT_USING_POSIX_FS
  366. fd = open(filename, O_RDONLY, 0);
  367. if (fd >= 0)
  368. {
  369. length = lseek(fd, 0, SEEK_END);
  370. lseek(fd, 0, SEEK_SET);
  371. if (length == 0) goto __exit;
  372. module_ptr = (uint8_t*) rt_malloc (length);
  373. if (!module_ptr) goto __exit;
  374. if (read(fd, module_ptr, length) != length)
  375. goto __exit;
  376. /* close file and release fd */
  377. close(fd);
  378. fd = -1;
  379. }
  380. else
  381. {
  382. goto __exit;
  383. }
  384. #endif
  385. if (!module_ptr) goto __exit;
  386. /* check ELF header */
  387. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  388. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  389. {
  390. rt_kprintf("Module: magic error\n");
  391. goto __exit;
  392. }
  393. /* check ELF class */
  394. if ((elf_module->e_ident[EI_CLASS] != ELFCLASS32)&&(elf_module->e_ident[EI_CLASS] != ELFCLASS64))
  395. {
  396. rt_kprintf("Module: ELF class error\n");
  397. goto __exit;
  398. }
  399. module = dlmodule_create();
  400. if (!module) goto __exit;
  401. /* set the name of module */
  402. _dlmodule_set_name(module, filename);
  403. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  404. if (elf_module->e_type == ET_REL)
  405. {
  406. ret = dlmodule_load_relocated_object(module, module_ptr);
  407. }
  408. else if (elf_module->e_type == ET_DYN)
  409. {
  410. ret = dlmodule_load_shared_object(module, module_ptr);
  411. }
  412. else
  413. {
  414. rt_kprintf("Module: unsupported elf type\n");
  415. goto __exit;
  416. }
  417. /* check return value */
  418. if (ret != RT_EOK) goto __exit;
  419. /* release module data */
  420. rt_free(module_ptr);
  421. /* increase module reference count */
  422. module->nref ++;
  423. /* deal with cache */
  424. #ifdef RT_USING_CACHE
  425. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  426. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  427. #endif
  428. /* set module initialization and cleanup function */
  429. module->init_func = dlsym(module, "module_init");
  430. module->cleanup_func = dlsym(module, "module_cleanup");
  431. module->stat = RT_DLMODULE_STAT_INIT;
  432. /* do module initialization */
  433. if (module->init_func)
  434. {
  435. module->init_func(module);
  436. }
  437. return module;
  438. __exit:
  439. #ifdef RT_USING_POSIX_FS
  440. if (fd >= 0) close(fd);
  441. #endif
  442. if (module_ptr) rt_free(module_ptr);
  443. if (module) dlmodule_destroy(module);
  444. return RT_NULL;
  445. }
  446. struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
  447. {
  448. struct rt_dlmodule *module = RT_NULL;
  449. module = dlmodule_load(pgname);
  450. if (module)
  451. {
  452. if (module->entry_addr)
  453. {
  454. /* exec this module */
  455. rt_thread_t tid;
  456. module->cmd_line = rt_strdup(cmd);
  457. /* check stack size and priority */
  458. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  459. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  460. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  461. module->stack_size, module->priority, 10);
  462. if (tid)
  463. {
  464. tid->parent.module_id= module;
  465. module->main_thread = tid;
  466. rt_thread_startup(tid);
  467. }
  468. else
  469. {
  470. /* destory dl module */
  471. dlmodule_destroy(module);
  472. module = RT_NULL;
  473. }
  474. }
  475. }
  476. return module;
  477. }
  478. #if defined(RT_USING_CUSTOM_DLMODULE)
  479. struct rt_dlmodule* dlmodule_load_custom(const char* filename, struct rt_dlmodule_ops* ops)
  480. {
  481. #ifdef RT_USING_POSIX_FS
  482. int fd = -1, length = 0;
  483. #endif
  484. rt_err_t ret = RT_EOK;
  485. rt_uint8_t *module_ptr = RT_NULL;
  486. struct rt_dlmodule *module = RT_NULL;
  487. if (ops)
  488. {
  489. RT_ASSERT(ops->load);
  490. RT_ASSERT(ops->unload);
  491. module_ptr = ops->load(filename);
  492. }
  493. #ifdef RT_USING_POSIX_FS
  494. else
  495. {
  496. fd = open(filename, O_RDONLY, 0);
  497. if (fd >= 0)
  498. {
  499. length = lseek(fd, 0, SEEK_END);
  500. lseek(fd, 0, SEEK_SET);
  501. if (length == 0) goto __exit;
  502. module_ptr = (uint8_t*) rt_malloc (length);
  503. if (!module_ptr) goto __exit;
  504. if (read(fd, module_ptr, length) != length)
  505. goto __exit;
  506. /* close file and release fd */
  507. close(fd);
  508. fd = -1;
  509. }
  510. else
  511. {
  512. goto __exit;
  513. }
  514. }
  515. #endif
  516. if (!module_ptr) goto __exit;
  517. /* check ELF header */
  518. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  519. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  520. {
  521. rt_kprintf("Module: magic error\n");
  522. goto __exit;
  523. }
  524. /* check ELF class */
  525. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  526. {
  527. rt_kprintf("Module: ELF class error\n");
  528. goto __exit;
  529. }
  530. module = dlmodule_create();
  531. if (!module) goto __exit;
  532. /* set the name of module */
  533. _dlmodule_set_name(module, filename);
  534. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  535. if (elf_module->e_type == ET_REL)
  536. {
  537. ret = dlmodule_load_relocated_object(module, module_ptr);
  538. }
  539. else if (elf_module->e_type == ET_DYN)
  540. {
  541. ret = dlmodule_load_shared_object(module, module_ptr);
  542. }
  543. else
  544. {
  545. rt_kprintf("Module: unsupported elf type\n");
  546. goto __exit;
  547. }
  548. /* check return value */
  549. if (ret != RT_EOK) goto __exit;
  550. /* release module data */
  551. if (ops)
  552. {
  553. ops->unload(module_ptr);
  554. }
  555. else
  556. {
  557. rt_free(module_ptr);
  558. }
  559. /* increase module reference count */
  560. module->nref ++;
  561. /* deal with cache */
  562. #ifdef RT_USING_CACHE
  563. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  564. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  565. #endif
  566. /* set module initialization and cleanup function */
  567. module->init_func = dlsym(module, "module_init");
  568. module->cleanup_func = dlsym(module, "module_cleanup");
  569. module->stat = RT_DLMODULE_STAT_INIT;
  570. /* do module initialization */
  571. if (module->init_func)
  572. {
  573. module->init_func(module);
  574. }
  575. return module;
  576. __exit:
  577. #ifdef RT_USING_POSIX_FS
  578. if (fd >= 0) close(fd);
  579. #endif
  580. if (module_ptr)
  581. {
  582. if (ops)
  583. {
  584. ops->unload(module_ptr);
  585. }
  586. else
  587. {
  588. rt_free(module_ptr);
  589. }
  590. }
  591. if (module) dlmodule_destroy(module);
  592. return RT_NULL;
  593. }
  594. struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, int cmd_size, struct rt_dlmodule_ops* ops)
  595. {
  596. struct rt_dlmodule *module = RT_NULL;
  597. module = dlmodule_load_custom(pgname, ops);
  598. if (module)
  599. {
  600. if (module->entry_addr)
  601. {
  602. /* exec this module */
  603. rt_thread_t tid;
  604. module->cmd_line = rt_strdup(cmd);
  605. /* check stack size and priority */
  606. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  607. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  608. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  609. module->stack_size, module->priority, 10);
  610. if (tid)
  611. {
  612. tid->parent.module_id= module;
  613. module->main_thread = tid;
  614. rt_thread_startup(tid);
  615. }
  616. else
  617. {
  618. /* destory dl module */
  619. dlmodule_destroy(module);
  620. module = RT_NULL;
  621. }
  622. }
  623. }
  624. return module;
  625. }
  626. #endif
  627. void dlmodule_exit(int ret_code)
  628. {
  629. rt_thread_t thread;
  630. struct rt_dlmodule *module;
  631. module = dlmodule_self();
  632. if (!module) return;
  633. /* disable scheduling */
  634. rt_enter_critical();
  635. /* module is not running */
  636. if (module->stat != RT_DLMODULE_STAT_RUNNING)
  637. {
  638. /* restore scheduling */
  639. rt_exit_critical();
  640. return;
  641. }
  642. /* set return code */
  643. module->ret_code = ret_code;
  644. /* do exit for this module */
  645. _dlmodule_exit();
  646. /* the stat of module was changed to CLOSING in _dlmodule_exit */
  647. thread = module->main_thread;
  648. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  649. {
  650. /* main thread already closed */
  651. rt_exit_critical();
  652. return ;
  653. }
  654. /* delete thread: insert to defunct thread list */
  655. rt_thread_delete(thread);
  656. /* enable scheduling */
  657. rt_exit_critical();
  658. }
  659. rt_uint32_t dlmodule_symbol_find(const char *sym_str)
  660. {
  661. /* find in kernel symbol table */
  662. struct rt_module_symtab *index;
  663. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  664. {
  665. if (rt_strcmp(index->name, sym_str) == 0)
  666. return (rt_uint32_t)index->addr;
  667. }
  668. return 0;
  669. }
  670. int rt_system_dlmodule_init(void)
  671. {
  672. #if defined(__GNUC__) && !defined(__CC_ARM)
  673. extern int __rtmsymtab_start;
  674. extern int __rtmsymtab_end;
  675. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  676. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  677. #elif defined (__CC_ARM)
  678. extern int RTMSymTab$$Base;
  679. extern int RTMSymTab$$Limit;
  680. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  681. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  682. #elif defined (__IAR_SYSTEMS_ICC__)
  683. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  684. _rt_module_symtab_end = __section_end("RTMSymTab");
  685. #endif
  686. return 0;
  687. }
  688. INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
  689. /**
  690. * This function will find the specified module.
  691. *
  692. * @param name the name of module finding
  693. *
  694. * @return the module
  695. */
  696. struct rt_dlmodule *dlmodule_find(const char *name)
  697. {
  698. rt_object_t object;
  699. struct rt_dlmodule *ret = RT_NULL;
  700. object = rt_object_find(name, RT_Object_Class_Module);
  701. if (object)
  702. {
  703. ret = (struct rt_dlmodule*) object;
  704. }
  705. return ret;
  706. }
  707. RTM_EXPORT(dlmodule_find);
  708. int list_symbols(void)
  709. {
  710. extern int __rtmsymtab_start;
  711. extern int __rtmsymtab_end;
  712. /* find in kernel symbol table */
  713. struct rt_module_symtab *index;
  714. for (index = _rt_module_symtab_begin;
  715. index != _rt_module_symtab_end;
  716. index ++)
  717. {
  718. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  719. }
  720. return 0;
  721. }
  722. MSH_CMD_EXPORT(list_symbols, list symbols information);
  723. int list_module(void)
  724. {
  725. struct rt_dlmodule *module;
  726. struct rt_list_node *list, *node;
  727. struct rt_object_information *info;
  728. info = rt_object_get_information(RT_Object_Class_Module);
  729. list = &info->object_list;
  730. rt_kprintf("module ref address \n");
  731. rt_kprintf("-------- -------- ------------\n");
  732. for (node = list->next; node != list; node = node->next)
  733. {
  734. module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
  735. rt_kprintf("%-*.*s %-04d 0x%08x\n",
  736. RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
  737. }
  738. return 0;
  739. }
  740. MSH_CMD_EXPORT(list_module, list modules in system);