module.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. * 2017-08-20 parai support intel 386 machine
  31. */
  32. #include <rthw.h>
  33. #include <rtthread.h>
  34. #include <rtm.h>
  35. #ifdef RT_USING_FINSH
  36. #include <finsh.h>
  37. #endif
  38. #ifdef RT_USING_MODULE
  39. #include "module.h"
  40. #define elf_module ((Elf32_Ehdr *)module_ptr)
  41. #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
  42. #define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
  43. #define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
  44. #define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
  45. #define IS_REL(s) (s.sh_type == SHT_REL)
  46. #define IS_RELA(s) (s.sh_type == SHT_RELA)
  47. #define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
  48. #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
  49. #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
  50. #ifdef RT_USING_MODULE_STKSZ
  51. #undef RT_USING_MODULE_STKSZ
  52. #endif
  53. #ifndef RT_USING_MODULE_STKSZ
  54. #define RT_USING_MODULE_STKSZ (4096 * 2)
  55. #endif
  56. #ifndef RT_USING_MODULE_PRIO
  57. #define RT_USING_MODULE_PRIO (RT_THREAD_PRIORITY_MAX - 2)
  58. #endif
  59. #ifdef RT_USING_SLAB
  60. #define PAGE_COUNT_MAX 256
  61. /* module memory allocator */
  62. struct rt_mem_head
  63. {
  64. rt_size_t size; /* size of memory block */
  65. struct rt_mem_head *next; /* next valid memory block */
  66. };
  67. struct rt_page_info
  68. {
  69. rt_uint32_t *page_ptr;
  70. rt_uint32_t npage;
  71. };
  72. static void *rt_module_malloc_page(rt_size_t npages);
  73. static void rt_module_free_page(rt_module_t module,
  74. void *page_ptr,
  75. rt_size_t npages);
  76. static struct rt_semaphore mod_sem;
  77. #endif
  78. static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
  79. static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
  80. #if defined(__IAR_SYSTEMS_ICC__) /* for IAR compiler */
  81. #pragma section="RTMSymTab"
  82. #endif
  83. /**
  84. * @ingroup SystemInit
  85. *
  86. * This function will initialize system module
  87. */
  88. int rt_system_module_init(void)
  89. {
  90. #if defined(__GNUC__) && !defined(__CC_ARM)
  91. extern int __rtmsymtab_start;
  92. extern int __rtmsymtab_end;
  93. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  94. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  95. #elif defined (__CC_ARM)
  96. extern int RTMSymTab$$Base;
  97. extern int RTMSymTab$$Limit;
  98. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  99. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  100. #elif defined (__IAR_SYSTEMS_ICC__)
  101. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  102. _rt_module_symtab_end = __section_end("RTMSymTab");
  103. #endif
  104. return 0;
  105. }
  106. INIT_COMPONENT_EXPORT(rt_system_module_init);
  107. #ifdef RT_USING_FINSH
  108. void list_symbol(void)
  109. {
  110. /* find in kernel symbol table */
  111. struct rt_module_symtab *index;
  112. for (index = _rt_module_symtab_begin;
  113. index != _rt_module_symtab_end;
  114. index ++)
  115. {
  116. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  117. }
  118. return ;
  119. }
  120. FINSH_FUNCTION_EXPORT(list_symbol, list symbol for module);
  121. MSH_CMD_EXPORT(list_symbol, list symbol for module);
  122. #endif
  123. static rt_uint32_t rt_module_symbol_find(const char *sym_str)
  124. {
  125. /* find in kernel symbol table */
  126. struct rt_module_symtab *index;
  127. for (index = _rt_module_symtab_begin;
  128. index != _rt_module_symtab_end;
  129. index ++)
  130. {
  131. if (rt_strcmp(index->name, sym_str) == 0)
  132. return (rt_uint32_t)index->addr;
  133. }
  134. return 0;
  135. }
  136. /**
  137. * This function will return self module object
  138. *
  139. * @return the self module object
  140. */
  141. rt_module_t rt_module_self(void)
  142. {
  143. rt_thread_t tid;
  144. tid = rt_thread_self();
  145. if (tid == RT_NULL)
  146. return RT_NULL;
  147. /* return current module */
  148. return (rt_module_t)tid->module_id;
  149. }
  150. RTM_EXPORT(rt_module_self);
  151. static int rt_module_arm_relocate(struct rt_module *module,
  152. Elf32_Rel *rel,
  153. Elf32_Addr sym_val)
  154. {
  155. Elf32_Addr *where, tmp;
  156. Elf32_Sword addend, offset;
  157. rt_uint32_t upper, lower, sign, j1, j2;
  158. where = (Elf32_Addr *)((rt_uint8_t *)module->module_space
  159. + rel->r_offset
  160. - module->vstart_addr);
  161. switch (ELF32_R_TYPE(rel->r_info))
  162. {
  163. case R_ARM_NONE:
  164. break;
  165. case R_ARM_ABS32:
  166. *where += (Elf32_Addr)sym_val;
  167. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n",
  168. where, *where));
  169. break;
  170. case R_ARM_PC24:
  171. case R_ARM_PLT32:
  172. case R_ARM_CALL:
  173. case R_ARM_JUMP24:
  174. addend = *where & 0x00ffffff;
  175. if (addend & 0x00800000)
  176. addend |= 0xff000000;
  177. tmp = sym_val - (Elf32_Addr)where + (addend << 2);
  178. tmp >>= 2;
  179. *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
  180. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n",
  181. where, *where));
  182. break;
  183. case R_ARM_REL32:
  184. *where += sym_val - (Elf32_Addr)where;
  185. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  186. ("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
  187. where, *where, sym_val, rel->r_offset));
  188. break;
  189. case R_ARM_V4BX:
  190. *where &= 0xf000000f;
  191. *where |= 0x01a0f000;
  192. break;
  193. #ifdef MODULE_USING_386
  194. case R_386_GLOB_DAT:
  195. case R_386_JUMP_SLOT:
  196. #endif
  197. case R_ARM_GLOB_DAT:
  198. case R_ARM_JUMP_SLOT:
  199. *where = (Elf32_Addr)sym_val;
  200. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n",
  201. where, *where, sym_val));
  202. break;
  203. #if 0 /* To do */
  204. case R_ARM_GOT_BREL:
  205. temp = (Elf32_Addr)sym_val;
  206. *where = (Elf32_Addr)&temp;
  207. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x\n",
  208. where, *where, sym_val));
  209. break;
  210. #endif
  211. #ifdef MODULE_USING_386
  212. case R_386_RELATIVE:
  213. #endif
  214. case R_ARM_RELATIVE:
  215. *where = (Elf32_Addr)sym_val + *where;
  216. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n",
  217. where, *where, sym_val));
  218. break;
  219. case R_ARM_THM_CALL:
  220. case R_ARM_THM_JUMP24:
  221. upper = *(rt_uint16_t *)where;
  222. lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
  223. sign = (upper >> 10) & 1;
  224. j1 = (lower >> 13) & 1;
  225. j2 = (lower >> 11) & 1;
  226. offset = (sign << 24) |
  227. ((~(j1 ^ sign) & 1) << 23) |
  228. ((~(j2 ^ sign) & 1) << 22) |
  229. ((upper & 0x03ff) << 12) |
  230. ((lower & 0x07ff) << 1);
  231. if (offset & 0x01000000)
  232. offset -= 0x02000000;
  233. offset += sym_val - (Elf32_Addr)where;
  234. if (!(offset & 1) ||
  235. offset <= (rt_int32_t)0xff000000 ||
  236. offset >= (rt_int32_t)0x01000000)
  237. {
  238. rt_kprintf("Module: Only Thumb addresses allowed\n");
  239. return -1;
  240. }
  241. sign = (offset >> 24) & 1;
  242. j1 = sign ^ (~(offset >> 23) & 1);
  243. j2 = sign ^ (~(offset >> 22) & 1);
  244. *(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) |
  245. (sign << 10) |
  246. ((offset >> 12) & 0x03ff));
  247. *(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
  248. (j1 << 13) | (j2 << 11) |
  249. ((offset >> 1) & 0x07ff));
  250. upper = *(rt_uint16_t *)where;
  251. lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
  252. break;
  253. default:
  254. return -1;
  255. }
  256. return 0;
  257. }
  258. #ifdef RT_USING_HOOK
  259. static void (*rt_module_load_hook)(rt_module_t module);
  260. static void (*rt_module_unload_hook)(rt_module_t module);
  261. /**
  262. * @addtogroup Hook
  263. */
  264. /**@{*/
  265. /**
  266. * This function will set a hook function, which will be invoked when module
  267. * be loaded to system.
  268. *
  269. * @param hook the hook function
  270. */
  271. void rt_module_load_sethook(void (*hook)(rt_module_t module))
  272. {
  273. rt_module_load_hook = hook;
  274. }
  275. /**
  276. * This function will set a hook function, which will be invoked when module
  277. * be unloaded from system.
  278. *
  279. * @param hook the hook function
  280. */
  281. void rt_module_unload_sethook(void (*hook)(rt_module_t module))
  282. {
  283. rt_module_unload_hook = hook;
  284. }
  285. /**@}*/
  286. #endif
  287. static struct rt_module *_load_shared_object(const char *name,
  288. void *module_ptr)
  289. {
  290. rt_module_t module = RT_NULL;
  291. rt_bool_t linked = RT_FALSE;
  292. rt_uint32_t index, module_size = 0;
  293. Elf32_Addr vstart_addr, vend_addr;
  294. rt_bool_t has_vstart;
  295. RT_ASSERT(module_ptr != RT_NULL);
  296. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
  297. {
  298. /* rtmlinker finished */
  299. linked = RT_TRUE;
  300. }
  301. /* get the ELF image size */
  302. has_vstart = RT_FALSE;
  303. vstart_addr = vend_addr = RT_NULL;
  304. for (index = 0; index < elf_module->e_phnum; index++)
  305. {
  306. if (phdr[index].p_type != PT_LOAD)
  307. continue;
  308. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("LOAD segment: %d, 0x%p, 0x%08x\n",
  309. index, phdr[index].p_vaddr, phdr[index].p_memsz));
  310. if (phdr[index].p_memsz < phdr[index].p_filesz)
  311. {
  312. rt_kprintf("invalid elf: segment %d: p_memsz: %d, p_filesz: %d\n",
  313. index, phdr[index].p_memsz, phdr[index].p_filesz);
  314. return RT_NULL;
  315. }
  316. if (!has_vstart)
  317. {
  318. vstart_addr = phdr[index].p_vaddr;
  319. vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
  320. has_vstart = RT_TRUE;
  321. if (vend_addr < vstart_addr)
  322. {
  323. rt_kprintf("invalid elf: segment %d: p_vaddr: %d, p_memsz: %d\n",
  324. index, phdr[index].p_vaddr, phdr[index].p_memsz);
  325. return RT_NULL;
  326. }
  327. }
  328. else
  329. {
  330. if (phdr[index].p_vaddr < vend_addr)
  331. {
  332. rt_kprintf("invalid elf: segment should be sorted and not overlapped\n");
  333. return RT_NULL;
  334. }
  335. if (phdr[index].p_vaddr > vend_addr + 16)
  336. {
  337. /* There should not be too much padding in the object files. */
  338. rt_kprintf("warning: too much padding before segment %d\n", index);
  339. }
  340. vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
  341. if (vend_addr < phdr[index].p_vaddr)
  342. {
  343. rt_kprintf("invalid elf: "
  344. "segment %d address overflow\n", index);
  345. return RT_NULL;
  346. }
  347. }
  348. }
  349. module_size = vend_addr - vstart_addr;
  350. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("module size: %d, vstart_addr: 0x%p\n",
  351. module_size, vstart_addr));
  352. if (module_size == 0)
  353. {
  354. rt_kprintf("Module: size error\n");
  355. return RT_NULL;
  356. }
  357. /* allocate module */
  358. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
  359. name);
  360. if (!module)
  361. return RT_NULL;
  362. module->vstart_addr = vstart_addr;
  363. module->nref = 0;
  364. /* allocate module space */
  365. module->module_space = rt_malloc(module_size);
  366. if (module->module_space == RT_NULL)
  367. {
  368. rt_kprintf("Module: allocate space failed.\n");
  369. rt_object_delete(&(module->parent));
  370. return RT_NULL;
  371. }
  372. /* zero all space */
  373. rt_memset(module->module_space, 0, module_size);
  374. for (index = 0; index < elf_module->e_phnum; index++)
  375. {
  376. if (phdr[index].p_type == PT_LOAD)
  377. {
  378. rt_memcpy(module->module_space + phdr[index].p_vaddr - vstart_addr,
  379. (rt_uint8_t *)elf_module + phdr[index].p_offset,
  380. phdr[index].p_filesz);
  381. }
  382. }
  383. /* set module entry */
  384. module->module_entry = module->module_space
  385. + elf_module->e_entry - vstart_addr;
  386. /* handle relocation section */
  387. for (index = 0; index < elf_module->e_shnum; index ++)
  388. {
  389. rt_uint32_t i, nr_reloc;
  390. Elf32_Sym *symtab;
  391. Elf32_Rel *rel;
  392. rt_uint8_t *strtab;
  393. static rt_bool_t unsolved = RT_FALSE;
  394. if (!IS_REL(shdr[index]))
  395. continue;
  396. /* get relocate item */
  397. rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  398. /* locate .rel.plt and .rel.dyn section */
  399. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
  400. shdr[shdr[index].sh_link].sh_offset);
  401. strtab = (rt_uint8_t *)module_ptr +
  402. shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  403. nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
  404. /* relocate every items */
  405. for (i = 0; i < nr_reloc; i ++)
  406. {
  407. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  408. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n",
  409. strtab + sym->st_name,
  410. sym->st_shndx));
  411. if ((sym->st_shndx != SHT_NULL) ||
  412. (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
  413. #ifdef MODULE_USING_386
  414. || ( (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) && (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) )
  415. #endif
  416. )
  417. {
  418. rt_module_arm_relocate(module, rel,
  419. (Elf32_Addr)(module->module_space
  420. + sym->st_value
  421. - vstart_addr));
  422. }
  423. else if (!linked)
  424. {
  425. Elf32_Addr addr;
  426. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  427. strtab + sym->st_name));
  428. /* need to resolve symbol in kernel symbol table */
  429. addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
  430. if (addr == 0)
  431. {
  432. rt_kprintf("Module: can't find %s in kernel symbol table\n",
  433. strtab + sym->st_name);
  434. unsolved = RT_TRUE;
  435. }
  436. else
  437. rt_module_arm_relocate(module, rel, addr);
  438. }
  439. rel ++;
  440. }
  441. if (unsolved)
  442. {
  443. rt_object_delete(&(module->parent));
  444. return RT_NULL;
  445. }
  446. }
  447. /* construct module symbol table */
  448. for (index = 0; index < elf_module->e_shnum; index ++)
  449. {
  450. /* find .dynsym section */
  451. rt_uint8_t *shstrab;
  452. shstrab = (rt_uint8_t *)module_ptr +
  453. shdr[elf_module->e_shstrndx].sh_offset;
  454. if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
  455. break;
  456. }
  457. /* found .dynsym section */
  458. if (index != elf_module->e_shnum)
  459. {
  460. int i, count = 0;
  461. Elf32_Sym *symtab = RT_NULL;
  462. rt_uint8_t *strtab = RT_NULL;
  463. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  464. strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
  465. for (i = 0; i < shdr[index].sh_size / sizeof(Elf32_Sym); i++)
  466. {
  467. if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
  468. (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
  469. count ++;
  470. }
  471. module->symtab = (struct rt_module_symtab *)rt_malloc
  472. (count * sizeof(struct rt_module_symtab));
  473. module->nsym = count;
  474. for (i = 0, count = 0; i < shdr[index].sh_size / sizeof(Elf32_Sym); i++)
  475. {
  476. rt_size_t length;
  477. if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
  478. (ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC))
  479. continue;
  480. length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
  481. module->symtab[count].addr =
  482. (void *)(module->module_space + symtab[i].st_value - module->vstart_addr);
  483. module->symtab[count].name = rt_malloc(length);
  484. rt_memset((void *)module->symtab[count].name, 0, length);
  485. rt_memcpy((void *)module->symtab[count].name,
  486. strtab + symtab[i].st_name,
  487. length);
  488. count ++;
  489. }
  490. }
  491. return module;
  492. }
  493. static struct rt_module* _load_relocated_object(const char *name,
  494. void *module_ptr)
  495. {
  496. rt_uint32_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
  497. rt_uint32_t module_addr = 0, module_size = 0;
  498. struct rt_module *module = RT_NULL;
  499. rt_uint8_t *ptr, *strtab, *shstrab;
  500. /* get the ELF image size */
  501. for (index = 0; index < elf_module->e_shnum; index ++)
  502. {
  503. /* text */
  504. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  505. {
  506. module_size += shdr[index].sh_size;
  507. module_addr = shdr[index].sh_addr;
  508. }
  509. /* rodata */
  510. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  511. {
  512. module_size += shdr[index].sh_size;
  513. }
  514. /* data */
  515. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  516. {
  517. module_size += shdr[index].sh_size;
  518. }
  519. /* bss */
  520. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  521. {
  522. module_size += shdr[index].sh_size;
  523. }
  524. }
  525. /* no text, data and bss on image */
  526. if (module_size == 0)
  527. return RT_NULL;
  528. /* allocate module */
  529. module = (struct rt_module *)
  530. rt_object_allocate(RT_Object_Class_Module, (const char *)name);
  531. if (module == RT_NULL)
  532. return RT_NULL;
  533. module->vstart_addr = 0;
  534. /* allocate module space */
  535. module->module_space = rt_malloc(module_size);
  536. if (module->module_space == RT_NULL)
  537. {
  538. rt_kprintf("Module: allocate space failed.\n");
  539. rt_object_delete(&(module->parent));
  540. return RT_NULL;
  541. }
  542. /* zero all space */
  543. ptr = module->module_space;
  544. rt_memset(ptr, 0, module_size);
  545. /* load text and data section */
  546. for (index = 0; index < elf_module->e_shnum; index ++)
  547. {
  548. /* load text section */
  549. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  550. {
  551. rt_memcpy(ptr,
  552. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  553. shdr[index].sh_size);
  554. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load text 0x%x, size %d\n",
  555. ptr, shdr[index].sh_size));
  556. ptr += shdr[index].sh_size;
  557. }
  558. /* load rodata section */
  559. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  560. {
  561. rt_memcpy(ptr,
  562. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  563. shdr[index].sh_size);
  564. rodata_addr = (rt_uint32_t)ptr;
  565. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  566. ("load rodata 0x%x, size %d, rodata 0x%x\n",
  567. ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
  568. ptr += shdr[index].sh_size;
  569. }
  570. /* load data section */
  571. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  572. {
  573. rt_memcpy(ptr,
  574. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  575. shdr[index].sh_size);
  576. data_addr = (rt_uint32_t)ptr;
  577. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  578. ("load data 0x%x, size %d, data 0x%x\n",
  579. ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
  580. ptr += shdr[index].sh_size;
  581. }
  582. /* load bss section */
  583. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  584. {
  585. rt_memset(ptr, 0, shdr[index].sh_size);
  586. bss_addr = (rt_uint32_t)ptr;
  587. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load bss 0x%x, size %d,\n",
  588. ptr, shdr[index].sh_size));
  589. }
  590. }
  591. /* set module entry */
  592. module->module_entry =
  593. (rt_uint8_t *)module->module_space + elf_module->e_entry - module_addr;
  594. /* handle relocation section */
  595. for (index = 0; index < elf_module->e_shnum; index ++)
  596. {
  597. rt_uint32_t i, nr_reloc;
  598. Elf32_Sym *symtab;
  599. Elf32_Rel *rel;
  600. if (!IS_REL(shdr[index]))
  601. continue;
  602. /* get relocate item */
  603. rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  604. /* locate .dynsym and .dynstr */
  605. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
  606. shdr[shdr[index].sh_link].sh_offset);
  607. strtab = (rt_uint8_t *)module_ptr +
  608. shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  609. shstrab = (rt_uint8_t *)module_ptr +
  610. shdr[elf_module->e_shstrndx].sh_offset;
  611. nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
  612. /* relocate every items */
  613. for (i = 0; i < nr_reloc; i ++)
  614. {
  615. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  616. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  617. strtab + sym->st_name));
  618. if (sym->st_shndx != STN_UNDEF)
  619. {
  620. if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) ||
  621. (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
  622. {
  623. if (rt_strncmp((const char *)(shstrab +
  624. shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
  625. {
  626. /* relocate rodata section */
  627. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rodata\n"));
  628. rt_module_arm_relocate(module, rel,
  629. (Elf32_Addr)(rodata_addr + sym->st_value));
  630. }
  631. else if (rt_strncmp((const char *)
  632. (shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
  633. {
  634. /* relocate bss section */
  635. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("bss\n"));
  636. rt_module_arm_relocate(module, rel,
  637. (Elf32_Addr)bss_addr + sym->st_value);
  638. }
  639. else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name),
  640. ELF_DATA, 6) == 0)
  641. {
  642. /* relocate data section */
  643. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("data\n"));
  644. rt_module_arm_relocate(module, rel,
  645. (Elf32_Addr)data_addr + sym->st_value);
  646. }
  647. }
  648. else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
  649. {
  650. /* relocate function */
  651. rt_module_arm_relocate(module, rel,
  652. (Elf32_Addr)((rt_uint8_t *)
  653. module->module_space
  654. - module_addr
  655. + sym->st_value));
  656. }
  657. }
  658. else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
  659. {
  660. /* relocate function */
  661. rt_module_arm_relocate(module, rel,
  662. (Elf32_Addr)((rt_uint8_t *)
  663. module->module_space
  664. - module_addr
  665. + sym->st_value));
  666. }
  667. else
  668. {
  669. Elf32_Addr addr;
  670. if (ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
  671. {
  672. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  673. strtab + sym->st_name));
  674. /* need to resolve symbol in kernel symbol table */
  675. addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
  676. if (addr != (Elf32_Addr)RT_NULL)
  677. {
  678. rt_module_arm_relocate(module, rel, addr);
  679. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("symbol addr 0x%x\n",
  680. addr));
  681. }
  682. else
  683. rt_kprintf("Module: can't find %s in kernel symbol table\n",
  684. strtab + sym->st_name);
  685. }
  686. else
  687. {
  688. rt_module_arm_relocate(module, rel,
  689. (Elf32_Addr)((rt_uint8_t *)
  690. module->module_space
  691. - module_addr
  692. + sym->st_value));
  693. }
  694. }
  695. rel ++;
  696. }
  697. }
  698. return module;
  699. }
  700. #define RT_MODULE_ARG_MAX 8
  701. static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[])
  702. {
  703. int argc = 0;
  704. char *ptr = cmd;
  705. while ((ptr - cmd) < length)
  706. {
  707. /* strip bank and tab */
  708. while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length)
  709. *ptr++ = '\0';
  710. /* check whether it's the end of line */
  711. if ((ptr - cmd) >= length) break;
  712. /* handle string with quote */
  713. if (*ptr == '"')
  714. {
  715. argv[argc++] = ++ptr;
  716. /* skip this string */
  717. while (*ptr != '"' && (ptr - cmd) < length)
  718. if (*ptr ++ == '\\') ptr ++;
  719. if ((ptr - cmd) >= length) break;
  720. /* skip '"' */
  721. *ptr ++ = '\0';
  722. }
  723. else
  724. {
  725. argv[argc++] = ptr;
  726. while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
  727. ptr ++;
  728. }
  729. if (argc >= RT_MODULE_ARG_MAX) break;
  730. }
  731. return argc;
  732. }
  733. /* module main thread entry */
  734. static void module_main_entry(void *parameter)
  735. {
  736. int argc;
  737. char *argv[RT_MODULE_ARG_MAX];
  738. typedef int (*main_func_t)(int argc, char **argv);
  739. rt_module_t module = (rt_module_t) parameter;
  740. if (module == RT_NULL)
  741. return;
  742. if (module->module_cmd_line == RT_NULL && module->module_cmd_size != 0)
  743. /* malloc for module_cmd_line failed. */
  744. return;
  745. /* FIXME: we should run some C++ initialize code before jump into the
  746. * entry. */
  747. if (module->module_cmd_line == RT_NULL)
  748. {
  749. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("run bare entry: 0x%p\n",
  750. module->module_entry));
  751. ((main_func_t)module->module_entry)(0, RT_NULL);
  752. return;
  753. }
  754. rt_memset(argv, 0x00, sizeof(argv));
  755. argc = _rt_module_split_arg((char *)module->module_cmd_line,
  756. module->module_cmd_size, argv);
  757. if (argc == 0)
  758. return;
  759. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("run main entry: 0x%p with %s\n",
  760. module->module_entry,
  761. module->module_cmd_line));
  762. /* do the main function */
  763. ((main_func_t)module->module_entry)(argc, argv);
  764. return;
  765. }
  766. /**
  767. * This function will load a module with a main function from memory and create a
  768. * main thread for it
  769. *
  770. * @param name the name of module, which shall be unique
  771. * @param module_ptr the memory address of module image
  772. * @argc the count of argument
  773. * @argd the argument data, which should be a
  774. *
  775. * @return the module object
  776. */
  777. rt_module_t rt_module_do_main(const char *name,
  778. void *module_ptr,
  779. const char *cmd_line,
  780. int line_size)
  781. {
  782. rt_module_t module;
  783. RT_DEBUG_NOT_IN_INTERRUPT;
  784. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_load: %s\n", name));
  785. /* check ELF header */
  786. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  787. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  788. {
  789. rt_kprintf("Module: magic error\n");
  790. return RT_NULL;
  791. }
  792. /* check ELF class */
  793. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  794. {
  795. rt_kprintf("Module: ELF class error\n");
  796. return RT_NULL;
  797. }
  798. if (elf_module->e_type == ET_REL)
  799. {
  800. module = _load_relocated_object(name, module_ptr);
  801. }
  802. else if (elf_module->e_type == ET_DYN)
  803. {
  804. module = _load_shared_object(name, module_ptr);
  805. }
  806. else
  807. {
  808. rt_kprintf("Module: unsupported elf type\n");
  809. return RT_NULL;
  810. }
  811. if (module == RT_NULL)
  812. return RT_NULL;
  813. if (line_size && cmd_line)
  814. {
  815. /* set module argument */
  816. module->module_cmd_line = (rt_uint8_t *)rt_malloc(line_size + 1);
  817. if (module->module_cmd_line)
  818. {
  819. rt_memcpy(module->module_cmd_line, cmd_line, line_size);
  820. module->module_cmd_line[line_size] = '\0';
  821. }
  822. module->module_cmd_size = line_size;
  823. }
  824. else
  825. {
  826. /* initialize an empty command */
  827. module->module_cmd_line = RT_NULL;
  828. module->module_cmd_size = 0;
  829. }
  830. /* increase module reference count */
  831. module->nref ++;
  832. if (elf_module->e_entry != 0)
  833. {
  834. /* create module thread */
  835. module->module_thread = rt_thread_create(name,
  836. module_main_entry, module,
  837. RT_USING_MODULE_STKSZ,
  838. RT_USING_MODULE_PRIO, 10);
  839. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("thread entry 0x%x\n",
  840. module->module_entry));
  841. /* set module id */
  842. module->module_thread->module_id = (void *)module;
  843. module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
  844. /* startup module thread */
  845. rt_thread_startup(module->module_thread);
  846. }
  847. else
  848. {
  849. /* without entry point */
  850. module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
  851. }
  852. #ifdef RT_USING_HOOK
  853. if (rt_module_load_hook != RT_NULL)
  854. {
  855. rt_module_load_hook(module);
  856. }
  857. #endif
  858. return module;
  859. }
  860. /**
  861. * This function will load a module from memory and create a thread for it
  862. *
  863. * @param name the name of module, which shall be unique
  864. * @param module_ptr the memory address of module image
  865. *
  866. * @return the module object
  867. */
  868. rt_module_t rt_module_load(const char *name, void *module_ptr)
  869. {
  870. return rt_module_do_main(name, module_ptr, RT_NULL, 0);
  871. }
  872. #ifdef RT_USING_DFS
  873. #include <dfs_posix.h>
  874. static char *_module_name(const char *path)
  875. {
  876. const char *first, *end, *ptr;
  877. char *name;
  878. int size;
  879. ptr = (char *)path;
  880. first = ptr;
  881. end = path + rt_strlen(path);
  882. while (*ptr != '\0')
  883. {
  884. if (*ptr == '/')
  885. first = ptr + 1;
  886. if (*ptr == '.')
  887. end = ptr - 1;
  888. ptr ++;
  889. }
  890. size = end - first + 1;
  891. name = rt_malloc(size);
  892. rt_strncpy(name, first, size);
  893. name[size] = '\0';
  894. return name;
  895. }
  896. /**
  897. * This function will load a module from a file
  898. *
  899. * @param path the full path of application module
  900. *
  901. * @return the module object
  902. */
  903. rt_module_t rt_module_open(const char *path)
  904. {
  905. int fd, length;
  906. struct rt_module *module;
  907. struct stat s;
  908. char *buffer, *offset_ptr;
  909. char *name;
  910. RT_DEBUG_NOT_IN_INTERRUPT;
  911. /* check parameters */
  912. RT_ASSERT(path != RT_NULL);
  913. if (stat(path, &s) != 0)
  914. {
  915. rt_kprintf("Module: access %s failed\n", path);
  916. return RT_NULL;
  917. }
  918. buffer = (char *)rt_malloc(s.st_size);
  919. if (buffer == RT_NULL)
  920. {
  921. rt_kprintf("Module: out of memory\n");
  922. return RT_NULL;
  923. }
  924. offset_ptr = buffer;
  925. fd = open(path, O_RDONLY, 0);
  926. if (fd < 0)
  927. {
  928. rt_kprintf("Module: open %s failed\n", path);
  929. rt_free(buffer);
  930. return RT_NULL;
  931. }
  932. do
  933. {
  934. length = read(fd, offset_ptr, 4096);
  935. if (length > 0)
  936. {
  937. offset_ptr += length;
  938. }
  939. }while (length > 0);
  940. /* close fd */
  941. close(fd);
  942. if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
  943. {
  944. rt_kprintf("Module: read file failed\n");
  945. rt_free(buffer);
  946. return RT_NULL;
  947. }
  948. name = _module_name(path);
  949. module = rt_module_load(name, (void *)buffer);
  950. rt_free(buffer);
  951. rt_free(name);
  952. return module;
  953. }
  954. /**
  955. * This function will do a excutable program with main function and parameters.
  956. *
  957. * @param path the full path of application module
  958. * @param cmd_line the command line of program
  959. * @param size the size of command line of program
  960. *
  961. * @return the module object
  962. */
  963. rt_module_t rt_module_exec_cmd(const char *path, const char *cmd_line, int size)
  964. {
  965. struct stat s;
  966. int fd, length;
  967. char *name, *buffer, *offset_ptr;
  968. struct rt_module *module = RT_NULL;
  969. name = buffer = RT_NULL;
  970. RT_DEBUG_NOT_IN_INTERRUPT;
  971. /* check parameters */
  972. RT_ASSERT(path != RT_NULL);
  973. /* get file size */
  974. if (stat(path, &s) != 0)
  975. {
  976. rt_kprintf("Module: access %s failed\n", path);
  977. goto __exit;
  978. }
  979. /* allocate buffer to save program */
  980. offset_ptr = buffer = (char *)rt_malloc(s.st_size);
  981. if (buffer == RT_NULL)
  982. {
  983. rt_kprintf("Module: out of memory\n");
  984. goto __exit;
  985. }
  986. fd = open(path, O_RDONLY, 0);
  987. if (fd < 0)
  988. {
  989. rt_kprintf("Module: open %s failed\n", path);
  990. goto __exit;
  991. }
  992. do
  993. {
  994. length = read(fd, offset_ptr, 4096);
  995. if (length > 0)
  996. {
  997. offset_ptr += length;
  998. }
  999. }while (length > 0);
  1000. /* close fd */
  1001. close(fd);
  1002. if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
  1003. {
  1004. rt_kprintf("Module: read file failed\n");
  1005. goto __exit;
  1006. }
  1007. /* get module */
  1008. name = _module_name(path);
  1009. /* execute module */
  1010. module = rt_module_do_main(name, (void *)buffer, cmd_line, size);
  1011. __exit:
  1012. rt_free(buffer);
  1013. rt_free(name);
  1014. return module;
  1015. }
  1016. #if defined(RT_USING_FINSH)
  1017. #include <finsh.h>
  1018. FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from a file);
  1019. #endif
  1020. #endif
  1021. /**
  1022. * This function will destroy a module and release its resource.
  1023. *
  1024. * @param module the module to be destroyed.
  1025. *
  1026. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  1027. */
  1028. rt_err_t rt_module_destroy(rt_module_t module)
  1029. {
  1030. int i;
  1031. RT_DEBUG_NOT_IN_INTERRUPT;
  1032. /* check parameter */
  1033. RT_ASSERT(module != RT_NULL);
  1034. RT_ASSERT(module->nref == 0);
  1035. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_destroy: %8.*s\n",
  1036. RT_NAME_MAX, module->parent.name));
  1037. /* module has entry point */
  1038. if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  1039. {
  1040. /* delete command line */
  1041. if (module->module_cmd_line != RT_NULL)
  1042. {
  1043. rt_free(module->module_cmd_line);
  1044. }
  1045. }
  1046. /* release module space memory */
  1047. rt_free(module->module_space);
  1048. /* release module symbol table */
  1049. for (i = 0; i < module->nsym; i ++)
  1050. {
  1051. rt_free((void *)module->symtab[i].name);
  1052. }
  1053. if (module->symtab != RT_NULL)
  1054. rt_free(module->symtab);
  1055. /* delete module object */
  1056. rt_object_delete((rt_object_t)module);
  1057. return RT_EOK;
  1058. }
  1059. /**
  1060. * This function will unload a module from memory and release resources
  1061. *
  1062. * @param module the module to be unloaded
  1063. *
  1064. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  1065. */
  1066. rt_err_t rt_module_unload(rt_module_t module)
  1067. {
  1068. RT_DEBUG_NOT_IN_INTERRUPT;
  1069. /* check parameter */
  1070. if (module == RT_NULL)
  1071. return -RT_ERROR;
  1072. rt_enter_critical();
  1073. /* invoke module cleanup */
  1074. rt_exit_critical();
  1075. #ifdef RT_USING_HOOK
  1076. if (rt_module_unload_hook != RT_NULL)
  1077. {
  1078. rt_module_unload_hook(module);
  1079. }
  1080. #endif
  1081. return RT_EOK;
  1082. }
  1083. /**
  1084. * This function will find the specified module.
  1085. *
  1086. * @param name the name of module finding
  1087. *
  1088. * @return the module
  1089. */
  1090. rt_module_t rt_module_find(const char *name)
  1091. {
  1092. struct rt_object_information *information;
  1093. struct rt_object *object;
  1094. struct rt_list_node *node;
  1095. RT_DEBUG_NOT_IN_INTERRUPT;
  1096. /* enter critical */
  1097. rt_enter_critical();
  1098. /* try to find device object */
  1099. information = rt_object_get_information(RT_Object_Class_Module);
  1100. RT_ASSERT(information != RT_NULL);
  1101. for (node = information->object_list.next;
  1102. node != &(information->object_list);
  1103. node = node->next)
  1104. {
  1105. object = rt_list_entry(node, struct rt_object, list);
  1106. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  1107. {
  1108. /* leave critical */
  1109. rt_exit_critical();
  1110. return (rt_module_t)object;
  1111. }
  1112. }
  1113. /* leave critical */
  1114. rt_exit_critical();
  1115. /* not found */
  1116. return RT_NULL;
  1117. }
  1118. RTM_EXPORT(rt_module_find);
  1119. #endif