dlmodule.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 *ret = RT_NULL;
  141. ret = (struct rt_dlmodule*) rt_object_allocate(RT_Object_Class_Module, "module");
  142. if (ret)
  143. {
  144. ret->stat = RT_DLMODULE_STAT_INIT;
  145. rt_list_init(&(ret->object_list));
  146. }
  147. return ret;
  148. }
  149. void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
  150. {
  151. RT_ASSERT(thread->module_id == module);
  152. /* lock scheduler to prevent scheduling in cleanup function. */
  153. rt_enter_critical();
  154. /* remove thread from thread_list (ready or defunct thread list) */
  155. rt_list_remove(&(thread->tlist));
  156. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  157. (thread->thread_timer.parent.type == (RT_Object_Class_Static | RT_Object_Class_Timer)))
  158. {
  159. /* release thread timer */
  160. rt_timer_detach(&(thread->thread_timer));
  161. }
  162. /* change stat */
  163. thread->stat = RT_THREAD_CLOSE;
  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->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->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. int fd, length = 0;
  353. rt_err_t ret = RT_EOK;
  354. rt_uint8_t *module_ptr = RT_NULL;
  355. struct rt_dlmodule *module = RT_NULL;
  356. fd = open(filename, O_RDONLY, 0);
  357. if (fd >= 0)
  358. {
  359. length = lseek(fd, 0, SEEK_END);
  360. lseek(fd, 0, SEEK_SET);
  361. if (length == 0) goto __exit;
  362. module_ptr = (uint8_t*) rt_malloc (length);
  363. if (!module_ptr) goto __exit;
  364. if (read(fd, module_ptr, length) != length)
  365. goto __exit;
  366. /* close file and release fd */
  367. close(fd);
  368. fd = -1;
  369. }
  370. /* check ELF header */
  371. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  372. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  373. {
  374. rt_kprintf("Module: magic error\n");
  375. goto __exit;
  376. }
  377. /* check ELF class */
  378. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  379. {
  380. rt_kprintf("Module: ELF class error\n");
  381. goto __exit;
  382. }
  383. module = dlmodule_create();
  384. if (!module) goto __exit;
  385. /* set the name of module */
  386. _dlmodule_set_name(module, filename);
  387. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  388. if (elf_module->e_type == ET_REL)
  389. {
  390. ret = dlmodule_load_relocated_object(module, module_ptr);
  391. }
  392. else if (elf_module->e_type == ET_DYN)
  393. {
  394. ret = dlmodule_load_shared_object(module, module_ptr);
  395. }
  396. else
  397. {
  398. rt_kprintf("Module: unsupported elf type\n");
  399. goto __exit;
  400. }
  401. /* check return value */
  402. if (ret != RT_EOK) goto __exit;
  403. /* release module data */
  404. rt_free(module_ptr);
  405. /* increase module reference count */
  406. module->nref ++;
  407. /* set module initialization and cleanup function */
  408. module->init_func = dlsym(module, "module_init");
  409. module->cleanup_func = dlsym(module, "module_cleanup");
  410. module->stat = RT_DLMODULE_STAT_INIT;
  411. /* do module initialization */
  412. if (module->init_func)
  413. {
  414. module->init_func(module);
  415. }
  416. return module;
  417. __exit:
  418. if (fd >= 0) close(fd);
  419. if (module_ptr) rt_free(module_ptr);
  420. if (module) dlmodule_destroy(module);
  421. return RT_NULL;
  422. }
  423. struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
  424. {
  425. struct rt_dlmodule *module = RT_NULL;
  426. module = dlmodule_load(pgname);
  427. if (module)
  428. {
  429. if (module->entry_addr)
  430. {
  431. /* exec this module */
  432. rt_thread_t tid;
  433. module->cmd_line = rt_strdup(cmd);
  434. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  435. 2048, RT_THREAD_PRIORITY_MAX - 1, 10);
  436. if (tid)
  437. {
  438. tid->module_id = module;
  439. module->main_thread = tid;
  440. rt_thread_startup(tid);
  441. }
  442. else
  443. {
  444. /* destory dl module */
  445. dlmodule_destroy(module);
  446. module = RT_NULL;
  447. }
  448. }
  449. }
  450. return module;
  451. }
  452. void dlmodule_exit(int ret_code)
  453. {
  454. rt_thread_t thread;
  455. struct rt_dlmodule *module;
  456. module = dlmodule_self();
  457. if (!module) return;
  458. /* disable scheduling */
  459. rt_enter_critical();
  460. /* module is not running */
  461. if (module->stat != RT_DLMODULE_STAT_RUNNING)
  462. {
  463. /* restore scheduling */
  464. rt_exit_critical();
  465. return;
  466. }
  467. /* set return code */
  468. module->ret_code = ret_code;
  469. /* do exit for this module */
  470. _dlmodule_exit();
  471. /* the stat of module was changed to CLOSING in _dlmodule_exit */
  472. thread = module->main_thread;
  473. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  474. {
  475. /* main thread already closed */
  476. rt_exit_critical();
  477. return ;
  478. }
  479. /* delete thread: insert to defunct thread list */
  480. rt_thread_delete(thread);
  481. /* enable scheduling */
  482. rt_exit_critical();
  483. }
  484. rt_uint32_t dlmodule_symbol_find(const char *sym_str)
  485. {
  486. /* find in kernel symbol table */
  487. struct rt_module_symtab *index;
  488. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  489. {
  490. if (rt_strcmp(index->name, sym_str) == 0)
  491. return (rt_uint32_t)index->addr;
  492. }
  493. return 0;
  494. }
  495. int rt_system_dlmodule_init(void)
  496. {
  497. #if defined(__GNUC__) && !defined(__CC_ARM)
  498. extern int __rtmsymtab_start;
  499. extern int __rtmsymtab_end;
  500. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  501. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  502. #elif defined (__CC_ARM)
  503. extern int RTMSymTab$$Base;
  504. extern int RTMSymTab$$Limit;
  505. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  506. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  507. #elif defined (__IAR_SYSTEMS_ICC__)
  508. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  509. _rt_module_symtab_end = __section_end("RTMSymTab");
  510. #endif
  511. return 0;
  512. }
  513. INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
  514. /**
  515. * This function will find the specified module.
  516. *
  517. * @param name the name of module finding
  518. *
  519. * @return the module
  520. */
  521. struct rt_dlmodule *dlmodule_find(const char *name)
  522. {
  523. rt_object_t object;
  524. struct rt_dlmodule *ret = RT_NULL;
  525. object = rt_object_find(name, RT_Object_Class_Module);
  526. if (object)
  527. {
  528. ret = (struct rt_dlmodule*) object;
  529. }
  530. return ret;
  531. }
  532. RTM_EXPORT(dlmodule_find);
  533. int list_symbols(void)
  534. {
  535. extern int __rtmsymtab_start;
  536. extern int __rtmsymtab_end;
  537. /* find in kernel symbol table */
  538. struct rt_module_symtab *index;
  539. for (index = _rt_module_symtab_begin;
  540. index != _rt_module_symtab_end;
  541. index ++)
  542. {
  543. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  544. }
  545. return 0;
  546. }
  547. MSH_CMD_EXPORT(list_symbols, list symbols information);
  548. int list_module(void)
  549. {
  550. struct rt_dlmodule *module;
  551. struct rt_list_node *list, *node;
  552. struct rt_object_information *info;
  553. info = rt_object_get_information(RT_Object_Class_Module);
  554. list = &info->object_list;
  555. rt_kprintf("module ref address \n");
  556. rt_kprintf("-------- -------- ------------\n");
  557. for (node = list->next; node != list; node = node->next)
  558. {
  559. module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
  560. rt_kprintf("%-*.*s %-04d 0x%08x\n",
  561. RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
  562. }
  563. return 0;
  564. }
  565. MSH_CMD_EXPORT(list_module, list modules in system);