1
2#include <sys/time.h>
3#include <sys/param.h>
4
5#include <stdio.h>
6#include <sys/types.h>
7#include <fcntl.h>
8#include <errno.h>
9#include <unistd.h>
10#include <sys/mman.h>
11#include <sys/resource.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15
16#include "qemu.h"
17#include "disas/disas.h"
18
19#ifdef _ARCH_PPC64
20#undef ARCH_DLINFO
21#undef ELF_PLATFORM
22#undef ELF_HWCAP
23#undef ELF_HWCAP2
24#undef ELF_CLASS
25#undef ELF_DATA
26#undef ELF_ARCH
27#endif
28
29#define ELF_OSABI ELFOSABI_SYSV
30
31
32
33
34
35
36
37
38enum {
39 ADDR_NO_RANDOMIZE = 0x0040000,
40 FDPIC_FUNCPTRS = 0x0080000,
41
42 MMAP_PAGE_ZERO = 0x0100000,
43 ADDR_COMPAT_LAYOUT = 0x0200000,
44 READ_IMPLIES_EXEC = 0x0400000,
45 ADDR_LIMIT_32BIT = 0x0800000,
46 SHORT_INODE = 0x1000000,
47 WHOLE_SECONDS = 0x2000000,
48 STICKY_TIMEOUTS = 0x4000000,
49 ADDR_LIMIT_3GB = 0x8000000,
50};
51
52
53
54
55
56
57
58enum {
59 PER_LINUX = 0x0000,
60 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
61 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
62 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
63 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
64 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
65 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
66 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
67 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
68 PER_BSD = 0x0006,
69 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
70 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
71 PER_LINUX32 = 0x0008,
72 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
73 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,
74 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,
75 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,
76 PER_RISCOS = 0x000c,
77 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
78 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
79 PER_OSF4 = 0x000f,
80 PER_HPUX = 0x0010,
81 PER_MASK = 0x00ff,
82};
83
84
85
86
87#define personality(pers) (pers & PER_MASK)
88
89
90#ifndef MAP_DENYWRITE
91#define MAP_DENYWRITE 0
92#endif
93
94
95#ifndef ELIBBAD
96#define ELIBBAD 80
97#endif
98
99#ifdef TARGET_WORDS_BIGENDIAN
100#define ELF_DATA ELFDATA2MSB
101#else
102#define ELF_DATA ELFDATA2LSB
103#endif
104
105#ifdef TARGET_ABI_MIPSN32
106typedef abi_ullong target_elf_greg_t;
107#define tswapreg(ptr) tswap64(ptr)
108#else
109typedef abi_ulong target_elf_greg_t;
110#define tswapreg(ptr) tswapal(ptr)
111#endif
112
113#ifdef USE_UID16
114typedef abi_ushort target_uid_t;
115typedef abi_ushort target_gid_t;
116#else
117typedef abi_uint target_uid_t;
118typedef abi_uint target_gid_t;
119#endif
120typedef abi_int target_pid_t;
121
122#ifdef TARGET_I386
123
124#define ELF_PLATFORM get_elf_platform()
125
126static const char *get_elf_platform(void)
127{
128 static char elf_platform[] = "i386";
129 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
130 if (family > 6)
131 family = 6;
132 if (family >= 3)
133 elf_platform[1] = '0' + family;
134 return elf_platform;
135}
136
137#define ELF_HWCAP get_elf_hwcap()
138
139static uint32_t get_elf_hwcap(void)
140{
141 X86CPU *cpu = X86_CPU(thread_cpu);
142
143 return cpu->env.features[FEAT_1_EDX];
144}
145
146#ifdef TARGET_X86_64
147#define ELF_START_MMAP 0x2aaaaab000ULL
148
149#define ELF_CLASS ELFCLASS64
150#define ELF_ARCH EM_X86_64
151
152static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
153{
154 regs->rax = 0;
155 regs->rsp = infop->start_stack;
156 regs->rip = infop->entry;
157}
158
159#define ELF_NREG 27
160typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
161
162
163
164
165
166
167
168
169static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
170{
171 (*regs)[0] = env->regs[15];
172 (*regs)[1] = env->regs[14];
173 (*regs)[2] = env->regs[13];
174 (*regs)[3] = env->regs[12];
175 (*regs)[4] = env->regs[R_EBP];
176 (*regs)[5] = env->regs[R_EBX];
177 (*regs)[6] = env->regs[11];
178 (*regs)[7] = env->regs[10];
179 (*regs)[8] = env->regs[9];
180 (*regs)[9] = env->regs[8];
181 (*regs)[10] = env->regs[R_EAX];
182 (*regs)[11] = env->regs[R_ECX];
183 (*regs)[12] = env->regs[R_EDX];
184 (*regs)[13] = env->regs[R_ESI];
185 (*regs)[14] = env->regs[R_EDI];
186 (*regs)[15] = env->regs[R_EAX];
187 (*regs)[16] = env->eip;
188 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
189 (*regs)[18] = env->eflags;
190 (*regs)[19] = env->regs[R_ESP];
191 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
192 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
193 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
194 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
195 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
196 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
197 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
198}
199
200#else
201
202#define ELF_START_MMAP 0x80000000
203
204
205
206
207#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
208
209
210
211
212#define ELF_CLASS ELFCLASS32
213#define ELF_ARCH EM_386
214
215static inline void init_thread(struct target_pt_regs *regs,
216 struct image_info *infop)
217{
218 regs->esp = infop->start_stack;
219 regs->eip = infop->entry;
220
221
222
223
224
225
226
227
228 regs->edx = 0;
229}
230
231#define ELF_NREG 17
232typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
233
234
235
236
237
238
239
240
241static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
242{
243 (*regs)[0] = env->regs[R_EBX];
244 (*regs)[1] = env->regs[R_ECX];
245 (*regs)[2] = env->regs[R_EDX];
246 (*regs)[3] = env->regs[R_ESI];
247 (*regs)[4] = env->regs[R_EDI];
248 (*regs)[5] = env->regs[R_EBP];
249 (*regs)[6] = env->regs[R_EAX];
250 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
251 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
252 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
253 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
254 (*regs)[11] = env->regs[R_EAX];
255 (*regs)[12] = env->eip;
256 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
257 (*regs)[14] = env->eflags;
258 (*regs)[15] = env->regs[R_ESP];
259 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
260}
261#endif
262
263#define USE_ELF_CORE_DUMP
264#define ELF_EXEC_PAGESIZE 4096
265
266#endif
267
268#ifdef TARGET_ARM
269
270#ifndef TARGET_AARCH64
271
272
273#define ELF_START_MMAP 0x80000000
274
275#define ELF_ARCH EM_ARM
276#define ELF_CLASS ELFCLASS32
277
278static inline void init_thread(struct target_pt_regs *regs,
279 struct image_info *infop)
280{
281 abi_long stack = infop->start_stack;
282 memset(regs, 0, sizeof(*regs));
283
284 regs->ARM_cpsr = 0x10;
285 if (infop->entry & 1)
286 regs->ARM_cpsr |= CPSR_T;
287 regs->ARM_pc = infop->entry & 0xfffffffe;
288 regs->ARM_sp = infop->start_stack;
289
290 get_user_ual(regs->ARM_r2, stack + 8);
291 get_user_ual(regs->ARM_r1, stack + 4);
292
293 regs->ARM_r0 = 0;
294
295
296 regs->ARM_r10 = infop->start_data;
297}
298
299#define ELF_NREG 18
300typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
301
302static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
303{
304 (*regs)[0] = tswapreg(env->regs[0]);
305 (*regs)[1] = tswapreg(env->regs[1]);
306 (*regs)[2] = tswapreg(env->regs[2]);
307 (*regs)[3] = tswapreg(env->regs[3]);
308 (*regs)[4] = tswapreg(env->regs[4]);
309 (*regs)[5] = tswapreg(env->regs[5]);
310 (*regs)[6] = tswapreg(env->regs[6]);
311 (*regs)[7] = tswapreg(env->regs[7]);
312 (*regs)[8] = tswapreg(env->regs[8]);
313 (*regs)[9] = tswapreg(env->regs[9]);
314 (*regs)[10] = tswapreg(env->regs[10]);
315 (*regs)[11] = tswapreg(env->regs[11]);
316 (*regs)[12] = tswapreg(env->regs[12]);
317 (*regs)[13] = tswapreg(env->regs[13]);
318 (*regs)[14] = tswapreg(env->regs[14]);
319 (*regs)[15] = tswapreg(env->regs[15]);
320
321 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
322 (*regs)[17] = tswapreg(env->regs[0]);
323}
324
325#define USE_ELF_CORE_DUMP
326#define ELF_EXEC_PAGESIZE 4096
327
328enum
329{
330 ARM_HWCAP_ARM_SWP = 1 << 0,
331 ARM_HWCAP_ARM_HALF = 1 << 1,
332 ARM_HWCAP_ARM_THUMB = 1 << 2,
333 ARM_HWCAP_ARM_26BIT = 1 << 3,
334 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
335 ARM_HWCAP_ARM_FPA = 1 << 5,
336 ARM_HWCAP_ARM_VFP = 1 << 6,
337 ARM_HWCAP_ARM_EDSP = 1 << 7,
338 ARM_HWCAP_ARM_JAVA = 1 << 8,
339 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
340 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
341 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
342 ARM_HWCAP_ARM_NEON = 1 << 12,
343 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
344 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
345 ARM_HWCAP_ARM_TLS = 1 << 15,
346 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
347 ARM_HWCAP_ARM_IDIVA = 1 << 17,
348 ARM_HWCAP_ARM_IDIVT = 1 << 18,
349 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
350 ARM_HWCAP_ARM_LPAE = 1 << 20,
351 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
352};
353
354enum {
355 ARM_HWCAP2_ARM_AES = 1 << 0,
356 ARM_HWCAP2_ARM_PMULL = 1 << 1,
357 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
358 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
359 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
360};
361
362
363
364#define TARGET_HAS_VALIDATE_GUEST_SPACE
365
366
367
368
369
370
371
372
373static int validate_guest_space(unsigned long guest_base,
374 unsigned long guest_size)
375{
376 unsigned long real_start, test_page_addr;
377
378
379
380
381 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
382
383
384
385
386 if (test_page_addr >= guest_base
387 && test_page_addr <= (guest_base + guest_size)) {
388 return -1;
389 }
390
391
392 real_start = (unsigned long)
393 mmap((void *)test_page_addr, qemu_host_page_size,
394 PROT_READ | PROT_WRITE,
395 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
396
397
398 if (real_start == -1ul) {
399 return 0;
400 }
401
402 if (real_start != test_page_addr) {
403
404 munmap((void *)real_start, qemu_host_page_size);
405 return 0;
406 }
407
408
409
410
411
412
413 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
414
415
416 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
417 perror("Protecting guest commpage");
418 exit(-1);
419 }
420
421 return 1;
422}
423
424#define ELF_HWCAP get_elf_hwcap()
425#define ELF_HWCAP2 get_elf_hwcap2()
426
427static uint32_t get_elf_hwcap(void)
428{
429 ARMCPU *cpu = ARM_CPU(thread_cpu);
430 uint32_t hwcaps = 0;
431
432 hwcaps |= ARM_HWCAP_ARM_SWP;
433 hwcaps |= ARM_HWCAP_ARM_HALF;
434 hwcaps |= ARM_HWCAP_ARM_THUMB;
435 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
436
437
438#define GET_FEATURE(feat, hwcap) \
439 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
440
441 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
442 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
443 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
444 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
445 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
446 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
447 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
448 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
449 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
450 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
451
452
453
454
455
456 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
457 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
458
459 return hwcaps;
460}
461
462static uint32_t get_elf_hwcap2(void)
463{
464 ARMCPU *cpu = ARM_CPU(thread_cpu);
465 uint32_t hwcaps = 0;
466
467 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
468 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
469 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
470 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
471 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
472 return hwcaps;
473}
474
475#undef GET_FEATURE
476
477#else
478
479#define ELF_START_MMAP 0x80000000
480
481#define ELF_ARCH EM_AARCH64
482#define ELF_CLASS ELFCLASS64
483#define ELF_PLATFORM "aarch64"
484
485static inline void init_thread(struct target_pt_regs *regs,
486 struct image_info *infop)
487{
488 abi_long stack = infop->start_stack;
489 memset(regs, 0, sizeof(*regs));
490
491 regs->pc = infop->entry & ~0x3ULL;
492 regs->sp = stack;
493}
494
495#define ELF_NREG 34
496typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
497
498static void elf_core_copy_regs(target_elf_gregset_t *regs,
499 const CPUARMState *env)
500{
501 int i;
502
503 for (i = 0; i < 32; i++) {
504 (*regs)[i] = tswapreg(env->xregs[i]);
505 }
506 (*regs)[32] = tswapreg(env->pc);
507 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
508}
509
510#define USE_ELF_CORE_DUMP
511#define ELF_EXEC_PAGESIZE 4096
512
513enum {
514 ARM_HWCAP_A64_FP = 1 << 0,
515 ARM_HWCAP_A64_ASIMD = 1 << 1,
516 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
517 ARM_HWCAP_A64_AES = 1 << 3,
518 ARM_HWCAP_A64_PMULL = 1 << 4,
519 ARM_HWCAP_A64_SHA1 = 1 << 5,
520 ARM_HWCAP_A64_SHA2 = 1 << 6,
521 ARM_HWCAP_A64_CRC32 = 1 << 7,
522};
523
524#define ELF_HWCAP get_elf_hwcap()
525
526static uint32_t get_elf_hwcap(void)
527{
528 ARMCPU *cpu = ARM_CPU(thread_cpu);
529 uint32_t hwcaps = 0;
530
531 hwcaps |= ARM_HWCAP_A64_FP;
532 hwcaps |= ARM_HWCAP_A64_ASIMD;
533
534
535#define GET_FEATURE(feat, hwcap) \
536 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
537 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
538 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
539 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
540 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
541 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
542#undef GET_FEATURE
543
544 return hwcaps;
545}
546
547#endif
548#endif
549
550#ifdef TARGET_UNICORE32
551
552#define ELF_START_MMAP 0x80000000
553
554#define ELF_CLASS ELFCLASS32
555#define ELF_DATA ELFDATA2LSB
556#define ELF_ARCH EM_UNICORE32
557
558static inline void init_thread(struct target_pt_regs *regs,
559 struct image_info *infop)
560{
561 abi_long stack = infop->start_stack;
562 memset(regs, 0, sizeof(*regs));
563 regs->UC32_REG_asr = 0x10;
564 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
565 regs->UC32_REG_sp = infop->start_stack;
566
567 get_user_ual(regs->UC32_REG_02, stack + 8);
568 get_user_ual(regs->UC32_REG_01, stack + 4);
569
570 regs->UC32_REG_00 = 0;
571}
572
573#define ELF_NREG 34
574typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
575
576static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
577{
578 (*regs)[0] = env->regs[0];
579 (*regs)[1] = env->regs[1];
580 (*regs)[2] = env->regs[2];
581 (*regs)[3] = env->regs[3];
582 (*regs)[4] = env->regs[4];
583 (*regs)[5] = env->regs[5];
584 (*regs)[6] = env->regs[6];
585 (*regs)[7] = env->regs[7];
586 (*regs)[8] = env->regs[8];
587 (*regs)[9] = env->regs[9];
588 (*regs)[10] = env->regs[10];
589 (*regs)[11] = env->regs[11];
590 (*regs)[12] = env->regs[12];
591 (*regs)[13] = env->regs[13];
592 (*regs)[14] = env->regs[14];
593 (*regs)[15] = env->regs[15];
594 (*regs)[16] = env->regs[16];
595 (*regs)[17] = env->regs[17];
596 (*regs)[18] = env->regs[18];
597 (*regs)[19] = env->regs[19];
598 (*regs)[20] = env->regs[20];
599 (*regs)[21] = env->regs[21];
600 (*regs)[22] = env->regs[22];
601 (*regs)[23] = env->regs[23];
602 (*regs)[24] = env->regs[24];
603 (*regs)[25] = env->regs[25];
604 (*regs)[26] = env->regs[26];
605 (*regs)[27] = env->regs[27];
606 (*regs)[28] = env->regs[28];
607 (*regs)[29] = env->regs[29];
608 (*regs)[30] = env->regs[30];
609 (*regs)[31] = env->regs[31];
610
611 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
612 (*regs)[33] = env->regs[0];
613}
614
615#define USE_ELF_CORE_DUMP
616#define ELF_EXEC_PAGESIZE 4096
617
618#define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
619
620#endif
621
622#ifdef TARGET_SPARC
623#ifdef TARGET_SPARC64
624
625#define ELF_START_MMAP 0x80000000
626#define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
627 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
628#ifndef TARGET_ABI32
629#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
630#else
631#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
632#endif
633
634#define ELF_CLASS ELFCLASS64
635#define ELF_ARCH EM_SPARCV9
636
637#define STACK_BIAS 2047
638
639static inline void init_thread(struct target_pt_regs *regs,
640 struct image_info *infop)
641{
642#ifndef TARGET_ABI32
643 regs->tstate = 0;
644#endif
645 regs->pc = infop->entry;
646 regs->npc = regs->pc + 4;
647 regs->y = 0;
648#ifdef TARGET_ABI32
649 regs->u_regs[14] = infop->start_stack - 16 * 4;
650#else
651 if (personality(infop->personality) == PER_LINUX32)
652 regs->u_regs[14] = infop->start_stack - 16 * 4;
653 else
654 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
655#endif
656}
657
658#else
659#define ELF_START_MMAP 0x80000000
660#define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
661 | HWCAP_SPARC_MULDIV)
662
663#define ELF_CLASS ELFCLASS32
664#define ELF_ARCH EM_SPARC
665
666static inline void init_thread(struct target_pt_regs *regs,
667 struct image_info *infop)
668{
669 regs->psr = 0;
670 regs->pc = infop->entry;
671 regs->npc = regs->pc + 4;
672 regs->y = 0;
673 regs->u_regs[14] = infop->start_stack - 16 * 4;
674}
675
676#endif
677#endif
678
679#ifdef TARGET_PPC
680
681#define ELF_MACHINE PPC_ELF_MACHINE
682#define ELF_START_MMAP 0x80000000
683
684#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
685
686#define elf_check_arch(x) ( (x) == EM_PPC64 )
687
688#define ELF_CLASS ELFCLASS64
689
690#else
691
692#define ELF_CLASS ELFCLASS32
693
694#endif
695
696#define ELF_ARCH EM_PPC
697
698
699
700enum {
701 QEMU_PPC_FEATURE_32 = 0x80000000,
702 QEMU_PPC_FEATURE_64 = 0x40000000,
703 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
704 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
705 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
706 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
707 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
708 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
709 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
710 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
711 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
712 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
713 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
714 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
715 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
716 QEMU_PPC_FEATURE_CELL = 0x00010000,
717 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
718 QEMU_PPC_FEATURE_SMT = 0x00004000,
719 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
720 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
721 QEMU_PPC_FEATURE_PA6T = 0x00000800,
722 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
723 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
724 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
725 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
726 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
727
728 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
729 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
730
731
732 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000,
733 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000,
734 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000,
735 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000,
736 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000,
737 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000,
738};
739
740#define ELF_HWCAP get_elf_hwcap()
741
742static uint32_t get_elf_hwcap(void)
743{
744 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
745 uint32_t features = 0;
746
747
748
749#define GET_FEATURE(flag, feature) \
750 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
751#define GET_FEATURE2(flag, feature) \
752 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
753 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
754 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
755 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
756 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
757 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
758 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
759 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
760 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
761 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
762 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
763 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
764 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
765 QEMU_PPC_FEATURE_ARCH_2_06);
766#undef GET_FEATURE
767#undef GET_FEATURE2
768
769 return features;
770}
771
772#define ELF_HWCAP2 get_elf_hwcap2()
773
774static uint32_t get_elf_hwcap2(void)
775{
776 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
777 uint32_t features = 0;
778
779#define GET_FEATURE(flag, feature) \
780 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
781#define GET_FEATURE2(flag, feature) \
782 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
783
784 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
785 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
786 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
787 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
788
789#undef GET_FEATURE
790#undef GET_FEATURE2
791
792 return features;
793}
794
795
796
797
798
799
800
801
802
803
804#define DLINFO_ARCH_ITEMS 5
805#define ARCH_DLINFO \
806 do { \
807 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
808 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
809 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
810 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
811
812
813 \
814 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
815 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
816 } while (0)
817
818static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
819{
820 _regs->gpr[1] = infop->start_stack;
821#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
822 if (get_ppc64_abi(infop) < 2) {
823 uint64_t val;
824 get_user_u64(val, infop->entry + 8);
825 _regs->gpr[2] = val + infop->load_bias;
826 get_user_u64(val, infop->entry);
827 infop->entry = val + infop->load_bias;
828 } else {
829 _regs->gpr[12] = infop->entry;
830 }
831#endif
832 _regs->nip = infop->entry;
833}
834
835
836#define ELF_NREG 48
837typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
838
839static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
840{
841 int i;
842 target_ulong ccr = 0;
843
844 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
845 (*regs)[i] = tswapreg(env->gpr[i]);
846 }
847
848 (*regs)[32] = tswapreg(env->nip);
849 (*regs)[33] = tswapreg(env->msr);
850 (*regs)[35] = tswapreg(env->ctr);
851 (*regs)[36] = tswapreg(env->lr);
852 (*regs)[37] = tswapreg(env->xer);
853
854 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
855 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
856 }
857 (*regs)[38] = tswapreg(ccr);
858}
859
860#define USE_ELF_CORE_DUMP
861#define ELF_EXEC_PAGESIZE 4096
862
863#endif
864
865#ifdef TARGET_MIPS
866
867#define ELF_START_MMAP 0x80000000
868
869#ifdef TARGET_MIPS64
870#define ELF_CLASS ELFCLASS64
871#else
872#define ELF_CLASS ELFCLASS32
873#endif
874#define ELF_ARCH EM_MIPS
875
876static inline void init_thread(struct target_pt_regs *regs,
877 struct image_info *infop)
878{
879 regs->cp0_status = 2 << CP0St_KSU;
880 regs->cp0_epc = infop->entry;
881 regs->regs[29] = infop->start_stack;
882}
883
884
885#define ELF_NREG 45
886typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
887
888
889enum {
890#ifdef TARGET_MIPS64
891 TARGET_EF_R0 = 0,
892#else
893 TARGET_EF_R0 = 6,
894#endif
895 TARGET_EF_R26 = TARGET_EF_R0 + 26,
896 TARGET_EF_R27 = TARGET_EF_R0 + 27,
897 TARGET_EF_LO = TARGET_EF_R0 + 32,
898 TARGET_EF_HI = TARGET_EF_R0 + 33,
899 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
900 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
901 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
902 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
903};
904
905
906static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
907{
908 int i;
909
910 for (i = 0; i < TARGET_EF_R0; i++) {
911 (*regs)[i] = 0;
912 }
913 (*regs)[TARGET_EF_R0] = 0;
914
915 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
916 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
917 }
918
919 (*regs)[TARGET_EF_R26] = 0;
920 (*regs)[TARGET_EF_R27] = 0;
921 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
922 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
923 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
924 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
925 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
926 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
927}
928
929#define USE_ELF_CORE_DUMP
930#define ELF_EXEC_PAGESIZE 4096
931
932#endif
933
934#ifdef TARGET_MICROBLAZE
935
936#define ELF_START_MMAP 0x80000000
937
938#define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
939
940#define ELF_CLASS ELFCLASS32
941#define ELF_ARCH EM_MICROBLAZE
942
943static inline void init_thread(struct target_pt_regs *regs,
944 struct image_info *infop)
945{
946 regs->pc = infop->entry;
947 regs->r1 = infop->start_stack;
948
949}
950
951#define ELF_EXEC_PAGESIZE 4096
952
953#define USE_ELF_CORE_DUMP
954#define ELF_NREG 38
955typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
956
957
958static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
959{
960 int i, pos = 0;
961
962 for (i = 0; i < 32; i++) {
963 (*regs)[pos++] = tswapreg(env->regs[i]);
964 }
965
966 for (i = 0; i < 6; i++) {
967 (*regs)[pos++] = tswapreg(env->sregs[i]);
968 }
969}
970
971#endif
972
973#ifdef TARGET_OPENRISC
974
975#define ELF_START_MMAP 0x08000000
976
977#define ELF_ARCH EM_OPENRISC
978#define ELF_CLASS ELFCLASS32
979#define ELF_DATA ELFDATA2MSB
980
981static inline void init_thread(struct target_pt_regs *regs,
982 struct image_info *infop)
983{
984 regs->pc = infop->entry;
985 regs->gpr[1] = infop->start_stack;
986}
987
988#define USE_ELF_CORE_DUMP
989#define ELF_EXEC_PAGESIZE 8192
990
991
992#define ELF_NREG 34
993typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
994
995static void elf_core_copy_regs(target_elf_gregset_t *regs,
996 const CPUOpenRISCState *env)
997{
998 int i;
999
1000 for (i = 0; i < 32; i++) {
1001 (*regs)[i] = tswapreg(env->gpr[i]);
1002 }
1003
1004 (*regs)[32] = tswapreg(env->pc);
1005 (*regs)[33] = tswapreg(env->sr);
1006}
1007#define ELF_HWCAP 0
1008#define ELF_PLATFORM NULL
1009
1010#endif
1011
1012#ifdef TARGET_SH4
1013
1014#define ELF_START_MMAP 0x80000000
1015
1016#define ELF_CLASS ELFCLASS32
1017#define ELF_ARCH EM_SH
1018
1019static inline void init_thread(struct target_pt_regs *regs,
1020 struct image_info *infop)
1021{
1022
1023 regs->pc = infop->entry;
1024 regs->regs[15] = infop->start_stack;
1025}
1026
1027
1028#define ELF_NREG 23
1029typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1030
1031
1032enum {
1033 TARGET_REG_PC = 16,
1034 TARGET_REG_PR = 17,
1035 TARGET_REG_SR = 18,
1036 TARGET_REG_GBR = 19,
1037 TARGET_REG_MACH = 20,
1038 TARGET_REG_MACL = 21,
1039 TARGET_REG_SYSCALL = 22
1040};
1041
1042static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1043 const CPUSH4State *env)
1044{
1045 int i;
1046
1047 for (i = 0; i < 16; i++) {
1048 (*regs[i]) = tswapreg(env->gregs[i]);
1049 }
1050
1051 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1052 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1053 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1054 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1055 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1056 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1057 (*regs)[TARGET_REG_SYSCALL] = 0;
1058}
1059
1060#define USE_ELF_CORE_DUMP
1061#define ELF_EXEC_PAGESIZE 4096
1062
1063enum {
1064 SH_CPU_HAS_FPU = 0x0001,
1065 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002,
1066 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004,
1067 SH_CPU_HAS_DSP = 0x0008,
1068 SH_CPU_HAS_PERF_COUNTER = 0x0010,
1069 SH_CPU_HAS_PTEA = 0x0020,
1070 SH_CPU_HAS_LLSC = 0x0040,
1071 SH_CPU_HAS_L2_CACHE = 0x0080,
1072 SH_CPU_HAS_OP32 = 0x0100,
1073 SH_CPU_HAS_PTEAEX = 0x0200,
1074};
1075
1076#define ELF_HWCAP get_elf_hwcap()
1077
1078static uint32_t get_elf_hwcap(void)
1079{
1080 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1081 uint32_t hwcap = 0;
1082
1083 hwcap |= SH_CPU_HAS_FPU;
1084
1085 if (cpu->env.features & SH_FEATURE_SH4A) {
1086 hwcap |= SH_CPU_HAS_LLSC;
1087 }
1088
1089 return hwcap;
1090}
1091
1092#endif
1093
1094#ifdef TARGET_CRIS
1095
1096#define ELF_START_MMAP 0x80000000
1097
1098#define ELF_CLASS ELFCLASS32
1099#define ELF_ARCH EM_CRIS
1100
1101static inline void init_thread(struct target_pt_regs *regs,
1102 struct image_info *infop)
1103{
1104 regs->erp = infop->entry;
1105}
1106
1107#define ELF_EXEC_PAGESIZE 8192
1108
1109#endif
1110
1111#ifdef TARGET_M68K
1112
1113#define ELF_START_MMAP 0x80000000
1114
1115#define ELF_CLASS ELFCLASS32
1116#define ELF_ARCH EM_68K
1117
1118
1119
1120
1121static inline void init_thread(struct target_pt_regs *regs,
1122 struct image_info *infop)
1123{
1124 regs->usp = infop->start_stack;
1125 regs->sr = 0;
1126 regs->pc = infop->entry;
1127}
1128
1129
1130#define ELF_NREG 20
1131typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1132
1133static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1134{
1135 (*regs)[0] = tswapreg(env->dregs[1]);
1136 (*regs)[1] = tswapreg(env->dregs[2]);
1137 (*regs)[2] = tswapreg(env->dregs[3]);
1138 (*regs)[3] = tswapreg(env->dregs[4]);
1139 (*regs)[4] = tswapreg(env->dregs[5]);
1140 (*regs)[5] = tswapreg(env->dregs[6]);
1141 (*regs)[6] = tswapreg(env->dregs[7]);
1142 (*regs)[7] = tswapreg(env->aregs[0]);
1143 (*regs)[8] = tswapreg(env->aregs[1]);
1144 (*regs)[9] = tswapreg(env->aregs[2]);
1145 (*regs)[10] = tswapreg(env->aregs[3]);
1146 (*regs)[11] = tswapreg(env->aregs[4]);
1147 (*regs)[12] = tswapreg(env->aregs[5]);
1148 (*regs)[13] = tswapreg(env->aregs[6]);
1149 (*regs)[14] = tswapreg(env->dregs[0]);
1150 (*regs)[15] = tswapreg(env->aregs[7]);
1151 (*regs)[16] = tswapreg(env->dregs[0]);
1152 (*regs)[17] = tswapreg(env->sr);
1153 (*regs)[18] = tswapreg(env->pc);
1154 (*regs)[19] = 0;
1155}
1156
1157#define USE_ELF_CORE_DUMP
1158#define ELF_EXEC_PAGESIZE 8192
1159
1160#endif
1161
1162#ifdef TARGET_ALPHA
1163
1164#define ELF_START_MMAP (0x30000000000ULL)
1165
1166#define ELF_CLASS ELFCLASS64
1167#define ELF_ARCH EM_ALPHA
1168
1169static inline void init_thread(struct target_pt_regs *regs,
1170 struct image_info *infop)
1171{
1172 regs->pc = infop->entry;
1173 regs->ps = 8;
1174 regs->usp = infop->start_stack;
1175}
1176
1177#define ELF_EXEC_PAGESIZE 8192
1178
1179#endif
1180
1181#ifdef TARGET_S390X
1182
1183#define ELF_START_MMAP (0x20000000000ULL)
1184
1185#define ELF_CLASS ELFCLASS64
1186#define ELF_DATA ELFDATA2MSB
1187#define ELF_ARCH EM_S390
1188
1189static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1190{
1191 regs->psw.addr = infop->entry;
1192 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1193 regs->gprs[15] = infop->start_stack;
1194}
1195
1196#endif
1197
1198#ifdef TARGET_TILEGX
1199
1200
1201#define ELF_START_MMAP (0x00000020000000000ULL)
1202
1203#define elf_check_arch(x) ((x) == EM_TILEGX)
1204
1205#define ELF_CLASS ELFCLASS64
1206#define ELF_DATA ELFDATA2LSB
1207#define ELF_ARCH EM_TILEGX
1208
1209static inline void init_thread(struct target_pt_regs *regs,
1210 struct image_info *infop)
1211{
1212 regs->pc = infop->entry;
1213 regs->sp = infop->start_stack;
1214
1215}
1216
1217#define ELF_EXEC_PAGESIZE 65536
1218
1219#endif
1220
1221#ifndef ELF_PLATFORM
1222#define ELF_PLATFORM (NULL)
1223#endif
1224
1225#ifndef ELF_MACHINE
1226#define ELF_MACHINE ELF_ARCH
1227#endif
1228
1229#ifndef elf_check_arch
1230#define elf_check_arch(x) ((x) == ELF_ARCH)
1231#endif
1232
1233#ifndef ELF_HWCAP
1234#define ELF_HWCAP 0
1235#endif
1236
1237#ifdef TARGET_ABI32
1238#undef ELF_CLASS
1239#define ELF_CLASS ELFCLASS32
1240#undef bswaptls
1241#define bswaptls(ptr) bswap32s(ptr)
1242#endif
1243
1244#include "elf.h"
1245
1246struct exec
1247{
1248 unsigned int a_info;
1249 unsigned int a_text;
1250 unsigned int a_data;
1251 unsigned int a_bss;
1252 unsigned int a_syms;
1253 unsigned int a_entry;
1254 unsigned int a_trsize;
1255 unsigned int a_drsize;
1256};
1257
1258
1259#define N_MAGIC(exec) ((exec).a_info & 0xffff)
1260#define OMAGIC 0407
1261#define NMAGIC 0410
1262#define ZMAGIC 0413
1263#define QMAGIC 0314
1264
1265
1266#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1267#define TARGET_ELF_PAGESTART(_v) ((_v) & \
1268 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1269#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1270
1271#define DLINFO_ITEMS 14
1272
1273static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1274{
1275 memcpy(to, from, n);
1276}
1277
1278#ifdef BSWAP_NEEDED
1279static void bswap_ehdr(struct elfhdr *ehdr)
1280{
1281 bswap16s(&ehdr->e_type);
1282 bswap16s(&ehdr->e_machine);
1283 bswap32s(&ehdr->e_version);
1284 bswaptls(&ehdr->e_entry);
1285 bswaptls(&ehdr->e_phoff);
1286 bswaptls(&ehdr->e_shoff);
1287 bswap32s(&ehdr->e_flags);
1288 bswap16s(&ehdr->e_ehsize);
1289 bswap16s(&ehdr->e_phentsize);
1290 bswap16s(&ehdr->e_phnum);
1291 bswap16s(&ehdr->e_shentsize);
1292 bswap16s(&ehdr->e_shnum);
1293 bswap16s(&ehdr->e_shstrndx);
1294}
1295
1296static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1297{
1298 int i;
1299 for (i = 0; i < phnum; ++i, ++phdr) {
1300 bswap32s(&phdr->p_type);
1301 bswap32s(&phdr->p_flags);
1302 bswaptls(&phdr->p_offset);
1303 bswaptls(&phdr->p_vaddr);
1304 bswaptls(&phdr->p_paddr);
1305 bswaptls(&phdr->p_filesz);
1306 bswaptls(&phdr->p_memsz);
1307 bswaptls(&phdr->p_align);
1308 }
1309}
1310
1311static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1312{
1313 int i;
1314 for (i = 0; i < shnum; ++i, ++shdr) {
1315 bswap32s(&shdr->sh_name);
1316 bswap32s(&shdr->sh_type);
1317 bswaptls(&shdr->sh_flags);
1318 bswaptls(&shdr->sh_addr);
1319 bswaptls(&shdr->sh_offset);
1320 bswaptls(&shdr->sh_size);
1321 bswap32s(&shdr->sh_link);
1322 bswap32s(&shdr->sh_info);
1323 bswaptls(&shdr->sh_addralign);
1324 bswaptls(&shdr->sh_entsize);
1325 }
1326}
1327
1328static void bswap_sym(struct elf_sym *sym)
1329{
1330 bswap32s(&sym->st_name);
1331 bswaptls(&sym->st_value);
1332 bswaptls(&sym->st_size);
1333 bswap16s(&sym->st_shndx);
1334}
1335#else
1336static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1337static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1338static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1339static inline void bswap_sym(struct elf_sym *sym) { }
1340#endif
1341
1342#ifdef USE_ELF_CORE_DUMP
1343static int elf_core_dump(int, const CPUArchState *);
1344#endif
1345static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1346
1347
1348
1349static bool elf_check_ident(struct elfhdr *ehdr)
1350{
1351 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1352 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1353 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1354 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1355 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1356 && ehdr->e_ident[EI_DATA] == ELF_DATA
1357 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1358}
1359
1360
1361
1362static bool elf_check_ehdr(struct elfhdr *ehdr)
1363{
1364 return (elf_check_arch(ehdr->e_machine)
1365 && ehdr->e_ehsize == sizeof(struct elfhdr)
1366 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1367 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1368}
1369
1370
1371
1372
1373
1374
1375
1376static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1377 abi_ulong p, abi_ulong stack_limit)
1378{
1379 char *tmp;
1380 int len, offset;
1381 abi_ulong top = p;
1382
1383 if (!p) {
1384 return 0;
1385 }
1386
1387 offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1388
1389 while (argc-- > 0) {
1390 tmp = argv[argc];
1391 if (!tmp) {
1392 fprintf(stderr, "VFS: argc is wrong");
1393 exit(-1);
1394 }
1395 len = strlen(tmp) + 1;
1396 tmp += len;
1397
1398 if (len > (p - stack_limit)) {
1399 return 0;
1400 }
1401 while (len) {
1402 int bytes_to_copy = (len > offset) ? offset : len;
1403 tmp -= bytes_to_copy;
1404 p -= bytes_to_copy;
1405 offset -= bytes_to_copy;
1406 len -= bytes_to_copy;
1407
1408 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1409
1410 if (offset == 0) {
1411 memcpy_to_target(p, scratch, top - p);
1412 top = p;
1413 offset = TARGET_PAGE_SIZE;
1414 }
1415 }
1416 }
1417 if (offset) {
1418 memcpy_to_target(p, scratch + offset, top - p);
1419 }
1420
1421 return p;
1422}
1423
1424
1425
1426
1427
1428
1429#define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1430
1431static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1432 struct image_info *info)
1433{
1434 abi_ulong size, error, guard;
1435
1436 size = guest_stack_size;
1437 if (size < STACK_LOWER_LIMIT) {
1438 size = STACK_LOWER_LIMIT;
1439 }
1440 guard = TARGET_PAGE_SIZE;
1441 if (guard < qemu_real_host_page_size) {
1442 guard = qemu_real_host_page_size;
1443 }
1444
1445 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1446 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1447 if (error == -1) {
1448 perror("mmap stack");
1449 exit(-1);
1450 }
1451
1452
1453 target_mprotect(error, guard, PROT_NONE);
1454
1455 info->stack_limit = error + guard;
1456
1457 return info->stack_limit + size - sizeof(void *);
1458}
1459
1460
1461
1462static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1463{
1464 uintptr_t host_start, host_map_start, host_end;
1465
1466 last_bss = TARGET_PAGE_ALIGN(last_bss);
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 host_start = (uintptr_t) g2h(elf_bss);
1480 host_end = (uintptr_t) g2h(last_bss);
1481 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1482
1483 if (host_map_start < host_end) {
1484 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1485 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1486 if (p == MAP_FAILED) {
1487 perror("cannot mmap brk");
1488 exit(-1);
1489 }
1490 }
1491
1492
1493 if ((page_get_flags(last_bss-1) & prot) != prot) {
1494 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1495 }
1496
1497 if (host_start < host_map_start) {
1498 memset((void *)host_start, 0, host_map_start - host_start);
1499 }
1500}
1501
1502#ifdef CONFIG_USE_FDPIC
1503static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1504{
1505 uint16_t n;
1506 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1507
1508
1509 n = info->nsegs;
1510 while (n--) {
1511 sp -= 12;
1512 put_user_u32(loadsegs[n].addr, sp+0);
1513 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1514 put_user_u32(loadsegs[n].p_memsz, sp+8);
1515 }
1516
1517
1518 sp -= 4;
1519 put_user_u16(0, sp+0);
1520 put_user_u16(info->nsegs, sp+2);
1521
1522 info->personality = PER_LINUX_FDPIC;
1523 info->loadmap_addr = sp;
1524
1525 return sp;
1526}
1527#endif
1528
1529static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1530 struct elfhdr *exec,
1531 struct image_info *info,
1532 struct image_info *interp_info)
1533{
1534 abi_ulong sp;
1535 abi_ulong sp_auxv;
1536 int size;
1537 int i;
1538 abi_ulong u_rand_bytes;
1539 uint8_t k_rand_bytes[16];
1540 abi_ulong u_platform;
1541 const char *k_platform;
1542 const int n = sizeof(elf_addr_t);
1543
1544 sp = p;
1545
1546#ifdef CONFIG_USE_FDPIC
1547
1548 if (elf_is_fdpic(exec)) {
1549
1550 sp &= ~3;
1551 sp = loader_build_fdpic_loadmap(info, sp);
1552 info->other_info = interp_info;
1553 if (interp_info) {
1554 interp_info->other_info = info;
1555 sp = loader_build_fdpic_loadmap(interp_info, sp);
1556 }
1557 }
1558#endif
1559
1560 u_platform = 0;
1561 k_platform = ELF_PLATFORM;
1562 if (k_platform) {
1563 size_t len = strlen(k_platform) + 1;
1564 sp -= (len + n - 1) & ~(n - 1);
1565 u_platform = sp;
1566
1567 memcpy_to_target(sp, k_platform, len);
1568 }
1569
1570
1571
1572
1573
1574 for (i = 0; i < 16; i++) {
1575 k_rand_bytes[i] = rand();
1576 }
1577 sp -= 16;
1578 u_rand_bytes = sp;
1579
1580 memcpy_to_target(sp, k_rand_bytes, 16);
1581
1582
1583
1584
1585 sp = sp &~ (abi_ulong)15;
1586 size = (DLINFO_ITEMS + 1) * 2;
1587 if (k_platform)
1588 size += 2;
1589#ifdef DLINFO_ARCH_ITEMS
1590 size += DLINFO_ARCH_ITEMS * 2;
1591#endif
1592#ifdef ELF_HWCAP2
1593 size += 2;
1594#endif
1595 size += envc + argc + 2;
1596 size += 1;
1597 size *= n;
1598 if (size & 15)
1599 sp -= 16 - (size & 15);
1600
1601
1602
1603
1604#define NEW_AUX_ENT(id, val) do { \
1605 sp -= n; put_user_ual(val, sp); \
1606 sp -= n; put_user_ual(id, sp); \
1607 } while(0)
1608
1609 sp_auxv = sp;
1610 NEW_AUX_ENT (AT_NULL, 0);
1611
1612
1613 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1614 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1615 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1616 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1617 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1618 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1619 NEW_AUX_ENT(AT_ENTRY, info->entry);
1620 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1621 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1622 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1623 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1624 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1625 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1626 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1627
1628#ifdef ELF_HWCAP2
1629 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1630#endif
1631
1632 if (k_platform)
1633 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1634#ifdef ARCH_DLINFO
1635
1636
1637
1638
1639 ARCH_DLINFO;
1640#endif
1641#undef NEW_AUX_ENT
1642
1643 info->saved_auxv = sp;
1644 info->auxv_len = sp_auxv - sp;
1645
1646 sp = loader_build_argptr(envc, argc, sp, p, 0);
1647
1648 assert(sp_auxv - sp == size);
1649 return sp;
1650}
1651
1652#ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1653
1654static int validate_guest_space(unsigned long guest_base,
1655 unsigned long guest_size)
1656{
1657 return 1;
1658}
1659#endif
1660
1661unsigned long init_guest_space(unsigned long host_start,
1662 unsigned long host_size,
1663 unsigned long guest_start,
1664 bool fixed)
1665{
1666 unsigned long current_start, real_start;
1667 int flags;
1668
1669 assert(host_start || host_size);
1670
1671
1672
1673 if (host_start && !host_size) {
1674 if (validate_guest_space(host_start, host_size) == 1) {
1675 return host_start;
1676 } else {
1677 return (unsigned long)-1;
1678 }
1679 }
1680
1681
1682 current_start = host_start & qemu_host_page_mask;
1683 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1684 if (fixed) {
1685 flags |= MAP_FIXED;
1686 }
1687
1688
1689
1690 while (1) {
1691 unsigned long real_size = host_size;
1692
1693
1694
1695
1696
1697 real_start = (unsigned long)
1698 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1699 if (real_start == (unsigned long)-1) {
1700 return (unsigned long)-1;
1701 }
1702
1703
1704 if (real_start & ~qemu_host_page_mask) {
1705 munmap((void *)real_start, host_size);
1706 real_size = host_size + qemu_host_page_size;
1707 real_start = (unsigned long)
1708 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1709 if (real_start == (unsigned long)-1) {
1710 return (unsigned long)-1;
1711 }
1712 real_start = HOST_PAGE_ALIGN(real_start);
1713 }
1714
1715
1716 if (!host_start || real_start == current_start) {
1717 int valid = validate_guest_space(real_start - guest_start,
1718 real_size);
1719 if (valid == 1) {
1720 break;
1721 } else if (valid == -1) {
1722 return (unsigned long)-1;
1723 }
1724
1725 }
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736 munmap((void *)real_start, host_size);
1737 current_start += qemu_host_page_size;
1738 if (host_start == current_start) {
1739
1740
1741
1742 return (unsigned long)-1;
1743 }
1744 }
1745
1746 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1747
1748 return real_start;
1749}
1750
1751static void probe_guest_base(const char *image_name,
1752 abi_ulong loaddr, abi_ulong hiaddr)
1753{
1754
1755
1756
1757
1758 const char *errmsg;
1759 if (!have_guest_base && !reserved_va) {
1760 unsigned long host_start, real_start, host_size;
1761
1762
1763 loaddr &= qemu_host_page_mask;
1764 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1765
1766 if (loaddr < mmap_min_addr) {
1767 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1768 } else {
1769 host_start = loaddr;
1770 if (host_start != loaddr) {
1771 errmsg = "Address overflow loading ELF binary";
1772 goto exit_errmsg;
1773 }
1774 }
1775 host_size = hiaddr - loaddr;
1776
1777
1778
1779
1780 real_start = init_guest_space(host_start, host_size, loaddr, false);
1781 if (real_start == (unsigned long)-1) {
1782 errmsg = "Unable to find space for application";
1783 goto exit_errmsg;
1784 }
1785 guest_base = real_start - loaddr;
1786
1787 qemu_log("Relocating guest address space from 0x"
1788 TARGET_ABI_FMT_lx " to 0x%lx\n",
1789 loaddr, real_start);
1790 }
1791 return;
1792
1793exit_errmsg:
1794 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1795 exit(-1);
1796}
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811static void load_elf_image(const char *image_name, int image_fd,
1812 struct image_info *info, char **pinterp_name,
1813 char bprm_buf[BPRM_BUF_SIZE])
1814{
1815 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1816 struct elf_phdr *phdr;
1817 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1818 int i, retval;
1819 const char *errmsg;
1820
1821
1822 errmsg = "Invalid ELF image for this architecture";
1823 if (!elf_check_ident(ehdr)) {
1824 goto exit_errmsg;
1825 }
1826 bswap_ehdr(ehdr);
1827 if (!elf_check_ehdr(ehdr)) {
1828 goto exit_errmsg;
1829 }
1830
1831 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1832 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1833 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1834 } else {
1835 phdr = (struct elf_phdr *) alloca(i);
1836 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1837 if (retval != i) {
1838 goto exit_read;
1839 }
1840 }
1841 bswap_phdr(phdr, ehdr->e_phnum);
1842
1843#ifdef CONFIG_USE_FDPIC
1844 info->nsegs = 0;
1845 info->pt_dynamic_addr = 0;
1846#endif
1847
1848
1849
1850 loaddr = -1, hiaddr = 0;
1851 for (i = 0; i < ehdr->e_phnum; ++i) {
1852 if (phdr[i].p_type == PT_LOAD) {
1853 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
1854 if (a < loaddr) {
1855 loaddr = a;
1856 }
1857 a = phdr[i].p_vaddr + phdr[i].p_memsz;
1858 if (a > hiaddr) {
1859 hiaddr = a;
1860 }
1861#ifdef CONFIG_USE_FDPIC
1862 ++info->nsegs;
1863#endif
1864 }
1865 }
1866
1867 load_addr = loaddr;
1868 if (ehdr->e_type == ET_DYN) {
1869
1870
1871
1872
1873
1874 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1875 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1876 -1, 0);
1877 if (load_addr == -1) {
1878 goto exit_perror;
1879 }
1880 } else if (pinterp_name != NULL) {
1881
1882
1883
1884 probe_guest_base(image_name, loaddr, hiaddr);
1885 }
1886 load_bias = load_addr - loaddr;
1887
1888#ifdef CONFIG_USE_FDPIC
1889 {
1890 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1891 g_malloc(sizeof(*loadsegs) * info->nsegs);
1892
1893 for (i = 0; i < ehdr->e_phnum; ++i) {
1894 switch (phdr[i].p_type) {
1895 case PT_DYNAMIC:
1896 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1897 break;
1898 case PT_LOAD:
1899 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1900 loadsegs->p_vaddr = phdr[i].p_vaddr;
1901 loadsegs->p_memsz = phdr[i].p_memsz;
1902 ++loadsegs;
1903 break;
1904 }
1905 }
1906 }
1907#endif
1908
1909 info->load_bias = load_bias;
1910 info->load_addr = load_addr;
1911 info->entry = ehdr->e_entry + load_bias;
1912 info->start_code = -1;
1913 info->end_code = 0;
1914 info->start_data = -1;
1915 info->end_data = 0;
1916 info->brk = 0;
1917 info->elf_flags = ehdr->e_flags;
1918
1919 for (i = 0; i < ehdr->e_phnum; i++) {
1920 struct elf_phdr *eppnt = phdr + i;
1921 if (eppnt->p_type == PT_LOAD) {
1922 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1923 int elf_prot = 0;
1924
1925 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1926 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1927 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1928
1929 vaddr = load_bias + eppnt->p_vaddr;
1930 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1931 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1932
1933 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1934 elf_prot, MAP_PRIVATE | MAP_FIXED,
1935 image_fd, eppnt->p_offset - vaddr_po);
1936 if (error == -1) {
1937 goto exit_perror;
1938 }
1939
1940 vaddr_ef = vaddr + eppnt->p_filesz;
1941 vaddr_em = vaddr + eppnt->p_memsz;
1942
1943
1944 if (vaddr_ef < vaddr_em) {
1945 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1946 }
1947
1948
1949 if (elf_prot & PROT_EXEC) {
1950 if (vaddr < info->start_code) {
1951 info->start_code = vaddr;
1952 }
1953 if (vaddr_ef > info->end_code) {
1954 info->end_code = vaddr_ef;
1955 }
1956 }
1957 if (elf_prot & PROT_WRITE) {
1958 if (vaddr < info->start_data) {
1959 info->start_data = vaddr;
1960 }
1961 if (vaddr_ef > info->end_data) {
1962 info->end_data = vaddr_ef;
1963 }
1964 if (vaddr_em > info->brk) {
1965 info->brk = vaddr_em;
1966 }
1967 }
1968 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1969 char *interp_name;
1970
1971 if (*pinterp_name) {
1972 errmsg = "Multiple PT_INTERP entries";
1973 goto exit_errmsg;
1974 }
1975 interp_name = malloc(eppnt->p_filesz);
1976 if (!interp_name) {
1977 goto exit_perror;
1978 }
1979
1980 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1981 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1982 eppnt->p_filesz);
1983 } else {
1984 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1985 eppnt->p_offset);
1986 if (retval != eppnt->p_filesz) {
1987 goto exit_perror;
1988 }
1989 }
1990 if (interp_name[eppnt->p_filesz - 1] != 0) {
1991 errmsg = "Invalid PT_INTERP entry";
1992 goto exit_errmsg;
1993 }
1994 *pinterp_name = interp_name;
1995 }
1996 }
1997
1998 if (info->end_data == 0) {
1999 info->start_data = info->end_code;
2000 info->end_data = info->end_code;
2001 info->brk = info->end_code;
2002 }
2003
2004 if (qemu_log_enabled()) {
2005 load_symbols(ehdr, image_fd, load_bias);
2006 }
2007
2008 close(image_fd);
2009 return;
2010
2011 exit_read:
2012 if (retval >= 0) {
2013 errmsg = "Incomplete read of file header";
2014 goto exit_errmsg;
2015 }
2016 exit_perror:
2017 errmsg = strerror(errno);
2018 exit_errmsg:
2019 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2020 exit(-1);
2021}
2022
2023static void load_elf_interp(const char *filename, struct image_info *info,
2024 char bprm_buf[BPRM_BUF_SIZE])
2025{
2026 int fd, retval;
2027
2028 fd = open(path(filename), O_RDONLY);
2029 if (fd < 0) {
2030 goto exit_perror;
2031 }
2032
2033 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2034 if (retval < 0) {
2035 goto exit_perror;
2036 }
2037 if (retval < BPRM_BUF_SIZE) {
2038 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2039 }
2040
2041 load_elf_image(filename, fd, info, NULL, bprm_buf);
2042 return;
2043
2044 exit_perror:
2045 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2046 exit(-1);
2047}
2048
2049static int symfind(const void *s0, const void *s1)
2050{
2051 target_ulong addr = *(target_ulong *)s0;
2052 struct elf_sym *sym = (struct elf_sym *)s1;
2053 int result = 0;
2054 if (addr < sym->st_value) {
2055 result = -1;
2056 } else if (addr >= sym->st_value + sym->st_size) {
2057 result = 1;
2058 }
2059 return result;
2060}
2061
2062static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2063{
2064#if ELF_CLASS == ELFCLASS32
2065 struct elf_sym *syms = s->disas_symtab.elf32;
2066#else
2067 struct elf_sym *syms = s->disas_symtab.elf64;
2068#endif
2069
2070
2071 struct elf_sym *sym;
2072
2073 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2074 if (sym != NULL) {
2075 return s->disas_strtab + sym->st_name;
2076 }
2077
2078 return "";
2079}
2080
2081
2082static int symcmp(const void *s0, const void *s1)
2083{
2084 struct elf_sym *sym0 = (struct elf_sym *)s0;
2085 struct elf_sym *sym1 = (struct elf_sym *)s1;
2086 return (sym0->st_value < sym1->st_value)
2087 ? -1
2088 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2089}
2090
2091
2092static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2093{
2094 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2095 struct elf_shdr *shdr;
2096 char *strings = NULL;
2097 struct syminfo *s = NULL;
2098 struct elf_sym *new_syms, *syms = NULL;
2099
2100 shnum = hdr->e_shnum;
2101 i = shnum * sizeof(struct elf_shdr);
2102 shdr = (struct elf_shdr *)alloca(i);
2103 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2104 return;
2105 }
2106
2107 bswap_shdr(shdr, shnum);
2108 for (i = 0; i < shnum; ++i) {
2109 if (shdr[i].sh_type == SHT_SYMTAB) {
2110 sym_idx = i;
2111 str_idx = shdr[i].sh_link;
2112 goto found;
2113 }
2114 }
2115
2116
2117 return;
2118
2119 found:
2120
2121 s = malloc(sizeof(*s));
2122 if (!s) {
2123 goto give_up;
2124 }
2125
2126 i = shdr[str_idx].sh_size;
2127 s->disas_strtab = strings = malloc(i);
2128 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
2129 goto give_up;
2130 }
2131
2132 i = shdr[sym_idx].sh_size;
2133 syms = malloc(i);
2134 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
2135 goto give_up;
2136 }
2137
2138 nsyms = i / sizeof(struct elf_sym);
2139 for (i = 0; i < nsyms; ) {
2140 bswap_sym(syms + i);
2141
2142 if (syms[i].st_shndx == SHN_UNDEF
2143 || syms[i].st_shndx >= SHN_LORESERVE
2144 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2145 if (i < --nsyms) {
2146 syms[i] = syms[nsyms];
2147 }
2148 } else {
2149#if defined(TARGET_ARM) || defined (TARGET_MIPS)
2150
2151 syms[i].st_value &= ~(target_ulong)1;
2152#endif
2153 syms[i].st_value += load_bias;
2154 i++;
2155 }
2156 }
2157
2158
2159 if (nsyms == 0) {
2160 goto give_up;
2161 }
2162
2163
2164
2165
2166
2167 new_syms = realloc(syms, nsyms * sizeof(*syms));
2168 if (new_syms == NULL) {
2169 goto give_up;
2170 }
2171 syms = new_syms;
2172
2173 qsort(syms, nsyms, sizeof(*syms), symcmp);
2174
2175 s->disas_num_syms = nsyms;
2176#if ELF_CLASS == ELFCLASS32
2177 s->disas_symtab.elf32 = syms;
2178#else
2179 s->disas_symtab.elf64 = syms;
2180#endif
2181 s->lookup_symbol = lookup_symbolxx;
2182 s->next = syminfos;
2183 syminfos = s;
2184
2185 return;
2186
2187give_up:
2188 free(s);
2189 free(strings);
2190 free(syms);
2191}
2192
2193int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2194{
2195 struct image_info interp_info;
2196 struct elfhdr elf_ex;
2197 char *elf_interpreter = NULL;
2198 char *scratch;
2199
2200 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2201
2202 load_elf_image(bprm->filename, bprm->fd, info,
2203 &elf_interpreter, bprm->buf);
2204
2205
2206
2207
2208 elf_ex = *(struct elfhdr *)bprm->buf;
2209
2210
2211
2212 bprm->p = setup_arg_pages(bprm, info);
2213
2214 scratch = g_new0(char, TARGET_PAGE_SIZE);
2215 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2216 bprm->p, info->stack_limit);
2217 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2218 bprm->p, info->stack_limit);
2219 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2220 bprm->p, info->stack_limit);
2221 g_free(scratch);
2222
2223 if (!bprm->p) {
2224 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2225 exit(-1);
2226 }
2227
2228 if (elf_interpreter) {
2229 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2230
2231
2232
2233
2234 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2235 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2236 info->personality = PER_SVR4;
2237
2238
2239
2240
2241
2242 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2243 MAP_FIXED | MAP_PRIVATE, -1, 0);
2244 }
2245 }
2246
2247 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2248 info, (elf_interpreter ? &interp_info : NULL));
2249 info->start_stack = bprm->p;
2250
2251
2252
2253
2254
2255 if (elf_interpreter) {
2256 info->load_bias = interp_info.load_bias;
2257 info->entry = interp_info.entry;
2258 free(elf_interpreter);
2259 }
2260
2261#ifdef USE_ELF_CORE_DUMP
2262 bprm->core_dump = &elf_core_dump;
2263#endif
2264
2265 return 0;
2266}
2267
2268#ifdef USE_ELF_CORE_DUMP
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309struct memelfnote {
2310 const char *name;
2311 size_t namesz;
2312 size_t namesz_rounded;
2313 int type;
2314 size_t datasz;
2315 size_t datasz_rounded;
2316 void *data;
2317 size_t notesz;
2318};
2319
2320struct target_elf_siginfo {
2321 abi_int si_signo;
2322 abi_int si_code;
2323 abi_int si_errno;
2324};
2325
2326struct target_elf_prstatus {
2327 struct target_elf_siginfo pr_info;
2328 abi_short pr_cursig;
2329 abi_ulong pr_sigpend;
2330 abi_ulong pr_sighold;
2331 target_pid_t pr_pid;
2332 target_pid_t pr_ppid;
2333 target_pid_t pr_pgrp;
2334 target_pid_t pr_sid;
2335 struct target_timeval pr_utime;
2336 struct target_timeval pr_stime;
2337 struct target_timeval pr_cutime;
2338 struct target_timeval pr_cstime;
2339 target_elf_gregset_t pr_reg;
2340 abi_int pr_fpvalid;
2341};
2342
2343#define ELF_PRARGSZ (80)
2344
2345struct target_elf_prpsinfo {
2346 char pr_state;
2347 char pr_sname;
2348 char pr_zomb;
2349 char pr_nice;
2350 abi_ulong pr_flag;
2351 target_uid_t pr_uid;
2352 target_gid_t pr_gid;
2353 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2354
2355 char pr_fname[16];
2356 char pr_psargs[ELF_PRARGSZ];
2357};
2358
2359
2360struct elf_thread_status {
2361 QTAILQ_ENTRY(elf_thread_status) ets_link;
2362 struct target_elf_prstatus prstatus;
2363#if 0
2364 elf_fpregset_t fpu;
2365 struct task_struct *thread;
2366 elf_fpxregset_t xfpu;
2367#endif
2368 struct memelfnote notes[1];
2369 int num_notes;
2370};
2371
2372struct elf_note_info {
2373 struct memelfnote *notes;
2374 struct target_elf_prstatus *prstatus;
2375 struct target_elf_prpsinfo *psinfo;
2376
2377 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2378#if 0
2379
2380
2381
2382
2383 elf_fpregset_t *fpu;
2384 elf_fpxregset_t *xfpu;
2385 int thread_status_size;
2386#endif
2387 int notes_size;
2388 int numnote;
2389};
2390
2391struct vm_area_struct {
2392 target_ulong vma_start;
2393 target_ulong vma_end;
2394 abi_ulong vma_flags;
2395 QTAILQ_ENTRY(vm_area_struct) vma_link;
2396};
2397
2398struct mm_struct {
2399 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2400 int mm_count;
2401};
2402
2403static struct mm_struct *vma_init(void);
2404static void vma_delete(struct mm_struct *);
2405static int vma_add_mapping(struct mm_struct *, target_ulong,
2406 target_ulong, abi_ulong);
2407static int vma_get_mapping_count(const struct mm_struct *);
2408static struct vm_area_struct *vma_first(const struct mm_struct *);
2409static struct vm_area_struct *vma_next(struct vm_area_struct *);
2410static abi_ulong vma_dump_size(const struct vm_area_struct *);
2411static int vma_walker(void *priv, target_ulong start, target_ulong end,
2412 unsigned long flags);
2413
2414static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2415static void fill_note(struct memelfnote *, const char *, int,
2416 unsigned int, void *);
2417static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2418static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2419static void fill_auxv_note(struct memelfnote *, const TaskState *);
2420static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2421static size_t note_size(const struct memelfnote *);
2422static void free_note_info(struct elf_note_info *);
2423static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2424static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2425static int core_dump_filename(const TaskState *, char *, size_t);
2426
2427static int dump_write(int, const void *, size_t);
2428static int write_note(struct memelfnote *, int);
2429static int write_note_info(struct elf_note_info *, int);
2430
2431#ifdef BSWAP_NEEDED
2432static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2433{
2434 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2435 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2436 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2437 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2438 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2439 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2440 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2441 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2442 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2443 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2444
2445
2446 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2447}
2448
2449static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2450{
2451 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2452 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2453 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2454 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2455 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2456 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2457 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2458}
2459
2460static void bswap_note(struct elf_note *en)
2461{
2462 bswap32s(&en->n_namesz);
2463 bswap32s(&en->n_descsz);
2464 bswap32s(&en->n_type);
2465}
2466#else
2467static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2468static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2469static inline void bswap_note(struct elf_note *en) { }
2470#endif
2471
2472
2473
2474
2475
2476
2477
2478
2479static struct mm_struct *vma_init(void)
2480{
2481 struct mm_struct *mm;
2482
2483 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2484 return (NULL);
2485
2486 mm->mm_count = 0;
2487 QTAILQ_INIT(&mm->mm_mmap);
2488
2489 return (mm);
2490}
2491
2492static void vma_delete(struct mm_struct *mm)
2493{
2494 struct vm_area_struct *vma;
2495
2496 while ((vma = vma_first(mm)) != NULL) {
2497 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2498 g_free(vma);
2499 }
2500 g_free(mm);
2501}
2502
2503static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2504 target_ulong end, abi_ulong flags)
2505{
2506 struct vm_area_struct *vma;
2507
2508 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2509 return (-1);
2510
2511 vma->vma_start = start;
2512 vma->vma_end = end;
2513 vma->vma_flags = flags;
2514
2515 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2516 mm->mm_count++;
2517
2518 return (0);
2519}
2520
2521static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2522{
2523 return (QTAILQ_FIRST(&mm->mm_mmap));
2524}
2525
2526static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2527{
2528 return (QTAILQ_NEXT(vma, vma_link));
2529}
2530
2531static int vma_get_mapping_count(const struct mm_struct *mm)
2532{
2533 return (mm->mm_count);
2534}
2535
2536
2537
2538
2539static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2540{
2541
2542 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2543 return (0);
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553 if (vma->vma_flags & PROT_EXEC) {
2554 char page[TARGET_PAGE_SIZE];
2555
2556 copy_from_user(page, vma->vma_start, sizeof (page));
2557 if ((page[EI_MAG0] == ELFMAG0) &&
2558 (page[EI_MAG1] == ELFMAG1) &&
2559 (page[EI_MAG2] == ELFMAG2) &&
2560 (page[EI_MAG3] == ELFMAG3)) {
2561
2562
2563
2564
2565 return (0);
2566 }
2567 }
2568
2569 return (vma->vma_end - vma->vma_start);
2570}
2571
2572static int vma_walker(void *priv, target_ulong start, target_ulong end,
2573 unsigned long flags)
2574{
2575 struct mm_struct *mm = (struct mm_struct *)priv;
2576
2577 vma_add_mapping(mm, start, end, flags);
2578 return (0);
2579}
2580
2581static void fill_note(struct memelfnote *note, const char *name, int type,
2582 unsigned int sz, void *data)
2583{
2584 unsigned int namesz;
2585
2586 namesz = strlen(name) + 1;
2587 note->name = name;
2588 note->namesz = namesz;
2589 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2590 note->type = type;
2591 note->datasz = sz;
2592 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2593
2594 note->data = data;
2595
2596
2597
2598
2599
2600 note->notesz = sizeof (struct elf_note) +
2601 note->namesz_rounded + note->datasz_rounded;
2602}
2603
2604static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2605 uint32_t flags)
2606{
2607 (void) memset(elf, 0, sizeof(*elf));
2608
2609 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2610 elf->e_ident[EI_CLASS] = ELF_CLASS;
2611 elf->e_ident[EI_DATA] = ELF_DATA;
2612 elf->e_ident[EI_VERSION] = EV_CURRENT;
2613 elf->e_ident[EI_OSABI] = ELF_OSABI;
2614
2615 elf->e_type = ET_CORE;
2616 elf->e_machine = machine;
2617 elf->e_version = EV_CURRENT;
2618 elf->e_phoff = sizeof(struct elfhdr);
2619 elf->e_flags = flags;
2620 elf->e_ehsize = sizeof(struct elfhdr);
2621 elf->e_phentsize = sizeof(struct elf_phdr);
2622 elf->e_phnum = segs;
2623
2624 bswap_ehdr(elf);
2625}
2626
2627static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2628{
2629 phdr->p_type = PT_NOTE;
2630 phdr->p_offset = offset;
2631 phdr->p_vaddr = 0;
2632 phdr->p_paddr = 0;
2633 phdr->p_filesz = sz;
2634 phdr->p_memsz = 0;
2635 phdr->p_flags = 0;
2636 phdr->p_align = 0;
2637
2638 bswap_phdr(phdr, 1);
2639}
2640
2641static size_t note_size(const struct memelfnote *note)
2642{
2643 return (note->notesz);
2644}
2645
2646static void fill_prstatus(struct target_elf_prstatus *prstatus,
2647 const TaskState *ts, int signr)
2648{
2649 (void) memset(prstatus, 0, sizeof (*prstatus));
2650 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2651 prstatus->pr_pid = ts->ts_tid;
2652 prstatus->pr_ppid = getppid();
2653 prstatus->pr_pgrp = getpgrp();
2654 prstatus->pr_sid = getsid(0);
2655
2656 bswap_prstatus(prstatus);
2657}
2658
2659static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2660{
2661 char *base_filename;
2662 unsigned int i, len;
2663
2664 (void) memset(psinfo, 0, sizeof (*psinfo));
2665
2666 len = ts->info->arg_end - ts->info->arg_start;
2667 if (len >= ELF_PRARGSZ)
2668 len = ELF_PRARGSZ - 1;
2669 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2670 return -EFAULT;
2671 for (i = 0; i < len; i++)
2672 if (psinfo->pr_psargs[i] == 0)
2673 psinfo->pr_psargs[i] = ' ';
2674 psinfo->pr_psargs[len] = 0;
2675
2676 psinfo->pr_pid = getpid();
2677 psinfo->pr_ppid = getppid();
2678 psinfo->pr_pgrp = getpgrp();
2679 psinfo->pr_sid = getsid(0);
2680 psinfo->pr_uid = getuid();
2681 psinfo->pr_gid = getgid();
2682
2683 base_filename = g_path_get_basename(ts->bprm->filename);
2684
2685
2686
2687
2688 (void) strncpy(psinfo->pr_fname, base_filename,
2689 sizeof(psinfo->pr_fname));
2690
2691 g_free(base_filename);
2692 bswap_psinfo(psinfo);
2693 return (0);
2694}
2695
2696static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2697{
2698 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2699 elf_addr_t orig_auxv = auxv;
2700 void *ptr;
2701 int len = ts->info->auxv_len;
2702
2703
2704
2705
2706
2707
2708
2709
2710 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2711 if (ptr != NULL) {
2712 fill_note(note, "CORE", NT_AUXV, len, ptr);
2713 unlock_user(ptr, auxv, len);
2714 }
2715}
2716
2717
2718
2719
2720
2721
2722
2723
2724static int core_dump_filename(const TaskState *ts, char *buf,
2725 size_t bufsize)
2726{
2727 char timestamp[64];
2728 char *filename = NULL;
2729 char *base_filename = NULL;
2730 struct timeval tv;
2731 struct tm tm;
2732
2733 assert(bufsize >= PATH_MAX);
2734
2735 if (gettimeofday(&tv, NULL) < 0) {
2736 (void) fprintf(stderr, "unable to get current timestamp: %s",
2737 strerror(errno));
2738 return (-1);
2739 }
2740
2741 filename = strdup(ts->bprm->filename);
2742 base_filename = strdup(basename(filename));
2743 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2744 localtime_r(&tv.tv_sec, &tm));
2745 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2746 base_filename, timestamp, (int)getpid());
2747 free(base_filename);
2748 free(filename);
2749
2750 return (0);
2751}
2752
2753static int dump_write(int fd, const void *ptr, size_t size)
2754{
2755 const char *bufp = (const char *)ptr;
2756 ssize_t bytes_written, bytes_left;
2757 struct rlimit dumpsize;
2758 off_t pos;
2759
2760 bytes_written = 0;
2761 getrlimit(RLIMIT_CORE, &dumpsize);
2762 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2763 if (errno == ESPIPE) {
2764 bytes_left = size;
2765 } else {
2766 return pos;
2767 }
2768 } else {
2769 if (dumpsize.rlim_cur <= pos) {
2770 return -1;
2771 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2772 bytes_left = size;
2773 } else {
2774 size_t limit_left=dumpsize.rlim_cur - pos;
2775 bytes_left = limit_left >= size ? size : limit_left ;
2776 }
2777 }
2778
2779
2780
2781
2782
2783 do {
2784 bytes_written = write(fd, bufp, bytes_left);
2785 if (bytes_written < 0) {
2786 if (errno == EINTR)
2787 continue;
2788 return (-1);
2789 } else if (bytes_written == 0) {
2790 return (-1);
2791 }
2792 bufp += bytes_written;
2793 bytes_left -= bytes_written;
2794 } while (bytes_left > 0);
2795
2796 return (0);
2797}
2798
2799static int write_note(struct memelfnote *men, int fd)
2800{
2801 struct elf_note en;
2802
2803 en.n_namesz = men->namesz;
2804 en.n_type = men->type;
2805 en.n_descsz = men->datasz;
2806
2807 bswap_note(&en);
2808
2809 if (dump_write(fd, &en, sizeof(en)) != 0)
2810 return (-1);
2811 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2812 return (-1);
2813 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2814 return (-1);
2815
2816 return (0);
2817}
2818
2819static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2820{
2821 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2822 TaskState *ts = (TaskState *)cpu->opaque;
2823 struct elf_thread_status *ets;
2824
2825 ets = g_malloc0(sizeof (*ets));
2826 ets->num_notes = 1;
2827 fill_prstatus(&ets->prstatus, ts, 0);
2828 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2829 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2830 &ets->prstatus);
2831
2832 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2833
2834 info->notes_size += note_size(&ets->notes[0]);
2835}
2836
2837static void init_note_info(struct elf_note_info *info)
2838{
2839
2840
2841
2842
2843 memset(info, 0, sizeof (*info));
2844 QTAILQ_INIT(&info->thread_list);
2845}
2846
2847static int fill_note_info(struct elf_note_info *info,
2848 long signr, const CPUArchState *env)
2849{
2850#define NUMNOTES 3
2851 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2852 TaskState *ts = (TaskState *)cpu->opaque;
2853 int i;
2854
2855 info->notes = g_new0(struct memelfnote, NUMNOTES);
2856 if (info->notes == NULL)
2857 return (-ENOMEM);
2858 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2859 if (info->prstatus == NULL)
2860 return (-ENOMEM);
2861 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2862 if (info->prstatus == NULL)
2863 return (-ENOMEM);
2864
2865
2866
2867
2868
2869 fill_prstatus(info->prstatus, ts, signr);
2870 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2871 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2872 sizeof (*info->prstatus), info->prstatus);
2873 fill_psinfo(info->psinfo, ts);
2874 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2875 sizeof (*info->psinfo), info->psinfo);
2876 fill_auxv_note(&info->notes[2], ts);
2877 info->numnote = 3;
2878
2879 info->notes_size = 0;
2880 for (i = 0; i < info->numnote; i++)
2881 info->notes_size += note_size(&info->notes[i]);
2882
2883
2884 cpu_list_lock();
2885 CPU_FOREACH(cpu) {
2886 if (cpu == thread_cpu) {
2887 continue;
2888 }
2889 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
2890 }
2891 cpu_list_unlock();
2892
2893 return (0);
2894}
2895
2896static void free_note_info(struct elf_note_info *info)
2897{
2898 struct elf_thread_status *ets;
2899
2900 while (!QTAILQ_EMPTY(&info->thread_list)) {
2901 ets = QTAILQ_FIRST(&info->thread_list);
2902 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2903 g_free(ets);
2904 }
2905
2906 g_free(info->prstatus);
2907 g_free(info->psinfo);
2908 g_free(info->notes);
2909}
2910
2911static int write_note_info(struct elf_note_info *info, int fd)
2912{
2913 struct elf_thread_status *ets;
2914 int i, error = 0;
2915
2916
2917 for (i = 0; i < info->numnote; i++)
2918 if ((error = write_note(&info->notes[i], fd)) != 0)
2919 return (error);
2920
2921
2922 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
2923 if ((error = write_note(&ets->notes[0], fd)) != 0)
2924 return (error);
2925 }
2926
2927 return (0);
2928}
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973static int elf_core_dump(int signr, const CPUArchState *env)
2974{
2975 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2976 const TaskState *ts = (const TaskState *)cpu->opaque;
2977 struct vm_area_struct *vma = NULL;
2978 char corefile[PATH_MAX];
2979 struct elf_note_info info;
2980 struct elfhdr elf;
2981 struct elf_phdr phdr;
2982 struct rlimit dumpsize;
2983 struct mm_struct *mm = NULL;
2984 off_t offset = 0, data_offset = 0;
2985 int segs = 0;
2986 int fd = -1;
2987
2988 init_note_info(&info);
2989
2990 errno = 0;
2991 getrlimit(RLIMIT_CORE, &dumpsize);
2992 if (dumpsize.rlim_cur == 0)
2993 return 0;
2994
2995 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2996 return (-errno);
2997
2998 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2999 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3000 return (-errno);
3001
3002
3003
3004
3005
3006
3007 if ((mm = vma_init()) == NULL)
3008 goto out;
3009
3010 walk_memory_regions(mm, vma_walker);
3011 segs = vma_get_mapping_count(mm);
3012
3013
3014
3015
3016
3017 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3018 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3019 goto out;
3020
3021
3022 if (fill_note_info(&info, signr, env) < 0)
3023 goto out;
3024
3025 offset += sizeof (elf);
3026 offset += (segs + 1) * sizeof (struct elf_phdr);
3027
3028
3029 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3030
3031 offset += info.notes_size;
3032 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3033 goto out;
3034
3035
3036
3037
3038
3039 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3040
3041
3042
3043
3044
3045 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3046 (void) memset(&phdr, 0, sizeof (phdr));
3047
3048 phdr.p_type = PT_LOAD;
3049 phdr.p_offset = offset;
3050 phdr.p_vaddr = vma->vma_start;
3051 phdr.p_paddr = 0;
3052 phdr.p_filesz = vma_dump_size(vma);
3053 offset += phdr.p_filesz;
3054 phdr.p_memsz = vma->vma_end - vma->vma_start;
3055 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3056 if (vma->vma_flags & PROT_WRITE)
3057 phdr.p_flags |= PF_W;
3058 if (vma->vma_flags & PROT_EXEC)
3059 phdr.p_flags |= PF_X;
3060 phdr.p_align = ELF_EXEC_PAGESIZE;
3061
3062 bswap_phdr(&phdr, 1);
3063 dump_write(fd, &phdr, sizeof (phdr));
3064 }
3065
3066
3067
3068
3069
3070 if (write_note_info(&info, fd) < 0)
3071 goto out;
3072
3073
3074 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3075 goto out;
3076
3077
3078
3079
3080 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3081 abi_ulong addr;
3082 abi_ulong end;
3083
3084 end = vma->vma_start + vma_dump_size(vma);
3085
3086 for (addr = vma->vma_start; addr < end;
3087 addr += TARGET_PAGE_SIZE) {
3088 char page[TARGET_PAGE_SIZE];
3089 int error;
3090
3091
3092
3093
3094
3095 error = copy_from_user(page, addr, sizeof (page));
3096 if (error != 0) {
3097 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3098 addr);
3099 errno = -error;
3100 goto out;
3101 }
3102 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3103 goto out;
3104 }
3105 }
3106
3107 out:
3108 free_note_info(&info);
3109 if (mm != NULL)
3110 vma_delete(mm);
3111 (void) close(fd);
3112
3113 if (errno != 0)
3114 return (-errno);
3115 return (0);
3116}
3117#endif
3118
3119void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3120{
3121 init_thread(regs, infop);
3122}
3123