b5565dd2297245b557562a2b9276702432676abb
[qemu] / linux-user / elfload.c
1 /* This is the Linux kernel elf-loading code, ported into user space */
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.h"
18
19 #ifdef _ARCH_PPC64
20 #undef ARCH_DLINFO
21 #undef ELF_PLATFORM
22 #undef ELF_HWCAP
23 #undef ELF_CLASS
24 #undef ELF_DATA
25 #undef ELF_ARCH
26 #endif
27
28 #define ELF_OSABI   ELFOSABI_SYSV
29
30 /* from personality.h */
31
32 /*
33  * Flags for bug emulation.
34  *
35  * These occupy the top three bytes.
36  */
37 enum {
38         ADDR_NO_RANDOMIZE =     0x0040000,      /* disable randomization of VA space */
39         FDPIC_FUNCPTRS =        0x0080000,      /* userspace function ptrs point to descriptors
40                                                  * (signal handling)
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  * Personality types.
54  *
55  * These go in the low byte.  Avoid using the top bit, it will
56  * conflict with error returns.
57  */
58 enum {
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 |
65                                          WHOLE_SECONDS | SHORT_INODE,
66         PER_OSR5 =              0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
67         PER_WYSEV386 =          0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
68         PER_ISCR4 =             0x0005 | STICKY_TIMEOUTS,
69         PER_BSD =               0x0006,
70         PER_SUNOS =             0x0006 | STICKY_TIMEOUTS,
71         PER_XENIX =             0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
72         PER_LINUX32 =           0x0008,
73         PER_LINUX32_3GB =       0x0008 | ADDR_LIMIT_3GB,
74         PER_IRIX32 =            0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
75         PER_IRIXN32 =           0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
76         PER_IRIX64 =            0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
77         PER_RISCOS =            0x000c,
78         PER_SOLARIS =           0x000d | STICKY_TIMEOUTS,
79         PER_UW7 =               0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
80         PER_OSF4 =              0x000f,                  /* OSF/1 v4 */
81         PER_HPUX =              0x0010,
82         PER_MASK =              0x00ff,
83 };
84
85 /*
86  * Return the base personality without flags.
87  */
88 #define personality(pers)       (pers & PER_MASK)
89
90 /* this flag is uneffective under linux too, should be deleted */
91 #ifndef MAP_DENYWRITE
92 #define MAP_DENYWRITE 0
93 #endif
94
95 /* should probably go in elf.h */
96 #ifndef ELIBBAD
97 #define ELIBBAD 80
98 #endif
99
100 #ifdef TARGET_I386
101
102 #define ELF_PLATFORM get_elf_platform()
103
104 static const char *get_elf_platform(void)
105 {
106     static char elf_platform[] = "i386";
107     int family = (thread_env->cpuid_version >> 8) & 0xff;
108     if (family > 6)
109         family = 6;
110     if (family >= 3)
111         elf_platform[1] = '0' + family;
112     return elf_platform;
113 }
114
115 #define ELF_HWCAP get_elf_hwcap()
116
117 static uint32_t get_elf_hwcap(void)
118 {
119   return thread_env->cpuid_features;
120 }
121
122 #ifdef TARGET_X86_64
123 #define ELF_START_MMAP 0x2aaaaab000ULL
124 #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
125
126 #define ELF_CLASS      ELFCLASS64
127 #define ELF_DATA       ELFDATA2LSB
128 #define ELF_ARCH       EM_X86_64
129
130 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
131 {
132     regs->rax = 0;
133     regs->rsp = infop->start_stack;
134     regs->rip = infop->entry;
135 }
136
137 typedef target_ulong    elf_greg_t;
138 typedef uint32_t        target_uid_t;
139 typedef uint32_t        target_gid_t;
140 typedef int32_t         target_pid_t;
141
142 #define ELF_NREG    27
143 typedef elf_greg_t  elf_gregset_t[ELF_NREG];
144
145 /*
146  * Note that ELF_NREG should be 29 as there should be place for
147  * TRAPNO and ERR "registers" as well but linux doesn't dump
148  * those.
149  *
150  * See linux kernel: arch/x86/include/asm/elf.h
151  */ 
152 static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env)
153 {
154     (*regs)[0] = env->regs[15];
155     (*regs)[1] = env->regs[14];
156     (*regs)[2] = env->regs[13];
157     (*regs)[3] = env->regs[12];
158     (*regs)[4] = env->regs[R_EBP];
159     (*regs)[5] = env->regs[R_EBX];
160     (*regs)[6] = env->regs[11];
161     (*regs)[7] = env->regs[10];
162     (*regs)[8] = env->regs[9];
163     (*regs)[9] = env->regs[8];
164     (*regs)[10] = env->regs[R_EAX];
165     (*regs)[11] = env->regs[R_ECX];
166     (*regs)[12] = env->regs[R_EDX];
167     (*regs)[13] = env->regs[R_ESI];
168     (*regs)[14] = env->regs[R_EDI];
169     (*regs)[15] = env->regs[R_EAX]; /* XXX */
170     (*regs)[16] = env->eip;
171     (*regs)[17] = env->segs[R_CS].selector & 0xffff;
172     (*regs)[18] = env->eflags;
173     (*regs)[19] = env->regs[R_ESP];
174     (*regs)[20] = env->segs[R_SS].selector & 0xffff;
175     (*regs)[21] = env->segs[R_FS].selector & 0xffff;
176     (*regs)[22] = env->segs[R_GS].selector & 0xffff;
177     (*regs)[23] = env->segs[R_DS].selector & 0xffff;
178     (*regs)[24] = env->segs[R_ES].selector & 0xffff;
179     (*regs)[25] = env->segs[R_FS].selector & 0xffff;
180     (*regs)[26] = env->segs[R_GS].selector & 0xffff;
181 }
182
183 #else
184
185 #define ELF_START_MMAP 0x80000000
186
187 /*
188  * This is used to ensure we don't load something for the wrong architecture.
189  */
190 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
191
192 /*
193  * These are used to set parameters in the core dumps.
194  */
195 #define ELF_CLASS       ELFCLASS32
196 #define ELF_DATA        ELFDATA2LSB
197 #define ELF_ARCH        EM_386
198
199 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
200 {
201     regs->esp = infop->start_stack;
202     regs->eip = infop->entry;
203
204     /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
205        starts %edx contains a pointer to a function which might be
206        registered using `atexit'.  This provides a mean for the
207        dynamic linker to call DT_FINI functions for shared libraries
208        that have been loaded before the code runs.
209
210        A value of 0 tells we have no such handler.  */
211     regs->edx = 0;
212 }
213
214 typedef target_ulong    elf_greg_t;
215 typedef uint16_t        target_uid_t;
216 typedef uint16_t        target_gid_t;
217 typedef int32_t         target_pid_t;
218
219 #define ELF_NREG    17
220 typedef elf_greg_t  elf_gregset_t[ELF_NREG];
221
222 /*
223  * Note that ELF_NREG should be 19 as there should be place for
224  * TRAPNO and ERR "registers" as well but linux doesn't dump
225  * those.
226  *
227  * See linux kernel: arch/x86/include/asm/elf.h
228  */ 
229 static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env)
230 {
231     (*regs)[0] = env->regs[R_EBX];
232     (*regs)[1] = env->regs[R_ECX];
233     (*regs)[2] = env->regs[R_EDX];
234     (*regs)[3] = env->regs[R_ESI];
235     (*regs)[4] = env->regs[R_EDI];
236     (*regs)[5] = env->regs[R_EBP];
237     (*regs)[6] = env->regs[R_EAX];
238     (*regs)[7] = env->segs[R_DS].selector & 0xffff;
239     (*regs)[8] = env->segs[R_ES].selector & 0xffff;
240     (*regs)[9] = env->segs[R_FS].selector & 0xffff;
241     (*regs)[10] = env->segs[R_GS].selector & 0xffff;
242     (*regs)[11] = env->regs[R_EAX]; /* XXX */
243     (*regs)[12] = env->eip;
244     (*regs)[13] = env->segs[R_CS].selector & 0xffff;
245     (*regs)[14] = env->eflags;
246     (*regs)[15] = env->regs[R_ESP];
247     (*regs)[16] = env->segs[R_SS].selector & 0xffff;
248 }
249 #endif
250
251 #define USE_ELF_CORE_DUMP
252 #define ELF_EXEC_PAGESIZE       4096
253
254 #endif
255
256 #ifdef TARGET_ARM
257
258 #define ELF_START_MMAP 0x80000000
259
260 #define elf_check_arch(x) ( (x) == EM_ARM )
261
262 #define ELF_CLASS       ELFCLASS32
263 #ifdef TARGET_WORDS_BIGENDIAN
264 #define ELF_DATA        ELFDATA2MSB
265 #else
266 #define ELF_DATA        ELFDATA2LSB
267 #endif
268 #define ELF_ARCH        EM_ARM
269
270 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
271 {
272     abi_long stack = infop->start_stack;
273     memset(regs, 0, sizeof(*regs));
274     regs->ARM_cpsr = 0x10;
275     if (infop->entry & 1)
276       regs->ARM_cpsr |= CPSR_T;
277     regs->ARM_pc = infop->entry & 0xfffffffe;
278     regs->ARM_sp = infop->start_stack;
279     /* FIXME - what to for failure of get_user()? */
280     get_user_ual(regs->ARM_r2, stack + 8); /* envp */
281     get_user_ual(regs->ARM_r1, stack + 4); /* envp */
282     /* XXX: it seems that r0 is zeroed after ! */
283     regs->ARM_r0 = 0;
284     /* For uClinux PIC binaries.  */
285     /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
286     regs->ARM_r10 = infop->start_data;
287 }
288
289 typedef uint32_t elf_greg_t;
290 typedef uint16_t target_uid_t;
291 typedef uint16_t target_gid_t;
292 typedef int32_t  target_pid_t;
293
294 #define ELF_NREG    18
295 typedef elf_greg_t  elf_gregset_t[ELF_NREG];
296
297 static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env)
298 {
299     (*regs)[0] = env->regs[0];
300     (*regs)[1] = env->regs[1];
301     (*regs)[2] = env->regs[2];
302     (*regs)[3] = env->regs[3]; 
303     (*regs)[4] = env->regs[4];
304     (*regs)[5] = env->regs[5];
305     (*regs)[6] = env->regs[6];
306     (*regs)[7] = env->regs[7];
307     (*regs)[8] = env->regs[8];
308     (*regs)[9] = env->regs[9];
309     (*regs)[10] = env->regs[10];
310     (*regs)[11] = env->regs[11];
311     (*regs)[12] = env->regs[12];
312     (*regs)[13] = env->regs[13]; 
313     (*regs)[14] = env->regs[14];
314     (*regs)[15] = env->regs[15];
315
316     (*regs)[16] = cpsr_read((CPUState *)env);
317     (*regs)[17] = env->regs[0]; /* XXX */
318 }
319
320 #define USE_ELF_CORE_DUMP
321 #define ELF_EXEC_PAGESIZE       4096
322
323 enum
324 {
325   ARM_HWCAP_ARM_SWP       = 1 << 0,
326   ARM_HWCAP_ARM_HALF      = 1 << 1,
327   ARM_HWCAP_ARM_THUMB     = 1 << 2,
328   ARM_HWCAP_ARM_26BIT     = 1 << 3,
329   ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
330   ARM_HWCAP_ARM_FPA       = 1 << 5,
331   ARM_HWCAP_ARM_VFP       = 1 << 6,
332   ARM_HWCAP_ARM_EDSP      = 1 << 7,
333 };
334
335 #define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF              \
336                     | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT     \
337                     | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP)
338
339 #endif
340
341 #ifdef TARGET_SPARC
342 #ifdef TARGET_SPARC64
343
344 #define ELF_START_MMAP 0x80000000
345
346 #ifndef TARGET_ABI32
347 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
348 #else
349 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
350 #endif
351
352 #define ELF_CLASS   ELFCLASS64
353 #define ELF_DATA    ELFDATA2MSB
354 #define ELF_ARCH    EM_SPARCV9
355
356 #define STACK_BIAS              2047
357
358 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
359 {
360 #ifndef TARGET_ABI32
361     regs->tstate = 0;
362 #endif
363     regs->pc = infop->entry;
364     regs->npc = regs->pc + 4;
365     regs->y = 0;
366 #ifdef TARGET_ABI32
367     regs->u_regs[14] = infop->start_stack - 16 * 4;
368 #else
369     if (personality(infop->personality) == PER_LINUX32)
370         regs->u_regs[14] = infop->start_stack - 16 * 4;
371     else
372         regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
373 #endif
374 }
375
376 #else
377 #define ELF_START_MMAP 0x80000000
378
379 #define elf_check_arch(x) ( (x) == EM_SPARC )
380
381 #define ELF_CLASS   ELFCLASS32
382 #define ELF_DATA    ELFDATA2MSB
383 #define ELF_ARCH    EM_SPARC
384
385 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
386 {
387     regs->psr = 0;
388     regs->pc = infop->entry;
389     regs->npc = regs->pc + 4;
390     regs->y = 0;
391     regs->u_regs[14] = infop->start_stack - 16 * 4;
392 }
393
394 #endif
395 #endif
396
397 #ifdef TARGET_PPC
398
399 #define ELF_START_MMAP 0x80000000
400
401 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
402
403 #define elf_check_arch(x) ( (x) == EM_PPC64 )
404
405 #define ELF_CLASS       ELFCLASS64
406
407 #else
408
409 #define elf_check_arch(x) ( (x) == EM_PPC )
410
411 #define ELF_CLASS       ELFCLASS32
412
413 #endif
414
415 #ifdef TARGET_WORDS_BIGENDIAN
416 #define ELF_DATA        ELFDATA2MSB
417 #else
418 #define ELF_DATA        ELFDATA2LSB
419 #endif
420 #define ELF_ARCH        EM_PPC
421
422 /*
423  * We need to put in some extra aux table entries to tell glibc what
424  * the cache block size is, so it can use the dcbz instruction safely.
425  */
426 #define AT_DCACHEBSIZE          19
427 #define AT_ICACHEBSIZE          20
428 #define AT_UCACHEBSIZE          21
429 /* A special ignored type value for PPC, for glibc compatibility.  */
430 #define AT_IGNOREPPC            22
431 /*
432  * The requirements here are:
433  * - keep the final alignment of sp (sp & 0xf)
434  * - make sure the 32-bit value at the first 16 byte aligned position of
435  *   AUXV is greater than 16 for glibc compatibility.
436  *   AT_IGNOREPPC is used for that.
437  * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
438  *   even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
439  */
440 #define DLINFO_ARCH_ITEMS       5
441 #define ARCH_DLINFO                                                     \
442 do {                                                                    \
443         NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20);                              \
444         NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20);                              \
445         NEW_AUX_ENT(AT_UCACHEBSIZE, 0);                                 \
446         /*                                                              \
447          * Now handle glibc compatibility.                              \
448          */                                                             \
449         NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);                        \
450         NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);                        \
451  } while (0)
452
453 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
454 {
455     abi_ulong pos = infop->start_stack;
456     abi_ulong tmp;
457 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
458     abi_ulong entry, toc;
459 #endif
460
461     _regs->gpr[1] = infop->start_stack;
462 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
463     entry = ldq_raw(infop->entry) + infop->load_addr;
464     toc = ldq_raw(infop->entry + 8) + infop->load_addr;
465     _regs->gpr[2] = toc;
466     infop->entry = entry;
467 #endif
468     _regs->nip = infop->entry;
469     /* Note that isn't exactly what regular kernel does
470      * but this is what the ABI wants and is needed to allow
471      * execution of PPC BSD programs.
472      */
473     /* FIXME - what to for failure of get_user()? */
474     get_user_ual(_regs->gpr[3], pos);
475     pos += sizeof(abi_ulong);
476     _regs->gpr[4] = pos;
477     for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
478         tmp = ldl(pos);
479     _regs->gpr[5] = pos;
480 }
481
482 #define ELF_EXEC_PAGESIZE       4096
483
484 #endif
485
486 #ifdef TARGET_MIPS
487
488 #define ELF_START_MMAP 0x80000000
489
490 #define elf_check_arch(x) ( (x) == EM_MIPS )
491
492 #ifdef TARGET_MIPS64
493 #define ELF_CLASS   ELFCLASS64
494 #else
495 #define ELF_CLASS   ELFCLASS32
496 #endif
497 #ifdef TARGET_WORDS_BIGENDIAN
498 #define ELF_DATA        ELFDATA2MSB
499 #else
500 #define ELF_DATA        ELFDATA2LSB
501 #endif
502 #define ELF_ARCH    EM_MIPS
503
504 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
505 {
506     regs->cp0_status = 2 << CP0St_KSU;
507     regs->cp0_epc = infop->entry;
508     regs->regs[29] = infop->start_stack;
509 }
510
511 #define ELF_EXEC_PAGESIZE        4096
512
513 #endif /* TARGET_MIPS */
514
515 #ifdef TARGET_SH4
516
517 #define ELF_START_MMAP 0x80000000
518
519 #define elf_check_arch(x) ( (x) == EM_SH )
520
521 #define ELF_CLASS ELFCLASS32
522 #define ELF_DATA  ELFDATA2LSB
523 #define ELF_ARCH  EM_SH
524
525 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
526 {
527   /* Check other registers XXXXX */
528   regs->pc = infop->entry;
529   regs->regs[15] = infop->start_stack;
530 }
531
532 #define ELF_EXEC_PAGESIZE        4096
533
534 #endif
535
536 #ifdef TARGET_CRIS
537
538 #define ELF_START_MMAP 0x80000000
539
540 #define elf_check_arch(x) ( (x) == EM_CRIS )
541
542 #define ELF_CLASS ELFCLASS32
543 #define ELF_DATA  ELFDATA2LSB
544 #define ELF_ARCH  EM_CRIS
545
546 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
547 {
548   regs->erp = infop->entry;
549 }
550
551 #define ELF_EXEC_PAGESIZE        8192
552
553 #endif
554
555 #ifdef TARGET_M68K
556
557 #define ELF_START_MMAP 0x80000000
558
559 #define elf_check_arch(x) ( (x) == EM_68K )
560
561 #define ELF_CLASS       ELFCLASS32
562 #define ELF_DATA        ELFDATA2MSB
563 #define ELF_ARCH        EM_68K
564
565 /* ??? Does this need to do anything?
566 #define ELF_PLAT_INIT(_r) */
567
568 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
569 {
570     regs->usp = infop->start_stack;
571     regs->sr = 0;
572     regs->pc = infop->entry;
573 }
574
575 #define ELF_EXEC_PAGESIZE       8192
576
577 #endif
578
579 #ifdef TARGET_ALPHA
580
581 #define ELF_START_MMAP (0x30000000000ULL)
582
583 #define elf_check_arch(x) ( (x) == ELF_ARCH )
584
585 #define ELF_CLASS      ELFCLASS64
586 #define ELF_DATA       ELFDATA2MSB
587 #define ELF_ARCH       EM_ALPHA
588
589 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
590 {
591     regs->pc = infop->entry;
592     regs->ps = 8;
593     regs->usp = infop->start_stack;
594     regs->unique = infop->start_data; /* ? */
595     printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
596            regs->unique, infop->start_data);
597 }
598
599 #define ELF_EXEC_PAGESIZE        8192
600
601 #endif /* TARGET_ALPHA */
602
603 #ifndef ELF_PLATFORM
604 #define ELF_PLATFORM (NULL)
605 #endif
606
607 #ifndef ELF_HWCAP
608 #define ELF_HWCAP 0
609 #endif
610
611 #ifdef TARGET_ABI32
612 #undef ELF_CLASS
613 #define ELF_CLASS ELFCLASS32
614 #undef bswaptls
615 #define bswaptls(ptr) bswap32s(ptr)
616 #endif
617
618 #include "elf.h"
619
620 struct exec
621 {
622   unsigned int a_info;   /* Use macros N_MAGIC, etc for access */
623   unsigned int a_text;   /* length of text, in bytes */
624   unsigned int a_data;   /* length of data, in bytes */
625   unsigned int a_bss;    /* length of uninitialized data area, in bytes */
626   unsigned int a_syms;   /* length of symbol table data in file, in bytes */
627   unsigned int a_entry;  /* start address */
628   unsigned int a_trsize; /* length of relocation info for text, in bytes */
629   unsigned int a_drsize; /* length of relocation info for data, in bytes */
630 };
631
632
633 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
634 #define OMAGIC 0407
635 #define NMAGIC 0410
636 #define ZMAGIC 0413
637 #define QMAGIC 0314
638
639 /* max code+data+bss space allocated to elf interpreter */
640 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
641
642 /* max code+data+bss+brk space allocated to ET_DYN executables */
643 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
644
645 /* Necessary parameters */
646 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
647 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
648 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
649
650 #define INTERPRETER_NONE 0
651 #define INTERPRETER_AOUT 1
652 #define INTERPRETER_ELF 2
653
654 #define DLINFO_ITEMS 12
655
656 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
657 {
658         memcpy(to, from, n);
659 }
660
661 static int load_aout_interp(void * exptr, int interp_fd);
662
663 #ifdef BSWAP_NEEDED
664 static void bswap_ehdr(struct elfhdr *ehdr)
665 {
666     bswap16s(&ehdr->e_type);                    /* Object file type */
667     bswap16s(&ehdr->e_machine);         /* Architecture */
668     bswap32s(&ehdr->e_version);         /* Object file version */
669     bswaptls(&ehdr->e_entry);           /* Entry point virtual address */
670     bswaptls(&ehdr->e_phoff);           /* Program header table file offset */
671     bswaptls(&ehdr->e_shoff);           /* Section header table file offset */
672     bswap32s(&ehdr->e_flags);           /* Processor-specific flags */
673     bswap16s(&ehdr->e_ehsize);          /* ELF header size in bytes */
674     bswap16s(&ehdr->e_phentsize);               /* Program header table entry size */
675     bswap16s(&ehdr->e_phnum);           /* Program header table entry count */
676     bswap16s(&ehdr->e_shentsize);               /* Section header table entry size */
677     bswap16s(&ehdr->e_shnum);           /* Section header table entry count */
678     bswap16s(&ehdr->e_shstrndx);                /* Section header string table index */
679 }
680
681 static void bswap_phdr(struct elf_phdr *phdr)
682 {
683     bswap32s(&phdr->p_type);                    /* Segment type */
684     bswaptls(&phdr->p_offset);          /* Segment file offset */
685     bswaptls(&phdr->p_vaddr);           /* Segment virtual address */
686     bswaptls(&phdr->p_paddr);           /* Segment physical address */
687     bswaptls(&phdr->p_filesz);          /* Segment size in file */
688     bswaptls(&phdr->p_memsz);           /* Segment size in memory */
689     bswap32s(&phdr->p_flags);           /* Segment flags */
690     bswaptls(&phdr->p_align);           /* Segment alignment */
691 }
692
693 static void bswap_shdr(struct elf_shdr *shdr)
694 {
695     bswap32s(&shdr->sh_name);
696     bswap32s(&shdr->sh_type);
697     bswaptls(&shdr->sh_flags);
698     bswaptls(&shdr->sh_addr);
699     bswaptls(&shdr->sh_offset);
700     bswaptls(&shdr->sh_size);
701     bswap32s(&shdr->sh_link);
702     bswap32s(&shdr->sh_info);
703     bswaptls(&shdr->sh_addralign);
704     bswaptls(&shdr->sh_entsize);
705 }
706
707 static void bswap_sym(struct elf_sym *sym)
708 {
709     bswap32s(&sym->st_name);
710     bswaptls(&sym->st_value);
711     bswaptls(&sym->st_size);
712     bswap16s(&sym->st_shndx);
713 }
714 #endif
715
716 #ifdef USE_ELF_CORE_DUMP
717 static int elf_core_dump(int, const CPUState *);
718
719 #ifdef BSWAP_NEEDED
720 static void bswap_note(struct elf_note *en)
721 {
722     bswaptls(&en->n_namesz);
723     bswaptls(&en->n_descsz);
724     bswaptls(&en->n_type);
725 }
726 #endif /* BSWAP_NEEDED */
727
728 #endif /* USE_ELF_CORE_DUMP */
729
730 /*
731  * 'copy_elf_strings()' copies argument/envelope strings from user
732  * memory to free pages in kernel mem. These are in a format ready
733  * to be put directly into the top of new user memory.
734  *
735  */
736 static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
737                                   abi_ulong p)
738 {
739     char *tmp, *tmp1, *pag = NULL;
740     int len, offset = 0;
741
742     if (!p) {
743         return 0;       /* bullet-proofing */
744     }
745     while (argc-- > 0) {
746         tmp = argv[argc];
747         if (!tmp) {
748             fprintf(stderr, "VFS: argc is wrong");
749             exit(-1);
750         }
751         tmp1 = tmp;
752         while (*tmp++);
753         len = tmp - tmp1;
754         if (p < len) {  /* this shouldn't happen - 128kB */
755                 return 0;
756         }
757         while (len) {
758             --p; --tmp; --len;
759             if (--offset < 0) {
760                 offset = p % TARGET_PAGE_SIZE;
761                 pag = (char *)page[p/TARGET_PAGE_SIZE];
762                 if (!pag) {
763                     pag = (char *)malloc(TARGET_PAGE_SIZE);
764                     memset(pag, 0, TARGET_PAGE_SIZE);
765                     page[p/TARGET_PAGE_SIZE] = pag;
766                     if (!pag)
767                         return 0;
768                 }
769             }
770             if (len == 0 || offset == 0) {
771                 *(pag + offset) = *tmp;
772             }
773             else {
774               int bytes_to_copy = (len > offset) ? offset : len;
775               tmp -= bytes_to_copy;
776               p -= bytes_to_copy;
777               offset -= bytes_to_copy;
778               len -= bytes_to_copy;
779               memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
780             }
781         }
782     }
783     return p;
784 }
785
786 static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
787                                  struct image_info *info)
788 {
789     abi_ulong stack_base, size, error;
790     int i;
791
792     /* Create enough stack to hold everything.  If we don't use
793      * it for args, we'll use it for something else...
794      */
795     size = x86_stack_size;
796     if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
797         size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
798     error = target_mmap(0,
799                         size + qemu_host_page_size,
800                         PROT_READ | PROT_WRITE,
801                         MAP_PRIVATE | MAP_ANONYMOUS,
802                         -1, 0);
803     if (error == -1) {
804         perror("stk mmap");
805         exit(-1);
806     }
807     /* we reserve one extra page at the top of the stack as guard */
808     target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
809
810     stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
811     p += stack_base;
812
813     for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
814         if (bprm->page[i]) {
815             info->rss++;
816             /* FIXME - check return value of memcpy_to_target() for failure */
817             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
818             free(bprm->page[i]);
819         }
820         stack_base += TARGET_PAGE_SIZE;
821     }
822     return p;
823 }
824
825 static void set_brk(abi_ulong start, abi_ulong end)
826 {
827         /* page-align the start and end addresses... */
828         start = HOST_PAGE_ALIGN(start);
829         end = HOST_PAGE_ALIGN(end);
830         if (end <= start)
831                 return;
832         if(target_mmap(start, end - start,
833                        PROT_READ | PROT_WRITE | PROT_EXEC,
834                        MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
835             perror("cannot mmap brk");
836             exit(-1);
837         }
838 }
839
840
841 /* We need to explicitly zero any fractional pages after the data
842    section (i.e. bss).  This would contain the junk from the file that
843    should not be in memory. */
844 static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
845 {
846         abi_ulong nbyte;
847
848         if (elf_bss >= last_bss)
849                 return;
850
851         /* XXX: this is really a hack : if the real host page size is
852            smaller than the target page size, some pages after the end
853            of the file may not be mapped. A better fix would be to
854            patch target_mmap(), but it is more complicated as the file
855            size must be known */
856         if (qemu_real_host_page_size < qemu_host_page_size) {
857             abi_ulong end_addr, end_addr1;
858             end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
859                 ~(qemu_real_host_page_size - 1);
860             end_addr = HOST_PAGE_ALIGN(elf_bss);
861             if (end_addr1 < end_addr) {
862                 mmap((void *)g2h(end_addr1), end_addr - end_addr1,
863                      PROT_READ|PROT_WRITE|PROT_EXEC,
864                      MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
865             }
866         }
867
868         nbyte = elf_bss & (qemu_host_page_size-1);
869         if (nbyte) {
870             nbyte = qemu_host_page_size - nbyte;
871             do {
872                 /* FIXME - what to do if put_user() fails? */
873                 put_user_u8(0, elf_bss);
874                 elf_bss++;
875             } while (--nbyte);
876         }
877 }
878
879
880 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
881                                    struct elfhdr * exec,
882                                    abi_ulong load_addr,
883                                    abi_ulong load_bias,
884                                    abi_ulong interp_load_addr, int ibcs,
885                                    struct image_info *info)
886 {
887         abi_ulong sp;
888         int size;
889         abi_ulong u_platform;
890         const char *k_platform;
891         const int n = sizeof(elf_addr_t);
892
893         sp = p;
894         u_platform = 0;
895         k_platform = ELF_PLATFORM;
896         if (k_platform) {
897             size_t len = strlen(k_platform) + 1;
898             sp -= (len + n - 1) & ~(n - 1);
899             u_platform = sp;
900             /* FIXME - check return value of memcpy_to_target() for failure */
901             memcpy_to_target(sp, k_platform, len);
902         }
903         /*
904          * Force 16 byte _final_ alignment here for generality.
905          */
906         sp = sp &~ (abi_ulong)15;
907         size = (DLINFO_ITEMS + 1) * 2;
908         if (k_platform)
909           size += 2;
910 #ifdef DLINFO_ARCH_ITEMS
911         size += DLINFO_ARCH_ITEMS * 2;
912 #endif
913         size += envc + argc + 2;
914         size += (!ibcs ? 3 : 1);        /* argc itself */
915         size *= n;
916         if (size & 15)
917             sp -= 16 - (size & 15);
918
919         /* This is correct because Linux defines
920          * elf_addr_t as Elf32_Off / Elf64_Off
921          */
922 #define NEW_AUX_ENT(id, val) do {               \
923             sp -= n; put_user_ual(val, sp);     \
924             sp -= n; put_user_ual(id, sp);      \
925           } while(0)
926
927         NEW_AUX_ENT (AT_NULL, 0);
928
929         /* There must be exactly DLINFO_ITEMS entries here.  */
930         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
931         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
932         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
933         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
934         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
935         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
936         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
937         NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
938         NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
939         NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
940         NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
941         NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
942         NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
943         if (k_platform)
944             NEW_AUX_ENT(AT_PLATFORM, u_platform);
945 #ifdef ARCH_DLINFO
946         /*
947          * ARCH_DLINFO must come last so platform specific code can enforce
948          * special alignment requirements on the AUXV if necessary (eg. PPC).
949          */
950         ARCH_DLINFO;
951 #endif
952 #undef NEW_AUX_ENT
953
954         info->saved_auxv = sp;
955
956         sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
957         return sp;
958 }
959
960
961 static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
962                                  int interpreter_fd,
963                                  abi_ulong *interp_load_addr)
964 {
965         struct elf_phdr *elf_phdata  =  NULL;
966         struct elf_phdr *eppnt;
967         abi_ulong load_addr = 0;
968         int load_addr_set = 0;
969         int retval;
970         abi_ulong last_bss, elf_bss;
971         abi_ulong error;
972         int i;
973
974         elf_bss = 0;
975         last_bss = 0;
976         error = 0;
977
978 #ifdef BSWAP_NEEDED
979         bswap_ehdr(interp_elf_ex);
980 #endif
981         /* First of all, some simple consistency checks */
982         if ((interp_elf_ex->e_type != ET_EXEC &&
983              interp_elf_ex->e_type != ET_DYN) ||
984            !elf_check_arch(interp_elf_ex->e_machine)) {
985                 return ~((abi_ulong)0UL);
986         }
987
988
989         /* Now read in all of the header information */
990
991         if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
992             return ~(abi_ulong)0UL;
993
994         elf_phdata =  (struct elf_phdr *)
995                 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
996
997         if (!elf_phdata)
998           return ~((abi_ulong)0UL);
999
1000         /*
1001          * If the size of this structure has changed, then punt, since
1002          * we will be doing the wrong thing.
1003          */
1004         if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
1005             free(elf_phdata);
1006             return ~((abi_ulong)0UL);
1007         }
1008
1009         retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
1010         if(retval >= 0) {
1011             retval = read(interpreter_fd,
1012                            (char *) elf_phdata,
1013                            sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1014         }
1015         if (retval < 0) {
1016                 perror("load_elf_interp");
1017                 exit(-1);
1018                 free (elf_phdata);
1019                 return retval;
1020         }
1021 #ifdef BSWAP_NEEDED
1022         eppnt = elf_phdata;
1023         for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
1024             bswap_phdr(eppnt);
1025         }
1026 #endif
1027
1028         if (interp_elf_ex->e_type == ET_DYN) {
1029             /* in order to avoid hardcoding the interpreter load
1030                address in qemu, we allocate a big enough memory zone */
1031             error = target_mmap(0, INTERP_MAP_SIZE,
1032                                 PROT_NONE, MAP_PRIVATE | MAP_ANON,
1033                                 -1, 0);
1034             if (error == -1) {
1035                 perror("mmap");
1036                 exit(-1);
1037             }
1038             load_addr = error;
1039             load_addr_set = 1;
1040         }
1041
1042         eppnt = elf_phdata;
1043         for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
1044           if (eppnt->p_type == PT_LOAD) {
1045             int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
1046             int elf_prot = 0;
1047             abi_ulong vaddr = 0;
1048             abi_ulong k;
1049
1050             if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
1051             if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1052             if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1053             if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
1054                 elf_type |= MAP_FIXED;
1055                 vaddr = eppnt->p_vaddr;
1056             }
1057             error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
1058                  eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
1059                  elf_prot,
1060                  elf_type,
1061                  interpreter_fd,
1062                  eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
1063
1064             if (error == -1) {
1065               /* Real error */
1066               close(interpreter_fd);
1067               free(elf_phdata);
1068               return ~((abi_ulong)0UL);
1069             }
1070
1071             if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
1072               load_addr = error;
1073               load_addr_set = 1;
1074             }
1075
1076             /*
1077              * Find the end of the file  mapping for this phdr, and keep
1078              * track of the largest address we see for this.
1079              */
1080             k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
1081             if (k > elf_bss) elf_bss = k;
1082
1083             /*
1084              * Do the same thing for the memory mapping - between
1085              * elf_bss and last_bss is the bss section.
1086              */
1087             k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
1088             if (k > last_bss) last_bss = k;
1089           }
1090
1091         /* Now use mmap to map the library into memory. */
1092
1093         close(interpreter_fd);
1094
1095         /*
1096          * Now fill out the bss section.  First pad the last page up
1097          * to the page boundary, and then perform a mmap to make sure
1098          * that there are zeromapped pages up to and including the last
1099          * bss page.
1100          */
1101         padzero(elf_bss, last_bss);
1102         elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
1103
1104         /* Map the last of the bss segment */
1105         if (last_bss > elf_bss) {
1106             target_mmap(elf_bss, last_bss-elf_bss,
1107                         PROT_READ|PROT_WRITE|PROT_EXEC,
1108                         MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1109         }
1110         free(elf_phdata);
1111
1112         *interp_load_addr = load_addr;
1113         return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
1114 }
1115
1116 static int symfind(const void *s0, const void *s1)
1117 {
1118     struct elf_sym *key = (struct elf_sym *)s0;
1119     struct elf_sym *sym = (struct elf_sym *)s1;
1120     int result = 0;
1121     if (key->st_value < sym->st_value) {
1122         result = -1;
1123     } else if (key->st_value > sym->st_value + sym->st_size) {
1124         result = 1;
1125     }
1126     return result;
1127 }
1128
1129 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1130 {
1131 #if ELF_CLASS == ELFCLASS32
1132     struct elf_sym *syms = s->disas_symtab.elf32;
1133 #else
1134     struct elf_sym *syms = s->disas_symtab.elf64;
1135 #endif
1136
1137     // binary search
1138     struct elf_sym key;
1139     struct elf_sym *sym;
1140
1141     key.st_value = orig_addr;
1142
1143     sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), symfind);
1144     if (sym != 0) {
1145         return s->disas_strtab + sym->st_name;
1146     }
1147
1148     return "";
1149 }
1150
1151 /* FIXME: This should use elf_ops.h  */
1152 static int symcmp(const void *s0, const void *s1)
1153 {
1154     struct elf_sym *sym0 = (struct elf_sym *)s0;
1155     struct elf_sym *sym1 = (struct elf_sym *)s1;
1156     return (sym0->st_value < sym1->st_value)
1157         ? -1
1158         : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1159 }
1160
1161 /* Best attempt to load symbols from this ELF object. */
1162 static void load_symbols(struct elfhdr *hdr, int fd)
1163 {
1164     unsigned int i, nsyms;
1165     struct elf_shdr sechdr, symtab, strtab;
1166     char *strings;
1167     struct syminfo *s;
1168     struct elf_sym *syms;
1169
1170     lseek(fd, hdr->e_shoff, SEEK_SET);
1171     for (i = 0; i < hdr->e_shnum; i++) {
1172         if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
1173             return;
1174 #ifdef BSWAP_NEEDED
1175         bswap_shdr(&sechdr);
1176 #endif
1177         if (sechdr.sh_type == SHT_SYMTAB) {
1178             symtab = sechdr;
1179             lseek(fd, hdr->e_shoff
1180                   + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
1181             if (read(fd, &strtab, sizeof(strtab))
1182                 != sizeof(strtab))
1183                 return;
1184 #ifdef BSWAP_NEEDED
1185             bswap_shdr(&strtab);
1186 #endif
1187             goto found;
1188         }
1189     }
1190     return; /* Shouldn't happen... */
1191
1192  found:
1193     /* Now know where the strtab and symtab are.  Snarf them. */
1194     s = malloc(sizeof(*s));
1195     syms = malloc(symtab.sh_size);
1196     if (!syms)
1197         return;
1198     s->disas_strtab = strings = malloc(strtab.sh_size);
1199     if (!s->disas_strtab)
1200         return;
1201
1202     lseek(fd, symtab.sh_offset, SEEK_SET);
1203     if (read(fd, syms, symtab.sh_size) != symtab.sh_size)
1204         return;
1205
1206     nsyms = symtab.sh_size / sizeof(struct elf_sym);
1207
1208     i = 0;
1209     while (i < nsyms) {
1210 #ifdef BSWAP_NEEDED
1211         bswap_sym(syms + i);
1212 #endif
1213         // Throw away entries which we do not need.
1214         if (syms[i].st_shndx == SHN_UNDEF ||
1215                 syms[i].st_shndx >= SHN_LORESERVE ||
1216                 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1217             nsyms--;
1218             if (i < nsyms) {
1219                 syms[i] = syms[nsyms];
1220             }
1221             continue;
1222         }
1223 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
1224         /* The bottom address bit marks a Thumb or MIPS16 symbol.  */
1225         syms[i].st_value &= ~(target_ulong)1;
1226 #endif
1227         i++;
1228     }
1229     syms = realloc(syms, nsyms * sizeof(*syms));
1230
1231     qsort(syms, nsyms, sizeof(*syms), symcmp);
1232
1233     lseek(fd, strtab.sh_offset, SEEK_SET);
1234     if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
1235         return;
1236     s->disas_num_syms = nsyms;
1237 #if ELF_CLASS == ELFCLASS32
1238     s->disas_symtab.elf32 = syms;
1239     s->lookup_symbol = lookup_symbolxx;
1240 #else
1241     s->disas_symtab.elf64 = syms;
1242     s->lookup_symbol = lookup_symbolxx;
1243 #endif
1244     s->next = syminfos;
1245     syminfos = s;
1246 }
1247
1248 int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1249                     struct image_info * info)
1250 {
1251     struct elfhdr elf_ex;
1252     struct elfhdr interp_elf_ex;
1253     struct exec interp_ex;
1254     int interpreter_fd = -1; /* avoid warning */
1255     abi_ulong load_addr, load_bias;
1256     int load_addr_set = 0;
1257     unsigned int interpreter_type = INTERPRETER_NONE;
1258     unsigned char ibcs2_interpreter;
1259     int i;
1260     abi_ulong mapped_addr;
1261     struct elf_phdr * elf_ppnt;
1262     struct elf_phdr *elf_phdata;
1263     abi_ulong elf_bss, k, elf_brk;
1264     int retval;
1265     char * elf_interpreter;
1266     abi_ulong elf_entry, interp_load_addr = 0;
1267     int status;
1268     abi_ulong start_code, end_code, start_data, end_data;
1269     abi_ulong reloc_func_desc = 0;
1270     abi_ulong elf_stack;
1271     char passed_fileno[6];
1272
1273     ibcs2_interpreter = 0;
1274     status = 0;
1275     load_addr = 0;
1276     load_bias = 0;
1277     elf_ex = *((struct elfhdr *) bprm->buf);          /* exec-header */
1278 #ifdef BSWAP_NEEDED
1279     bswap_ehdr(&elf_ex);
1280 #endif
1281
1282     /* First of all, some simple consistency checks */
1283     if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1284                                 (! elf_check_arch(elf_ex.e_machine))) {
1285             return -ENOEXEC;
1286     }
1287
1288     bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1289     bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1290     bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1291     if (!bprm->p) {
1292         retval = -E2BIG;
1293     }
1294
1295     /* Now read in all of the header information */
1296     elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1297     if (elf_phdata == NULL) {
1298         return -ENOMEM;
1299     }
1300
1301     retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1302     if(retval > 0) {
1303         retval = read(bprm->fd, (char *) elf_phdata,
1304                                 elf_ex.e_phentsize * elf_ex.e_phnum);
1305     }
1306
1307     if (retval < 0) {
1308         perror("load_elf_binary");
1309         exit(-1);
1310         free (elf_phdata);
1311         return -errno;
1312     }
1313
1314 #ifdef BSWAP_NEEDED
1315     elf_ppnt = elf_phdata;
1316     for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1317         bswap_phdr(elf_ppnt);
1318     }
1319 #endif
1320     elf_ppnt = elf_phdata;
1321
1322     elf_bss = 0;
1323     elf_brk = 0;
1324
1325
1326     elf_stack = ~((abi_ulong)0UL);
1327     elf_interpreter = NULL;
1328     start_code = ~((abi_ulong)0UL);
1329     end_code = 0;
1330     start_data = 0;
1331     end_data = 0;
1332     interp_ex.a_info = 0;
1333
1334     for(i=0;i < elf_ex.e_phnum; i++) {
1335         if (elf_ppnt->p_type == PT_INTERP) {
1336             if ( elf_interpreter != NULL )
1337             {
1338                 free (elf_phdata);
1339                 free(elf_interpreter);
1340                 close(bprm->fd);
1341                 return -EINVAL;
1342             }
1343
1344             /* This is the program interpreter used for
1345              * shared libraries - for now assume that this
1346              * is an a.out format binary
1347              */
1348
1349             elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
1350
1351             if (elf_interpreter == NULL) {
1352                 free (elf_phdata);
1353                 close(bprm->fd);
1354                 return -ENOMEM;
1355             }
1356
1357             retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1358             if(retval >= 0) {
1359                 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
1360             }
1361             if(retval < 0) {
1362                 perror("load_elf_binary2");
1363                 exit(-1);
1364             }
1365
1366             /* If the program interpreter is one of these two,
1367                then assume an iBCS2 image. Otherwise assume
1368                a native linux image. */
1369
1370             /* JRP - Need to add X86 lib dir stuff here... */
1371
1372             if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1373                 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1374               ibcs2_interpreter = 1;
1375             }
1376
1377 #if 0
1378             printf("Using ELF interpreter %s\n", elf_interpreter);
1379 #endif
1380             if (retval >= 0) {
1381                 retval = open(path(elf_interpreter), O_RDONLY);
1382                 if(retval >= 0) {
1383                     interpreter_fd = retval;
1384                 }
1385                 else {
1386                     perror(elf_interpreter);
1387                     exit(-1);
1388                     /* retval = -errno; */
1389                 }
1390             }
1391
1392             if (retval >= 0) {
1393                 retval = lseek(interpreter_fd, 0, SEEK_SET);
1394                 if(retval >= 0) {
1395                     retval = read(interpreter_fd,bprm->buf,128);
1396                 }
1397             }
1398             if (retval >= 0) {
1399                 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
1400                 interp_elf_ex=*((struct elfhdr *) bprm->buf); /* elf exec-header */
1401             }
1402             if (retval < 0) {
1403                 perror("load_elf_binary3");
1404                 exit(-1);
1405                 free (elf_phdata);
1406                 free(elf_interpreter);
1407                 close(bprm->fd);
1408                 return retval;
1409             }
1410         }
1411         elf_ppnt++;
1412     }
1413
1414     /* Some simple consistency checks for the interpreter */
1415     if (elf_interpreter){
1416         interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1417
1418         /* Now figure out which format our binary is */
1419         if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1420                 (N_MAGIC(interp_ex) != QMAGIC)) {
1421           interpreter_type = INTERPRETER_ELF;
1422         }
1423
1424         if (interp_elf_ex.e_ident[0] != 0x7f ||
1425             strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
1426             interpreter_type &= ~INTERPRETER_ELF;
1427         }
1428
1429         if (!interpreter_type) {
1430             free(elf_interpreter);
1431             free(elf_phdata);
1432             close(bprm->fd);
1433             return -ELIBBAD;
1434         }
1435     }
1436
1437     /* OK, we are done with that, now set up the arg stuff,
1438        and then start this sucker up */
1439
1440     {
1441         char * passed_p;
1442
1443         if (interpreter_type == INTERPRETER_AOUT) {
1444             snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
1445             passed_p = passed_fileno;
1446
1447             if (elf_interpreter) {
1448                 bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
1449                 bprm->argc++;
1450             }
1451         }
1452         if (!bprm->p) {
1453             if (elf_interpreter) {
1454                 free(elf_interpreter);
1455             }
1456             free (elf_phdata);
1457             close(bprm->fd);
1458             return -E2BIG;
1459         }
1460     }
1461
1462     /* OK, This is the point of no return */
1463     info->end_data = 0;
1464     info->end_code = 0;
1465     info->start_mmap = (abi_ulong)ELF_START_MMAP;
1466     info->mmap = 0;
1467     elf_entry = (abi_ulong) elf_ex.e_entry;
1468
1469 #if defined(CONFIG_USE_GUEST_BASE)
1470     /*
1471      * In case where user has not explicitly set the guest_base, we
1472      * probe here that should we set it automatically.
1473      */
1474     if (guest_base == 0) {
1475         /*
1476          * Go through ELF program header table and find out whether
1477          * any of the segments drop below our current mmap_min_addr and
1478          * in that case set guest_base to corresponding address.
1479          */
1480         for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
1481             i++, elf_ppnt++) {
1482             if (elf_ppnt->p_type != PT_LOAD)
1483                 continue;
1484             if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
1485                 guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
1486                 qemu_log("setting guest_base=0x%lx\n", guest_base);
1487                 break;
1488             }
1489         }
1490     }
1491 #endif /* CONFIG_USE_GUEST_BASE */
1492
1493     /* Do this so that we can load the interpreter, if need be.  We will
1494        change some of these later */
1495     info->rss = 0;
1496     bprm->p = setup_arg_pages(bprm->p, bprm, info);
1497     info->start_stack = bprm->p;
1498
1499     /* Now we do a little grungy work by mmaping the ELF image into
1500      * the correct location in memory.  At this point, we assume that
1501      * the image should be loaded at fixed address, not at a variable
1502      * address.
1503      */
1504
1505     for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
1506         int elf_prot = 0;
1507         int elf_flags = 0;
1508         abi_ulong error;
1509
1510         if (elf_ppnt->p_type != PT_LOAD)
1511             continue;
1512
1513         if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1514         if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1515         if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1516         elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1517         if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1518             elf_flags |= MAP_FIXED;
1519         } else if (elf_ex.e_type == ET_DYN) {
1520             /* Try and get dynamic programs out of the way of the default mmap
1521                base, as well as whatever program they might try to exec.  This
1522                is because the brk will follow the loader, and is not movable.  */
1523             /* NOTE: for qemu, we do a big mmap to get enough space
1524                without hardcoding any address */
1525             error = target_mmap(0, ET_DYN_MAP_SIZE,
1526                                 PROT_NONE, MAP_PRIVATE | MAP_ANON,
1527                                 -1, 0);
1528             if (error == -1) {
1529                 perror("mmap");
1530                 exit(-1);
1531             }
1532             load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
1533         }
1534
1535         error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1536                             (elf_ppnt->p_filesz +
1537                              TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1538                             elf_prot,
1539                             (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1540                             bprm->fd,
1541                             (elf_ppnt->p_offset -
1542                              TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
1543         if (error == -1) {
1544             perror("mmap");
1545             exit(-1);
1546         }
1547
1548 #ifdef LOW_ELF_STACK
1549         if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1550             elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
1551 #endif
1552
1553         if (!load_addr_set) {
1554             load_addr_set = 1;
1555             load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1556             if (elf_ex.e_type == ET_DYN) {
1557                 load_bias += error -
1558                     TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
1559                 load_addr += load_bias;
1560                 reloc_func_desc = load_bias;
1561             }
1562         }
1563         k = elf_ppnt->p_vaddr;
1564         if (k < start_code)
1565             start_code = k;
1566         if (start_data < k)
1567             start_data = k;
1568         k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
1569         if (k > elf_bss)
1570             elf_bss = k;
1571         if ((elf_ppnt->p_flags & PF_X) && end_code <  k)
1572             end_code = k;
1573         if (end_data < k)
1574             end_data = k;
1575         k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1576         if (k > elf_brk) elf_brk = k;
1577     }
1578
1579     elf_entry += load_bias;
1580     elf_bss += load_bias;
1581     elf_brk += load_bias;
1582     start_code += load_bias;
1583     end_code += load_bias;
1584     start_data += load_bias;
1585     end_data += load_bias;
1586
1587     if (elf_interpreter) {
1588         if (interpreter_type & 1) {
1589             elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1590         }
1591         else if (interpreter_type & 2) {
1592             elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1593                                             &interp_load_addr);
1594         }
1595         reloc_func_desc = interp_load_addr;
1596
1597         close(interpreter_fd);
1598         free(elf_interpreter);
1599
1600         if (elf_entry == ~((abi_ulong)0UL)) {
1601             printf("Unable to load interpreter\n");
1602             free(elf_phdata);
1603             exit(-1);
1604             return 0;
1605         }
1606     }
1607
1608     free(elf_phdata);
1609
1610     if (qemu_log_enabled())
1611         load_symbols(&elf_ex, bprm->fd);
1612
1613     if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1614     info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1615
1616 #ifdef LOW_ELF_STACK
1617     info->start_stack = bprm->p = elf_stack - 4;
1618 #endif
1619     bprm->p = create_elf_tables(bprm->p,
1620                     bprm->argc,
1621                     bprm->envc,
1622                     &elf_ex,
1623                     load_addr, load_bias,
1624                     interp_load_addr,
1625                     (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1626                     info);
1627     info->load_addr = reloc_func_desc;
1628     info->start_brk = info->brk = elf_brk;
1629     info->end_code = end_code;
1630     info->start_code = start_code;
1631     info->start_data = start_data;
1632     info->end_data = end_data;
1633     info->start_stack = bprm->p;
1634
1635     /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1636        sections */
1637     set_brk(elf_bss, elf_brk);
1638
1639     padzero(elf_bss, elf_brk);
1640
1641 #if 0
1642     printf("(start_brk) %x\n" , info->start_brk);
1643     printf("(end_code) %x\n" , info->end_code);
1644     printf("(start_code) %x\n" , info->start_code);
1645     printf("(end_data) %x\n" , info->end_data);
1646     printf("(start_stack) %x\n" , info->start_stack);
1647     printf("(brk) %x\n" , info->brk);
1648 #endif
1649
1650     if ( info->personality == PER_SVR4 )
1651     {
1652             /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
1653                and some applications "depend" upon this behavior.
1654                Since we do not have the power to recompile these, we
1655                emulate the SVr4 behavior.  Sigh.  */
1656             mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
1657                                       MAP_FIXED | MAP_PRIVATE, -1, 0);
1658     }
1659
1660     info->entry = elf_entry;
1661
1662 #ifdef USE_ELF_CORE_DUMP
1663     bprm->core_dump = &elf_core_dump;
1664 #endif
1665
1666     return 0;
1667 }
1668
1669 #ifdef USE_ELF_CORE_DUMP
1670
1671 /*
1672  * Definitions to generate Intel SVR4-like core files.
1673  * These mostly have the same names as the SVR4 types with "elf_"
1674  * tacked on the front to prevent clashes with linux definitions,
1675  * and the typedef forms have been avoided.  This is mostly like
1676  * the SVR4 structure, but more Linuxy, with things that Linux does
1677  * not support and which gdb doesn't really use excluded.
1678  *
1679  * Fields we don't dump (their contents is zero) in linux-user qemu
1680  * are marked with XXX.
1681  *
1682  * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
1683  *
1684  * Porting ELF coredump for target is (quite) simple process.  First you
1685  * define ELF_USE_CORE_DUMP in target ELF code (where init_thread() for
1686  * the target resides):
1687  *
1688  * #define USE_ELF_CORE_DUMP
1689  *
1690  * Next you define type of register set used for dumping.  ELF specification
1691  * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
1692  *
1693  * typedef <target_regtype> elf_greg_t;
1694  * #define ELF_NREG <number of registers>
1695  * typedef elf_greg_t elf_gregset_t[ELF_NREG];
1696  *
1697  * Then define following types to match target types.  Actual types can
1698  * be found from linux kernel (arch/<ARCH>/include/asm/posix_types.h):
1699  *
1700  * typedef <target_uid_type> target_uid_t;
1701  * typedef <target_gid_type> target_gid_t;
1702  * typedef <target_pid_type> target_pid_t;
1703  *
1704  * Last step is to implement target specific function that copies registers
1705  * from given cpu into just specified register set.  Prototype is:
1706  *
1707  * static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env);
1708  *
1709  * Parameters:
1710  *     regs - copy register values into here (allocated and zeroed by caller)
1711  *     env - copy registers from here
1712  *
1713  * Example for ARM target is provided in this file.
1714  */
1715
1716 /* An ELF note in memory */
1717 struct memelfnote {
1718     const char *name;
1719     size_t     namesz;
1720     size_t     namesz_rounded;
1721     int        type;
1722     size_t     datasz;
1723     void       *data;
1724     size_t     notesz;
1725 };
1726
1727 struct elf_siginfo {
1728     int  si_signo; /* signal number */
1729     int  si_code;  /* extra code */
1730     int  si_errno; /* errno */
1731 };
1732
1733 struct elf_prstatus {
1734     struct elf_siginfo pr_info;      /* Info associated with signal */
1735     short              pr_cursig;    /* Current signal */
1736     target_ulong       pr_sigpend;   /* XXX */
1737     target_ulong       pr_sighold;   /* XXX */
1738     target_pid_t       pr_pid;
1739     target_pid_t       pr_ppid;
1740     target_pid_t       pr_pgrp;
1741     target_pid_t       pr_sid;
1742     struct target_timeval pr_utime;  /* XXX User time */
1743     struct target_timeval pr_stime;  /* XXX System time */
1744     struct target_timeval pr_cutime; /* XXX Cumulative user time */
1745     struct target_timeval pr_cstime; /* XXX Cumulative system time */
1746     elf_gregset_t      pr_reg;       /* GP registers */
1747     int                pr_fpvalid;   /* XXX */
1748 };
1749
1750 #define ELF_PRARGSZ     (80) /* Number of chars for args */
1751
1752 struct elf_prpsinfo {
1753     char         pr_state;       /* numeric process state */
1754     char         pr_sname;       /* char for pr_state */
1755     char         pr_zomb;        /* zombie */
1756     char         pr_nice;        /* nice val */
1757     target_ulong pr_flag;        /* flags */
1758     target_uid_t pr_uid;
1759     target_gid_t pr_gid;
1760     target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
1761     /* Lots missing */
1762     char    pr_fname[16];           /* filename of executable */
1763     char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
1764 };
1765
1766 /* Here is the structure in which status of each thread is captured. */
1767 struct elf_thread_status {
1768     TAILQ_ENTRY(elf_thread_status)  ets_link;
1769     struct elf_prstatus prstatus;   /* NT_PRSTATUS */
1770 #if 0
1771     elf_fpregset_t fpu;             /* NT_PRFPREG */
1772     struct task_struct *thread;
1773     elf_fpxregset_t xfpu;           /* ELF_CORE_XFPREG_TYPE */
1774 #endif
1775     struct memelfnote notes[1];
1776     int num_notes;
1777 };
1778
1779 struct elf_note_info {
1780     struct memelfnote   *notes;
1781     struct elf_prstatus *prstatus;  /* NT_PRSTATUS */
1782     struct elf_prpsinfo *psinfo;    /* NT_PRPSINFO */
1783
1784     TAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
1785 #if 0
1786     /*
1787      * Current version of ELF coredump doesn't support
1788      * dumping fp regs etc.
1789      */
1790     elf_fpregset_t *fpu;
1791     elf_fpxregset_t *xfpu;
1792     int thread_status_size;
1793 #endif
1794     int notes_size;
1795     int numnote;
1796 };
1797
1798 struct vm_area_struct {
1799     abi_ulong   vma_start;  /* start vaddr of memory region */
1800     abi_ulong   vma_end;    /* end vaddr of memory region */
1801     abi_ulong   vma_flags;  /* protection etc. flags for the region */
1802     TAILQ_ENTRY(vm_area_struct) vma_link;
1803 };
1804
1805 struct mm_struct {
1806     TAILQ_HEAD(, vm_area_struct) mm_mmap;
1807     int mm_count;           /* number of mappings */
1808 };
1809
1810 static struct mm_struct *vma_init(void);
1811 static void vma_delete(struct mm_struct *);
1812 static int vma_add_mapping(struct mm_struct *, abi_ulong,
1813     abi_ulong, abi_ulong);
1814 static int vma_get_mapping_count(const struct mm_struct *);
1815 static struct vm_area_struct *vma_first(const struct mm_struct *);
1816 static struct vm_area_struct *vma_next(struct vm_area_struct *);
1817 static abi_ulong vma_dump_size(const struct vm_area_struct *);
1818 static int vma_walker(void *priv, unsigned long start, unsigned long end,
1819     unsigned long flags);
1820
1821 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
1822 static void fill_note(struct memelfnote *, const char *, int,
1823     unsigned int, void *);
1824 static void fill_prstatus(struct elf_prstatus *, const TaskState *, int);
1825 static int fill_psinfo(struct elf_prpsinfo *, const TaskState *);
1826 static void fill_auxv_note(struct memelfnote *, const TaskState *);
1827 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
1828 static size_t note_size(const struct memelfnote *);
1829 static void free_note_info(struct elf_note_info *);
1830 static int fill_note_info(struct elf_note_info *, long, const CPUState *);
1831 static void fill_thread_info(struct elf_note_info *, const CPUState *);
1832 static int core_dump_filename(const TaskState *, char *, size_t);
1833
1834 static int dump_write(int, const void *, size_t);
1835 static int write_note(struct memelfnote *, int);
1836 static int write_note_info(struct elf_note_info *, int);
1837
1838 #ifdef BSWAP_NEEDED
1839 static void bswap_prstatus(struct elf_prstatus *);
1840 static void bswap_psinfo(struct elf_prpsinfo *);
1841
1842 static void bswap_prstatus(struct elf_prstatus *prstatus)
1843 {
1844     prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
1845     prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
1846     prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
1847     prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
1848     prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
1849     prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
1850     prstatus->pr_pid = tswap32(prstatus->pr_pid);
1851     prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
1852     prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
1853     prstatus->pr_sid = tswap32(prstatus->pr_sid);
1854     /* cpu times are not filled, so we skip them */
1855     /* regs should be in correct format already */
1856     prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
1857 }
1858
1859 static void bswap_psinfo(struct elf_prpsinfo *psinfo)
1860 {
1861     psinfo->pr_flag = tswapl(psinfo->pr_flag);
1862     psinfo->pr_uid = tswap16(psinfo->pr_uid);
1863     psinfo->pr_gid = tswap16(psinfo->pr_gid);
1864     psinfo->pr_pid = tswap32(psinfo->pr_pid);
1865     psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
1866     psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
1867     psinfo->pr_sid = tswap32(psinfo->pr_sid);
1868 }
1869 #endif /* BSWAP_NEEDED */
1870
1871 /*
1872  * Minimal support for linux memory regions.  These are needed
1873  * when we are finding out what memory exactly belongs to
1874  * emulated process.  No locks needed here, as long as
1875  * thread that received the signal is stopped.
1876  */
1877
1878 static struct mm_struct *vma_init(void)
1879 {
1880     struct mm_struct *mm;
1881
1882     if ((mm = qemu_malloc(sizeof (*mm))) == NULL)
1883         return (NULL);
1884
1885     mm->mm_count = 0;
1886     TAILQ_INIT(&mm->mm_mmap);
1887
1888     return (mm);
1889 }
1890
1891 static void vma_delete(struct mm_struct *mm)
1892 {
1893     struct vm_area_struct *vma;
1894
1895     while ((vma = vma_first(mm)) != NULL) {
1896         TAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
1897         qemu_free(vma);
1898     }
1899     qemu_free(mm);
1900 }
1901
1902 static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
1903     abi_ulong end, abi_ulong flags)
1904 {
1905     struct vm_area_struct *vma;
1906
1907     if ((vma = qemu_mallocz(sizeof (*vma))) == NULL)
1908         return (-1);
1909
1910     vma->vma_start = start;
1911     vma->vma_end = end;
1912     vma->vma_flags = flags;
1913
1914     TAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
1915     mm->mm_count++;
1916
1917     return (0);
1918 }
1919
1920 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
1921 {
1922     return (TAILQ_FIRST(&mm->mm_mmap));
1923 }
1924
1925 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
1926 {
1927     return (TAILQ_NEXT(vma, vma_link));
1928 }
1929
1930 static int vma_get_mapping_count(const struct mm_struct *mm)
1931 {
1932     return (mm->mm_count);
1933 }
1934
1935 /*
1936  * Calculate file (dump) size of given memory region.
1937  */
1938 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
1939 {
1940     /* if we cannot even read the first page, skip it */
1941     if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
1942         return (0);
1943
1944     /*
1945      * Usually we don't dump executable pages as they contain
1946      * non-writable code that debugger can read directly from
1947      * target library etc.  However, thread stacks are marked
1948      * also executable so we read in first page of given region
1949      * and check whether it contains elf header.  If there is
1950      * no elf header, we dump it.
1951      */
1952     if (vma->vma_flags & PROT_EXEC) {
1953         char page[TARGET_PAGE_SIZE];
1954
1955         copy_from_user(page, vma->vma_start, sizeof (page));
1956         if ((page[EI_MAG0] == ELFMAG0) &&
1957             (page[EI_MAG1] == ELFMAG1) &&
1958             (page[EI_MAG2] == ELFMAG2) &&
1959             (page[EI_MAG3] == ELFMAG3)) {
1960             /*
1961              * Mappings are possibly from ELF binary.  Don't dump
1962              * them.
1963              */
1964             return (0);
1965         }
1966     }
1967
1968     return (vma->vma_end - vma->vma_start);
1969 }
1970
1971 static int vma_walker(void *priv, unsigned long start, unsigned long end,
1972     unsigned long flags)
1973 {
1974     struct mm_struct *mm = (struct mm_struct *)priv;
1975
1976     /*
1977      * Don't dump anything that qemu has reserved for internal use.
1978      */
1979     if (flags & PAGE_RESERVED)
1980         return (0);
1981
1982     vma_add_mapping(mm, start, end, flags);
1983     return (0);
1984 }
1985
1986 static void fill_note(struct memelfnote *note, const char *name, int type,
1987     unsigned int sz, void *data)
1988 {
1989     unsigned int namesz;
1990
1991     namesz = strlen(name) + 1;
1992     note->name = name;
1993     note->namesz = namesz;
1994     note->namesz_rounded = roundup(namesz, sizeof (int32_t));
1995     note->type = type;
1996     note->datasz = roundup(sz, sizeof (int32_t));;
1997     note->data = data;
1998
1999     /*
2000      * We calculate rounded up note size here as specified by
2001      * ELF document.
2002      */
2003     note->notesz = sizeof (struct elf_note) +
2004         note->namesz_rounded + note->datasz;
2005 }
2006
2007 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2008     uint32_t flags)
2009 {
2010     (void) memset(elf, 0, sizeof(*elf));
2011
2012     (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2013     elf->e_ident[EI_CLASS] = ELF_CLASS;
2014     elf->e_ident[EI_DATA] = ELF_DATA;
2015     elf->e_ident[EI_VERSION] = EV_CURRENT;
2016     elf->e_ident[EI_OSABI] = ELF_OSABI;
2017
2018     elf->e_type = ET_CORE;
2019     elf->e_machine = machine;
2020     elf->e_version = EV_CURRENT;
2021     elf->e_phoff = sizeof(struct elfhdr);
2022     elf->e_flags = flags;
2023     elf->e_ehsize = sizeof(struct elfhdr);
2024     elf->e_phentsize = sizeof(struct elf_phdr);
2025     elf->e_phnum = segs;
2026
2027 #ifdef BSWAP_NEEDED
2028     bswap_ehdr(elf);
2029 #endif
2030 }
2031
2032 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2033 {
2034     phdr->p_type = PT_NOTE;
2035     phdr->p_offset = offset;
2036     phdr->p_vaddr = 0;
2037     phdr->p_paddr = 0;
2038     phdr->p_filesz = sz;
2039     phdr->p_memsz = 0;
2040     phdr->p_flags = 0;
2041     phdr->p_align = 0;
2042
2043 #ifdef BSWAP_NEEDED
2044     bswap_phdr(phdr);
2045 #endif
2046 }
2047
2048 static size_t note_size(const struct memelfnote *note)
2049 {
2050     return (note->notesz);
2051 }
2052
2053 static void fill_prstatus(struct elf_prstatus *prstatus,
2054     const TaskState *ts, int signr)
2055 {
2056     (void) memset(prstatus, 0, sizeof (*prstatus));
2057     prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2058     prstatus->pr_pid = ts->ts_tid;
2059     prstatus->pr_ppid = getppid();
2060     prstatus->pr_pgrp = getpgrp();
2061     prstatus->pr_sid = getsid(0);
2062
2063 #ifdef BSWAP_NEEDED
2064     bswap_prstatus(prstatus);
2065 #endif
2066 }
2067
2068 static int fill_psinfo(struct elf_prpsinfo *psinfo, const TaskState *ts)
2069 {
2070     char *filename, *base_filename;
2071     unsigned int i, len;
2072
2073     (void) memset(psinfo, 0, sizeof (*psinfo));
2074
2075     len = ts->info->arg_end - ts->info->arg_start;
2076     if (len >= ELF_PRARGSZ)
2077         len = ELF_PRARGSZ - 1;
2078     if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2079         return -EFAULT;
2080     for (i = 0; i < len; i++)
2081         if (psinfo->pr_psargs[i] == 0)
2082             psinfo->pr_psargs[i] = ' ';
2083     psinfo->pr_psargs[len] = 0;
2084
2085     psinfo->pr_pid = getpid();
2086     psinfo->pr_ppid = getppid();
2087     psinfo->pr_pgrp = getpgrp();
2088     psinfo->pr_sid = getsid(0);
2089     psinfo->pr_uid = getuid();
2090     psinfo->pr_gid = getgid();
2091
2092     filename = strdup(ts->bprm->filename);
2093     base_filename = strdup(basename(filename));
2094     (void) strncpy(psinfo->pr_fname, base_filename,
2095         sizeof(psinfo->pr_fname));
2096     free(base_filename);
2097     free(filename);
2098
2099 #ifdef BSWAP_NEEDED
2100     bswap_psinfo(psinfo);
2101 #endif
2102     return (0);
2103 }
2104
2105 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2106 {
2107     elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2108     elf_addr_t orig_auxv = auxv;
2109     abi_ulong val;
2110     void *ptr;
2111     int i, len;
2112
2113     /*
2114      * Auxiliary vector is stored in target process stack.  It contains
2115      * {type, value} pairs that we need to dump into note.  This is not
2116      * strictly necessary but we do it here for sake of completeness.
2117      */
2118
2119     /* find out lenght of the vector, AT_NULL is terminator */
2120     i = len = 0;
2121     do {
2122         get_user_ual(val, auxv);
2123         i += 2;
2124         auxv += 2 * sizeof (elf_addr_t);
2125     } while (val != AT_NULL);
2126     len = i * sizeof (elf_addr_t);
2127
2128     /* read in whole auxv vector and copy it to memelfnote */
2129     ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2130     if (ptr != NULL) {
2131         fill_note(note, "CORE", NT_AUXV, len, ptr);
2132         unlock_user(ptr, auxv, len);
2133     }
2134 }
2135
2136 /*
2137  * Constructs name of coredump file.  We have following convention
2138  * for the name:
2139  *     qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2140  *
2141  * Returns 0 in case of success, -1 otherwise (errno is set).
2142  */
2143 static int core_dump_filename(const TaskState *ts, char *buf,
2144     size_t bufsize)
2145 {
2146     char timestamp[64];
2147     char *filename = NULL;
2148     char *base_filename = NULL;
2149     struct timeval tv;
2150     struct tm tm;
2151
2152     assert(bufsize >= PATH_MAX);
2153
2154     if (gettimeofday(&tv, NULL) < 0) {
2155         (void) fprintf(stderr, "unable to get current timestamp: %s",
2156             strerror(errno));
2157         return (-1);
2158     }
2159
2160     filename = strdup(ts->bprm->filename);
2161     base_filename = strdup(basename(filename));
2162     (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2163         localtime_r(&tv.tv_sec, &tm));
2164     (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2165         base_filename, timestamp, (int)getpid());
2166     free(base_filename);
2167     free(filename);
2168
2169     return (0);
2170 }
2171
2172 static int dump_write(int fd, const void *ptr, size_t size)
2173 {
2174     const char *bufp = (const char *)ptr;
2175     ssize_t bytes_written, bytes_left;
2176     struct rlimit dumpsize;
2177     off_t pos;
2178
2179     bytes_written = 0;
2180     getrlimit(RLIMIT_CORE, &dumpsize);
2181     if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2182         if (errno == ESPIPE) { /* not a seekable stream */
2183             bytes_left = size;
2184         } else {
2185             return pos;
2186         }
2187     } else {
2188         if (dumpsize.rlim_cur <= pos) {
2189             return -1;
2190         } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2191             bytes_left = size;
2192         } else {
2193             size_t limit_left=dumpsize.rlim_cur - pos;
2194             bytes_left = limit_left >= size ? size : limit_left ;
2195         }
2196     }
2197
2198     /*
2199      * In normal conditions, single write(2) should do but
2200      * in case of socket etc. this mechanism is more portable.
2201      */
2202     do {
2203         bytes_written = write(fd, bufp, bytes_left);
2204         if (bytes_written < 0) {
2205             if (errno == EINTR)
2206                 continue;
2207             return (-1);
2208         } else if (bytes_written == 0) { /* eof */
2209             return (-1);
2210         }
2211         bufp += bytes_written;
2212         bytes_left -= bytes_written;
2213     } while (bytes_left > 0);
2214
2215     return (0);
2216 }
2217
2218 static int write_note(struct memelfnote *men, int fd)
2219 {
2220     struct elf_note en;
2221
2222     en.n_namesz = men->namesz;
2223     en.n_type = men->type;
2224     en.n_descsz = men->datasz;
2225
2226 #ifdef BSWAP_NEEDED
2227     bswap_note(&en);
2228 #endif
2229
2230     if (dump_write(fd, &en, sizeof(en)) != 0)
2231         return (-1);
2232     if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2233         return (-1);
2234     if (dump_write(fd, men->data, men->datasz) != 0)
2235         return (-1);
2236
2237     return (0);
2238 }
2239
2240 static void fill_thread_info(struct elf_note_info *info, const CPUState *env)
2241 {
2242     TaskState *ts = (TaskState *)env->opaque;
2243     struct elf_thread_status *ets;
2244
2245     ets = qemu_mallocz(sizeof (*ets));
2246     ets->num_notes = 1; /* only prstatus is dumped */
2247     fill_prstatus(&ets->prstatus, ts, 0);
2248     elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2249     fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2250         &ets->prstatus);
2251
2252     TAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2253
2254     info->notes_size += note_size(&ets->notes[0]);
2255 }
2256
2257 static int fill_note_info(struct elf_note_info *info,
2258     long signr, const CPUState *env)
2259 {
2260 #define NUMNOTES 3
2261     CPUState *cpu = NULL;
2262     TaskState *ts = (TaskState *)env->opaque;
2263     int i;
2264
2265     (void) memset(info, 0, sizeof (*info));
2266
2267     TAILQ_INIT(&info->thread_list);
2268
2269     info->notes = qemu_mallocz(NUMNOTES * sizeof (struct memelfnote));
2270     if (info->notes == NULL)
2271         return (-ENOMEM);
2272     info->prstatus = qemu_mallocz(sizeof (*info->prstatus));
2273     if (info->prstatus == NULL)
2274         return (-ENOMEM);
2275     info->psinfo = qemu_mallocz(sizeof (*info->psinfo));
2276     if (info->prstatus == NULL)
2277         return (-ENOMEM);
2278
2279     /*
2280      * First fill in status (and registers) of current thread
2281      * including process info & aux vector.
2282      */
2283     fill_prstatus(info->prstatus, ts, signr);
2284     elf_core_copy_regs(&info->prstatus->pr_reg, env);
2285     fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2286         sizeof (*info->prstatus), info->prstatus);
2287     fill_psinfo(info->psinfo, ts);
2288     fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2289         sizeof (*info->psinfo), info->psinfo);
2290     fill_auxv_note(&info->notes[2], ts);
2291     info->numnote = 3;
2292
2293     info->notes_size = 0;
2294     for (i = 0; i < info->numnote; i++)
2295         info->notes_size += note_size(&info->notes[i]);
2296
2297     /* read and fill status of all threads */
2298     cpu_list_lock();
2299     for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2300         if (cpu == thread_env)
2301             continue;
2302         fill_thread_info(info, cpu);
2303     }
2304     cpu_list_unlock();
2305
2306     return (0);
2307 }
2308
2309 static void free_note_info(struct elf_note_info *info)
2310 {
2311     struct elf_thread_status *ets;
2312
2313     while (!TAILQ_EMPTY(&info->thread_list)) {
2314         ets = TAILQ_FIRST(&info->thread_list);
2315         TAILQ_REMOVE(&info->thread_list, ets, ets_link);
2316         qemu_free(ets);
2317     }
2318
2319     qemu_free(info->prstatus);
2320     qemu_free(info->psinfo);
2321     qemu_free(info->notes);
2322 }
2323
2324 static int write_note_info(struct elf_note_info *info, int fd)
2325 {
2326     struct elf_thread_status *ets;
2327     int i, error = 0;
2328
2329     /* write prstatus, psinfo and auxv for current thread */
2330     for (i = 0; i < info->numnote; i++)
2331         if ((error = write_note(&info->notes[i], fd)) != 0)
2332             return (error);
2333
2334     /* write prstatus for each thread */
2335     for (ets = info->thread_list.tqh_first; ets != NULL;
2336         ets = ets->ets_link.tqe_next) {
2337         if ((error = write_note(&ets->notes[0], fd)) != 0)
2338             return (error);
2339     }
2340
2341     return (0);
2342 }
2343
2344 /*
2345  * Write out ELF coredump.
2346  *
2347  * See documentation of ELF object file format in:
2348  * http://www.caldera.com/developers/devspecs/gabi41.pdf
2349  *
2350  * Coredump format in linux is following:
2351  *
2352  * 0   +----------------------+         \
2353  *     | ELF header           | ET_CORE  |
2354  *     +----------------------+          |
2355  *     | ELF program headers  |          |--- headers
2356  *     | - NOTE section       |          |
2357  *     | - PT_LOAD sections   |          |
2358  *     +----------------------+         /
2359  *     | NOTEs:               |
2360  *     | - NT_PRSTATUS        |
2361  *     | - NT_PRSINFO         |
2362  *     | - NT_AUXV            |
2363  *     +----------------------+ <-- aligned to target page
2364  *     | Process memory dump  |
2365  *     :                      :
2366  *     .                      .
2367  *     :                      :
2368  *     |                      |
2369  *     +----------------------+
2370  *
2371  * NT_PRSTATUS -> struct elf_prstatus (per thread)
2372  * NT_PRSINFO  -> struct elf_prpsinfo
2373  * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2374  *
2375  * Format follows System V format as close as possible.  Current
2376  * version limitations are as follows:
2377  *     - no floating point registers are dumped
2378  *
2379  * Function returns 0 in case of success, negative errno otherwise.
2380  *
2381  * TODO: make this work also during runtime: it should be
2382  * possible to force coredump from running process and then
2383  * continue processing.  For example qemu could set up SIGUSR2
2384  * handler (provided that target process haven't registered
2385  * handler for that) that does the dump when signal is received.
2386  */
2387 static int elf_core_dump(int signr, const CPUState *env)
2388 {
2389     const TaskState *ts = (const TaskState *)env->opaque;
2390     struct vm_area_struct *vma = NULL;
2391     char corefile[PATH_MAX];
2392     struct elf_note_info info;
2393     struct elfhdr elf;
2394     struct elf_phdr phdr;
2395     struct rlimit dumpsize;
2396     struct mm_struct *mm = NULL;
2397     off_t offset = 0, data_offset = 0;
2398     int segs = 0;
2399     int fd = -1;
2400
2401     errno = 0;
2402     getrlimit(RLIMIT_CORE, &dumpsize);
2403     if (dumpsize.rlim_cur == 0)
2404        return 0;
2405
2406     if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2407         return (-errno);
2408
2409     if ((fd = open(corefile, O_WRONLY | O_CREAT,
2410         S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2411         return (-errno);
2412
2413     /*
2414      * Walk through target process memory mappings and
2415      * set up structure containing this information.  After
2416      * this point vma_xxx functions can be used.
2417      */
2418     if ((mm = vma_init()) == NULL)
2419         goto out;
2420
2421     walk_memory_regions(mm, vma_walker);
2422     segs = vma_get_mapping_count(mm);
2423
2424     /*
2425      * Construct valid coredump ELF header.  We also
2426      * add one more segment for notes.
2427      */
2428     fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2429     if (dump_write(fd, &elf, sizeof (elf)) != 0)
2430         goto out;
2431
2432     /* fill in in-memory version of notes */
2433     if (fill_note_info(&info, signr, env) < 0)
2434         goto out;
2435
2436     offset += sizeof (elf);                             /* elf header */
2437     offset += (segs + 1) * sizeof (struct elf_phdr);    /* program headers */
2438
2439     /* write out notes program header */
2440     fill_elf_note_phdr(&phdr, info.notes_size, offset);
2441
2442     offset += info.notes_size;
2443     if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2444         goto out;
2445
2446     /*
2447      * ELF specification wants data to start at page boundary so
2448      * we align it here.
2449      */
2450     offset = roundup(offset, ELF_EXEC_PAGESIZE);
2451
2452     /*
2453      * Write program headers for memory regions mapped in
2454      * the target process.
2455      */
2456     for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2457         (void) memset(&phdr, 0, sizeof (phdr));
2458
2459         phdr.p_type = PT_LOAD;
2460         phdr.p_offset = offset;
2461         phdr.p_vaddr = vma->vma_start;
2462         phdr.p_paddr = 0;
2463         phdr.p_filesz = vma_dump_size(vma);
2464         offset += phdr.p_filesz;
2465         phdr.p_memsz = vma->vma_end - vma->vma_start;
2466         phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2467         if (vma->vma_flags & PROT_WRITE)
2468             phdr.p_flags |= PF_W;
2469         if (vma->vma_flags & PROT_EXEC)
2470             phdr.p_flags |= PF_X;
2471         phdr.p_align = ELF_EXEC_PAGESIZE;
2472
2473         dump_write(fd, &phdr, sizeof (phdr));
2474     }
2475
2476     /*
2477      * Next we write notes just after program headers.  No
2478      * alignment needed here.
2479      */
2480     if (write_note_info(&info, fd) < 0)
2481         goto out;
2482
2483     /* align data to page boundary */
2484     data_offset = lseek(fd, 0, SEEK_CUR);
2485     data_offset = TARGET_PAGE_ALIGN(data_offset);
2486     if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2487         goto out;
2488
2489     /*
2490      * Finally we can dump process memory into corefile as well.
2491      */
2492     for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2493         abi_ulong addr;
2494         abi_ulong end;
2495
2496         end = vma->vma_start + vma_dump_size(vma);
2497
2498         for (addr = vma->vma_start; addr < end;
2499             addr += TARGET_PAGE_SIZE) {
2500             char page[TARGET_PAGE_SIZE];
2501             int error;
2502
2503             /*
2504              *  Read in page from target process memory and
2505              *  write it to coredump file.
2506              */
2507             error = copy_from_user(page, addr, sizeof (page));
2508             if (error != 0) {
2509                 (void) fprintf(stderr, "unable to dump " TARGET_FMT_lx "\n",
2510                     addr);
2511                 errno = -error;
2512                 goto out;
2513             }
2514             if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2515                 goto out;
2516         }
2517     }
2518
2519 out:
2520     free_note_info(&info);
2521     if (mm != NULL)
2522         vma_delete(mm);
2523     (void) close(fd);
2524
2525     if (errno != 0)
2526         return (-errno);
2527     return (0);
2528 }
2529
2530 #endif /* USE_ELF_CORE_DUMP */
2531
2532 static int load_aout_interp(void * exptr, int interp_fd)
2533 {
2534     printf("a.out interpreter not yet supported\n");
2535     return(0);
2536 }
2537
2538 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2539 {
2540     init_thread(regs, infop);
2541 }