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