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 qemu_log_mask(CPU_LOG_PAGE, "invalid %lx && %lx != %lx\n", 2176 host_start, real_start, current_start); 2177 goto try_again; 2178 } 2179 2180 /* Ensure the address is properly aligned. */ 2181 if (real_start & (align - 1)) { 2182 /* Ideally, we adjust like 2183 * 2184 * pages: [ ][ ][ ][ ][ ] 2185 * old: [ real ] 2186 * [ aligned ] 2187 * new: [ real ] 2188 * [ aligned ] 2189 * 2190 * But if there is something else mapped right after it, 2191 * then obviously it won't have room to grow, and the 2192 * kernel will put the new larger real someplace else with 2193 * unknown alignment (if we made it to here, then 2194 * fixed=false). Which is why we grow real by a full page 2195 * size, instead of by part of one; so that even if we get 2196 * moved, we can still guarantee alignment. But this does 2197 * mean that there is a padding of < 1 page both before 2198 * and after the aligned range; the "after" could could 2199 * cause problems for ARM emulation where it could butt in 2200 * to where we need to put the commpage. 2201 */ 2202 munmap((void *)real_start, host_size); 2203 real_size = aligned_size + align; 2204 real_start = (unsigned long) 2205 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0); 2206 if (real_start == (unsigned long)-1) { 2207 return (unsigned long)-1; 2208 } 2209 aligned_start = ROUND_UP(real_start, align); 2210 } else { 2211 aligned_start = real_start; 2212 } 2213 2214 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) 2215 /* On 32-bit ARM, we need to also be able to map the commpage. */ 2216 int valid = init_guest_commpage(aligned_start - guest_start, 2217 aligned_size + guest_start); 2218 if (valid == -1) { 2219 munmap((void *)real_start, real_size); 2220 return (unsigned long)-1; 2221 } else if (valid == 0) { 2222 goto try_again; 2223 } 2224 #endif 2225 2226 /* If nothing has said `return -1` or `goto try_again` yet, 2227 * then the address we have is good. 2228 */ 2229 break; 2230 2231 try_again: 2232 /* That address didn't work. Unmap and try a different one. 2233 * The address the host picked because is typically right at 2234 * the top of the host address space and leaves the guest with 2235 * no usable address space. Resort to a linear search. We 2236 * already compensated for mmap_min_addr, so this should not 2237 * happen often. Probably means we got unlucky and host 2238 * address space randomization put a shared library somewhere 2239 * inconvenient. 2240 * 2241 * This is probably a good strategy if host_start, but is 2242 * probably a bad strategy if not, which means we got here 2243 * because of trouble with ARM commpage setup. 2244 */ 2245 if (munmap((void *)real_start, real_size) != 0) { 2246 error_report("%s: failed to unmap %lx:%lx (%s)", __func__, 2247 real_start, real_size, strerror(errno)); 2248 abort(); 2249 } 2250 current_start += align; 2251 if (host_start == current_start) { 2252 /* Theoretically possible if host doesn't have any suitably 2253 * aligned areas. Normally the first mmap will fail. 2254 */ 2255 return (unsigned long)-1; 2256 } 2257 } 2258 2259 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address space\n", host_size); 2260 2261 return aligned_start; 2262 } 2263 2264 static void probe_guest_base(const char *image_name, 2265 abi_ulong loaddr, abi_ulong hiaddr) 2266 { 2267 /* Probe for a suitable guest base address, if the user has not set 2268 * it explicitly, and set guest_base appropriately. 2269 * In case of error we will print a suitable message and exit. 2270 */ 2271 const char *errmsg; 2272 if (!have_guest_base && !reserved_va) { 2273 unsigned long host_start, real_start, host_size; 2274 2275 /* Round addresses to page boundaries. */ 2276 loaddr &= qemu_host_page_mask; 2277 hiaddr = HOST_PAGE_ALIGN(hiaddr); 2278 2279 if (loaddr < mmap_min_addr) { 2280 host_start = HOST_PAGE_ALIGN(mmap_min_addr); 2281 } else { 2282 host_start = loaddr; 2283 if (host_start != loaddr) { 2284 errmsg = "Address overflow loading ELF binary"; 2285 goto exit_errmsg; 2286 } 2287 } 2288 host_size = hiaddr - loaddr; 2289 2290 /* Setup the initial guest memory space with ranges gleaned from 2291 * the ELF image that is being loaded. 2292 */ 2293 real_start = init_guest_space(host_start, host_size, loaddr, false); 2294 if (real_start == (unsigned long)-1) { 2295 errmsg = "Unable to find space for application"; 2296 goto exit_errmsg; 2297 } 2298 guest_base = real_start - loaddr; 2299 2300 qemu_log_mask(CPU_LOG_PAGE, "Relocating guest address space from 0x" 2301 TARGET_ABI_FMT_lx " to 0x%lx\n", 2302 loaddr, real_start); 2303 } 2304 return; 2305 2306 exit_errmsg: 2307 fprintf(stderr, "%s: %s\n", image_name, errmsg); 2308 exit(-1); 2309 } 2310 2311 2312 /* Load an ELF image into the address space. 2313 2314 IMAGE_NAME is the filename of the image, to use in error messages. 2315 IMAGE_FD is the open file descriptor for the image. 2316 2317 BPRM_BUF is a copy of the beginning of the file; this of course 2318 contains the elf file header at offset 0. It is assumed that this 2319 buffer is sufficiently aligned to present no problems to the host 2320 in accessing data at aligned offsets within the buffer. 2321 2322 On return: INFO values will be filled in, as necessary or available. */ 2323 2324 static void load_elf_image(const char *image_name, int image_fd, 2325 struct image_info *info, char **pinterp_name, 2326 char bprm_buf[BPRM_BUF_SIZE]) 2327 { 2328 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; 2329 struct elf_phdr *phdr; 2330 abi_ulong load_addr, load_bias, loaddr, hiaddr, error; 2331 int i, retval; 2332 const char *errmsg; 2333 2334 /* First of all, some simple consistency checks */ 2335 errmsg = "Invalid ELF image for this architecture"; 2336 if (!elf_check_ident(ehdr)) { 2337 goto exit_errmsg; 2338 } 2339 bswap_ehdr(ehdr); 2340 if (!elf_check_ehdr(ehdr)) { 2341 goto exit_errmsg; 2342 } 2343 2344 i = ehdr->e_phnum * sizeof(struct elf_phdr); 2345 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) { 2346 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff); 2347 } else { 2348 phdr = (struct elf_phdr *) alloca(i); 2349 retval = pread(image_fd, phdr, i, ehdr->e_phoff); 2350 if (retval != i) { 2351 goto exit_read; 2352 } 2353 } 2354 bswap_phdr(phdr, ehdr->e_phnum); 2355 2356 info->nsegs = 0; 2357 info->pt_dynamic_addr = 0; 2358 2359 mmap_lock(); 2360 2361 /* Find the maximum size of the image and allocate an appropriate 2362 amount of memory to handle that. */ 2363 loaddr = -1, hiaddr = 0; 2364 info->alignment = 0; 2365 for (i = 0; i < ehdr->e_phnum; ++i) { 2366 if (phdr[i].p_type == PT_LOAD) { 2367 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset; 2368 if (a < loaddr) { 2369 loaddr = a; 2370 } 2371 a = phdr[i].p_vaddr + phdr[i].p_memsz; 2372 if (a > hiaddr) { 2373 hiaddr = a; 2374 } 2375 ++info->nsegs; 2376 info->alignment |= phdr[i].p_align; 2377 } 2378 } 2379 2380 if (pinterp_name != NULL) { 2381 /* 2382 * This is the main executable. 2383 * 2384 * Reserve extra space for brk. 2385 * We hold on to this space while placing the interpreter 2386 * and the stack, lest they be placed immediately after 2387 * the data segment and block allocation from the brk. 2388 * 2389 * 16MB is chosen as "large enough" without being so large 2390 * as to allow the result to not fit with a 32-bit guest on 2391 * a 32-bit host. 2392 */ 2393 info->reserve_brk = 16 * MiB; 2394 hiaddr += info->reserve_brk; 2395 2396 if (ehdr->e_type == ET_EXEC) { 2397 /* 2398 * Make sure that the low address does not conflict with 2399 * MMAP_MIN_ADDR or the QEMU application itself. 2400 */ 2401 probe_guest_base(image_name, loaddr, hiaddr); 2402 } 2403 } 2404 2405 /* 2406 * Reserve address space for all of this. 2407 * 2408 * In the case of ET_EXEC, we supply MAP_FIXED so that we get 2409 * exactly the address range that is required. 2410 * 2411 * Otherwise this is ET_DYN, and we are searching for a location 2412 * that can hold the memory space required. If the image is 2413 * pre-linked, LOADDR will be non-zero, and the kernel should 2414 * honor that address if it happens to be free. 2415 * 2416 * In both cases, we will overwrite pages in this range with mappings 2417 * from the executable. 2418 */ 2419 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE, 2420 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | 2421 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0), 2422 -1, 0); 2423 if (load_addr == -1) { 2424 goto exit_perror; 2425 } 2426 load_bias = load_addr - loaddr; 2427 2428 if (elf_is_fdpic(ehdr)) { 2429 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = 2430 g_malloc(sizeof(*loadsegs) * info->nsegs); 2431 2432 for (i = 0; i < ehdr->e_phnum; ++i) { 2433 switch (phdr[i].p_type) { 2434 case PT_DYNAMIC: 2435 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias; 2436 break; 2437 case PT_LOAD: 2438 loadsegs->addr = phdr[i].p_vaddr + load_bias; 2439 loadsegs->p_vaddr = phdr[i].p_vaddr; 2440 loadsegs->p_memsz = phdr[i].p_memsz; 2441 ++loadsegs; 2442 break; 2443 } 2444 } 2445 } 2446 2447 info->load_bias = load_bias; 2448 info->code_offset = load_bias; 2449 info->data_offset = load_bias; 2450 info->load_addr = load_addr; 2451 info->entry = ehdr->e_entry + load_bias; 2452 info->start_code = -1; 2453 info->end_code = 0; 2454 info->start_data = -1; 2455 info->end_data = 0; 2456 info->brk = 0; 2457 info->elf_flags = ehdr->e_flags; 2458 2459 for (i = 0; i < ehdr->e_phnum; i++) { 2460 struct elf_phdr *eppnt = phdr + i; 2461 if (eppnt->p_type == PT_LOAD) { 2462 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len; 2463 int elf_prot = 0; 2464 2465 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ; 2466 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE; 2467 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; 2468 2469 vaddr = load_bias + eppnt->p_vaddr; 2470 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr); 2471 vaddr_ps = TARGET_ELF_PAGESTART(vaddr); 2472 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po); 2473 2474 /* 2475 * Some segments may be completely empty without any backing file 2476 * segment, in that case just let zero_bss allocate an empty buffer 2477 * for it. 2478 */ 2479 if (eppnt->p_filesz != 0) { 2480 error = target_mmap(vaddr_ps, vaddr_len, elf_prot, 2481 MAP_PRIVATE | MAP_FIXED, 2482 image_fd, eppnt->p_offset - vaddr_po); 2483 2484 if (error == -1) { 2485 goto exit_perror; 2486 } 2487 } 2488 2489 vaddr_ef = vaddr + eppnt->p_filesz; 2490 vaddr_em = vaddr + eppnt->p_memsz; 2491 2492 /* If the load segment requests extra zeros (e.g. bss), map it. */ 2493 if (vaddr_ef < vaddr_em) { 2494 zero_bss(vaddr_ef, vaddr_em, elf_prot); 2495 } 2496 2497 /* Find the full program boundaries. */ 2498 if (elf_prot & PROT_EXEC) { 2499 if (vaddr < info->start_code) { 2500 info->start_code = vaddr; 2501 } 2502 if (vaddr_ef > info->end_code) { 2503 info->end_code = vaddr_ef; 2504 } 2505 } 2506 if (elf_prot & PROT_WRITE) { 2507 if (vaddr < info->start_data) { 2508 info->start_data = vaddr; 2509 } 2510 if (vaddr_ef > info->end_data) { 2511 info->end_data = vaddr_ef; 2512 } 2513 if (vaddr_em > info->brk) { 2514 info->brk = vaddr_em; 2515 } 2516 } 2517 } else if (eppnt->p_type == PT_INTERP && pinterp_name) { 2518 char *interp_name; 2519 2520 if (*pinterp_name) { 2521 errmsg = "Multiple PT_INTERP entries"; 2522 goto exit_errmsg; 2523 } 2524 interp_name = malloc(eppnt->p_filesz); 2525 if (!interp_name) { 2526 goto exit_perror; 2527 } 2528 2529 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) { 2530 memcpy(interp_name, bprm_buf + eppnt->p_offset, 2531 eppnt->p_filesz); 2532 } else { 2533 retval = pread(image_fd, interp_name, eppnt->p_filesz, 2534 eppnt->p_offset); 2535 if (retval != eppnt->p_filesz) { 2536 goto exit_perror; 2537 } 2538 } 2539 if (interp_name[eppnt->p_filesz - 1] != 0) { 2540 errmsg = "Invalid PT_INTERP entry"; 2541 goto exit_errmsg; 2542 } 2543 *pinterp_name = interp_name; 2544 #ifdef TARGET_MIPS 2545 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { 2546 Mips_elf_abiflags_v0 abiflags; 2547 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) { 2548 errmsg = "Invalid PT_MIPS_ABIFLAGS entry"; 2549 goto exit_errmsg; 2550 } 2551 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) { 2552 memcpy(&abiflags, bprm_buf + eppnt->p_offset, 2553 sizeof(Mips_elf_abiflags_v0)); 2554 } else { 2555 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0), 2556 eppnt->p_offset); 2557 if (retval != sizeof(Mips_elf_abiflags_v0)) { 2558 goto exit_perror; 2559 } 2560 } 2561 bswap_mips_abiflags(&abiflags); 2562 info->fp_abi = abiflags.fp_abi; 2563 #endif 2564 } 2565 } 2566 2567 if (info->end_data == 0) { 2568 info->start_data = info->end_code; 2569 info->end_data = info->end_code; 2570 info->brk = info->end_code; 2571 } 2572 2573 if (qemu_log_enabled()) { 2574 load_symbols(ehdr, image_fd, load_bias); 2575 } 2576 2577 mmap_unlock(); 2578 2579 close(image_fd); 2580 return; 2581 2582 exit_read: 2583 if (retval >= 0) { 2584 errmsg = "Incomplete read of file header"; 2585 goto exit_errmsg; 2586 } 2587 exit_perror: 2588 errmsg = strerror(errno); 2589 exit_errmsg: 2590 fprintf(stderr, "%s: %s\n", image_name, errmsg); 2591 exit(-1); 2592 } 2593 2594 static void load_elf_interp(const char *filename, struct image_info *info, 2595 char bprm_buf[BPRM_BUF_SIZE]) 2596 { 2597 int fd, retval; 2598 2599 fd = open(path(filename), O_RDONLY); 2600 if (fd < 0) { 2601 goto exit_perror; 2602 } 2603 2604 retval = read(fd, bprm_buf, BPRM_BUF_SIZE); 2605 if (retval < 0) { 2606 goto exit_perror; 2607 } 2608 if (retval < BPRM_BUF_SIZE) { 2609 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); 2610 } 2611 2612 load_elf_image(filename, fd, info, NULL, bprm_buf); 2613 return; 2614 2615 exit_perror: 2616 fprintf(stderr, "%s: %s\n", filename, strerror(errno)); 2617 exit(-1); 2618 } 2619 2620 static int symfind(const void *s0, const void *s1) 2621 { 2622 target_ulong addr = *(target_ulong *)s0; 2623 struct elf_sym *sym = (struct elf_sym *)s1; 2624 int result = 0; 2625 if (addr < sym->st_value) { 2626 result = -1; 2627 } else if (addr >= sym->st_value + sym->st_size) { 2628 result = 1; 2629 } 2630 return result; 2631 } 2632 2633 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr) 2634 { 2635 #if ELF_CLASS == ELFCLASS32 2636 struct elf_sym *syms = s->disas_symtab.elf32; 2637 #else 2638 struct elf_sym *syms = s->disas_symtab.elf64; 2639 #endif 2640 2641 // binary search 2642 struct elf_sym *sym; 2643 2644 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); 2645 if (sym != NULL) { 2646 return s->disas_strtab + sym->st_name; 2647 } 2648 2649 return ""; 2650 } 2651 2652 /* FIXME: This should use elf_ops.h */ 2653 static int symcmp(const void *s0, const void *s1) 2654 { 2655 struct elf_sym *sym0 = (struct elf_sym *)s0; 2656 struct elf_sym *sym1 = (struct elf_sym *)s1; 2657 return (sym0->st_value < sym1->st_value) 2658 ? -1 2659 : ((sym0->st_value > sym1->st_value) ? 1 : 0); 2660 } 2661 2662 /* Best attempt to load symbols from this ELF object. */ 2663 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) 2664 { 2665 int i, shnum, nsyms, sym_idx = 0, str_idx = 0; 2666 uint64_t segsz; 2667 struct elf_shdr *shdr; 2668 char *strings = NULL; 2669 struct syminfo *s = NULL; 2670 struct elf_sym *new_syms, *syms = NULL; 2671 2672 shnum = hdr->e_shnum; 2673 i = shnum * sizeof(struct elf_shdr); 2674 shdr = (struct elf_shdr *)alloca(i); 2675 if (pread(fd, shdr, i, hdr->e_shoff) != i) { 2676 return; 2677 } 2678 2679 bswap_shdr(shdr, shnum); 2680 for (i = 0; i < shnum; ++i) { 2681 if (shdr[i].sh_type == SHT_SYMTAB) { 2682 sym_idx = i; 2683 str_idx = shdr[i].sh_link; 2684 goto found; 2685 } 2686 } 2687 2688 /* There will be no symbol table if the file was stripped. */ 2689 return; 2690 2691 found: 2692 /* Now know where the strtab and symtab are. Snarf them. */ 2693 s = g_try_new(struct syminfo, 1); 2694 if (!s) { 2695 goto give_up; 2696 } 2697 2698 segsz = shdr[str_idx].sh_size; 2699 s->disas_strtab = strings = g_try_malloc(segsz); 2700 if (!strings || 2701 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) { 2702 goto give_up; 2703 } 2704 2705 segsz = shdr[sym_idx].sh_size; 2706 syms = g_try_malloc(segsz); 2707 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) { 2708 goto give_up; 2709 } 2710 2711 if (segsz / sizeof(struct elf_sym) > INT_MAX) { 2712 /* Implausibly large symbol table: give up rather than ploughing 2713 * on with the number of symbols calculation overflowing 2714 */ 2715 goto give_up; 2716 } 2717 nsyms = segsz / sizeof(struct elf_sym); 2718 for (i = 0; i < nsyms; ) { 2719 bswap_sym(syms + i); 2720 /* Throw away entries which we do not need. */ 2721 if (syms[i].st_shndx == SHN_UNDEF 2722 || syms[i].st_shndx >= SHN_LORESERVE 2723 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { 2724 if (i < --nsyms) { 2725 syms[i] = syms[nsyms]; 2726 } 2727 } else { 2728 #if defined(TARGET_ARM) || defined (TARGET_MIPS) 2729 /* The bottom address bit marks a Thumb or MIPS16 symbol. */ 2730 syms[i].st_value &= ~(target_ulong)1; 2731 #endif 2732 syms[i].st_value += load_bias; 2733 i++; 2734 } 2735 } 2736 2737 /* No "useful" symbol. */ 2738 if (nsyms == 0) { 2739 goto give_up; 2740 } 2741 2742 /* Attempt to free the storage associated with the local symbols 2743 that we threw away. Whether or not this has any effect on the 2744 memory allocation depends on the malloc implementation and how 2745 many symbols we managed to discard. */ 2746 new_syms = g_try_renew(struct elf_sym, syms, nsyms); 2747 if (new_syms == NULL) { 2748 goto give_up; 2749 } 2750 syms = new_syms; 2751 2752 qsort(syms, nsyms, sizeof(*syms), symcmp); 2753 2754 s->disas_num_syms = nsyms; 2755 #if ELF_CLASS == ELFCLASS32 2756 s->disas_symtab.elf32 = syms; 2757 #else 2758 s->disas_symtab.elf64 = syms; 2759 #endif 2760 s->lookup_symbol = lookup_symbolxx; 2761 s->next = syminfos; 2762 syminfos = s; 2763 2764 return; 2765 2766 give_up: 2767 g_free(s); 2768 g_free(strings); 2769 g_free(syms); 2770 } 2771 2772 uint32_t get_elf_eflags(int fd) 2773 { 2774 struct elfhdr ehdr; 2775 off_t offset; 2776 int ret; 2777 2778 /* Read ELF header */ 2779 offset = lseek(fd, 0, SEEK_SET); 2780 if (offset == (off_t) -1) { 2781 return 0; 2782 } 2783 ret = read(fd, &ehdr, sizeof(ehdr)); 2784 if (ret < sizeof(ehdr)) { 2785 return 0; 2786 } 2787 offset = lseek(fd, offset, SEEK_SET); 2788 if (offset == (off_t) -1) { 2789 return 0; 2790 } 2791 2792 /* Check ELF signature */ 2793 if (!elf_check_ident(&ehdr)) { 2794 return 0; 2795 } 2796 2797 /* check header */ 2798 bswap_ehdr(&ehdr); 2799 if (!elf_check_ehdr(&ehdr)) { 2800 return 0; 2801 } 2802 2803 /* return architecture id */ 2804 return ehdr.e_flags; 2805 } 2806 2807 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) 2808 { 2809 struct image_info interp_info; 2810 struct elfhdr elf_ex; 2811 char *elf_interpreter = NULL; 2812 char *scratch; 2813 2814 memset(&interp_info, 0, sizeof(interp_info)); 2815 #ifdef TARGET_MIPS 2816 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN; 2817 #endif 2818 2819 info->start_mmap = (abi_ulong)ELF_START_MMAP; 2820 2821 load_elf_image(bprm->filename, bprm->fd, info, 2822 &elf_interpreter, bprm->buf); 2823 2824 /* ??? We need a copy of the elf header for passing to create_elf_tables. 2825 If we do nothing, we'll have overwritten this when we re-use bprm->buf 2826 when we load the interpreter. */ 2827 elf_ex = *(struct elfhdr *)bprm->buf; 2828 2829 /* Do this so that we can load the interpreter, if need be. We will 2830 change some of these later */ 2831 bprm->p = setup_arg_pages(bprm, info); 2832 2833 scratch = g_new0(char, TARGET_PAGE_SIZE); 2834 if (STACK_GROWS_DOWN) { 2835 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 2836 bprm->p, info->stack_limit); 2837 info->file_string = bprm->p; 2838 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 2839 bprm->p, info->stack_limit); 2840 info->env_strings = bprm->p; 2841 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 2842 bprm->p, info->stack_limit); 2843 info->arg_strings = bprm->p; 2844 } else { 2845 info->arg_strings = bprm->p; 2846 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 2847 bprm->p, info->stack_limit); 2848 info->env_strings = bprm->p; 2849 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 2850 bprm->p, info->stack_limit); 2851 info->file_string = bprm->p; 2852 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 2853 bprm->p, info->stack_limit); 2854 } 2855 2856 g_free(scratch); 2857 2858 if (!bprm->p) { 2859 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); 2860 exit(-1); 2861 } 2862 2863 if (elf_interpreter) { 2864 load_elf_interp(elf_interpreter, &interp_info, bprm->buf); 2865 2866 /* If the program interpreter is one of these two, then assume 2867 an iBCS2 image. Otherwise assume a native linux image. */ 2868 2869 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 2870 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) { 2871 info->personality = PER_SVR4; 2872 2873 /* Why this, you ask??? Well SVr4 maps page 0 as read-only, 2874 and some applications "depend" upon this behavior. Since 2875 we do not have the power to recompile these, we emulate 2876 the SVr4 behavior. Sigh. */ 2877 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC, 2878 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 2879 } 2880 #ifdef TARGET_MIPS 2881 info->interp_fp_abi = interp_info.fp_abi; 2882 #endif 2883 } 2884 2885 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex, 2886 info, (elf_interpreter ? &interp_info : NULL)); 2887 info->start_stack = bprm->p; 2888 2889 /* If we have an interpreter, set that as the program's entry point. 2890 Copy the load_bias as well, to help PPC64 interpret the entry 2891 point as a function descriptor. Do this after creating elf tables 2892 so that we copy the original program entry point into the AUXV. */ 2893 if (elf_interpreter) { 2894 info->load_bias = interp_info.load_bias; 2895 info->entry = interp_info.entry; 2896 free(elf_interpreter); 2897 } 2898 2899 #ifdef USE_ELF_CORE_DUMP 2900 bprm->core_dump = &elf_core_dump; 2901 #endif 2902 2903 /* 2904 * If we reserved extra space for brk, release it now. 2905 * The implementation of do_brk in syscalls.c expects to be able 2906 * to mmap pages in this space. 2907 */ 2908 if (info->reserve_brk) { 2909 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk); 2910 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk); 2911 target_munmap(start_brk, end_brk - start_brk); 2912 } 2913 2914 return 0; 2915 } 2916 2917 #ifdef USE_ELF_CORE_DUMP 2918 /* 2919 * Definitions to generate Intel SVR4-like core files. 2920 * These mostly have the same names as the SVR4 types with "target_elf_" 2921 * tacked on the front to prevent clashes with linux definitions, 2922 * and the typedef forms have been avoided. This is mostly like 2923 * the SVR4 structure, but more Linuxy, with things that Linux does 2924 * not support and which gdb doesn't really use excluded. 2925 * 2926 * Fields we don't dump (their contents is zero) in linux-user qemu 2927 * are marked with XXX. 2928 * 2929 * Core dump code is copied from linux kernel (fs/binfmt_elf.c). 2930 * 2931 * Porting ELF coredump for target is (quite) simple process. First you 2932 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for 2933 * the target resides): 2934 * 2935 * #define USE_ELF_CORE_DUMP 2936 * 2937 * Next you define type of register set used for dumping. ELF specification 2938 * says that it needs to be array of elf_greg_t that has size of ELF_NREG. 2939 * 2940 * typedef <target_regtype> target_elf_greg_t; 2941 * #define ELF_NREG <number of registers> 2942 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG]; 2943 * 2944 * Last step is to implement target specific function that copies registers 2945 * from given cpu into just specified register set. Prototype is: 2946 * 2947 * static void elf_core_copy_regs(taret_elf_gregset_t *regs, 2948 * const CPUArchState *env); 2949 * 2950 * Parameters: 2951 * regs - copy register values into here (allocated and zeroed by caller) 2952 * env - copy registers from here 2953 * 2954 * Example for ARM target is provided in this file. 2955 */ 2956 2957 /* An ELF note in memory */ 2958 struct memelfnote { 2959 const char *name; 2960 size_t namesz; 2961 size_t namesz_rounded; 2962 int type; 2963 size_t datasz; 2964 size_t datasz_rounded; 2965 void *data; 2966 size_t notesz; 2967 }; 2968 2969 struct target_elf_siginfo { 2970 abi_int si_signo; /* signal number */ 2971 abi_int si_code; /* extra code */ 2972 abi_int si_errno; /* errno */ 2973 }; 2974 2975 struct target_elf_prstatus { 2976 struct target_elf_siginfo pr_info; /* Info associated with signal */ 2977 abi_short pr_cursig; /* Current signal */ 2978 abi_ulong pr_sigpend; /* XXX */ 2979 abi_ulong pr_sighold; /* XXX */ 2980 target_pid_t pr_pid; 2981 target_pid_t pr_ppid; 2982 target_pid_t pr_pgrp; 2983 target_pid_t pr_sid; 2984 struct target_timeval pr_utime; /* XXX User time */ 2985 struct target_timeval pr_stime; /* XXX System time */ 2986 struct target_timeval pr_cutime; /* XXX Cumulative user time */ 2987 struct target_timeval pr_cstime; /* XXX Cumulative system time */ 2988 target_elf_gregset_t pr_reg; /* GP registers */ 2989 abi_int pr_fpvalid; /* XXX */ 2990 }; 2991 2992 #define ELF_PRARGSZ (80) /* Number of chars for args */ 2993 2994 struct target_elf_prpsinfo { 2995 char pr_state; /* numeric process state */ 2996 char pr_sname; /* char for pr_state */ 2997 char pr_zomb; /* zombie */ 2998 char pr_nice; /* nice val */ 2999 abi_ulong pr_flag; /* flags */ 3000 target_uid_t pr_uid; 3001 target_gid_t pr_gid; 3002 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; 3003 /* Lots missing */ 3004 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */ 3005 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ 3006 }; 3007 3008 /* Here is the structure in which status of each thread is captured. */ 3009 struct elf_thread_status { 3010 QTAILQ_ENTRY(elf_thread_status) ets_link; 3011 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ 3012 #if 0 3013 elf_fpregset_t fpu; /* NT_PRFPREG */ 3014 struct task_struct *thread; 3015 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */ 3016 #endif 3017 struct memelfnote notes[1]; 3018 int num_notes; 3019 }; 3020 3021 struct elf_note_info { 3022 struct memelfnote *notes; 3023 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */ 3024 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */ 3025 3026 QTAILQ_HEAD(, elf_thread_status) thread_list; 3027 #if 0 3028 /* 3029 * Current version of ELF coredump doesn't support 3030 * dumping fp regs etc. 3031 */ 3032 elf_fpregset_t *fpu; 3033 elf_fpxregset_t *xfpu; 3034 int thread_status_size; 3035 #endif 3036 int notes_size; 3037 int numnote; 3038 }; 3039 3040 struct vm_area_struct { 3041 target_ulong vma_start; /* start vaddr of memory region */ 3042 target_ulong vma_end; /* end vaddr of memory region */ 3043 abi_ulong vma_flags; /* protection etc. flags for the region */ 3044 QTAILQ_ENTRY(vm_area_struct) vma_link; 3045 }; 3046 3047 struct mm_struct { 3048 QTAILQ_HEAD(, vm_area_struct) mm_mmap; 3049 int mm_count; /* number of mappings */ 3050 }; 3051 3052 static struct mm_struct *vma_init(void); 3053 static void vma_delete(struct mm_struct *); 3054 static int vma_add_mapping(struct mm_struct *, target_ulong, 3055 target_ulong, abi_ulong); 3056 static int vma_get_mapping_count(const struct mm_struct *); 3057 static struct vm_area_struct *vma_first(const struct mm_struct *); 3058 static struct vm_area_struct *vma_next(struct vm_area_struct *); 3059 static abi_ulong vma_dump_size(const struct vm_area_struct *); 3060 static int vma_walker(void *priv, target_ulong start, target_ulong end, 3061 unsigned long flags); 3062 3063 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t); 3064 static void fill_note(struct memelfnote *, const char *, int, 3065 unsigned int, void *); 3066 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int); 3067 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *); 3068 static void fill_auxv_note(struct memelfnote *, const TaskState *); 3069 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t); 3070 static size_t note_size(const struct memelfnote *); 3071 static void free_note_info(struct elf_note_info *); 3072 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *); 3073 static void fill_thread_info(struct elf_note_info *, const CPUArchState *); 3074 static int core_dump_filename(const TaskState *, char *, size_t); 3075 3076 static int dump_write(int, const void *, size_t); 3077 static int write_note(struct memelfnote *, int); 3078 static int write_note_info(struct elf_note_info *, int); 3079 3080 #ifdef BSWAP_NEEDED 3081 static void bswap_prstatus(struct target_elf_prstatus *prstatus) 3082 { 3083 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo); 3084 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code); 3085 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno); 3086 prstatus->pr_cursig = tswap16(prstatus->pr_cursig); 3087 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); 3088 prstatus->pr_sighold = tswapal(prstatus->pr_sighold); 3089 prstatus->pr_pid = tswap32(prstatus->pr_pid); 3090 prstatus->pr_ppid = tswap32(prstatus->pr_ppid); 3091 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp); 3092 prstatus->pr_sid = tswap32(prstatus->pr_sid); 3093 /* cpu times are not filled, so we skip them */ 3094 /* regs should be in correct format already */ 3095 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); 3096 } 3097 3098 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) 3099 { 3100 psinfo->pr_flag = tswapal(psinfo->pr_flag); 3101 psinfo->pr_uid = tswap16(psinfo->pr_uid); 3102 psinfo->pr_gid = tswap16(psinfo->pr_gid); 3103 psinfo->pr_pid = tswap32(psinfo->pr_pid); 3104 psinfo->pr_ppid = tswap32(psinfo->pr_ppid); 3105 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp); 3106 psinfo->pr_sid = tswap32(psinfo->pr_sid); 3107 } 3108 3109 static void bswap_note(struct elf_note *en) 3110 { 3111 bswap32s(&en->n_namesz); 3112 bswap32s(&en->n_descsz); 3113 bswap32s(&en->n_type); 3114 } 3115 #else 3116 static inline void bswap_prstatus(struct target_elf_prstatus *p) { } 3117 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} 3118 static inline void bswap_note(struct elf_note *en) { } 3119 #endif /* BSWAP_NEEDED */ 3120 3121 /* 3122 * Minimal support for linux memory regions. These are needed 3123 * when we are finding out what memory exactly belongs to 3124 * emulated process. No locks needed here, as long as 3125 * thread that received the signal is stopped. 3126 */ 3127 3128 static struct mm_struct *vma_init(void) 3129 { 3130 struct mm_struct *mm; 3131 3132 if ((mm = g_malloc(sizeof (*mm))) == NULL) 3133 return (NULL); 3134 3135 mm->mm_count = 0; 3136 QTAILQ_INIT(&mm->mm_mmap); 3137 3138 return (mm); 3139 } 3140 3141 static void vma_delete(struct mm_struct *mm) 3142 { 3143 struct vm_area_struct *vma; 3144 3145 while ((vma = vma_first(mm)) != NULL) { 3146 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link); 3147 g_free(vma); 3148 } 3149 g_free(mm); 3150 } 3151 3152 static int vma_add_mapping(struct mm_struct *mm, target_ulong start, 3153 target_ulong end, abi_ulong flags) 3154 { 3155 struct vm_area_struct *vma; 3156 3157 if ((vma = g_malloc0(sizeof (*vma))) == NULL) 3158 return (-1); 3159 3160 vma->vma_start = start; 3161 vma->vma_end = end; 3162 vma->vma_flags = flags; 3163 3164 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link); 3165 mm->mm_count++; 3166 3167 return (0); 3168 } 3169 3170 static struct vm_area_struct *vma_first(const struct mm_struct *mm) 3171 { 3172 return (QTAILQ_FIRST(&mm->mm_mmap)); 3173 } 3174 3175 static struct vm_area_struct *vma_next(struct vm_area_struct *vma) 3176 { 3177 return (QTAILQ_NEXT(vma, vma_link)); 3178 } 3179 3180 static int vma_get_mapping_count(const struct mm_struct *mm) 3181 { 3182 return (mm->mm_count); 3183 } 3184 3185 /* 3186 * Calculate file (dump) size of given memory region. 3187 */ 3188 static abi_ulong vma_dump_size(const struct vm_area_struct *vma) 3189 { 3190 /* if we cannot even read the first page, skip it */ 3191 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE)) 3192 return (0); 3193 3194 /* 3195 * Usually we don't dump executable pages as they contain 3196 * non-writable code that debugger can read directly from 3197 * target library etc. However, thread stacks are marked 3198 * also executable so we read in first page of given region 3199 * and check whether it contains elf header. If there is 3200 * no elf header, we dump it. 3201 */ 3202 if (vma->vma_flags & PROT_EXEC) { 3203 char page[TARGET_PAGE_SIZE]; 3204 3205 copy_from_user(page, vma->vma_start, sizeof (page)); 3206 if ((page[EI_MAG0] == ELFMAG0) && 3207 (page[EI_MAG1] == ELFMAG1) && 3208 (page[EI_MAG2] == ELFMAG2) && 3209 (page[EI_MAG3] == ELFMAG3)) { 3210 /* 3211 * Mappings are possibly from ELF binary. Don't dump 3212 * them. 3213 */ 3214 return (0); 3215 } 3216 } 3217 3218 return (vma->vma_end - vma->vma_start); 3219 } 3220 3221 static int vma_walker(void *priv, target_ulong start, target_ulong end, 3222 unsigned long flags) 3223 { 3224 struct mm_struct *mm = (struct mm_struct *)priv; 3225 3226 vma_add_mapping(mm, start, end, flags); 3227 return (0); 3228 } 3229 3230 static void fill_note(struct memelfnote *note, const char *name, int type, 3231 unsigned int sz, void *data) 3232 { 3233 unsigned int namesz; 3234 3235 namesz = strlen(name) + 1; 3236 note->name = name; 3237 note->namesz = namesz; 3238 note->namesz_rounded = roundup(namesz, sizeof (int32_t)); 3239 note->type = type; 3240 note->datasz = sz; 3241 note->datasz_rounded = roundup(sz, sizeof (int32_t)); 3242 3243 note->data = data; 3244 3245 /* 3246 * We calculate rounded up note size here as specified by 3247 * ELF document. 3248 */ 3249 note->notesz = sizeof (struct elf_note) + 3250 note->namesz_rounded + note->datasz_rounded; 3251 } 3252 3253 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, 3254 uint32_t flags) 3255 { 3256 (void) memset(elf, 0, sizeof(*elf)); 3257 3258 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG); 3259 elf->e_ident[EI_CLASS] = ELF_CLASS; 3260 elf->e_ident[EI_DATA] = ELF_DATA; 3261 elf->e_ident[EI_VERSION] = EV_CURRENT; 3262 elf->e_ident[EI_OSABI] = ELF_OSABI; 3263 3264 elf->e_type = ET_CORE; 3265 elf->e_machine = machine; 3266 elf->e_version = EV_CURRENT; 3267 elf->e_phoff = sizeof(struct elfhdr); 3268 elf->e_flags = flags; 3269 elf->e_ehsize = sizeof(struct elfhdr); 3270 elf->e_phentsize = sizeof(struct elf_phdr); 3271 elf->e_phnum = segs; 3272 3273 bswap_ehdr(elf); 3274 } 3275 3276 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset) 3277 { 3278 phdr->p_type = PT_NOTE; 3279 phdr->p_offset = offset; 3280 phdr->p_vaddr = 0; 3281 phdr->p_paddr = 0; 3282 phdr->p_filesz = sz; 3283 phdr->p_memsz = 0; 3284 phdr->p_flags = 0; 3285 phdr->p_align = 0; 3286 3287 bswap_phdr(phdr, 1); 3288 } 3289 3290 static size_t note_size(const struct memelfnote *note) 3291 { 3292 return (note->notesz); 3293 } 3294 3295 static void fill_prstatus(struct target_elf_prstatus *prstatus, 3296 const TaskState *ts, int signr) 3297 { 3298 (void) memset(prstatus, 0, sizeof (*prstatus)); 3299 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr; 3300 prstatus->pr_pid = ts->ts_tid; 3301 prstatus->pr_ppid = getppid(); 3302 prstatus->pr_pgrp = getpgrp(); 3303 prstatus->pr_sid = getsid(0); 3304 3305 bswap_prstatus(prstatus); 3306 } 3307 3308 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts) 3309 { 3310 char *base_filename; 3311 unsigned int i, len; 3312 3313 (void) memset(psinfo, 0, sizeof (*psinfo)); 3314 3315 len = ts->info->arg_end - ts->info->arg_start; 3316 if (len >= ELF_PRARGSZ) 3317 len = ELF_PRARGSZ - 1; 3318 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len)) 3319 return -EFAULT; 3320 for (i = 0; i < len; i++) 3321 if (psinfo->pr_psargs[i] == 0) 3322 psinfo->pr_psargs[i] = ' '; 3323 psinfo->pr_psargs[len] = 0; 3324 3325 psinfo->pr_pid = getpid(); 3326 psinfo->pr_ppid = getppid(); 3327 psinfo->pr_pgrp = getpgrp(); 3328 psinfo->pr_sid = getsid(0); 3329 psinfo->pr_uid = getuid(); 3330 psinfo->pr_gid = getgid(); 3331 3332 base_filename = g_path_get_basename(ts->bprm->filename); 3333 /* 3334 * Using strncpy here is fine: at max-length, 3335 * this field is not NUL-terminated. 3336 */ 3337 (void) strncpy(psinfo->pr_fname, base_filename, 3338 sizeof(psinfo->pr_fname)); 3339 3340 g_free(base_filename); 3341 bswap_psinfo(psinfo); 3342 return (0); 3343 } 3344 3345 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts) 3346 { 3347 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv; 3348 elf_addr_t orig_auxv = auxv; 3349 void *ptr; 3350 int len = ts->info->auxv_len; 3351 3352 /* 3353 * Auxiliary vector is stored in target process stack. It contains 3354 * {type, value} pairs that we need to dump into note. This is not 3355 * strictly necessary but we do it here for sake of completeness. 3356 */ 3357 3358 /* read in whole auxv vector and copy it to memelfnote */ 3359 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0); 3360 if (ptr != NULL) { 3361 fill_note(note, "CORE", NT_AUXV, len, ptr); 3362 unlock_user(ptr, auxv, len); 3363 } 3364 } 3365 3366 /* 3367 * Constructs name of coredump file. We have following convention 3368 * for the name: 3369 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core 3370 * 3371 * Returns 0 in case of success, -1 otherwise (errno is set). 3372 */ 3373 static int core_dump_filename(const TaskState *ts, char *buf, 3374 size_t bufsize) 3375 { 3376 char timestamp[64]; 3377 char *base_filename = NULL; 3378 struct timeval tv; 3379 struct tm tm; 3380 3381 assert(bufsize >= PATH_MAX); 3382 3383 if (gettimeofday(&tv, NULL) < 0) { 3384 (void) fprintf(stderr, "unable to get current timestamp: %s", 3385 strerror(errno)); 3386 return (-1); 3387 } 3388 3389 base_filename = g_path_get_basename(ts->bprm->filename); 3390 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S", 3391 localtime_r(&tv.tv_sec, &tm)); 3392 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core", 3393 base_filename, timestamp, (int)getpid()); 3394 g_free(base_filename); 3395 3396 return (0); 3397 } 3398 3399 static int dump_write(int fd, const void *ptr, size_t size) 3400 { 3401 const char *bufp = (const char *)ptr; 3402 ssize_t bytes_written, bytes_left; 3403 struct rlimit dumpsize; 3404 off_t pos; 3405 3406 bytes_written = 0; 3407 getrlimit(RLIMIT_CORE, &dumpsize); 3408 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) { 3409 if (errno == ESPIPE) { /* not a seekable stream */ 3410 bytes_left = size; 3411 } else { 3412 return pos; 3413 } 3414 } else { 3415 if (dumpsize.rlim_cur <= pos) { 3416 return -1; 3417 } else if (dumpsize.rlim_cur == RLIM_INFINITY) { 3418 bytes_left = size; 3419 } else { 3420 size_t limit_left=dumpsize.rlim_cur - pos; 3421 bytes_left = limit_left >= size ? size : limit_left ; 3422 } 3423 } 3424 3425 /* 3426 * In normal conditions, single write(2) should do but 3427 * in case of socket etc. this mechanism is more portable. 3428 */ 3429 do { 3430 bytes_written = write(fd, bufp, bytes_left); 3431 if (bytes_written < 0) { 3432 if (errno == EINTR) 3433 continue; 3434 return (-1); 3435 } else if (bytes_written == 0) { /* eof */ 3436 return (-1); 3437 } 3438 bufp += bytes_written; 3439 bytes_left -= bytes_written; 3440 } while (bytes_left > 0); 3441 3442 return (0); 3443 } 3444 3445 static int write_note(struct memelfnote *men, int fd) 3446 { 3447 struct elf_note en; 3448 3449 en.n_namesz = men->namesz; 3450 en.n_type = men->type; 3451 en.n_descsz = men->datasz; 3452 3453 bswap_note(&en); 3454 3455 if (dump_write(fd, &en, sizeof(en)) != 0) 3456 return (-1); 3457 if (dump_write(fd, men->name, men->namesz_rounded) != 0) 3458 return (-1); 3459 if (dump_write(fd, men->data, men->datasz_rounded) != 0) 3460 return (-1); 3461 3462 return (0); 3463 } 3464 3465 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env) 3466 { 3467 CPUState *cpu = env_cpu((CPUArchState *)env); 3468 TaskState *ts = (TaskState *)cpu->opaque; 3469 struct elf_thread_status *ets; 3470 3471 ets = g_malloc0(sizeof (*ets)); 3472 ets->num_notes = 1; /* only prstatus is dumped */ 3473 fill_prstatus(&ets->prstatus, ts, 0); 3474 elf_core_copy_regs(&ets->prstatus.pr_reg, env); 3475 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus), 3476 &ets->prstatus); 3477 3478 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link); 3479 3480 info->notes_size += note_size(&ets->notes[0]); 3481 } 3482 3483 static void init_note_info(struct elf_note_info *info) 3484 { 3485 /* Initialize the elf_note_info structure so that it is at 3486 * least safe to call free_note_info() on it. Must be 3487 * called before calling fill_note_info(). 3488 */ 3489 memset(info, 0, sizeof (*info)); 3490 QTAILQ_INIT(&info->thread_list); 3491 } 3492 3493 static int fill_note_info(struct elf_note_info *info, 3494 long signr, const CPUArchState *env) 3495 { 3496 #define NUMNOTES 3 3497 CPUState *cpu = env_cpu((CPUArchState *)env); 3498 TaskState *ts = (TaskState *)cpu->opaque; 3499 int i; 3500 3501 info->notes = g_new0(struct memelfnote, NUMNOTES); 3502 if (info->notes == NULL) 3503 return (-ENOMEM); 3504 info->prstatus = g_malloc0(sizeof (*info->prstatus)); 3505 if (info->prstatus == NULL) 3506 return (-ENOMEM); 3507 info->psinfo = g_malloc0(sizeof (*info->psinfo)); 3508 if (info->prstatus == NULL) 3509 return (-ENOMEM); 3510 3511 /* 3512 * First fill in status (and registers) of current thread 3513 * including process info & aux vector. 3514 */ 3515 fill_prstatus(info->prstatus, ts, signr); 3516 elf_core_copy_regs(&info->prstatus->pr_reg, env); 3517 fill_note(&info->notes[0], "CORE", NT_PRSTATUS, 3518 sizeof (*info->prstatus), info->prstatus); 3519 fill_psinfo(info->psinfo, ts); 3520 fill_note(&info->notes[1], "CORE", NT_PRPSINFO, 3521 sizeof (*info->psinfo), info->psinfo); 3522 fill_auxv_note(&info->notes[2], ts); 3523 info->numnote = 3; 3524 3525 info->notes_size = 0; 3526 for (i = 0; i < info->numnote; i++) 3527 info->notes_size += note_size(&info->notes[i]); 3528 3529 /* read and fill status of all threads */ 3530 cpu_list_lock(); 3531 CPU_FOREACH(cpu) { 3532 if (cpu == thread_cpu) { 3533 continue; 3534 } 3535 fill_thread_info(info, (CPUArchState *)cpu->env_ptr); 3536 } 3537 cpu_list_unlock(); 3538 3539 return (0); 3540 } 3541 3542 static void free_note_info(struct elf_note_info *info) 3543 { 3544 struct elf_thread_status *ets; 3545 3546 while (!QTAILQ_EMPTY(&info->thread_list)) { 3547 ets = QTAILQ_FIRST(&info->thread_list); 3548 QTAILQ_REMOVE(&info->thread_list, ets, ets_link); 3549 g_free(ets); 3550 } 3551 3552 g_free(info->prstatus); 3553 g_free(info->psinfo); 3554 g_free(info->notes); 3555 } 3556 3557 static int write_note_info(struct elf_note_info *info, int fd) 3558 { 3559 struct elf_thread_status *ets; 3560 int i, error = 0; 3561 3562 /* write prstatus, psinfo and auxv for current thread */ 3563 for (i = 0; i < info->numnote; i++) 3564 if ((error = write_note(&info->notes[i], fd)) != 0) 3565 return (error); 3566 3567 /* write prstatus for each thread */ 3568 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) { 3569 if ((error = write_note(&ets->notes[0], fd)) != 0) 3570 return (error); 3571 } 3572 3573 return (0); 3574 } 3575 3576 /* 3577 * Write out ELF coredump. 3578 * 3579 * See documentation of ELF object file format in: 3580 * http://www.caldera.com/developers/devspecs/gabi41.pdf 3581 * 3582 * Coredump format in linux is following: 3583 * 3584 * 0 +----------------------+ \ 3585 * | ELF header | ET_CORE | 3586 * +----------------------+ | 3587 * | ELF program headers | |--- headers 3588 * | - NOTE section | | 3589 * | - PT_LOAD sections | | 3590 * +----------------------+ / 3591 * | NOTEs: | 3592 * | - NT_PRSTATUS | 3593 * | - NT_PRSINFO | 3594 * | - NT_AUXV | 3595 * +----------------------+ <-- aligned to target page 3596 * | Process memory dump | 3597 * : : 3598 * . . 3599 * : : 3600 * | | 3601 * +----------------------+ 3602 * 3603 * NT_PRSTATUS -> struct elf_prstatus (per thread) 3604 * NT_PRSINFO -> struct elf_prpsinfo 3605 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). 3606 * 3607 * Format follows System V format as close as possible. Current 3608 * version limitations are as follows: 3609 * - no floating point registers are dumped 3610 * 3611 * Function returns 0 in case of success, negative errno otherwise. 3612 * 3613 * TODO: make this work also during runtime: it should be 3614 * possible to force coredump from running process and then 3615 * continue processing. For example qemu could set up SIGUSR2 3616 * handler (provided that target process haven't registered 3617 * handler for that) that does the dump when signal is received. 3618 */ 3619 static int elf_core_dump(int signr, const CPUArchState *env) 3620 { 3621 const CPUState *cpu = env_cpu((CPUArchState *)env); 3622 const TaskState *ts = (const TaskState *)cpu->opaque; 3623 struct vm_area_struct *vma = NULL; 3624 char corefile[PATH_MAX]; 3625 struct elf_note_info info; 3626 struct elfhdr elf; 3627 struct elf_phdr phdr; 3628 struct rlimit dumpsize; 3629 struct mm_struct *mm = NULL; 3630 off_t offset = 0, data_offset = 0; 3631 int segs = 0; 3632 int fd = -1; 3633 3634 init_note_info(&info); 3635 3636 errno = 0; 3637 getrlimit(RLIMIT_CORE, &dumpsize); 3638 if (dumpsize.rlim_cur == 0) 3639 return 0; 3640 3641 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0) 3642 return (-errno); 3643 3644 if ((fd = open(corefile, O_WRONLY | O_CREAT, 3645 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 3646 return (-errno); 3647 3648 /* 3649 * Walk through target process memory mappings and 3650 * set up structure containing this information. After 3651 * this point vma_xxx functions can be used. 3652 */ 3653 if ((mm = vma_init()) == NULL) 3654 goto out; 3655 3656 walk_memory_regions(mm, vma_walker); 3657 segs = vma_get_mapping_count(mm); 3658 3659 /* 3660 * Construct valid coredump ELF header. We also 3661 * add one more segment for notes. 3662 */ 3663 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0); 3664 if (dump_write(fd, &elf, sizeof (elf)) != 0) 3665 goto out; 3666 3667 /* fill in the in-memory version of notes */ 3668 if (fill_note_info(&info, signr, env) < 0) 3669 goto out; 3670 3671 offset += sizeof (elf); /* elf header */ 3672 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */ 3673 3674 /* write out notes program header */ 3675 fill_elf_note_phdr(&phdr, info.notes_size, offset); 3676 3677 offset += info.notes_size; 3678 if (dump_write(fd, &phdr, sizeof (phdr)) != 0) 3679 goto out; 3680 3681 /* 3682 * ELF specification wants data to start at page boundary so 3683 * we align it here. 3684 */ 3685 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE); 3686 3687 /* 3688 * Write program headers for memory regions mapped in 3689 * the target process. 3690 */ 3691 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) { 3692 (void) memset(&phdr, 0, sizeof (phdr)); 3693 3694 phdr.p_type = PT_LOAD; 3695 phdr.p_offset = offset; 3696 phdr.p_vaddr = vma->vma_start; 3697 phdr.p_paddr = 0; 3698 phdr.p_filesz = vma_dump_size(vma); 3699 offset += phdr.p_filesz; 3700 phdr.p_memsz = vma->vma_end - vma->vma_start; 3701 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0; 3702 if (vma->vma_flags & PROT_WRITE) 3703 phdr.p_flags |= PF_W; 3704 if (vma->vma_flags & PROT_EXEC) 3705 phdr.p_flags |= PF_X; 3706 phdr.p_align = ELF_EXEC_PAGESIZE; 3707 3708 bswap_phdr(&phdr, 1); 3709 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) { 3710 goto out; 3711 } 3712 } 3713 3714 /* 3715 * Next we write notes just after program headers. No 3716 * alignment needed here. 3717 */ 3718 if (write_note_info(&info, fd) < 0) 3719 goto out; 3720 3721 /* align data to page boundary */ 3722 if (lseek(fd, data_offset, SEEK_SET) != data_offset) 3723 goto out; 3724 3725 /* 3726 * Finally we can dump process memory into corefile as well. 3727 */ 3728 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) { 3729 abi_ulong addr; 3730 abi_ulong end; 3731 3732 end = vma->vma_start + vma_dump_size(vma); 3733 3734 for (addr = vma->vma_start; addr < end; 3735 addr += TARGET_PAGE_SIZE) { 3736 char page[TARGET_PAGE_SIZE]; 3737 int error; 3738 3739 /* 3740 * Read in page from target process memory and 3741 * write it to coredump file. 3742 */ 3743 error = copy_from_user(page, addr, sizeof (page)); 3744 if (error != 0) { 3745 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n", 3746 addr); 3747 errno = -error; 3748 goto out; 3749 } 3750 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0) 3751 goto out; 3752 } 3753 } 3754 3755 out: 3756 free_note_info(&info); 3757 if (mm != NULL) 3758 vma_delete(mm); 3759 (void) close(fd); 3760 3761 if (errno != 0) 3762 return (-errno); 3763 return (0); 3764 } 3765 #endif /* USE_ELF_CORE_DUMP */ 3766 3767 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) 3768 { 3769 init_thread(regs, infop); 3770 } 3771