elfload.c 47 KB

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