dlmodule.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #include <dfs_posix.h>
  15. #define DBG_SECTION_NAME "DLMD"
  16. #define DBG_ENABLE // enable debug macro
  17. #define DBG_LEVEL DBG_INFO
  18. #define DBG_COLOR
  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, 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. 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. /* check ELF header */
  374. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  375. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  376. {
  377. rt_kprintf("Module: magic error\n");
  378. goto __exit;
  379. }
  380. /* check ELF class */
  381. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  382. {
  383. rt_kprintf("Module: ELF class error\n");
  384. goto __exit;
  385. }
  386. module = dlmodule_create();
  387. if (!module) goto __exit;
  388. /* set the name of module */
  389. _dlmodule_set_name(module, filename);
  390. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  391. if (elf_module->e_type == ET_REL)
  392. {
  393. ret = dlmodule_load_relocated_object(module, module_ptr);
  394. }
  395. else if (elf_module->e_type == ET_DYN)
  396. {
  397. ret = dlmodule_load_shared_object(module, module_ptr);
  398. }
  399. else
  400. {
  401. rt_kprintf("Module: unsupported elf type\n");
  402. goto __exit;
  403. }
  404. /* check return value */
  405. if (ret != RT_EOK) goto __exit;
  406. /* release module data */
  407. rt_free(module_ptr);
  408. /* increase module reference count */
  409. module->nref ++;
  410. /* deal with cache */
  411. #ifdef RT_USING_CACHE
  412. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  413. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  414. #endif
  415. /* set module initialization and cleanup function */
  416. module->init_func = dlsym(module, "module_init");
  417. module->cleanup_func = dlsym(module, "module_cleanup");
  418. module->stat = RT_DLMODULE_STAT_INIT;
  419. /* do module initialization */
  420. if (module->init_func)
  421. {
  422. module->init_func(module);
  423. }
  424. return module;
  425. __exit:
  426. if (fd >= 0) close(fd);
  427. if (module_ptr) rt_free(module_ptr);
  428. if (module) dlmodule_destroy(module);
  429. return RT_NULL;
  430. }
  431. struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
  432. {
  433. struct rt_dlmodule *module = RT_NULL;
  434. module = dlmodule_load(pgname);
  435. if (module)
  436. {
  437. if (module->entry_addr)
  438. {
  439. /* exec this module */
  440. rt_thread_t tid;
  441. module->cmd_line = rt_strdup(cmd);
  442. /* check stack size and priority */
  443. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  444. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  445. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  446. module->stack_size, module->priority, 10);
  447. if (tid)
  448. {
  449. tid->module_id = module;
  450. module->main_thread = tid;
  451. rt_thread_startup(tid);
  452. }
  453. else
  454. {
  455. /* destory dl module */
  456. dlmodule_destroy(module);
  457. module = RT_NULL;
  458. }
  459. }
  460. }
  461. return module;
  462. }
  463. void dlmodule_exit(int ret_code)
  464. {
  465. rt_thread_t thread;
  466. struct rt_dlmodule *module;
  467. module = dlmodule_self();
  468. if (!module) return;
  469. /* disable scheduling */
  470. rt_enter_critical();
  471. /* module is not running */
  472. if (module->stat != RT_DLMODULE_STAT_RUNNING)
  473. {
  474. /* restore scheduling */
  475. rt_exit_critical();
  476. return;
  477. }
  478. /* set return code */
  479. module->ret_code = ret_code;
  480. /* do exit for this module */
  481. _dlmodule_exit();
  482. /* the stat of module was changed to CLOSING in _dlmodule_exit */
  483. thread = module->main_thread;
  484. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  485. {
  486. /* main thread already closed */
  487. rt_exit_critical();
  488. return ;
  489. }
  490. /* delete thread: insert to defunct thread list */
  491. rt_thread_delete(thread);
  492. /* enable scheduling */
  493. rt_exit_critical();
  494. }
  495. rt_uint32_t dlmodule_symbol_find(const char *sym_str)
  496. {
  497. /* find in kernel symbol table */
  498. struct rt_module_symtab *index;
  499. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  500. {
  501. if (rt_strcmp(index->name, sym_str) == 0)
  502. return (rt_uint32_t)index->addr;
  503. }
  504. return 0;
  505. }
  506. int rt_system_dlmodule_init(void)
  507. {
  508. #if defined(__GNUC__) && !defined(__CC_ARM)
  509. extern int __rtmsymtab_start;
  510. extern int __rtmsymtab_end;
  511. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  512. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  513. #elif defined (__CC_ARM)
  514. extern int RTMSymTab$$Base;
  515. extern int RTMSymTab$$Limit;
  516. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  517. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  518. #elif defined (__IAR_SYSTEMS_ICC__)
  519. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  520. _rt_module_symtab_end = __section_end("RTMSymTab");
  521. #endif
  522. return 0;
  523. }
  524. INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
  525. /**
  526. * This function will find the specified module.
  527. *
  528. * @param name the name of module finding
  529. *
  530. * @return the module
  531. */
  532. struct rt_dlmodule *dlmodule_find(const char *name)
  533. {
  534. rt_object_t object;
  535. struct rt_dlmodule *ret = RT_NULL;
  536. object = rt_object_find(name, RT_Object_Class_Module);
  537. if (object)
  538. {
  539. ret = (struct rt_dlmodule*) object;
  540. }
  541. return ret;
  542. }
  543. RTM_EXPORT(dlmodule_find);
  544. int list_symbols(void)
  545. {
  546. extern int __rtmsymtab_start;
  547. extern int __rtmsymtab_end;
  548. /* find in kernel symbol table */
  549. struct rt_module_symtab *index;
  550. for (index = _rt_module_symtab_begin;
  551. index != _rt_module_symtab_end;
  552. index ++)
  553. {
  554. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  555. }
  556. return 0;
  557. }
  558. MSH_CMD_EXPORT(list_symbols, list symbols information);
  559. int list_module(void)
  560. {
  561. struct rt_dlmodule *module;
  562. struct rt_list_node *list, *node;
  563. struct rt_object_information *info;
  564. info = rt_object_get_information(RT_Object_Class_Module);
  565. list = &info->object_list;
  566. rt_kprintf("module ref address \n");
  567. rt_kprintf("-------- -------- ------------\n");
  568. for (node = list->next; node != list; node = node->next)
  569. {
  570. module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
  571. rt_kprintf("%-*.*s %-04d 0x%08x\n",
  572. RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
  573. }
  574. return 0;
  575. }
  576. MSH_CMD_EXPORT(list_module, list modules in system);