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/prctl.h> 6 #include <sys/resource.h> 7 #include <sys/shm.h> 8 9 #include "qemu.h" 10 #include "user/tswap-target.h" 11 #include "user/page-protection.h" 12 #include "exec/page-protection.h" 13 #include "exec/mmap-lock.h" 14 #include "exec/translation-block.h" 15 #include "user/guest-base.h" 16 #include "user-internals.h" 17 #include "signal-common.h" 18 #include "loader.h" 19 #include "user-mmap.h" 20 #include "disas/disas.h" 21 #include "qemu/bitops.h" 22 #include "qemu/path.h" 23 #include "qemu/queue.h" 24 #include "qemu/guest-random.h" 25 #include "qemu/units.h" 26 #include "qemu/selfmap.h" 27 #include "qemu/lockable.h" 28 #include "qapi/error.h" 29 #include "qemu/error-report.h" 30 #include "target_signal.h" 31 #include "tcg/debuginfo.h" 32 33 #ifdef TARGET_ARM 34 #include "target/arm/cpu-features.h" 35 #endif 36 37 #ifdef _ARCH_PPC64 38 #undef ARCH_DLINFO 39 #undef ELF_PLATFORM 40 #undef ELF_HWCAP 41 #undef ELF_HWCAP2 42 #undef ELF_CLASS 43 #undef ELF_DATA 44 #undef ELF_ARCH 45 #endif 46 47 #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE 48 #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0 49 #endif 50 51 typedef struct { 52 const uint8_t *image; 53 const uint32_t *relocs; 54 unsigned image_size; 55 unsigned reloc_count; 56 unsigned sigreturn_ofs; 57 unsigned rt_sigreturn_ofs; 58 } VdsoImageInfo; 59 60 #define ELF_OSABI ELFOSABI_SYSV 61 62 /* from personality.h */ 63 64 /* 65 * Flags for bug emulation. 66 * 67 * These occupy the top three bytes. 68 */ 69 enum { 70 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */ 71 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to 72 descriptors (signal handling) */ 73 MMAP_PAGE_ZERO = 0x0100000, 74 ADDR_COMPAT_LAYOUT = 0x0200000, 75 READ_IMPLIES_EXEC = 0x0400000, 76 ADDR_LIMIT_32BIT = 0x0800000, 77 SHORT_INODE = 0x1000000, 78 WHOLE_SECONDS = 0x2000000, 79 STICKY_TIMEOUTS = 0x4000000, 80 ADDR_LIMIT_3GB = 0x8000000, 81 }; 82 83 /* 84 * Personality types. 85 * 86 * These go in the low byte. Avoid using the top bit, it will 87 * conflict with error returns. 88 */ 89 enum { 90 PER_LINUX = 0x0000, 91 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT, 92 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS, 93 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, 94 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE, 95 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE, 96 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS, 97 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE, 98 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS, 99 PER_BSD = 0x0006, 100 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS, 101 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE, 102 PER_LINUX32 = 0x0008, 103 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB, 104 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */ 105 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */ 106 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */ 107 PER_RISCOS = 0x000c, 108 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS, 109 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, 110 PER_OSF4 = 0x000f, /* OSF/1 v4 */ 111 PER_HPUX = 0x0010, 112 PER_MASK = 0x00ff, 113 }; 114 115 /* 116 * Return the base personality without flags. 117 */ 118 #define personality(pers) (pers & PER_MASK) 119 120 int info_is_fdpic(struct image_info *info) 121 { 122 return info->personality == PER_LINUX_FDPIC; 123 } 124 125 /* this flag is uneffective under linux too, should be deleted */ 126 #ifndef MAP_DENYWRITE 127 #define MAP_DENYWRITE 0 128 #endif 129 130 /* should probably go in elf.h */ 131 #ifndef ELIBBAD 132 #define ELIBBAD 80 133 #endif 134 135 #if TARGET_BIG_ENDIAN 136 #define ELF_DATA ELFDATA2MSB 137 #else 138 #define ELF_DATA ELFDATA2LSB 139 #endif 140 141 #ifdef TARGET_ABI_MIPSN32 142 typedef abi_ullong target_elf_greg_t; 143 #define tswapreg(ptr) tswap64(ptr) 144 #else 145 typedef abi_ulong target_elf_greg_t; 146 #define tswapreg(ptr) tswapal(ptr) 147 #endif 148 149 #ifdef USE_UID16 150 typedef abi_ushort target_uid_t; 151 typedef abi_ushort target_gid_t; 152 #else 153 typedef abi_uint target_uid_t; 154 typedef abi_uint target_gid_t; 155 #endif 156 typedef abi_int target_pid_t; 157 158 #ifdef TARGET_I386 159 160 #define ELF_HWCAP get_elf_hwcap() 161 162 static uint32_t get_elf_hwcap(void) 163 { 164 X86CPU *cpu = X86_CPU(thread_cpu); 165 166 return cpu->env.features[FEAT_1_EDX]; 167 } 168 169 #ifdef TARGET_X86_64 170 #define ELF_CLASS ELFCLASS64 171 #define ELF_ARCH EM_X86_64 172 173 #define ELF_PLATFORM "x86_64" 174 175 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) 176 { 177 regs->rax = 0; 178 regs->rsp = infop->start_stack; 179 regs->rip = infop->entry; 180 } 181 182 #define ELF_NREG 27 183 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 184 185 /* 186 * Note that ELF_NREG should be 29 as there should be place for 187 * TRAPNO and ERR "registers" as well but linux doesn't dump 188 * those. 189 * 190 * See linux kernel: arch/x86/include/asm/elf.h 191 */ 192 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) 193 { 194 (*regs)[0] = tswapreg(env->regs[15]); 195 (*regs)[1] = tswapreg(env->regs[14]); 196 (*regs)[2] = tswapreg(env->regs[13]); 197 (*regs)[3] = tswapreg(env->regs[12]); 198 (*regs)[4] = tswapreg(env->regs[R_EBP]); 199 (*regs)[5] = tswapreg(env->regs[R_EBX]); 200 (*regs)[6] = tswapreg(env->regs[11]); 201 (*regs)[7] = tswapreg(env->regs[10]); 202 (*regs)[8] = tswapreg(env->regs[9]); 203 (*regs)[9] = tswapreg(env->regs[8]); 204 (*regs)[10] = tswapreg(env->regs[R_EAX]); 205 (*regs)[11] = tswapreg(env->regs[R_ECX]); 206 (*regs)[12] = tswapreg(env->regs[R_EDX]); 207 (*regs)[13] = tswapreg(env->regs[R_ESI]); 208 (*regs)[14] = tswapreg(env->regs[R_EDI]); 209 (*regs)[15] = tswapreg(get_task_state(env_cpu_const(env))->orig_ax); 210 (*regs)[16] = tswapreg(env->eip); 211 (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff); 212 (*regs)[18] = tswapreg(env->eflags); 213 (*regs)[19] = tswapreg(env->regs[R_ESP]); 214 (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff); 215 (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff); 216 (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff); 217 (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff); 218 (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff); 219 (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff); 220 (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff); 221 } 222 223 #if ULONG_MAX > UINT32_MAX 224 #define INIT_GUEST_COMMPAGE 225 static bool init_guest_commpage(void) 226 { 227 /* 228 * The vsyscall page is at a high negative address aka kernel space, 229 * which means that we cannot actually allocate it with target_mmap. 230 * We still should be able to use page_set_flags, unless the user 231 * has specified -R reserved_va, which would trigger an assert(). 232 */ 233 if (reserved_va != 0 && 234 TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE - 1 > reserved_va) { 235 error_report("Cannot allocate vsyscall page"); 236 exit(EXIT_FAILURE); 237 } 238 page_set_flags(TARGET_VSYSCALL_PAGE, 239 TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK, 240 PAGE_EXEC | PAGE_VALID); 241 return true; 242 } 243 #endif 244 #else 245 246 /* 247 * This is used to ensure we don't load something for the wrong architecture. 248 */ 249 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) 250 251 /* 252 * These are used to set parameters in the core dumps. 253 */ 254 #define ELF_CLASS ELFCLASS32 255 #define ELF_ARCH EM_386 256 257 #define ELF_PLATFORM get_elf_platform() 258 #define EXSTACK_DEFAULT true 259 260 static const char *get_elf_platform(void) 261 { 262 static char elf_platform[] = "i386"; 263 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL); 264 if (family > 6) { 265 family = 6; 266 } 267 if (family >= 3) { 268 elf_platform[1] = '0' + family; 269 } 270 return elf_platform; 271 } 272 273 static inline void init_thread(struct target_pt_regs *regs, 274 struct image_info *infop) 275 { 276 regs->esp = infop->start_stack; 277 regs->eip = infop->entry; 278 279 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program 280 starts %edx contains a pointer to a function which might be 281 registered using `atexit'. This provides a mean for the 282 dynamic linker to call DT_FINI functions for shared libraries 283 that have been loaded before the code runs. 284 285 A value of 0 tells we have no such handler. */ 286 regs->edx = 0; 287 } 288 289 #define ELF_NREG 17 290 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 291 292 /* 293 * Note that ELF_NREG should be 19 as there should be place for 294 * TRAPNO and ERR "registers" as well but linux doesn't dump 295 * those. 296 * 297 * See linux kernel: arch/x86/include/asm/elf.h 298 */ 299 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) 300 { 301 (*regs)[0] = tswapreg(env->regs[R_EBX]); 302 (*regs)[1] = tswapreg(env->regs[R_ECX]); 303 (*regs)[2] = tswapreg(env->regs[R_EDX]); 304 (*regs)[3] = tswapreg(env->regs[R_ESI]); 305 (*regs)[4] = tswapreg(env->regs[R_EDI]); 306 (*regs)[5] = tswapreg(env->regs[R_EBP]); 307 (*regs)[6] = tswapreg(env->regs[R_EAX]); 308 (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff); 309 (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff); 310 (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff); 311 (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff); 312 (*regs)[11] = tswapreg(get_task_state(env_cpu_const(env))->orig_ax); 313 (*regs)[12] = tswapreg(env->eip); 314 (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff); 315 (*regs)[14] = tswapreg(env->eflags); 316 (*regs)[15] = tswapreg(env->regs[R_ESP]); 317 (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff); 318 } 319 320 /* 321 * i386 is the only target which supplies AT_SYSINFO for the vdso. 322 * All others only supply AT_SYSINFO_EHDR. 323 */ 324 #define DLINFO_ARCH_ITEMS (vdso_info != NULL) 325 #define ARCH_DLINFO \ 326 do { \ 327 if (vdso_info) { \ 328 NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry); \ 329 } \ 330 } while (0) 331 332 #endif /* TARGET_X86_64 */ 333 334 #define VDSO_HEADER "vdso.c.inc" 335 336 #define USE_ELF_CORE_DUMP 337 #define ELF_EXEC_PAGESIZE 4096 338 339 #endif /* TARGET_I386 */ 340 341 #ifdef TARGET_ARM 342 343 #ifndef TARGET_AARCH64 344 /* 32 bit ARM definitions */ 345 346 #define ELF_ARCH EM_ARM 347 #define ELF_CLASS ELFCLASS32 348 #define EXSTACK_DEFAULT true 349 350 static inline void init_thread(struct target_pt_regs *regs, 351 struct image_info *infop) 352 { 353 abi_long stack = infop->start_stack; 354 memset(regs, 0, sizeof(*regs)); 355 356 regs->uregs[16] = ARM_CPU_MODE_USR; 357 if (infop->entry & 1) { 358 regs->uregs[16] |= CPSR_T; 359 } 360 regs->uregs[15] = infop->entry & 0xfffffffe; 361 regs->uregs[13] = infop->start_stack; 362 /* FIXME - what to for failure of get_user()? */ 363 get_user_ual(regs->uregs[2], stack + 8); /* envp */ 364 get_user_ual(regs->uregs[1], stack + 4); /* envp */ 365 /* XXX: it seems that r0 is zeroed after ! */ 366 regs->uregs[0] = 0; 367 /* For uClinux PIC binaries. */ 368 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */ 369 regs->uregs[10] = infop->start_data; 370 371 /* Support ARM FDPIC. */ 372 if (info_is_fdpic(infop)) { 373 /* As described in the ABI document, r7 points to the loadmap info 374 * prepared by the kernel. If an interpreter is needed, r8 points 375 * to the interpreter loadmap and r9 points to the interpreter 376 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and 377 * r9 points to the main program PT_DYNAMIC info. 378 */ 379 regs->uregs[7] = infop->loadmap_addr; 380 if (infop->interpreter_loadmap_addr) { 381 /* Executable is dynamically loaded. */ 382 regs->uregs[8] = infop->interpreter_loadmap_addr; 383 regs->uregs[9] = infop->interpreter_pt_dynamic_addr; 384 } else { 385 regs->uregs[8] = 0; 386 regs->uregs[9] = infop->pt_dynamic_addr; 387 } 388 } 389 } 390 391 #define ELF_NREG 18 392 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 393 394 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env) 395 { 396 (*regs)[0] = tswapreg(env->regs[0]); 397 (*regs)[1] = tswapreg(env->regs[1]); 398 (*regs)[2] = tswapreg(env->regs[2]); 399 (*regs)[3] = tswapreg(env->regs[3]); 400 (*regs)[4] = tswapreg(env->regs[4]); 401 (*regs)[5] = tswapreg(env->regs[5]); 402 (*regs)[6] = tswapreg(env->regs[6]); 403 (*regs)[7] = tswapreg(env->regs[7]); 404 (*regs)[8] = tswapreg(env->regs[8]); 405 (*regs)[9] = tswapreg(env->regs[9]); 406 (*regs)[10] = tswapreg(env->regs[10]); 407 (*regs)[11] = tswapreg(env->regs[11]); 408 (*regs)[12] = tswapreg(env->regs[12]); 409 (*regs)[13] = tswapreg(env->regs[13]); 410 (*regs)[14] = tswapreg(env->regs[14]); 411 (*regs)[15] = tswapreg(env->regs[15]); 412 413 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env)); 414 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */ 415 } 416 417 #define USE_ELF_CORE_DUMP 418 #define ELF_EXEC_PAGESIZE 4096 419 420 enum 421 { 422 ARM_HWCAP_ARM_SWP = 1 << 0, 423 ARM_HWCAP_ARM_HALF = 1 << 1, 424 ARM_HWCAP_ARM_THUMB = 1 << 2, 425 ARM_HWCAP_ARM_26BIT = 1 << 3, 426 ARM_HWCAP_ARM_FAST_MULT = 1 << 4, 427 ARM_HWCAP_ARM_FPA = 1 << 5, 428 ARM_HWCAP_ARM_VFP = 1 << 6, 429 ARM_HWCAP_ARM_EDSP = 1 << 7, 430 ARM_HWCAP_ARM_JAVA = 1 << 8, 431 ARM_HWCAP_ARM_IWMMXT = 1 << 9, 432 ARM_HWCAP_ARM_CRUNCH = 1 << 10, 433 ARM_HWCAP_ARM_THUMBEE = 1 << 11, 434 ARM_HWCAP_ARM_NEON = 1 << 12, 435 ARM_HWCAP_ARM_VFPv3 = 1 << 13, 436 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14, 437 ARM_HWCAP_ARM_TLS = 1 << 15, 438 ARM_HWCAP_ARM_VFPv4 = 1 << 16, 439 ARM_HWCAP_ARM_IDIVA = 1 << 17, 440 ARM_HWCAP_ARM_IDIVT = 1 << 18, 441 ARM_HWCAP_ARM_VFPD32 = 1 << 19, 442 ARM_HWCAP_ARM_LPAE = 1 << 20, 443 ARM_HWCAP_ARM_EVTSTRM = 1 << 21, 444 ARM_HWCAP_ARM_FPHP = 1 << 22, 445 ARM_HWCAP_ARM_ASIMDHP = 1 << 23, 446 ARM_HWCAP_ARM_ASIMDDP = 1 << 24, 447 ARM_HWCAP_ARM_ASIMDFHM = 1 << 25, 448 ARM_HWCAP_ARM_ASIMDBF16 = 1 << 26, 449 ARM_HWCAP_ARM_I8MM = 1 << 27, 450 }; 451 452 enum { 453 ARM_HWCAP2_ARM_AES = 1 << 0, 454 ARM_HWCAP2_ARM_PMULL = 1 << 1, 455 ARM_HWCAP2_ARM_SHA1 = 1 << 2, 456 ARM_HWCAP2_ARM_SHA2 = 1 << 3, 457 ARM_HWCAP2_ARM_CRC32 = 1 << 4, 458 ARM_HWCAP2_ARM_SB = 1 << 5, 459 ARM_HWCAP2_ARM_SSBS = 1 << 6, 460 }; 461 462 /* The commpage only exists for 32 bit kernels */ 463 464 #define HI_COMMPAGE (intptr_t)0xffff0f00u 465 466 static bool init_guest_commpage(void) 467 { 468 ARMCPU *cpu = ARM_CPU(thread_cpu); 469 int host_page_size = qemu_real_host_page_size(); 470 abi_ptr commpage; 471 void *want; 472 void *addr; 473 474 /* 475 * M-profile allocates maximum of 2GB address space, so can never 476 * allocate the commpage. Skip it. 477 */ 478 if (arm_feature(&cpu->env, ARM_FEATURE_M)) { 479 return true; 480 } 481 482 commpage = HI_COMMPAGE & -host_page_size; 483 want = g2h_untagged(commpage); 484 addr = mmap(want, host_page_size, PROT_READ | PROT_WRITE, 485 MAP_ANONYMOUS | MAP_PRIVATE | 486 (commpage < reserved_va ? MAP_FIXED : MAP_FIXED_NOREPLACE), 487 -1, 0); 488 489 if (addr == MAP_FAILED) { 490 perror("Allocating guest commpage"); 491 exit(EXIT_FAILURE); 492 } 493 if (addr != want) { 494 return false; 495 } 496 497 /* Set kernel helper versions; rest of page is 0. */ 498 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu)); 499 500 if (mprotect(addr, host_page_size, PROT_READ)) { 501 perror("Protecting guest commpage"); 502 exit(EXIT_FAILURE); 503 } 504 505 page_set_flags(commpage, commpage | (host_page_size - 1), 506 PAGE_READ | PAGE_EXEC | PAGE_VALID); 507 return true; 508 } 509 510 #define ELF_HWCAP get_elf_hwcap() 511 #define ELF_HWCAP2 get_elf_hwcap2() 512 513 uint32_t get_elf_hwcap(void) 514 { 515 ARMCPU *cpu = ARM_CPU(thread_cpu); 516 uint32_t hwcaps = 0; 517 518 hwcaps |= ARM_HWCAP_ARM_SWP; 519 hwcaps |= ARM_HWCAP_ARM_HALF; 520 hwcaps |= ARM_HWCAP_ARM_THUMB; 521 hwcaps |= ARM_HWCAP_ARM_FAST_MULT; 522 523 /* probe for the extra features */ 524 #define GET_FEATURE(feat, hwcap) \ 525 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0) 526 527 #define GET_FEATURE_ID(feat, hwcap) \ 528 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0) 529 530 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */ 531 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP); 532 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT); 533 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE); 534 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON); 535 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS); 536 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE); 537 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA); 538 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT); 539 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP); 540 541 if (cpu_isar_feature(aa32_fpsp_v3, cpu) || 542 cpu_isar_feature(aa32_fpdp_v3, cpu)) { 543 hwcaps |= ARM_HWCAP_ARM_VFPv3; 544 if (cpu_isar_feature(aa32_simd_r32, cpu)) { 545 hwcaps |= ARM_HWCAP_ARM_VFPD32; 546 } else { 547 hwcaps |= ARM_HWCAP_ARM_VFPv3D16; 548 } 549 } 550 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4); 551 /* 552 * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same 553 * isar_feature function for both. The kernel reports them as two hwcaps. 554 */ 555 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_FPHP); 556 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_ASIMDHP); 557 GET_FEATURE_ID(aa32_dp, ARM_HWCAP_ARM_ASIMDDP); 558 GET_FEATURE_ID(aa32_fhm, ARM_HWCAP_ARM_ASIMDFHM); 559 GET_FEATURE_ID(aa32_bf16, ARM_HWCAP_ARM_ASIMDBF16); 560 GET_FEATURE_ID(aa32_i8mm, ARM_HWCAP_ARM_I8MM); 561 562 return hwcaps; 563 } 564 565 uint64_t get_elf_hwcap2(void) 566 { 567 ARMCPU *cpu = ARM_CPU(thread_cpu); 568 uint64_t hwcaps = 0; 569 570 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES); 571 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL); 572 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1); 573 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2); 574 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32); 575 GET_FEATURE_ID(aa32_sb, ARM_HWCAP2_ARM_SB); 576 GET_FEATURE_ID(aa32_ssbs, ARM_HWCAP2_ARM_SSBS); 577 return hwcaps; 578 } 579 580 const char *elf_hwcap_str(uint32_t bit) 581 { 582 static const char *hwcap_str[] = { 583 [__builtin_ctz(ARM_HWCAP_ARM_SWP )] = "swp", 584 [__builtin_ctz(ARM_HWCAP_ARM_HALF )] = "half", 585 [__builtin_ctz(ARM_HWCAP_ARM_THUMB )] = "thumb", 586 [__builtin_ctz(ARM_HWCAP_ARM_26BIT )] = "26bit", 587 [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT)] = "fast_mult", 588 [__builtin_ctz(ARM_HWCAP_ARM_FPA )] = "fpa", 589 [__builtin_ctz(ARM_HWCAP_ARM_VFP )] = "vfp", 590 [__builtin_ctz(ARM_HWCAP_ARM_EDSP )] = "edsp", 591 [__builtin_ctz(ARM_HWCAP_ARM_JAVA )] = "java", 592 [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT )] = "iwmmxt", 593 [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH )] = "crunch", 594 [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE )] = "thumbee", 595 [__builtin_ctz(ARM_HWCAP_ARM_NEON )] = "neon", 596 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3 )] = "vfpv3", 597 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16 )] = "vfpv3d16", 598 [__builtin_ctz(ARM_HWCAP_ARM_TLS )] = "tls", 599 [__builtin_ctz(ARM_HWCAP_ARM_VFPv4 )] = "vfpv4", 600 [__builtin_ctz(ARM_HWCAP_ARM_IDIVA )] = "idiva", 601 [__builtin_ctz(ARM_HWCAP_ARM_IDIVT )] = "idivt", 602 [__builtin_ctz(ARM_HWCAP_ARM_VFPD32 )] = "vfpd32", 603 [__builtin_ctz(ARM_HWCAP_ARM_LPAE )] = "lpae", 604 [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM )] = "evtstrm", 605 [__builtin_ctz(ARM_HWCAP_ARM_FPHP )] = "fphp", 606 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP )] = "asimdhp", 607 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP )] = "asimddp", 608 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM )] = "asimdfhm", 609 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16)] = "asimdbf16", 610 [__builtin_ctz(ARM_HWCAP_ARM_I8MM )] = "i8mm", 611 }; 612 613 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 614 } 615 616 const char *elf_hwcap2_str(uint32_t bit) 617 { 618 static const char *hwcap_str[] = { 619 [__builtin_ctz(ARM_HWCAP2_ARM_AES )] = "aes", 620 [__builtin_ctz(ARM_HWCAP2_ARM_PMULL)] = "pmull", 621 [__builtin_ctz(ARM_HWCAP2_ARM_SHA1 )] = "sha1", 622 [__builtin_ctz(ARM_HWCAP2_ARM_SHA2 )] = "sha2", 623 [__builtin_ctz(ARM_HWCAP2_ARM_CRC32)] = "crc32", 624 [__builtin_ctz(ARM_HWCAP2_ARM_SB )] = "sb", 625 [__builtin_ctz(ARM_HWCAP2_ARM_SSBS )] = "ssbs", 626 }; 627 628 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 629 } 630 631 #undef GET_FEATURE 632 #undef GET_FEATURE_ID 633 634 #define ELF_PLATFORM get_elf_platform() 635 636 static const char *get_elf_platform(void) 637 { 638 CPUARMState *env = cpu_env(thread_cpu); 639 640 #if TARGET_BIG_ENDIAN 641 # define END "b" 642 #else 643 # define END "l" 644 #endif 645 646 if (arm_feature(env, ARM_FEATURE_V8)) { 647 return "v8" END; 648 } else if (arm_feature(env, ARM_FEATURE_V7)) { 649 if (arm_feature(env, ARM_FEATURE_M)) { 650 return "v7m" END; 651 } else { 652 return "v7" END; 653 } 654 } else if (arm_feature(env, ARM_FEATURE_V6)) { 655 return "v6" END; 656 } else if (arm_feature(env, ARM_FEATURE_V5)) { 657 return "v5" END; 658 } else { 659 return "v4" END; 660 } 661 662 #undef END 663 } 664 665 #if TARGET_BIG_ENDIAN 666 #include "elf.h" 667 #include "vdso-be8.c.inc" 668 #include "vdso-be32.c.inc" 669 670 static const VdsoImageInfo *vdso_image_info(uint32_t elf_flags) 671 { 672 return (EF_ARM_EABI_VERSION(elf_flags) >= EF_ARM_EABI_VER4 673 && (elf_flags & EF_ARM_BE8) 674 ? &vdso_be8_image_info 675 : &vdso_be32_image_info); 676 } 677 #define vdso_image_info vdso_image_info 678 #else 679 # define VDSO_HEADER "vdso-le.c.inc" 680 #endif 681 682 #else 683 /* 64 bit ARM definitions */ 684 685 #define ELF_ARCH EM_AARCH64 686 #define ELF_CLASS ELFCLASS64 687 #if TARGET_BIG_ENDIAN 688 # define ELF_PLATFORM "aarch64_be" 689 #else 690 # define ELF_PLATFORM "aarch64" 691 #endif 692 693 static inline void init_thread(struct target_pt_regs *regs, 694 struct image_info *infop) 695 { 696 abi_long stack = infop->start_stack; 697 memset(regs, 0, sizeof(*regs)); 698 699 regs->pc = infop->entry & ~0x3ULL; 700 regs->sp = stack; 701 } 702 703 #define ELF_NREG 34 704 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 705 706 static void elf_core_copy_regs(target_elf_gregset_t *regs, 707 const CPUARMState *env) 708 { 709 int i; 710 711 for (i = 0; i < 32; i++) { 712 (*regs)[i] = tswapreg(env->xregs[i]); 713 } 714 (*regs)[32] = tswapreg(env->pc); 715 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env)); 716 } 717 718 #define USE_ELF_CORE_DUMP 719 #define ELF_EXEC_PAGESIZE 4096 720 721 enum { 722 ARM_HWCAP_A64_FP = 1 << 0, 723 ARM_HWCAP_A64_ASIMD = 1 << 1, 724 ARM_HWCAP_A64_EVTSTRM = 1 << 2, 725 ARM_HWCAP_A64_AES = 1 << 3, 726 ARM_HWCAP_A64_PMULL = 1 << 4, 727 ARM_HWCAP_A64_SHA1 = 1 << 5, 728 ARM_HWCAP_A64_SHA2 = 1 << 6, 729 ARM_HWCAP_A64_CRC32 = 1 << 7, 730 ARM_HWCAP_A64_ATOMICS = 1 << 8, 731 ARM_HWCAP_A64_FPHP = 1 << 9, 732 ARM_HWCAP_A64_ASIMDHP = 1 << 10, 733 ARM_HWCAP_A64_CPUID = 1 << 11, 734 ARM_HWCAP_A64_ASIMDRDM = 1 << 12, 735 ARM_HWCAP_A64_JSCVT = 1 << 13, 736 ARM_HWCAP_A64_FCMA = 1 << 14, 737 ARM_HWCAP_A64_LRCPC = 1 << 15, 738 ARM_HWCAP_A64_DCPOP = 1 << 16, 739 ARM_HWCAP_A64_SHA3 = 1 << 17, 740 ARM_HWCAP_A64_SM3 = 1 << 18, 741 ARM_HWCAP_A64_SM4 = 1 << 19, 742 ARM_HWCAP_A64_ASIMDDP = 1 << 20, 743 ARM_HWCAP_A64_SHA512 = 1 << 21, 744 ARM_HWCAP_A64_SVE = 1 << 22, 745 ARM_HWCAP_A64_ASIMDFHM = 1 << 23, 746 ARM_HWCAP_A64_DIT = 1 << 24, 747 ARM_HWCAP_A64_USCAT = 1 << 25, 748 ARM_HWCAP_A64_ILRCPC = 1 << 26, 749 ARM_HWCAP_A64_FLAGM = 1 << 27, 750 ARM_HWCAP_A64_SSBS = 1 << 28, 751 ARM_HWCAP_A64_SB = 1 << 29, 752 ARM_HWCAP_A64_PACA = 1 << 30, 753 ARM_HWCAP_A64_PACG = 1UL << 31, 754 755 ARM_HWCAP2_A64_DCPODP = 1 << 0, 756 ARM_HWCAP2_A64_SVE2 = 1 << 1, 757 ARM_HWCAP2_A64_SVEAES = 1 << 2, 758 ARM_HWCAP2_A64_SVEPMULL = 1 << 3, 759 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4, 760 ARM_HWCAP2_A64_SVESHA3 = 1 << 5, 761 ARM_HWCAP2_A64_SVESM4 = 1 << 6, 762 ARM_HWCAP2_A64_FLAGM2 = 1 << 7, 763 ARM_HWCAP2_A64_FRINT = 1 << 8, 764 ARM_HWCAP2_A64_SVEI8MM = 1 << 9, 765 ARM_HWCAP2_A64_SVEF32MM = 1 << 10, 766 ARM_HWCAP2_A64_SVEF64MM = 1 << 11, 767 ARM_HWCAP2_A64_SVEBF16 = 1 << 12, 768 ARM_HWCAP2_A64_I8MM = 1 << 13, 769 ARM_HWCAP2_A64_BF16 = 1 << 14, 770 ARM_HWCAP2_A64_DGH = 1 << 15, 771 ARM_HWCAP2_A64_RNG = 1 << 16, 772 ARM_HWCAP2_A64_BTI = 1 << 17, 773 ARM_HWCAP2_A64_MTE = 1 << 18, 774 ARM_HWCAP2_A64_ECV = 1 << 19, 775 ARM_HWCAP2_A64_AFP = 1 << 20, 776 ARM_HWCAP2_A64_RPRES = 1 << 21, 777 ARM_HWCAP2_A64_MTE3 = 1 << 22, 778 ARM_HWCAP2_A64_SME = 1 << 23, 779 ARM_HWCAP2_A64_SME_I16I64 = 1 << 24, 780 ARM_HWCAP2_A64_SME_F64F64 = 1 << 25, 781 ARM_HWCAP2_A64_SME_I8I32 = 1 << 26, 782 ARM_HWCAP2_A64_SME_F16F32 = 1 << 27, 783 ARM_HWCAP2_A64_SME_B16F32 = 1 << 28, 784 ARM_HWCAP2_A64_SME_F32F32 = 1 << 29, 785 ARM_HWCAP2_A64_SME_FA64 = 1 << 30, 786 ARM_HWCAP2_A64_WFXT = 1ULL << 31, 787 ARM_HWCAP2_A64_EBF16 = 1ULL << 32, 788 ARM_HWCAP2_A64_SVE_EBF16 = 1ULL << 33, 789 ARM_HWCAP2_A64_CSSC = 1ULL << 34, 790 ARM_HWCAP2_A64_RPRFM = 1ULL << 35, 791 ARM_HWCAP2_A64_SVE2P1 = 1ULL << 36, 792 ARM_HWCAP2_A64_SME2 = 1ULL << 37, 793 ARM_HWCAP2_A64_SME2P1 = 1ULL << 38, 794 ARM_HWCAP2_A64_SME_I16I32 = 1ULL << 39, 795 ARM_HWCAP2_A64_SME_BI32I32 = 1ULL << 40, 796 ARM_HWCAP2_A64_SME_B16B16 = 1ULL << 41, 797 ARM_HWCAP2_A64_SME_F16F16 = 1ULL << 42, 798 ARM_HWCAP2_A64_MOPS = 1ULL << 43, 799 ARM_HWCAP2_A64_HBC = 1ULL << 44, 800 }; 801 802 #define ELF_HWCAP get_elf_hwcap() 803 #define ELF_HWCAP2 get_elf_hwcap2() 804 805 #define GET_FEATURE_ID(feat, hwcap) \ 806 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0) 807 808 uint32_t get_elf_hwcap(void) 809 { 810 ARMCPU *cpu = ARM_CPU(thread_cpu); 811 uint32_t hwcaps = 0; 812 813 hwcaps |= ARM_HWCAP_A64_FP; 814 hwcaps |= ARM_HWCAP_A64_ASIMD; 815 hwcaps |= ARM_HWCAP_A64_CPUID; 816 817 /* probe for the extra features */ 818 819 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES); 820 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL); 821 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1); 822 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2); 823 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512); 824 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32); 825 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3); 826 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3); 827 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4); 828 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP); 829 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS); 830 GET_FEATURE_ID(aa64_lse2, ARM_HWCAP_A64_USCAT); 831 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM); 832 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP); 833 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA); 834 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE); 835 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG); 836 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM); 837 GET_FEATURE_ID(aa64_dit, ARM_HWCAP_A64_DIT); 838 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT); 839 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB); 840 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM); 841 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP); 842 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC); 843 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC); 844 845 return hwcaps; 846 } 847 848 uint64_t get_elf_hwcap2(void) 849 { 850 ARMCPU *cpu = ARM_CPU(thread_cpu); 851 uint64_t hwcaps = 0; 852 853 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP); 854 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2); 855 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES); 856 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL); 857 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM); 858 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3); 859 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4); 860 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2); 861 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT); 862 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM); 863 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM); 864 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM); 865 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16); 866 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM); 867 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16); 868 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG); 869 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI); 870 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE); 871 GET_FEATURE_ID(aa64_mte3, ARM_HWCAP2_A64_MTE3); 872 GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME | 873 ARM_HWCAP2_A64_SME_F32F32 | 874 ARM_HWCAP2_A64_SME_B16F32 | 875 ARM_HWCAP2_A64_SME_F16F32 | 876 ARM_HWCAP2_A64_SME_I8I32)); 877 GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64); 878 GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64); 879 GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64); 880 GET_FEATURE_ID(aa64_hbc, ARM_HWCAP2_A64_HBC); 881 GET_FEATURE_ID(aa64_mops, ARM_HWCAP2_A64_MOPS); 882 883 return hwcaps; 884 } 885 886 const char *elf_hwcap_str(uint32_t bit) 887 { 888 static const char *hwcap_str[] = { 889 [__builtin_ctz(ARM_HWCAP_A64_FP )] = "fp", 890 [__builtin_ctz(ARM_HWCAP_A64_ASIMD )] = "asimd", 891 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM )] = "evtstrm", 892 [__builtin_ctz(ARM_HWCAP_A64_AES )] = "aes", 893 [__builtin_ctz(ARM_HWCAP_A64_PMULL )] = "pmull", 894 [__builtin_ctz(ARM_HWCAP_A64_SHA1 )] = "sha1", 895 [__builtin_ctz(ARM_HWCAP_A64_SHA2 )] = "sha2", 896 [__builtin_ctz(ARM_HWCAP_A64_CRC32 )] = "crc32", 897 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS )] = "atomics", 898 [__builtin_ctz(ARM_HWCAP_A64_FPHP )] = "fphp", 899 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP )] = "asimdhp", 900 [__builtin_ctz(ARM_HWCAP_A64_CPUID )] = "cpuid", 901 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM)] = "asimdrdm", 902 [__builtin_ctz(ARM_HWCAP_A64_JSCVT )] = "jscvt", 903 [__builtin_ctz(ARM_HWCAP_A64_FCMA )] = "fcma", 904 [__builtin_ctz(ARM_HWCAP_A64_LRCPC )] = "lrcpc", 905 [__builtin_ctz(ARM_HWCAP_A64_DCPOP )] = "dcpop", 906 [__builtin_ctz(ARM_HWCAP_A64_SHA3 )] = "sha3", 907 [__builtin_ctz(ARM_HWCAP_A64_SM3 )] = "sm3", 908 [__builtin_ctz(ARM_HWCAP_A64_SM4 )] = "sm4", 909 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP )] = "asimddp", 910 [__builtin_ctz(ARM_HWCAP_A64_SHA512 )] = "sha512", 911 [__builtin_ctz(ARM_HWCAP_A64_SVE )] = "sve", 912 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM)] = "asimdfhm", 913 [__builtin_ctz(ARM_HWCAP_A64_DIT )] = "dit", 914 [__builtin_ctz(ARM_HWCAP_A64_USCAT )] = "uscat", 915 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC )] = "ilrcpc", 916 [__builtin_ctz(ARM_HWCAP_A64_FLAGM )] = "flagm", 917 [__builtin_ctz(ARM_HWCAP_A64_SSBS )] = "ssbs", 918 [__builtin_ctz(ARM_HWCAP_A64_SB )] = "sb", 919 [__builtin_ctz(ARM_HWCAP_A64_PACA )] = "paca", 920 [__builtin_ctz(ARM_HWCAP_A64_PACG )] = "pacg", 921 }; 922 923 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 924 } 925 926 const char *elf_hwcap2_str(uint32_t bit) 927 { 928 static const char *hwcap_str[] = { 929 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP )] = "dcpodp", 930 [__builtin_ctz(ARM_HWCAP2_A64_SVE2 )] = "sve2", 931 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES )] = "sveaes", 932 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL )] = "svepmull", 933 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM )] = "svebitperm", 934 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3 )] = "svesha3", 935 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4 )] = "svesm4", 936 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2 )] = "flagm2", 937 [__builtin_ctz(ARM_HWCAP2_A64_FRINT )] = "frint", 938 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM )] = "svei8mm", 939 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM )] = "svef32mm", 940 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM )] = "svef64mm", 941 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16 )] = "svebf16", 942 [__builtin_ctz(ARM_HWCAP2_A64_I8MM )] = "i8mm", 943 [__builtin_ctz(ARM_HWCAP2_A64_BF16 )] = "bf16", 944 [__builtin_ctz(ARM_HWCAP2_A64_DGH )] = "dgh", 945 [__builtin_ctz(ARM_HWCAP2_A64_RNG )] = "rng", 946 [__builtin_ctz(ARM_HWCAP2_A64_BTI )] = "bti", 947 [__builtin_ctz(ARM_HWCAP2_A64_MTE )] = "mte", 948 [__builtin_ctz(ARM_HWCAP2_A64_ECV )] = "ecv", 949 [__builtin_ctz(ARM_HWCAP2_A64_AFP )] = "afp", 950 [__builtin_ctz(ARM_HWCAP2_A64_RPRES )] = "rpres", 951 [__builtin_ctz(ARM_HWCAP2_A64_MTE3 )] = "mte3", 952 [__builtin_ctz(ARM_HWCAP2_A64_SME )] = "sme", 953 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64 )] = "smei16i64", 954 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64 )] = "smef64f64", 955 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32 )] = "smei8i32", 956 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32 )] = "smef16f32", 957 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32 )] = "smeb16f32", 958 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32 )] = "smef32f32", 959 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64 )] = "smefa64", 960 [__builtin_ctz(ARM_HWCAP2_A64_WFXT )] = "wfxt", 961 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16 )] = "ebf16", 962 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16 )] = "sveebf16", 963 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC )] = "cssc", 964 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM )] = "rprfm", 965 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1 )] = "sve2p1", 966 [__builtin_ctzll(ARM_HWCAP2_A64_SME2 )] = "sme2", 967 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1 )] = "sme2p1", 968 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32 )] = "smei16i32", 969 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32)] = "smebi32i32", 970 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16 )] = "smeb16b16", 971 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16 )] = "smef16f16", 972 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS )] = "mops", 973 [__builtin_ctzll(ARM_HWCAP2_A64_HBC )] = "hbc", 974 }; 975 976 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 977 } 978 979 #undef GET_FEATURE_ID 980 981 #if TARGET_BIG_ENDIAN 982 # define VDSO_HEADER "vdso-be.c.inc" 983 #else 984 # define VDSO_HEADER "vdso-le.c.inc" 985 #endif 986 987 #endif /* not TARGET_AARCH64 */ 988 989 #endif /* TARGET_ARM */ 990 991 #ifdef TARGET_SPARC 992 993 #ifndef TARGET_SPARC64 994 # define ELF_CLASS ELFCLASS32 995 # define ELF_ARCH EM_SPARC 996 #elif defined(TARGET_ABI32) 997 # define ELF_CLASS ELFCLASS32 998 # define elf_check_arch(x) ((x) == EM_SPARC32PLUS || (x) == EM_SPARC) 999 #else 1000 # define ELF_CLASS ELFCLASS64 1001 # define ELF_ARCH EM_SPARCV9 1002 #endif 1003 1004 #include "elf.h" 1005 1006 #define ELF_HWCAP get_elf_hwcap() 1007 1008 static uint32_t get_elf_hwcap(void) 1009 { 1010 /* There are not many sparc32 hwcap bits -- we have all of them. */ 1011 uint32_t r = HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | 1012 HWCAP_SPARC_SWAP | HWCAP_SPARC_MULDIV; 1013 1014 #ifdef TARGET_SPARC64 1015 CPUSPARCState *env = cpu_env(thread_cpu); 1016 uint32_t features = env->def.features; 1017 1018 r |= HWCAP_SPARC_V9 | HWCAP_SPARC_V8PLUS; 1019 /* 32x32 multiply and divide are efficient. */ 1020 r |= HWCAP_SPARC_MUL32 | HWCAP_SPARC_DIV32; 1021 /* We don't have an internal feature bit for this. */ 1022 r |= HWCAP_SPARC_POPC; 1023 r |= features & CPU_FEATURE_FSMULD ? HWCAP_SPARC_FSMULD : 0; 1024 r |= features & CPU_FEATURE_VIS1 ? HWCAP_SPARC_VIS : 0; 1025 r |= features & CPU_FEATURE_VIS2 ? HWCAP_SPARC_VIS2 : 0; 1026 r |= features & CPU_FEATURE_FMAF ? HWCAP_SPARC_FMAF : 0; 1027 r |= features & CPU_FEATURE_VIS3 ? HWCAP_SPARC_VIS3 : 0; 1028 r |= features & CPU_FEATURE_IMA ? HWCAP_SPARC_IMA : 0; 1029 #endif 1030 1031 return r; 1032 } 1033 1034 static inline void init_thread(struct target_pt_regs *regs, 1035 struct image_info *infop) 1036 { 1037 /* Note that target_cpu_copy_regs does not read psr/tstate. */ 1038 regs->pc = infop->entry; 1039 regs->npc = regs->pc + 4; 1040 regs->y = 0; 1041 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong) 1042 - TARGET_STACK_BIAS); 1043 } 1044 #endif /* TARGET_SPARC */ 1045 1046 #ifdef TARGET_PPC 1047 1048 #define ELF_MACHINE PPC_ELF_MACHINE 1049 1050 #if defined(TARGET_PPC64) 1051 1052 #define elf_check_arch(x) ( (x) == EM_PPC64 ) 1053 1054 #define ELF_CLASS ELFCLASS64 1055 1056 #else 1057 1058 #define ELF_CLASS ELFCLASS32 1059 #define EXSTACK_DEFAULT true 1060 1061 #endif 1062 1063 #define ELF_ARCH EM_PPC 1064 1065 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP). 1066 See arch/powerpc/include/asm/cputable.h. */ 1067 enum { 1068 QEMU_PPC_FEATURE_32 = 0x80000000, 1069 QEMU_PPC_FEATURE_64 = 0x40000000, 1070 QEMU_PPC_FEATURE_601_INSTR = 0x20000000, 1071 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000, 1072 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000, 1073 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000, 1074 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000, 1075 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000, 1076 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000, 1077 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000, 1078 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000, 1079 QEMU_PPC_FEATURE_NO_TB = 0x00100000, 1080 QEMU_PPC_FEATURE_POWER4 = 0x00080000, 1081 QEMU_PPC_FEATURE_POWER5 = 0x00040000, 1082 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000, 1083 QEMU_PPC_FEATURE_CELL = 0x00010000, 1084 QEMU_PPC_FEATURE_BOOKE = 0x00008000, 1085 QEMU_PPC_FEATURE_SMT = 0x00004000, 1086 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000, 1087 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000, 1088 QEMU_PPC_FEATURE_PA6T = 0x00000800, 1089 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400, 1090 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200, 1091 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100, 1092 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080, 1093 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040, 1094 1095 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002, 1096 QEMU_PPC_FEATURE_PPC_LE = 0x00000001, 1097 1098 /* Feature definitions in AT_HWCAP2. */ 1099 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */ 1100 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */ 1101 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */ 1102 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */ 1103 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */ 1104 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */ 1105 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000, 1106 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000, 1107 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */ 1108 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */ 1109 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */ 1110 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */ 1111 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */ 1112 QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */ 1113 QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */ 1114 }; 1115 1116 #define ELF_HWCAP get_elf_hwcap() 1117 1118 static uint32_t get_elf_hwcap(void) 1119 { 1120 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); 1121 uint32_t features = 0; 1122 1123 /* We don't have to be terribly complete here; the high points are 1124 Altivec/FP/SPE support. Anything else is just a bonus. */ 1125 #define GET_FEATURE(flag, feature) \ 1126 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0) 1127 #define GET_FEATURE2(flags, feature) \ 1128 do { \ 1129 if ((cpu->env.insns_flags2 & flags) == flags) { \ 1130 features |= feature; \ 1131 } \ 1132 } while (0) 1133 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64); 1134 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU); 1135 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC); 1136 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE); 1137 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE); 1138 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE); 1139 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE); 1140 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC); 1141 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP); 1142 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX); 1143 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 | 1144 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206), 1145 QEMU_PPC_FEATURE_ARCH_2_06); 1146 #undef GET_FEATURE 1147 #undef GET_FEATURE2 1148 1149 return features; 1150 } 1151 1152 #define ELF_HWCAP2 get_elf_hwcap2() 1153 1154 static uint32_t get_elf_hwcap2(void) 1155 { 1156 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); 1157 uint32_t features = 0; 1158 1159 #define GET_FEATURE(flag, feature) \ 1160 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0) 1161 #define GET_FEATURE2(flag, feature) \ 1162 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0) 1163 1164 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL); 1165 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR); 1166 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 | 1167 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 | 1168 QEMU_PPC_FEATURE2_VEC_CRYPTO); 1169 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 | 1170 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128); 1171 GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 | 1172 QEMU_PPC_FEATURE2_MMA); 1173 1174 #undef GET_FEATURE 1175 #undef GET_FEATURE2 1176 1177 return features; 1178 } 1179 1180 /* 1181 * The requirements here are: 1182 * - keep the final alignment of sp (sp & 0xf) 1183 * - make sure the 32-bit value at the first 16 byte aligned position of 1184 * AUXV is greater than 16 for glibc compatibility. 1185 * AT_IGNOREPPC is used for that. 1186 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC, 1187 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined. 1188 */ 1189 #define DLINFO_ARCH_ITEMS 5 1190 #define ARCH_DLINFO \ 1191 do { \ 1192 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \ 1193 /* \ 1194 * Handle glibc compatibility: these magic entries must \ 1195 * be at the lowest addresses in the final auxv. \ 1196 */ \ 1197 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ 1198 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ 1199 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \ 1200 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \ 1201 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \ 1202 } while (0) 1203 1204 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop) 1205 { 1206 _regs->gpr[1] = infop->start_stack; 1207 #if defined(TARGET_PPC64) 1208 if (get_ppc64_abi(infop) < 2) { 1209 uint64_t val; 1210 get_user_u64(val, infop->entry + 8); 1211 _regs->gpr[2] = val + infop->load_bias; 1212 get_user_u64(val, infop->entry); 1213 infop->entry = val + infop->load_bias; 1214 } else { 1215 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */ 1216 } 1217 #endif 1218 _regs->nip = infop->entry; 1219 } 1220 1221 /* See linux kernel: arch/powerpc/include/asm/elf.h. */ 1222 #define ELF_NREG 48 1223 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1224 1225 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env) 1226 { 1227 int i; 1228 target_ulong ccr = 0; 1229 1230 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { 1231 (*regs)[i] = tswapreg(env->gpr[i]); 1232 } 1233 1234 (*regs)[32] = tswapreg(env->nip); 1235 (*regs)[33] = tswapreg(env->msr); 1236 (*regs)[35] = tswapreg(env->ctr); 1237 (*regs)[36] = tswapreg(env->lr); 1238 (*regs)[37] = tswapreg(cpu_read_xer(env)); 1239 1240 ccr = ppc_get_cr(env); 1241 (*regs)[38] = tswapreg(ccr); 1242 } 1243 1244 #define USE_ELF_CORE_DUMP 1245 #define ELF_EXEC_PAGESIZE 4096 1246 1247 #ifndef TARGET_PPC64 1248 # define VDSO_HEADER "vdso-32.c.inc" 1249 #elif TARGET_BIG_ENDIAN 1250 # define VDSO_HEADER "vdso-64.c.inc" 1251 #else 1252 # define VDSO_HEADER "vdso-64le.c.inc" 1253 #endif 1254 1255 #endif 1256 1257 #ifdef TARGET_LOONGARCH64 1258 1259 #define ELF_CLASS ELFCLASS64 1260 #define ELF_ARCH EM_LOONGARCH 1261 #define EXSTACK_DEFAULT true 1262 1263 #define elf_check_arch(x) ((x) == EM_LOONGARCH) 1264 1265 #define VDSO_HEADER "vdso.c.inc" 1266 1267 static inline void init_thread(struct target_pt_regs *regs, 1268 struct image_info *infop) 1269 { 1270 /*Set crmd PG,DA = 1,0 */ 1271 regs->csr.crmd = 2 << 3; 1272 regs->csr.era = infop->entry; 1273 regs->regs[3] = infop->start_stack; 1274 } 1275 1276 /* See linux kernel: arch/loongarch/include/asm/elf.h */ 1277 #define ELF_NREG 45 1278 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1279 1280 enum { 1281 TARGET_EF_R0 = 0, 1282 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33, 1283 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34, 1284 }; 1285 1286 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1287 const CPULoongArchState *env) 1288 { 1289 int i; 1290 1291 (*regs)[TARGET_EF_R0] = 0; 1292 1293 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) { 1294 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]); 1295 } 1296 1297 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc); 1298 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV); 1299 } 1300 1301 #define USE_ELF_CORE_DUMP 1302 #define ELF_EXEC_PAGESIZE 4096 1303 1304 #define ELF_HWCAP get_elf_hwcap() 1305 1306 /* See arch/loongarch/include/uapi/asm/hwcap.h */ 1307 enum { 1308 HWCAP_LOONGARCH_CPUCFG = (1 << 0), 1309 HWCAP_LOONGARCH_LAM = (1 << 1), 1310 HWCAP_LOONGARCH_UAL = (1 << 2), 1311 HWCAP_LOONGARCH_FPU = (1 << 3), 1312 HWCAP_LOONGARCH_LSX = (1 << 4), 1313 HWCAP_LOONGARCH_LASX = (1 << 5), 1314 HWCAP_LOONGARCH_CRC32 = (1 << 6), 1315 HWCAP_LOONGARCH_COMPLEX = (1 << 7), 1316 HWCAP_LOONGARCH_CRYPTO = (1 << 8), 1317 HWCAP_LOONGARCH_LVZ = (1 << 9), 1318 HWCAP_LOONGARCH_LBT_X86 = (1 << 10), 1319 HWCAP_LOONGARCH_LBT_ARM = (1 << 11), 1320 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12), 1321 }; 1322 1323 static uint32_t get_elf_hwcap(void) 1324 { 1325 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu); 1326 uint32_t hwcaps = 0; 1327 1328 hwcaps |= HWCAP_LOONGARCH_CRC32; 1329 1330 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) { 1331 hwcaps |= HWCAP_LOONGARCH_UAL; 1332 } 1333 1334 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) { 1335 hwcaps |= HWCAP_LOONGARCH_FPU; 1336 } 1337 1338 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) { 1339 hwcaps |= HWCAP_LOONGARCH_LAM; 1340 } 1341 1342 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) { 1343 hwcaps |= HWCAP_LOONGARCH_LSX; 1344 } 1345 1346 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) { 1347 hwcaps |= HWCAP_LOONGARCH_LASX; 1348 } 1349 1350 return hwcaps; 1351 } 1352 1353 #define ELF_PLATFORM "loongarch" 1354 1355 #endif /* TARGET_LOONGARCH64 */ 1356 1357 #ifdef TARGET_MIPS 1358 1359 #ifdef TARGET_MIPS64 1360 #define ELF_CLASS ELFCLASS64 1361 #else 1362 #define ELF_CLASS ELFCLASS32 1363 #endif 1364 #define ELF_ARCH EM_MIPS 1365 #define EXSTACK_DEFAULT true 1366 1367 #ifdef TARGET_ABI_MIPSN32 1368 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2) 1369 #else 1370 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2)) 1371 #endif 1372 1373 #define ELF_BASE_PLATFORM get_elf_base_platform() 1374 1375 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \ 1376 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \ 1377 { return _base_platform; } } while (0) 1378 1379 static const char *get_elf_base_platform(void) 1380 { 1381 MIPSCPU *cpu = MIPS_CPU(thread_cpu); 1382 1383 /* 64 bit ISAs goes first */ 1384 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6"); 1385 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5"); 1386 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2"); 1387 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64"); 1388 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5"); 1389 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4"); 1390 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3"); 1391 1392 /* 32 bit ISAs */ 1393 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6"); 1394 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5"); 1395 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2"); 1396 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32"); 1397 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2"); 1398 1399 /* Fallback */ 1400 return "mips"; 1401 } 1402 #undef MATCH_PLATFORM_INSN 1403 1404 static inline void init_thread(struct target_pt_regs *regs, 1405 struct image_info *infop) 1406 { 1407 regs->cp0_status = 2 << CP0St_KSU; 1408 regs->cp0_epc = infop->entry; 1409 regs->regs[29] = infop->start_stack; 1410 } 1411 1412 /* See linux kernel: arch/mips/include/asm/elf.h. */ 1413 #define ELF_NREG 45 1414 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1415 1416 /* See linux kernel: arch/mips/include/asm/reg.h. */ 1417 enum { 1418 #ifdef TARGET_MIPS64 1419 TARGET_EF_R0 = 0, 1420 #else 1421 TARGET_EF_R0 = 6, 1422 #endif 1423 TARGET_EF_R26 = TARGET_EF_R0 + 26, 1424 TARGET_EF_R27 = TARGET_EF_R0 + 27, 1425 TARGET_EF_LO = TARGET_EF_R0 + 32, 1426 TARGET_EF_HI = TARGET_EF_R0 + 33, 1427 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34, 1428 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35, 1429 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36, 1430 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37 1431 }; 1432 1433 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ 1434 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env) 1435 { 1436 int i; 1437 1438 for (i = 0; i < TARGET_EF_R0; i++) { 1439 (*regs)[i] = 0; 1440 } 1441 (*regs)[TARGET_EF_R0] = 0; 1442 1443 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) { 1444 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]); 1445 } 1446 1447 (*regs)[TARGET_EF_R26] = 0; 1448 (*regs)[TARGET_EF_R27] = 0; 1449 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]); 1450 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]); 1451 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC); 1452 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr); 1453 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status); 1454 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause); 1455 } 1456 1457 #define USE_ELF_CORE_DUMP 1458 #define ELF_EXEC_PAGESIZE 4096 1459 1460 /* See arch/mips/include/uapi/asm/hwcap.h. */ 1461 enum { 1462 HWCAP_MIPS_R6 = (1 << 0), 1463 HWCAP_MIPS_MSA = (1 << 1), 1464 HWCAP_MIPS_CRC32 = (1 << 2), 1465 HWCAP_MIPS_MIPS16 = (1 << 3), 1466 HWCAP_MIPS_MDMX = (1 << 4), 1467 HWCAP_MIPS_MIPS3D = (1 << 5), 1468 HWCAP_MIPS_SMARTMIPS = (1 << 6), 1469 HWCAP_MIPS_DSP = (1 << 7), 1470 HWCAP_MIPS_DSP2 = (1 << 8), 1471 HWCAP_MIPS_DSP3 = (1 << 9), 1472 HWCAP_MIPS_MIPS16E2 = (1 << 10), 1473 HWCAP_LOONGSON_MMI = (1 << 11), 1474 HWCAP_LOONGSON_EXT = (1 << 12), 1475 HWCAP_LOONGSON_EXT2 = (1 << 13), 1476 HWCAP_LOONGSON_CPUCFG = (1 << 14), 1477 }; 1478 1479 #define ELF_HWCAP get_elf_hwcap() 1480 1481 #define GET_FEATURE_INSN(_flag, _hwcap) \ 1482 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0) 1483 1484 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \ 1485 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0) 1486 1487 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \ 1488 do { \ 1489 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \ 1490 hwcaps |= _hwcap; \ 1491 } \ 1492 } while (0) 1493 1494 static uint32_t get_elf_hwcap(void) 1495 { 1496 MIPSCPU *cpu = MIPS_CPU(thread_cpu); 1497 uint32_t hwcaps = 0; 1498 1499 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH, 1500 2, HWCAP_MIPS_R6); 1501 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA); 1502 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI); 1503 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT); 1504 1505 return hwcaps; 1506 } 1507 1508 #undef GET_FEATURE_REG_EQU 1509 #undef GET_FEATURE_REG_SET 1510 #undef GET_FEATURE_INSN 1511 1512 #endif /* TARGET_MIPS */ 1513 1514 #ifdef TARGET_MICROBLAZE 1515 1516 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD) 1517 1518 #define ELF_CLASS ELFCLASS32 1519 #define ELF_ARCH EM_MICROBLAZE 1520 1521 static inline void init_thread(struct target_pt_regs *regs, 1522 struct image_info *infop) 1523 { 1524 regs->pc = infop->entry; 1525 regs->r1 = infop->start_stack; 1526 1527 } 1528 1529 #define ELF_EXEC_PAGESIZE 4096 1530 1531 #define USE_ELF_CORE_DUMP 1532 #define ELF_NREG 38 1533 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1534 1535 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ 1536 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env) 1537 { 1538 int i, pos = 0; 1539 1540 for (i = 0; i < 32; i++) { 1541 (*regs)[pos++] = tswapreg(env->regs[i]); 1542 } 1543 1544 (*regs)[pos++] = tswapreg(env->pc); 1545 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env)); 1546 (*regs)[pos++] = 0; 1547 (*regs)[pos++] = tswapreg(env->ear); 1548 (*regs)[pos++] = 0; 1549 (*regs)[pos++] = tswapreg(env->esr); 1550 } 1551 1552 #endif /* TARGET_MICROBLAZE */ 1553 1554 #ifdef TARGET_OPENRISC 1555 1556 #define ELF_ARCH EM_OPENRISC 1557 #define ELF_CLASS ELFCLASS32 1558 #define ELF_DATA ELFDATA2MSB 1559 1560 static inline void init_thread(struct target_pt_regs *regs, 1561 struct image_info *infop) 1562 { 1563 regs->pc = infop->entry; 1564 regs->gpr[1] = infop->start_stack; 1565 } 1566 1567 #define USE_ELF_CORE_DUMP 1568 #define ELF_EXEC_PAGESIZE 8192 1569 1570 /* See linux kernel arch/openrisc/include/asm/elf.h. */ 1571 #define ELF_NREG 34 /* gprs and pc, sr */ 1572 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1573 1574 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1575 const CPUOpenRISCState *env) 1576 { 1577 int i; 1578 1579 for (i = 0; i < 32; i++) { 1580 (*regs)[i] = tswapreg(cpu_get_gpr(env, i)); 1581 } 1582 (*regs)[32] = tswapreg(env->pc); 1583 (*regs)[33] = tswapreg(cpu_get_sr(env)); 1584 } 1585 #define ELF_HWCAP 0 1586 #define ELF_PLATFORM NULL 1587 1588 #endif /* TARGET_OPENRISC */ 1589 1590 #ifdef TARGET_SH4 1591 1592 #define ELF_CLASS ELFCLASS32 1593 #define ELF_ARCH EM_SH 1594 1595 static inline void init_thread(struct target_pt_regs *regs, 1596 struct image_info *infop) 1597 { 1598 /* Check other registers XXXXX */ 1599 regs->pc = infop->entry; 1600 regs->regs[15] = infop->start_stack; 1601 } 1602 1603 /* See linux kernel: arch/sh/include/asm/elf.h. */ 1604 #define ELF_NREG 23 1605 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1606 1607 /* See linux kernel: arch/sh/include/asm/ptrace.h. */ 1608 enum { 1609 TARGET_REG_PC = 16, 1610 TARGET_REG_PR = 17, 1611 TARGET_REG_SR = 18, 1612 TARGET_REG_GBR = 19, 1613 TARGET_REG_MACH = 20, 1614 TARGET_REG_MACL = 21, 1615 TARGET_REG_SYSCALL = 22 1616 }; 1617 1618 static inline void elf_core_copy_regs(target_elf_gregset_t *regs, 1619 const CPUSH4State *env) 1620 { 1621 int i; 1622 1623 for (i = 0; i < 16; i++) { 1624 (*regs)[i] = tswapreg(env->gregs[i]); 1625 } 1626 1627 (*regs)[TARGET_REG_PC] = tswapreg(env->pc); 1628 (*regs)[TARGET_REG_PR] = tswapreg(env->pr); 1629 (*regs)[TARGET_REG_SR] = tswapreg(env->sr); 1630 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr); 1631 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach); 1632 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl); 1633 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */ 1634 } 1635 1636 #define USE_ELF_CORE_DUMP 1637 #define ELF_EXEC_PAGESIZE 4096 1638 1639 enum { 1640 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */ 1641 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */ 1642 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */ 1643 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */ 1644 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */ 1645 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */ 1646 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */ 1647 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */ 1648 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */ 1649 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */ 1650 }; 1651 1652 #define ELF_HWCAP get_elf_hwcap() 1653 1654 static uint32_t get_elf_hwcap(void) 1655 { 1656 SuperHCPU *cpu = SUPERH_CPU(thread_cpu); 1657 uint32_t hwcap = 0; 1658 1659 hwcap |= SH_CPU_HAS_FPU; 1660 1661 if (cpu->env.features & SH_FEATURE_SH4A) { 1662 hwcap |= SH_CPU_HAS_LLSC; 1663 } 1664 1665 return hwcap; 1666 } 1667 1668 #endif 1669 1670 #ifdef TARGET_M68K 1671 1672 #define ELF_CLASS ELFCLASS32 1673 #define ELF_ARCH EM_68K 1674 1675 /* ??? Does this need to do anything? 1676 #define ELF_PLAT_INIT(_r) */ 1677 1678 static inline void init_thread(struct target_pt_regs *regs, 1679 struct image_info *infop) 1680 { 1681 regs->usp = infop->start_stack; 1682 regs->sr = 0; 1683 regs->pc = infop->entry; 1684 } 1685 1686 /* See linux kernel: arch/m68k/include/asm/elf.h. */ 1687 #define ELF_NREG 20 1688 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1689 1690 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env) 1691 { 1692 (*regs)[0] = tswapreg(env->dregs[1]); 1693 (*regs)[1] = tswapreg(env->dregs[2]); 1694 (*regs)[2] = tswapreg(env->dregs[3]); 1695 (*regs)[3] = tswapreg(env->dregs[4]); 1696 (*regs)[4] = tswapreg(env->dregs[5]); 1697 (*regs)[5] = tswapreg(env->dregs[6]); 1698 (*regs)[6] = tswapreg(env->dregs[7]); 1699 (*regs)[7] = tswapreg(env->aregs[0]); 1700 (*regs)[8] = tswapreg(env->aregs[1]); 1701 (*regs)[9] = tswapreg(env->aregs[2]); 1702 (*regs)[10] = tswapreg(env->aregs[3]); 1703 (*regs)[11] = tswapreg(env->aregs[4]); 1704 (*regs)[12] = tswapreg(env->aregs[5]); 1705 (*regs)[13] = tswapreg(env->aregs[6]); 1706 (*regs)[14] = tswapreg(env->dregs[0]); 1707 (*regs)[15] = tswapreg(env->aregs[7]); 1708 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */ 1709 (*regs)[17] = tswapreg(env->sr); 1710 (*regs)[18] = tswapreg(env->pc); 1711 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */ 1712 } 1713 1714 #define USE_ELF_CORE_DUMP 1715 #define ELF_EXEC_PAGESIZE 8192 1716 1717 #endif 1718 1719 #ifdef TARGET_ALPHA 1720 1721 #define ELF_CLASS ELFCLASS64 1722 #define ELF_ARCH EM_ALPHA 1723 1724 static inline void init_thread(struct target_pt_regs *regs, 1725 struct image_info *infop) 1726 { 1727 regs->pc = infop->entry; 1728 regs->ps = 8; 1729 regs->usp = infop->start_stack; 1730 } 1731 1732 #define ELF_EXEC_PAGESIZE 8192 1733 1734 #endif /* TARGET_ALPHA */ 1735 1736 #ifdef TARGET_S390X 1737 1738 #define ELF_CLASS ELFCLASS64 1739 #define ELF_DATA ELFDATA2MSB 1740 #define ELF_ARCH EM_S390 1741 1742 #include "elf.h" 1743 1744 #define ELF_HWCAP get_elf_hwcap() 1745 1746 #define GET_FEATURE(_feat, _hwcap) \ 1747 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0) 1748 1749 uint32_t get_elf_hwcap(void) 1750 { 1751 /* 1752 * Let's assume we always have esan3 and zarch. 1753 * 31-bit processes can use 64-bit registers (high gprs). 1754 */ 1755 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS; 1756 1757 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE); 1758 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA); 1759 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP); 1760 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM); 1761 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) && 1762 s390_has_feat(S390_FEAT_ETF3_ENH)) { 1763 hwcap |= HWCAP_S390_ETF3EH; 1764 } 1765 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS); 1766 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT); 1767 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2); 1768 1769 return hwcap; 1770 } 1771 1772 const char *elf_hwcap_str(uint32_t bit) 1773 { 1774 static const char *hwcap_str[] = { 1775 [HWCAP_S390_NR_ESAN3] = "esan3", 1776 [HWCAP_S390_NR_ZARCH] = "zarch", 1777 [HWCAP_S390_NR_STFLE] = "stfle", 1778 [HWCAP_S390_NR_MSA] = "msa", 1779 [HWCAP_S390_NR_LDISP] = "ldisp", 1780 [HWCAP_S390_NR_EIMM] = "eimm", 1781 [HWCAP_S390_NR_DFP] = "dfp", 1782 [HWCAP_S390_NR_HPAGE] = "edat", 1783 [HWCAP_S390_NR_ETF3EH] = "etf3eh", 1784 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs", 1785 [HWCAP_S390_NR_TE] = "te", 1786 [HWCAP_S390_NR_VXRS] = "vx", 1787 [HWCAP_S390_NR_VXRS_BCD] = "vxd", 1788 [HWCAP_S390_NR_VXRS_EXT] = "vxe", 1789 [HWCAP_S390_NR_GS] = "gs", 1790 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2", 1791 [HWCAP_S390_NR_VXRS_PDE] = "vxp", 1792 [HWCAP_S390_NR_SORT] = "sort", 1793 [HWCAP_S390_NR_DFLT] = "dflt", 1794 [HWCAP_S390_NR_NNPA] = "nnpa", 1795 [HWCAP_S390_NR_PCI_MIO] = "pcimio", 1796 [HWCAP_S390_NR_SIE] = "sie", 1797 }; 1798 1799 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 1800 } 1801 1802 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) 1803 { 1804 regs->psw.addr = infop->entry; 1805 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \ 1806 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \ 1807 PSW_MASK_32; 1808 regs->gprs[15] = infop->start_stack; 1809 } 1810 1811 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */ 1812 #define ELF_NREG 27 1813 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1814 1815 enum { 1816 TARGET_REG_PSWM = 0, 1817 TARGET_REG_PSWA = 1, 1818 TARGET_REG_GPRS = 2, 1819 TARGET_REG_ARS = 18, 1820 TARGET_REG_ORIG_R2 = 26, 1821 }; 1822 1823 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1824 const CPUS390XState *env) 1825 { 1826 int i; 1827 uint32_t *aregs; 1828 1829 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask); 1830 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr); 1831 for (i = 0; i < 16; i++) { 1832 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]); 1833 } 1834 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]); 1835 for (i = 0; i < 16; i++) { 1836 aregs[i] = tswap32(env->aregs[i]); 1837 } 1838 (*regs)[TARGET_REG_ORIG_R2] = 0; 1839 } 1840 1841 #define USE_ELF_CORE_DUMP 1842 #define ELF_EXEC_PAGESIZE 4096 1843 1844 #define VDSO_HEADER "vdso.c.inc" 1845 1846 #endif /* TARGET_S390X */ 1847 1848 #ifdef TARGET_RISCV 1849 1850 #define ELF_ARCH EM_RISCV 1851 1852 #ifdef TARGET_RISCV32 1853 #define ELF_CLASS ELFCLASS32 1854 #define VDSO_HEADER "vdso-32.c.inc" 1855 #else 1856 #define ELF_CLASS ELFCLASS64 1857 #define VDSO_HEADER "vdso-64.c.inc" 1858 #endif 1859 1860 #define ELF_HWCAP get_elf_hwcap() 1861 1862 static uint32_t get_elf_hwcap(void) 1863 { 1864 #define MISA_BIT(EXT) (1 << (EXT - 'A')) 1865 RISCVCPU *cpu = RISCV_CPU(thread_cpu); 1866 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A') 1867 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C') 1868 | MISA_BIT('V'); 1869 1870 return cpu->env.misa_ext & mask; 1871 #undef MISA_BIT 1872 } 1873 1874 static inline void init_thread(struct target_pt_regs *regs, 1875 struct image_info *infop) 1876 { 1877 regs->sepc = infop->entry; 1878 regs->sp = infop->start_stack; 1879 } 1880 1881 #define ELF_EXEC_PAGESIZE 4096 1882 1883 #endif /* TARGET_RISCV */ 1884 1885 #ifdef TARGET_HPPA 1886 1887 #define ELF_CLASS ELFCLASS32 1888 #define ELF_ARCH EM_PARISC 1889 #define ELF_PLATFORM "PARISC" 1890 #define STACK_GROWS_DOWN 0 1891 #define STACK_ALIGNMENT 64 1892 1893 #define VDSO_HEADER "vdso.c.inc" 1894 1895 static inline void init_thread(struct target_pt_regs *regs, 1896 struct image_info *infop) 1897 { 1898 regs->iaoq[0] = infop->entry | PRIV_USER; 1899 regs->iaoq[1] = regs->iaoq[0] + 4; 1900 regs->gr[23] = 0; 1901 regs->gr[24] = infop->argv; 1902 regs->gr[25] = infop->argc; 1903 /* The top-of-stack contains a linkage buffer. */ 1904 regs->gr[30] = infop->start_stack + 64; 1905 regs->gr[31] = infop->entry; 1906 } 1907 1908 #define LO_COMMPAGE 0 1909 1910 static bool init_guest_commpage(void) 1911 { 1912 /* If reserved_va, then we have already mapped 0 page on the host. */ 1913 if (!reserved_va) { 1914 void *want, *addr; 1915 1916 want = g2h_untagged(LO_COMMPAGE); 1917 addr = mmap(want, TARGET_PAGE_SIZE, PROT_NONE, 1918 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE, -1, 0); 1919 if (addr == MAP_FAILED) { 1920 perror("Allocating guest commpage"); 1921 exit(EXIT_FAILURE); 1922 } 1923 if (addr != want) { 1924 return false; 1925 } 1926 } 1927 1928 /* 1929 * On Linux, page zero is normally marked execute only + gateway. 1930 * Normal read or write is supposed to fail (thus PROT_NONE above), 1931 * but specific offsets have kernel code mapped to raise permissions 1932 * and implement syscalls. Here, simply mark the page executable. 1933 * Special case the entry points during translation (see do_page_zero). 1934 */ 1935 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK, 1936 PAGE_EXEC | PAGE_VALID); 1937 return true; 1938 } 1939 1940 #endif /* TARGET_HPPA */ 1941 1942 #ifdef TARGET_XTENSA 1943 1944 #define ELF_CLASS ELFCLASS32 1945 #define ELF_ARCH EM_XTENSA 1946 1947 static inline void init_thread(struct target_pt_regs *regs, 1948 struct image_info *infop) 1949 { 1950 regs->windowbase = 0; 1951 regs->windowstart = 1; 1952 regs->areg[1] = infop->start_stack; 1953 regs->pc = infop->entry; 1954 if (info_is_fdpic(infop)) { 1955 regs->areg[4] = infop->loadmap_addr; 1956 regs->areg[5] = infop->interpreter_loadmap_addr; 1957 if (infop->interpreter_loadmap_addr) { 1958 regs->areg[6] = infop->interpreter_pt_dynamic_addr; 1959 } else { 1960 regs->areg[6] = infop->pt_dynamic_addr; 1961 } 1962 } 1963 } 1964 1965 /* See linux kernel: arch/xtensa/include/asm/elf.h. */ 1966 #define ELF_NREG 128 1967 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1968 1969 enum { 1970 TARGET_REG_PC, 1971 TARGET_REG_PS, 1972 TARGET_REG_LBEG, 1973 TARGET_REG_LEND, 1974 TARGET_REG_LCOUNT, 1975 TARGET_REG_SAR, 1976 TARGET_REG_WINDOWSTART, 1977 TARGET_REG_WINDOWBASE, 1978 TARGET_REG_THREADPTR, 1979 TARGET_REG_AR0 = 64, 1980 }; 1981 1982 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1983 const CPUXtensaState *env) 1984 { 1985 unsigned i; 1986 1987 (*regs)[TARGET_REG_PC] = tswapreg(env->pc); 1988 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM); 1989 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]); 1990 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]); 1991 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]); 1992 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]); 1993 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]); 1994 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]); 1995 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]); 1996 xtensa_sync_phys_from_window((CPUXtensaState *)env); 1997 for (i = 0; i < env->config->nareg; ++i) { 1998 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]); 1999 } 2000 } 2001 2002 #define USE_ELF_CORE_DUMP 2003 #define ELF_EXEC_PAGESIZE 4096 2004 2005 #endif /* TARGET_XTENSA */ 2006 2007 #ifdef TARGET_HEXAGON 2008 2009 #define ELF_CLASS ELFCLASS32 2010 #define ELF_ARCH EM_HEXAGON 2011 2012 static inline void init_thread(struct target_pt_regs *regs, 2013 struct image_info *infop) 2014 { 2015 regs->sepc = infop->entry; 2016 regs->sp = infop->start_stack; 2017 } 2018 2019 #endif /* TARGET_HEXAGON */ 2020 2021 #ifndef ELF_BASE_PLATFORM 2022 #define ELF_BASE_PLATFORM (NULL) 2023 #endif 2024 2025 #ifndef ELF_PLATFORM 2026 #define ELF_PLATFORM (NULL) 2027 #endif 2028 2029 #ifndef ELF_MACHINE 2030 #define ELF_MACHINE ELF_ARCH 2031 #endif 2032 2033 #ifndef elf_check_arch 2034 #define elf_check_arch(x) ((x) == ELF_ARCH) 2035 #endif 2036 2037 #ifndef elf_check_abi 2038 #define elf_check_abi(x) (1) 2039 #endif 2040 2041 #ifndef ELF_HWCAP 2042 #define ELF_HWCAP 0 2043 #endif 2044 2045 #ifndef STACK_GROWS_DOWN 2046 #define STACK_GROWS_DOWN 1 2047 #endif 2048 2049 #ifndef STACK_ALIGNMENT 2050 #define STACK_ALIGNMENT 16 2051 #endif 2052 2053 #ifdef TARGET_ABI32 2054 #undef ELF_CLASS 2055 #define ELF_CLASS ELFCLASS32 2056 #undef bswaptls 2057 #define bswaptls(ptr) bswap32s(ptr) 2058 #endif 2059 2060 #ifndef EXSTACK_DEFAULT 2061 #define EXSTACK_DEFAULT false 2062 #endif 2063 2064 #include "elf.h" 2065 2066 /* We must delay the following stanzas until after "elf.h". */ 2067 #if defined(TARGET_AARCH64) 2068 2069 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, 2070 const uint32_t *data, 2071 struct image_info *info, 2072 Error **errp) 2073 { 2074 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) { 2075 if (pr_datasz != sizeof(uint32_t)) { 2076 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND"); 2077 return false; 2078 } 2079 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */ 2080 info->note_flags = *data; 2081 } 2082 return true; 2083 } 2084 #define ARCH_USE_GNU_PROPERTY 1 2085 2086 #else 2087 2088 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, 2089 const uint32_t *data, 2090 struct image_info *info, 2091 Error **errp) 2092 { 2093 g_assert_not_reached(); 2094 } 2095 #define ARCH_USE_GNU_PROPERTY 0 2096 2097 #endif 2098 2099 struct exec 2100 { 2101 unsigned int a_info; /* Use macros N_MAGIC, etc for access */ 2102 unsigned int a_text; /* length of text, in bytes */ 2103 unsigned int a_data; /* length of data, in bytes */ 2104 unsigned int a_bss; /* length of uninitialized data area, in bytes */ 2105 unsigned int a_syms; /* length of symbol table data in file, in bytes */ 2106 unsigned int a_entry; /* start address */ 2107 unsigned int a_trsize; /* length of relocation info for text, in bytes */ 2108 unsigned int a_drsize; /* length of relocation info for data, in bytes */ 2109 }; 2110 2111 2112 #define N_MAGIC(exec) ((exec).a_info & 0xffff) 2113 #define OMAGIC 0407 2114 #define NMAGIC 0410 2115 #define ZMAGIC 0413 2116 #define QMAGIC 0314 2117 2118 #define DLINFO_ITEMS 16 2119 2120 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) 2121 { 2122 memcpy(to, from, n); 2123 } 2124 2125 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN 2126 static void bswap_ehdr(struct elfhdr *ehdr) 2127 { 2128 bswap16s(&ehdr->e_type); /* Object file type */ 2129 bswap16s(&ehdr->e_machine); /* Architecture */ 2130 bswap32s(&ehdr->e_version); /* Object file version */ 2131 bswaptls(&ehdr->e_entry); /* Entry point virtual address */ 2132 bswaptls(&ehdr->e_phoff); /* Program header table file offset */ 2133 bswaptls(&ehdr->e_shoff); /* Section header table file offset */ 2134 bswap32s(&ehdr->e_flags); /* Processor-specific flags */ 2135 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ 2136 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ 2137 bswap16s(&ehdr->e_phnum); /* Program header table entry count */ 2138 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ 2139 bswap16s(&ehdr->e_shnum); /* Section header table entry count */ 2140 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ 2141 } 2142 2143 static void bswap_phdr(struct elf_phdr *phdr, int phnum) 2144 { 2145 int i; 2146 for (i = 0; i < phnum; ++i, ++phdr) { 2147 bswap32s(&phdr->p_type); /* Segment type */ 2148 bswap32s(&phdr->p_flags); /* Segment flags */ 2149 bswaptls(&phdr->p_offset); /* Segment file offset */ 2150 bswaptls(&phdr->p_vaddr); /* Segment virtual address */ 2151 bswaptls(&phdr->p_paddr); /* Segment physical address */ 2152 bswaptls(&phdr->p_filesz); /* Segment size in file */ 2153 bswaptls(&phdr->p_memsz); /* Segment size in memory */ 2154 bswaptls(&phdr->p_align); /* Segment alignment */ 2155 } 2156 } 2157 2158 static void bswap_shdr(struct elf_shdr *shdr, int shnum) 2159 { 2160 int i; 2161 for (i = 0; i < shnum; ++i, ++shdr) { 2162 bswap32s(&shdr->sh_name); 2163 bswap32s(&shdr->sh_type); 2164 bswaptls(&shdr->sh_flags); 2165 bswaptls(&shdr->sh_addr); 2166 bswaptls(&shdr->sh_offset); 2167 bswaptls(&shdr->sh_size); 2168 bswap32s(&shdr->sh_link); 2169 bswap32s(&shdr->sh_info); 2170 bswaptls(&shdr->sh_addralign); 2171 bswaptls(&shdr->sh_entsize); 2172 } 2173 } 2174 2175 static void bswap_sym(struct elf_sym *sym) 2176 { 2177 bswap32s(&sym->st_name); 2178 bswaptls(&sym->st_value); 2179 bswaptls(&sym->st_size); 2180 bswap16s(&sym->st_shndx); 2181 } 2182 2183 #ifdef TARGET_MIPS 2184 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) 2185 { 2186 bswap16s(&abiflags->version); 2187 bswap32s(&abiflags->ases); 2188 bswap32s(&abiflags->isa_ext); 2189 bswap32s(&abiflags->flags1); 2190 bswap32s(&abiflags->flags2); 2191 } 2192 #endif 2193 #else 2194 static inline void bswap_ehdr(struct elfhdr *ehdr) { } 2195 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { } 2196 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { } 2197 static inline void bswap_sym(struct elf_sym *sym) { } 2198 #ifdef TARGET_MIPS 2199 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { } 2200 #endif 2201 #endif 2202 2203 #ifdef USE_ELF_CORE_DUMP 2204 static int elf_core_dump(int, const CPUArchState *); 2205 #endif /* USE_ELF_CORE_DUMP */ 2206 static void load_symbols(struct elfhdr *hdr, const ImageSource *src, 2207 abi_ulong load_bias); 2208 2209 /* Verify the portions of EHDR within E_IDENT for the target. 2210 This can be performed before bswapping the entire header. */ 2211 static bool elf_check_ident(struct elfhdr *ehdr) 2212 { 2213 return (ehdr->e_ident[EI_MAG0] == ELFMAG0 2214 && ehdr->e_ident[EI_MAG1] == ELFMAG1 2215 && ehdr->e_ident[EI_MAG2] == ELFMAG2 2216 && ehdr->e_ident[EI_MAG3] == ELFMAG3 2217 && ehdr->e_ident[EI_CLASS] == ELF_CLASS 2218 && ehdr->e_ident[EI_DATA] == ELF_DATA 2219 && ehdr->e_ident[EI_VERSION] == EV_CURRENT); 2220 } 2221 2222 /* Verify the portions of EHDR outside of E_IDENT for the target. 2223 This has to wait until after bswapping the header. */ 2224 static bool elf_check_ehdr(struct elfhdr *ehdr) 2225 { 2226 return (elf_check_arch(ehdr->e_machine) 2227 && elf_check_abi(ehdr->e_flags) 2228 && ehdr->e_ehsize == sizeof(struct elfhdr) 2229 && ehdr->e_phentsize == sizeof(struct elf_phdr) 2230 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)); 2231 } 2232 2233 /* 2234 * 'copy_elf_strings()' copies argument/envelope strings from user 2235 * memory to free pages in kernel mem. These are in a format ready 2236 * to be put directly into the top of new user memory. 2237 * 2238 */ 2239 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch, 2240 abi_ulong p, abi_ulong stack_limit) 2241 { 2242 char *tmp; 2243 int len, i; 2244 abi_ulong top = p; 2245 2246 if (!p) { 2247 return 0; /* bullet-proofing */ 2248 } 2249 2250 if (STACK_GROWS_DOWN) { 2251 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1; 2252 for (i = argc - 1; i >= 0; --i) { 2253 tmp = argv[i]; 2254 if (!tmp) { 2255 fprintf(stderr, "VFS: argc is wrong"); 2256 exit(-1); 2257 } 2258 len = strlen(tmp) + 1; 2259 tmp += len; 2260 2261 if (len > (p - stack_limit)) { 2262 return 0; 2263 } 2264 while (len) { 2265 int bytes_to_copy = (len > offset) ? offset : len; 2266 tmp -= bytes_to_copy; 2267 p -= bytes_to_copy; 2268 offset -= bytes_to_copy; 2269 len -= bytes_to_copy; 2270 2271 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy); 2272 2273 if (offset == 0) { 2274 memcpy_to_target(p, scratch, top - p); 2275 top = p; 2276 offset = TARGET_PAGE_SIZE; 2277 } 2278 } 2279 } 2280 if (p != top) { 2281 memcpy_to_target(p, scratch + offset, top - p); 2282 } 2283 } else { 2284 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE); 2285 for (i = 0; i < argc; ++i) { 2286 tmp = argv[i]; 2287 if (!tmp) { 2288 fprintf(stderr, "VFS: argc is wrong"); 2289 exit(-1); 2290 } 2291 len = strlen(tmp) + 1; 2292 if (len > (stack_limit - p)) { 2293 return 0; 2294 } 2295 while (len) { 2296 int bytes_to_copy = (len > remaining) ? remaining : len; 2297 2298 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy); 2299 2300 tmp += bytes_to_copy; 2301 remaining -= bytes_to_copy; 2302 p += bytes_to_copy; 2303 len -= bytes_to_copy; 2304 2305 if (remaining == 0) { 2306 memcpy_to_target(top, scratch, p - top); 2307 top = p; 2308 remaining = TARGET_PAGE_SIZE; 2309 } 2310 } 2311 } 2312 if (p != top) { 2313 memcpy_to_target(top, scratch, p - top); 2314 } 2315 } 2316 2317 return p; 2318 } 2319 2320 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of 2321 * argument/environment space. Newer kernels (>2.6.33) allow more, 2322 * dependent on stack size, but guarantee at least 32 pages for 2323 * backwards compatibility. 2324 */ 2325 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE) 2326 2327 static abi_ulong setup_arg_pages(struct linux_binprm *bprm, 2328 struct image_info *info) 2329 { 2330 abi_ulong size, error, guard; 2331 int prot; 2332 2333 size = guest_stack_size; 2334 if (size < STACK_LOWER_LIMIT) { 2335 size = STACK_LOWER_LIMIT; 2336 } 2337 2338 if (STACK_GROWS_DOWN) { 2339 guard = TARGET_PAGE_SIZE; 2340 if (guard < qemu_real_host_page_size()) { 2341 guard = qemu_real_host_page_size(); 2342 } 2343 } else { 2344 /* no guard page for hppa target where stack grows upwards. */ 2345 guard = 0; 2346 } 2347 2348 prot = PROT_READ | PROT_WRITE; 2349 if (info->exec_stack) { 2350 prot |= PROT_EXEC; 2351 } 2352 error = target_mmap(0, size + guard, prot, 2353 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 2354 if (error == -1) { 2355 perror("mmap stack"); 2356 exit(-1); 2357 } 2358 2359 /* We reserve one extra page at the top of the stack as guard. */ 2360 if (STACK_GROWS_DOWN) { 2361 target_mprotect(error, guard, PROT_NONE); 2362 info->stack_limit = error + guard; 2363 return info->stack_limit + size - sizeof(void *); 2364 } else { 2365 info->stack_limit = error + size; 2366 return error; 2367 } 2368 } 2369 2370 /** 2371 * zero_bss: 2372 * 2373 * Map and zero the bss. We need to explicitly zero any fractional pages 2374 * after the data section (i.e. bss). Return false on mapping failure. 2375 */ 2376 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss, 2377 int prot, Error **errp) 2378 { 2379 abi_ulong align_bss; 2380 2381 /* We only expect writable bss; the code segment shouldn't need this. */ 2382 if (!(prot & PROT_WRITE)) { 2383 error_setg(errp, "PT_LOAD with non-writable bss"); 2384 return false; 2385 } 2386 2387 align_bss = TARGET_PAGE_ALIGN(start_bss); 2388 end_bss = TARGET_PAGE_ALIGN(end_bss); 2389 2390 if (start_bss < align_bss) { 2391 int flags = page_get_flags(start_bss); 2392 2393 if (!(flags & PAGE_RWX)) { 2394 /* 2395 * The whole address space of the executable was reserved 2396 * at the start, therefore all pages will be VALID. 2397 * But assuming there are no PROT_NONE PT_LOAD segments, 2398 * a PROT_NONE page means no data all bss, and we can 2399 * simply extend the new anon mapping back to the start 2400 * of the page of bss. 2401 */ 2402 align_bss -= TARGET_PAGE_SIZE; 2403 } else { 2404 /* 2405 * The start of the bss shares a page with something. 2406 * The only thing that we expect is the data section, 2407 * which would already be marked writable. 2408 * Overlapping the RX code segment seems malformed. 2409 */ 2410 if (!(flags & PAGE_WRITE)) { 2411 error_setg(errp, "PT_LOAD with bss overlapping " 2412 "non-writable page"); 2413 return false; 2414 } 2415 2416 /* The page is already mapped and writable. */ 2417 memset(g2h_untagged(start_bss), 0, align_bss - start_bss); 2418 } 2419 } 2420 2421 if (align_bss < end_bss && 2422 target_mmap(align_bss, end_bss - align_bss, prot, 2423 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) { 2424 error_setg_errno(errp, errno, "Error mapping bss"); 2425 return false; 2426 } 2427 return true; 2428 } 2429 2430 #if defined(TARGET_ARM) 2431 static int elf_is_fdpic(struct elfhdr *exec) 2432 { 2433 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC; 2434 } 2435 #elif defined(TARGET_XTENSA) 2436 static int elf_is_fdpic(struct elfhdr *exec) 2437 { 2438 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC; 2439 } 2440 #else 2441 /* Default implementation, always false. */ 2442 static int elf_is_fdpic(struct elfhdr *exec) 2443 { 2444 return 0; 2445 } 2446 #endif 2447 2448 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp) 2449 { 2450 uint16_t n; 2451 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs; 2452 2453 /* elf32_fdpic_loadseg */ 2454 n = info->nsegs; 2455 while (n--) { 2456 sp -= 12; 2457 put_user_u32(loadsegs[n].addr, sp+0); 2458 put_user_u32(loadsegs[n].p_vaddr, sp+4); 2459 put_user_u32(loadsegs[n].p_memsz, sp+8); 2460 } 2461 2462 /* elf32_fdpic_loadmap */ 2463 sp -= 4; 2464 put_user_u16(0, sp+0); /* version */ 2465 put_user_u16(info->nsegs, sp+2); /* nsegs */ 2466 2467 info->personality = PER_LINUX_FDPIC; 2468 info->loadmap_addr = sp; 2469 2470 return sp; 2471 } 2472 2473 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, 2474 struct elfhdr *exec, 2475 struct image_info *info, 2476 struct image_info *interp_info, 2477 struct image_info *vdso_info) 2478 { 2479 abi_ulong sp; 2480 abi_ulong u_argc, u_argv, u_envp, u_auxv; 2481 int size; 2482 int i; 2483 abi_ulong u_rand_bytes; 2484 uint8_t k_rand_bytes[16]; 2485 abi_ulong u_platform, u_base_platform; 2486 const char *k_platform, *k_base_platform; 2487 const int n = sizeof(elf_addr_t); 2488 2489 sp = p; 2490 2491 /* Needs to be before we load the env/argc/... */ 2492 if (elf_is_fdpic(exec)) { 2493 /* Need 4 byte alignment for these structs */ 2494 sp &= ~3; 2495 sp = loader_build_fdpic_loadmap(info, sp); 2496 info->other_info = interp_info; 2497 if (interp_info) { 2498 interp_info->other_info = info; 2499 sp = loader_build_fdpic_loadmap(interp_info, sp); 2500 info->interpreter_loadmap_addr = interp_info->loadmap_addr; 2501 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr; 2502 } else { 2503 info->interpreter_loadmap_addr = 0; 2504 info->interpreter_pt_dynamic_addr = 0; 2505 } 2506 } 2507 2508 u_base_platform = 0; 2509 k_base_platform = ELF_BASE_PLATFORM; 2510 if (k_base_platform) { 2511 size_t len = strlen(k_base_platform) + 1; 2512 if (STACK_GROWS_DOWN) { 2513 sp -= (len + n - 1) & ~(n - 1); 2514 u_base_platform = sp; 2515 /* FIXME - check return value of memcpy_to_target() for failure */ 2516 memcpy_to_target(sp, k_base_platform, len); 2517 } else { 2518 memcpy_to_target(sp, k_base_platform, len); 2519 u_base_platform = sp; 2520 sp += len + 1; 2521 } 2522 } 2523 2524 u_platform = 0; 2525 k_platform = ELF_PLATFORM; 2526 if (k_platform) { 2527 size_t len = strlen(k_platform) + 1; 2528 if (STACK_GROWS_DOWN) { 2529 sp -= (len + n - 1) & ~(n - 1); 2530 u_platform = sp; 2531 /* FIXME - check return value of memcpy_to_target() for failure */ 2532 memcpy_to_target(sp, k_platform, len); 2533 } else { 2534 memcpy_to_target(sp, k_platform, len); 2535 u_platform = sp; 2536 sp += len + 1; 2537 } 2538 } 2539 2540 /* Provide 16 byte alignment for the PRNG, and basic alignment for 2541 * the argv and envp pointers. 2542 */ 2543 if (STACK_GROWS_DOWN) { 2544 sp = QEMU_ALIGN_DOWN(sp, 16); 2545 } else { 2546 sp = QEMU_ALIGN_UP(sp, 16); 2547 } 2548 2549 /* 2550 * Generate 16 random bytes for userspace PRNG seeding. 2551 */ 2552 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes)); 2553 if (STACK_GROWS_DOWN) { 2554 sp -= 16; 2555 u_rand_bytes = sp; 2556 /* FIXME - check return value of memcpy_to_target() for failure */ 2557 memcpy_to_target(sp, k_rand_bytes, 16); 2558 } else { 2559 memcpy_to_target(sp, k_rand_bytes, 16); 2560 u_rand_bytes = sp; 2561 sp += 16; 2562 } 2563 2564 size = (DLINFO_ITEMS + 1) * 2; 2565 if (k_base_platform) { 2566 size += 2; 2567 } 2568 if (k_platform) { 2569 size += 2; 2570 } 2571 if (vdso_info) { 2572 size += 2; 2573 } 2574 #ifdef DLINFO_ARCH_ITEMS 2575 size += DLINFO_ARCH_ITEMS * 2; 2576 #endif 2577 #ifdef ELF_HWCAP2 2578 size += 2; 2579 #endif 2580 info->auxv_len = size * n; 2581 2582 size += envc + argc + 2; 2583 size += 1; /* argc itself */ 2584 size *= n; 2585 2586 /* Allocate space and finalize stack alignment for entry now. */ 2587 if (STACK_GROWS_DOWN) { 2588 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT); 2589 sp = u_argc; 2590 } else { 2591 u_argc = sp; 2592 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT); 2593 } 2594 2595 u_argv = u_argc + n; 2596 u_envp = u_argv + (argc + 1) * n; 2597 u_auxv = u_envp + (envc + 1) * n; 2598 info->saved_auxv = u_auxv; 2599 info->argc = argc; 2600 info->envc = envc; 2601 info->argv = u_argv; 2602 info->envp = u_envp; 2603 2604 /* This is correct because Linux defines 2605 * elf_addr_t as Elf32_Off / Elf64_Off 2606 */ 2607 #define NEW_AUX_ENT(id, val) do { \ 2608 put_user_ual(id, u_auxv); u_auxv += n; \ 2609 put_user_ual(val, u_auxv); u_auxv += n; \ 2610 } while(0) 2611 2612 #ifdef ARCH_DLINFO 2613 /* 2614 * ARCH_DLINFO must come first so platform specific code can enforce 2615 * special alignment requirements on the AUXV if necessary (eg. PPC). 2616 */ 2617 ARCH_DLINFO; 2618 #endif 2619 /* There must be exactly DLINFO_ITEMS entries here, or the assert 2620 * on info->auxv_len will trigger. 2621 */ 2622 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff)); 2623 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); 2624 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); 2625 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE)); 2626 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0)); 2627 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); 2628 NEW_AUX_ENT(AT_ENTRY, info->entry); 2629 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid()); 2630 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid()); 2631 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid()); 2632 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid()); 2633 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP); 2634 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK)); 2635 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes); 2636 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE)); 2637 NEW_AUX_ENT(AT_EXECFN, info->file_string); 2638 2639 #ifdef ELF_HWCAP2 2640 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2); 2641 #endif 2642 2643 if (u_base_platform) { 2644 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform); 2645 } 2646 if (u_platform) { 2647 NEW_AUX_ENT(AT_PLATFORM, u_platform); 2648 } 2649 if (vdso_info) { 2650 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr); 2651 } 2652 NEW_AUX_ENT (AT_NULL, 0); 2653 #undef NEW_AUX_ENT 2654 2655 /* Check that our initial calculation of the auxv length matches how much 2656 * we actually put into it. 2657 */ 2658 assert(info->auxv_len == u_auxv - info->saved_auxv); 2659 2660 put_user_ual(argc, u_argc); 2661 2662 p = info->arg_strings; 2663 for (i = 0; i < argc; ++i) { 2664 put_user_ual(p, u_argv); 2665 u_argv += n; 2666 p += target_strlen(p) + 1; 2667 } 2668 put_user_ual(0, u_argv); 2669 2670 p = info->env_strings; 2671 for (i = 0; i < envc; ++i) { 2672 put_user_ual(p, u_envp); 2673 u_envp += n; 2674 p += target_strlen(p) + 1; 2675 } 2676 put_user_ual(0, u_envp); 2677 2678 return sp; 2679 } 2680 2681 #if defined(HI_COMMPAGE) 2682 #define LO_COMMPAGE -1 2683 #elif defined(LO_COMMPAGE) 2684 #define HI_COMMPAGE 0 2685 #else 2686 #define HI_COMMPAGE 0 2687 #define LO_COMMPAGE -1 2688 #ifndef INIT_GUEST_COMMPAGE 2689 #define init_guest_commpage() true 2690 #endif 2691 #endif 2692 2693 /** 2694 * pgb_try_mmap: 2695 * @addr: host start address 2696 * @addr_last: host last address 2697 * @keep: do not unmap the probe region 2698 * 2699 * Return 1 if [@addr, @addr_last] is not mapped in the host, 2700 * return 0 if it is not available to map, and -1 on mmap error. 2701 * If @keep, the region is left mapped on success, otherwise unmapped. 2702 */ 2703 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep) 2704 { 2705 size_t size = addr_last - addr + 1; 2706 void *p = mmap((void *)addr, size, PROT_NONE, 2707 MAP_ANONYMOUS | MAP_PRIVATE | 2708 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0); 2709 int ret; 2710 2711 if (p == MAP_FAILED) { 2712 return errno == EEXIST ? 0 : -1; 2713 } 2714 ret = p == (void *)addr; 2715 if (!keep || !ret) { 2716 munmap(p, size); 2717 } 2718 return ret; 2719 } 2720 2721 /** 2722 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk) 2723 * @addr: host address 2724 * @addr_last: host last address 2725 * @brk: host brk 2726 * 2727 * Like pgb_try_mmap, but additionally reserve some memory following brk. 2728 */ 2729 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last, 2730 uintptr_t brk, bool keep) 2731 { 2732 uintptr_t brk_last = brk + 16 * MiB - 1; 2733 2734 /* Do not map anything close to the host brk. */ 2735 if (addr <= brk_last && brk <= addr_last) { 2736 return 0; 2737 } 2738 return pgb_try_mmap(addr, addr_last, keep); 2739 } 2740 2741 /** 2742 * pgb_try_mmap_set: 2743 * @ga: set of guest addrs 2744 * @base: guest_base 2745 * @brk: host brk 2746 * 2747 * Return true if all @ga can be mapped by the host at @base. 2748 * On success, retain the mapping at index 0 for reserved_va. 2749 */ 2750 2751 typedef struct PGBAddrs { 2752 uintptr_t bounds[3][2]; /* start/last pairs */ 2753 int nbounds; 2754 } PGBAddrs; 2755 2756 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk) 2757 { 2758 for (int i = ga->nbounds - 1; i >= 0; --i) { 2759 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base, 2760 ga->bounds[i][1] + base, 2761 brk, i == 0 && reserved_va) <= 0) { 2762 return false; 2763 } 2764 } 2765 return true; 2766 } 2767 2768 /** 2769 * pgb_addr_set: 2770 * @ga: output set of guest addrs 2771 * @guest_loaddr: guest image low address 2772 * @guest_loaddr: guest image high address 2773 * @identity: create for identity mapping 2774 * 2775 * Fill in @ga with the image, COMMPAGE and NULL page. 2776 */ 2777 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr, 2778 abi_ulong guest_hiaddr, bool try_identity) 2779 { 2780 int n; 2781 2782 /* 2783 * With a low commpage, or a guest mapped very low, 2784 * we may not be able to use the identity map. 2785 */ 2786 if (try_identity) { 2787 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) { 2788 return false; 2789 } 2790 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) { 2791 return false; 2792 } 2793 } 2794 2795 memset(ga, 0, sizeof(*ga)); 2796 n = 0; 2797 2798 if (reserved_va) { 2799 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0; 2800 ga->bounds[n][1] = reserved_va; 2801 n++; 2802 /* LO_COMMPAGE and NULL handled by reserving from 0. */ 2803 } else { 2804 /* Add any LO_COMMPAGE or NULL page. */ 2805 if (LO_COMMPAGE != -1) { 2806 ga->bounds[n][0] = 0; 2807 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1; 2808 n++; 2809 } else if (!try_identity) { 2810 ga->bounds[n][0] = 0; 2811 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1; 2812 n++; 2813 } 2814 2815 /* Add the guest image for ET_EXEC. */ 2816 if (guest_loaddr) { 2817 ga->bounds[n][0] = guest_loaddr; 2818 ga->bounds[n][1] = guest_hiaddr; 2819 n++; 2820 } 2821 } 2822 2823 /* 2824 * Temporarily disable 2825 * "comparison is always false due to limited range of data type" 2826 * due to comparison between unsigned and (possible) 0. 2827 */ 2828 #pragma GCC diagnostic push 2829 #pragma GCC diagnostic ignored "-Wtype-limits" 2830 2831 /* Add any HI_COMMPAGE not covered by reserved_va. */ 2832 if (reserved_va < HI_COMMPAGE) { 2833 ga->bounds[n][0] = HI_COMMPAGE & qemu_real_host_page_mask(); 2834 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1; 2835 n++; 2836 } 2837 2838 #pragma GCC diagnostic pop 2839 2840 ga->nbounds = n; 2841 return true; 2842 } 2843 2844 static void pgb_fail_in_use(const char *image_name) 2845 { 2846 error_report("%s: requires virtual address space that is in use " 2847 "(omit the -B option or choose a different value)", 2848 image_name); 2849 exit(EXIT_FAILURE); 2850 } 2851 2852 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr, 2853 uintptr_t guest_hiaddr, uintptr_t align) 2854 { 2855 PGBAddrs ga; 2856 uintptr_t brk = (uintptr_t)sbrk(0); 2857 2858 if (!QEMU_IS_ALIGNED(guest_base, align)) { 2859 fprintf(stderr, "Requested guest base %p does not satisfy " 2860 "host minimum alignment (0x%" PRIxPTR ")\n", 2861 (void *)guest_base, align); 2862 exit(EXIT_FAILURE); 2863 } 2864 2865 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base) 2866 || !pgb_try_mmap_set(&ga, guest_base, brk)) { 2867 pgb_fail_in_use(image_name); 2868 } 2869 } 2870 2871 /** 2872 * pgb_find_fallback: 2873 * 2874 * This is a fallback method for finding holes in the host address space 2875 * if we don't have the benefit of being able to access /proc/self/map. 2876 * It can potentially take a very long time as we can only dumbly iterate 2877 * up the host address space seeing if the allocation would work. 2878 */ 2879 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align, 2880 uintptr_t brk) 2881 { 2882 /* TODO: come up with a better estimate of how much to skip. */ 2883 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB; 2884 2885 for (uintptr_t base = skip; ; base += skip) { 2886 base = ROUND_UP(base, align); 2887 if (pgb_try_mmap_set(ga, base, brk)) { 2888 return base; 2889 } 2890 if (base >= -skip) { 2891 return -1; 2892 } 2893 } 2894 } 2895 2896 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base, 2897 IntervalTreeRoot *root) 2898 { 2899 for (int i = ga->nbounds - 1; i >= 0; --i) { 2900 uintptr_t s = base + ga->bounds[i][0]; 2901 uintptr_t l = base + ga->bounds[i][1]; 2902 IntervalTreeNode *n; 2903 2904 if (l < s) { 2905 /* Wraparound. Skip to advance S to mmap_min_addr. */ 2906 return mmap_min_addr - s; 2907 } 2908 2909 n = interval_tree_iter_first(root, s, l); 2910 if (n != NULL) { 2911 /* Conflict. Skip to advance S to LAST + 1. */ 2912 return n->last - s + 1; 2913 } 2914 } 2915 return 0; /* success */ 2916 } 2917 2918 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root, 2919 uintptr_t align, uintptr_t brk) 2920 { 2921 uintptr_t last = sizeof(uintptr_t) == 4 ? MiB : GiB; 2922 uintptr_t base, skip; 2923 2924 while (true) { 2925 base = ROUND_UP(last, align); 2926 if (base < last) { 2927 return -1; 2928 } 2929 2930 skip = pgb_try_itree(ga, base, root); 2931 if (skip == 0) { 2932 break; 2933 } 2934 2935 last = base + skip; 2936 if (last < base) { 2937 return -1; 2938 } 2939 } 2940 2941 /* 2942 * We've chosen 'base' based on holes in the interval tree, 2943 * but we don't yet know if it is a valid host address. 2944 * Because it is the first matching hole, if the host addresses 2945 * are invalid we know there are no further matches. 2946 */ 2947 return pgb_try_mmap_set(ga, base, brk) ? base : -1; 2948 } 2949 2950 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr, 2951 uintptr_t guest_hiaddr, uintptr_t align) 2952 { 2953 IntervalTreeRoot *root; 2954 uintptr_t brk, ret; 2955 PGBAddrs ga; 2956 2957 /* Try the identity map first. */ 2958 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) { 2959 brk = (uintptr_t)sbrk(0); 2960 if (pgb_try_mmap_set(&ga, 0, brk)) { 2961 guest_base = 0; 2962 return; 2963 } 2964 } 2965 2966 /* 2967 * Rebuild the address set for non-identity map. 2968 * This differs in the mapping of the guest NULL page. 2969 */ 2970 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false); 2971 2972 root = read_self_maps(); 2973 2974 /* Read brk after we've read the maps, which will malloc. */ 2975 brk = (uintptr_t)sbrk(0); 2976 2977 if (!root) { 2978 ret = pgb_find_fallback(&ga, align, brk); 2979 } else { 2980 /* 2981 * Reserve the area close to the host brk. 2982 * This will be freed with the rest of the tree. 2983 */ 2984 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1); 2985 b->start = brk; 2986 b->last = brk + 16 * MiB - 1; 2987 interval_tree_insert(b, root); 2988 2989 ret = pgb_find_itree(&ga, root, align, brk); 2990 free_self_maps(root); 2991 } 2992 2993 if (ret == -1) { 2994 int w = TARGET_LONG_BITS / 4; 2995 2996 error_report("%s: Unable to find a guest_base to satisfy all " 2997 "guest address mapping requirements", image_name); 2998 2999 for (int i = 0; i < ga.nbounds; ++i) { 3000 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n", 3001 w, (uint64_t)ga.bounds[i][0], 3002 w, (uint64_t)ga.bounds[i][1]); 3003 } 3004 exit(EXIT_FAILURE); 3005 } 3006 guest_base = ret; 3007 } 3008 3009 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, 3010 abi_ulong guest_hiaddr) 3011 { 3012 /* In order to use host shmat, we must be able to honor SHMLBA. */ 3013 uintptr_t align = MAX(SHMLBA, TARGET_PAGE_SIZE); 3014 3015 /* Sanity check the guest binary. */ 3016 if (reserved_va) { 3017 if (guest_hiaddr > reserved_va) { 3018 error_report("%s: requires more than reserved virtual " 3019 "address space (0x%" PRIx64 " > 0x%lx)", 3020 image_name, (uint64_t)guest_hiaddr, reserved_va); 3021 exit(EXIT_FAILURE); 3022 } 3023 } else { 3024 if (guest_hiaddr != (uintptr_t)guest_hiaddr) { 3025 error_report("%s: requires more virtual address space " 3026 "than the host can provide (0x%" PRIx64 ")", 3027 image_name, (uint64_t)guest_hiaddr + 1); 3028 exit(EXIT_FAILURE); 3029 } 3030 } 3031 3032 if (have_guest_base) { 3033 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align); 3034 } else { 3035 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align); 3036 } 3037 3038 /* Reserve and initialize the commpage. */ 3039 if (!init_guest_commpage()) { 3040 /* We have already probed for the commpage being free. */ 3041 g_assert_not_reached(); 3042 } 3043 3044 assert(QEMU_IS_ALIGNED(guest_base, align)); 3045 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space " 3046 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base); 3047 } 3048 3049 enum { 3050 /* The string "GNU\0" as a magic number. */ 3051 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16), 3052 NOTE_DATA_SZ = 1 * KiB, 3053 NOTE_NAME_SZ = 4, 3054 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8, 3055 }; 3056 3057 /* 3058 * Process a single gnu_property entry. 3059 * Return false for error. 3060 */ 3061 static bool parse_elf_property(const uint32_t *data, int *off, int datasz, 3062 struct image_info *info, bool have_prev_type, 3063 uint32_t *prev_type, Error **errp) 3064 { 3065 uint32_t pr_type, pr_datasz, step; 3066 3067 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) { 3068 goto error_data; 3069 } 3070 datasz -= *off; 3071 data += *off / sizeof(uint32_t); 3072 3073 if (datasz < 2 * sizeof(uint32_t)) { 3074 goto error_data; 3075 } 3076 pr_type = data[0]; 3077 pr_datasz = data[1]; 3078 data += 2; 3079 datasz -= 2 * sizeof(uint32_t); 3080 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN); 3081 if (step > datasz) { 3082 goto error_data; 3083 } 3084 3085 /* Properties are supposed to be unique and sorted on pr_type. */ 3086 if (have_prev_type && pr_type <= *prev_type) { 3087 if (pr_type == *prev_type) { 3088 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY"); 3089 } else { 3090 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY"); 3091 } 3092 return false; 3093 } 3094 *prev_type = pr_type; 3095 3096 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) { 3097 return false; 3098 } 3099 3100 *off += 2 * sizeof(uint32_t) + step; 3101 return true; 3102 3103 error_data: 3104 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY"); 3105 return false; 3106 } 3107 3108 /* Process NT_GNU_PROPERTY_TYPE_0. */ 3109 static bool parse_elf_properties(const ImageSource *src, 3110 struct image_info *info, 3111 const struct elf_phdr *phdr, 3112 Error **errp) 3113 { 3114 union { 3115 struct elf_note nhdr; 3116 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)]; 3117 } note; 3118 3119 int n, off, datasz; 3120 bool have_prev_type; 3121 uint32_t prev_type; 3122 3123 /* Unless the arch requires properties, ignore them. */ 3124 if (!ARCH_USE_GNU_PROPERTY) { 3125 return true; 3126 } 3127 3128 /* If the properties are crazy large, that's too bad. */ 3129 n = phdr->p_filesz; 3130 if (n > sizeof(note)) { 3131 error_setg(errp, "PT_GNU_PROPERTY too large"); 3132 return false; 3133 } 3134 if (n < sizeof(note.nhdr)) { 3135 error_setg(errp, "PT_GNU_PROPERTY too small"); 3136 return false; 3137 } 3138 3139 if (!imgsrc_read(¬e, phdr->p_offset, n, src, errp)) { 3140 return false; 3141 } 3142 3143 /* 3144 * The contents of a valid PT_GNU_PROPERTY is a sequence of uint32_t. 3145 * Swap most of them now, beyond the header and namesz. 3146 */ 3147 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN 3148 for (int i = 4; i < n / 4; i++) { 3149 bswap32s(note.data + i); 3150 } 3151 #endif 3152 3153 /* 3154 * Note that nhdr is 3 words, and that the "name" described by namesz 3155 * immediately follows nhdr and is thus at the 4th word. Further, all 3156 * of the inputs to the kernel's round_up are multiples of 4. 3157 */ 3158 if (tswap32(note.nhdr.n_type) != NT_GNU_PROPERTY_TYPE_0 || 3159 tswap32(note.nhdr.n_namesz) != NOTE_NAME_SZ || 3160 note.data[3] != GNU0_MAGIC) { 3161 error_setg(errp, "Invalid note in PT_GNU_PROPERTY"); 3162 return false; 3163 } 3164 off = sizeof(note.nhdr) + NOTE_NAME_SZ; 3165 3166 datasz = tswap32(note.nhdr.n_descsz) + off; 3167 if (datasz > n) { 3168 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY"); 3169 return false; 3170 } 3171 3172 have_prev_type = false; 3173 prev_type = 0; 3174 while (1) { 3175 if (off == datasz) { 3176 return true; /* end, exit ok */ 3177 } 3178 if (!parse_elf_property(note.data, &off, datasz, info, 3179 have_prev_type, &prev_type, errp)) { 3180 return false; 3181 } 3182 have_prev_type = true; 3183 } 3184 } 3185 3186 /** 3187 * load_elf_image: Load an ELF image into the address space. 3188 * @image_name: the filename of the image, to use in error messages. 3189 * @src: the ImageSource from which to read. 3190 * @info: info collected from the loaded image. 3191 * @ehdr: the ELF header, not yet bswapped. 3192 * @pinterp_name: record any PT_INTERP string found. 3193 * 3194 * On return: @info values will be filled in, as necessary or available. 3195 */ 3196 3197 static void load_elf_image(const char *image_name, const ImageSource *src, 3198 struct image_info *info, struct elfhdr *ehdr, 3199 char **pinterp_name) 3200 { 3201 g_autofree struct elf_phdr *phdr = NULL; 3202 abi_ulong load_addr, load_bias, loaddr, hiaddr, error, align; 3203 size_t reserve_size, align_size; 3204 int i, prot_exec; 3205 Error *err = NULL; 3206 3207 /* 3208 * First of all, some simple consistency checks. 3209 * Note that we rely on the bswapped ehdr staying in bprm_buf, 3210 * for later use by load_elf_binary and create_elf_tables. 3211 */ 3212 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) { 3213 goto exit_errmsg; 3214 } 3215 if (!elf_check_ident(ehdr)) { 3216 error_setg(&err, "Invalid ELF image for this architecture"); 3217 goto exit_errmsg; 3218 } 3219 bswap_ehdr(ehdr); 3220 if (!elf_check_ehdr(ehdr)) { 3221 error_setg(&err, "Invalid ELF image for this architecture"); 3222 goto exit_errmsg; 3223 } 3224 3225 phdr = imgsrc_read_alloc(ehdr->e_phoff, 3226 ehdr->e_phnum * sizeof(struct elf_phdr), 3227 src, &err); 3228 if (phdr == NULL) { 3229 goto exit_errmsg; 3230 } 3231 bswap_phdr(phdr, ehdr->e_phnum); 3232 3233 info->nsegs = 0; 3234 info->pt_dynamic_addr = 0; 3235 3236 mmap_lock(); 3237 3238 /* 3239 * Find the maximum size of the image and allocate an appropriate 3240 * amount of memory to handle that. Locate the interpreter, if any. 3241 */ 3242 loaddr = -1, hiaddr = 0; 3243 align = 0; 3244 info->exec_stack = EXSTACK_DEFAULT; 3245 for (i = 0; i < ehdr->e_phnum; ++i) { 3246 struct elf_phdr *eppnt = phdr + i; 3247 if (eppnt->p_type == PT_LOAD) { 3248 abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK; 3249 if (a < loaddr) { 3250 loaddr = a; 3251 } 3252 a = eppnt->p_vaddr + eppnt->p_memsz - 1; 3253 if (a > hiaddr) { 3254 hiaddr = a; 3255 } 3256 ++info->nsegs; 3257 align |= eppnt->p_align; 3258 } else if (eppnt->p_type == PT_INTERP && pinterp_name) { 3259 g_autofree char *interp_name = NULL; 3260 3261 if (*pinterp_name) { 3262 error_setg(&err, "Multiple PT_INTERP entries"); 3263 goto exit_errmsg; 3264 } 3265 3266 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz, 3267 src, &err); 3268 if (interp_name == NULL) { 3269 goto exit_errmsg; 3270 } 3271 if (interp_name[eppnt->p_filesz - 1] != 0) { 3272 error_setg(&err, "Invalid PT_INTERP entry"); 3273 goto exit_errmsg; 3274 } 3275 *pinterp_name = g_steal_pointer(&interp_name); 3276 } else if (eppnt->p_type == PT_GNU_PROPERTY) { 3277 if (!parse_elf_properties(src, info, eppnt, &err)) { 3278 goto exit_errmsg; 3279 } 3280 } else if (eppnt->p_type == PT_GNU_STACK) { 3281 info->exec_stack = eppnt->p_flags & PF_X; 3282 } 3283 } 3284 3285 load_addr = loaddr; 3286 3287 align = pow2ceil(align); 3288 3289 if (pinterp_name != NULL) { 3290 if (ehdr->e_type == ET_EXEC) { 3291 /* 3292 * Make sure that the low address does not conflict with 3293 * MMAP_MIN_ADDR or the QEMU application itself. 3294 */ 3295 probe_guest_base(image_name, loaddr, hiaddr); 3296 } else { 3297 /* 3298 * The binary is dynamic, but we still need to 3299 * select guest_base. In this case we pass a size. 3300 */ 3301 probe_guest_base(image_name, 0, hiaddr - loaddr); 3302 3303 /* 3304 * Avoid collision with the loader by providing a different 3305 * default load address. 3306 */ 3307 load_addr += elf_et_dyn_base; 3308 3309 /* 3310 * TODO: Better support for mmap alignment is desirable. 3311 * Since we do not have complete control over the guest 3312 * address space, we prefer the kernel to choose some address 3313 * rather than force the use of LOAD_ADDR via MAP_FIXED. 3314 */ 3315 if (align) { 3316 load_addr &= -align; 3317 } 3318 } 3319 } 3320 3321 /* 3322 * Reserve address space for all of this. 3323 * 3324 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get 3325 * exactly the address range that is required. Without reserved_va, 3326 * the guest address space is not isolated. We have attempted to avoid 3327 * conflict with the host program itself via probe_guest_base, but using 3328 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check. 3329 * 3330 * Otherwise this is ET_DYN, and we are searching for a location 3331 * that can hold the memory space required. If the image is 3332 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should 3333 * honor that address if it happens to be free. 3334 * 3335 * In both cases, we will overwrite pages in this range with mappings 3336 * from the executable. 3337 */ 3338 reserve_size = (size_t)hiaddr - loaddr + 1; 3339 align_size = reserve_size; 3340 3341 if (ehdr->e_type != ET_EXEC && align > qemu_real_host_page_size()) { 3342 align_size += align - 1; 3343 } 3344 3345 load_addr = target_mmap(load_addr, align_size, PROT_NONE, 3346 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | 3347 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), 3348 -1, 0); 3349 if (load_addr == -1) { 3350 goto exit_mmap; 3351 } 3352 3353 if (align_size != reserve_size) { 3354 abi_ulong align_addr = ROUND_UP(load_addr, align); 3355 abi_ulong align_end = TARGET_PAGE_ALIGN(align_addr + reserve_size); 3356 abi_ulong load_end = TARGET_PAGE_ALIGN(load_addr + align_size); 3357 3358 if (align_addr != load_addr) { 3359 target_munmap(load_addr, align_addr - load_addr); 3360 } 3361 if (align_end != load_end) { 3362 target_munmap(align_end, load_end - align_end); 3363 } 3364 load_addr = align_addr; 3365 } 3366 3367 load_bias = load_addr - loaddr; 3368 3369 if (elf_is_fdpic(ehdr)) { 3370 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = 3371 g_malloc(sizeof(*loadsegs) * info->nsegs); 3372 3373 for (i = 0; i < ehdr->e_phnum; ++i) { 3374 switch (phdr[i].p_type) { 3375 case PT_DYNAMIC: 3376 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias; 3377 break; 3378 case PT_LOAD: 3379 loadsegs->addr = phdr[i].p_vaddr + load_bias; 3380 loadsegs->p_vaddr = phdr[i].p_vaddr; 3381 loadsegs->p_memsz = phdr[i].p_memsz; 3382 ++loadsegs; 3383 break; 3384 } 3385 } 3386 } 3387 3388 info->load_bias = load_bias; 3389 info->code_offset = load_bias; 3390 info->data_offset = load_bias; 3391 info->load_addr = load_addr; 3392 info->entry = ehdr->e_entry + load_bias; 3393 info->start_code = -1; 3394 info->end_code = 0; 3395 info->start_data = -1; 3396 info->end_data = 0; 3397 /* Usual start for brk is after all sections of the main executable. */ 3398 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias); 3399 info->elf_flags = ehdr->e_flags; 3400 3401 prot_exec = PROT_EXEC; 3402 #ifdef TARGET_AARCH64 3403 /* 3404 * If the BTI feature is present, this indicates that the executable 3405 * pages of the startup binary should be mapped with PROT_BTI, so that 3406 * branch targets are enforced. 3407 * 3408 * The startup binary is either the interpreter or the static executable. 3409 * The interpreter is responsible for all pages of a dynamic executable. 3410 * 3411 * Elf notes are backward compatible to older cpus. 3412 * Do not enable BTI unless it is supported. 3413 */ 3414 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) 3415 && (pinterp_name == NULL || *pinterp_name == 0) 3416 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) { 3417 prot_exec |= TARGET_PROT_BTI; 3418 } 3419 #endif 3420 3421 for (i = 0; i < ehdr->e_phnum; i++) { 3422 struct elf_phdr *eppnt = phdr + i; 3423 if (eppnt->p_type == PT_LOAD) { 3424 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; 3425 int elf_prot = 0; 3426 3427 if (eppnt->p_flags & PF_R) { 3428 elf_prot |= PROT_READ; 3429 } 3430 if (eppnt->p_flags & PF_W) { 3431 elf_prot |= PROT_WRITE; 3432 } 3433 if (eppnt->p_flags & PF_X) { 3434 elf_prot |= prot_exec; 3435 } 3436 3437 vaddr = load_bias + eppnt->p_vaddr; 3438 vaddr_po = vaddr & ~TARGET_PAGE_MASK; 3439 vaddr_ps = vaddr & TARGET_PAGE_MASK; 3440 3441 vaddr_ef = vaddr + eppnt->p_filesz; 3442 vaddr_em = vaddr + eppnt->p_memsz; 3443 3444 /* 3445 * Some segments may be completely empty, with a non-zero p_memsz 3446 * but no backing file segment. 3447 */ 3448 if (eppnt->p_filesz != 0) { 3449 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, 3450 elf_prot, MAP_PRIVATE | MAP_FIXED, 3451 src, eppnt->p_offset - vaddr_po); 3452 if (error == -1) { 3453 goto exit_mmap; 3454 } 3455 } 3456 3457 /* If the load segment requests extra zeros (e.g. bss), map it. */ 3458 if (vaddr_ef < vaddr_em && 3459 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) { 3460 goto exit_errmsg; 3461 } 3462 3463 /* Find the full program boundaries. */ 3464 if (elf_prot & PROT_EXEC) { 3465 if (vaddr < info->start_code) { 3466 info->start_code = vaddr; 3467 } 3468 if (vaddr_ef > info->end_code) { 3469 info->end_code = vaddr_ef; 3470 } 3471 } 3472 if (elf_prot & PROT_WRITE) { 3473 if (vaddr < info->start_data) { 3474 info->start_data = vaddr; 3475 } 3476 if (vaddr_ef > info->end_data) { 3477 info->end_data = vaddr_ef; 3478 } 3479 } 3480 #ifdef TARGET_MIPS 3481 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { 3482 Mips_elf_abiflags_v0 abiflags; 3483 3484 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags), 3485 src, &err)) { 3486 goto exit_errmsg; 3487 } 3488 bswap_mips_abiflags(&abiflags); 3489 info->fp_abi = abiflags.fp_abi; 3490 #endif 3491 } 3492 } 3493 3494 if (info->end_data == 0) { 3495 info->start_data = info->end_code; 3496 info->end_data = info->end_code; 3497 } 3498 3499 if (qemu_log_enabled()) { 3500 load_symbols(ehdr, src, load_bias); 3501 } 3502 3503 debuginfo_report_elf(image_name, src->fd, load_bias); 3504 3505 mmap_unlock(); 3506 3507 close(src->fd); 3508 return; 3509 3510 exit_mmap: 3511 error_setg_errno(&err, errno, "Error mapping file"); 3512 goto exit_errmsg; 3513 exit_errmsg: 3514 error_reportf_err(err, "%s: ", image_name); 3515 exit(-1); 3516 } 3517 3518 static void load_elf_interp(const char *filename, struct image_info *info, 3519 char bprm_buf[BPRM_BUF_SIZE]) 3520 { 3521 struct elfhdr ehdr; 3522 ImageSource src; 3523 int fd, retval; 3524 Error *err = NULL; 3525 3526 fd = open(path(filename), O_RDONLY); 3527 if (fd < 0) { 3528 error_setg_file_open(&err, errno, filename); 3529 error_report_err(err); 3530 exit(-1); 3531 } 3532 3533 retval = read(fd, bprm_buf, BPRM_BUF_SIZE); 3534 if (retval < 0) { 3535 error_setg_errno(&err, errno, "Error reading file header"); 3536 error_reportf_err(err, "%s: ", filename); 3537 exit(-1); 3538 } 3539 3540 src.fd = fd; 3541 src.cache = bprm_buf; 3542 src.cache_size = retval; 3543 3544 load_elf_image(filename, &src, info, &ehdr, NULL); 3545 } 3546 3547 #ifndef vdso_image_info 3548 #ifdef VDSO_HEADER 3549 #include VDSO_HEADER 3550 #define vdso_image_info(flags) &vdso_image_info 3551 #else 3552 #define vdso_image_info(flags) NULL 3553 #endif /* VDSO_HEADER */ 3554 #endif /* vdso_image_info */ 3555 3556 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso) 3557 { 3558 ImageSource src; 3559 struct elfhdr ehdr; 3560 abi_ulong load_bias, load_addr; 3561 3562 src.fd = -1; 3563 src.cache = vdso->image; 3564 src.cache_size = vdso->image_size; 3565 3566 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL); 3567 load_addr = info->load_addr; 3568 load_bias = info->load_bias; 3569 3570 /* 3571 * We need to relocate the VDSO image. The one built into the kernel 3572 * is built for a fixed address. The one built for QEMU is not, since 3573 * that requires close control of the guest address space. 3574 * We pre-processed the image to locate all of the addresses that need 3575 * to be updated. 3576 */ 3577 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) { 3578 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]); 3579 *addr = tswapal(tswapal(*addr) + load_bias); 3580 } 3581 3582 /* Install signal trampolines, if present. */ 3583 if (vdso->sigreturn_ofs) { 3584 default_sigreturn = load_addr + vdso->sigreturn_ofs; 3585 } 3586 if (vdso->rt_sigreturn_ofs) { 3587 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs; 3588 } 3589 3590 /* Remove write from VDSO segment. */ 3591 target_mprotect(info->start_data, info->end_data - info->start_data, 3592 PROT_READ | PROT_EXEC); 3593 } 3594 3595 static int symfind(const void *s0, const void *s1) 3596 { 3597 struct elf_sym *sym = (struct elf_sym *)s1; 3598 __typeof(sym->st_value) addr = *(uint64_t *)s0; 3599 int result = 0; 3600 3601 if (addr < sym->st_value) { 3602 result = -1; 3603 } else if (addr >= sym->st_value + sym->st_size) { 3604 result = 1; 3605 } 3606 return result; 3607 } 3608 3609 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr) 3610 { 3611 #if ELF_CLASS == ELFCLASS32 3612 struct elf_sym *syms = s->disas_symtab.elf32; 3613 #else 3614 struct elf_sym *syms = s->disas_symtab.elf64; 3615 #endif 3616 3617 // binary search 3618 struct elf_sym *sym; 3619 3620 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); 3621 if (sym != NULL) { 3622 return s->disas_strtab + sym->st_name; 3623 } 3624 3625 return ""; 3626 } 3627 3628 /* FIXME: This should use elf_ops.h.inc */ 3629 static int symcmp(const void *s0, const void *s1) 3630 { 3631 struct elf_sym *sym0 = (struct elf_sym *)s0; 3632 struct elf_sym *sym1 = (struct elf_sym *)s1; 3633 return (sym0->st_value < sym1->st_value) 3634 ? -1 3635 : ((sym0->st_value > sym1->st_value) ? 1 : 0); 3636 } 3637 3638 /* Best attempt to load symbols from this ELF object. */ 3639 static void load_symbols(struct elfhdr *hdr, const ImageSource *src, 3640 abi_ulong load_bias) 3641 { 3642 int i, shnum, nsyms, sym_idx = 0, str_idx = 0; 3643 g_autofree struct elf_shdr *shdr = NULL; 3644 char *strings = NULL; 3645 struct elf_sym *syms = NULL; 3646 struct elf_sym *new_syms; 3647 uint64_t segsz; 3648 3649 shnum = hdr->e_shnum; 3650 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr), 3651 src, NULL); 3652 if (shdr == NULL) { 3653 return; 3654 } 3655 3656 bswap_shdr(shdr, shnum); 3657 for (i = 0; i < shnum; ++i) { 3658 if (shdr[i].sh_type == SHT_SYMTAB) { 3659 sym_idx = i; 3660 str_idx = shdr[i].sh_link; 3661 goto found; 3662 } 3663 } 3664 3665 /* There will be no symbol table if the file was stripped. */ 3666 return; 3667 3668 found: 3669 /* Now know where the strtab and symtab are. Snarf them. */ 3670 3671 segsz = shdr[str_idx].sh_size; 3672 strings = g_try_malloc(segsz); 3673 if (!strings) { 3674 goto give_up; 3675 } 3676 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) { 3677 goto give_up; 3678 } 3679 3680 segsz = shdr[sym_idx].sh_size; 3681 if (segsz / sizeof(struct elf_sym) > INT_MAX) { 3682 /* 3683 * Implausibly large symbol table: give up rather than ploughing 3684 * on with the number of symbols calculation overflowing. 3685 */ 3686 goto give_up; 3687 } 3688 nsyms = segsz / sizeof(struct elf_sym); 3689 syms = g_try_malloc(segsz); 3690 if (!syms) { 3691 goto give_up; 3692 } 3693 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) { 3694 goto give_up; 3695 } 3696 3697 for (i = 0; i < nsyms; ) { 3698 bswap_sym(syms + i); 3699 /* Throw away entries which we do not need. */ 3700 if (syms[i].st_shndx == SHN_UNDEF 3701 || syms[i].st_shndx >= SHN_LORESERVE 3702 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { 3703 if (i < --nsyms) { 3704 syms[i] = syms[nsyms]; 3705 } 3706 } else { 3707 #if defined(TARGET_ARM) || defined (TARGET_MIPS) 3708 /* The bottom address bit marks a Thumb or MIPS16 symbol. */ 3709 syms[i].st_value &= ~(target_ulong)1; 3710 #endif 3711 syms[i].st_value += load_bias; 3712 i++; 3713 } 3714 } 3715 3716 /* No "useful" symbol. */ 3717 if (nsyms == 0) { 3718 goto give_up; 3719 } 3720 3721 /* 3722 * Attempt to free the storage associated with the local symbols 3723 * that we threw away. Whether or not this has any effect on the 3724 * memory allocation depends on the malloc implementation and how 3725 * many symbols we managed to discard. 3726 */ 3727 new_syms = g_try_renew(struct elf_sym, syms, nsyms); 3728 if (new_syms == NULL) { 3729 goto give_up; 3730 } 3731 syms = new_syms; 3732 3733 qsort(syms, nsyms, sizeof(*syms), symcmp); 3734 3735 { 3736 struct syminfo *s = g_new(struct syminfo, 1); 3737 3738 s->disas_strtab = strings; 3739 s->disas_num_syms = nsyms; 3740 #if ELF_CLASS == ELFCLASS32 3741 s->disas_symtab.elf32 = syms; 3742 #else 3743 s->disas_symtab.elf64 = syms; 3744 #endif 3745 s->lookup_symbol = lookup_symbolxx; 3746 s->next = syminfos; 3747 syminfos = s; 3748 } 3749 return; 3750 3751 give_up: 3752 g_free(strings); 3753 g_free(syms); 3754 } 3755 3756 uint32_t get_elf_eflags(int fd) 3757 { 3758 struct elfhdr ehdr; 3759 off_t offset; 3760 int ret; 3761 3762 /* Read ELF header */ 3763 offset = lseek(fd, 0, SEEK_SET); 3764 if (offset == (off_t) -1) { 3765 return 0; 3766 } 3767 ret = read(fd, &ehdr, sizeof(ehdr)); 3768 if (ret < sizeof(ehdr)) { 3769 return 0; 3770 } 3771 offset = lseek(fd, offset, SEEK_SET); 3772 if (offset == (off_t) -1) { 3773 return 0; 3774 } 3775 3776 /* Check ELF signature */ 3777 if (!elf_check_ident(&ehdr)) { 3778 return 0; 3779 } 3780 3781 /* check header */ 3782 bswap_ehdr(&ehdr); 3783 if (!elf_check_ehdr(&ehdr)) { 3784 return 0; 3785 } 3786 3787 /* return architecture id */ 3788 return ehdr.e_flags; 3789 } 3790 3791 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) 3792 { 3793 /* 3794 * We need a copy of the elf header for passing to create_elf_tables. 3795 * We will have overwritten the original when we re-use bprm->buf 3796 * while loading the interpreter. Allocate the storage for this now 3797 * and let elf_load_image do any swapping that may be required. 3798 */ 3799 struct elfhdr ehdr; 3800 struct image_info interp_info, vdso_info; 3801 char *elf_interpreter = NULL; 3802 char *scratch; 3803 3804 memset(&interp_info, 0, sizeof(interp_info)); 3805 #ifdef TARGET_MIPS 3806 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN; 3807 #endif 3808 3809 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter); 3810 3811 /* Do this so that we can load the interpreter, if need be. We will 3812 change some of these later */ 3813 bprm->p = setup_arg_pages(bprm, info); 3814 3815 scratch = g_new0(char, TARGET_PAGE_SIZE); 3816 if (STACK_GROWS_DOWN) { 3817 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 3818 bprm->p, info->stack_limit); 3819 info->file_string = bprm->p; 3820 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 3821 bprm->p, info->stack_limit); 3822 info->env_strings = bprm->p; 3823 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 3824 bprm->p, info->stack_limit); 3825 info->arg_strings = bprm->p; 3826 } else { 3827 info->arg_strings = bprm->p; 3828 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 3829 bprm->p, info->stack_limit); 3830 info->env_strings = bprm->p; 3831 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 3832 bprm->p, info->stack_limit); 3833 info->file_string = bprm->p; 3834 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 3835 bprm->p, info->stack_limit); 3836 } 3837 3838 g_free(scratch); 3839 3840 if (!bprm->p) { 3841 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); 3842 exit(-1); 3843 } 3844 3845 if (elf_interpreter) { 3846 load_elf_interp(elf_interpreter, &interp_info, bprm->buf); 3847 3848 /* 3849 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky 3850 * with the mappings the interpreter can be loaded above but 3851 * near the main executable, which can leave very little room 3852 * for the heap. 3853 * If the current brk has less than 16MB, use the end of the 3854 * interpreter. 3855 */ 3856 if (interp_info.brk > info->brk && 3857 interp_info.load_bias - info->brk < 16 * MiB) { 3858 info->brk = interp_info.brk; 3859 } 3860 3861 /* If the program interpreter is one of these two, then assume 3862 an iBCS2 image. Otherwise assume a native linux image. */ 3863 3864 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 3865 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) { 3866 info->personality = PER_SVR4; 3867 3868 /* Why this, you ask??? Well SVr4 maps page 0 as read-only, 3869 and some applications "depend" upon this behavior. Since 3870 we do not have the power to recompile these, we emulate 3871 the SVr4 behavior. Sigh. */ 3872 target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC, 3873 MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, 3874 -1, 0); 3875 } 3876 #ifdef TARGET_MIPS 3877 info->interp_fp_abi = interp_info.fp_abi; 3878 #endif 3879 } 3880 3881 /* 3882 * Load a vdso if available, which will amongst other things contain the 3883 * signal trampolines. Otherwise, allocate a separate page for them. 3884 */ 3885 const VdsoImageInfo *vdso = vdso_image_info(info->elf_flags); 3886 if (vdso) { 3887 load_elf_vdso(&vdso_info, vdso); 3888 info->vdso = vdso_info.load_bias; 3889 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { 3890 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE, 3891 PROT_READ | PROT_WRITE, 3892 MAP_PRIVATE | MAP_ANON, -1, 0); 3893 if (tramp_page == -1) { 3894 return -errno; 3895 } 3896 3897 setup_sigtramp(tramp_page); 3898 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); 3899 } 3900 3901 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, 3902 elf_interpreter ? &interp_info : NULL, 3903 vdso ? &vdso_info : NULL); 3904 info->start_stack = bprm->p; 3905 3906 /* If we have an interpreter, set that as the program's entry point. 3907 Copy the load_bias as well, to help PPC64 interpret the entry 3908 point as a function descriptor. Do this after creating elf tables 3909 so that we copy the original program entry point into the AUXV. */ 3910 if (elf_interpreter) { 3911 info->load_bias = interp_info.load_bias; 3912 info->entry = interp_info.entry; 3913 g_free(elf_interpreter); 3914 } 3915 3916 #ifdef USE_ELF_CORE_DUMP 3917 bprm->core_dump = &elf_core_dump; 3918 #endif 3919 3920 return 0; 3921 } 3922 3923 #ifdef USE_ELF_CORE_DUMP 3924 3925 /* 3926 * Definitions to generate Intel SVR4-like core files. 3927 * These mostly have the same names as the SVR4 types with "target_elf_" 3928 * tacked on the front to prevent clashes with linux definitions, 3929 * and the typedef forms have been avoided. This is mostly like 3930 * the SVR4 structure, but more Linuxy, with things that Linux does 3931 * not support and which gdb doesn't really use excluded. 3932 * 3933 * Fields we don't dump (their contents is zero) in linux-user qemu 3934 * are marked with XXX. 3935 * 3936 * Core dump code is copied from linux kernel (fs/binfmt_elf.c). 3937 * 3938 * Porting ELF coredump for target is (quite) simple process. First you 3939 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for 3940 * the target resides): 3941 * 3942 * #define USE_ELF_CORE_DUMP 3943 * 3944 * Next you define type of register set used for dumping. ELF specification 3945 * says that it needs to be array of elf_greg_t that has size of ELF_NREG. 3946 * 3947 * typedef <target_regtype> target_elf_greg_t; 3948 * #define ELF_NREG <number of registers> 3949 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG]; 3950 * 3951 * Last step is to implement target specific function that copies registers 3952 * from given cpu into just specified register set. Prototype is: 3953 * 3954 * static void elf_core_copy_regs(taret_elf_gregset_t *regs, 3955 * const CPUArchState *env); 3956 * 3957 * Parameters: 3958 * regs - copy register values into here (allocated and zeroed by caller) 3959 * env - copy registers from here 3960 * 3961 * Example for ARM target is provided in this file. 3962 */ 3963 3964 struct target_elf_siginfo { 3965 abi_int si_signo; /* signal number */ 3966 abi_int si_code; /* extra code */ 3967 abi_int si_errno; /* errno */ 3968 }; 3969 3970 struct target_elf_prstatus { 3971 struct target_elf_siginfo pr_info; /* Info associated with signal */ 3972 abi_short pr_cursig; /* Current signal */ 3973 abi_ulong pr_sigpend; /* XXX */ 3974 abi_ulong pr_sighold; /* XXX */ 3975 target_pid_t pr_pid; 3976 target_pid_t pr_ppid; 3977 target_pid_t pr_pgrp; 3978 target_pid_t pr_sid; 3979 struct target_timeval pr_utime; /* XXX User time */ 3980 struct target_timeval pr_stime; /* XXX System time */ 3981 struct target_timeval pr_cutime; /* XXX Cumulative user time */ 3982 struct target_timeval pr_cstime; /* XXX Cumulative system time */ 3983 target_elf_gregset_t pr_reg; /* GP registers */ 3984 abi_int pr_fpvalid; /* XXX */ 3985 }; 3986 3987 #define ELF_PRARGSZ (80) /* Number of chars for args */ 3988 3989 struct target_elf_prpsinfo { 3990 char pr_state; /* numeric process state */ 3991 char pr_sname; /* char for pr_state */ 3992 char pr_zomb; /* zombie */ 3993 char pr_nice; /* nice val */ 3994 abi_ulong pr_flag; /* flags */ 3995 target_uid_t pr_uid; 3996 target_gid_t pr_gid; 3997 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; 3998 /* Lots missing */ 3999 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */ 4000 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ 4001 }; 4002 4003 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN 4004 static void bswap_prstatus(struct target_elf_prstatus *prstatus) 4005 { 4006 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo); 4007 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code); 4008 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno); 4009 prstatus->pr_cursig = tswap16(prstatus->pr_cursig); 4010 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); 4011 prstatus->pr_sighold = tswapal(prstatus->pr_sighold); 4012 prstatus->pr_pid = tswap32(prstatus->pr_pid); 4013 prstatus->pr_ppid = tswap32(prstatus->pr_ppid); 4014 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp); 4015 prstatus->pr_sid = tswap32(prstatus->pr_sid); 4016 /* cpu times are not filled, so we skip them */ 4017 /* regs should be in correct format already */ 4018 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); 4019 } 4020 4021 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) 4022 { 4023 psinfo->pr_flag = tswapal(psinfo->pr_flag); 4024 psinfo->pr_uid = tswap16(psinfo->pr_uid); 4025 psinfo->pr_gid = tswap16(psinfo->pr_gid); 4026 psinfo->pr_pid = tswap32(psinfo->pr_pid); 4027 psinfo->pr_ppid = tswap32(psinfo->pr_ppid); 4028 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp); 4029 psinfo->pr_sid = tswap32(psinfo->pr_sid); 4030 } 4031 4032 static void bswap_note(struct elf_note *en) 4033 { 4034 bswap32s(&en->n_namesz); 4035 bswap32s(&en->n_descsz); 4036 bswap32s(&en->n_type); 4037 } 4038 #else 4039 static inline void bswap_prstatus(struct target_elf_prstatus *p) { } 4040 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} 4041 static inline void bswap_note(struct elf_note *en) { } 4042 #endif /* HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN */ 4043 4044 /* 4045 * Calculate file (dump) size of given memory region. 4046 */ 4047 static size_t vma_dump_size(target_ulong start, target_ulong end, 4048 unsigned long flags) 4049 { 4050 /* The area must be readable. */ 4051 if (!(flags & PAGE_READ)) { 4052 return 0; 4053 } 4054 4055 /* 4056 * Usually we don't dump executable pages as they contain 4057 * non-writable code that debugger can read directly from 4058 * target library etc. If there is no elf header, we dump it. 4059 */ 4060 if (!(flags & PAGE_WRITE_ORG) && 4061 (flags & PAGE_EXEC) && 4062 memcmp(g2h_untagged(start), ELFMAG, SELFMAG) == 0) { 4063 return 0; 4064 } 4065 4066 return end - start; 4067 } 4068 4069 static size_t size_note(const char *name, size_t datasz) 4070 { 4071 size_t namesz = strlen(name) + 1; 4072 4073 namesz = ROUND_UP(namesz, 4); 4074 datasz = ROUND_UP(datasz, 4); 4075 4076 return sizeof(struct elf_note) + namesz + datasz; 4077 } 4078 4079 static void *fill_note(void **pptr, int type, const char *name, size_t datasz) 4080 { 4081 void *ptr = *pptr; 4082 struct elf_note *n = ptr; 4083 size_t namesz = strlen(name) + 1; 4084 4085 n->n_namesz = namesz; 4086 n->n_descsz = datasz; 4087 n->n_type = type; 4088 bswap_note(n); 4089 4090 ptr += sizeof(*n); 4091 memcpy(ptr, name, namesz); 4092 4093 namesz = ROUND_UP(namesz, 4); 4094 datasz = ROUND_UP(datasz, 4); 4095 4096 *pptr = ptr + namesz + datasz; 4097 return ptr + namesz; 4098 } 4099 4100 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, 4101 uint32_t flags) 4102 { 4103 memcpy(elf->e_ident, ELFMAG, SELFMAG); 4104 4105 elf->e_ident[EI_CLASS] = ELF_CLASS; 4106 elf->e_ident[EI_DATA] = ELF_DATA; 4107 elf->e_ident[EI_VERSION] = EV_CURRENT; 4108 elf->e_ident[EI_OSABI] = ELF_OSABI; 4109 4110 elf->e_type = ET_CORE; 4111 elf->e_machine = machine; 4112 elf->e_version = EV_CURRENT; 4113 elf->e_phoff = sizeof(struct elfhdr); 4114 elf->e_flags = flags; 4115 elf->e_ehsize = sizeof(struct elfhdr); 4116 elf->e_phentsize = sizeof(struct elf_phdr); 4117 elf->e_phnum = segs; 4118 4119 bswap_ehdr(elf); 4120 } 4121 4122 static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) 4123 { 4124 phdr->p_type = PT_NOTE; 4125 phdr->p_offset = offset; 4126 phdr->p_filesz = sz; 4127 4128 bswap_phdr(phdr, 1); 4129 } 4130 4131 static void fill_prstatus_note(void *data, CPUState *cpu, int signr) 4132 { 4133 /* 4134 * Because note memory is only aligned to 4, and target_elf_prstatus 4135 * may well have higher alignment requirements, fill locally and 4136 * memcpy to the destination afterward. 4137 */ 4138 struct target_elf_prstatus prstatus = { 4139 .pr_info.si_signo = signr, 4140 .pr_cursig = signr, 4141 .pr_pid = get_task_state(cpu)->ts_tid, 4142 .pr_ppid = getppid(), 4143 .pr_pgrp = getpgrp(), 4144 .pr_sid = getsid(0), 4145 }; 4146 4147 elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); 4148 bswap_prstatus(&prstatus); 4149 memcpy(data, &prstatus, sizeof(prstatus)); 4150 } 4151 4152 static void fill_prpsinfo_note(void *data, const TaskState *ts) 4153 { 4154 /* 4155 * Because note memory is only aligned to 4, and target_elf_prpsinfo 4156 * may well have higher alignment requirements, fill locally and 4157 * memcpy to the destination afterward. 4158 */ 4159 struct target_elf_prpsinfo psinfo = { 4160 .pr_pid = getpid(), 4161 .pr_ppid = getppid(), 4162 .pr_pgrp = getpgrp(), 4163 .pr_sid = getsid(0), 4164 .pr_uid = getuid(), 4165 .pr_gid = getgid(), 4166 }; 4167 char *base_filename; 4168 size_t len; 4169 4170 len = ts->info->env_strings - ts->info->arg_strings; 4171 len = MIN(len, ELF_PRARGSZ); 4172 memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); 4173 for (size_t i = 0; i < len; i++) { 4174 if (psinfo.pr_psargs[i] == 0) { 4175 psinfo.pr_psargs[i] = ' '; 4176 } 4177 } 4178 4179 base_filename = g_path_get_basename(ts->bprm->filename); 4180 /* 4181 * Using strncpy here is fine: at max-length, 4182 * this field is not NUL-terminated. 4183 */ 4184 strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname)); 4185 g_free(base_filename); 4186 4187 bswap_psinfo(&psinfo); 4188 memcpy(data, &psinfo, sizeof(psinfo)); 4189 } 4190 4191 static void fill_auxv_note(void *data, const TaskState *ts) 4192 { 4193 memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len); 4194 } 4195 4196 /* 4197 * Constructs name of coredump file. We have following convention 4198 * for the name: 4199 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core 4200 * 4201 * Returns the filename 4202 */ 4203 static char *core_dump_filename(const TaskState *ts) 4204 { 4205 g_autoptr(GDateTime) now = g_date_time_new_now_local(); 4206 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S"); 4207 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename); 4208 4209 return g_strdup_printf("qemu_%s_%s_%d.core", 4210 base_filename, nowstr, (int)getpid()); 4211 } 4212 4213 static int dump_write(int fd, const void *ptr, size_t size) 4214 { 4215 const char *bufp = (const char *)ptr; 4216 ssize_t bytes_written, bytes_left; 4217 4218 bytes_written = 0; 4219 bytes_left = size; 4220 4221 /* 4222 * In normal conditions, single write(2) should do but 4223 * in case of socket etc. this mechanism is more portable. 4224 */ 4225 do { 4226 bytes_written = write(fd, bufp, bytes_left); 4227 if (bytes_written < 0) { 4228 if (errno == EINTR) 4229 continue; 4230 return (-1); 4231 } else if (bytes_written == 0) { /* eof */ 4232 return (-1); 4233 } 4234 bufp += bytes_written; 4235 bytes_left -= bytes_written; 4236 } while (bytes_left > 0); 4237 4238 return (0); 4239 } 4240 4241 static int wmr_page_unprotect_regions(void *opaque, target_ulong start, 4242 target_ulong end, unsigned long flags) 4243 { 4244 if ((flags & (PAGE_WRITE | PAGE_WRITE_ORG)) == PAGE_WRITE_ORG) { 4245 size_t step = MAX(TARGET_PAGE_SIZE, qemu_real_host_page_size()); 4246 4247 while (1) { 4248 page_unprotect(start, 0); 4249 if (end - start <= step) { 4250 break; 4251 } 4252 start += step; 4253 } 4254 } 4255 return 0; 4256 } 4257 4258 typedef struct { 4259 unsigned count; 4260 size_t size; 4261 } CountAndSizeRegions; 4262 4263 static int wmr_count_and_size_regions(void *opaque, target_ulong start, 4264 target_ulong end, unsigned long flags) 4265 { 4266 CountAndSizeRegions *css = opaque; 4267 4268 css->count++; 4269 css->size += vma_dump_size(start, end, flags); 4270 return 0; 4271 } 4272 4273 typedef struct { 4274 struct elf_phdr *phdr; 4275 off_t offset; 4276 } FillRegionPhdr; 4277 4278 static int wmr_fill_region_phdr(void *opaque, target_ulong start, 4279 target_ulong end, unsigned long flags) 4280 { 4281 FillRegionPhdr *d = opaque; 4282 struct elf_phdr *phdr = d->phdr; 4283 4284 phdr->p_type = PT_LOAD; 4285 phdr->p_vaddr = start; 4286 phdr->p_paddr = 0; 4287 phdr->p_filesz = vma_dump_size(start, end, flags); 4288 phdr->p_offset = d->offset; 4289 d->offset += phdr->p_filesz; 4290 phdr->p_memsz = end - start; 4291 phdr->p_flags = (flags & PAGE_READ ? PF_R : 0) 4292 | (flags & PAGE_WRITE_ORG ? PF_W : 0) 4293 | (flags & PAGE_EXEC ? PF_X : 0); 4294 phdr->p_align = ELF_EXEC_PAGESIZE; 4295 4296 bswap_phdr(phdr, 1); 4297 d->phdr = phdr + 1; 4298 return 0; 4299 } 4300 4301 static int wmr_write_region(void *opaque, target_ulong start, 4302 target_ulong end, unsigned long flags) 4303 { 4304 int fd = *(int *)opaque; 4305 size_t size = vma_dump_size(start, end, flags); 4306 4307 if (!size) { 4308 return 0; 4309 } 4310 return dump_write(fd, g2h_untagged(start), size); 4311 } 4312 4313 /* 4314 * Write out ELF coredump. 4315 * 4316 * See documentation of ELF object file format in: 4317 * http://www.caldera.com/developers/devspecs/gabi41.pdf 4318 * 4319 * Coredump format in linux is following: 4320 * 4321 * 0 +----------------------+ \ 4322 * | ELF header | ET_CORE | 4323 * +----------------------+ | 4324 * | ELF program headers | |--- headers 4325 * | - NOTE section | | 4326 * | - PT_LOAD sections | | 4327 * +----------------------+ / 4328 * | NOTEs: | 4329 * | - NT_PRSTATUS | 4330 * | - NT_PRSINFO | 4331 * | - NT_AUXV | 4332 * +----------------------+ <-- aligned to target page 4333 * | Process memory dump | 4334 * : : 4335 * . . 4336 * : : 4337 * | | 4338 * +----------------------+ 4339 * 4340 * NT_PRSTATUS -> struct elf_prstatus (per thread) 4341 * NT_PRSINFO -> struct elf_prpsinfo 4342 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). 4343 * 4344 * Format follows System V format as close as possible. Current 4345 * version limitations are as follows: 4346 * - no floating point registers are dumped 4347 * 4348 * Function returns 0 in case of success, negative errno otherwise. 4349 * 4350 * TODO: make this work also during runtime: it should be 4351 * possible to force coredump from running process and then 4352 * continue processing. For example qemu could set up SIGUSR2 4353 * handler (provided that target process haven't registered 4354 * handler for that) that does the dump when signal is received. 4355 */ 4356 static int elf_core_dump(int signr, const CPUArchState *env) 4357 { 4358 const CPUState *cpu = env_cpu_const(env); 4359 const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu); 4360 struct rlimit dumpsize; 4361 CountAndSizeRegions css; 4362 off_t offset, note_offset, data_offset; 4363 size_t note_size; 4364 int cpus, ret; 4365 int fd = -1; 4366 CPUState *cpu_iter; 4367 4368 if (prctl(PR_GET_DUMPABLE) == 0) { 4369 return 0; 4370 } 4371 4372 if (getrlimit(RLIMIT_CORE, &dumpsize) < 0 || dumpsize.rlim_cur == 0) { 4373 return 0; 4374 } 4375 4376 cpu_list_lock(); 4377 mmap_lock(); 4378 4379 /* By unprotecting, we merge vmas that might be split. */ 4380 walk_memory_regions(NULL, wmr_page_unprotect_regions); 4381 4382 /* 4383 * Walk through target process memory mappings and 4384 * set up structure containing this information. 4385 */ 4386 memset(&css, 0, sizeof(css)); 4387 walk_memory_regions(&css, wmr_count_and_size_regions); 4388 4389 cpus = 0; 4390 CPU_FOREACH(cpu_iter) { 4391 cpus++; 4392 } 4393 4394 offset = sizeof(struct elfhdr); 4395 offset += (css.count + 1) * sizeof(struct elf_phdr); 4396 note_offset = offset; 4397 4398 offset += size_note("CORE", ts->info->auxv_len); 4399 offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); 4400 offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; 4401 note_size = offset - note_offset; 4402 data_offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); 4403 4404 /* Do not dump if the corefile size exceeds the limit. */ 4405 if (dumpsize.rlim_cur != RLIM_INFINITY 4406 && dumpsize.rlim_cur < data_offset + css.size) { 4407 errno = 0; 4408 goto out; 4409 } 4410 4411 { 4412 g_autofree char *corefile = core_dump_filename(ts); 4413 fd = open(corefile, O_WRONLY | O_CREAT | O_TRUNC, 4414 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 4415 } 4416 if (fd < 0) { 4417 goto out; 4418 } 4419 4420 /* 4421 * There is a fair amount of alignment padding within the notes 4422 * as well as preceeding the process memory. Allocate a zeroed 4423 * block to hold it all. Write all of the headers directly into 4424 * this buffer and then write it out as a block. 4425 */ 4426 { 4427 g_autofree void *header = g_malloc0(data_offset); 4428 FillRegionPhdr frp; 4429 void *hptr, *dptr; 4430 4431 /* Create elf file header. */ 4432 hptr = header; 4433 fill_elf_header(hptr, css.count + 1, ELF_MACHINE, 0); 4434 hptr += sizeof(struct elfhdr); 4435 4436 /* Create elf program headers. */ 4437 fill_elf_note_phdr(hptr, note_size, note_offset); 4438 hptr += sizeof(struct elf_phdr); 4439 4440 frp.phdr = hptr; 4441 frp.offset = data_offset; 4442 walk_memory_regions(&frp, wmr_fill_region_phdr); 4443 hptr = frp.phdr; 4444 4445 /* Create the notes. */ 4446 dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); 4447 fill_auxv_note(dptr, ts); 4448 4449 dptr = fill_note(&hptr, NT_PRPSINFO, "CORE", 4450 sizeof(struct target_elf_prpsinfo)); 4451 fill_prpsinfo_note(dptr, ts); 4452 4453 CPU_FOREACH(cpu_iter) { 4454 dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", 4455 sizeof(struct target_elf_prstatus)); 4456 fill_prstatus_note(dptr, cpu_iter, cpu_iter == cpu ? signr : 0); 4457 } 4458 4459 if (dump_write(fd, header, data_offset) < 0) { 4460 goto out; 4461 } 4462 } 4463 4464 /* 4465 * Finally write process memory into the corefile as well. 4466 */ 4467 if (walk_memory_regions(&fd, wmr_write_region) < 0) { 4468 goto out; 4469 } 4470 errno = 0; 4471 4472 out: 4473 ret = -errno; 4474 mmap_unlock(); 4475 cpu_list_unlock(); 4476 if (fd >= 0) { 4477 close(fd); 4478 } 4479 return ret; 4480 } 4481 #endif /* USE_ELF_CORE_DUMP */ 4482 4483 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) 4484 { 4485 init_thread(regs, infop); 4486 } 4487