2
0

elfload.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. /* This is the Linux kernel elf-loading code, ported into user space */
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <sys/mman.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "qemu.h"
  11. #include "disas.h"
  12. #ifdef _ARCH_PPC64
  13. #undef ARCH_DLINFO
  14. #undef ELF_PLATFORM
  15. #undef ELF_HWCAP
  16. #undef ELF_CLASS
  17. #undef ELF_DATA
  18. #undef ELF_ARCH
  19. #endif
  20. /* from personality.h */
  21. /*
  22. * Flags for bug emulation.
  23. *
  24. * These occupy the top three bytes.
  25. */
  26. enum {
  27. ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
  28. FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to descriptors
  29. * (signal handling)
  30. */
  31. MMAP_PAGE_ZERO = 0x0100000,
  32. ADDR_COMPAT_LAYOUT = 0x0200000,
  33. READ_IMPLIES_EXEC = 0x0400000,
  34. ADDR_LIMIT_32BIT = 0x0800000,
  35. SHORT_INODE = 0x1000000,
  36. WHOLE_SECONDS = 0x2000000,
  37. STICKY_TIMEOUTS = 0x4000000,
  38. ADDR_LIMIT_3GB = 0x8000000,
  39. };
  40. /*
  41. * Personality types.
  42. *
  43. * These go in the low byte. Avoid using the top bit, it will
  44. * conflict with error returns.
  45. */
  46. enum {
  47. PER_LINUX = 0x0000,
  48. PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
  49. PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
  50. PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
  51. PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
  52. PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS |
  53. WHOLE_SECONDS | SHORT_INODE,
  54. PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
  55. PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
  56. PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
  57. PER_BSD = 0x0006,
  58. PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
  59. PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
  60. PER_LINUX32 = 0x0008,
  61. PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
  62. PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
  63. PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
  64. PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
  65. PER_RISCOS = 0x000c,
  66. PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
  67. PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
  68. PER_OSF4 = 0x000f, /* OSF/1 v4 */
  69. PER_HPUX = 0x0010,
  70. PER_MASK = 0x00ff,
  71. };
  72. /*
  73. * Return the base personality without flags.
  74. */
  75. #define personality(pers) (pers & PER_MASK)
  76. /* this flag is uneffective under linux too, should be deleted */
  77. #ifndef MAP_DENYWRITE
  78. #define MAP_DENYWRITE 0
  79. #endif
  80. /* should probably go in elf.h */
  81. #ifndef ELIBBAD
  82. #define ELIBBAD 80
  83. #endif
  84. #ifdef TARGET_I386
  85. #define ELF_PLATFORM get_elf_platform()
  86. static const char *get_elf_platform(void)
  87. {
  88. static char elf_platform[] = "i386";
  89. int family = (thread_env->cpuid_version >> 8) & 0xff;
  90. if (family > 6)
  91. family = 6;
  92. if (family >= 3)
  93. elf_platform[1] = '0' + family;
  94. return elf_platform;
  95. }
  96. #define ELF_HWCAP get_elf_hwcap()
  97. static uint32_t get_elf_hwcap(void)
  98. {
  99. return thread_env->cpuid_features;
  100. }
  101. #ifdef TARGET_X86_64
  102. #define ELF_START_MMAP 0x2aaaaab000ULL
  103. #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
  104. #define ELF_CLASS ELFCLASS64
  105. #define ELF_DATA ELFDATA2LSB
  106. #define ELF_ARCH EM_X86_64
  107. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  108. {
  109. regs->rax = 0;
  110. regs->rsp = infop->start_stack;
  111. regs->rip = infop->entry;
  112. if (bsd_type == target_freebsd) {
  113. regs->rdi = infop->start_stack;
  114. }
  115. }
  116. #else
  117. #define ELF_START_MMAP 0x80000000
  118. /*
  119. * This is used to ensure we don't load something for the wrong architecture.
  120. */
  121. #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
  122. /*
  123. * These are used to set parameters in the core dumps.
  124. */
  125. #define ELF_CLASS ELFCLASS32
  126. #define ELF_DATA ELFDATA2LSB
  127. #define ELF_ARCH EM_386
  128. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  129. {
  130. regs->esp = infop->start_stack;
  131. regs->eip = infop->entry;
  132. /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
  133. starts %edx contains a pointer to a function which might be
  134. registered using `atexit'. This provides a mean for the
  135. dynamic linker to call DT_FINI functions for shared libraries
  136. that have been loaded before the code runs.
  137. A value of 0 tells we have no such handler. */
  138. regs->edx = 0;
  139. }
  140. #endif
  141. #define USE_ELF_CORE_DUMP
  142. #define ELF_EXEC_PAGESIZE 4096
  143. #endif
  144. #ifdef TARGET_ARM
  145. #define ELF_START_MMAP 0x80000000
  146. #define elf_check_arch(x) ( (x) == EM_ARM )
  147. #define ELF_CLASS ELFCLASS32
  148. #ifdef TARGET_WORDS_BIGENDIAN
  149. #define ELF_DATA ELFDATA2MSB
  150. #else
  151. #define ELF_DATA ELFDATA2LSB
  152. #endif
  153. #define ELF_ARCH EM_ARM
  154. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  155. {
  156. abi_long stack = infop->start_stack;
  157. memset(regs, 0, sizeof(*regs));
  158. regs->ARM_cpsr = 0x10;
  159. if (infop->entry & 1)
  160. regs->ARM_cpsr |= CPSR_T;
  161. regs->ARM_pc = infop->entry & 0xfffffffe;
  162. regs->ARM_sp = infop->start_stack;
  163. /* FIXME - what to for failure of get_user()? */
  164. get_user_ual(regs->ARM_r2, stack + 8); /* envp */
  165. get_user_ual(regs->ARM_r1, stack + 4); /* envp */
  166. /* XXX: it seems that r0 is zeroed after ! */
  167. regs->ARM_r0 = 0;
  168. /* For uClinux PIC binaries. */
  169. /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
  170. regs->ARM_r10 = infop->start_data;
  171. }
  172. #define USE_ELF_CORE_DUMP
  173. #define ELF_EXEC_PAGESIZE 4096
  174. enum
  175. {
  176. ARM_HWCAP_ARM_SWP = 1 << 0,
  177. ARM_HWCAP_ARM_HALF = 1 << 1,
  178. ARM_HWCAP_ARM_THUMB = 1 << 2,
  179. ARM_HWCAP_ARM_26BIT = 1 << 3,
  180. ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
  181. ARM_HWCAP_ARM_FPA = 1 << 5,
  182. ARM_HWCAP_ARM_VFP = 1 << 6,
  183. ARM_HWCAP_ARM_EDSP = 1 << 7,
  184. };
  185. #define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
  186. | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
  187. | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP)
  188. #endif
  189. #ifdef TARGET_SPARC
  190. #ifdef TARGET_SPARC64
  191. #define ELF_START_MMAP 0x80000000
  192. #ifndef TARGET_ABI32
  193. #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
  194. #else
  195. #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
  196. #endif
  197. #define ELF_CLASS ELFCLASS64
  198. #define ELF_DATA ELFDATA2MSB
  199. #define ELF_ARCH EM_SPARCV9
  200. #define STACK_BIAS 2047
  201. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  202. {
  203. #ifndef TARGET_ABI32
  204. regs->tstate = 0;
  205. #endif
  206. regs->pc = infop->entry;
  207. regs->npc = regs->pc + 4;
  208. regs->y = 0;
  209. #ifdef TARGET_ABI32
  210. regs->u_regs[14] = infop->start_stack - 16 * 4;
  211. #else
  212. if (personality(infop->personality) == PER_LINUX32)
  213. regs->u_regs[14] = infop->start_stack - 16 * 4;
  214. else {
  215. regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
  216. if (bsd_type == target_freebsd) {
  217. regs->u_regs[8] = infop->start_stack;
  218. regs->u_regs[11] = infop->start_stack;
  219. }
  220. }
  221. #endif
  222. }
  223. #else
  224. #define ELF_START_MMAP 0x80000000
  225. #define elf_check_arch(x) ( (x) == EM_SPARC )
  226. #define ELF_CLASS ELFCLASS32
  227. #define ELF_DATA ELFDATA2MSB
  228. #define ELF_ARCH EM_SPARC
  229. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  230. {
  231. regs->psr = 0;
  232. regs->pc = infop->entry;
  233. regs->npc = regs->pc + 4;
  234. regs->y = 0;
  235. regs->u_regs[14] = infop->start_stack - 16 * 4;
  236. }
  237. #endif
  238. #endif
  239. #ifdef TARGET_PPC
  240. #define ELF_START_MMAP 0x80000000
  241. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  242. #define elf_check_arch(x) ( (x) == EM_PPC64 )
  243. #define ELF_CLASS ELFCLASS64
  244. #else
  245. #define elf_check_arch(x) ( (x) == EM_PPC )
  246. #define ELF_CLASS ELFCLASS32
  247. #endif
  248. #ifdef TARGET_WORDS_BIGENDIAN
  249. #define ELF_DATA ELFDATA2MSB
  250. #else
  251. #define ELF_DATA ELFDATA2LSB
  252. #endif
  253. #define ELF_ARCH EM_PPC
  254. /*
  255. * We need to put in some extra aux table entries to tell glibc what
  256. * the cache block size is, so it can use the dcbz instruction safely.
  257. */
  258. #define AT_DCACHEBSIZE 19
  259. #define AT_ICACHEBSIZE 20
  260. #define AT_UCACHEBSIZE 21
  261. /* A special ignored type value for PPC, for glibc compatibility. */
  262. #define AT_IGNOREPPC 22
  263. /*
  264. * The requirements here are:
  265. * - keep the final alignment of sp (sp & 0xf)
  266. * - make sure the 32-bit value at the first 16 byte aligned position of
  267. * AUXV is greater than 16 for glibc compatibility.
  268. * AT_IGNOREPPC is used for that.
  269. * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
  270. * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
  271. */
  272. #define DLINFO_ARCH_ITEMS 5
  273. #define ARCH_DLINFO \
  274. do { \
  275. NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
  276. NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
  277. NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
  278. /* \
  279. * Now handle glibc compatibility. \
  280. */ \
  281. NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
  282. NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
  283. } while (0)
  284. static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
  285. {
  286. abi_ulong pos = infop->start_stack;
  287. abi_ulong tmp;
  288. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  289. abi_ulong entry, toc;
  290. #endif
  291. _regs->gpr[1] = infop->start_stack;
  292. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  293. entry = ldq_raw(infop->entry) + infop->load_addr;
  294. toc = ldq_raw(infop->entry + 8) + infop->load_addr;
  295. _regs->gpr[2] = toc;
  296. infop->entry = entry;
  297. #endif
  298. _regs->nip = infop->entry;
  299. /* Note that isn't exactly what regular kernel does
  300. * but this is what the ABI wants and is needed to allow
  301. * execution of PPC BSD programs.
  302. */
  303. /* FIXME - what to for failure of get_user()? */
  304. get_user_ual(_regs->gpr[3], pos);
  305. pos += sizeof(abi_ulong);
  306. _regs->gpr[4] = pos;
  307. for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
  308. tmp = ldl(pos);
  309. _regs->gpr[5] = pos;
  310. }
  311. #define USE_ELF_CORE_DUMP
  312. #define ELF_EXEC_PAGESIZE 4096
  313. #endif
  314. #ifdef TARGET_MIPS
  315. #define ELF_START_MMAP 0x80000000
  316. #define elf_check_arch(x) ( (x) == EM_MIPS )
  317. #ifdef TARGET_MIPS64
  318. #define ELF_CLASS ELFCLASS64
  319. #else
  320. #define ELF_CLASS ELFCLASS32
  321. #endif
  322. #ifdef TARGET_WORDS_BIGENDIAN
  323. #define ELF_DATA ELFDATA2MSB
  324. #else
  325. #define ELF_DATA ELFDATA2LSB
  326. #endif
  327. #define ELF_ARCH EM_MIPS
  328. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  329. {
  330. regs->cp0_status = 2 << CP0St_KSU;
  331. regs->cp0_epc = infop->entry;
  332. regs->regs[29] = infop->start_stack;
  333. }
  334. #define USE_ELF_CORE_DUMP
  335. #define ELF_EXEC_PAGESIZE 4096
  336. #endif /* TARGET_MIPS */
  337. #ifdef TARGET_SH4
  338. #define ELF_START_MMAP 0x80000000
  339. #define elf_check_arch(x) ( (x) == EM_SH )
  340. #define ELF_CLASS ELFCLASS32
  341. #define ELF_DATA ELFDATA2LSB
  342. #define ELF_ARCH EM_SH
  343. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  344. {
  345. /* Check other registers XXXXX */
  346. regs->pc = infop->entry;
  347. regs->regs[15] = infop->start_stack;
  348. }
  349. #define USE_ELF_CORE_DUMP
  350. #define ELF_EXEC_PAGESIZE 4096
  351. #endif
  352. #ifdef TARGET_CRIS
  353. #define ELF_START_MMAP 0x80000000
  354. #define elf_check_arch(x) ( (x) == EM_CRIS )
  355. #define ELF_CLASS ELFCLASS32
  356. #define ELF_DATA ELFDATA2LSB
  357. #define ELF_ARCH EM_CRIS
  358. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  359. {
  360. regs->erp = infop->entry;
  361. }
  362. #define USE_ELF_CORE_DUMP
  363. #define ELF_EXEC_PAGESIZE 8192
  364. #endif
  365. #ifdef TARGET_M68K
  366. #define ELF_START_MMAP 0x80000000
  367. #define elf_check_arch(x) ( (x) == EM_68K )
  368. #define ELF_CLASS ELFCLASS32
  369. #define ELF_DATA ELFDATA2MSB
  370. #define ELF_ARCH EM_68K
  371. /* ??? Does this need to do anything?
  372. #define ELF_PLAT_INIT(_r) */
  373. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  374. {
  375. regs->usp = infop->start_stack;
  376. regs->sr = 0;
  377. regs->pc = infop->entry;
  378. }
  379. #define USE_ELF_CORE_DUMP
  380. #define ELF_EXEC_PAGESIZE 8192
  381. #endif
  382. #ifdef TARGET_ALPHA
  383. #define ELF_START_MMAP (0x30000000000ULL)
  384. #define elf_check_arch(x) ( (x) == ELF_ARCH )
  385. #define ELF_CLASS ELFCLASS64
  386. #define ELF_DATA ELFDATA2MSB
  387. #define ELF_ARCH EM_ALPHA
  388. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  389. {
  390. regs->pc = infop->entry;
  391. regs->ps = 8;
  392. regs->usp = infop->start_stack;
  393. regs->unique = infop->start_data; /* ? */
  394. printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
  395. regs->unique, infop->start_data);
  396. }
  397. #define USE_ELF_CORE_DUMP
  398. #define ELF_EXEC_PAGESIZE 8192
  399. #endif /* TARGET_ALPHA */
  400. #ifndef ELF_PLATFORM
  401. #define ELF_PLATFORM (NULL)
  402. #endif
  403. #ifndef ELF_HWCAP
  404. #define ELF_HWCAP 0
  405. #endif
  406. #ifdef TARGET_ABI32
  407. #undef ELF_CLASS
  408. #define ELF_CLASS ELFCLASS32
  409. #undef bswaptls
  410. #define bswaptls(ptr) bswap32s(ptr)
  411. #endif
  412. #include "elf.h"
  413. struct exec
  414. {
  415. unsigned int a_info; /* Use macros N_MAGIC, etc for access */
  416. unsigned int a_text; /* length of text, in bytes */
  417. unsigned int a_data; /* length of data, in bytes */
  418. unsigned int a_bss; /* length of uninitialized data area, in bytes */
  419. unsigned int a_syms; /* length of symbol table data in file, in bytes */
  420. unsigned int a_entry; /* start address */
  421. unsigned int a_trsize; /* length of relocation info for text, in bytes */
  422. unsigned int a_drsize; /* length of relocation info for data, in bytes */
  423. };
  424. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  425. #define OMAGIC 0407
  426. #define NMAGIC 0410
  427. #define ZMAGIC 0413
  428. #define QMAGIC 0314
  429. /* max code+data+bss space allocated to elf interpreter */
  430. #define INTERP_MAP_SIZE (32 * 1024 * 1024)
  431. /* max code+data+bss+brk space allocated to ET_DYN executables */
  432. #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
  433. /* Necessary parameters */
  434. #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
  435. #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
  436. #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
  437. #define INTERPRETER_NONE 0
  438. #define INTERPRETER_AOUT 1
  439. #define INTERPRETER_ELF 2
  440. #define DLINFO_ITEMS 12
  441. static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
  442. {
  443. memcpy(to, from, n);
  444. }
  445. static int load_aout_interp(void * exptr, int interp_fd);
  446. #ifdef BSWAP_NEEDED
  447. static void bswap_ehdr(struct elfhdr *ehdr)
  448. {
  449. bswap16s(&ehdr->e_type); /* Object file type */
  450. bswap16s(&ehdr->e_machine); /* Architecture */
  451. bswap32s(&ehdr->e_version); /* Object file version */
  452. bswaptls(&ehdr->e_entry); /* Entry point virtual address */
  453. bswaptls(&ehdr->e_phoff); /* Program header table file offset */
  454. bswaptls(&ehdr->e_shoff); /* Section header table file offset */
  455. bswap32s(&ehdr->e_flags); /* Processor-specific flags */
  456. bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
  457. bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
  458. bswap16s(&ehdr->e_phnum); /* Program header table entry count */
  459. bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
  460. bswap16s(&ehdr->e_shnum); /* Section header table entry count */
  461. bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
  462. }
  463. static void bswap_phdr(struct elf_phdr *phdr)
  464. {
  465. bswap32s(&phdr->p_type); /* Segment type */
  466. bswaptls(&phdr->p_offset); /* Segment file offset */
  467. bswaptls(&phdr->p_vaddr); /* Segment virtual address */
  468. bswaptls(&phdr->p_paddr); /* Segment physical address */
  469. bswaptls(&phdr->p_filesz); /* Segment size in file */
  470. bswaptls(&phdr->p_memsz); /* Segment size in memory */
  471. bswap32s(&phdr->p_flags); /* Segment flags */
  472. bswaptls(&phdr->p_align); /* Segment alignment */
  473. }
  474. static void bswap_shdr(struct elf_shdr *shdr)
  475. {
  476. bswap32s(&shdr->sh_name);
  477. bswap32s(&shdr->sh_type);
  478. bswaptls(&shdr->sh_flags);
  479. bswaptls(&shdr->sh_addr);
  480. bswaptls(&shdr->sh_offset);
  481. bswaptls(&shdr->sh_size);
  482. bswap32s(&shdr->sh_link);
  483. bswap32s(&shdr->sh_info);
  484. bswaptls(&shdr->sh_addralign);
  485. bswaptls(&shdr->sh_entsize);
  486. }
  487. static void bswap_sym(struct elf_sym *sym)
  488. {
  489. bswap32s(&sym->st_name);
  490. bswaptls(&sym->st_value);
  491. bswaptls(&sym->st_size);
  492. bswap16s(&sym->st_shndx);
  493. }
  494. #endif
  495. /*
  496. * 'copy_elf_strings()' copies argument/envelope strings from user
  497. * memory to free pages in kernel mem. These are in a format ready
  498. * to be put directly into the top of new user memory.
  499. *
  500. */
  501. static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
  502. abi_ulong p)
  503. {
  504. char *tmp, *tmp1, *pag = NULL;
  505. int len, offset = 0;
  506. if (!p) {
  507. return 0; /* bullet-proofing */
  508. }
  509. while (argc-- > 0) {
  510. tmp = argv[argc];
  511. if (!tmp) {
  512. fprintf(stderr, "VFS: argc is wrong");
  513. exit(-1);
  514. }
  515. tmp1 = tmp;
  516. while (*tmp++);
  517. len = tmp - tmp1;
  518. if (p < len) { /* this shouldn't happen - 128kB */
  519. return 0;
  520. }
  521. while (len) {
  522. --p; --tmp; --len;
  523. if (--offset < 0) {
  524. offset = p % TARGET_PAGE_SIZE;
  525. pag = (char *)page[p/TARGET_PAGE_SIZE];
  526. if (!pag) {
  527. pag = g_try_malloc0(TARGET_PAGE_SIZE);
  528. page[p/TARGET_PAGE_SIZE] = pag;
  529. if (!pag)
  530. return 0;
  531. }
  532. }
  533. if (len == 0 || offset == 0) {
  534. *(pag + offset) = *tmp;
  535. }
  536. else {
  537. int bytes_to_copy = (len > offset) ? offset : len;
  538. tmp -= bytes_to_copy;
  539. p -= bytes_to_copy;
  540. offset -= bytes_to_copy;
  541. len -= bytes_to_copy;
  542. memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
  543. }
  544. }
  545. }
  546. return p;
  547. }
  548. static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
  549. struct image_info *info)
  550. {
  551. abi_ulong stack_base, size, error;
  552. int i;
  553. /* Create enough stack to hold everything. If we don't use
  554. * it for args, we'll use it for something else...
  555. */
  556. size = x86_stack_size;
  557. if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
  558. size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
  559. error = target_mmap(0,
  560. size + qemu_host_page_size,
  561. PROT_READ | PROT_WRITE,
  562. MAP_PRIVATE | MAP_ANON,
  563. -1, 0);
  564. if (error == -1) {
  565. perror("stk mmap");
  566. exit(-1);
  567. }
  568. /* we reserve one extra page at the top of the stack as guard */
  569. target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
  570. stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
  571. p += stack_base;
  572. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  573. if (bprm->page[i]) {
  574. info->rss++;
  575. /* FIXME - check return value of memcpy_to_target() for failure */
  576. memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
  577. g_free(bprm->page[i]);
  578. }
  579. stack_base += TARGET_PAGE_SIZE;
  580. }
  581. return p;
  582. }
  583. static void set_brk(abi_ulong start, abi_ulong end)
  584. {
  585. /* page-align the start and end addresses... */
  586. start = HOST_PAGE_ALIGN(start);
  587. end = HOST_PAGE_ALIGN(end);
  588. if (end <= start)
  589. return;
  590. if(target_mmap(start, end - start,
  591. PROT_READ | PROT_WRITE | PROT_EXEC,
  592. MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
  593. perror("cannot mmap brk");
  594. exit(-1);
  595. }
  596. }
  597. /* We need to explicitly zero any fractional pages after the data
  598. section (i.e. bss). This would contain the junk from the file that
  599. should not be in memory. */
  600. static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
  601. {
  602. abi_ulong nbyte;
  603. if (elf_bss >= last_bss)
  604. return;
  605. /* XXX: this is really a hack : if the real host page size is
  606. smaller than the target page size, some pages after the end
  607. of the file may not be mapped. A better fix would be to
  608. patch target_mmap(), but it is more complicated as the file
  609. size must be known */
  610. if (qemu_real_host_page_size < qemu_host_page_size) {
  611. abi_ulong end_addr, end_addr1;
  612. end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
  613. ~(qemu_real_host_page_size - 1);
  614. end_addr = HOST_PAGE_ALIGN(elf_bss);
  615. if (end_addr1 < end_addr) {
  616. mmap((void *)g2h(end_addr1), end_addr - end_addr1,
  617. PROT_READ|PROT_WRITE|PROT_EXEC,
  618. MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0);
  619. }
  620. }
  621. nbyte = elf_bss & (qemu_host_page_size-1);
  622. if (nbyte) {
  623. nbyte = qemu_host_page_size - nbyte;
  624. do {
  625. /* FIXME - what to do if put_user() fails? */
  626. put_user_u8(0, elf_bss);
  627. elf_bss++;
  628. } while (--nbyte);
  629. }
  630. }
  631. static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
  632. struct elfhdr * exec,
  633. abi_ulong load_addr,
  634. abi_ulong load_bias,
  635. abi_ulong interp_load_addr, int ibcs,
  636. struct image_info *info)
  637. {
  638. abi_ulong sp;
  639. int size;
  640. abi_ulong u_platform;
  641. const char *k_platform;
  642. const int n = sizeof(elf_addr_t);
  643. sp = p;
  644. u_platform = 0;
  645. k_platform = ELF_PLATFORM;
  646. if (k_platform) {
  647. size_t len = strlen(k_platform) + 1;
  648. sp -= (len + n - 1) & ~(n - 1);
  649. u_platform = sp;
  650. /* FIXME - check return value of memcpy_to_target() for failure */
  651. memcpy_to_target(sp, k_platform, len);
  652. }
  653. /*
  654. * Force 16 byte _final_ alignment here for generality.
  655. */
  656. sp = sp &~ (abi_ulong)15;
  657. size = (DLINFO_ITEMS + 1) * 2;
  658. if (k_platform)
  659. size += 2;
  660. #ifdef DLINFO_ARCH_ITEMS
  661. size += DLINFO_ARCH_ITEMS * 2;
  662. #endif
  663. size += envc + argc + 2;
  664. size += (!ibcs ? 3 : 1); /* argc itself */
  665. size *= n;
  666. if (size & 15)
  667. sp -= 16 - (size & 15);
  668. /* This is correct because Linux defines
  669. * elf_addr_t as Elf32_Off / Elf64_Off
  670. */
  671. #define NEW_AUX_ENT(id, val) do { \
  672. sp -= n; put_user_ual(val, sp); \
  673. sp -= n; put_user_ual(id, sp); \
  674. } while(0)
  675. NEW_AUX_ENT (AT_NULL, 0);
  676. /* There must be exactly DLINFO_ITEMS entries here. */
  677. NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
  678. NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
  679. NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
  680. NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
  681. NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
  682. NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
  683. NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
  684. NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
  685. NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
  686. NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
  687. NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
  688. NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
  689. NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
  690. if (k_platform)
  691. NEW_AUX_ENT(AT_PLATFORM, u_platform);
  692. #ifdef ARCH_DLINFO
  693. /*
  694. * ARCH_DLINFO must come last so platform specific code can enforce
  695. * special alignment requirements on the AUXV if necessary (eg. PPC).
  696. */
  697. ARCH_DLINFO;
  698. #endif
  699. #undef NEW_AUX_ENT
  700. sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
  701. return sp;
  702. }
  703. static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
  704. int interpreter_fd,
  705. abi_ulong *interp_load_addr)
  706. {
  707. struct elf_phdr *elf_phdata = NULL;
  708. struct elf_phdr *eppnt;
  709. abi_ulong load_addr = 0;
  710. int load_addr_set = 0;
  711. int retval;
  712. abi_ulong last_bss, elf_bss;
  713. abi_ulong error;
  714. int i;
  715. elf_bss = 0;
  716. last_bss = 0;
  717. error = 0;
  718. #ifdef BSWAP_NEEDED
  719. bswap_ehdr(interp_elf_ex);
  720. #endif
  721. /* First of all, some simple consistency checks */
  722. if ((interp_elf_ex->e_type != ET_EXEC &&
  723. interp_elf_ex->e_type != ET_DYN) ||
  724. !elf_check_arch(interp_elf_ex->e_machine)) {
  725. return ~((abi_ulong)0UL);
  726. }
  727. /* Now read in all of the header information */
  728. if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
  729. return ~(abi_ulong)0UL;
  730. elf_phdata = (struct elf_phdr *)
  731. malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
  732. if (!elf_phdata)
  733. return ~((abi_ulong)0UL);
  734. /*
  735. * If the size of this structure has changed, then punt, since
  736. * we will be doing the wrong thing.
  737. */
  738. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
  739. free(elf_phdata);
  740. return ~((abi_ulong)0UL);
  741. }
  742. retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
  743. if(retval >= 0) {
  744. retval = read(interpreter_fd,
  745. (char *) elf_phdata,
  746. sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
  747. }
  748. if (retval < 0) {
  749. perror("load_elf_interp");
  750. exit(-1);
  751. free (elf_phdata);
  752. return retval;
  753. }
  754. #ifdef BSWAP_NEEDED
  755. eppnt = elf_phdata;
  756. for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
  757. bswap_phdr(eppnt);
  758. }
  759. #endif
  760. if (interp_elf_ex->e_type == ET_DYN) {
  761. /* in order to avoid hardcoding the interpreter load
  762. address in qemu, we allocate a big enough memory zone */
  763. error = target_mmap(0, INTERP_MAP_SIZE,
  764. PROT_NONE, MAP_PRIVATE | MAP_ANON,
  765. -1, 0);
  766. if (error == -1) {
  767. perror("mmap");
  768. exit(-1);
  769. }
  770. load_addr = error;
  771. load_addr_set = 1;
  772. }
  773. eppnt = elf_phdata;
  774. for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
  775. if (eppnt->p_type == PT_LOAD) {
  776. int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
  777. int elf_prot = 0;
  778. abi_ulong vaddr = 0;
  779. abi_ulong k;
  780. if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
  781. if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
  782. if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
  783. if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
  784. elf_type |= MAP_FIXED;
  785. vaddr = eppnt->p_vaddr;
  786. }
  787. error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
  788. eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
  789. elf_prot,
  790. elf_type,
  791. interpreter_fd,
  792. eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
  793. if (error == -1) {
  794. /* Real error */
  795. close(interpreter_fd);
  796. free(elf_phdata);
  797. return ~((abi_ulong)0UL);
  798. }
  799. if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
  800. load_addr = error;
  801. load_addr_set = 1;
  802. }
  803. /*
  804. * Find the end of the file mapping for this phdr, and keep
  805. * track of the largest address we see for this.
  806. */
  807. k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
  808. if (k > elf_bss) elf_bss = k;
  809. /*
  810. * Do the same thing for the memory mapping - between
  811. * elf_bss and last_bss is the bss section.
  812. */
  813. k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
  814. if (k > last_bss) last_bss = k;
  815. }
  816. /* Now use mmap to map the library into memory. */
  817. close(interpreter_fd);
  818. /*
  819. * Now fill out the bss section. First pad the last page up
  820. * to the page boundary, and then perform a mmap to make sure
  821. * that there are zeromapped pages up to and including the last
  822. * bss page.
  823. */
  824. padzero(elf_bss, last_bss);
  825. elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
  826. /* Map the last of the bss segment */
  827. if (last_bss > elf_bss) {
  828. target_mmap(elf_bss, last_bss-elf_bss,
  829. PROT_READ|PROT_WRITE|PROT_EXEC,
  830. MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0);
  831. }
  832. free(elf_phdata);
  833. *interp_load_addr = load_addr;
  834. return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
  835. }
  836. static int symfind(const void *s0, const void *s1)
  837. {
  838. target_ulong addr = *(target_ulong *)s0;
  839. struct elf_sym *sym = (struct elf_sym *)s1;
  840. int result = 0;
  841. if (addr < sym->st_value) {
  842. result = -1;
  843. } else if (addr >= sym->st_value + sym->st_size) {
  844. result = 1;
  845. }
  846. return result;
  847. }
  848. static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
  849. {
  850. #if ELF_CLASS == ELFCLASS32
  851. struct elf_sym *syms = s->disas_symtab.elf32;
  852. #else
  853. struct elf_sym *syms = s->disas_symtab.elf64;
  854. #endif
  855. // binary search
  856. struct elf_sym *sym;
  857. sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
  858. if (sym != NULL) {
  859. return s->disas_strtab + sym->st_name;
  860. }
  861. return "";
  862. }
  863. /* FIXME: This should use elf_ops.h */
  864. static int symcmp(const void *s0, const void *s1)
  865. {
  866. struct elf_sym *sym0 = (struct elf_sym *)s0;
  867. struct elf_sym *sym1 = (struct elf_sym *)s1;
  868. return (sym0->st_value < sym1->st_value)
  869. ? -1
  870. : ((sym0->st_value > sym1->st_value) ? 1 : 0);
  871. }
  872. /* Best attempt to load symbols from this ELF object. */
  873. static void load_symbols(struct elfhdr *hdr, int fd)
  874. {
  875. unsigned int i, nsyms;
  876. struct elf_shdr sechdr, symtab, strtab;
  877. char *strings;
  878. struct syminfo *s;
  879. struct elf_sym *syms, *new_syms;
  880. lseek(fd, hdr->e_shoff, SEEK_SET);
  881. for (i = 0; i < hdr->e_shnum; i++) {
  882. if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
  883. return;
  884. #ifdef BSWAP_NEEDED
  885. bswap_shdr(&sechdr);
  886. #endif
  887. if (sechdr.sh_type == SHT_SYMTAB) {
  888. symtab = sechdr;
  889. lseek(fd, hdr->e_shoff
  890. + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
  891. if (read(fd, &strtab, sizeof(strtab))
  892. != sizeof(strtab))
  893. return;
  894. #ifdef BSWAP_NEEDED
  895. bswap_shdr(&strtab);
  896. #endif
  897. goto found;
  898. }
  899. }
  900. return; /* Shouldn't happen... */
  901. found:
  902. /* Now know where the strtab and symtab are. Snarf them. */
  903. s = malloc(sizeof(*s));
  904. syms = malloc(symtab.sh_size);
  905. if (!syms) {
  906. free(s);
  907. return;
  908. }
  909. s->disas_strtab = strings = malloc(strtab.sh_size);
  910. if (!s->disas_strtab) {
  911. free(s);
  912. free(syms);
  913. return;
  914. }
  915. lseek(fd, symtab.sh_offset, SEEK_SET);
  916. if (read(fd, syms, symtab.sh_size) != symtab.sh_size) {
  917. free(s);
  918. free(syms);
  919. free(strings);
  920. return;
  921. }
  922. nsyms = symtab.sh_size / sizeof(struct elf_sym);
  923. i = 0;
  924. while (i < nsyms) {
  925. #ifdef BSWAP_NEEDED
  926. bswap_sym(syms + i);
  927. #endif
  928. // Throw away entries which we do not need.
  929. if (syms[i].st_shndx == SHN_UNDEF ||
  930. syms[i].st_shndx >= SHN_LORESERVE ||
  931. ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
  932. nsyms--;
  933. if (i < nsyms) {
  934. syms[i] = syms[nsyms];
  935. }
  936. continue;
  937. }
  938. #if defined(TARGET_ARM) || defined (TARGET_MIPS)
  939. /* The bottom address bit marks a Thumb or MIPS16 symbol. */
  940. syms[i].st_value &= ~(target_ulong)1;
  941. #endif
  942. i++;
  943. }
  944. /* Attempt to free the storage associated with the local symbols
  945. that we threw away. Whether or not this has any effect on the
  946. memory allocation depends on the malloc implementation and how
  947. many symbols we managed to discard. */
  948. new_syms = realloc(syms, nsyms * sizeof(*syms));
  949. if (new_syms == NULL) {
  950. free(s);
  951. free(syms);
  952. free(strings);
  953. return;
  954. }
  955. syms = new_syms;
  956. qsort(syms, nsyms, sizeof(*syms), symcmp);
  957. lseek(fd, strtab.sh_offset, SEEK_SET);
  958. if (read(fd, strings, strtab.sh_size) != strtab.sh_size) {
  959. free(s);
  960. free(syms);
  961. free(strings);
  962. return;
  963. }
  964. s->disas_num_syms = nsyms;
  965. #if ELF_CLASS == ELFCLASS32
  966. s->disas_symtab.elf32 = syms;
  967. s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
  968. #else
  969. s->disas_symtab.elf64 = syms;
  970. s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
  971. #endif
  972. s->next = syminfos;
  973. syminfos = s;
  974. }
  975. int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
  976. struct image_info * info)
  977. {
  978. struct elfhdr elf_ex;
  979. struct elfhdr interp_elf_ex;
  980. struct exec interp_ex;
  981. int interpreter_fd = -1; /* avoid warning */
  982. abi_ulong load_addr, load_bias;
  983. int load_addr_set = 0;
  984. unsigned int interpreter_type = INTERPRETER_NONE;
  985. unsigned char ibcs2_interpreter;
  986. int i;
  987. abi_ulong mapped_addr;
  988. struct elf_phdr * elf_ppnt;
  989. struct elf_phdr *elf_phdata;
  990. abi_ulong elf_bss, k, elf_brk;
  991. int retval;
  992. char * elf_interpreter;
  993. abi_ulong elf_entry, interp_load_addr = 0;
  994. int status;
  995. abi_ulong start_code, end_code, start_data, end_data;
  996. abi_ulong reloc_func_desc = 0;
  997. abi_ulong elf_stack;
  998. char passed_fileno[6];
  999. ibcs2_interpreter = 0;
  1000. status = 0;
  1001. load_addr = 0;
  1002. load_bias = 0;
  1003. elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
  1004. #ifdef BSWAP_NEEDED
  1005. bswap_ehdr(&elf_ex);
  1006. #endif
  1007. /* First of all, some simple consistency checks */
  1008. if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
  1009. (! elf_check_arch(elf_ex.e_machine))) {
  1010. return -ENOEXEC;
  1011. }
  1012. bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
  1013. bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
  1014. bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
  1015. if (!bprm->p) {
  1016. retval = -E2BIG;
  1017. }
  1018. /* Now read in all of the header information */
  1019. elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
  1020. if (elf_phdata == NULL) {
  1021. return -ENOMEM;
  1022. }
  1023. retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
  1024. if(retval > 0) {
  1025. retval = read(bprm->fd, (char *) elf_phdata,
  1026. elf_ex.e_phentsize * elf_ex.e_phnum);
  1027. }
  1028. if (retval < 0) {
  1029. perror("load_elf_binary");
  1030. exit(-1);
  1031. free (elf_phdata);
  1032. return -errno;
  1033. }
  1034. #ifdef BSWAP_NEEDED
  1035. elf_ppnt = elf_phdata;
  1036. for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
  1037. bswap_phdr(elf_ppnt);
  1038. }
  1039. #endif
  1040. elf_ppnt = elf_phdata;
  1041. elf_bss = 0;
  1042. elf_brk = 0;
  1043. elf_stack = ~((abi_ulong)0UL);
  1044. elf_interpreter = NULL;
  1045. start_code = ~((abi_ulong)0UL);
  1046. end_code = 0;
  1047. start_data = 0;
  1048. end_data = 0;
  1049. interp_ex.a_info = 0;
  1050. for(i=0;i < elf_ex.e_phnum; i++) {
  1051. if (elf_ppnt->p_type == PT_INTERP) {
  1052. if ( elf_interpreter != NULL )
  1053. {
  1054. free (elf_phdata);
  1055. free(elf_interpreter);
  1056. close(bprm->fd);
  1057. return -EINVAL;
  1058. }
  1059. /* This is the program interpreter used for
  1060. * shared libraries - for now assume that this
  1061. * is an a.out format binary
  1062. */
  1063. elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
  1064. if (elf_interpreter == NULL) {
  1065. free (elf_phdata);
  1066. close(bprm->fd);
  1067. return -ENOMEM;
  1068. }
  1069. retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
  1070. if(retval >= 0) {
  1071. retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
  1072. }
  1073. if(retval < 0) {
  1074. perror("load_elf_binary2");
  1075. exit(-1);
  1076. }
  1077. /* If the program interpreter is one of these two,
  1078. then assume an iBCS2 image. Otherwise assume
  1079. a native linux image. */
  1080. /* JRP - Need to add X86 lib dir stuff here... */
  1081. if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
  1082. strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
  1083. ibcs2_interpreter = 1;
  1084. }
  1085. #if 0
  1086. printf("Using ELF interpreter %s\n", path(elf_interpreter));
  1087. #endif
  1088. if (retval >= 0) {
  1089. retval = open(path(elf_interpreter), O_RDONLY);
  1090. if(retval >= 0) {
  1091. interpreter_fd = retval;
  1092. }
  1093. else {
  1094. perror(elf_interpreter);
  1095. exit(-1);
  1096. /* retval = -errno; */
  1097. }
  1098. }
  1099. if (retval >= 0) {
  1100. retval = lseek(interpreter_fd, 0, SEEK_SET);
  1101. if(retval >= 0) {
  1102. retval = read(interpreter_fd,bprm->buf,128);
  1103. }
  1104. }
  1105. if (retval >= 0) {
  1106. interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
  1107. interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */
  1108. }
  1109. if (retval < 0) {
  1110. perror("load_elf_binary3");
  1111. exit(-1);
  1112. free (elf_phdata);
  1113. free(elf_interpreter);
  1114. close(bprm->fd);
  1115. return retval;
  1116. }
  1117. }
  1118. elf_ppnt++;
  1119. }
  1120. /* Some simple consistency checks for the interpreter */
  1121. if (elf_interpreter){
  1122. interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
  1123. /* Now figure out which format our binary is */
  1124. if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
  1125. (N_MAGIC(interp_ex) != QMAGIC)) {
  1126. interpreter_type = INTERPRETER_ELF;
  1127. }
  1128. if (interp_elf_ex.e_ident[0] != 0x7f ||
  1129. strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
  1130. interpreter_type &= ~INTERPRETER_ELF;
  1131. }
  1132. if (!interpreter_type) {
  1133. free(elf_interpreter);
  1134. free(elf_phdata);
  1135. close(bprm->fd);
  1136. return -ELIBBAD;
  1137. }
  1138. }
  1139. /* OK, we are done with that, now set up the arg stuff,
  1140. and then start this sucker up */
  1141. {
  1142. char * passed_p;
  1143. if (interpreter_type == INTERPRETER_AOUT) {
  1144. snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
  1145. passed_p = passed_fileno;
  1146. if (elf_interpreter) {
  1147. bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
  1148. bprm->argc++;
  1149. }
  1150. }
  1151. if (!bprm->p) {
  1152. if (elf_interpreter) {
  1153. free(elf_interpreter);
  1154. }
  1155. free (elf_phdata);
  1156. close(bprm->fd);
  1157. return -E2BIG;
  1158. }
  1159. }
  1160. /* OK, This is the point of no return */
  1161. info->end_data = 0;
  1162. info->end_code = 0;
  1163. info->start_mmap = (abi_ulong)ELF_START_MMAP;
  1164. info->mmap = 0;
  1165. elf_entry = (abi_ulong) elf_ex.e_entry;
  1166. #if defined(CONFIG_USE_GUEST_BASE)
  1167. /*
  1168. * In case where user has not explicitly set the guest_base, we
  1169. * probe here that should we set it automatically.
  1170. */
  1171. if (!have_guest_base) {
  1172. /*
  1173. * Go through ELF program header table and find out whether
  1174. * any of the segments drop below our current mmap_min_addr and
  1175. * in that case set guest_base to corresponding address.
  1176. */
  1177. for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
  1178. i++, elf_ppnt++) {
  1179. if (elf_ppnt->p_type != PT_LOAD)
  1180. continue;
  1181. if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
  1182. guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
  1183. break;
  1184. }
  1185. }
  1186. }
  1187. #endif /* CONFIG_USE_GUEST_BASE */
  1188. /* Do this so that we can load the interpreter, if need be. We will
  1189. change some of these later */
  1190. info->rss = 0;
  1191. bprm->p = setup_arg_pages(bprm->p, bprm, info);
  1192. info->start_stack = bprm->p;
  1193. /* Now we do a little grungy work by mmaping the ELF image into
  1194. * the correct location in memory. At this point, we assume that
  1195. * the image should be loaded at fixed address, not at a variable
  1196. * address.
  1197. */
  1198. for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
  1199. int elf_prot = 0;
  1200. int elf_flags = 0;
  1201. abi_ulong error;
  1202. if (elf_ppnt->p_type != PT_LOAD)
  1203. continue;
  1204. if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
  1205. if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
  1206. if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
  1207. elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
  1208. if (elf_ex.e_type == ET_EXEC || load_addr_set) {
  1209. elf_flags |= MAP_FIXED;
  1210. } else if (elf_ex.e_type == ET_DYN) {
  1211. /* Try and get dynamic programs out of the way of the default mmap
  1212. base, as well as whatever program they might try to exec. This
  1213. is because the brk will follow the loader, and is not movable. */
  1214. /* NOTE: for qemu, we do a big mmap to get enough space
  1215. without hardcoding any address */
  1216. error = target_mmap(0, ET_DYN_MAP_SIZE,
  1217. PROT_NONE, MAP_PRIVATE | MAP_ANON,
  1218. -1, 0);
  1219. if (error == -1) {
  1220. perror("mmap");
  1221. exit(-1);
  1222. }
  1223. load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
  1224. }
  1225. error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
  1226. (elf_ppnt->p_filesz +
  1227. TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
  1228. elf_prot,
  1229. (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
  1230. bprm->fd,
  1231. (elf_ppnt->p_offset -
  1232. TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
  1233. if (error == -1) {
  1234. perror("mmap");
  1235. exit(-1);
  1236. }
  1237. #ifdef LOW_ELF_STACK
  1238. if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
  1239. elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
  1240. #endif
  1241. if (!load_addr_set) {
  1242. load_addr_set = 1;
  1243. load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
  1244. if (elf_ex.e_type == ET_DYN) {
  1245. load_bias += error -
  1246. TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
  1247. load_addr += load_bias;
  1248. reloc_func_desc = load_bias;
  1249. }
  1250. }
  1251. k = elf_ppnt->p_vaddr;
  1252. if (k < start_code)
  1253. start_code = k;
  1254. if (start_data < k)
  1255. start_data = k;
  1256. k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  1257. if (k > elf_bss)
  1258. elf_bss = k;
  1259. if ((elf_ppnt->p_flags & PF_X) && end_code < k)
  1260. end_code = k;
  1261. if (end_data < k)
  1262. end_data = k;
  1263. k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  1264. if (k > elf_brk) elf_brk = k;
  1265. }
  1266. elf_entry += load_bias;
  1267. elf_bss += load_bias;
  1268. elf_brk += load_bias;
  1269. start_code += load_bias;
  1270. end_code += load_bias;
  1271. start_data += load_bias;
  1272. end_data += load_bias;
  1273. if (elf_interpreter) {
  1274. if (interpreter_type & 1) {
  1275. elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
  1276. }
  1277. else if (interpreter_type & 2) {
  1278. elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
  1279. &interp_load_addr);
  1280. }
  1281. reloc_func_desc = interp_load_addr;
  1282. close(interpreter_fd);
  1283. free(elf_interpreter);
  1284. if (elf_entry == ~((abi_ulong)0UL)) {
  1285. printf("Unable to load interpreter\n");
  1286. free(elf_phdata);
  1287. exit(-1);
  1288. return 0;
  1289. }
  1290. }
  1291. free(elf_phdata);
  1292. if (qemu_log_enabled())
  1293. load_symbols(&elf_ex, bprm->fd);
  1294. if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
  1295. info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
  1296. #ifdef LOW_ELF_STACK
  1297. info->start_stack = bprm->p = elf_stack - 4;
  1298. #endif
  1299. bprm->p = create_elf_tables(bprm->p,
  1300. bprm->argc,
  1301. bprm->envc,
  1302. &elf_ex,
  1303. load_addr, load_bias,
  1304. interp_load_addr,
  1305. (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
  1306. info);
  1307. info->load_addr = reloc_func_desc;
  1308. info->start_brk = info->brk = elf_brk;
  1309. info->end_code = end_code;
  1310. info->start_code = start_code;
  1311. info->start_data = start_data;
  1312. info->end_data = end_data;
  1313. info->start_stack = bprm->p;
  1314. /* Calling set_brk effectively mmaps the pages that we need for the bss and break
  1315. sections */
  1316. set_brk(elf_bss, elf_brk);
  1317. padzero(elf_bss, elf_brk);
  1318. #if 0
  1319. printf("(start_brk) %x\n" , info->start_brk);
  1320. printf("(end_code) %x\n" , info->end_code);
  1321. printf("(start_code) %x\n" , info->start_code);
  1322. printf("(end_data) %x\n" , info->end_data);
  1323. printf("(start_stack) %x\n" , info->start_stack);
  1324. printf("(brk) %x\n" , info->brk);
  1325. #endif
  1326. if ( info->personality == PER_SVR4 )
  1327. {
  1328. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  1329. and some applications "depend" upon this behavior.
  1330. Since we do not have the power to recompile these, we
  1331. emulate the SVr4 behavior. Sigh. */
  1332. mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
  1333. MAP_FIXED | MAP_PRIVATE, -1, 0);
  1334. }
  1335. info->entry = elf_entry;
  1336. return 0;
  1337. }
  1338. static int load_aout_interp(void * exptr, int interp_fd)
  1339. {
  1340. printf("a.out interpreter not yet supported\n");
  1341. return(0);
  1342. }
  1343. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
  1344. {
  1345. init_thread(regs, infop);
  1346. }