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