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