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