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