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