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