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