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