2
0

elfload.c 46 KB

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