linux-user: Add support for btrfs ioctls used to manage quota
[qemu.git] / linux-user / elfload.c
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
4
5 #include <sys/resource.h>
6 #include <sys/shm.h>
7
8 #include "qemu.h"
9 #include "disas/disas.h"
10 #include "qemu/path.h"
11 #include "qemu/queue.h"
12 #include "qemu/guest-random.h"
13 #include "qemu/units.h"
14 #include "qemu/selfmap.h"
15
16 #ifdef _ARCH_PPC64
17 #undef ARCH_DLINFO
18 #undef ELF_PLATFORM
19 #undef ELF_HWCAP
20 #undef ELF_HWCAP2
21 #undef ELF_CLASS
22 #undef ELF_DATA
23 #undef ELF_ARCH
24 #endif
25
26 #define ELF_OSABI ELFOSABI_SYSV
27
28 /* from personality.h */
29
30 /*
31 * Flags for bug emulation.
32 *
33 * These occupy the top three bytes.
34 */
35 enum {
36 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
37 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
38 descriptors (signal handling) */
39 MMAP_PAGE_ZERO = 0x0100000,
40 ADDR_COMPAT_LAYOUT = 0x0200000,
41 READ_IMPLIES_EXEC = 0x0400000,
42 ADDR_LIMIT_32BIT = 0x0800000,
43 SHORT_INODE = 0x1000000,
44 WHOLE_SECONDS = 0x2000000,
45 STICKY_TIMEOUTS = 0x4000000,
46 ADDR_LIMIT_3GB = 0x8000000,
47 };
48
49 /*
50 * Personality types.
51 *
52 * These go in the low byte. Avoid using the top bit, it will
53 * conflict with error returns.
54 */
55 enum {
56 PER_LINUX = 0x0000,
57 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
58 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
59 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
60 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
61 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
62 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
63 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
64 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
65 PER_BSD = 0x0006,
66 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
67 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
68 PER_LINUX32 = 0x0008,
69 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
70 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
71 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
72 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
73 PER_RISCOS = 0x000c,
74 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
75 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
76 PER_OSF4 = 0x000f, /* OSF/1 v4 */
77 PER_HPUX = 0x0010,
78 PER_MASK = 0x00ff,
79 };
80
81 /*
82 * Return the base personality without flags.
83 */
84 #define personality(pers) (pers & PER_MASK)
85
86 int info_is_fdpic(struct image_info *info)
87 {
88 return info->personality == PER_LINUX_FDPIC;
89 }
90
91 /* this flag is uneffective under linux too, should be deleted */
92 #ifndef MAP_DENYWRITE
93 #define MAP_DENYWRITE 0
94 #endif
95
96 /* should probably go in elf.h */
97 #ifndef ELIBBAD
98 #define ELIBBAD 80
99 #endif
100
101 #ifdef TARGET_WORDS_BIGENDIAN
102 #define ELF_DATA ELFDATA2MSB
103 #else
104 #define ELF_DATA ELFDATA2LSB
105 #endif
106
107 #ifdef TARGET_ABI_MIPSN32
108 typedef abi_ullong target_elf_greg_t;
109 #define tswapreg(ptr) tswap64(ptr)
110 #else
111 typedef abi_ulong target_elf_greg_t;
112 #define tswapreg(ptr) tswapal(ptr)
113 #endif
114
115 #ifdef USE_UID16
116 typedef abi_ushort target_uid_t;
117 typedef abi_ushort target_gid_t;
118 #else
119 typedef abi_uint target_uid_t;
120 typedef abi_uint target_gid_t;
121 #endif
122 typedef abi_int target_pid_t;
123
124 #ifdef TARGET_I386
125
126 #define ELF_PLATFORM get_elf_platform()
127
128 static const char *get_elf_platform(void)
129 {
130 static char elf_platform[] = "i386";
131 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
132 if (family > 6)
133 family = 6;
134 if (family >= 3)
135 elf_platform[1] = '0' + family;
136 return elf_platform;
137 }
138
139 #define ELF_HWCAP get_elf_hwcap()
140
141 static uint32_t get_elf_hwcap(void)
142 {
143 X86CPU *cpu = X86_CPU(thread_cpu);
144
145 return cpu->env.features[FEAT_1_EDX];
146 }
147
148 #ifdef TARGET_X86_64
149 #define ELF_START_MMAP 0x2aaaaab000ULL
150
151 #define ELF_CLASS ELFCLASS64
152 #define ELF_ARCH EM_X86_64
153
154 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
155 {
156 regs->rax = 0;
157 regs->rsp = infop->start_stack;
158 regs->rip = infop->entry;
159 }
160
161 #define ELF_NREG 27
162 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
163
164 /*
165 * Note that ELF_NREG should be 29 as there should be place for
166 * TRAPNO and ERR "registers" as well but linux doesn't dump
167 * those.
168 *
169 * See linux kernel: arch/x86/include/asm/elf.h
170 */
171 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
172 {
173 (*regs)[0] = env->regs[15];
174 (*regs)[1] = env->regs[14];
175 (*regs)[2] = env->regs[13];
176 (*regs)[3] = env->regs[12];
177 (*regs)[4] = env->regs[R_EBP];
178 (*regs)[5] = env->regs[R_EBX];
179 (*regs)[6] = env->regs[11];
180 (*regs)[7] = env->regs[10];
181 (*regs)[8] = env->regs[9];
182 (*regs)[9] = env->regs[8];
183 (*regs)[10] = env->regs[R_EAX];
184 (*regs)[11] = env->regs[R_ECX];
185 (*regs)[12] = env->regs[R_EDX];
186 (*regs)[13] = env->regs[R_ESI];
187 (*regs)[14] = env->regs[R_EDI];
188 (*regs)[15] = env->regs[R_EAX]; /* XXX */
189 (*regs)[16] = env->eip;
190 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
191 (*regs)[18] = env->eflags;
192 (*regs)[19] = env->regs[R_ESP];
193 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
194 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
195 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
196 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
197 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
198 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
199 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
200 }
201
202 #else
203
204 #define ELF_START_MMAP 0x80000000
205
206 /*
207 * This is used to ensure we don't load something for the wrong architecture.
208 */
209 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
210
211 /*
212 * These are used to set parameters in the core dumps.
213 */
214 #define ELF_CLASS ELFCLASS32
215 #define ELF_ARCH EM_386
216
217 static inline void init_thread(struct target_pt_regs *regs,
218 struct image_info *infop)
219 {
220 regs->esp = infop->start_stack;
221 regs->eip = infop->entry;
222
223 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
224 starts %edx contains a pointer to a function which might be
225 registered using `atexit'. This provides a mean for the
226 dynamic linker to call DT_FINI functions for shared libraries
227 that have been loaded before the code runs.
228
229 A value of 0 tells we have no such handler. */
230 regs->edx = 0;
231 }
232
233 #define ELF_NREG 17
234 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
235
236 /*
237 * Note that ELF_NREG should be 19 as there should be place for
238 * TRAPNO and ERR "registers" as well but linux doesn't dump
239 * those.
240 *
241 * See linux kernel: arch/x86/include/asm/elf.h
242 */
243 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
244 {
245 (*regs)[0] = env->regs[R_EBX];
246 (*regs)[1] = env->regs[R_ECX];
247 (*regs)[2] = env->regs[R_EDX];
248 (*regs)[3] = env->regs[R_ESI];
249 (*regs)[4] = env->regs[R_EDI];
250 (*regs)[5] = env->regs[R_EBP];
251 (*regs)[6] = env->regs[R_EAX];
252 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
253 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
254 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
255 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
256 (*regs)[11] = env->regs[R_EAX]; /* XXX */
257 (*regs)[12] = env->eip;
258 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
259 (*regs)[14] = env->eflags;
260 (*regs)[15] = env->regs[R_ESP];
261 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
262 }
263 #endif
264
265 #define USE_ELF_CORE_DUMP
266 #define ELF_EXEC_PAGESIZE 4096
267
268 #endif
269
270 #ifdef TARGET_ARM
271
272 #ifndef TARGET_AARCH64
273 /* 32 bit ARM definitions */
274
275 #define ELF_START_MMAP 0x80000000
276
277 #define ELF_ARCH EM_ARM
278 #define ELF_CLASS ELFCLASS32
279
280 static inline void init_thread(struct target_pt_regs *regs,
281 struct image_info *infop)
282 {
283 abi_long stack = infop->start_stack;
284 memset(regs, 0, sizeof(*regs));
285
286 regs->uregs[16] = ARM_CPU_MODE_USR;
287 if (infop->entry & 1) {
288 regs->uregs[16] |= CPSR_T;
289 }
290 regs->uregs[15] = infop->entry & 0xfffffffe;
291 regs->uregs[13] = infop->start_stack;
292 /* FIXME - what to for failure of get_user()? */
293 get_user_ual(regs->uregs[2], stack + 8); /* envp */
294 get_user_ual(regs->uregs[1], stack + 4); /* envp */
295 /* XXX: it seems that r0 is zeroed after ! */
296 regs->uregs[0] = 0;
297 /* For uClinux PIC binaries. */
298 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
299 regs->uregs[10] = infop->start_data;
300
301 /* Support ARM FDPIC. */
302 if (info_is_fdpic(infop)) {
303 /* As described in the ABI document, r7 points to the loadmap info
304 * prepared by the kernel. If an interpreter is needed, r8 points
305 * to the interpreter loadmap and r9 points to the interpreter
306 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
307 * r9 points to the main program PT_DYNAMIC info.
308 */
309 regs->uregs[7] = infop->loadmap_addr;
310 if (infop->interpreter_loadmap_addr) {
311 /* Executable is dynamically loaded. */
312 regs->uregs[8] = infop->interpreter_loadmap_addr;
313 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
314 } else {
315 regs->uregs[8] = 0;
316 regs->uregs[9] = infop->pt_dynamic_addr;
317 }
318 }
319 }
320
321 #define ELF_NREG 18
322 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
323
324 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
325 {
326 (*regs)[0] = tswapreg(env->regs[0]);
327 (*regs)[1] = tswapreg(env->regs[1]);
328 (*regs)[2] = tswapreg(env->regs[2]);
329 (*regs)[3] = tswapreg(env->regs[3]);
330 (*regs)[4] = tswapreg(env->regs[4]);
331 (*regs)[5] = tswapreg(env->regs[5]);
332 (*regs)[6] = tswapreg(env->regs[6]);
333 (*regs)[7] = tswapreg(env->regs[7]);
334 (*regs)[8] = tswapreg(env->regs[8]);
335 (*regs)[9] = tswapreg(env->regs[9]);
336 (*regs)[10] = tswapreg(env->regs[10]);
337 (*regs)[11] = tswapreg(env->regs[11]);
338 (*regs)[12] = tswapreg(env->regs[12]);
339 (*regs)[13] = tswapreg(env->regs[13]);
340 (*regs)[14] = tswapreg(env->regs[14]);
341 (*regs)[15] = tswapreg(env->regs[15]);
342
343 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
344 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
345 }
346
347 #define USE_ELF_CORE_DUMP
348 #define ELF_EXEC_PAGESIZE 4096
349
350 enum
351 {
352 ARM_HWCAP_ARM_SWP = 1 << 0,
353 ARM_HWCAP_ARM_HALF = 1 << 1,
354 ARM_HWCAP_ARM_THUMB = 1 << 2,
355 ARM_HWCAP_ARM_26BIT = 1 << 3,
356 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
357 ARM_HWCAP_ARM_FPA = 1 << 5,
358 ARM_HWCAP_ARM_VFP = 1 << 6,
359 ARM_HWCAP_ARM_EDSP = 1 << 7,
360 ARM_HWCAP_ARM_JAVA = 1 << 8,
361 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
362 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
363 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
364 ARM_HWCAP_ARM_NEON = 1 << 12,
365 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
366 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
367 ARM_HWCAP_ARM_TLS = 1 << 15,
368 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
369 ARM_HWCAP_ARM_IDIVA = 1 << 17,
370 ARM_HWCAP_ARM_IDIVT = 1 << 18,
371 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
372 ARM_HWCAP_ARM_LPAE = 1 << 20,
373 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
374 };
375
376 enum {
377 ARM_HWCAP2_ARM_AES = 1 << 0,
378 ARM_HWCAP2_ARM_PMULL = 1 << 1,
379 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
380 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
381 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
382 };
383
384 /* The commpage only exists for 32 bit kernels */
385
386 #define ARM_COMMPAGE (intptr_t)0xffff0f00u
387
388 static bool init_guest_commpage(void)
389 {
390 void *want = g2h(ARM_COMMPAGE & -qemu_host_page_size);
391 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
392 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
393
394 if (addr == MAP_FAILED) {
395 perror("Allocating guest commpage");
396 exit(EXIT_FAILURE);
397 }
398 if (addr != want) {
399 return false;
400 }
401
402 /* Set kernel helper versions; rest of page is 0. */
403 __put_user(5, (uint32_t *)g2h(0xffff0ffcu));
404
405 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
406 perror("Protecting guest commpage");
407 exit(EXIT_FAILURE);
408 }
409 return true;
410 }
411
412 #define ELF_HWCAP get_elf_hwcap()
413 #define ELF_HWCAP2 get_elf_hwcap2()
414
415 static uint32_t get_elf_hwcap(void)
416 {
417 ARMCPU *cpu = ARM_CPU(thread_cpu);
418 uint32_t hwcaps = 0;
419
420 hwcaps |= ARM_HWCAP_ARM_SWP;
421 hwcaps |= ARM_HWCAP_ARM_HALF;
422 hwcaps |= ARM_HWCAP_ARM_THUMB;
423 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
424
425 /* probe for the extra features */
426 #define GET_FEATURE(feat, hwcap) \
427 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
428
429 #define GET_FEATURE_ID(feat, hwcap) \
430 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
431
432 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
433 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
434 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
435 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
436 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
437 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
438 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
439 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
440 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
441 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
442
443 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
444 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
445 hwcaps |= ARM_HWCAP_ARM_VFPv3;
446 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
447 hwcaps |= ARM_HWCAP_ARM_VFPD32;
448 } else {
449 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
450 }
451 }
452 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
453
454 return hwcaps;
455 }
456
457 static uint32_t get_elf_hwcap2(void)
458 {
459 ARMCPU *cpu = ARM_CPU(thread_cpu);
460 uint32_t hwcaps = 0;
461
462 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
463 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
464 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
465 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
466 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
467 return hwcaps;
468 }
469
470 #undef GET_FEATURE
471 #undef GET_FEATURE_ID
472
473 #define ELF_PLATFORM get_elf_platform()
474
475 static const char *get_elf_platform(void)
476 {
477 CPUARMState *env = thread_cpu->env_ptr;
478
479 #ifdef TARGET_WORDS_BIGENDIAN
480 # define END "b"
481 #else
482 # define END "l"
483 #endif
484
485 if (arm_feature(env, ARM_FEATURE_V8)) {
486 return "v8" END;
487 } else if (arm_feature(env, ARM_FEATURE_V7)) {
488 if (arm_feature(env, ARM_FEATURE_M)) {
489 return "v7m" END;
490 } else {
491 return "v7" END;
492 }
493 } else if (arm_feature(env, ARM_FEATURE_V6)) {
494 return "v6" END;
495 } else if (arm_feature(env, ARM_FEATURE_V5)) {
496 return "v5" END;
497 } else {
498 return "v4" END;
499 }
500
501 #undef END
502 }
503
504 #else
505 /* 64 bit ARM definitions */
506 #define ELF_START_MMAP 0x80000000
507
508 #define ELF_ARCH EM_AARCH64
509 #define ELF_CLASS ELFCLASS64
510 #ifdef TARGET_WORDS_BIGENDIAN
511 # define ELF_PLATFORM "aarch64_be"
512 #else
513 # define ELF_PLATFORM "aarch64"
514 #endif
515
516 static inline void init_thread(struct target_pt_regs *regs,
517 struct image_info *infop)
518 {
519 abi_long stack = infop->start_stack;
520 memset(regs, 0, sizeof(*regs));
521
522 regs->pc = infop->entry & ~0x3ULL;
523 regs->sp = stack;
524 }
525
526 #define ELF_NREG 34
527 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
528
529 static void elf_core_copy_regs(target_elf_gregset_t *regs,
530 const CPUARMState *env)
531 {
532 int i;
533
534 for (i = 0; i < 32; i++) {
535 (*regs)[i] = tswapreg(env->xregs[i]);
536 }
537 (*regs)[32] = tswapreg(env->pc);
538 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
539 }
540
541 #define USE_ELF_CORE_DUMP
542 #define ELF_EXEC_PAGESIZE 4096
543
544 enum {
545 ARM_HWCAP_A64_FP = 1 << 0,
546 ARM_HWCAP_A64_ASIMD = 1 << 1,
547 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
548 ARM_HWCAP_A64_AES = 1 << 3,
549 ARM_HWCAP_A64_PMULL = 1 << 4,
550 ARM_HWCAP_A64_SHA1 = 1 << 5,
551 ARM_HWCAP_A64_SHA2 = 1 << 6,
552 ARM_HWCAP_A64_CRC32 = 1 << 7,
553 ARM_HWCAP_A64_ATOMICS = 1 << 8,
554 ARM_HWCAP_A64_FPHP = 1 << 9,
555 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
556 ARM_HWCAP_A64_CPUID = 1 << 11,
557 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
558 ARM_HWCAP_A64_JSCVT = 1 << 13,
559 ARM_HWCAP_A64_FCMA = 1 << 14,
560 ARM_HWCAP_A64_LRCPC = 1 << 15,
561 ARM_HWCAP_A64_DCPOP = 1 << 16,
562 ARM_HWCAP_A64_SHA3 = 1 << 17,
563 ARM_HWCAP_A64_SM3 = 1 << 18,
564 ARM_HWCAP_A64_SM4 = 1 << 19,
565 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
566 ARM_HWCAP_A64_SHA512 = 1 << 21,
567 ARM_HWCAP_A64_SVE = 1 << 22,
568 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
569 ARM_HWCAP_A64_DIT = 1 << 24,
570 ARM_HWCAP_A64_USCAT = 1 << 25,
571 ARM_HWCAP_A64_ILRCPC = 1 << 26,
572 ARM_HWCAP_A64_FLAGM = 1 << 27,
573 ARM_HWCAP_A64_SSBS = 1 << 28,
574 ARM_HWCAP_A64_SB = 1 << 29,
575 ARM_HWCAP_A64_PACA = 1 << 30,
576 ARM_HWCAP_A64_PACG = 1UL << 31,
577
578 ARM_HWCAP2_A64_DCPODP = 1 << 0,
579 ARM_HWCAP2_A64_SVE2 = 1 << 1,
580 ARM_HWCAP2_A64_SVEAES = 1 << 2,
581 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
582 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
583 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
584 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
585 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
586 ARM_HWCAP2_A64_FRINT = 1 << 8,
587 };
588
589 #define ELF_HWCAP get_elf_hwcap()
590 #define ELF_HWCAP2 get_elf_hwcap2()
591
592 #define GET_FEATURE_ID(feat, hwcap) \
593 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
594
595 static uint32_t get_elf_hwcap(void)
596 {
597 ARMCPU *cpu = ARM_CPU(thread_cpu);
598 uint32_t hwcaps = 0;
599
600 hwcaps |= ARM_HWCAP_A64_FP;
601 hwcaps |= ARM_HWCAP_A64_ASIMD;
602 hwcaps |= ARM_HWCAP_A64_CPUID;
603
604 /* probe for the extra features */
605
606 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
607 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
608 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
609 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
610 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
611 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
612 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
613 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
614 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
615 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
616 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
617 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
618 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
619 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
620 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
621 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
622 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
623 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
624 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
625 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
626 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
627 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
628 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
629
630 return hwcaps;
631 }
632
633 static uint32_t get_elf_hwcap2(void)
634 {
635 ARMCPU *cpu = ARM_CPU(thread_cpu);
636 uint32_t hwcaps = 0;
637
638 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
639 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
640 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
641
642 return hwcaps;
643 }
644
645 #undef GET_FEATURE_ID
646
647 #endif /* not TARGET_AARCH64 */
648 #endif /* TARGET_ARM */
649
650 #ifdef TARGET_SPARC
651 #ifdef TARGET_SPARC64
652
653 #define ELF_START_MMAP 0x80000000
654 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
655 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
656 #ifndef TARGET_ABI32
657 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
658 #else
659 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
660 #endif
661
662 #define ELF_CLASS ELFCLASS64
663 #define ELF_ARCH EM_SPARCV9
664
665 #define STACK_BIAS 2047
666
667 static inline void init_thread(struct target_pt_regs *regs,
668 struct image_info *infop)
669 {
670 #ifndef TARGET_ABI32
671 regs->tstate = 0;
672 #endif
673 regs->pc = infop->entry;
674 regs->npc = regs->pc + 4;
675 regs->y = 0;
676 #ifdef TARGET_ABI32
677 regs->u_regs[14] = infop->start_stack - 16 * 4;
678 #else
679 if (personality(infop->personality) == PER_LINUX32)
680 regs->u_regs[14] = infop->start_stack - 16 * 4;
681 else
682 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
683 #endif
684 }
685
686 #else
687 #define ELF_START_MMAP 0x80000000
688 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
689 | HWCAP_SPARC_MULDIV)
690
691 #define ELF_CLASS ELFCLASS32
692 #define ELF_ARCH EM_SPARC
693
694 static inline void init_thread(struct target_pt_regs *regs,
695 struct image_info *infop)
696 {
697 regs->psr = 0;
698 regs->pc = infop->entry;
699 regs->npc = regs->pc + 4;
700 regs->y = 0;
701 regs->u_regs[14] = infop->start_stack - 16 * 4;
702 }
703
704 #endif
705 #endif
706
707 #ifdef TARGET_PPC
708
709 #define ELF_MACHINE PPC_ELF_MACHINE
710 #define ELF_START_MMAP 0x80000000
711
712 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
713
714 #define elf_check_arch(x) ( (x) == EM_PPC64 )
715
716 #define ELF_CLASS ELFCLASS64
717
718 #else
719
720 #define ELF_CLASS ELFCLASS32
721
722 #endif
723
724 #define ELF_ARCH EM_PPC
725
726 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
727 See arch/powerpc/include/asm/cputable.h. */
728 enum {
729 QEMU_PPC_FEATURE_32 = 0x80000000,
730 QEMU_PPC_FEATURE_64 = 0x40000000,
731 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
732 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
733 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
734 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
735 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
736 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
737 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
738 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
739 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
740 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
741 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
742 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
743 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
744 QEMU_PPC_FEATURE_CELL = 0x00010000,
745 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
746 QEMU_PPC_FEATURE_SMT = 0x00004000,
747 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
748 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
749 QEMU_PPC_FEATURE_PA6T = 0x00000800,
750 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
751 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
752 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
753 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
754 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
755
756 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
757 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
758
759 /* Feature definitions in AT_HWCAP2. */
760 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
761 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
762 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
763 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
764 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
765 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
766 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
767 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
768 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
769 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
770 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
771 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
772 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
773 };
774
775 #define ELF_HWCAP get_elf_hwcap()
776
777 static uint32_t get_elf_hwcap(void)
778 {
779 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
780 uint32_t features = 0;
781
782 /* We don't have to be terribly complete here; the high points are
783 Altivec/FP/SPE support. Anything else is just a bonus. */
784 #define GET_FEATURE(flag, feature) \
785 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
786 #define GET_FEATURE2(flags, feature) \
787 do { \
788 if ((cpu->env.insns_flags2 & flags) == flags) { \
789 features |= feature; \
790 } \
791 } while (0)
792 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
793 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
794 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
795 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
796 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
797 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
798 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
799 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
800 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
801 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
802 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
803 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
804 QEMU_PPC_FEATURE_ARCH_2_06);
805 #undef GET_FEATURE
806 #undef GET_FEATURE2
807
808 return features;
809 }
810
811 #define ELF_HWCAP2 get_elf_hwcap2()
812
813 static uint32_t get_elf_hwcap2(void)
814 {
815 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
816 uint32_t features = 0;
817
818 #define GET_FEATURE(flag, feature) \
819 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
820 #define GET_FEATURE2(flag, feature) \
821 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
822
823 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
824 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
825 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
826 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
827 QEMU_PPC_FEATURE2_VEC_CRYPTO);
828 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
829 QEMU_PPC_FEATURE2_DARN);
830
831 #undef GET_FEATURE
832 #undef GET_FEATURE2
833
834 return features;
835 }
836
837 /*
838 * The requirements here are:
839 * - keep the final alignment of sp (sp & 0xf)
840 * - make sure the 32-bit value at the first 16 byte aligned position of
841 * AUXV is greater than 16 for glibc compatibility.
842 * AT_IGNOREPPC is used for that.
843 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
844 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
845 */
846 #define DLINFO_ARCH_ITEMS 5
847 #define ARCH_DLINFO \
848 do { \
849 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
850 /* \
851 * Handle glibc compatibility: these magic entries must \
852 * be at the lowest addresses in the final auxv. \
853 */ \
854 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
855 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
856 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
857 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
858 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
859 } while (0)
860
861 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
862 {
863 _regs->gpr[1] = infop->start_stack;
864 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
865 if (get_ppc64_abi(infop) < 2) {
866 uint64_t val;
867 get_user_u64(val, infop->entry + 8);
868 _regs->gpr[2] = val + infop->load_bias;
869 get_user_u64(val, infop->entry);
870 infop->entry = val + infop->load_bias;
871 } else {
872 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
873 }
874 #endif
875 _regs->nip = infop->entry;
876 }
877
878 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
879 #define ELF_NREG 48
880 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
881
882 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
883 {
884 int i;
885 target_ulong ccr = 0;
886
887 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
888 (*regs)[i] = tswapreg(env->gpr[i]);
889 }
890
891 (*regs)[32] = tswapreg(env->nip);
892 (*regs)[33] = tswapreg(env->msr);
893 (*regs)[35] = tswapreg(env->ctr);
894 (*regs)[36] = tswapreg(env->lr);
895 (*regs)[37] = tswapreg(env->xer);
896
897 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
898 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
899 }
900 (*regs)[38] = tswapreg(ccr);
901 }
902
903 #define USE_ELF_CORE_DUMP
904 #define ELF_EXEC_PAGESIZE 4096
905
906 #endif
907
908 #ifdef TARGET_MIPS
909
910 #define ELF_START_MMAP 0x80000000
911
912 #ifdef TARGET_MIPS64
913 #define ELF_CLASS ELFCLASS64
914 #else
915 #define ELF_CLASS ELFCLASS32
916 #endif
917 #define ELF_ARCH EM_MIPS
918
919 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
920
921 #ifdef TARGET_ABI_MIPSN32
922 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
923 #else
924 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
925 #endif
926
927 static inline void init_thread(struct target_pt_regs *regs,
928 struct image_info *infop)
929 {
930 regs->cp0_status = 2 << CP0St_KSU;
931 regs->cp0_epc = infop->entry;
932 regs->regs[29] = infop->start_stack;
933 }
934
935 /* See linux kernel: arch/mips/include/asm/elf.h. */
936 #define ELF_NREG 45
937 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
938
939 /* See linux kernel: arch/mips/include/asm/reg.h. */
940 enum {
941 #ifdef TARGET_MIPS64
942 TARGET_EF_R0 = 0,
943 #else
944 TARGET_EF_R0 = 6,
945 #endif
946 TARGET_EF_R26 = TARGET_EF_R0 + 26,
947 TARGET_EF_R27 = TARGET_EF_R0 + 27,
948 TARGET_EF_LO = TARGET_EF_R0 + 32,
949 TARGET_EF_HI = TARGET_EF_R0 + 33,
950 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
951 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
952 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
953 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
954 };
955
956 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
957 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
958 {
959 int i;
960
961 for (i = 0; i < TARGET_EF_R0; i++) {
962 (*regs)[i] = 0;
963 }
964 (*regs)[TARGET_EF_R0] = 0;
965
966 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
967 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
968 }
969
970 (*regs)[TARGET_EF_R26] = 0;
971 (*regs)[TARGET_EF_R27] = 0;
972 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
973 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
974 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
975 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
976 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
977 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
978 }
979
980 #define USE_ELF_CORE_DUMP
981 #define ELF_EXEC_PAGESIZE 4096
982
983 /* See arch/mips/include/uapi/asm/hwcap.h. */
984 enum {
985 HWCAP_MIPS_R6 = (1 << 0),
986 HWCAP_MIPS_MSA = (1 << 1),
987 };
988
989 #define ELF_HWCAP get_elf_hwcap()
990
991 static uint32_t get_elf_hwcap(void)
992 {
993 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
994 uint32_t hwcaps = 0;
995
996 #define GET_FEATURE(flag, hwcap) \
997 do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
998
999 GET_FEATURE(ISA_MIPS32R6 | ISA_MIPS64R6, HWCAP_MIPS_R6);
1000 GET_FEATURE(ASE_MSA, HWCAP_MIPS_MSA);
1001
1002 #undef GET_FEATURE
1003
1004 return hwcaps;
1005 }
1006
1007 #endif /* TARGET_MIPS */
1008
1009 #ifdef TARGET_MICROBLAZE
1010
1011 #define ELF_START_MMAP 0x80000000
1012
1013 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1014
1015 #define ELF_CLASS ELFCLASS32
1016 #define ELF_ARCH EM_MICROBLAZE
1017
1018 static inline void init_thread(struct target_pt_regs *regs,
1019 struct image_info *infop)
1020 {
1021 regs->pc = infop->entry;
1022 regs->r1 = infop->start_stack;
1023
1024 }
1025
1026 #define ELF_EXEC_PAGESIZE 4096
1027
1028 #define USE_ELF_CORE_DUMP
1029 #define ELF_NREG 38
1030 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1031
1032 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1033 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1034 {
1035 int i, pos = 0;
1036
1037 for (i = 0; i < 32; i++) {
1038 (*regs)[pos++] = tswapreg(env->regs[i]);
1039 }
1040
1041 for (i = 0; i < 6; i++) {
1042 (*regs)[pos++] = tswapreg(env->sregs[i]);
1043 }
1044 }
1045
1046 #endif /* TARGET_MICROBLAZE */
1047
1048 #ifdef TARGET_NIOS2
1049
1050 #define ELF_START_MMAP 0x80000000
1051
1052 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1053
1054 #define ELF_CLASS ELFCLASS32
1055 #define ELF_ARCH EM_ALTERA_NIOS2
1056
1057 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1058 {
1059 regs->ea = infop->entry;
1060 regs->sp = infop->start_stack;
1061 regs->estatus = 0x3;
1062 }
1063
1064 #define ELF_EXEC_PAGESIZE 4096
1065
1066 #define USE_ELF_CORE_DUMP
1067 #define ELF_NREG 49
1068 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1069
1070 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1071 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1072 const CPUNios2State *env)
1073 {
1074 int i;
1075
1076 (*regs)[0] = -1;
1077 for (i = 1; i < 8; i++) /* r0-r7 */
1078 (*regs)[i] = tswapreg(env->regs[i + 7]);
1079
1080 for (i = 8; i < 16; i++) /* r8-r15 */
1081 (*regs)[i] = tswapreg(env->regs[i - 8]);
1082
1083 for (i = 16; i < 24; i++) /* r16-r23 */
1084 (*regs)[i] = tswapreg(env->regs[i + 7]);
1085 (*regs)[24] = -1; /* R_ET */
1086 (*regs)[25] = -1; /* R_BT */
1087 (*regs)[26] = tswapreg(env->regs[R_GP]);
1088 (*regs)[27] = tswapreg(env->regs[R_SP]);
1089 (*regs)[28] = tswapreg(env->regs[R_FP]);
1090 (*regs)[29] = tswapreg(env->regs[R_EA]);
1091 (*regs)[30] = -1; /* R_SSTATUS */
1092 (*regs)[31] = tswapreg(env->regs[R_RA]);
1093
1094 (*regs)[32] = tswapreg(env->regs[R_PC]);
1095
1096 (*regs)[33] = -1; /* R_STATUS */
1097 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1098
1099 for (i = 35; i < 49; i++) /* ... */
1100 (*regs)[i] = -1;
1101 }
1102
1103 #endif /* TARGET_NIOS2 */
1104
1105 #ifdef TARGET_OPENRISC
1106
1107 #define ELF_START_MMAP 0x08000000
1108
1109 #define ELF_ARCH EM_OPENRISC
1110 #define ELF_CLASS ELFCLASS32
1111 #define ELF_DATA ELFDATA2MSB
1112
1113 static inline void init_thread(struct target_pt_regs *regs,
1114 struct image_info *infop)
1115 {
1116 regs->pc = infop->entry;
1117 regs->gpr[1] = infop->start_stack;
1118 }
1119
1120 #define USE_ELF_CORE_DUMP
1121 #define ELF_EXEC_PAGESIZE 8192
1122
1123 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1124 #define ELF_NREG 34 /* gprs and pc, sr */
1125 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1126
1127 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1128 const CPUOpenRISCState *env)
1129 {
1130 int i;
1131
1132 for (i = 0; i < 32; i++) {
1133 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1134 }
1135 (*regs)[32] = tswapreg(env->pc);
1136 (*regs)[33] = tswapreg(cpu_get_sr(env));
1137 }
1138 #define ELF_HWCAP 0
1139 #define ELF_PLATFORM NULL
1140
1141 #endif /* TARGET_OPENRISC */
1142
1143 #ifdef TARGET_SH4
1144
1145 #define ELF_START_MMAP 0x80000000
1146
1147 #define ELF_CLASS ELFCLASS32
1148 #define ELF_ARCH EM_SH
1149
1150 static inline void init_thread(struct target_pt_regs *regs,
1151 struct image_info *infop)
1152 {
1153 /* Check other registers XXXXX */
1154 regs->pc = infop->entry;
1155 regs->regs[15] = infop->start_stack;
1156 }
1157
1158 /* See linux kernel: arch/sh/include/asm/elf.h. */
1159 #define ELF_NREG 23
1160 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1161
1162 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1163 enum {
1164 TARGET_REG_PC = 16,
1165 TARGET_REG_PR = 17,
1166 TARGET_REG_SR = 18,
1167 TARGET_REG_GBR = 19,
1168 TARGET_REG_MACH = 20,
1169 TARGET_REG_MACL = 21,
1170 TARGET_REG_SYSCALL = 22
1171 };
1172
1173 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1174 const CPUSH4State *env)
1175 {
1176 int i;
1177
1178 for (i = 0; i < 16; i++) {
1179 (*regs)[i] = tswapreg(env->gregs[i]);
1180 }
1181
1182 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1183 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1184 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1185 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1186 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1187 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1188 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1189 }
1190
1191 #define USE_ELF_CORE_DUMP
1192 #define ELF_EXEC_PAGESIZE 4096
1193
1194 enum {
1195 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1196 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1197 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1198 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1199 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1200 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1201 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1202 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1203 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1204 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1205 };
1206
1207 #define ELF_HWCAP get_elf_hwcap()
1208
1209 static uint32_t get_elf_hwcap(void)
1210 {
1211 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1212 uint32_t hwcap = 0;
1213
1214 hwcap |= SH_CPU_HAS_FPU;
1215
1216 if (cpu->env.features & SH_FEATURE_SH4A) {
1217 hwcap |= SH_CPU_HAS_LLSC;
1218 }
1219
1220 return hwcap;
1221 }
1222
1223 #endif
1224
1225 #ifdef TARGET_CRIS
1226
1227 #define ELF_START_MMAP 0x80000000
1228
1229 #define ELF_CLASS ELFCLASS32
1230 #define ELF_ARCH EM_CRIS
1231
1232 static inline void init_thread(struct target_pt_regs *regs,
1233 struct image_info *infop)
1234 {
1235 regs->erp = infop->entry;
1236 }
1237
1238 #define ELF_EXEC_PAGESIZE 8192
1239
1240 #endif
1241
1242 #ifdef TARGET_M68K
1243
1244 #define ELF_START_MMAP 0x80000000
1245
1246 #define ELF_CLASS ELFCLASS32
1247 #define ELF_ARCH EM_68K
1248
1249 /* ??? Does this need to do anything?
1250 #define ELF_PLAT_INIT(_r) */
1251
1252 static inline void init_thread(struct target_pt_regs *regs,
1253 struct image_info *infop)
1254 {
1255 regs->usp = infop->start_stack;
1256 regs->sr = 0;
1257 regs->pc = infop->entry;
1258 }
1259
1260 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1261 #define ELF_NREG 20
1262 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1263
1264 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1265 {
1266 (*regs)[0] = tswapreg(env->dregs[1]);
1267 (*regs)[1] = tswapreg(env->dregs[2]);
1268 (*regs)[2] = tswapreg(env->dregs[3]);
1269 (*regs)[3] = tswapreg(env->dregs[4]);
1270 (*regs)[4] = tswapreg(env->dregs[5]);
1271 (*regs)[5] = tswapreg(env->dregs[6]);
1272 (*regs)[6] = tswapreg(env->dregs[7]);
1273 (*regs)[7] = tswapreg(env->aregs[0]);
1274 (*regs)[8] = tswapreg(env->aregs[1]);
1275 (*regs)[9] = tswapreg(env->aregs[2]);
1276 (*regs)[10] = tswapreg(env->aregs[3]);
1277 (*regs)[11] = tswapreg(env->aregs[4]);
1278 (*regs)[12] = tswapreg(env->aregs[5]);
1279 (*regs)[13] = tswapreg(env->aregs[6]);
1280 (*regs)[14] = tswapreg(env->dregs[0]);
1281 (*regs)[15] = tswapreg(env->aregs[7]);
1282 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1283 (*regs)[17] = tswapreg(env->sr);
1284 (*regs)[18] = tswapreg(env->pc);
1285 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1286 }
1287
1288 #define USE_ELF_CORE_DUMP
1289 #define ELF_EXEC_PAGESIZE 8192
1290
1291 #endif
1292
1293 #ifdef TARGET_ALPHA
1294
1295 #define ELF_START_MMAP (0x30000000000ULL)
1296
1297 #define ELF_CLASS ELFCLASS64
1298 #define ELF_ARCH EM_ALPHA
1299
1300 static inline void init_thread(struct target_pt_regs *regs,
1301 struct image_info *infop)
1302 {
1303 regs->pc = infop->entry;
1304 regs->ps = 8;
1305 regs->usp = infop->start_stack;
1306 }
1307
1308 #define ELF_EXEC_PAGESIZE 8192
1309
1310 #endif /* TARGET_ALPHA */
1311
1312 #ifdef TARGET_S390X
1313
1314 #define ELF_START_MMAP (0x20000000000ULL)
1315
1316 #define ELF_CLASS ELFCLASS64
1317 #define ELF_DATA ELFDATA2MSB
1318 #define ELF_ARCH EM_S390
1319
1320 #include "elf.h"
1321
1322 #define ELF_HWCAP get_elf_hwcap()
1323
1324 #define GET_FEATURE(_feat, _hwcap) \
1325 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1326
1327 static uint32_t get_elf_hwcap(void)
1328 {
1329 /*
1330 * Let's assume we always have esan3 and zarch.
1331 * 31-bit processes can use 64-bit registers (high gprs).
1332 */
1333 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1334
1335 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1336 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1337 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1338 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1339 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1340 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1341 hwcap |= HWCAP_S390_ETF3EH;
1342 }
1343 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1344
1345 return hwcap;
1346 }
1347
1348 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1349 {
1350 regs->psw.addr = infop->entry;
1351 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1352 regs->gprs[15] = infop->start_stack;
1353 }
1354
1355 #endif /* TARGET_S390X */
1356
1357 #ifdef TARGET_TILEGX
1358
1359 /* 42 bits real used address, a half for user mode */
1360 #define ELF_START_MMAP (0x00000020000000000ULL)
1361
1362 #define elf_check_arch(x) ((x) == EM_TILEGX)
1363
1364 #define ELF_CLASS ELFCLASS64
1365 #define ELF_DATA ELFDATA2LSB
1366 #define ELF_ARCH EM_TILEGX
1367
1368 static inline void init_thread(struct target_pt_regs *regs,
1369 struct image_info *infop)
1370 {
1371 regs->pc = infop->entry;
1372 regs->sp = infop->start_stack;
1373
1374 }
1375
1376 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1377
1378 #endif /* TARGET_TILEGX */
1379
1380 #ifdef TARGET_RISCV
1381
1382 #define ELF_START_MMAP 0x80000000
1383 #define ELF_ARCH EM_RISCV
1384
1385 #ifdef TARGET_RISCV32
1386 #define ELF_CLASS ELFCLASS32
1387 #else
1388 #define ELF_CLASS ELFCLASS64
1389 #endif
1390
1391 static inline void init_thread(struct target_pt_regs *regs,
1392 struct image_info *infop)
1393 {
1394 regs->sepc = infop->entry;
1395 regs->sp = infop->start_stack;
1396 }
1397
1398 #define ELF_EXEC_PAGESIZE 4096
1399
1400 #endif /* TARGET_RISCV */
1401
1402 #ifdef TARGET_HPPA
1403
1404 #define ELF_START_MMAP 0x80000000
1405 #define ELF_CLASS ELFCLASS32
1406 #define ELF_ARCH EM_PARISC
1407 #define ELF_PLATFORM "PARISC"
1408 #define STACK_GROWS_DOWN 0
1409 #define STACK_ALIGNMENT 64
1410
1411 static inline void init_thread(struct target_pt_regs *regs,
1412 struct image_info *infop)
1413 {
1414 regs->iaoq[0] = infop->entry;
1415 regs->iaoq[1] = infop->entry + 4;
1416 regs->gr[23] = 0;
1417 regs->gr[24] = infop->arg_start;
1418 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1419 /* The top-of-stack contains a linkage buffer. */
1420 regs->gr[30] = infop->start_stack + 64;
1421 regs->gr[31] = infop->entry;
1422 }
1423
1424 #endif /* TARGET_HPPA */
1425
1426 #ifdef TARGET_XTENSA
1427
1428 #define ELF_START_MMAP 0x20000000
1429
1430 #define ELF_CLASS ELFCLASS32
1431 #define ELF_ARCH EM_XTENSA
1432
1433 static inline void init_thread(struct target_pt_regs *regs,
1434 struct image_info *infop)
1435 {
1436 regs->windowbase = 0;
1437 regs->windowstart = 1;
1438 regs->areg[1] = infop->start_stack;
1439 regs->pc = infop->entry;
1440 }
1441
1442 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1443 #define ELF_NREG 128
1444 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1445
1446 enum {
1447 TARGET_REG_PC,
1448 TARGET_REG_PS,
1449 TARGET_REG_LBEG,
1450 TARGET_REG_LEND,
1451 TARGET_REG_LCOUNT,
1452 TARGET_REG_SAR,
1453 TARGET_REG_WINDOWSTART,
1454 TARGET_REG_WINDOWBASE,
1455 TARGET_REG_THREADPTR,
1456 TARGET_REG_AR0 = 64,
1457 };
1458
1459 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1460 const CPUXtensaState *env)
1461 {
1462 unsigned i;
1463
1464 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1465 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1466 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1467 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1468 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1469 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1470 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1471 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1472 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1473 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1474 for (i = 0; i < env->config->nareg; ++i) {
1475 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1476 }
1477 }
1478
1479 #define USE_ELF_CORE_DUMP
1480 #define ELF_EXEC_PAGESIZE 4096
1481
1482 #endif /* TARGET_XTENSA */
1483
1484 #ifndef ELF_PLATFORM
1485 #define ELF_PLATFORM (NULL)
1486 #endif
1487
1488 #ifndef ELF_MACHINE
1489 #define ELF_MACHINE ELF_ARCH
1490 #endif
1491
1492 #ifndef elf_check_arch
1493 #define elf_check_arch(x) ((x) == ELF_ARCH)
1494 #endif
1495
1496 #ifndef elf_check_abi
1497 #define elf_check_abi(x) (1)
1498 #endif
1499
1500 #ifndef ELF_HWCAP
1501 #define ELF_HWCAP 0
1502 #endif
1503
1504 #ifndef STACK_GROWS_DOWN
1505 #define STACK_GROWS_DOWN 1
1506 #endif
1507
1508 #ifndef STACK_ALIGNMENT
1509 #define STACK_ALIGNMENT 16
1510 #endif
1511
1512 #ifdef TARGET_ABI32
1513 #undef ELF_CLASS
1514 #define ELF_CLASS ELFCLASS32
1515 #undef bswaptls
1516 #define bswaptls(ptr) bswap32s(ptr)
1517 #endif
1518
1519 #include "elf.h"
1520
1521 struct exec
1522 {
1523 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1524 unsigned int a_text; /* length of text, in bytes */
1525 unsigned int a_data; /* length of data, in bytes */
1526 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1527 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1528 unsigned int a_entry; /* start address */
1529 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1530 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1531 };
1532
1533
1534 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1535 #define OMAGIC 0407
1536 #define NMAGIC 0410
1537 #define ZMAGIC 0413
1538 #define QMAGIC 0314
1539
1540 /* Necessary parameters */
1541 #define TARGET_ELF_EXEC_PAGESIZE \
1542 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1543 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1544 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1545 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1546 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1547 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1548
1549 #define DLINFO_ITEMS 16
1550
1551 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1552 {
1553 memcpy(to, from, n);
1554 }
1555
1556 #ifdef BSWAP_NEEDED
1557 static void bswap_ehdr(struct elfhdr *ehdr)
1558 {
1559 bswap16s(&ehdr->e_type); /* Object file type */
1560 bswap16s(&ehdr->e_machine); /* Architecture */
1561 bswap32s(&ehdr->e_version); /* Object file version */
1562 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1563 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1564 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1565 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1566 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1567 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1568 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1569 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1570 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1571 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1572 }
1573
1574 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1575 {
1576 int i;
1577 for (i = 0; i < phnum; ++i, ++phdr) {
1578 bswap32s(&phdr->p_type); /* Segment type */
1579 bswap32s(&phdr->p_flags); /* Segment flags */
1580 bswaptls(&phdr->p_offset); /* Segment file offset */
1581 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1582 bswaptls(&phdr->p_paddr); /* Segment physical address */
1583 bswaptls(&phdr->p_filesz); /* Segment size in file */
1584 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1585 bswaptls(&phdr->p_align); /* Segment alignment */
1586 }
1587 }
1588
1589 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1590 {
1591 int i;
1592 for (i = 0; i < shnum; ++i, ++shdr) {
1593 bswap32s(&shdr->sh_name);
1594 bswap32s(&shdr->sh_type);
1595 bswaptls(&shdr->sh_flags);
1596 bswaptls(&shdr->sh_addr);
1597 bswaptls(&shdr->sh_offset);
1598 bswaptls(&shdr->sh_size);
1599 bswap32s(&shdr->sh_link);
1600 bswap32s(&shdr->sh_info);
1601 bswaptls(&shdr->sh_addralign);
1602 bswaptls(&shdr->sh_entsize);
1603 }
1604 }
1605
1606 static void bswap_sym(struct elf_sym *sym)
1607 {
1608 bswap32s(&sym->st_name);
1609 bswaptls(&sym->st_value);
1610 bswaptls(&sym->st_size);
1611 bswap16s(&sym->st_shndx);
1612 }
1613
1614 #ifdef TARGET_MIPS
1615 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1616 {
1617 bswap16s(&abiflags->version);
1618 bswap32s(&abiflags->ases);
1619 bswap32s(&abiflags->isa_ext);
1620 bswap32s(&abiflags->flags1);
1621 bswap32s(&abiflags->flags2);
1622 }
1623 #endif
1624 #else
1625 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1626 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1627 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1628 static inline void bswap_sym(struct elf_sym *sym) { }
1629 #ifdef TARGET_MIPS
1630 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1631 #endif
1632 #endif
1633
1634 #ifdef USE_ELF_CORE_DUMP
1635 static int elf_core_dump(int, const CPUArchState *);
1636 #endif /* USE_ELF_CORE_DUMP */
1637 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1638
1639 /* Verify the portions of EHDR within E_IDENT for the target.
1640 This can be performed before bswapping the entire header. */
1641 static bool elf_check_ident(struct elfhdr *ehdr)
1642 {
1643 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1644 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1645 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1646 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1647 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1648 && ehdr->e_ident[EI_DATA] == ELF_DATA
1649 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1650 }
1651
1652 /* Verify the portions of EHDR outside of E_IDENT for the target.
1653 This has to wait until after bswapping the header. */
1654 static bool elf_check_ehdr(struct elfhdr *ehdr)
1655 {
1656 return (elf_check_arch(ehdr->e_machine)
1657 && elf_check_abi(ehdr->e_flags)
1658 && ehdr->e_ehsize == sizeof(struct elfhdr)
1659 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1660 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1661 }
1662
1663 /*
1664 * 'copy_elf_strings()' copies argument/envelope strings from user
1665 * memory to free pages in kernel mem. These are in a format ready
1666 * to be put directly into the top of new user memory.
1667 *
1668 */
1669 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1670 abi_ulong p, abi_ulong stack_limit)
1671 {
1672 char *tmp;
1673 int len, i;
1674 abi_ulong top = p;
1675
1676 if (!p) {
1677 return 0; /* bullet-proofing */
1678 }
1679
1680 if (STACK_GROWS_DOWN) {
1681 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1682 for (i = argc - 1; i >= 0; --i) {
1683 tmp = argv[i];
1684 if (!tmp) {
1685 fprintf(stderr, "VFS: argc is wrong");
1686 exit(-1);
1687 }
1688 len = strlen(tmp) + 1;
1689 tmp += len;
1690
1691 if (len > (p - stack_limit)) {
1692 return 0;
1693 }
1694 while (len) {
1695 int bytes_to_copy = (len > offset) ? offset : len;
1696 tmp -= bytes_to_copy;
1697 p -= bytes_to_copy;
1698 offset -= bytes_to_copy;
1699 len -= bytes_to_copy;
1700
1701 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1702
1703 if (offset == 0) {
1704 memcpy_to_target(p, scratch, top - p);
1705 top = p;
1706 offset = TARGET_PAGE_SIZE;
1707 }
1708 }
1709 }
1710 if (p != top) {
1711 memcpy_to_target(p, scratch + offset, top - p);
1712 }
1713 } else {
1714 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1715 for (i = 0; i < argc; ++i) {
1716 tmp = argv[i];
1717 if (!tmp) {
1718 fprintf(stderr, "VFS: argc is wrong");
1719 exit(-1);
1720 }
1721 len = strlen(tmp) + 1;
1722 if (len > (stack_limit - p)) {
1723 return 0;
1724 }
1725 while (len) {
1726 int bytes_to_copy = (len > remaining) ? remaining : len;
1727
1728 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1729
1730 tmp += bytes_to_copy;
1731 remaining -= bytes_to_copy;
1732 p += bytes_to_copy;
1733 len -= bytes_to_copy;
1734
1735 if (remaining == 0) {
1736 memcpy_to_target(top, scratch, p - top);
1737 top = p;
1738 remaining = TARGET_PAGE_SIZE;
1739 }
1740 }
1741 }
1742 if (p != top) {
1743 memcpy_to_target(top, scratch, p - top);
1744 }
1745 }
1746
1747 return p;
1748 }
1749
1750 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1751 * argument/environment space. Newer kernels (>2.6.33) allow more,
1752 * dependent on stack size, but guarantee at least 32 pages for
1753 * backwards compatibility.
1754 */
1755 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1756
1757 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1758 struct image_info *info)
1759 {
1760 abi_ulong size, error, guard;
1761
1762 size = guest_stack_size;
1763 if (size < STACK_LOWER_LIMIT) {
1764 size = STACK_LOWER_LIMIT;
1765 }
1766 guard = TARGET_PAGE_SIZE;
1767 if (guard < qemu_real_host_page_size) {
1768 guard = qemu_real_host_page_size;
1769 }
1770
1771 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1772 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1773 if (error == -1) {
1774 perror("mmap stack");
1775 exit(-1);
1776 }
1777
1778 /* We reserve one extra page at the top of the stack as guard. */
1779 if (STACK_GROWS_DOWN) {
1780 target_mprotect(error, guard, PROT_NONE);
1781 info->stack_limit = error + guard;
1782 return info->stack_limit + size - sizeof(void *);
1783 } else {
1784 target_mprotect(error + size, guard, PROT_NONE);
1785 info->stack_limit = error + size;
1786 return error;
1787 }
1788 }
1789
1790 /* Map and zero the bss. We need to explicitly zero any fractional pages
1791 after the data section (i.e. bss). */
1792 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1793 {
1794 uintptr_t host_start, host_map_start, host_end;
1795
1796 last_bss = TARGET_PAGE_ALIGN(last_bss);
1797
1798 /* ??? There is confusion between qemu_real_host_page_size and
1799 qemu_host_page_size here and elsewhere in target_mmap, which
1800 may lead to the end of the data section mapping from the file
1801 not being mapped. At least there was an explicit test and
1802 comment for that here, suggesting that "the file size must
1803 be known". The comment probably pre-dates the introduction
1804 of the fstat system call in target_mmap which does in fact
1805 find out the size. What isn't clear is if the workaround
1806 here is still actually needed. For now, continue with it,
1807 but merge it with the "normal" mmap that would allocate the bss. */
1808
1809 host_start = (uintptr_t) g2h(elf_bss);
1810 host_end = (uintptr_t) g2h(last_bss);
1811 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1812
1813 if (host_map_start < host_end) {
1814 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1815 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1816 if (p == MAP_FAILED) {
1817 perror("cannot mmap brk");
1818 exit(-1);
1819 }
1820 }
1821
1822 /* Ensure that the bss page(s) are valid */
1823 if ((page_get_flags(last_bss-1) & prot) != prot) {
1824 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1825 }
1826
1827 if (host_start < host_map_start) {
1828 memset((void *)host_start, 0, host_map_start - host_start);
1829 }
1830 }
1831
1832 #ifdef TARGET_ARM
1833 static int elf_is_fdpic(struct elfhdr *exec)
1834 {
1835 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1836 }
1837 #else
1838 /* Default implementation, always false. */
1839 static int elf_is_fdpic(struct elfhdr *exec)
1840 {
1841 return 0;
1842 }
1843 #endif
1844
1845 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1846 {
1847 uint16_t n;
1848 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1849
1850 /* elf32_fdpic_loadseg */
1851 n = info->nsegs;
1852 while (n--) {
1853 sp -= 12;
1854 put_user_u32(loadsegs[n].addr, sp+0);
1855 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1856 put_user_u32(loadsegs[n].p_memsz, sp+8);
1857 }
1858
1859 /* elf32_fdpic_loadmap */
1860 sp -= 4;
1861 put_user_u16(0, sp+0); /* version */
1862 put_user_u16(info->nsegs, sp+2); /* nsegs */
1863
1864 info->personality = PER_LINUX_FDPIC;
1865 info->loadmap_addr = sp;
1866
1867 return sp;
1868 }
1869
1870 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1871 struct elfhdr *exec,
1872 struct image_info *info,
1873 struct image_info *interp_info)
1874 {
1875 abi_ulong sp;
1876 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1877 int size;
1878 int i;
1879 abi_ulong u_rand_bytes;
1880 uint8_t k_rand_bytes[16];
1881 abi_ulong u_platform;
1882 const char *k_platform;
1883 const int n = sizeof(elf_addr_t);
1884
1885 sp = p;
1886
1887 /* Needs to be before we load the env/argc/... */
1888 if (elf_is_fdpic(exec)) {
1889 /* Need 4 byte alignment for these structs */
1890 sp &= ~3;
1891 sp = loader_build_fdpic_loadmap(info, sp);
1892 info->other_info = interp_info;
1893 if (interp_info) {
1894 interp_info->other_info = info;
1895 sp = loader_build_fdpic_loadmap(interp_info, sp);
1896 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
1897 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
1898 } else {
1899 info->interpreter_loadmap_addr = 0;
1900 info->interpreter_pt_dynamic_addr = 0;
1901 }
1902 }
1903
1904 u_platform = 0;
1905 k_platform = ELF_PLATFORM;
1906 if (k_platform) {
1907 size_t len = strlen(k_platform) + 1;
1908 if (STACK_GROWS_DOWN) {
1909 sp -= (len + n - 1) & ~(n - 1);
1910 u_platform = sp;
1911 /* FIXME - check return value of memcpy_to_target() for failure */
1912 memcpy_to_target(sp, k_platform, len);
1913 } else {
1914 memcpy_to_target(sp, k_platform, len);
1915 u_platform = sp;
1916 sp += len + 1;
1917 }
1918 }
1919
1920 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1921 * the argv and envp pointers.
1922 */
1923 if (STACK_GROWS_DOWN) {
1924 sp = QEMU_ALIGN_DOWN(sp, 16);
1925 } else {
1926 sp = QEMU_ALIGN_UP(sp, 16);
1927 }
1928
1929 /*
1930 * Generate 16 random bytes for userspace PRNG seeding.
1931 */
1932 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
1933 if (STACK_GROWS_DOWN) {
1934 sp -= 16;
1935 u_rand_bytes = sp;
1936 /* FIXME - check return value of memcpy_to_target() for failure */
1937 memcpy_to_target(sp, k_rand_bytes, 16);
1938 } else {
1939 memcpy_to_target(sp, k_rand_bytes, 16);
1940 u_rand_bytes = sp;
1941 sp += 16;
1942 }
1943
1944 size = (DLINFO_ITEMS + 1) * 2;
1945 if (k_platform)
1946 size += 2;
1947 #ifdef DLINFO_ARCH_ITEMS
1948 size += DLINFO_ARCH_ITEMS * 2;
1949 #endif
1950 #ifdef ELF_HWCAP2
1951 size += 2;
1952 #endif
1953 info->auxv_len = size * n;
1954
1955 size += envc + argc + 2;
1956 size += 1; /* argc itself */
1957 size *= n;
1958
1959 /* Allocate space and finalize stack alignment for entry now. */
1960 if (STACK_GROWS_DOWN) {
1961 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1962 sp = u_argc;
1963 } else {
1964 u_argc = sp;
1965 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
1966 }
1967
1968 u_argv = u_argc + n;
1969 u_envp = u_argv + (argc + 1) * n;
1970 u_auxv = u_envp + (envc + 1) * n;
1971 info->saved_auxv = u_auxv;
1972 info->arg_start = u_argv;
1973 info->arg_end = u_argv + argc * n;
1974
1975 /* This is correct because Linux defines
1976 * elf_addr_t as Elf32_Off / Elf64_Off
1977 */
1978 #define NEW_AUX_ENT(id, val) do { \
1979 put_user_ual(id, u_auxv); u_auxv += n; \
1980 put_user_ual(val, u_auxv); u_auxv += n; \
1981 } while(0)
1982
1983 #ifdef ARCH_DLINFO
1984 /*
1985 * ARCH_DLINFO must come first so platform specific code can enforce
1986 * special alignment requirements on the AUXV if necessary (eg. PPC).
1987 */
1988 ARCH_DLINFO;
1989 #endif
1990 /* There must be exactly DLINFO_ITEMS entries here, or the assert
1991 * on info->auxv_len will trigger.
1992 */
1993 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1994 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1995 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1996 if ((info->alignment & ~qemu_host_page_mask) != 0) {
1997 /* Target doesn't support host page size alignment */
1998 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1999 } else {
2000 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2001 qemu_host_page_size)));
2002 }
2003 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2004 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2005 NEW_AUX_ENT(AT_ENTRY, info->entry);
2006 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2007 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2008 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2009 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2010 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2011 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2012 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2013 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2014 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2015
2016 #ifdef ELF_HWCAP2
2017 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2018 #endif
2019
2020 if (u_platform) {
2021 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2022 }
2023 NEW_AUX_ENT (AT_NULL, 0);
2024 #undef NEW_AUX_ENT
2025
2026 /* Check that our initial calculation of the auxv length matches how much
2027 * we actually put into it.
2028 */
2029 assert(info->auxv_len == u_auxv - info->saved_auxv);
2030
2031 put_user_ual(argc, u_argc);
2032
2033 p = info->arg_strings;
2034 for (i = 0; i < argc; ++i) {
2035 put_user_ual(p, u_argv);
2036 u_argv += n;
2037 p += target_strlen(p) + 1;
2038 }
2039 put_user_ual(0, u_argv);
2040
2041 p = info->env_strings;
2042 for (i = 0; i < envc; ++i) {
2043 put_user_ual(p, u_envp);
2044 u_envp += n;
2045 p += target_strlen(p) + 1;
2046 }
2047 put_user_ual(0, u_envp);
2048
2049 return sp;
2050 }
2051
2052 #ifndef ARM_COMMPAGE
2053 #define ARM_COMMPAGE 0
2054 #define init_guest_commpage() true
2055 #endif
2056
2057 static void pgb_fail_in_use(const char *image_name)
2058 {
2059 error_report("%s: requires virtual address space that is in use "
2060 "(omit the -B option or choose a different value)",
2061 image_name);
2062 exit(EXIT_FAILURE);
2063 }
2064
2065 static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
2066 abi_ulong guest_hiaddr, long align)
2067 {
2068 const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2069 void *addr, *test;
2070
2071 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2072 fprintf(stderr, "Requested guest base 0x%lx does not satisfy "
2073 "host minimum alignment (0x%lx)\n",
2074 guest_base, align);
2075 exit(EXIT_FAILURE);
2076 }
2077
2078 /* Sanity check the guest binary. */
2079 if (reserved_va) {
2080 if (guest_hiaddr > reserved_va) {
2081 error_report("%s: requires more than reserved virtual "
2082 "address space (0x%" PRIx64 " > 0x%lx)",
2083 image_name, (uint64_t)guest_hiaddr, reserved_va);
2084 exit(EXIT_FAILURE);
2085 }
2086 } else {
2087 #if HOST_LONG_BITS < TARGET_ABI_BITS
2088 if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
2089 error_report("%s: requires more virtual address space "
2090 "than the host can provide (0x%" PRIx64 ")",
2091 image_name, (uint64_t)guest_hiaddr - guest_base);
2092 exit(EXIT_FAILURE);
2093 }
2094 #endif
2095 }
2096
2097 /*
2098 * Expand the allocation to the entire reserved_va.
2099 * Exclude the mmap_min_addr hole.
2100 */
2101 if (reserved_va) {
2102 guest_loaddr = (guest_base >= mmap_min_addr ? 0
2103 : mmap_min_addr - guest_base);
2104 guest_hiaddr = reserved_va;
2105 }
2106
2107 /* Reserve the address space for the binary, or reserved_va. */
2108 test = g2h(guest_loaddr);
2109 addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
2110 if (test != addr) {
2111 pgb_fail_in_use(image_name);
2112 }
2113 }
2114
2115 /**
2116 * pgd_find_hole_fallback: potential mmap address
2117 * @guest_size: size of available space
2118 * @brk: location of break
2119 * @align: memory alignment
2120 *
2121 * This is a fallback method for finding a hole in the host address
2122 * space if we don't have the benefit of being able to access
2123 * /proc/self/map. It can potentially take a very long time as we can
2124 * only dumbly iterate up the host address space seeing if the
2125 * allocation would work.
2126 */
2127 static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
2128 long align, uintptr_t offset)
2129 {
2130 uintptr_t base;
2131
2132 /* Start (aligned) at the bottom and work our way up */
2133 base = ROUND_UP(mmap_min_addr, align);
2134
2135 while (true) {
2136 uintptr_t align_start, end;
2137 align_start = ROUND_UP(base, align);
2138 end = align_start + guest_size + offset;
2139
2140 /* if brk is anywhere in the range give ourselves some room to grow. */
2141 if (align_start <= brk && brk < end) {
2142 base = brk + (16 * MiB);
2143 continue;
2144 } else if (align_start + guest_size < align_start) {
2145 /* we have run out of space */
2146 return -1;
2147 } else {
2148 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2149 MAP_FIXED_NOREPLACE;
2150 void * mmap_start = mmap((void *) align_start, guest_size,
2151 PROT_NONE, flags, -1, 0);
2152 if (mmap_start != MAP_FAILED) {
2153 munmap((void *) align_start, guest_size);
2154 if (MAP_FIXED_NOREPLACE || mmap_start == (void *) align_start) {
2155 return (uintptr_t) mmap_start + offset;
2156 }
2157 }
2158 base += qemu_host_page_size;
2159 }
2160 }
2161 }
2162
2163 /* Return value for guest_base, or -1 if no hole found. */
2164 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
2165 long align, uintptr_t offset)
2166 {
2167 GSList *maps, *iter;
2168 uintptr_t this_start, this_end, next_start, brk;
2169 intptr_t ret = -1;
2170
2171 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2172
2173 maps = read_self_maps();
2174
2175 /* Read brk after we've read the maps, which will malloc. */
2176 brk = (uintptr_t)sbrk(0);
2177
2178 if (!maps) {
2179 return pgd_find_hole_fallback(guest_size, brk, align, offset);
2180 }
2181
2182 /* The first hole is before the first map entry. */
2183 this_start = mmap_min_addr;
2184
2185 for (iter = maps; iter;
2186 this_start = next_start, iter = g_slist_next(iter)) {
2187 uintptr_t align_start, hole_size;
2188
2189 this_end = ((MapInfo *)iter->data)->start;
2190 next_start = ((MapInfo *)iter->data)->end;
2191 align_start = ROUND_UP(this_start + offset, align);
2192
2193 /* Skip holes that are too small. */
2194 if (align_start >= this_end) {
2195 continue;
2196 }
2197 hole_size = this_end - align_start;
2198 if (hole_size < guest_size) {
2199 continue;
2200 }
2201
2202 /* If this hole contains brk, give ourselves some room to grow. */
2203 if (this_start <= brk && brk < this_end) {
2204 hole_size -= guest_size;
2205 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2206 align_start += 1 * GiB;
2207 } else if (hole_size >= 16 * MiB) {
2208 align_start += 16 * MiB;
2209 } else {
2210 align_start = (this_end - guest_size) & -align;
2211 if (align_start < this_start) {
2212 continue;
2213 }
2214 }
2215 }
2216
2217 /* Record the lowest successful match. */
2218 if (ret < 0) {
2219 ret = align_start - guest_loaddr;
2220 }
2221 /* If this hole contains the identity map, select it. */
2222 if (align_start <= guest_loaddr &&
2223 guest_loaddr + guest_size <= this_end) {
2224 ret = 0;
2225 }
2226 /* If this hole ends above the identity map, stop looking. */
2227 if (this_end >= guest_loaddr) {
2228 break;
2229 }
2230 }
2231 free_self_maps(maps);
2232
2233 return ret;
2234 }
2235
2236 static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2237 abi_ulong orig_hiaddr, long align)
2238 {
2239 uintptr_t loaddr = orig_loaddr;
2240 uintptr_t hiaddr = orig_hiaddr;
2241 uintptr_t offset = 0;
2242 uintptr_t addr;
2243
2244 if (hiaddr != orig_hiaddr) {
2245 error_report("%s: requires virtual address space that the "
2246 "host cannot provide (0x%" PRIx64 ")",
2247 image_name, (uint64_t)orig_hiaddr);
2248 exit(EXIT_FAILURE);
2249 }
2250
2251 loaddr &= -align;
2252 if (ARM_COMMPAGE) {
2253 /*
2254 * Extend the allocation to include the commpage.
2255 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2256 * need to ensure there is space bellow the guest_base so we
2257 * can map the commpage in the place needed when the address
2258 * arithmetic wraps around.
2259 */
2260 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
2261 hiaddr = (uintptr_t) 4 << 30;
2262 } else {
2263 offset = -(ARM_COMMPAGE & -align);
2264 }
2265 }
2266
2267 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
2268 if (addr == -1) {
2269 /*
2270 * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
2271 * that can satisfy both. But as the normal arm32 link base address
2272 * is ~32k, and we extend down to include the commpage, making the
2273 * overhead only ~96k, this is unlikely.
2274 */
2275 error_report("%s: Unable to allocate %#zx bytes of "
2276 "virtual address space", image_name,
2277 (size_t)(hiaddr - loaddr));
2278 exit(EXIT_FAILURE);
2279 }
2280
2281 guest_base = addr;
2282 }
2283
2284 static void pgb_dynamic(const char *image_name, long align)
2285 {
2286 /*
2287 * The executable is dynamic and does not require a fixed address.
2288 * All we need is a commpage that satisfies align.
2289 * If we do not need a commpage, leave guest_base == 0.
2290 */
2291 if (ARM_COMMPAGE) {
2292 uintptr_t addr, commpage;
2293
2294 /* 64-bit hosts should have used reserved_va. */
2295 assert(sizeof(uintptr_t) == 4);
2296
2297 /*
2298 * By putting the commpage at the first hole, that puts guest_base
2299 * just above that, and maximises the positive guest addresses.
2300 */
2301 commpage = ARM_COMMPAGE & -align;
2302 addr = pgb_find_hole(commpage, -commpage, align, 0);
2303 assert(addr != -1);
2304 guest_base = addr;
2305 }
2306 }
2307
2308 static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2309 abi_ulong guest_hiaddr, long align)
2310 {
2311 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2312 void *addr, *test;
2313
2314 if (guest_hiaddr > reserved_va) {
2315 error_report("%s: requires more than reserved virtual "
2316 "address space (0x%" PRIx64 " > 0x%lx)",
2317 image_name, (uint64_t)guest_hiaddr, reserved_va);
2318 exit(EXIT_FAILURE);
2319 }
2320
2321 /* Widen the "image" to the entire reserved address space. */
2322 pgb_static(image_name, 0, reserved_va, align);
2323
2324 /* osdep.h defines this as 0 if it's missing */
2325 flags |= MAP_FIXED_NOREPLACE;
2326
2327 /* Reserve the memory on the host. */
2328 assert(guest_base != 0);
2329 test = g2h(0);
2330 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
2331 if (addr == MAP_FAILED) {
2332 error_report("Unable to reserve 0x%lx bytes of virtual address "
2333 "space (%s) for use as guest address space (check your "
2334 "virtual memory ulimit setting or reserve less "
2335 "using -R option)", reserved_va, strerror(errno));
2336 exit(EXIT_FAILURE);
2337 }
2338 assert(addr == test);
2339 }
2340
2341 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2342 abi_ulong guest_hiaddr)
2343 {
2344 /* In order to use host shmat, we must be able to honor SHMLBA. */
2345 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2346
2347 if (have_guest_base) {
2348 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2349 } else if (reserved_va) {
2350 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2351 } else if (guest_loaddr) {
2352 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2353 } else {
2354 pgb_dynamic(image_name, align);
2355 }
2356
2357 /* Reserve and initialize the commpage. */
2358 if (!init_guest_commpage()) {
2359 /*
2360 * With have_guest_base, the user has selected the address and
2361 * we are trying to work with that. Otherwise, we have selected
2362 * free space and init_guest_commpage must succeeded.
2363 */
2364 assert(have_guest_base);
2365 pgb_fail_in_use(image_name);
2366 }
2367
2368 assert(QEMU_IS_ALIGNED(guest_base, align));
2369 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2370 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2371 }
2372
2373 /* Load an ELF image into the address space.
2374
2375 IMAGE_NAME is the filename of the image, to use in error messages.
2376 IMAGE_FD is the open file descriptor for the image.
2377
2378 BPRM_BUF is a copy of the beginning of the file; this of course
2379 contains the elf file header at offset 0. It is assumed that this
2380 buffer is sufficiently aligned to present no problems to the host
2381 in accessing data at aligned offsets within the buffer.
2382
2383 On return: INFO values will be filled in, as necessary or available. */
2384
2385 static void load_elf_image(const char *image_name, int image_fd,
2386 struct image_info *info, char **pinterp_name,
2387 char bprm_buf[BPRM_BUF_SIZE])
2388 {
2389 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2390 struct elf_phdr *phdr;
2391 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2392 int i, retval;
2393 const char *errmsg;
2394
2395 /* First of all, some simple consistency checks */
2396 errmsg = "Invalid ELF image for this architecture";
2397 if (!elf_check_ident(ehdr)) {
2398 goto exit_errmsg;
2399 }
2400 bswap_ehdr(ehdr);
2401 if (!elf_check_ehdr(ehdr)) {
2402 goto exit_errmsg;
2403 }
2404
2405 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2406 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2407 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2408 } else {
2409 phdr = (struct elf_phdr *) alloca(i);
2410 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2411 if (retval != i) {
2412 goto exit_read;
2413 }
2414 }
2415 bswap_phdr(phdr, ehdr->e_phnum);
2416
2417 info->nsegs = 0;
2418 info->pt_dynamic_addr = 0;
2419
2420 mmap_lock();
2421
2422 /* Find the maximum size of the image and allocate an appropriate
2423 amount of memory to handle that. */
2424 loaddr = -1, hiaddr = 0;
2425 info->alignment = 0;
2426 for (i = 0; i < ehdr->e_phnum; ++i) {
2427 if (phdr[i].p_type == PT_LOAD) {
2428 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
2429 if (a < loaddr) {
2430 loaddr = a;
2431 }
2432 a = phdr[i].p_vaddr + phdr[i].p_memsz;
2433 if (a > hiaddr) {
2434 hiaddr = a;
2435 }
2436 ++info->nsegs;
2437 info->alignment |= phdr[i].p_align;
2438 }
2439 }
2440
2441 if (pinterp_name != NULL) {
2442 /*
2443 * This is the main executable.
2444 *
2445 * Reserve extra space for brk.
2446 * We hold on to this space while placing the interpreter
2447 * and the stack, lest they be placed immediately after
2448 * the data segment and block allocation from the brk.
2449 *
2450 * 16MB is chosen as "large enough" without being so large
2451 * as to allow the result to not fit with a 32-bit guest on
2452 * a 32-bit host.
2453 */
2454 info->reserve_brk = 16 * MiB;
2455 hiaddr += info->reserve_brk;
2456
2457 if (ehdr->e_type == ET_EXEC) {
2458 /*
2459 * Make sure that the low address does not conflict with
2460 * MMAP_MIN_ADDR or the QEMU application itself.
2461 */
2462 probe_guest_base(image_name, loaddr, hiaddr);
2463 } else {
2464 /*
2465 * The binary is dynamic, but we still need to
2466 * select guest_base. In this case we pass a size.
2467 */
2468 probe_guest_base(image_name, 0, hiaddr - loaddr);
2469 }
2470 }
2471
2472 /*
2473 * Reserve address space for all of this.
2474 *
2475 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2476 * exactly the address range that is required.
2477 *
2478 * Otherwise this is ET_DYN, and we are searching for a location
2479 * that can hold the memory space required. If the image is
2480 * pre-linked, LOADDR will be non-zero, and the kernel should
2481 * honor that address if it happens to be free.
2482 *
2483 * In both cases, we will overwrite pages in this range with mappings
2484 * from the executable.
2485 */
2486 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2487 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
2488 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
2489 -1, 0);
2490 if (load_addr == -1) {
2491 goto exit_perror;
2492 }
2493 load_bias = load_addr - loaddr;
2494
2495 if (elf_is_fdpic(ehdr)) {
2496 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2497 g_malloc(sizeof(*loadsegs) * info->nsegs);
2498
2499 for (i = 0; i < ehdr->e_phnum; ++i) {
2500 switch (phdr[i].p_type) {
2501 case PT_DYNAMIC:
2502 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2503 break;
2504 case PT_LOAD:
2505 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2506 loadsegs->p_vaddr = phdr[i].p_vaddr;
2507 loadsegs->p_memsz = phdr[i].p_memsz;
2508 ++loadsegs;
2509 break;
2510 }
2511 }
2512 }
2513
2514 info->load_bias = load_bias;
2515 info->code_offset = load_bias;
2516 info->data_offset = load_bias;
2517 info->load_addr = load_addr;
2518 info->entry = ehdr->e_entry + load_bias;
2519 info->start_code = -1;
2520 info->end_code = 0;
2521 info->start_data = -1;
2522 info->end_data = 0;
2523 info->brk = 0;
2524 info->elf_flags = ehdr->e_flags;
2525
2526 for (i = 0; i < ehdr->e_phnum; i++) {
2527 struct elf_phdr *eppnt = phdr + i;
2528 if (eppnt->p_type == PT_LOAD) {
2529 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2530 int elf_prot = 0;
2531
2532 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
2533 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
2534 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
2535
2536 vaddr = load_bias + eppnt->p_vaddr;
2537 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2538 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2539 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2540
2541 /*
2542 * Some segments may be completely empty without any backing file
2543 * segment, in that case just let zero_bss allocate an empty buffer
2544 * for it.
2545 */
2546 if (eppnt->p_filesz != 0) {
2547 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2548 MAP_PRIVATE | MAP_FIXED,
2549 image_fd, eppnt->p_offset - vaddr_po);
2550
2551 if (error == -1) {
2552 goto exit_perror;
2553 }
2554 }
2555
2556 vaddr_ef = vaddr + eppnt->p_filesz;
2557 vaddr_em = vaddr + eppnt->p_memsz;
2558
2559 /* If the load segment requests extra zeros (e.g. bss), map it. */
2560 if (vaddr_ef < vaddr_em) {
2561 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2562 }
2563
2564 /* Find the full program boundaries. */
2565 if (elf_prot & PROT_EXEC) {
2566 if (vaddr < info->start_code) {
2567 info->start_code = vaddr;
2568 }
2569 if (vaddr_ef > info->end_code) {
2570 info->end_code = vaddr_ef;
2571 }
2572 }
2573 if (elf_prot & PROT_WRITE) {
2574 if (vaddr < info->start_data) {
2575 info->start_data = vaddr;
2576 }
2577 if (vaddr_ef > info->end_data) {
2578 info->end_data = vaddr_ef;
2579 }
2580 if (vaddr_em > info->brk) {
2581 info->brk = vaddr_em;
2582 }
2583 }
2584 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2585 char *interp_name;
2586
2587 if (*pinterp_name) {
2588 errmsg = "Multiple PT_INTERP entries";
2589 goto exit_errmsg;
2590 }
2591 interp_name = malloc(eppnt->p_filesz);
2592 if (!interp_name) {
2593 goto exit_perror;
2594 }
2595
2596 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2597 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2598 eppnt->p_filesz);
2599 } else {
2600 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2601 eppnt->p_offset);
2602 if (retval != eppnt->p_filesz) {
2603 goto exit_perror;
2604 }
2605 }
2606 if (interp_name[eppnt->p_filesz - 1] != 0) {
2607 errmsg = "Invalid PT_INTERP entry";
2608 goto exit_errmsg;
2609 }
2610 *pinterp_name = interp_name;
2611 #ifdef TARGET_MIPS
2612 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2613 Mips_elf_abiflags_v0 abiflags;
2614 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2615 errmsg = "Invalid PT_MIPS_ABIFLAGS entry";
2616 goto exit_errmsg;
2617 }
2618 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2619 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2620 sizeof(Mips_elf_abiflags_v0));
2621 } else {
2622 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2623 eppnt->p_offset);
2624 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2625 goto exit_perror;
2626 }
2627 }
2628 bswap_mips_abiflags(&abiflags);
2629 info->fp_abi = abiflags.fp_abi;
2630 #endif
2631 }
2632 }
2633
2634 if (info->end_data == 0) {
2635 info->start_data = info->end_code;
2636 info->end_data = info->end_code;
2637 info->brk = info->end_code;
2638 }
2639
2640 if (qemu_log_enabled()) {
2641 load_symbols(ehdr, image_fd, load_bias);
2642 }
2643
2644 mmap_unlock();
2645
2646 close(image_fd);
2647 return;
2648
2649 exit_read:
2650 if (retval >= 0) {
2651 errmsg = "Incomplete read of file header";
2652 goto exit_errmsg;
2653 }
2654 exit_perror:
2655 errmsg = strerror(errno);
2656 exit_errmsg:
2657 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2658 exit(-1);
2659 }
2660
2661 static void load_elf_interp(const char *filename, struct image_info *info,
2662 char bprm_buf[BPRM_BUF_SIZE])
2663 {
2664 int fd, retval;
2665
2666 fd = open(path(filename), O_RDONLY);
2667 if (fd < 0) {
2668 goto exit_perror;
2669 }
2670
2671 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2672 if (retval < 0) {
2673 goto exit_perror;
2674 }
2675 if (retval < BPRM_BUF_SIZE) {
2676 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2677 }
2678
2679 load_elf_image(filename, fd, info, NULL, bprm_buf);
2680 return;
2681
2682 exit_perror:
2683 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2684 exit(-1);
2685 }
2686
2687 static int symfind(const void *s0, const void *s1)
2688 {
2689 target_ulong addr = *(target_ulong *)s0;
2690 struct elf_sym *sym = (struct elf_sym *)s1;
2691 int result = 0;
2692 if (addr < sym->st_value) {
2693 result = -1;
2694 } else if (addr >= sym->st_value + sym->st_size) {
2695 result = 1;
2696 }
2697 return result;
2698 }
2699
2700 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2701 {
2702 #if ELF_CLASS == ELFCLASS32
2703 struct elf_sym *syms = s->disas_symtab.elf32;
2704 #else
2705 struct elf_sym *syms = s->disas_symtab.elf64;
2706 #endif
2707
2708 // binary search
2709 struct elf_sym *sym;
2710
2711 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2712 if (sym != NULL) {
2713 return s->disas_strtab + sym->st_name;
2714 }
2715
2716 return "";
2717 }
2718
2719 /* FIXME: This should use elf_ops.h */
2720 static int symcmp(const void *s0, const void *s1)
2721 {
2722 struct elf_sym *sym0 = (struct elf_sym *)s0;
2723 struct elf_sym *sym1 = (struct elf_sym *)s1;
2724 return (sym0->st_value < sym1->st_value)
2725 ? -1
2726 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2727 }
2728
2729 /* Best attempt to load symbols from this ELF object. */
2730 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2731 {
2732 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2733 uint64_t segsz;
2734 struct elf_shdr *shdr;
2735 char *strings = NULL;
2736 struct syminfo *s = NULL;
2737 struct elf_sym *new_syms, *syms = NULL;
2738
2739 shnum = hdr->e_shnum;
2740 i = shnum * sizeof(struct elf_shdr);
2741 shdr = (struct elf_shdr *)alloca(i);
2742 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2743 return;
2744 }
2745
2746 bswap_shdr(shdr, shnum);
2747 for (i = 0; i < shnum; ++i) {
2748 if (shdr[i].sh_type == SHT_SYMTAB) {
2749 sym_idx = i;
2750 str_idx = shdr[i].sh_link;
2751 goto found;
2752 }
2753 }
2754
2755 /* There will be no symbol table if the file was stripped. */
2756 return;
2757
2758 found:
2759 /* Now know where the strtab and symtab are. Snarf them. */
2760 s = g_try_new(struct syminfo, 1);
2761 if (!s) {
2762 goto give_up;
2763 }
2764
2765 segsz = shdr[str_idx].sh_size;
2766 s->disas_strtab = strings = g_try_malloc(segsz);
2767 if (!strings ||
2768 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2769 goto give_up;
2770 }
2771
2772 segsz = shdr[sym_idx].sh_size;
2773 syms = g_try_malloc(segsz);
2774 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2775 goto give_up;
2776 }
2777
2778 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2779 /* Implausibly large symbol table: give up rather than ploughing
2780 * on with the number of symbols calculation overflowing
2781 */
2782 goto give_up;
2783 }
2784 nsyms = segsz / sizeof(struct elf_sym);
2785 for (i = 0; i < nsyms; ) {
2786 bswap_sym(syms + i);
2787 /* Throw away entries which we do not need. */
2788 if (syms[i].st_shndx == SHN_UNDEF
2789 || syms[i].st_shndx >= SHN_LORESERVE
2790 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2791 if (i < --nsyms) {
2792 syms[i] = syms[nsyms];
2793 }
2794 } else {
2795 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2796 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2797 syms[i].st_value &= ~(target_ulong)1;
2798 #endif
2799 syms[i].st_value += load_bias;
2800 i++;
2801 }
2802 }
2803
2804 /* No "useful" symbol. */
2805 if (nsyms == 0) {
2806 goto give_up;
2807 }
2808
2809 /* Attempt to free the storage associated with the local symbols
2810 that we threw away. Whether or not this has any effect on the
2811 memory allocation depends on the malloc implementation and how
2812 many symbols we managed to discard. */
2813 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
2814 if (new_syms == NULL) {
2815 goto give_up;
2816 }
2817 syms = new_syms;
2818
2819 qsort(syms, nsyms, sizeof(*syms), symcmp);
2820
2821 s->disas_num_syms = nsyms;
2822 #if ELF_CLASS == ELFCLASS32
2823 s->disas_symtab.elf32 = syms;
2824 #else
2825 s->disas_symtab.elf64 = syms;
2826 #endif
2827 s->lookup_symbol = lookup_symbolxx;
2828 s->next = syminfos;
2829 syminfos = s;
2830
2831 return;
2832
2833 give_up:
2834 g_free(s);
2835 g_free(strings);
2836 g_free(syms);
2837 }
2838
2839 uint32_t get_elf_eflags(int fd)
2840 {
2841 struct elfhdr ehdr;
2842 off_t offset;
2843 int ret;
2844
2845 /* Read ELF header */
2846 offset = lseek(fd, 0, SEEK_SET);
2847 if (offset == (off_t) -1) {
2848 return 0;
2849 }
2850 ret = read(fd, &ehdr, sizeof(ehdr));
2851 if (ret < sizeof(ehdr)) {
2852 return 0;
2853 }
2854 offset = lseek(fd, offset, SEEK_SET);
2855 if (offset == (off_t) -1) {
2856 return 0;
2857 }
2858
2859 /* Check ELF signature */
2860 if (!elf_check_ident(&ehdr)) {
2861 return 0;
2862 }
2863
2864 /* check header */
2865 bswap_ehdr(&ehdr);
2866 if (!elf_check_ehdr(&ehdr)) {
2867 return 0;
2868 }
2869
2870 /* return architecture id */
2871 return ehdr.e_flags;
2872 }
2873
2874 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2875 {
2876 struct image_info interp_info;
2877 struct elfhdr elf_ex;
2878 char *elf_interpreter = NULL;
2879 char *scratch;
2880
2881 memset(&interp_info, 0, sizeof(interp_info));
2882 #ifdef TARGET_MIPS
2883 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
2884 #endif
2885
2886 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2887
2888 load_elf_image(bprm->filename, bprm->fd, info,
2889 &elf_interpreter, bprm->buf);
2890
2891 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2892 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2893 when we load the interpreter. */
2894 elf_ex = *(struct elfhdr *)bprm->buf;
2895
2896 /* Do this so that we can load the interpreter, if need be. We will
2897 change some of these later */
2898 bprm->p = setup_arg_pages(bprm, info);
2899
2900 scratch = g_new0(char, TARGET_PAGE_SIZE);
2901 if (STACK_GROWS_DOWN) {
2902 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2903 bprm->p, info->stack_limit);
2904 info->file_string = bprm->p;
2905 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2906 bprm->p, info->stack_limit);
2907 info->env_strings = bprm->p;
2908 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2909 bprm->p, info->stack_limit);
2910 info->arg_strings = bprm->p;
2911 } else {
2912 info->arg_strings = bprm->p;
2913 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2914 bprm->p, info->stack_limit);
2915 info->env_strings = bprm->p;
2916 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2917 bprm->p, info->stack_limit);
2918 info->file_string = bprm->p;
2919 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2920 bprm->p, info->stack_limit);
2921 }
2922
2923 g_free(scratch);
2924
2925 if (!bprm->p) {
2926 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2927 exit(-1);
2928 }
2929
2930 if (elf_interpreter) {
2931 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2932
2933 /* If the program interpreter is one of these two, then assume
2934 an iBCS2 image. Otherwise assume a native linux image. */
2935
2936 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2937 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2938 info->personality = PER_SVR4;
2939
2940 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2941 and some applications "depend" upon this behavior. Since
2942 we do not have the power to recompile these, we emulate
2943 the SVr4 behavior. Sigh. */
2944 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2945 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2946 }
2947 #ifdef TARGET_MIPS
2948 info->interp_fp_abi = interp_info.fp_abi;
2949 #endif
2950 }
2951
2952 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2953 info, (elf_interpreter ? &interp_info : NULL));
2954 info->start_stack = bprm->p;
2955
2956 /* If we have an interpreter, set that as the program's entry point.
2957 Copy the load_bias as well, to help PPC64 interpret the entry
2958 point as a function descriptor. Do this after creating elf tables
2959 so that we copy the original program entry point into the AUXV. */
2960 if (elf_interpreter) {
2961 info->load_bias = interp_info.load_bias;
2962 info->entry = interp_info.entry;
2963 free(elf_interpreter);
2964 }
2965
2966 #ifdef USE_ELF_CORE_DUMP
2967 bprm->core_dump = &elf_core_dump;
2968 #endif
2969
2970 /*
2971 * If we reserved extra space for brk, release it now.
2972 * The implementation of do_brk in syscalls.c expects to be able
2973 * to mmap pages in this space.
2974 */
2975 if (info->reserve_brk) {
2976 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
2977 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
2978 target_munmap(start_brk, end_brk - start_brk);
2979 }
2980
2981 return 0;
2982 }
2983
2984 #ifdef USE_ELF_CORE_DUMP
2985 /*
2986 * Definitions to generate Intel SVR4-like core files.
2987 * These mostly have the same names as the SVR4 types with "target_elf_"
2988 * tacked on the front to prevent clashes with linux definitions,
2989 * and the typedef forms have been avoided. This is mostly like
2990 * the SVR4 structure, but more Linuxy, with things that Linux does
2991 * not support and which gdb doesn't really use excluded.
2992 *
2993 * Fields we don't dump (their contents is zero) in linux-user qemu
2994 * are marked with XXX.
2995 *
2996 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2997 *
2998 * Porting ELF coredump for target is (quite) simple process. First you
2999 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3000 * the target resides):
3001 *
3002 * #define USE_ELF_CORE_DUMP
3003 *
3004 * Next you define type of register set used for dumping. ELF specification
3005 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3006 *
3007 * typedef <target_regtype> target_elf_greg_t;
3008 * #define ELF_NREG <number of registers>
3009 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3010 *
3011 * Last step is to implement target specific function that copies registers
3012 * from given cpu into just specified register set. Prototype is:
3013 *
3014 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3015 * const CPUArchState *env);
3016 *
3017 * Parameters:
3018 * regs - copy register values into here (allocated and zeroed by caller)
3019 * env - copy registers from here
3020 *
3021 * Example for ARM target is provided in this file.
3022 */
3023
3024 /* An ELF note in memory */
3025 struct memelfnote {
3026 const char *name;
3027 size_t namesz;
3028 size_t namesz_rounded;
3029 int type;
3030 size_t datasz;
3031 size_t datasz_rounded;
3032 void *data;
3033 size_t notesz;
3034 };
3035
3036 struct target_elf_siginfo {
3037 abi_int si_signo; /* signal number */
3038 abi_int si_code; /* extra code */
3039 abi_int si_errno; /* errno */
3040 };
3041
3042 struct target_elf_prstatus {
3043 struct target_elf_siginfo pr_info; /* Info associated with signal */
3044 abi_short pr_cursig; /* Current signal */
3045 abi_ulong pr_sigpend; /* XXX */
3046 abi_ulong pr_sighold; /* XXX */
3047 target_pid_t pr_pid;
3048 target_pid_t pr_ppid;
3049 target_pid_t pr_pgrp;
3050 target_pid_t pr_sid;
3051 struct target_timeval pr_utime; /* XXX User time */
3052 struct target_timeval pr_stime; /* XXX System time */
3053 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3054 struct target_timeval pr_cstime; /* XXX Cumulative system time */
3055 target_elf_gregset_t pr_reg; /* GP registers */
3056 abi_int pr_fpvalid; /* XXX */
3057 };
3058
3059 #define ELF_PRARGSZ (80) /* Number of chars for args */
3060
3061 struct target_elf_prpsinfo {
3062 char pr_state; /* numeric process state */
3063 char pr_sname; /* char for pr_state */
3064 char pr_zomb; /* zombie */
3065 char pr_nice; /* nice val */
3066 abi_ulong pr_flag; /* flags */
3067 target_uid_t pr_uid;
3068 target_gid_t pr_gid;
3069 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
3070 /* Lots missing */
3071 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
3072 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3073 };
3074
3075 /* Here is the structure in which status of each thread is captured. */
3076 struct elf_thread_status {
3077 QTAILQ_ENTRY(elf_thread_status) ets_link;
3078 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
3079 #if 0
3080 elf_fpregset_t fpu; /* NT_PRFPREG */
3081 struct task_struct *thread;
3082 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3083 #endif
3084 struct memelfnote notes[1];
3085 int num_notes;
3086 };
3087
3088 struct elf_note_info {
3089 struct memelfnote *notes;
3090 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3091 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
3092
3093 QTAILQ_HEAD(, elf_thread_status) thread_list;
3094 #if 0
3095 /*
3096 * Current version of ELF coredump doesn't support
3097 * dumping fp regs etc.
3098 */
3099 elf_fpregset_t *fpu;
3100 elf_fpxregset_t *xfpu;
3101 int thread_status_size;
3102 #endif
3103 int notes_size;
3104 int numnote;
3105 };
3106
3107 struct vm_area_struct {
3108 target_ulong vma_start; /* start vaddr of memory region */
3109 target_ulong vma_end; /* end vaddr of memory region */
3110 abi_ulong vma_flags; /* protection etc. flags for the region */
3111 QTAILQ_ENTRY(vm_area_struct) vma_link;
3112 };
3113
3114 struct mm_struct {
3115 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
3116 int mm_count; /* number of mappings */
3117 };
3118
3119 static struct mm_struct *vma_init(void);
3120 static void vma_delete(struct mm_struct *);
3121 static int vma_add_mapping(struct mm_struct *, target_ulong,
3122 target_ulong, abi_ulong);
3123 static int vma_get_mapping_count(const struct mm_struct *);
3124 static struct vm_area_struct *vma_first(const struct mm_struct *);
3125 static struct vm_area_struct *vma_next(struct vm_area_struct *);
3126 static abi_ulong vma_dump_size(const struct vm_area_struct *);
3127 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3128 unsigned long flags);
3129
3130 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3131 static void fill_note(struct memelfnote *, const char *, int,
3132 unsigned int, void *);
3133 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3134 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
3135 static void fill_auxv_note(struct memelfnote *, const TaskState *);
3136 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3137 static size_t note_size(const struct memelfnote *);
3138 static void free_note_info(struct elf_note_info *);
3139 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3140 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
3141 static int core_dump_filename(const TaskState *, char *, size_t);
3142
3143 static int dump_write(int, const void *, size_t);
3144 static int write_note(struct memelfnote *, int);
3145 static int write_note_info(struct elf_note_info *, int);
3146
3147 #ifdef BSWAP_NEEDED
3148 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3149 {
3150 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3151 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3152 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3153 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3154 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3155 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3156 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3157 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3158 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3159 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3160 /* cpu times are not filled, so we skip them */
3161 /* regs should be in correct format already */
3162 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3163 }
3164
3165 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3166 {
3167 psinfo->pr_flag = tswapal(psinfo->pr_flag);
3168 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3169 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3170 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3171 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3172 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3173 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3174 }
3175
3176 static void bswap_note(struct elf_note *en)
3177 {
3178 bswap32s(&en->n_namesz);
3179 bswap32s(&en->n_descsz);
3180 bswap32s(&en->n_type);
3181 }
3182 #else
3183 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3184 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3185 static inline void bswap_note(struct elf_note *en) { }
3186 #endif /* BSWAP_NEEDED */
3187
3188 /*
3189 * Minimal support for linux memory regions. These are needed
3190 * when we are finding out what memory exactly belongs to
3191 * emulated process. No locks needed here, as long as
3192 * thread that received the signal is stopped.
3193 */
3194
3195 static struct mm_struct *vma_init(void)
3196 {
3197 struct mm_struct *mm;
3198
3199 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3200 return (NULL);
3201
3202 mm->mm_count = 0;
3203 QTAILQ_INIT(&mm->mm_mmap);
3204
3205 return (mm);
3206 }
3207
3208 static void vma_delete(struct mm_struct *mm)
3209 {
3210 struct vm_area_struct *vma;
3211
3212 while ((vma = vma_first(mm)) != NULL) {
3213 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3214 g_free(vma);
3215 }
3216 g_free(mm);
3217 }
3218
3219 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3220 target_ulong end, abi_ulong flags)
3221 {
3222 struct vm_area_struct *vma;
3223
3224 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3225 return (-1);
3226
3227 vma->vma_start = start;
3228 vma->vma_end = end;
3229 vma->vma_flags = flags;
3230
3231 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3232 mm->mm_count++;
3233
3234 return (0);
3235 }
3236
3237 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3238 {
3239 return (QTAILQ_FIRST(&mm->mm_mmap));
3240 }
3241
3242 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3243 {
3244 return (QTAILQ_NEXT(vma, vma_link));
3245 }
3246
3247 static int vma_get_mapping_count(const struct mm_struct *mm)
3248 {
3249 return (mm->mm_count);
3250 }
3251
3252 /*
3253 * Calculate file (dump) size of given memory region.
3254 */
3255 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3256 {
3257 /* if we cannot even read the first page, skip it */
3258 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3259 return (0);
3260
3261 /*
3262 * Usually we don't dump executable pages as they contain
3263 * non-writable code that debugger can read directly from
3264 * target library etc. However, thread stacks are marked
3265 * also executable so we read in first page of given region
3266 * and check whether it contains elf header. If there is
3267 * no elf header, we dump it.
3268 */
3269 if (vma->vma_flags & PROT_EXEC) {
3270 char page[TARGET_PAGE_SIZE];
3271
3272 copy_from_user(page, vma->vma_start, sizeof (page));
3273 if ((page[EI_MAG0] == ELFMAG0) &&
3274 (page[EI_MAG1] == ELFMAG1) &&
3275 (page[EI_MAG2] == ELFMAG2) &&
3276 (page[EI_MAG3] == ELFMAG3)) {
3277 /*
3278 * Mappings are possibly from ELF binary. Don't dump
3279 * them.
3280 */
3281 return (0);
3282 }
3283 }
3284
3285 return (vma->vma_end - vma->vma_start);
3286 }
3287
3288 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3289 unsigned long flags)
3290 {
3291 struct mm_struct *mm = (struct mm_struct *)priv;
3292
3293 vma_add_mapping(mm, start, end, flags);
3294 return (0);
3295 }
3296
3297 static void fill_note(struct memelfnote *note, const char *name, int type,
3298 unsigned int sz, void *data)
3299 {
3300 unsigned int namesz;
3301
3302 namesz = strlen(name) + 1;
3303 note->name = name;
3304 note->namesz = namesz;
3305 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3306 note->type = type;
3307 note->datasz = sz;
3308 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3309
3310 note->data = data;
3311
3312 /*
3313 * We calculate rounded up note size here as specified by
3314 * ELF document.
3315 */
3316 note->notesz = sizeof (struct elf_note) +
3317 note->namesz_rounded + note->datasz_rounded;
3318 }
3319
3320 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3321 uint32_t flags)
3322 {
3323 (void) memset(elf, 0