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