module.c 38 KB

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