module.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. /*
  2. * File : module.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-01-09 Bernard first version
  23. * 2010-04-09 yi.qiu implement based on first version
  24. * 2010-10-23 yi.qiu implement module memory allocator
  25. * 2011-05-25 yi.qiu implement module hook function
  26. * 2011-06-23 yi.qiu rewrite module memory allocator
  27. * 2012-11-23 Bernard using RT_DEBUG_LOG instead of rt_kprintf.
  28. * 2012-11-28 Bernard remove rt_current_module and user
  29. * can use rt_module_unload to remove a module.
  30. */
  31. #include <rthw.h>
  32. #include <rtthread.h>
  33. #include <rtm.h>
  34. #ifdef RT_USING_MODULE
  35. #include "module.h"
  36. #define elf_module ((Elf32_Ehdr *)module_ptr)
  37. #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
  38. #define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
  39. #define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
  40. #define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
  41. #define IS_REL(s) (s.sh_type == SHT_REL)
  42. #define IS_RELA(s) (s.sh_type == SHT_RELA)
  43. #define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
  44. #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
  45. #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
  46. #ifdef RT_USING_SLAB
  47. #define PAGE_COUNT_MAX 256
  48. /* module memory allocator */
  49. struct rt_mem_head
  50. {
  51. rt_size_t size; /* size of memory block */
  52. struct rt_mem_head *next; /* next valid memory block */
  53. };
  54. struct rt_page_info
  55. {
  56. rt_uint32_t *page_ptr;
  57. rt_uint32_t npage;
  58. };
  59. static void *rt_module_malloc_page(rt_size_t npages);
  60. static void rt_module_free_page(rt_module_t module,
  61. void *page_ptr,
  62. rt_size_t npages);
  63. static struct rt_semaphore mod_sem;
  64. #endif
  65. static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
  66. static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
  67. /**
  68. * @ingroup SystemInit
  69. *
  70. * This function will initialize system module
  71. */
  72. int rt_system_module_init(void)
  73. {
  74. #ifdef __GNUC__
  75. extern int __rtmsymtab_start;
  76. extern int __rtmsymtab_end;
  77. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  78. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  79. #elif defined (__CC_ARM)
  80. extern int RTMSymTab$$Base;
  81. extern int RTMSymTab$$Limit;
  82. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  83. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  84. #endif
  85. #ifdef RT_USING_SLAB
  86. /* initialize heap semaphore */
  87. rt_sem_init(&mod_sem, "module", 1, RT_IPC_FLAG_FIFO);
  88. #endif
  89. return 0;
  90. }
  91. INIT_COMPONENT_EXPORT(rt_system_module_init);
  92. static rt_uint32_t rt_module_symbol_find(const char *sym_str)
  93. {
  94. /* find in kernel symbol table */
  95. struct rt_module_symtab *index;
  96. for (index = _rt_module_symtab_begin;
  97. index != _rt_module_symtab_end;
  98. index ++)
  99. {
  100. if (rt_strcmp(index->name, sym_str) == 0)
  101. return (rt_uint32_t)index->addr;
  102. }
  103. return 0;
  104. }
  105. /**
  106. * This function will return self module object
  107. *
  108. * @return the self module object
  109. */
  110. rt_module_t rt_module_self(void)
  111. {
  112. rt_thread_t tid;
  113. tid = rt_thread_self();
  114. if (tid == RT_NULL)
  115. return RT_NULL;
  116. /* return current module */
  117. return (rt_module_t)tid->module_id;
  118. }
  119. static int rt_module_arm_relocate(struct rt_module *module,
  120. Elf32_Rel *rel,
  121. Elf32_Addr sym_val)
  122. {
  123. Elf32_Addr *where, tmp;
  124. Elf32_Sword addend, offset;
  125. rt_uint32_t upper, lower, sign, j1, j2;
  126. where = (Elf32_Addr *)((rt_uint8_t *)module->module_space + rel->r_offset);
  127. switch (ELF32_R_TYPE(rel->r_info))
  128. {
  129. case R_ARM_NONE:
  130. break;
  131. case R_ARM_ABS32:
  132. *where += (Elf32_Addr)sym_val;
  133. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n",
  134. where, *where));
  135. break;
  136. case R_ARM_PC24:
  137. case R_ARM_PLT32:
  138. case R_ARM_CALL:
  139. case R_ARM_JUMP24:
  140. addend = *where & 0x00ffffff;
  141. if (addend & 0x00800000)
  142. addend |= 0xff000000;
  143. tmp = sym_val - (Elf32_Addr)where + (addend << 2);
  144. tmp >>= 2;
  145. *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
  146. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n",
  147. where, *where));
  148. break;
  149. case R_ARM_REL32:
  150. *where += sym_val - (Elf32_Addr)where;
  151. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  152. ("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
  153. where, *where, sym_val, rel->r_offset));
  154. break;
  155. case R_ARM_V4BX:
  156. *where &= 0xf000000f;
  157. *where |= 0x01a0f000;
  158. break;
  159. case R_ARM_GLOB_DAT:
  160. case R_ARM_JUMP_SLOT:
  161. *where = (Elf32_Addr)sym_val;
  162. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n",
  163. where, *where, sym_val));
  164. break;
  165. #if 0 /* To do */
  166. case R_ARM_GOT_BREL:
  167. temp = (Elf32_Addr)sym_val;
  168. *where = (Elf32_Addr)&temp;
  169. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x\n",
  170. where, *where, sym_val));
  171. break;
  172. #endif
  173. case R_ARM_RELATIVE:
  174. *where = (Elf32_Addr)sym_val + *where;
  175. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n",
  176. where, *where, sym_val));
  177. break;
  178. case R_ARM_THM_CALL:
  179. case R_ARM_THM_JUMP24:
  180. upper = *(rt_uint16_t *)where;
  181. lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
  182. sign = (upper >> 10) & 1;
  183. j1 = (lower >> 13) & 1;
  184. j2 = (lower >> 11) & 1;
  185. offset = (sign << 24) |
  186. ((~(j1 ^ sign) & 1) << 23) |
  187. ((~(j2 ^ sign) & 1) << 22) |
  188. ((upper & 0x03ff) << 12) |
  189. ((lower & 0x07ff) << 1);
  190. if (offset & 0x01000000)
  191. offset -= 0x02000000;
  192. offset += sym_val - (Elf32_Addr)where;
  193. if (!(offset & 1) ||
  194. offset <= (rt_int32_t)0xff000000 ||
  195. offset >= (rt_int32_t)0x01000000)
  196. {
  197. rt_kprintf("Module: Only Thumb addresses allowed\n");
  198. return -1;
  199. }
  200. sign = (offset >> 24) & 1;
  201. j1 = sign ^ (~(offset >> 23) & 1);
  202. j2 = sign ^ (~(offset >> 22) & 1);
  203. *(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) |
  204. (sign << 10) |
  205. ((offset >> 12) & 0x03ff));
  206. *(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
  207. (j1 << 13) | (j2 << 11) |
  208. ((offset >> 1) & 0x07ff));
  209. upper = *(rt_uint16_t *)where;
  210. lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
  211. break;
  212. default:
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. static void rt_module_init_object_container(struct rt_module *module)
  218. {
  219. RT_ASSERT(module != RT_NULL);
  220. /* initialize object container - thread */
  221. rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
  222. module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
  223. module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;
  224. #ifdef RT_USING_SEMAPHORE
  225. /* initialize object container - semaphore */
  226. rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
  227. module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
  228. module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
  229. #endif
  230. #ifdef RT_USING_MUTEX
  231. /* initialize object container - mutex */
  232. rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
  233. module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
  234. module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
  235. #endif
  236. #ifdef RT_USING_EVENT
  237. /* initialize object container - event */
  238. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  239. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  240. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  241. #endif
  242. #ifdef RT_USING_MAILBOX
  243. /* initialize object container - mailbox */
  244. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  245. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  246. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  247. #endif
  248. #ifdef RT_USING_MESSAGEQUEUE
  249. /* initialize object container - message queue */
  250. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  251. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  252. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  253. #endif
  254. #ifdef RT_USING_MEMHEAP
  255. /* initialize object container - memory heap */
  256. rt_list_init(&(module->module_object[RT_Object_Class_MemHeap].object_list));
  257. module->module_object[RT_Object_Class_MemHeap].object_size = sizeof(struct rt_memheap);
  258. module->module_object[RT_Object_Class_MemHeap].type = RT_Object_Class_MemHeap;
  259. #endif
  260. #ifdef RT_USING_MEMPOOL
  261. /* initialize object container - memory pool */
  262. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  263. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  264. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  265. #endif
  266. #ifdef RT_USING_DEVICE
  267. /* initialize object container - device */
  268. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  269. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  270. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  271. #endif
  272. /* initialize object container - timer */
  273. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  274. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  275. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  276. }
  277. #ifdef RT_USING_HOOK
  278. static void (*rt_module_load_hook)(rt_module_t module);
  279. static void (*rt_module_unload_hook)(rt_module_t module);
  280. /**
  281. * @addtogroup Hook
  282. */
  283. /*@{*/
  284. /**
  285. * This function will set a hook function, which will be invoked when module
  286. * be loaded to system.
  287. *
  288. * @param hook the hook function
  289. */
  290. void rt_module_load_sethook(void (*hook)(rt_module_t module))
  291. {
  292. rt_module_load_hook = hook;
  293. }
  294. /**
  295. * This function will set a hook function, which will be invoked when module
  296. * be unloaded from system.
  297. *
  298. * @param hook the hook function
  299. */
  300. void rt_module_unload_sethook(void (*hook)(rt_module_t module))
  301. {
  302. rt_module_unload_hook = hook;
  303. }
  304. /*@}*/
  305. #endif
  306. static struct rt_module *_load_shared_object(const char *name,
  307. void *module_ptr)
  308. {
  309. rt_uint8_t *ptr = RT_NULL;
  310. rt_module_t module = RT_NULL;
  311. rt_bool_t linked = RT_FALSE;
  312. rt_uint32_t index, module_size = 0;
  313. RT_ASSERT(module_ptr != RT_NULL);
  314. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
  315. {
  316. /* rtmlinker finished */
  317. linked = RT_TRUE;
  318. }
  319. /* get the ELF image size */
  320. for (index = 0; index < elf_module->e_phnum; index++)
  321. {
  322. if (phdr[index].p_type == PT_LOAD)
  323. module_size += phdr[index].p_memsz;
  324. }
  325. if (module_size == 0)
  326. {
  327. rt_kprintf("Module: size error\n");
  328. return RT_NULL;
  329. }
  330. /* allocate module */
  331. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
  332. name);
  333. if (!module)
  334. return RT_NULL;
  335. module->nref = 0;
  336. /* allocate module space */
  337. module->module_space = rt_malloc(module_size);
  338. if (module->module_space == RT_NULL)
  339. {
  340. rt_kprintf("Module: allocate space failed.\n");
  341. rt_object_delete(&(module->parent));
  342. return RT_NULL;
  343. }
  344. /* zero all space */
  345. ptr = module->module_space;
  346. rt_memset(ptr, 0, module_size);
  347. for (index = 0; index < elf_module->e_phnum; index++)
  348. {
  349. if (phdr[index].p_type == PT_LOAD)
  350. {
  351. rt_memcpy(ptr + phdr[index].p_paddr,
  352. (rt_uint8_t *)elf_module + phdr[index].p_offset,
  353. phdr[index].p_filesz);
  354. }
  355. }
  356. /* set module entry */
  357. module->module_entry = module->module_space + elf_module->e_entry;
  358. /* handle relocation section */
  359. for (index = 0; index < elf_module->e_shnum; index ++)
  360. {
  361. rt_uint32_t i, nr_reloc;
  362. Elf32_Sym *symtab;
  363. Elf32_Rel *rel;
  364. rt_uint8_t *strtab;
  365. static rt_bool_t unsolved = RT_FALSE;
  366. if (!IS_REL(shdr[index]))
  367. continue;
  368. /* get relocate item */
  369. rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  370. /* locate .rel.plt and .rel.dyn section */
  371. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
  372. shdr[shdr[index].sh_link].sh_offset);
  373. strtab = (rt_uint8_t *)module_ptr +
  374. shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  375. nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
  376. /* relocate every items */
  377. for (i = 0; i < nr_reloc; i ++)
  378. {
  379. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  380. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n",
  381. strtab + sym->st_name,
  382. sym->st_shndx));
  383. if ((sym->st_shndx != SHT_NULL) ||
  384. (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
  385. {
  386. rt_module_arm_relocate(module, rel,
  387. (Elf32_Addr)(module->module_space + sym->st_value));
  388. }
  389. else if (!linked)
  390. {
  391. Elf32_Addr addr;
  392. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  393. strtab + sym->st_name));
  394. /* need to resolve symbol in kernel symbol table */
  395. addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
  396. if (addr == 0)
  397. {
  398. rt_kprintf("Module: can't find %s in kernel symbol table\n",
  399. strtab + sym->st_name);
  400. unsolved = RT_TRUE;
  401. }
  402. else
  403. rt_module_arm_relocate(module, rel, addr);
  404. }
  405. rel ++;
  406. }
  407. if (unsolved)
  408. {
  409. rt_object_delete(&(module->parent));
  410. return RT_NULL;
  411. }
  412. }
  413. /* construct module symbol table */
  414. for (index = 0; index < elf_module->e_shnum; index ++)
  415. {
  416. /* find .dynsym section */
  417. rt_uint8_t *shstrab;
  418. shstrab = (rt_uint8_t *)module_ptr +
  419. shdr[elf_module->e_shstrndx].sh_offset;
  420. if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
  421. break;
  422. }
  423. /* found .dynsym section */
  424. if (index != elf_module->e_shnum)
  425. {
  426. int i, count = 0;
  427. Elf32_Sym *symtab = RT_NULL;
  428. rt_uint8_t *strtab = RT_NULL;
  429. symtab =(Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  430. strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
  431. for (i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
  432. {
  433. if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
  434. (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
  435. count ++;
  436. }
  437. module->symtab = (struct rt_module_symtab *)rt_malloc
  438. (count * sizeof(struct rt_module_symtab));
  439. module->nsym = count;
  440. for (i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
  441. {
  442. rt_size_t length;
  443. if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
  444. (ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC))
  445. continue;
  446. length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
  447. module->symtab[count].addr =
  448. (void *)(module->module_space + symtab[i].st_value);
  449. module->symtab[count].name = rt_malloc(length);
  450. rt_memset((void *)module->symtab[count].name, 0, length);
  451. rt_memcpy((void *)module->symtab[count].name,
  452. strtab + symtab[i].st_name,
  453. length);
  454. count ++;
  455. }
  456. }
  457. return module;
  458. }
  459. static struct rt_module* _load_relocated_object(const char *name,
  460. void *module_ptr)
  461. {
  462. rt_uint32_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
  463. rt_uint32_t module_addr = 0, module_size = 0;
  464. struct rt_module *module = RT_NULL;
  465. rt_uint8_t *ptr, *strtab, *shstrab;
  466. /* get the ELF image size */
  467. for (index = 0; index < elf_module->e_shnum; index ++)
  468. {
  469. /* text */
  470. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  471. {
  472. module_size += shdr[index].sh_size;
  473. module_addr = shdr[index].sh_addr;
  474. }
  475. /* rodata */
  476. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  477. {
  478. module_size += shdr[index].sh_size;
  479. }
  480. /* data */
  481. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  482. {
  483. module_size += shdr[index].sh_size;
  484. }
  485. /* bss */
  486. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  487. {
  488. module_size += shdr[index].sh_size;
  489. }
  490. }
  491. /* no text, data and bss on image */
  492. if (module_size == 0)
  493. return RT_NULL;
  494. /* allocate module */
  495. module = (struct rt_module *)
  496. rt_object_allocate(RT_Object_Class_Module, (const char *)name);
  497. if (module == RT_NULL)
  498. return RT_NULL;
  499. /* allocate module space */
  500. module->module_space = rt_malloc(module_size);
  501. if (module->module_space == RT_NULL)
  502. {
  503. rt_kprintf("Module: allocate space failed.\n");
  504. rt_object_delete(&(module->parent));
  505. return RT_NULL;
  506. }
  507. /* zero all space */
  508. ptr = module->module_space;
  509. rt_memset(ptr, 0, module_size);
  510. /* load text and data section */
  511. for (index = 0; index < elf_module->e_shnum; index ++)
  512. {
  513. /* load text section */
  514. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  515. {
  516. rt_memcpy(ptr,
  517. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  518. shdr[index].sh_size);
  519. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load text 0x%x, size %d\n",
  520. ptr, shdr[index].sh_size));
  521. ptr += shdr[index].sh_size;
  522. }
  523. /* load rodata section */
  524. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  525. {
  526. rt_memcpy(ptr,
  527. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  528. shdr[index].sh_size);
  529. rodata_addr = (rt_uint32_t)ptr;
  530. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  531. ("load rodata 0x%x, size %d, rodata 0x%x\n",
  532. ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
  533. ptr += shdr[index].sh_size;
  534. }
  535. /* load data section */
  536. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  537. {
  538. rt_memcpy(ptr,
  539. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  540. shdr[index].sh_size);
  541. data_addr = (rt_uint32_t)ptr;
  542. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  543. ("load data 0x%x, size %d, data 0x%x\n",
  544. ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
  545. ptr += shdr[index].sh_size;
  546. }
  547. /* load bss section */
  548. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  549. {
  550. rt_memset(ptr, 0, shdr[index].sh_size);
  551. bss_addr = (rt_uint32_t)ptr;
  552. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load bss 0x%x, size %d,\n",
  553. ptr, shdr[index].sh_size));
  554. }
  555. }
  556. /* set module entry */
  557. module->module_entry =
  558. (rt_uint8_t *)module->module_space + elf_module->e_entry - module_addr;
  559. /* handle relocation section */
  560. for (index = 0; index < elf_module->e_shnum; index ++)
  561. {
  562. rt_uint32_t i, nr_reloc;
  563. Elf32_Sym *symtab;
  564. Elf32_Rel *rel;
  565. if (!IS_REL(shdr[index]))
  566. continue;
  567. /* get relocate item */
  568. rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  569. /* locate .dynsym and .dynstr */
  570. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
  571. shdr[shdr[index].sh_link].sh_offset);
  572. strtab = (rt_uint8_t *)module_ptr +
  573. shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  574. shstrab = (rt_uint8_t *)module_ptr +
  575. shdr[elf_module->e_shstrndx].sh_offset;
  576. nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
  577. /* relocate every items */
  578. for (i = 0; i < nr_reloc; i ++)
  579. {
  580. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  581. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  582. strtab + sym->st_name));
  583. if (sym->st_shndx != STN_UNDEF)
  584. {
  585. if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) ||
  586. (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
  587. {
  588. if (rt_strncmp((const char *)(shstrab +
  589. shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
  590. {
  591. /* relocate rodata section */
  592. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rodata\n"));
  593. rt_module_arm_relocate(module, rel,
  594. (Elf32_Addr)(rodata_addr + sym->st_value));
  595. }
  596. else if (rt_strncmp((const char*)
  597. (shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
  598. {
  599. /* relocate bss section */
  600. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("bss\n"));
  601. rt_module_arm_relocate(module, rel,
  602. (Elf32_Addr)bss_addr + sym->st_value);
  603. }
  604. else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name),
  605. ELF_DATA, 6) == 0)
  606. {
  607. /* relocate data section */
  608. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("data\n"));
  609. rt_module_arm_relocate(module, rel,
  610. (Elf32_Addr)data_addr + sym->st_value);
  611. }
  612. }
  613. }
  614. else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
  615. {
  616. /* relocate function */
  617. rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t *)
  618. module->module_space - module_addr + sym->st_value));
  619. }
  620. else
  621. {
  622. Elf32_Addr addr;
  623. if (ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
  624. {
  625. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  626. strtab + sym->st_name));
  627. /* need to resolve symbol in kernel symbol table */
  628. addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
  629. if (addr != (Elf32_Addr)RT_NULL)
  630. {
  631. rt_module_arm_relocate(module, rel, addr);
  632. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("symbol addr 0x%x\n",
  633. addr));
  634. }
  635. else
  636. rt_kprintf("Module: can't find %s in kernel symbol table\n",
  637. strtab + sym->st_name);
  638. }
  639. else
  640. {
  641. rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t*)
  642. module->module_space - module_addr + sym->st_value));
  643. }
  644. }
  645. rel ++;
  646. }
  647. }
  648. return module;
  649. }
  650. /**
  651. * This function will load a module from memory and create a thread for it
  652. *
  653. * @param name the name of module, which shall be unique
  654. * @param module_ptr the memory address of module image
  655. *
  656. * @return the module object
  657. */
  658. rt_module_t rt_module_load(const char *name, void *module_ptr)
  659. {
  660. rt_module_t module;
  661. RT_DEBUG_NOT_IN_INTERRUPT;
  662. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_load: %s ,", name));
  663. /* check ELF header */
  664. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  665. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  666. {
  667. rt_kprintf("Module: magic error\n");
  668. return RT_NULL;
  669. }
  670. /* check ELF class */
  671. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  672. {
  673. rt_kprintf("Module: ELF class error\n");
  674. return RT_NULL;
  675. }
  676. if (elf_module->e_type == ET_REL)
  677. {
  678. module = _load_relocated_object(name, module_ptr);
  679. }
  680. else if (elf_module->e_type == ET_DYN)
  681. {
  682. module = _load_shared_object(name, module_ptr);
  683. }
  684. else
  685. {
  686. rt_kprintf("Module: unsupported elf type\n");
  687. return RT_NULL;
  688. }
  689. if (module == RT_NULL)
  690. return RT_NULL;
  691. /* init module object container */
  692. rt_module_init_object_container(module);
  693. /* initialize an empty command */
  694. module->module_cmd_line = RT_NULL;
  695. module->module_cmd_size = 0;
  696. /* increase module reference count */
  697. module->nref ++;
  698. if (elf_module->e_entry != 0)
  699. {
  700. #ifdef RT_USING_SLAB
  701. /* init module memory allocator */
  702. module->mem_list = RT_NULL;
  703. /* create page array */
  704. module->page_array =
  705. (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
  706. module->page_cnt = 0;
  707. #endif
  708. /* create module thread */
  709. module->module_thread = rt_thread_create(name,
  710. (void(*)(void *))module->module_entry, RT_NULL,
  711. 2048, RT_THREAD_PRIORITY_MAX - 2, 10);
  712. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("thread entry 0x%x\n",
  713. module->module_entry));
  714. /* set module id */
  715. module->module_thread->module_id = (void *)module;
  716. module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
  717. /* startup module thread */
  718. rt_thread_startup(module->module_thread);
  719. }
  720. else
  721. {
  722. /* without entry point */
  723. module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
  724. }
  725. #ifdef RT_USING_HOOK
  726. if (rt_module_load_hook != RT_NULL)
  727. {
  728. rt_module_load_hook(module);
  729. }
  730. #endif
  731. return module;
  732. }
  733. #define RT_MODULE_ARG_MAX 8
  734. static int _rt_module_split_arg(char* cmd, rt_size_t length, char* argv[])
  735. {
  736. int argc = 0;
  737. char *ptr = cmd;
  738. while ((ptr - cmd) < length)
  739. {
  740. /* strip bank and tab */
  741. while ((*ptr == ' ' || *ptr == '\t') && (ptr -cmd)< length)
  742. *ptr++ = '\0';
  743. /* check whether it's the end of line */
  744. if ((ptr - cmd)>= length) break;
  745. /* handle string with quote */
  746. if (*ptr == '"')
  747. {
  748. argv[argc++] = ++ptr;
  749. /* skip this string */
  750. while (*ptr != '"' && (ptr-cmd) < length)
  751. if (*ptr ++ == '\\') ptr ++;
  752. if ((ptr - cmd) >= length) break;
  753. /* skip '"' */
  754. *ptr ++ = '\0';
  755. }
  756. else
  757. {
  758. argv[argc++] = ptr;
  759. while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
  760. ptr ++;
  761. }
  762. if (argc >= RT_MODULE_ARG_MAX) break;
  763. }
  764. return argc;
  765. }
  766. /* module main thread entry */
  767. static void module_main_entry(void* parameter)
  768. {
  769. int argc;
  770. char *argv[RT_MODULE_ARG_MAX];
  771. typedef int (*main_func_t)(int argc, char** argv);
  772. rt_module_t module = (rt_module_t) parameter;
  773. if (module == RT_NULL || module->module_cmd_line == RT_NULL) return;
  774. rt_memset(argv, 0x00, sizeof(argv));
  775. argc = _rt_module_split_arg((char*)module->module_cmd_line, module->module_cmd_size, argv);
  776. if (argc == 0) return ;
  777. /* do the main function */
  778. ((main_func_t)module->module_entry)(argc, argv);
  779. return;
  780. }
  781. /**
  782. * This function will load a module with a main function from memory and create a
  783. * main thread for it
  784. *
  785. * @param name the name of module, which shall be unique
  786. * @param module_ptr the memory address of module image
  787. * @argc the count of argument
  788. * @argd the argument data, which should be a
  789. *
  790. * @return the module object
  791. */
  792. rt_module_t rt_module_do_main(const char *name, void *module_ptr, char* cmd_line, int line_size)
  793. {
  794. rt_module_t module;
  795. RT_DEBUG_NOT_IN_INTERRUPT;
  796. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_load: %s ,", name));
  797. /* check ELF header */
  798. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  799. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  800. {
  801. rt_kprintf("Module: magic error\n");
  802. return RT_NULL;
  803. }
  804. /* check ELF class */
  805. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  806. {
  807. rt_kprintf("Module: ELF class error\n");
  808. return RT_NULL;
  809. }
  810. if (elf_module->e_type == ET_REL)
  811. {
  812. module = _load_relocated_object(name, module_ptr);
  813. }
  814. else if (elf_module->e_type == ET_DYN)
  815. {
  816. module = _load_shared_object(name, module_ptr);
  817. }
  818. else
  819. {
  820. rt_kprintf("Module: unsupported excutable program\n");
  821. return RT_NULL;
  822. }
  823. if (module == RT_NULL)
  824. return RT_NULL;
  825. /* init module object container */
  826. rt_module_init_object_container(module);
  827. /* increase module reference count */
  828. module->nref ++;
  829. if (elf_module->e_entry != 0)
  830. {
  831. #ifdef RT_USING_SLAB
  832. /* init module memory allocator */
  833. module->mem_list = RT_NULL;
  834. /* create page array */
  835. module->page_array =
  836. (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
  837. module->page_cnt = 0;
  838. #endif
  839. /* set module argument */
  840. module->module_cmd_line = (rt_uint8_t*)rt_malloc(line_size + 1);
  841. rt_memcpy(module->module_cmd_line, cmd_line, line_size);
  842. module->module_cmd_line[line_size] = '\0';
  843. module->module_cmd_size = line_size;
  844. /* create module thread */
  845. module->module_thread = rt_thread_create(name,
  846. module_main_entry, module,
  847. 2048, RT_THREAD_PRIORITY_MAX - 2, 10);
  848. /* set module id */
  849. module->module_thread->module_id = (void *)module;
  850. module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
  851. /* startup main thread */
  852. rt_thread_startup(module->module_thread);
  853. }
  854. else
  855. {
  856. /* without entry point */
  857. module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
  858. }
  859. #ifdef RT_USING_HOOK
  860. if (rt_module_load_hook != RT_NULL)
  861. {
  862. rt_module_load_hook(module);
  863. }
  864. #endif
  865. return module;
  866. }
  867. #ifdef RT_USING_DFS
  868. #include <dfs_posix.h>
  869. static char* _module_name(const char *path)
  870. {
  871. const char *first, *end, *ptr;
  872. char *name;
  873. int size;
  874. ptr = (char *)path;
  875. first = ptr;
  876. end = path + rt_strlen(path);
  877. while (*ptr != '\0')
  878. {
  879. if (*ptr == '/')
  880. first = ptr + 1;
  881. if (*ptr == '.')
  882. end = ptr - 1;
  883. ptr ++;
  884. }
  885. size = end - first + 1;
  886. name = rt_malloc(size);
  887. rt_strncpy(name, first, size);
  888. name[size] = '\0';
  889. return name;
  890. }
  891. /**
  892. * This function will load a module from a file
  893. *
  894. * @param path the full path of application module
  895. *
  896. * @return the module object
  897. */
  898. rt_module_t rt_module_open(const char *path)
  899. {
  900. int fd, length;
  901. struct rt_module *module;
  902. struct stat s;
  903. char *buffer, *offset_ptr;
  904. char *name;
  905. RT_DEBUG_NOT_IN_INTERRUPT;
  906. /* check parameters */
  907. RT_ASSERT(path != RT_NULL);
  908. if (stat(path, &s) !=0)
  909. {
  910. rt_kprintf("Module: access %s failed\n", path);
  911. return RT_NULL;
  912. }
  913. buffer = (char *)rt_malloc(s.st_size);
  914. if (buffer == RT_NULL)
  915. {
  916. rt_kprintf("Module: out of memory\n");
  917. return RT_NULL;
  918. }
  919. offset_ptr = buffer;
  920. fd = open(path, O_RDONLY, 0);
  921. if (fd < 0)
  922. {
  923. rt_kprintf("Module: open %s failed\n", path);
  924. rt_free(buffer);
  925. return RT_NULL;
  926. }
  927. do
  928. {
  929. length = read(fd, offset_ptr, 4096);
  930. if (length > 0)
  931. {
  932. offset_ptr += length;
  933. }
  934. }while (length > 0);
  935. /* close fd */
  936. close(fd);
  937. if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
  938. {
  939. rt_kprintf("Module: read file failed\n");
  940. rt_free(buffer);
  941. return RT_NULL;
  942. }
  943. name = _module_name(path);
  944. module = rt_module_load(name, (void *)buffer);
  945. rt_free(buffer);
  946. rt_free(name);
  947. return module;
  948. }
  949. /**
  950. * This function will do a excutable program with main function and parameters.
  951. *
  952. * @param path the full path of application module
  953. * @cmd_line the command line of program
  954. * @size the size of command line of program
  955. *
  956. * @return the module object
  957. */
  958. rt_module_t rt_module_exec_cmd(const char *path, char* cmd_line, int size)
  959. {
  960. struct stat s;
  961. int fd, length;
  962. char *name, *buffer, *offset_ptr;
  963. struct rt_module *module = RT_NULL;
  964. name = buffer = RT_NULL;
  965. RT_DEBUG_NOT_IN_INTERRUPT;
  966. /* check parameters */
  967. RT_ASSERT(path != RT_NULL);
  968. /* get file size */
  969. if (stat(path, &s) !=0)
  970. {
  971. rt_kprintf("Module: access %s failed\n", path);
  972. goto __exit;
  973. }
  974. /* allocate buffer to save program */
  975. offset_ptr = buffer = (char *)rt_malloc(s.st_size);
  976. if (buffer == RT_NULL)
  977. {
  978. rt_kprintf("Module: out of memory\n");
  979. goto __exit;
  980. }
  981. fd = open(path, O_RDONLY, 0);
  982. if (fd < 0)
  983. {
  984. rt_kprintf("Module: open %s failed\n", path);
  985. goto __exit;
  986. }
  987. do
  988. {
  989. length = read(fd, offset_ptr, 4096);
  990. if (length > 0)
  991. {
  992. offset_ptr += length;
  993. }
  994. }while (length > 0);
  995. /* close fd */
  996. close(fd);
  997. if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
  998. {
  999. rt_kprintf("Module: read file failed\n");
  1000. goto __exit;
  1001. }
  1002. /* get module */
  1003. name = _module_name(path);
  1004. /* execute module */
  1005. module = rt_module_do_main(name, (void *)buffer, cmd_line, size);
  1006. __exit:
  1007. rt_free(buffer);
  1008. rt_free(name);
  1009. return module;
  1010. }
  1011. #if defined(RT_USING_FINSH)
  1012. #include <finsh.h>
  1013. FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from a file);
  1014. #endif
  1015. #endif
  1016. /**
  1017. * This function will destroy a module and release its resource.
  1018. *
  1019. * @param module the module to be destroyed.
  1020. *
  1021. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  1022. */
  1023. rt_err_t rt_module_destroy(rt_module_t module)
  1024. {
  1025. int i;
  1026. struct rt_object *object;
  1027. struct rt_list_node *list;
  1028. RT_DEBUG_NOT_IN_INTERRUPT;
  1029. /* check parameter */
  1030. RT_ASSERT(module != RT_NULL);
  1031. RT_ASSERT(module->nref == 0);
  1032. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_destroy: %8.*s\n",
  1033. RT_NAME_MAX, module->parent.name));
  1034. /* module has entry point */
  1035. if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  1036. {
  1037. #ifdef RT_USING_SEMAPHORE
  1038. /* delete semaphores */
  1039. list = &module->module_object[RT_Object_Class_Thread].object_list;
  1040. while (list->next != list)
  1041. {
  1042. object = rt_list_entry(list->next, struct rt_object, list);
  1043. if (rt_object_is_systemobject(object) == RT_TRUE)
  1044. {
  1045. /* detach static object */
  1046. rt_sem_detach((rt_sem_t)object);
  1047. }
  1048. else
  1049. {
  1050. /* delete dynamic object */
  1051. rt_sem_delete((rt_sem_t)object);
  1052. }
  1053. }
  1054. #endif
  1055. #ifdef RT_USING_MUTEX
  1056. /* delete mutexs*/
  1057. list = &module->module_object[RT_Object_Class_Mutex].object_list;
  1058. while (list->next != list)
  1059. {
  1060. object = rt_list_entry(list->next, struct rt_object, list);
  1061. if (rt_object_is_systemobject(object) == RT_TRUE)
  1062. {
  1063. /* detach static object */
  1064. rt_mutex_detach((rt_mutex_t)object);
  1065. }
  1066. else
  1067. {
  1068. /* delete dynamic object */
  1069. rt_mutex_delete((rt_mutex_t)object);
  1070. }
  1071. }
  1072. #endif
  1073. #ifdef RT_USING_EVENT
  1074. /* delete mailboxs */
  1075. list = &module->module_object[RT_Object_Class_Event].object_list;
  1076. while (list->next != list)
  1077. {
  1078. object = rt_list_entry(list->next, struct rt_object, list);
  1079. if (rt_object_is_systemobject(object) == RT_TRUE)
  1080. {
  1081. /* detach static object */
  1082. rt_event_detach((rt_event_t)object);
  1083. }
  1084. else
  1085. {
  1086. /* delete dynamic object */
  1087. rt_event_delete((rt_event_t)object);
  1088. }
  1089. }
  1090. #endif
  1091. #ifdef RT_USING_MAILBOX
  1092. /* delete mailboxs */
  1093. list = &module->module_object[RT_Object_Class_MailBox].object_list;
  1094. while (list->next != list)
  1095. {
  1096. object = rt_list_entry(list->next, struct rt_object, list);
  1097. if (rt_object_is_systemobject(object) == RT_TRUE)
  1098. {
  1099. /* detach static object */
  1100. rt_mb_detach((rt_mailbox_t)object);
  1101. }
  1102. else
  1103. {
  1104. /* delete dynamic object */
  1105. rt_mb_delete((rt_mailbox_t)object);
  1106. }
  1107. }
  1108. #endif
  1109. #ifdef RT_USING_MESSAGEQUEUE
  1110. /* delete msgqueues */
  1111. list = &module->module_object[RT_Object_Class_MessageQueue].object_list;
  1112. while (list->next != list)
  1113. {
  1114. object = rt_list_entry(list->next, struct rt_object, list);
  1115. if (rt_object_is_systemobject(object) == RT_TRUE)
  1116. {
  1117. /* detach static object */
  1118. rt_mq_detach((rt_mq_t)object);
  1119. }
  1120. else
  1121. {
  1122. /* delete dynamic object */
  1123. rt_mq_delete((rt_mq_t)object);
  1124. }
  1125. }
  1126. #endif
  1127. #ifdef RT_USING_MEMPOOL
  1128. /* delete mempools */
  1129. list = &module->module_object[RT_Object_Class_MemPool].object_list;
  1130. while (list->next != list)
  1131. {
  1132. object = rt_list_entry(list->next, struct rt_object, list);
  1133. if (rt_object_is_systemobject(object) == RT_TRUE)
  1134. {
  1135. /* detach static object */
  1136. rt_mp_detach((rt_mp_t)object);
  1137. }
  1138. else
  1139. {
  1140. /* delete dynamic object */
  1141. rt_mp_delete((rt_mp_t)object);
  1142. }
  1143. }
  1144. #endif
  1145. #ifdef RT_USING_DEVICE
  1146. /* delete devices */
  1147. list = &module->module_object[RT_Object_Class_Device].object_list;
  1148. while (list->next != list)
  1149. {
  1150. object = rt_list_entry(list->next, struct rt_object, list);
  1151. rt_device_unregister((rt_device_t)object);
  1152. }
  1153. #endif
  1154. /* delete timers */
  1155. list = &module->module_object[RT_Object_Class_Timer].object_list;
  1156. while (list->next != list)
  1157. {
  1158. object = rt_list_entry(list->next, struct rt_object, list);
  1159. if (rt_object_is_systemobject(object) == RT_TRUE)
  1160. {
  1161. /* detach static object */
  1162. rt_timer_detach((rt_timer_t)object);
  1163. }
  1164. else
  1165. {
  1166. /* delete dynamic object */
  1167. rt_timer_delete((rt_timer_t)object);
  1168. }
  1169. }
  1170. /* delete command line */
  1171. if (module->module_cmd_line != RT_NULL)
  1172. {
  1173. rt_free(module->module_cmd_line);
  1174. }
  1175. }
  1176. #ifdef RT_USING_SLAB
  1177. if (module->page_cnt > 0)
  1178. {
  1179. struct rt_page_info *page = (struct rt_page_info *)module->page_array;
  1180. rt_kprintf("Module: warning - memory still hasn't been free finished\n");
  1181. while (module->page_cnt != 0)
  1182. {
  1183. rt_module_free_page(module, page[0].page_ptr, page[0].npage);
  1184. }
  1185. }
  1186. #endif
  1187. /* release module space memory */
  1188. rt_free(module->module_space);
  1189. /* release module symbol table */
  1190. for (i = 0; i < module->nsym; i ++)
  1191. {
  1192. rt_free((void *)module->symtab[i].name);
  1193. }
  1194. if (module->symtab != RT_NULL)
  1195. rt_free(module->symtab);
  1196. #ifdef RT_USING_SLAB
  1197. if (module->page_array != RT_NULL)
  1198. rt_free(module->page_array);
  1199. #endif
  1200. /* delete module object */
  1201. rt_object_delete((rt_object_t)module);
  1202. return RT_EOK;
  1203. }
  1204. /**
  1205. * This function will unload a module from memory and release resources
  1206. *
  1207. * @param module the module to be unloaded
  1208. *
  1209. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  1210. */
  1211. rt_err_t rt_module_unload(rt_module_t module)
  1212. {
  1213. struct rt_object *object;
  1214. struct rt_list_node *list;
  1215. RT_DEBUG_NOT_IN_INTERRUPT;
  1216. /* check parameter */
  1217. if (module == RT_NULL)
  1218. return -RT_ERROR;
  1219. rt_enter_critical();
  1220. if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  1221. {
  1222. /* delete all sub-threads */
  1223. list = &module->module_object[RT_Object_Class_Thread].object_list;
  1224. while (list->next != list)
  1225. {
  1226. object = rt_list_entry(list->next, struct rt_object, list);
  1227. if (rt_object_is_systemobject(object) == RT_TRUE)
  1228. {
  1229. /* detach static object */
  1230. rt_thread_detach((rt_thread_t)object);
  1231. }
  1232. else
  1233. {
  1234. /* delete dynamic object */
  1235. rt_thread_delete((rt_thread_t)object);
  1236. }
  1237. }
  1238. /* delete the main thread of module */
  1239. if (module->module_thread != RT_NULL)
  1240. {
  1241. rt_thread_delete(module->module_thread);
  1242. }
  1243. }
  1244. rt_exit_critical();
  1245. #ifdef RT_USING_HOOK
  1246. if (rt_module_unload_hook != RT_NULL)
  1247. {
  1248. rt_module_unload_hook(module);
  1249. }
  1250. #endif
  1251. return RT_EOK;
  1252. }
  1253. /**
  1254. * This function will find the specified module.
  1255. *
  1256. * @param name the name of module finding
  1257. *
  1258. * @return the module
  1259. */
  1260. rt_module_t rt_module_find(const char *name)
  1261. {
  1262. struct rt_object_information *information;
  1263. struct rt_object *object;
  1264. struct rt_list_node *node;
  1265. extern struct rt_object_information rt_object_container[];
  1266. RT_DEBUG_NOT_IN_INTERRUPT;
  1267. /* enter critical */
  1268. rt_enter_critical();
  1269. /* try to find device object */
  1270. information = &rt_object_container[RT_Object_Class_Module];
  1271. for (node = information->object_list.next;
  1272. node != &(information->object_list);
  1273. node = node->next)
  1274. {
  1275. object = rt_list_entry(node, struct rt_object, list);
  1276. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  1277. {
  1278. /* leave critical */
  1279. rt_exit_critical();
  1280. return (rt_module_t)object;
  1281. }
  1282. }
  1283. /* leave critical */
  1284. rt_exit_critical();
  1285. /* not found */
  1286. return RT_NULL;
  1287. }
  1288. #ifdef RT_USING_SLAB
  1289. /*
  1290. * This function will allocate the numbers page with specified size
  1291. * in page memory.
  1292. *
  1293. * @param size the size of memory to be allocated.
  1294. * @note this function is used for RT-Thread Application Module
  1295. */
  1296. static void *rt_module_malloc_page(rt_size_t npages)
  1297. {
  1298. void *chunk;
  1299. struct rt_page_info *page;
  1300. rt_module_t self_module;
  1301. self_module = rt_module_self();
  1302. RT_ASSERT(self_module != RT_NULL);
  1303. chunk = rt_page_alloc(npages);
  1304. if (chunk == RT_NULL)
  1305. return RT_NULL;
  1306. page = (struct rt_page_info *)self_module->page_array;
  1307. page[self_module->page_cnt].page_ptr = chunk;
  1308. page[self_module->page_cnt].npage = npages;
  1309. self_module->page_cnt ++;
  1310. RT_ASSERT(self_module->page_cnt <= PAGE_COUNT_MAX);
  1311. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_malloc_page 0x%x %d\n",
  1312. chunk, npages));
  1313. return chunk;
  1314. }
  1315. /*
  1316. * This function will release the previously allocated memory page
  1317. * by rt_malloc_page.
  1318. *
  1319. * @param page_ptr the page address to be released.
  1320. * @param npages the number of page shall be released.
  1321. *
  1322. * @note this function is used for RT-Thread Application Module
  1323. */
  1324. static void rt_module_free_page(rt_module_t module,
  1325. void *page_ptr,
  1326. rt_size_t npages)
  1327. {
  1328. int i, index;
  1329. struct rt_page_info *page;
  1330. rt_module_t self_module;
  1331. self_module = rt_module_self();
  1332. RT_ASSERT(self_module != RT_NULL);
  1333. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_free_page 0x%x %d\n",
  1334. page_ptr, npages));
  1335. rt_page_free(page_ptr, npages);
  1336. page = (struct rt_page_info *)module->page_array;
  1337. for (i = 0; i < module->page_cnt; i ++)
  1338. {
  1339. if (page[i].page_ptr == page_ptr)
  1340. {
  1341. if (page[i].npage == npages + 1)
  1342. {
  1343. page[i].page_ptr +=
  1344. npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t);
  1345. page[i].npage -= npages;
  1346. }
  1347. else if (page[i].npage == npages)
  1348. {
  1349. for (index = i; index < module->page_cnt-1; index ++)
  1350. {
  1351. page[index].page_ptr = page[index + 1].page_ptr;
  1352. page[index].npage = page[index + 1].npage;
  1353. }
  1354. page[module->page_cnt - 1].page_ptr = RT_NULL;
  1355. page[module->page_cnt - 1].npage = 0;
  1356. module->page_cnt --;
  1357. }
  1358. else
  1359. RT_ASSERT(RT_FALSE);
  1360. self_module->page_cnt --;
  1361. return;
  1362. }
  1363. }
  1364. /* should not get here */
  1365. RT_ASSERT(RT_FALSE);
  1366. }
  1367. /**
  1368. * rt_module_malloc - allocate memory block in free list
  1369. */
  1370. void *rt_module_malloc(rt_size_t size)
  1371. {
  1372. struct rt_mem_head *b, *n, *up;
  1373. struct rt_mem_head **prev;
  1374. rt_uint32_t npage;
  1375. rt_size_t nunits;
  1376. rt_module_t self_module;
  1377. self_module = rt_module_self();
  1378. RT_ASSERT(self_module != RT_NULL);
  1379. RT_DEBUG_NOT_IN_INTERRUPT;
  1380. nunits = (size + sizeof(struct rt_mem_head) - 1) /
  1381. sizeof(struct rt_mem_head)
  1382. + 1;
  1383. RT_ASSERT(size != 0);
  1384. RT_ASSERT(nunits != 0);
  1385. rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
  1386. for (prev = (struct rt_mem_head **)&self_module->mem_list;
  1387. (b = *prev) != RT_NULL;
  1388. prev = &(b->next))
  1389. {
  1390. if (b->size > nunits)
  1391. {
  1392. /* split memory */
  1393. n = b + nunits;
  1394. n->next = b->next;
  1395. n->size = b->size - nunits;
  1396. b->size = nunits;
  1397. *prev = n;
  1398. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_malloc 0x%x, %d\n",
  1399. b + 1, size));
  1400. rt_sem_release(&mod_sem);
  1401. return (void *)(b + 1);
  1402. }
  1403. if (b->size == nunits)
  1404. {
  1405. /* this node fit, remove this node */
  1406. *prev = b->next;
  1407. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_malloc 0x%x, %d\n",
  1408. b + 1, size));
  1409. rt_sem_release(&mod_sem);
  1410. return (void *)(b + 1);
  1411. }
  1412. }
  1413. /* allocate pages from system heap */
  1414. npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1) /
  1415. RT_MM_PAGE_SIZE;
  1416. if ((up = (struct rt_mem_head *)rt_module_malloc_page(npage)) == RT_NULL)
  1417. return RT_NULL;
  1418. up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
  1419. for (prev = (struct rt_mem_head **)&self_module->mem_list;
  1420. (b = *prev) != RT_NULL;
  1421. prev = &(b->next))
  1422. {
  1423. if (b > up + up->size)
  1424. break;
  1425. }
  1426. up->next = b;
  1427. *prev = up;
  1428. rt_sem_release(&mod_sem);
  1429. return rt_module_malloc(size);
  1430. }
  1431. /**
  1432. * rt_module_free - free memory block in free list
  1433. */
  1434. void rt_module_free(rt_module_t module, void *addr)
  1435. {
  1436. struct rt_mem_head *b, *n, *r;
  1437. struct rt_mem_head **prev;
  1438. RT_DEBUG_NOT_IN_INTERRUPT;
  1439. RT_ASSERT(addr);
  1440. RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0);
  1441. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_free 0x%x\n", addr));
  1442. rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
  1443. n = (struct rt_mem_head *)addr - 1;
  1444. prev = (struct rt_mem_head **)&module->mem_list;
  1445. while ((b = *prev) != RT_NULL)
  1446. {
  1447. RT_ASSERT(b->size > 0);
  1448. RT_ASSERT(b > n || b + b->size <= n);
  1449. if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0))
  1450. {
  1451. if (b + (b->size + n->size) == b->next)
  1452. {
  1453. b->size += b->next->size + n->size;
  1454. b->next = b->next->next;
  1455. }
  1456. else
  1457. b->size += n->size;
  1458. if ((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0)
  1459. {
  1460. int npage =
  1461. b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
  1462. if (npage > 0)
  1463. {
  1464. if ((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
  1465. {
  1466. rt_size_t nunits = npage *
  1467. RT_MM_PAGE_SIZE /
  1468. sizeof(struct rt_mem_head);
  1469. /* split memory */
  1470. r = b + nunits;
  1471. r->next = b->next;
  1472. r->size = b->size - nunits;
  1473. *prev = r;
  1474. }
  1475. else
  1476. {
  1477. *prev = b->next;
  1478. }
  1479. rt_module_free_page(module, b, npage);
  1480. }
  1481. }
  1482. /* unlock */
  1483. rt_sem_release(&mod_sem);
  1484. return;
  1485. }
  1486. if (b == n + n->size)
  1487. {
  1488. n->size = b->size + n->size;
  1489. n->next = b->next;
  1490. if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
  1491. {
  1492. int npage =
  1493. n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
  1494. if (npage > 0)
  1495. {
  1496. if ((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
  1497. {
  1498. rt_size_t nunits = npage *
  1499. RT_MM_PAGE_SIZE /
  1500. sizeof(struct rt_mem_head);
  1501. /* split memory */
  1502. r = n + nunits;
  1503. r->next = n->next;
  1504. r->size = n->size - nunits;
  1505. *prev = r;
  1506. }
  1507. else
  1508. *prev = n->next;
  1509. rt_module_free_page(module, n, npage);
  1510. }
  1511. }
  1512. else
  1513. {
  1514. *prev = n;
  1515. }
  1516. /* unlock */
  1517. rt_sem_release(&mod_sem);
  1518. return;
  1519. }
  1520. if (b > n + n->size)
  1521. break;
  1522. prev = &(b->next);
  1523. }
  1524. if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
  1525. {
  1526. int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
  1527. if (npage > 0)
  1528. {
  1529. rt_module_free_page(module, n, npage);
  1530. if (n->size % RT_MM_PAGE_SIZE != 0)
  1531. {
  1532. rt_size_t nunits =
  1533. npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
  1534. /* split memory */
  1535. r = n + nunits;
  1536. r->next = b;
  1537. r->size = n->size - nunits;
  1538. *prev = r;
  1539. }
  1540. else
  1541. {
  1542. *prev = b;
  1543. }
  1544. }
  1545. }
  1546. else
  1547. {
  1548. n->next = b;
  1549. *prev = n;
  1550. }
  1551. /* unlock */
  1552. rt_sem_release(&mod_sem);
  1553. }
  1554. /**
  1555. * rt_module_realloc - realloc memory block in free list
  1556. */
  1557. void *rt_module_realloc(void *ptr, rt_size_t size)
  1558. {
  1559. struct rt_mem_head *b, *p, *prev, *tmpp;
  1560. rt_size_t nunits;
  1561. rt_module_t self_module;
  1562. self_module = rt_module_self();
  1563. RT_ASSERT(self_module != RT_NULL);
  1564. RT_DEBUG_NOT_IN_INTERRUPT;
  1565. if (!ptr)
  1566. return rt_module_malloc(size);
  1567. if (size == 0)
  1568. {
  1569. rt_module_free(self_module, ptr);
  1570. return RT_NULL;
  1571. }
  1572. nunits = (size + sizeof(struct rt_mem_head) - 1) /
  1573. sizeof(struct rt_mem_head)
  1574. +1;
  1575. b = (struct rt_mem_head *)ptr - 1;
  1576. if (nunits <= b->size)
  1577. {
  1578. /* new size is smaller or equal then before */
  1579. if (nunits == b->size)
  1580. return ptr;
  1581. else
  1582. {
  1583. p = b + nunits;
  1584. p->size = b->size - nunits;
  1585. b->size = nunits;
  1586. rt_module_free(self_module, (void *)(p + 1));
  1587. return (void *)(b + 1);
  1588. }
  1589. }
  1590. else
  1591. {
  1592. /* more space then required */
  1593. prev = (struct rt_mem_head *)self_module->mem_list;
  1594. for (p = prev->next;
  1595. p != (b->size + b) && p != RT_NULL;
  1596. prev = p, p = p->next)
  1597. {
  1598. break;
  1599. }
  1600. /* available block after ap in freelist */
  1601. if (p != RT_NULL &&
  1602. (p->size >= (nunits - (b->size))) &&
  1603. p == (b + b->size))
  1604. {
  1605. /* perfect match */
  1606. if (p->size == (nunits - (b->size)))
  1607. {
  1608. b->size = nunits;
  1609. prev->next = p->next;
  1610. }
  1611. else /* more space then required, split block */
  1612. {
  1613. /* pointer to old header */
  1614. tmpp = p;
  1615. p = b + nunits;
  1616. /* restoring old pointer */
  1617. p->next = tmpp->next;
  1618. /* new size for p */
  1619. p->size = tmpp->size + b->size - nunits;
  1620. b->size = nunits;
  1621. prev->next = p;
  1622. }
  1623. self_module->mem_list = (void *)prev;
  1624. return (void *)(b + 1);
  1625. }
  1626. else /* allocate new memory and copy old data */
  1627. {
  1628. if ((p = rt_module_malloc(size)) == RT_NULL)
  1629. return RT_NULL;
  1630. rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head)));
  1631. rt_module_free(self_module, (void *)(b + 1));
  1632. return (void *)(p);
  1633. }
  1634. }
  1635. }
  1636. #ifdef RT_USING_FINSH
  1637. #include <finsh.h>
  1638. void list_memlist(const char *name)
  1639. {
  1640. rt_module_t module;
  1641. struct rt_mem_head **prev;
  1642. struct rt_mem_head *b;
  1643. module = rt_module_find(name);
  1644. if (module == RT_NULL)
  1645. return;
  1646. for (prev = (struct rt_mem_head **)&module->mem_list;
  1647. (b = *prev) != RT_NULL;
  1648. prev = &(b->next))
  1649. {
  1650. rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head));
  1651. }
  1652. }
  1653. FINSH_FUNCTION_EXPORT(list_memlist, list module free memory information)
  1654. void list_mempage(const char *name)
  1655. {
  1656. rt_module_t module;
  1657. struct rt_page_info *page;
  1658. int i;
  1659. module = rt_module_find(name);
  1660. if (module == RT_NULL)
  1661. return;
  1662. page = (struct rt_page_info *)module->page_array;
  1663. for (i = 0; i < module->page_cnt; i ++)
  1664. {
  1665. rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage);
  1666. }
  1667. }
  1668. FINSH_FUNCTION_EXPORT(list_mempage, list module using memory page information)
  1669. #endif
  1670. #endif
  1671. #endif