blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1 | /* This is the Linux kernel elf-loading code, ported into user space */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <errno.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "qemu.h" |
Paolo Bonzini | 76cad71 | 2012-10-24 11:12:21 +0200 | [diff] [blame] | 13 | #include "disas/disas.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 14 | |
malc | e58ffeb | 2009-01-14 18:39:49 +0000 | [diff] [blame] | 15 | #ifdef _ARCH_PPC64 |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 16 | #undef ARCH_DLINFO |
| 17 | #undef ELF_PLATFORM |
| 18 | #undef ELF_HWCAP |
| 19 | #undef ELF_CLASS |
| 20 | #undef ELF_DATA |
| 21 | #undef ELF_ARCH |
| 22 | #endif |
| 23 | |
| 24 | /* from personality.h */ |
| 25 | |
| 26 | /* |
| 27 | * Flags for bug emulation. |
| 28 | * |
| 29 | * These occupy the top three bytes. |
| 30 | */ |
| 31 | enum { |
| 32 | ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */ |
| 33 | FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to descriptors |
| 34 | * (signal handling) |
| 35 | */ |
| 36 | MMAP_PAGE_ZERO = 0x0100000, |
| 37 | ADDR_COMPAT_LAYOUT = 0x0200000, |
| 38 | READ_IMPLIES_EXEC = 0x0400000, |
| 39 | ADDR_LIMIT_32BIT = 0x0800000, |
| 40 | SHORT_INODE = 0x1000000, |
| 41 | WHOLE_SECONDS = 0x2000000, |
| 42 | STICKY_TIMEOUTS = 0x4000000, |
| 43 | ADDR_LIMIT_3GB = 0x8000000, |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Personality types. |
| 48 | * |
| 49 | * These go in the low byte. Avoid using the top bit, it will |
| 50 | * conflict with error returns. |
| 51 | */ |
| 52 | enum { |
| 53 | PER_LINUX = 0x0000, |
| 54 | PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT, |
| 55 | PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS, |
| 56 | PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, |
| 57 | PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE, |
| 58 | PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | |
| 59 | WHOLE_SECONDS | SHORT_INODE, |
| 60 | PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS, |
| 61 | PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE, |
| 62 | PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS, |
| 63 | PER_BSD = 0x0006, |
| 64 | PER_SUNOS = 0x0006 | STICKY_TIMEOUTS, |
| 65 | PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE, |
| 66 | PER_LINUX32 = 0x0008, |
| 67 | PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB, |
| 68 | PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */ |
| 69 | PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */ |
| 70 | PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */ |
| 71 | PER_RISCOS = 0x000c, |
| 72 | PER_SOLARIS = 0x000d | STICKY_TIMEOUTS, |
| 73 | PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, |
| 74 | PER_OSF4 = 0x000f, /* OSF/1 v4 */ |
| 75 | PER_HPUX = 0x0010, |
| 76 | PER_MASK = 0x00ff, |
| 77 | }; |
| 78 | |
| 79 | /* |
| 80 | * Return the base personality without flags. |
| 81 | */ |
| 82 | #define personality(pers) (pers & PER_MASK) |
| 83 | |
| 84 | /* this flag is uneffective under linux too, should be deleted */ |
| 85 | #ifndef MAP_DENYWRITE |
| 86 | #define MAP_DENYWRITE 0 |
| 87 | #endif |
| 88 | |
| 89 | /* should probably go in elf.h */ |
| 90 | #ifndef ELIBBAD |
| 91 | #define ELIBBAD 80 |
| 92 | #endif |
| 93 | |
| 94 | #ifdef TARGET_I386 |
| 95 | |
| 96 | #define ELF_PLATFORM get_elf_platform() |
| 97 | |
| 98 | static const char *get_elf_platform(void) |
| 99 | { |
| 100 | static char elf_platform[] = "i386"; |
Andreas Färber | dca1173 | 2013-06-09 19:51:23 +0200 | [diff] [blame] | 101 | int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 102 | if (family > 6) |
| 103 | family = 6; |
| 104 | if (family >= 3) |
| 105 | elf_platform[1] = '0' + family; |
| 106 | return elf_platform; |
| 107 | } |
| 108 | |
| 109 | #define ELF_HWCAP get_elf_hwcap() |
| 110 | |
| 111 | static uint32_t get_elf_hwcap(void) |
| 112 | { |
Andreas Färber | dca1173 | 2013-06-09 19:51:23 +0200 | [diff] [blame] | 113 | X86CPU *cpu = X86_CPU(thread_cpu); |
| 114 | |
| 115 | return cpu->env.features[FEAT_1_EDX]; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #ifdef TARGET_X86_64 |
| 119 | #define ELF_START_MMAP 0x2aaaaab000ULL |
| 120 | #define elf_check_arch(x) ( ((x) == ELF_ARCH) ) |
| 121 | |
| 122 | #define ELF_CLASS ELFCLASS64 |
| 123 | #define ELF_DATA ELFDATA2LSB |
| 124 | #define ELF_ARCH EM_X86_64 |
| 125 | |
| 126 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 127 | { |
| 128 | regs->rax = 0; |
| 129 | regs->rsp = infop->start_stack; |
| 130 | regs->rip = infop->entry; |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 131 | if (bsd_type == target_freebsd) { |
| 132 | regs->rdi = infop->start_stack; |
| 133 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | #else |
| 137 | |
| 138 | #define ELF_START_MMAP 0x80000000 |
| 139 | |
| 140 | /* |
| 141 | * This is used to ensure we don't load something for the wrong architecture. |
| 142 | */ |
| 143 | #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) |
| 144 | |
| 145 | /* |
| 146 | * These are used to set parameters in the core dumps. |
| 147 | */ |
| 148 | #define ELF_CLASS ELFCLASS32 |
| 149 | #define ELF_DATA ELFDATA2LSB |
| 150 | #define ELF_ARCH EM_386 |
| 151 | |
| 152 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 153 | { |
| 154 | regs->esp = infop->start_stack; |
| 155 | regs->eip = infop->entry; |
| 156 | |
| 157 | /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program |
| 158 | starts %edx contains a pointer to a function which might be |
| 159 | registered using `atexit'. This provides a mean for the |
| 160 | dynamic linker to call DT_FINI functions for shared libraries |
| 161 | that have been loaded before the code runs. |
| 162 | |
| 163 | A value of 0 tells we have no such handler. */ |
| 164 | regs->edx = 0; |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | #define USE_ELF_CORE_DUMP |
| 169 | #define ELF_EXEC_PAGESIZE 4096 |
| 170 | |
| 171 | #endif |
| 172 | |
| 173 | #ifdef TARGET_ARM |
| 174 | |
| 175 | #define ELF_START_MMAP 0x80000000 |
| 176 | |
| 177 | #define elf_check_arch(x) ( (x) == EM_ARM ) |
| 178 | |
| 179 | #define ELF_CLASS ELFCLASS32 |
| 180 | #ifdef TARGET_WORDS_BIGENDIAN |
| 181 | #define ELF_DATA ELFDATA2MSB |
| 182 | #else |
| 183 | #define ELF_DATA ELFDATA2LSB |
| 184 | #endif |
| 185 | #define ELF_ARCH EM_ARM |
| 186 | |
| 187 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 188 | { |
| 189 | abi_long stack = infop->start_stack; |
| 190 | memset(regs, 0, sizeof(*regs)); |
| 191 | regs->ARM_cpsr = 0x10; |
| 192 | if (infop->entry & 1) |
| 193 | regs->ARM_cpsr |= CPSR_T; |
| 194 | regs->ARM_pc = infop->entry & 0xfffffffe; |
| 195 | regs->ARM_sp = infop->start_stack; |
| 196 | /* FIXME - what to for failure of get_user()? */ |
| 197 | get_user_ual(regs->ARM_r2, stack + 8); /* envp */ |
| 198 | get_user_ual(regs->ARM_r1, stack + 4); /* envp */ |
| 199 | /* XXX: it seems that r0 is zeroed after ! */ |
| 200 | regs->ARM_r0 = 0; |
| 201 | /* For uClinux PIC binaries. */ |
| 202 | /* XXX: Linux does this only on ARM with no MMU (do we care ?) */ |
| 203 | regs->ARM_r10 = infop->start_data; |
| 204 | } |
| 205 | |
| 206 | #define USE_ELF_CORE_DUMP |
| 207 | #define ELF_EXEC_PAGESIZE 4096 |
| 208 | |
| 209 | enum |
| 210 | { |
| 211 | ARM_HWCAP_ARM_SWP = 1 << 0, |
| 212 | ARM_HWCAP_ARM_HALF = 1 << 1, |
| 213 | ARM_HWCAP_ARM_THUMB = 1 << 2, |
| 214 | ARM_HWCAP_ARM_26BIT = 1 << 3, |
| 215 | ARM_HWCAP_ARM_FAST_MULT = 1 << 4, |
| 216 | ARM_HWCAP_ARM_FPA = 1 << 5, |
| 217 | ARM_HWCAP_ARM_VFP = 1 << 6, |
| 218 | ARM_HWCAP_ARM_EDSP = 1 << 7, |
| 219 | }; |
| 220 | |
| 221 | #define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \ |
| 222 | | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \ |
| 223 | | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP) |
| 224 | |
| 225 | #endif |
| 226 | |
| 227 | #ifdef TARGET_SPARC |
| 228 | #ifdef TARGET_SPARC64 |
| 229 | |
| 230 | #define ELF_START_MMAP 0x80000000 |
| 231 | |
| 232 | #ifndef TARGET_ABI32 |
| 233 | #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS ) |
| 234 | #else |
| 235 | #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC ) |
| 236 | #endif |
| 237 | |
| 238 | #define ELF_CLASS ELFCLASS64 |
| 239 | #define ELF_DATA ELFDATA2MSB |
| 240 | #define ELF_ARCH EM_SPARCV9 |
| 241 | |
| 242 | #define STACK_BIAS 2047 |
| 243 | |
| 244 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 245 | { |
| 246 | #ifndef TARGET_ABI32 |
| 247 | regs->tstate = 0; |
| 248 | #endif |
| 249 | regs->pc = infop->entry; |
| 250 | regs->npc = regs->pc + 4; |
| 251 | regs->y = 0; |
| 252 | #ifdef TARGET_ABI32 |
| 253 | regs->u_regs[14] = infop->start_stack - 16 * 4; |
| 254 | #else |
| 255 | if (personality(infop->personality) == PER_LINUX32) |
| 256 | regs->u_regs[14] = infop->start_stack - 16 * 4; |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 257 | else { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 258 | regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS; |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 259 | if (bsd_type == target_freebsd) { |
| 260 | regs->u_regs[8] = infop->start_stack; |
| 261 | regs->u_regs[11] = infop->start_stack; |
| 262 | } |
| 263 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 264 | #endif |
| 265 | } |
| 266 | |
| 267 | #else |
| 268 | #define ELF_START_MMAP 0x80000000 |
| 269 | |
| 270 | #define elf_check_arch(x) ( (x) == EM_SPARC ) |
| 271 | |
| 272 | #define ELF_CLASS ELFCLASS32 |
| 273 | #define ELF_DATA ELFDATA2MSB |
| 274 | #define ELF_ARCH EM_SPARC |
| 275 | |
| 276 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 277 | { |
| 278 | regs->psr = 0; |
| 279 | regs->pc = infop->entry; |
| 280 | regs->npc = regs->pc + 4; |
| 281 | regs->y = 0; |
| 282 | regs->u_regs[14] = infop->start_stack - 16 * 4; |
| 283 | } |
| 284 | |
| 285 | #endif |
| 286 | #endif |
| 287 | |
| 288 | #ifdef TARGET_PPC |
| 289 | |
| 290 | #define ELF_START_MMAP 0x80000000 |
| 291 | |
| 292 | #if defined(TARGET_PPC64) && !defined(TARGET_ABI32) |
| 293 | |
| 294 | #define elf_check_arch(x) ( (x) == EM_PPC64 ) |
| 295 | |
| 296 | #define ELF_CLASS ELFCLASS64 |
| 297 | |
| 298 | #else |
| 299 | |
| 300 | #define elf_check_arch(x) ( (x) == EM_PPC ) |
| 301 | |
| 302 | #define ELF_CLASS ELFCLASS32 |
| 303 | |
| 304 | #endif |
| 305 | |
| 306 | #ifdef TARGET_WORDS_BIGENDIAN |
| 307 | #define ELF_DATA ELFDATA2MSB |
| 308 | #else |
| 309 | #define ELF_DATA ELFDATA2LSB |
| 310 | #endif |
| 311 | #define ELF_ARCH EM_PPC |
| 312 | |
| 313 | /* |
| 314 | * We need to put in some extra aux table entries to tell glibc what |
| 315 | * the cache block size is, so it can use the dcbz instruction safely. |
| 316 | */ |
| 317 | #define AT_DCACHEBSIZE 19 |
| 318 | #define AT_ICACHEBSIZE 20 |
| 319 | #define AT_UCACHEBSIZE 21 |
| 320 | /* A special ignored type value for PPC, for glibc compatibility. */ |
| 321 | #define AT_IGNOREPPC 22 |
| 322 | /* |
| 323 | * The requirements here are: |
| 324 | * - keep the final alignment of sp (sp & 0xf) |
| 325 | * - make sure the 32-bit value at the first 16 byte aligned position of |
| 326 | * AUXV is greater than 16 for glibc compatibility. |
| 327 | * AT_IGNOREPPC is used for that. |
| 328 | * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC, |
| 329 | * even if DLINFO_ARCH_ITEMS goes to zero or is undefined. |
| 330 | */ |
| 331 | #define DLINFO_ARCH_ITEMS 5 |
| 332 | #define ARCH_DLINFO \ |
| 333 | do { \ |
| 334 | NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \ |
| 335 | NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \ |
| 336 | NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \ |
| 337 | /* \ |
| 338 | * Now handle glibc compatibility. \ |
| 339 | */ \ |
| 340 | NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ |
| 341 | NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ |
| 342 | } while (0) |
| 343 | |
| 344 | static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop) |
| 345 | { |
| 346 | abi_ulong pos = infop->start_stack; |
| 347 | abi_ulong tmp; |
| 348 | #if defined(TARGET_PPC64) && !defined(TARGET_ABI32) |
| 349 | abi_ulong entry, toc; |
| 350 | #endif |
| 351 | |
| 352 | _regs->gpr[1] = infop->start_stack; |
| 353 | #if defined(TARGET_PPC64) && !defined(TARGET_ABI32) |
Peter Maydell | b8d6ac9 | 2015-01-20 15:19:33 +0000 | [diff] [blame] | 354 | get_user_u64(entry, infop->entry); |
| 355 | entry += infop->load_addr; |
| 356 | get_user_u64(toc, infop->entry + 8); |
| 357 | toc += infop->load_addr; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 358 | _regs->gpr[2] = toc; |
| 359 | infop->entry = entry; |
| 360 | #endif |
| 361 | _regs->nip = infop->entry; |
| 362 | /* Note that isn't exactly what regular kernel does |
| 363 | * but this is what the ABI wants and is needed to allow |
| 364 | * execution of PPC BSD programs. |
| 365 | */ |
| 366 | /* FIXME - what to for failure of get_user()? */ |
| 367 | get_user_ual(_regs->gpr[3], pos); |
| 368 | pos += sizeof(abi_ulong); |
| 369 | _regs->gpr[4] = pos; |
Peter Maydell | b8d6ac9 | 2015-01-20 15:19:33 +0000 | [diff] [blame] | 370 | for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong)) { |
| 371 | get_user_ual(tmp, pos); |
| 372 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 373 | _regs->gpr[5] = pos; |
| 374 | } |
| 375 | |
| 376 | #define USE_ELF_CORE_DUMP |
| 377 | #define ELF_EXEC_PAGESIZE 4096 |
| 378 | |
| 379 | #endif |
| 380 | |
| 381 | #ifdef TARGET_MIPS |
| 382 | |
| 383 | #define ELF_START_MMAP 0x80000000 |
| 384 | |
| 385 | #define elf_check_arch(x) ( (x) == EM_MIPS ) |
| 386 | |
| 387 | #ifdef TARGET_MIPS64 |
| 388 | #define ELF_CLASS ELFCLASS64 |
| 389 | #else |
| 390 | #define ELF_CLASS ELFCLASS32 |
| 391 | #endif |
| 392 | #ifdef TARGET_WORDS_BIGENDIAN |
| 393 | #define ELF_DATA ELFDATA2MSB |
| 394 | #else |
| 395 | #define ELF_DATA ELFDATA2LSB |
| 396 | #endif |
| 397 | #define ELF_ARCH EM_MIPS |
| 398 | |
| 399 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 400 | { |
| 401 | regs->cp0_status = 2 << CP0St_KSU; |
| 402 | regs->cp0_epc = infop->entry; |
| 403 | regs->regs[29] = infop->start_stack; |
| 404 | } |
| 405 | |
| 406 | #define USE_ELF_CORE_DUMP |
| 407 | #define ELF_EXEC_PAGESIZE 4096 |
| 408 | |
| 409 | #endif /* TARGET_MIPS */ |
| 410 | |
| 411 | #ifdef TARGET_SH4 |
| 412 | |
| 413 | #define ELF_START_MMAP 0x80000000 |
| 414 | |
| 415 | #define elf_check_arch(x) ( (x) == EM_SH ) |
| 416 | |
| 417 | #define ELF_CLASS ELFCLASS32 |
| 418 | #define ELF_DATA ELFDATA2LSB |
| 419 | #define ELF_ARCH EM_SH |
| 420 | |
| 421 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 422 | { |
| 423 | /* Check other registers XXXXX */ |
| 424 | regs->pc = infop->entry; |
| 425 | regs->regs[15] = infop->start_stack; |
| 426 | } |
| 427 | |
| 428 | #define USE_ELF_CORE_DUMP |
| 429 | #define ELF_EXEC_PAGESIZE 4096 |
| 430 | |
| 431 | #endif |
| 432 | |
| 433 | #ifdef TARGET_CRIS |
| 434 | |
| 435 | #define ELF_START_MMAP 0x80000000 |
| 436 | |
| 437 | #define elf_check_arch(x) ( (x) == EM_CRIS ) |
| 438 | |
| 439 | #define ELF_CLASS ELFCLASS32 |
| 440 | #define ELF_DATA ELFDATA2LSB |
| 441 | #define ELF_ARCH EM_CRIS |
| 442 | |
| 443 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 444 | { |
| 445 | regs->erp = infop->entry; |
| 446 | } |
| 447 | |
| 448 | #define USE_ELF_CORE_DUMP |
| 449 | #define ELF_EXEC_PAGESIZE 8192 |
| 450 | |
| 451 | #endif |
| 452 | |
| 453 | #ifdef TARGET_M68K |
| 454 | |
| 455 | #define ELF_START_MMAP 0x80000000 |
| 456 | |
| 457 | #define elf_check_arch(x) ( (x) == EM_68K ) |
| 458 | |
| 459 | #define ELF_CLASS ELFCLASS32 |
| 460 | #define ELF_DATA ELFDATA2MSB |
| 461 | #define ELF_ARCH EM_68K |
| 462 | |
| 463 | /* ??? Does this need to do anything? |
| 464 | #define ELF_PLAT_INIT(_r) */ |
| 465 | |
| 466 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 467 | { |
| 468 | regs->usp = infop->start_stack; |
| 469 | regs->sr = 0; |
| 470 | regs->pc = infop->entry; |
| 471 | } |
| 472 | |
| 473 | #define USE_ELF_CORE_DUMP |
| 474 | #define ELF_EXEC_PAGESIZE 8192 |
| 475 | |
| 476 | #endif |
| 477 | |
| 478 | #ifdef TARGET_ALPHA |
| 479 | |
| 480 | #define ELF_START_MMAP (0x30000000000ULL) |
| 481 | |
| 482 | #define elf_check_arch(x) ( (x) == ELF_ARCH ) |
| 483 | |
| 484 | #define ELF_CLASS ELFCLASS64 |
| 485 | #define ELF_DATA ELFDATA2MSB |
| 486 | #define ELF_ARCH EM_ALPHA |
| 487 | |
| 488 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 489 | { |
| 490 | regs->pc = infop->entry; |
| 491 | regs->ps = 8; |
| 492 | regs->usp = infop->start_stack; |
| 493 | regs->unique = infop->start_data; /* ? */ |
| 494 | printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n", |
| 495 | regs->unique, infop->start_data); |
| 496 | } |
| 497 | |
| 498 | #define USE_ELF_CORE_DUMP |
| 499 | #define ELF_EXEC_PAGESIZE 8192 |
| 500 | |
| 501 | #endif /* TARGET_ALPHA */ |
| 502 | |
| 503 | #ifndef ELF_PLATFORM |
| 504 | #define ELF_PLATFORM (NULL) |
| 505 | #endif |
| 506 | |
| 507 | #ifndef ELF_HWCAP |
| 508 | #define ELF_HWCAP 0 |
| 509 | #endif |
| 510 | |
| 511 | #ifdef TARGET_ABI32 |
| 512 | #undef ELF_CLASS |
| 513 | #define ELF_CLASS ELFCLASS32 |
| 514 | #undef bswaptls |
| 515 | #define bswaptls(ptr) bswap32s(ptr) |
| 516 | #endif |
| 517 | |
| 518 | #include "elf.h" |
| 519 | |
| 520 | struct exec |
| 521 | { |
| 522 | unsigned int a_info; /* Use macros N_MAGIC, etc for access */ |
| 523 | unsigned int a_text; /* length of text, in bytes */ |
| 524 | unsigned int a_data; /* length of data, in bytes */ |
| 525 | unsigned int a_bss; /* length of uninitialized data area, in bytes */ |
| 526 | unsigned int a_syms; /* length of symbol table data in file, in bytes */ |
| 527 | unsigned int a_entry; /* start address */ |
| 528 | unsigned int a_trsize; /* length of relocation info for text, in bytes */ |
| 529 | unsigned int a_drsize; /* length of relocation info for data, in bytes */ |
| 530 | }; |
| 531 | |
| 532 | |
| 533 | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
| 534 | #define OMAGIC 0407 |
| 535 | #define NMAGIC 0410 |
| 536 | #define ZMAGIC 0413 |
| 537 | #define QMAGIC 0314 |
| 538 | |
| 539 | /* max code+data+bss space allocated to elf interpreter */ |
| 540 | #define INTERP_MAP_SIZE (32 * 1024 * 1024) |
| 541 | |
| 542 | /* max code+data+bss+brk space allocated to ET_DYN executables */ |
| 543 | #define ET_DYN_MAP_SIZE (128 * 1024 * 1024) |
| 544 | |
| 545 | /* Necessary parameters */ |
| 546 | #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE |
| 547 | #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1)) |
| 548 | #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1)) |
| 549 | |
| 550 | #define INTERPRETER_NONE 0 |
| 551 | #define INTERPRETER_AOUT 1 |
| 552 | #define INTERPRETER_ELF 2 |
| 553 | |
| 554 | #define DLINFO_ITEMS 12 |
| 555 | |
| 556 | static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) |
| 557 | { |
| 558 | memcpy(to, from, n); |
| 559 | } |
| 560 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 561 | static int load_aout_interp(void * exptr, int interp_fd); |
| 562 | |
| 563 | #ifdef BSWAP_NEEDED |
| 564 | static void bswap_ehdr(struct elfhdr *ehdr) |
| 565 | { |
| 566 | bswap16s(&ehdr->e_type); /* Object file type */ |
| 567 | bswap16s(&ehdr->e_machine); /* Architecture */ |
| 568 | bswap32s(&ehdr->e_version); /* Object file version */ |
| 569 | bswaptls(&ehdr->e_entry); /* Entry point virtual address */ |
| 570 | bswaptls(&ehdr->e_phoff); /* Program header table file offset */ |
| 571 | bswaptls(&ehdr->e_shoff); /* Section header table file offset */ |
| 572 | bswap32s(&ehdr->e_flags); /* Processor-specific flags */ |
| 573 | bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ |
| 574 | bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ |
| 575 | bswap16s(&ehdr->e_phnum); /* Program header table entry count */ |
| 576 | bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ |
| 577 | bswap16s(&ehdr->e_shnum); /* Section header table entry count */ |
| 578 | bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ |
| 579 | } |
| 580 | |
| 581 | static void bswap_phdr(struct elf_phdr *phdr) |
| 582 | { |
| 583 | bswap32s(&phdr->p_type); /* Segment type */ |
| 584 | bswaptls(&phdr->p_offset); /* Segment file offset */ |
| 585 | bswaptls(&phdr->p_vaddr); /* Segment virtual address */ |
| 586 | bswaptls(&phdr->p_paddr); /* Segment physical address */ |
| 587 | bswaptls(&phdr->p_filesz); /* Segment size in file */ |
| 588 | bswaptls(&phdr->p_memsz); /* Segment size in memory */ |
| 589 | bswap32s(&phdr->p_flags); /* Segment flags */ |
| 590 | bswaptls(&phdr->p_align); /* Segment alignment */ |
| 591 | } |
| 592 | |
| 593 | static void bswap_shdr(struct elf_shdr *shdr) |
| 594 | { |
| 595 | bswap32s(&shdr->sh_name); |
| 596 | bswap32s(&shdr->sh_type); |
| 597 | bswaptls(&shdr->sh_flags); |
| 598 | bswaptls(&shdr->sh_addr); |
| 599 | bswaptls(&shdr->sh_offset); |
| 600 | bswaptls(&shdr->sh_size); |
| 601 | bswap32s(&shdr->sh_link); |
| 602 | bswap32s(&shdr->sh_info); |
| 603 | bswaptls(&shdr->sh_addralign); |
| 604 | bswaptls(&shdr->sh_entsize); |
| 605 | } |
| 606 | |
| 607 | static void bswap_sym(struct elf_sym *sym) |
| 608 | { |
| 609 | bswap32s(&sym->st_name); |
| 610 | bswaptls(&sym->st_value); |
| 611 | bswaptls(&sym->st_size); |
| 612 | bswap16s(&sym->st_shndx); |
| 613 | } |
| 614 | #endif |
| 615 | |
| 616 | /* |
| 617 | * 'copy_elf_strings()' copies argument/envelope strings from user |
| 618 | * memory to free pages in kernel mem. These are in a format ready |
| 619 | * to be put directly into the top of new user memory. |
| 620 | * |
| 621 | */ |
| 622 | static abi_ulong copy_elf_strings(int argc,char ** argv, void **page, |
| 623 | abi_ulong p) |
| 624 | { |
| 625 | char *tmp, *tmp1, *pag = NULL; |
| 626 | int len, offset = 0; |
| 627 | |
| 628 | if (!p) { |
| 629 | return 0; /* bullet-proofing */ |
| 630 | } |
| 631 | while (argc-- > 0) { |
| 632 | tmp = argv[argc]; |
| 633 | if (!tmp) { |
Peter Maydell | 9bb9318 | 2014-06-02 13:24:37 +0100 | [diff] [blame] | 634 | fprintf(stderr, "VFS: argc is wrong"); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 635 | exit(-1); |
| 636 | } |
| 637 | tmp1 = tmp; |
| 638 | while (*tmp++); |
| 639 | len = tmp - tmp1; |
| 640 | if (p < len) { /* this shouldn't happen - 128kB */ |
| 641 | return 0; |
| 642 | } |
| 643 | while (len) { |
| 644 | --p; --tmp; --len; |
| 645 | if (--offset < 0) { |
| 646 | offset = p % TARGET_PAGE_SIZE; |
| 647 | pag = (char *)page[p/TARGET_PAGE_SIZE]; |
| 648 | if (!pag) { |
Stefan Weil | c580dee | 2011-11-21 21:06:22 +0100 | [diff] [blame] | 649 | pag = g_try_malloc0(TARGET_PAGE_SIZE); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 650 | page[p/TARGET_PAGE_SIZE] = pag; |
| 651 | if (!pag) |
| 652 | return 0; |
| 653 | } |
| 654 | } |
| 655 | if (len == 0 || offset == 0) { |
| 656 | *(pag + offset) = *tmp; |
| 657 | } |
| 658 | else { |
| 659 | int bytes_to_copy = (len > offset) ? offset : len; |
| 660 | tmp -= bytes_to_copy; |
| 661 | p -= bytes_to_copy; |
| 662 | offset -= bytes_to_copy; |
| 663 | len -= bytes_to_copy; |
| 664 | memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | return p; |
| 669 | } |
| 670 | |
| 671 | static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm, |
| 672 | struct image_info *info) |
| 673 | { |
| 674 | abi_ulong stack_base, size, error; |
| 675 | int i; |
| 676 | |
| 677 | /* Create enough stack to hold everything. If we don't use |
| 678 | * it for args, we'll use it for something else... |
| 679 | */ |
| 680 | size = x86_stack_size; |
| 681 | if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) |
| 682 | size = MAX_ARG_PAGES*TARGET_PAGE_SIZE; |
| 683 | error = target_mmap(0, |
| 684 | size + qemu_host_page_size, |
| 685 | PROT_READ | PROT_WRITE, |
| 686 | MAP_PRIVATE | MAP_ANON, |
| 687 | -1, 0); |
| 688 | if (error == -1) { |
| 689 | perror("stk mmap"); |
| 690 | exit(-1); |
| 691 | } |
| 692 | /* we reserve one extra page at the top of the stack as guard */ |
| 693 | target_mprotect(error + size, qemu_host_page_size, PROT_NONE); |
| 694 | |
| 695 | stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE; |
| 696 | p += stack_base; |
| 697 | |
| 698 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
| 699 | if (bprm->page[i]) { |
| 700 | info->rss++; |
| 701 | /* FIXME - check return value of memcpy_to_target() for failure */ |
| 702 | memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE); |
Stefan Weil | c580dee | 2011-11-21 21:06:22 +0100 | [diff] [blame] | 703 | g_free(bprm->page[i]); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 704 | } |
| 705 | stack_base += TARGET_PAGE_SIZE; |
| 706 | } |
| 707 | return p; |
| 708 | } |
| 709 | |
| 710 | static void set_brk(abi_ulong start, abi_ulong end) |
| 711 | { |
| 712 | /* page-align the start and end addresses... */ |
| 713 | start = HOST_PAGE_ALIGN(start); |
| 714 | end = HOST_PAGE_ALIGN(end); |
| 715 | if (end <= start) |
| 716 | return; |
| 717 | if(target_mmap(start, end - start, |
| 718 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 719 | MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) { |
| 720 | perror("cannot mmap brk"); |
| 721 | exit(-1); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | |
| 726 | /* We need to explicitly zero any fractional pages after the data |
| 727 | section (i.e. bss). This would contain the junk from the file that |
| 728 | should not be in memory. */ |
| 729 | static void padzero(abi_ulong elf_bss, abi_ulong last_bss) |
| 730 | { |
| 731 | abi_ulong nbyte; |
| 732 | |
| 733 | if (elf_bss >= last_bss) |
| 734 | return; |
| 735 | |
| 736 | /* XXX: this is really a hack : if the real host page size is |
| 737 | smaller than the target page size, some pages after the end |
| 738 | of the file may not be mapped. A better fix would be to |
| 739 | patch target_mmap(), but it is more complicated as the file |
| 740 | size must be known */ |
| 741 | if (qemu_real_host_page_size < qemu_host_page_size) { |
| 742 | abi_ulong end_addr, end_addr1; |
| 743 | end_addr1 = (elf_bss + qemu_real_host_page_size - 1) & |
| 744 | ~(qemu_real_host_page_size - 1); |
| 745 | end_addr = HOST_PAGE_ALIGN(elf_bss); |
| 746 | if (end_addr1 < end_addr) { |
| 747 | mmap((void *)g2h(end_addr1), end_addr - end_addr1, |
| 748 | PROT_READ|PROT_WRITE|PROT_EXEC, |
| 749 | MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | nbyte = elf_bss & (qemu_host_page_size-1); |
| 754 | if (nbyte) { |
| 755 | nbyte = qemu_host_page_size - nbyte; |
| 756 | do { |
| 757 | /* FIXME - what to do if put_user() fails? */ |
| 758 | put_user_u8(0, elf_bss); |
| 759 | elf_bss++; |
| 760 | } while (--nbyte); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | |
| 765 | static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, |
| 766 | struct elfhdr * exec, |
| 767 | abi_ulong load_addr, |
| 768 | abi_ulong load_bias, |
| 769 | abi_ulong interp_load_addr, int ibcs, |
| 770 | struct image_info *info) |
| 771 | { |
| 772 | abi_ulong sp; |
| 773 | int size; |
| 774 | abi_ulong u_platform; |
| 775 | const char *k_platform; |
| 776 | const int n = sizeof(elf_addr_t); |
| 777 | |
| 778 | sp = p; |
| 779 | u_platform = 0; |
| 780 | k_platform = ELF_PLATFORM; |
| 781 | if (k_platform) { |
| 782 | size_t len = strlen(k_platform) + 1; |
| 783 | sp -= (len + n - 1) & ~(n - 1); |
| 784 | u_platform = sp; |
| 785 | /* FIXME - check return value of memcpy_to_target() for failure */ |
| 786 | memcpy_to_target(sp, k_platform, len); |
| 787 | } |
| 788 | /* |
| 789 | * Force 16 byte _final_ alignment here for generality. |
| 790 | */ |
| 791 | sp = sp &~ (abi_ulong)15; |
| 792 | size = (DLINFO_ITEMS + 1) * 2; |
| 793 | if (k_platform) |
| 794 | size += 2; |
| 795 | #ifdef DLINFO_ARCH_ITEMS |
| 796 | size += DLINFO_ARCH_ITEMS * 2; |
| 797 | #endif |
| 798 | size += envc + argc + 2; |
| 799 | size += (!ibcs ? 3 : 1); /* argc itself */ |
| 800 | size *= n; |
| 801 | if (size & 15) |
| 802 | sp -= 16 - (size & 15); |
| 803 | |
| 804 | /* This is correct because Linux defines |
| 805 | * elf_addr_t as Elf32_Off / Elf64_Off |
| 806 | */ |
| 807 | #define NEW_AUX_ENT(id, val) do { \ |
| 808 | sp -= n; put_user_ual(val, sp); \ |
| 809 | sp -= n; put_user_ual(id, sp); \ |
| 810 | } while(0) |
| 811 | |
| 812 | NEW_AUX_ENT (AT_NULL, 0); |
| 813 | |
| 814 | /* There must be exactly DLINFO_ITEMS entries here. */ |
| 815 | NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff)); |
| 816 | NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); |
| 817 | NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); |
| 818 | NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE)); |
| 819 | NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr)); |
| 820 | NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); |
| 821 | NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry); |
| 822 | NEW_AUX_ENT(AT_UID, (abi_ulong) getuid()); |
| 823 | NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid()); |
| 824 | NEW_AUX_ENT(AT_GID, (abi_ulong) getgid()); |
| 825 | NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid()); |
| 826 | NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP); |
| 827 | NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK)); |
| 828 | if (k_platform) |
| 829 | NEW_AUX_ENT(AT_PLATFORM, u_platform); |
| 830 | #ifdef ARCH_DLINFO |
| 831 | /* |
| 832 | * ARCH_DLINFO must come last so platform specific code can enforce |
| 833 | * special alignment requirements on the AUXV if necessary (eg. PPC). |
| 834 | */ |
| 835 | ARCH_DLINFO; |
| 836 | #endif |
| 837 | #undef NEW_AUX_ENT |
| 838 | |
| 839 | sp = loader_build_argptr(envc, argc, sp, p, !ibcs); |
| 840 | return sp; |
| 841 | } |
| 842 | |
| 843 | |
| 844 | static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex, |
| 845 | int interpreter_fd, |
| 846 | abi_ulong *interp_load_addr) |
| 847 | { |
| 848 | struct elf_phdr *elf_phdata = NULL; |
| 849 | struct elf_phdr *eppnt; |
| 850 | abi_ulong load_addr = 0; |
| 851 | int load_addr_set = 0; |
| 852 | int retval; |
| 853 | abi_ulong last_bss, elf_bss; |
| 854 | abi_ulong error; |
| 855 | int i; |
| 856 | |
| 857 | elf_bss = 0; |
| 858 | last_bss = 0; |
| 859 | error = 0; |
| 860 | |
| 861 | #ifdef BSWAP_NEEDED |
| 862 | bswap_ehdr(interp_elf_ex); |
| 863 | #endif |
| 864 | /* First of all, some simple consistency checks */ |
| 865 | if ((interp_elf_ex->e_type != ET_EXEC && |
| 866 | interp_elf_ex->e_type != ET_DYN) || |
| 867 | !elf_check_arch(interp_elf_ex->e_machine)) { |
| 868 | return ~((abi_ulong)0UL); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | /* Now read in all of the header information */ |
| 873 | |
| 874 | if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE) |
| 875 | return ~(abi_ulong)0UL; |
| 876 | |
| 877 | elf_phdata = (struct elf_phdr *) |
| 878 | malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); |
| 879 | |
| 880 | if (!elf_phdata) |
| 881 | return ~((abi_ulong)0UL); |
| 882 | |
| 883 | /* |
| 884 | * If the size of this structure has changed, then punt, since |
| 885 | * we will be doing the wrong thing. |
| 886 | */ |
| 887 | if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) { |
| 888 | free(elf_phdata); |
| 889 | return ~((abi_ulong)0UL); |
| 890 | } |
| 891 | |
| 892 | retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET); |
| 893 | if(retval >= 0) { |
| 894 | retval = read(interpreter_fd, |
| 895 | (char *) elf_phdata, |
| 896 | sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); |
| 897 | } |
| 898 | if (retval < 0) { |
| 899 | perror("load_elf_interp"); |
| 900 | exit(-1); |
| 901 | free (elf_phdata); |
| 902 | return retval; |
| 903 | } |
| 904 | #ifdef BSWAP_NEEDED |
| 905 | eppnt = elf_phdata; |
| 906 | for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) { |
| 907 | bswap_phdr(eppnt); |
| 908 | } |
| 909 | #endif |
| 910 | |
| 911 | if (interp_elf_ex->e_type == ET_DYN) { |
| 912 | /* in order to avoid hardcoding the interpreter load |
| 913 | address in qemu, we allocate a big enough memory zone */ |
| 914 | error = target_mmap(0, INTERP_MAP_SIZE, |
| 915 | PROT_NONE, MAP_PRIVATE | MAP_ANON, |
| 916 | -1, 0); |
| 917 | if (error == -1) { |
| 918 | perror("mmap"); |
| 919 | exit(-1); |
| 920 | } |
| 921 | load_addr = error; |
| 922 | load_addr_set = 1; |
| 923 | } |
| 924 | |
| 925 | eppnt = elf_phdata; |
| 926 | for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) |
| 927 | if (eppnt->p_type == PT_LOAD) { |
| 928 | int elf_type = MAP_PRIVATE | MAP_DENYWRITE; |
| 929 | int elf_prot = 0; |
| 930 | abi_ulong vaddr = 0; |
| 931 | abi_ulong k; |
| 932 | |
| 933 | if (eppnt->p_flags & PF_R) elf_prot = PROT_READ; |
| 934 | if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE; |
| 935 | if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; |
| 936 | if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) { |
| 937 | elf_type |= MAP_FIXED; |
| 938 | vaddr = eppnt->p_vaddr; |
| 939 | } |
| 940 | error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr), |
| 941 | eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr), |
| 942 | elf_prot, |
| 943 | elf_type, |
| 944 | interpreter_fd, |
| 945 | eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr)); |
| 946 | |
| 947 | if (error == -1) { |
| 948 | /* Real error */ |
| 949 | close(interpreter_fd); |
| 950 | free(elf_phdata); |
| 951 | return ~((abi_ulong)0UL); |
| 952 | } |
| 953 | |
| 954 | if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) { |
| 955 | load_addr = error; |
| 956 | load_addr_set = 1; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * Find the end of the file mapping for this phdr, and keep |
| 961 | * track of the largest address we see for this. |
| 962 | */ |
| 963 | k = load_addr + eppnt->p_vaddr + eppnt->p_filesz; |
| 964 | if (k > elf_bss) elf_bss = k; |
| 965 | |
| 966 | /* |
| 967 | * Do the same thing for the memory mapping - between |
| 968 | * elf_bss and last_bss is the bss section. |
| 969 | */ |
| 970 | k = load_addr + eppnt->p_memsz + eppnt->p_vaddr; |
| 971 | if (k > last_bss) last_bss = k; |
| 972 | } |
| 973 | |
| 974 | /* Now use mmap to map the library into memory. */ |
| 975 | |
| 976 | close(interpreter_fd); |
| 977 | |
| 978 | /* |
| 979 | * Now fill out the bss section. First pad the last page up |
| 980 | * to the page boundary, and then perform a mmap to make sure |
| 981 | * that there are zeromapped pages up to and including the last |
| 982 | * bss page. |
| 983 | */ |
| 984 | padzero(elf_bss, last_bss); |
| 985 | elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */ |
| 986 | |
| 987 | /* Map the last of the bss segment */ |
| 988 | if (last_bss > elf_bss) { |
| 989 | target_mmap(elf_bss, last_bss-elf_bss, |
| 990 | PROT_READ|PROT_WRITE|PROT_EXEC, |
| 991 | MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0); |
| 992 | } |
| 993 | free(elf_phdata); |
| 994 | |
| 995 | *interp_load_addr = load_addr; |
| 996 | return ((abi_ulong) interp_elf_ex->e_entry) + load_addr; |
| 997 | } |
| 998 | |
| 999 | static int symfind(const void *s0, const void *s1) |
| 1000 | { |
Stefan Weil | c7c530c | 2012-01-05 15:39:39 +0100 | [diff] [blame] | 1001 | target_ulong addr = *(target_ulong *)s0; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1002 | struct elf_sym *sym = (struct elf_sym *)s1; |
| 1003 | int result = 0; |
Stefan Weil | c7c530c | 2012-01-05 15:39:39 +0100 | [diff] [blame] | 1004 | if (addr < sym->st_value) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1005 | result = -1; |
Stefan Weil | c7c530c | 2012-01-05 15:39:39 +0100 | [diff] [blame] | 1006 | } else if (addr >= sym->st_value + sym->st_size) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1007 | result = 1; |
| 1008 | } |
| 1009 | return result; |
| 1010 | } |
| 1011 | |
| 1012 | static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr) |
| 1013 | { |
| 1014 | #if ELF_CLASS == ELFCLASS32 |
| 1015 | struct elf_sym *syms = s->disas_symtab.elf32; |
| 1016 | #else |
| 1017 | struct elf_sym *syms = s->disas_symtab.elf64; |
| 1018 | #endif |
| 1019 | |
| 1020 | // binary search |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1021 | struct elf_sym *sym; |
| 1022 | |
Stefan Weil | c7c530c | 2012-01-05 15:39:39 +0100 | [diff] [blame] | 1023 | sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); |
Blue Swirl | 7cba04f | 2009-08-01 10:13:20 +0000 | [diff] [blame] | 1024 | if (sym != NULL) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1025 | return s->disas_strtab + sym->st_name; |
| 1026 | } |
| 1027 | |
| 1028 | return ""; |
| 1029 | } |
| 1030 | |
| 1031 | /* FIXME: This should use elf_ops.h */ |
| 1032 | static int symcmp(const void *s0, const void *s1) |
| 1033 | { |
| 1034 | struct elf_sym *sym0 = (struct elf_sym *)s0; |
| 1035 | struct elf_sym *sym1 = (struct elf_sym *)s1; |
| 1036 | return (sym0->st_value < sym1->st_value) |
| 1037 | ? -1 |
| 1038 | : ((sym0->st_value > sym1->st_value) ? 1 : 0); |
| 1039 | } |
| 1040 | |
| 1041 | /* Best attempt to load symbols from this ELF object. */ |
| 1042 | static void load_symbols(struct elfhdr *hdr, int fd) |
| 1043 | { |
| 1044 | unsigned int i, nsyms; |
| 1045 | struct elf_shdr sechdr, symtab, strtab; |
| 1046 | char *strings; |
| 1047 | struct syminfo *s; |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1048 | struct elf_sym *syms, *new_syms; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1049 | |
| 1050 | lseek(fd, hdr->e_shoff, SEEK_SET); |
| 1051 | for (i = 0; i < hdr->e_shnum; i++) { |
| 1052 | if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr)) |
| 1053 | return; |
| 1054 | #ifdef BSWAP_NEEDED |
| 1055 | bswap_shdr(&sechdr); |
| 1056 | #endif |
| 1057 | if (sechdr.sh_type == SHT_SYMTAB) { |
| 1058 | symtab = sechdr; |
| 1059 | lseek(fd, hdr->e_shoff |
| 1060 | + sizeof(sechdr) * sechdr.sh_link, SEEK_SET); |
| 1061 | if (read(fd, &strtab, sizeof(strtab)) |
| 1062 | != sizeof(strtab)) |
| 1063 | return; |
| 1064 | #ifdef BSWAP_NEEDED |
| 1065 | bswap_shdr(&strtab); |
| 1066 | #endif |
| 1067 | goto found; |
| 1068 | } |
| 1069 | } |
| 1070 | return; /* Shouldn't happen... */ |
| 1071 | |
| 1072 | found: |
| 1073 | /* Now know where the strtab and symtab are. Snarf them. */ |
| 1074 | s = malloc(sizeof(*s)); |
| 1075 | syms = malloc(symtab.sh_size); |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1076 | if (!syms) { |
| 1077 | free(s); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1078 | return; |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1079 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1080 | s->disas_strtab = strings = malloc(strtab.sh_size); |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1081 | if (!s->disas_strtab) { |
| 1082 | free(s); |
| 1083 | free(syms); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1084 | return; |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1085 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1086 | |
| 1087 | lseek(fd, symtab.sh_offset, SEEK_SET); |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1088 | if (read(fd, syms, symtab.sh_size) != symtab.sh_size) { |
| 1089 | free(s); |
| 1090 | free(syms); |
| 1091 | free(strings); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1092 | return; |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1093 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1094 | |
| 1095 | nsyms = symtab.sh_size / sizeof(struct elf_sym); |
| 1096 | |
| 1097 | i = 0; |
| 1098 | while (i < nsyms) { |
| 1099 | #ifdef BSWAP_NEEDED |
| 1100 | bswap_sym(syms + i); |
| 1101 | #endif |
| 1102 | // Throw away entries which we do not need. |
| 1103 | if (syms[i].st_shndx == SHN_UNDEF || |
| 1104 | syms[i].st_shndx >= SHN_LORESERVE || |
| 1105 | ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { |
| 1106 | nsyms--; |
| 1107 | if (i < nsyms) { |
| 1108 | syms[i] = syms[nsyms]; |
| 1109 | } |
| 1110 | continue; |
| 1111 | } |
| 1112 | #if defined(TARGET_ARM) || defined (TARGET_MIPS) |
| 1113 | /* The bottom address bit marks a Thumb or MIPS16 symbol. */ |
| 1114 | syms[i].st_value &= ~(target_ulong)1; |
| 1115 | #endif |
| 1116 | i++; |
| 1117 | } |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1118 | |
| 1119 | /* Attempt to free the storage associated with the local symbols |
| 1120 | that we threw away. Whether or not this has any effect on the |
| 1121 | memory allocation depends on the malloc implementation and how |
| 1122 | many symbols we managed to discard. */ |
| 1123 | new_syms = realloc(syms, nsyms * sizeof(*syms)); |
| 1124 | if (new_syms == NULL) { |
| 1125 | free(s); |
| 1126 | free(syms); |
| 1127 | free(strings); |
| 1128 | return; |
| 1129 | } |
| 1130 | syms = new_syms; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1131 | |
| 1132 | qsort(syms, nsyms, sizeof(*syms), symcmp); |
| 1133 | |
| 1134 | lseek(fd, strtab.sh_offset, SEEK_SET); |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1135 | if (read(fd, strings, strtab.sh_size) != strtab.sh_size) { |
| 1136 | free(s); |
| 1137 | free(syms); |
| 1138 | free(strings); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1139 | return; |
Stefan Weil | 2971871 | 2011-01-16 16:28:20 +0100 | [diff] [blame] | 1140 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1141 | s->disas_num_syms = nsyms; |
| 1142 | #if ELF_CLASS == ELFCLASS32 |
| 1143 | s->disas_symtab.elf32 = syms; |
Blue Swirl | 032e51d | 2009-09-27 19:30:56 +0000 | [diff] [blame] | 1144 | s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1145 | #else |
| 1146 | s->disas_symtab.elf64 = syms; |
Blue Swirl | 032e51d | 2009-09-27 19:30:56 +0000 | [diff] [blame] | 1147 | s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1148 | #endif |
| 1149 | s->next = syminfos; |
| 1150 | syminfos = s; |
| 1151 | } |
| 1152 | |
| 1153 | int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, |
| 1154 | struct image_info * info) |
| 1155 | { |
| 1156 | struct elfhdr elf_ex; |
| 1157 | struct elfhdr interp_elf_ex; |
| 1158 | struct exec interp_ex; |
| 1159 | int interpreter_fd = -1; /* avoid warning */ |
| 1160 | abi_ulong load_addr, load_bias; |
| 1161 | int load_addr_set = 0; |
| 1162 | unsigned int interpreter_type = INTERPRETER_NONE; |
| 1163 | unsigned char ibcs2_interpreter; |
| 1164 | int i; |
| 1165 | abi_ulong mapped_addr; |
| 1166 | struct elf_phdr * elf_ppnt; |
| 1167 | struct elf_phdr *elf_phdata; |
| 1168 | abi_ulong elf_bss, k, elf_brk; |
| 1169 | int retval; |
| 1170 | char * elf_interpreter; |
| 1171 | abi_ulong elf_entry, interp_load_addr = 0; |
| 1172 | int status; |
| 1173 | abi_ulong start_code, end_code, start_data, end_data; |
| 1174 | abi_ulong reloc_func_desc = 0; |
| 1175 | abi_ulong elf_stack; |
| 1176 | char passed_fileno[6]; |
| 1177 | |
| 1178 | ibcs2_interpreter = 0; |
| 1179 | status = 0; |
| 1180 | load_addr = 0; |
| 1181 | load_bias = 0; |
| 1182 | elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */ |
| 1183 | #ifdef BSWAP_NEEDED |
| 1184 | bswap_ehdr(&elf_ex); |
| 1185 | #endif |
| 1186 | |
| 1187 | /* First of all, some simple consistency checks */ |
| 1188 | if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) || |
| 1189 | (! elf_check_arch(elf_ex.e_machine))) { |
| 1190 | return -ENOEXEC; |
| 1191 | } |
| 1192 | |
| 1193 | bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p); |
| 1194 | bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p); |
| 1195 | bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p); |
| 1196 | if (!bprm->p) { |
| 1197 | retval = -E2BIG; |
| 1198 | } |
| 1199 | |
| 1200 | /* Now read in all of the header information */ |
| 1201 | elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum); |
| 1202 | if (elf_phdata == NULL) { |
| 1203 | return -ENOMEM; |
| 1204 | } |
| 1205 | |
| 1206 | retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET); |
| 1207 | if(retval > 0) { |
| 1208 | retval = read(bprm->fd, (char *) elf_phdata, |
| 1209 | elf_ex.e_phentsize * elf_ex.e_phnum); |
| 1210 | } |
| 1211 | |
| 1212 | if (retval < 0) { |
| 1213 | perror("load_elf_binary"); |
| 1214 | exit(-1); |
| 1215 | free (elf_phdata); |
| 1216 | return -errno; |
| 1217 | } |
| 1218 | |
| 1219 | #ifdef BSWAP_NEEDED |
| 1220 | elf_ppnt = elf_phdata; |
| 1221 | for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) { |
| 1222 | bswap_phdr(elf_ppnt); |
| 1223 | } |
| 1224 | #endif |
| 1225 | elf_ppnt = elf_phdata; |
| 1226 | |
| 1227 | elf_bss = 0; |
| 1228 | elf_brk = 0; |
| 1229 | |
| 1230 | |
| 1231 | elf_stack = ~((abi_ulong)0UL); |
| 1232 | elf_interpreter = NULL; |
| 1233 | start_code = ~((abi_ulong)0UL); |
| 1234 | end_code = 0; |
| 1235 | start_data = 0; |
| 1236 | end_data = 0; |
| 1237 | interp_ex.a_info = 0; |
| 1238 | |
| 1239 | for(i=0;i < elf_ex.e_phnum; i++) { |
| 1240 | if (elf_ppnt->p_type == PT_INTERP) { |
| 1241 | if ( elf_interpreter != NULL ) |
| 1242 | { |
| 1243 | free (elf_phdata); |
| 1244 | free(elf_interpreter); |
| 1245 | close(bprm->fd); |
| 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | |
| 1249 | /* This is the program interpreter used for |
| 1250 | * shared libraries - for now assume that this |
| 1251 | * is an a.out format binary |
| 1252 | */ |
| 1253 | |
| 1254 | elf_interpreter = (char *)malloc(elf_ppnt->p_filesz); |
| 1255 | |
| 1256 | if (elf_interpreter == NULL) { |
| 1257 | free (elf_phdata); |
| 1258 | close(bprm->fd); |
| 1259 | return -ENOMEM; |
| 1260 | } |
| 1261 | |
| 1262 | retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET); |
| 1263 | if(retval >= 0) { |
| 1264 | retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz); |
| 1265 | } |
| 1266 | if(retval < 0) { |
| 1267 | perror("load_elf_binary2"); |
| 1268 | exit(-1); |
| 1269 | } |
| 1270 | |
| 1271 | /* If the program interpreter is one of these two, |
| 1272 | then assume an iBCS2 image. Otherwise assume |
| 1273 | a native linux image. */ |
| 1274 | |
| 1275 | /* JRP - Need to add X86 lib dir stuff here... */ |
| 1276 | |
| 1277 | if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 || |
| 1278 | strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) { |
| 1279 | ibcs2_interpreter = 1; |
| 1280 | } |
| 1281 | |
| 1282 | #if 0 |
Paul Bolle | b7d43d0 | 2009-10-04 14:49:54 +0200 | [diff] [blame] | 1283 | printf("Using ELF interpreter %s\n", path(elf_interpreter)); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1284 | #endif |
| 1285 | if (retval >= 0) { |
| 1286 | retval = open(path(elf_interpreter), O_RDONLY); |
| 1287 | if(retval >= 0) { |
| 1288 | interpreter_fd = retval; |
| 1289 | } |
| 1290 | else { |
| 1291 | perror(elf_interpreter); |
| 1292 | exit(-1); |
| 1293 | /* retval = -errno; */ |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | if (retval >= 0) { |
| 1298 | retval = lseek(interpreter_fd, 0, SEEK_SET); |
| 1299 | if(retval >= 0) { |
| 1300 | retval = read(interpreter_fd,bprm->buf,128); |
| 1301 | } |
| 1302 | } |
| 1303 | if (retval >= 0) { |
| 1304 | interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */ |
Michael S. Tsirkin | 6ece4df | 2009-09-30 19:43:38 +0200 | [diff] [blame] | 1305 | interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1306 | } |
| 1307 | if (retval < 0) { |
| 1308 | perror("load_elf_binary3"); |
| 1309 | exit(-1); |
| 1310 | free (elf_phdata); |
| 1311 | free(elf_interpreter); |
| 1312 | close(bprm->fd); |
| 1313 | return retval; |
| 1314 | } |
| 1315 | } |
| 1316 | elf_ppnt++; |
| 1317 | } |
| 1318 | |
| 1319 | /* Some simple consistency checks for the interpreter */ |
| 1320 | if (elf_interpreter){ |
| 1321 | interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT; |
| 1322 | |
| 1323 | /* Now figure out which format our binary is */ |
| 1324 | if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) && |
| 1325 | (N_MAGIC(interp_ex) != QMAGIC)) { |
| 1326 | interpreter_type = INTERPRETER_ELF; |
| 1327 | } |
| 1328 | |
| 1329 | if (interp_elf_ex.e_ident[0] != 0x7f || |
Christoph Egger | fff2a02 | 2009-07-17 17:48:01 +0000 | [diff] [blame] | 1330 | strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1331 | interpreter_type &= ~INTERPRETER_ELF; |
| 1332 | } |
| 1333 | |
| 1334 | if (!interpreter_type) { |
| 1335 | free(elf_interpreter); |
| 1336 | free(elf_phdata); |
| 1337 | close(bprm->fd); |
| 1338 | return -ELIBBAD; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | /* OK, we are done with that, now set up the arg stuff, |
| 1343 | and then start this sucker up */ |
| 1344 | |
| 1345 | { |
| 1346 | char * passed_p; |
| 1347 | |
| 1348 | if (interpreter_type == INTERPRETER_AOUT) { |
| 1349 | snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd); |
| 1350 | passed_p = passed_fileno; |
| 1351 | |
| 1352 | if (elf_interpreter) { |
| 1353 | bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p); |
| 1354 | bprm->argc++; |
| 1355 | } |
| 1356 | } |
| 1357 | if (!bprm->p) { |
Daniel P. Berrange | ef1e1e0 | 2015-08-26 12:17:18 +0100 | [diff] [blame] | 1358 | free(elf_interpreter); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1359 | free (elf_phdata); |
| 1360 | close(bprm->fd); |
| 1361 | return -E2BIG; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /* OK, This is the point of no return */ |
| 1366 | info->end_data = 0; |
| 1367 | info->end_code = 0; |
| 1368 | info->start_mmap = (abi_ulong)ELF_START_MMAP; |
| 1369 | info->mmap = 0; |
| 1370 | elf_entry = (abi_ulong) elf_ex.e_entry; |
| 1371 | |
Blue Swirl | 2fa5d9b | 2009-09-27 19:30:51 +0000 | [diff] [blame] | 1372 | /* |
| 1373 | * In case where user has not explicitly set the guest_base, we |
| 1374 | * probe here that should we set it automatically. |
| 1375 | */ |
| 1376 | if (!have_guest_base) { |
| 1377 | /* |
| 1378 | * Go through ELF program header table and find out whether |
| 1379 | * any of the segments drop below our current mmap_min_addr and |
| 1380 | * in that case set guest_base to corresponding address. |
| 1381 | */ |
| 1382 | for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; |
| 1383 | i++, elf_ppnt++) { |
| 1384 | if (elf_ppnt->p_type != PT_LOAD) |
| 1385 | continue; |
| 1386 | if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) { |
| 1387 | guest_base = HOST_PAGE_ALIGN(mmap_min_addr); |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
Blue Swirl | 2fa5d9b | 2009-09-27 19:30:51 +0000 | [diff] [blame] | 1392 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1393 | /* Do this so that we can load the interpreter, if need be. We will |
| 1394 | change some of these later */ |
| 1395 | info->rss = 0; |
| 1396 | bprm->p = setup_arg_pages(bprm->p, bprm, info); |
| 1397 | info->start_stack = bprm->p; |
| 1398 | |
| 1399 | /* Now we do a little grungy work by mmaping the ELF image into |
| 1400 | * the correct location in memory. At this point, we assume that |
| 1401 | * the image should be loaded at fixed address, not at a variable |
| 1402 | * address. |
| 1403 | */ |
| 1404 | |
| 1405 | for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) { |
| 1406 | int elf_prot = 0; |
| 1407 | int elf_flags = 0; |
| 1408 | abi_ulong error; |
| 1409 | |
| 1410 | if (elf_ppnt->p_type != PT_LOAD) |
| 1411 | continue; |
| 1412 | |
| 1413 | if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ; |
| 1414 | if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE; |
| 1415 | if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; |
| 1416 | elf_flags = MAP_PRIVATE | MAP_DENYWRITE; |
| 1417 | if (elf_ex.e_type == ET_EXEC || load_addr_set) { |
| 1418 | elf_flags |= MAP_FIXED; |
| 1419 | } else if (elf_ex.e_type == ET_DYN) { |
| 1420 | /* Try and get dynamic programs out of the way of the default mmap |
| 1421 | base, as well as whatever program they might try to exec. This |
| 1422 | is because the brk will follow the loader, and is not movable. */ |
| 1423 | /* NOTE: for qemu, we do a big mmap to get enough space |
| 1424 | without hardcoding any address */ |
| 1425 | error = target_mmap(0, ET_DYN_MAP_SIZE, |
| 1426 | PROT_NONE, MAP_PRIVATE | MAP_ANON, |
| 1427 | -1, 0); |
| 1428 | if (error == -1) { |
| 1429 | perror("mmap"); |
| 1430 | exit(-1); |
| 1431 | } |
| 1432 | load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr); |
| 1433 | } |
| 1434 | |
| 1435 | error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr), |
| 1436 | (elf_ppnt->p_filesz + |
| 1437 | TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)), |
| 1438 | elf_prot, |
| 1439 | (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE), |
| 1440 | bprm->fd, |
| 1441 | (elf_ppnt->p_offset - |
| 1442 | TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr))); |
| 1443 | if (error == -1) { |
| 1444 | perror("mmap"); |
| 1445 | exit(-1); |
| 1446 | } |
| 1447 | |
| 1448 | #ifdef LOW_ELF_STACK |
| 1449 | if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack) |
| 1450 | elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr); |
| 1451 | #endif |
| 1452 | |
| 1453 | if (!load_addr_set) { |
| 1454 | load_addr_set = 1; |
| 1455 | load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset; |
| 1456 | if (elf_ex.e_type == ET_DYN) { |
| 1457 | load_bias += error - |
| 1458 | TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr); |
| 1459 | load_addr += load_bias; |
| 1460 | reloc_func_desc = load_bias; |
| 1461 | } |
| 1462 | } |
| 1463 | k = elf_ppnt->p_vaddr; |
| 1464 | if (k < start_code) |
| 1465 | start_code = k; |
| 1466 | if (start_data < k) |
| 1467 | start_data = k; |
| 1468 | k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz; |
| 1469 | if (k > elf_bss) |
| 1470 | elf_bss = k; |
| 1471 | if ((elf_ppnt->p_flags & PF_X) && end_code < k) |
| 1472 | end_code = k; |
| 1473 | if (end_data < k) |
| 1474 | end_data = k; |
| 1475 | k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz; |
| 1476 | if (k > elf_brk) elf_brk = k; |
| 1477 | } |
| 1478 | |
| 1479 | elf_entry += load_bias; |
| 1480 | elf_bss += load_bias; |
| 1481 | elf_brk += load_bias; |
| 1482 | start_code += load_bias; |
| 1483 | end_code += load_bias; |
| 1484 | start_data += load_bias; |
| 1485 | end_data += load_bias; |
| 1486 | |
| 1487 | if (elf_interpreter) { |
| 1488 | if (interpreter_type & 1) { |
| 1489 | elf_entry = load_aout_interp(&interp_ex, interpreter_fd); |
| 1490 | } |
| 1491 | else if (interpreter_type & 2) { |
| 1492 | elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd, |
| 1493 | &interp_load_addr); |
| 1494 | } |
| 1495 | reloc_func_desc = interp_load_addr; |
| 1496 | |
| 1497 | close(interpreter_fd); |
| 1498 | free(elf_interpreter); |
| 1499 | |
| 1500 | if (elf_entry == ~((abi_ulong)0UL)) { |
| 1501 | printf("Unable to load interpreter\n"); |
| 1502 | free(elf_phdata); |
| 1503 | exit(-1); |
| 1504 | return 0; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | free(elf_phdata); |
| 1509 | |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1510 | if (qemu_log_enabled()) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1511 | load_symbols(&elf_ex, bprm->fd); |
| 1512 | |
| 1513 | if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd); |
| 1514 | info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX); |
| 1515 | |
| 1516 | #ifdef LOW_ELF_STACK |
| 1517 | info->start_stack = bprm->p = elf_stack - 4; |
| 1518 | #endif |
| 1519 | bprm->p = create_elf_tables(bprm->p, |
| 1520 | bprm->argc, |
| 1521 | bprm->envc, |
| 1522 | &elf_ex, |
| 1523 | load_addr, load_bias, |
| 1524 | interp_load_addr, |
| 1525 | (interpreter_type == INTERPRETER_AOUT ? 0 : 1), |
| 1526 | info); |
| 1527 | info->load_addr = reloc_func_desc; |
| 1528 | info->start_brk = info->brk = elf_brk; |
| 1529 | info->end_code = end_code; |
| 1530 | info->start_code = start_code; |
| 1531 | info->start_data = start_data; |
| 1532 | info->end_data = end_data; |
| 1533 | info->start_stack = bprm->p; |
| 1534 | |
| 1535 | /* Calling set_brk effectively mmaps the pages that we need for the bss and break |
| 1536 | sections */ |
| 1537 | set_brk(elf_bss, elf_brk); |
| 1538 | |
| 1539 | padzero(elf_bss, elf_brk); |
| 1540 | |
| 1541 | #if 0 |
| 1542 | printf("(start_brk) %x\n" , info->start_brk); |
| 1543 | printf("(end_code) %x\n" , info->end_code); |
| 1544 | printf("(start_code) %x\n" , info->start_code); |
| 1545 | printf("(end_data) %x\n" , info->end_data); |
| 1546 | printf("(start_stack) %x\n" , info->start_stack); |
| 1547 | printf("(brk) %x\n" , info->brk); |
| 1548 | #endif |
| 1549 | |
| 1550 | if ( info->personality == PER_SVR4 ) |
| 1551 | { |
| 1552 | /* Why this, you ask??? Well SVr4 maps page 0 as read-only, |
| 1553 | and some applications "depend" upon this behavior. |
| 1554 | Since we do not have the power to recompile these, we |
| 1555 | emulate the SVr4 behavior. Sigh. */ |
| 1556 | mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC, |
| 1557 | MAP_FIXED | MAP_PRIVATE, -1, 0); |
| 1558 | } |
| 1559 | |
| 1560 | info->entry = elf_entry; |
| 1561 | |
| 1562 | return 0; |
| 1563 | } |
| 1564 | |
| 1565 | static int load_aout_interp(void * exptr, int interp_fd) |
| 1566 | { |
| 1567 | printf("a.out interpreter not yet supported\n"); |
| 1568 | return(0); |
| 1569 | } |
| 1570 | |
| 1571 | void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 1572 | { |
| 1573 | init_thread(regs, infop); |
| 1574 | } |