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