elfload.c 47 KB

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