~jderose/ubuntu/raring/qemu/vde-again

« back to all changes in this revision

Viewing changes to linux-user/elfload.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno, Aurelien Jarno
  • Date: 2008-08-25 04:38:35 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825043835-8e3tftavy8bujdch
Tags: 0.9.1-6
[ Aurelien Jarno ]
* debian/control: 
  - Update list of supported targets (Closes: bug#488339).
* debian/qemu-make-debian-root:
  - Use mktemp instead of $$ to create temporary directories (Closes: 
    bug#496394).
* debian/links:
  - Add missing links to manpages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include <stdio.h>
4
4
#include <sys/types.h>
5
5
#include <fcntl.h>
6
 
#include <sys/stat.h>
7
6
#include <errno.h>
8
7
#include <unistd.h>
9
8
#include <sys/mman.h>
13
12
#include "qemu.h"
14
13
#include "disas.h"
15
14
 
 
15
/* from personality.h */
 
16
 
 
17
/*
 
18
 * Flags for bug emulation.
 
19
 *
 
20
 * These occupy the top three bytes.
 
21
 */
 
22
enum {
 
23
        ADDR_NO_RANDOMIZE =     0x0040000,      /* disable randomization of VA space */
 
24
        FDPIC_FUNCPTRS =        0x0080000,      /* userspace function ptrs point to descriptors
 
25
                                                 * (signal handling)
 
26
                                                 */
 
27
        MMAP_PAGE_ZERO =        0x0100000,
 
28
        ADDR_COMPAT_LAYOUT =    0x0200000,
 
29
        READ_IMPLIES_EXEC =     0x0400000,
 
30
        ADDR_LIMIT_32BIT =      0x0800000,
 
31
        SHORT_INODE =           0x1000000,
 
32
        WHOLE_SECONDS =         0x2000000,
 
33
        STICKY_TIMEOUTS =       0x4000000,
 
34
        ADDR_LIMIT_3GB =        0x8000000,
 
35
};
 
36
 
 
37
/*
 
38
 * Personality types.
 
39
 *
 
40
 * These go in the low byte.  Avoid using the top bit, it will
 
41
 * conflict with error returns.
 
42
 */
 
43
enum {
 
44
        PER_LINUX =             0x0000,
 
45
        PER_LINUX_32BIT =       0x0000 | ADDR_LIMIT_32BIT,
 
46
        PER_LINUX_FDPIC =       0x0000 | FDPIC_FUNCPTRS,
 
47
        PER_SVR4 =              0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
 
48
        PER_SVR3 =              0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
 
49
        PER_SCOSVR3 =           0x0003 | STICKY_TIMEOUTS |
 
50
                                         WHOLE_SECONDS | SHORT_INODE,
 
51
        PER_OSR5 =              0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
 
52
        PER_WYSEV386 =          0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
 
53
        PER_ISCR4 =             0x0005 | STICKY_TIMEOUTS,
 
54
        PER_BSD =               0x0006,
 
55
        PER_SUNOS =             0x0006 | STICKY_TIMEOUTS,
 
56
        PER_XENIX =             0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
 
57
        PER_LINUX32 =           0x0008,
 
58
        PER_LINUX32_3GB =       0x0008 | ADDR_LIMIT_3GB,
 
59
        PER_IRIX32 =            0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
 
60
        PER_IRIXN32 =           0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
 
61
        PER_IRIX64 =            0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
 
62
        PER_RISCOS =            0x000c,
 
63
        PER_SOLARIS =           0x000d | STICKY_TIMEOUTS,
 
64
        PER_UW7 =               0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
 
65
        PER_OSF4 =              0x000f,                  /* OSF/1 v4 */
 
66
        PER_HPUX =              0x0010,
 
67
        PER_MASK =              0x00ff,
 
68
};
 
69
 
 
70
/*
 
71
 * Return the base personality without flags.
 
72
 */
 
73
#define personality(pers)       (pers & PER_MASK)
 
74
 
 
75
/* this flag is uneffective under linux too, should be deleted */
 
76
#ifndef MAP_DENYWRITE
 
77
#define MAP_DENYWRITE 0
 
78
#endif
 
79
 
 
80
/* should probably go in elf.h */
 
81
#ifndef ELIBBAD
 
82
#define ELIBBAD 80
 
83
#endif
 
84
 
16
85
#ifdef TARGET_I386
17
86
 
 
87
#define ELF_PLATFORM get_elf_platform()
 
88
 
 
89
static const char *get_elf_platform(void)
 
90
{
 
91
    static char elf_platform[] = "i386";
 
92
    int family = (global_env->cpuid_version >> 8) & 0xff;
 
93
    if (family > 6)
 
94
        family = 6;
 
95
    if (family >= 3)
 
96
        elf_platform[1] = '0' + family;
 
97
    return elf_platform;
 
98
}
 
99
 
 
100
#define ELF_HWCAP get_elf_hwcap()
 
101
 
 
102
static uint32_t get_elf_hwcap(void)
 
103
{
 
104
  return global_env->cpuid_features;
 
105
}
 
106
 
 
107
#ifdef TARGET_X86_64
 
108
#define ELF_START_MMAP 0x2aaaaab000ULL
 
109
#define elf_check_arch(x) ( ((x) == ELF_ARCH) )
 
110
 
 
111
#define ELF_CLASS      ELFCLASS64
 
112
#define ELF_DATA       ELFDATA2LSB
 
113
#define ELF_ARCH       EM_X86_64
 
114
 
 
115
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
116
{
 
117
    regs->rax = 0;
 
118
    regs->rsp = infop->start_stack;
 
119
    regs->rip = infop->entry;
 
120
}
 
121
 
 
122
#else
 
123
 
18
124
#define ELF_START_MMAP 0x80000000
19
125
 
20
126
/*
29
135
#define ELF_DATA        ELFDATA2LSB
30
136
#define ELF_ARCH        EM_386
31
137
 
32
 
        /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
33
 
           starts %edx contains a pointer to a function which might be
34
 
           registered using `atexit'.  This provides a mean for the
35
 
           dynamic linker to call DT_FINI functions for shared libraries
36
 
           that have been loaded before the code runs.
37
 
 
38
 
           A value of 0 tells we have no such handler.  */
39
 
#define ELF_PLAT_INIT(_r)       _r->edx = 0
40
 
 
41
138
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
42
139
{
43
140
    regs->esp = infop->start_stack;
44
141
    regs->eip = infop->entry;
 
142
 
 
143
    /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
 
144
       starts %edx contains a pointer to a function which might be
 
145
       registered using `atexit'.  This provides a mean for the
 
146
       dynamic linker to call DT_FINI functions for shared libraries
 
147
       that have been loaded before the code runs.
 
148
 
 
149
       A value of 0 tells we have no such handler.  */
 
150
    regs->edx = 0;
45
151
}
 
152
#endif
46
153
 
47
154
#define USE_ELF_CORE_DUMP
48
155
#define ELF_EXEC_PAGESIZE       4096
63
170
#endif
64
171
#define ELF_ARCH        EM_ARM
65
172
 
66
 
#define ELF_PLAT_INIT(_r)       _r->ARM_r0 = 0
67
 
 
68
173
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
69
174
{
70
 
    target_long *stack = (void *)infop->start_stack;
 
175
    abi_long stack = infop->start_stack;
71
176
    memset(regs, 0, sizeof(*regs));
72
177
    regs->ARM_cpsr = 0x10;
73
 
    regs->ARM_pc = infop->entry;
 
178
    if (infop->entry & 1)
 
179
      regs->ARM_cpsr |= CPSR_T;
 
180
    regs->ARM_pc = infop->entry & 0xfffffffe;
74
181
    regs->ARM_sp = infop->start_stack;
75
 
    regs->ARM_r2 = tswapl(stack[2]); /* envp */
76
 
    regs->ARM_r1 = tswapl(stack[1]); /* argv */
 
182
    /* FIXME - what to for failure of get_user()? */
 
183
    get_user_ual(regs->ARM_r2, stack + 8); /* envp */
 
184
    get_user_ual(regs->ARM_r1, stack + 4); /* envp */
77
185
    /* XXX: it seems that r0 is zeroed after ! */
78
 
    //    regs->ARM_r0 = tswapl(stack[0]); /* argc */
 
186
    regs->ARM_r0 = 0;
 
187
    /* For uClinux PIC binaries.  */
 
188
    /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
 
189
    regs->ARM_r10 = infop->start_data;
79
190
}
80
191
 
81
192
#define USE_ELF_CORE_DUMP
82
193
#define ELF_EXEC_PAGESIZE       4096
83
194
 
 
195
enum
 
196
{
 
197
  ARM_HWCAP_ARM_SWP       = 1 << 0,
 
198
  ARM_HWCAP_ARM_HALF      = 1 << 1,
 
199
  ARM_HWCAP_ARM_THUMB     = 1 << 2,
 
200
  ARM_HWCAP_ARM_26BIT     = 1 << 3,
 
201
  ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
 
202
  ARM_HWCAP_ARM_FPA       = 1 << 5,
 
203
  ARM_HWCAP_ARM_VFP       = 1 << 6,
 
204
  ARM_HWCAP_ARM_EDSP      = 1 << 7,
 
205
};
 
206
 
 
207
#define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF              \
 
208
                    | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT     \
 
209
                    | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP)
 
210
 
84
211
#endif
85
212
 
86
213
#ifdef TARGET_SPARC
87
 
 
 
214
#ifdef TARGET_SPARC64
 
215
 
 
216
#define ELF_START_MMAP 0x80000000
 
217
 
 
218
#ifndef TARGET_ABI32
 
219
#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
 
220
#else
 
221
#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
 
222
#endif
 
223
 
 
224
#define ELF_CLASS   ELFCLASS64
 
225
#define ELF_DATA    ELFDATA2MSB
 
226
#define ELF_ARCH    EM_SPARCV9
 
227
 
 
228
#define STACK_BIAS              2047
 
229
 
 
230
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
231
{
 
232
#ifndef TARGET_ABI32
 
233
    regs->tstate = 0;
 
234
#endif
 
235
    regs->pc = infop->entry;
 
236
    regs->npc = regs->pc + 4;
 
237
    regs->y = 0;
 
238
#ifdef TARGET_ABI32
 
239
    regs->u_regs[14] = infop->start_stack - 16 * 4;
 
240
#else
 
241
    if (personality(infop->personality) == PER_LINUX32)
 
242
        regs->u_regs[14] = infop->start_stack - 16 * 4;
 
243
    else
 
244
        regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
 
245
#endif
 
246
}
 
247
 
 
248
#else
88
249
#define ELF_START_MMAP 0x80000000
89
250
 
90
251
#define elf_check_arch(x) ( (x) == EM_SPARC )
93
254
#define ELF_DATA    ELFDATA2MSB
94
255
#define ELF_ARCH    EM_SPARC
95
256
 
96
 
/*XXX*/
97
 
#define ELF_PLAT_INIT(_r)
98
 
 
99
257
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
100
258
{
101
259
    regs->psr = 0;
106
264
}
107
265
 
108
266
#endif
 
267
#endif
109
268
 
110
269
#ifdef TARGET_PPC
111
270
 
112
271
#define ELF_START_MMAP 0x80000000
113
272
 
 
273
#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
 
274
 
 
275
#define elf_check_arch(x) ( (x) == EM_PPC64 )
 
276
 
 
277
#define ELF_CLASS       ELFCLASS64
 
278
 
 
279
#else
 
280
 
114
281
#define elf_check_arch(x) ( (x) == EM_PPC )
115
282
 
116
283
#define ELF_CLASS       ELFCLASS32
 
284
 
 
285
#endif
 
286
 
117
287
#ifdef TARGET_WORDS_BIGENDIAN
118
288
#define ELF_DATA        ELFDATA2MSB
119
289
#else
121
291
#endif
122
292
#define ELF_ARCH        EM_PPC
123
293
 
124
 
/* Note that isn't exactly what regular kernel does
125
 
 * but this is what the ABI wants and is needed to allow
126
 
 * execution of PPC BSD programs.
127
 
 */
128
 
#define ELF_PLAT_INIT(_r)                                  \
129
 
do {                                                       \
130
 
   unsigned long *pos = (unsigned long *)bprm->p, tmp = 1; \
131
 
    _r->gpr[3] = bprm->argc;                               \
132
 
    _r->gpr[4] = (unsigned long)++pos;                     \
133
 
    for (; tmp != 0; pos++)                                \
134
 
        tmp = *pos;                                        \
135
 
    _r->gpr[5] = (unsigned long)pos;                       \
136
 
} while (0)
137
 
 
138
294
/*
139
295
 * We need to put in some extra aux table entries to tell glibc what
140
296
 * the cache block size is, so it can use the dcbz instruction safely.
153
309
 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
154
310
 *   even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
155
311
 */
156
 
#define DLINFO_ARCH_ITEMS       3
 
312
#define DLINFO_ARCH_ITEMS       5
157
313
#define ARCH_DLINFO                                                     \
158
314
do {                                                                    \
159
 
        sp -= DLINFO_ARCH_ITEMS * 2;                                    \
160
 
        NEW_AUX_ENT(0, AT_DCACHEBSIZE, 0x20);                           \
161
 
        NEW_AUX_ENT(1, AT_ICACHEBSIZE, 0x20);                           \
162
 
        NEW_AUX_ENT(2, AT_UCACHEBSIZE, 0);                              \
 
315
        NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20);                              \
 
316
        NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20);                              \
 
317
        NEW_AUX_ENT(AT_UCACHEBSIZE, 0);                                 \
163
318
        /*                                                              \
164
319
         * Now handle glibc compatibility.                              \
165
320
         */                                                             \
166
 
        sp -= 2*2;                                                      \
167
 
        NEW_AUX_ENT(0, AT_IGNOREPPC, AT_IGNOREPPC);                     \
168
 
        NEW_AUX_ENT(1, AT_IGNOREPPC, AT_IGNOREPPC);                     \
 
321
        NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);                        \
 
322
        NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);                        \
169
323
 } while (0)
170
324
 
171
325
static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
172
326
{
173
 
    _regs->msr = 1 << MSR_PR; /* Set user mode */
 
327
    abi_ulong pos = infop->start_stack;
 
328
    abi_ulong tmp;
 
329
#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
 
330
    abi_ulong entry, toc;
 
331
#endif
 
332
 
174
333
    _regs->gpr[1] = infop->start_stack;
 
334
#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
 
335
    entry = ldq_raw(infop->entry) + infop->load_addr;
 
336
    toc = ldq_raw(infop->entry + 8) + infop->load_addr;
 
337
    _regs->gpr[2] = toc;
 
338
    infop->entry = entry;
 
339
#endif
175
340
    _regs->nip = infop->entry;
 
341
    /* Note that isn't exactly what regular kernel does
 
342
     * but this is what the ABI wants and is needed to allow
 
343
     * execution of PPC BSD programs.
 
344
     */
 
345
    /* FIXME - what to for failure of get_user()? */
 
346
    get_user_ual(_regs->gpr[3], pos);
 
347
    pos += sizeof(abi_ulong);
 
348
    _regs->gpr[4] = pos;
 
349
    for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
 
350
        tmp = ldl(pos);
 
351
    _regs->gpr[5] = pos;
176
352
}
177
353
 
178
354
#define USE_ELF_CORE_DUMP
180
356
 
181
357
#endif
182
358
 
 
359
#ifdef TARGET_MIPS
 
360
 
 
361
#define ELF_START_MMAP 0x80000000
 
362
 
 
363
#define elf_check_arch(x) ( (x) == EM_MIPS )
 
364
 
 
365
#ifdef TARGET_MIPS64
 
366
#define ELF_CLASS   ELFCLASS64
 
367
#else
 
368
#define ELF_CLASS   ELFCLASS32
 
369
#endif
 
370
#ifdef TARGET_WORDS_BIGENDIAN
 
371
#define ELF_DATA        ELFDATA2MSB
 
372
#else
 
373
#define ELF_DATA        ELFDATA2LSB
 
374
#endif
 
375
#define ELF_ARCH    EM_MIPS
 
376
 
 
377
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
378
{
 
379
    regs->cp0_status = 2 << CP0St_KSU;
 
380
    regs->cp0_epc = infop->entry;
 
381
    regs->regs[29] = infop->start_stack;
 
382
}
 
383
 
 
384
#define USE_ELF_CORE_DUMP
 
385
#define ELF_EXEC_PAGESIZE        4096
 
386
 
 
387
#endif /* TARGET_MIPS */
 
388
 
 
389
#ifdef TARGET_SH4
 
390
 
 
391
#define ELF_START_MMAP 0x80000000
 
392
 
 
393
#define elf_check_arch(x) ( (x) == EM_SH )
 
394
 
 
395
#define ELF_CLASS ELFCLASS32
 
396
#define ELF_DATA  ELFDATA2LSB
 
397
#define ELF_ARCH  EM_SH
 
398
 
 
399
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
400
{
 
401
  /* Check other registers XXXXX */
 
402
  regs->pc = infop->entry;
 
403
  regs->regs[15] = infop->start_stack;
 
404
}
 
405
 
 
406
#define USE_ELF_CORE_DUMP
 
407
#define ELF_EXEC_PAGESIZE        4096
 
408
 
 
409
#endif
 
410
 
 
411
#ifdef TARGET_CRIS
 
412
 
 
413
#define ELF_START_MMAP 0x80000000
 
414
 
 
415
#define elf_check_arch(x) ( (x) == EM_CRIS )
 
416
 
 
417
#define ELF_CLASS ELFCLASS32
 
418
#define ELF_DATA  ELFDATA2LSB
 
419
#define ELF_ARCH  EM_CRIS
 
420
 
 
421
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
422
{
 
423
  regs->erp = infop->entry;
 
424
}
 
425
 
 
426
#define USE_ELF_CORE_DUMP
 
427
#define ELF_EXEC_PAGESIZE        8192
 
428
 
 
429
#endif
 
430
 
 
431
#ifdef TARGET_M68K
 
432
 
 
433
#define ELF_START_MMAP 0x80000000
 
434
 
 
435
#define elf_check_arch(x) ( (x) == EM_68K )
 
436
 
 
437
#define ELF_CLASS       ELFCLASS32
 
438
#define ELF_DATA        ELFDATA2MSB
 
439
#define ELF_ARCH        EM_68K
 
440
 
 
441
/* ??? Does this need to do anything?
 
442
#define ELF_PLAT_INIT(_r) */
 
443
 
 
444
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
445
{
 
446
    regs->usp = infop->start_stack;
 
447
    regs->sr = 0;
 
448
    regs->pc = infop->entry;
 
449
}
 
450
 
 
451
#define USE_ELF_CORE_DUMP
 
452
#define ELF_EXEC_PAGESIZE       8192
 
453
 
 
454
#endif
 
455
 
 
456
#ifdef TARGET_ALPHA
 
457
 
 
458
#define ELF_START_MMAP (0x30000000000ULL)
 
459
 
 
460
#define elf_check_arch(x) ( (x) == ELF_ARCH )
 
461
 
 
462
#define ELF_CLASS      ELFCLASS64
 
463
#define ELF_DATA       ELFDATA2MSB
 
464
#define ELF_ARCH       EM_ALPHA
 
465
 
 
466
static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
467
{
 
468
    regs->pc = infop->entry;
 
469
    regs->ps = 8;
 
470
    regs->usp = infop->start_stack;
 
471
    regs->unique = infop->start_data; /* ? */
 
472
    printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
 
473
           regs->unique, infop->start_data);
 
474
}
 
475
 
 
476
#define USE_ELF_CORE_DUMP
 
477
#define ELF_EXEC_PAGESIZE        8192
 
478
 
 
479
#endif /* TARGET_ALPHA */
 
480
 
 
481
#ifndef ELF_PLATFORM
 
482
#define ELF_PLATFORM (NULL)
 
483
#endif
 
484
 
 
485
#ifndef ELF_HWCAP
 
486
#define ELF_HWCAP 0
 
487
#endif
 
488
 
 
489
#ifdef TARGET_ABI32
 
490
#undef ELF_CLASS
 
491
#define ELF_CLASS ELFCLASS32
 
492
#undef bswaptls
 
493
#define bswaptls(ptr) bswap32s(ptr)
 
494
#endif
 
495
 
183
496
#include "elf.h"
184
497
 
185
 
/*
186
 
 * MAX_ARG_PAGES defines the number of pages allocated for arguments
187
 
 * and envelope for the new program. 32 should suffice, this gives
188
 
 * a maximum env+arg of 128kB w/4KB pages!
189
 
 */
190
 
#define MAX_ARG_PAGES 32
191
 
 
192
 
/*
193
 
 * This structure is used to hold the arguments that are 
194
 
 * used when loading binaries.
195
 
 */
196
 
struct linux_binprm {
197
 
        char buf[128];
198
 
        unsigned long page[MAX_ARG_PAGES];
199
 
        unsigned long p;
200
 
        int sh_bang;
201
 
        int fd;
202
 
        int e_uid, e_gid;
203
 
        int argc, envc;
204
 
        char * filename;        /* Name of binary */
205
 
        unsigned long loader, exec;
206
 
        int dont_iput;          /* binfmt handler has put inode */
207
 
};
208
 
 
209
498
struct exec
210
499
{
211
500
  unsigned int a_info;   /* Use macros N_MAGIC, etc for access */
231
520
/* max code+data+bss+brk space allocated to ET_DYN executables */
232
521
#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
233
522
 
234
 
/* from personality.h */
235
 
 
236
 
/* Flags for bug emulation. These occupy the top three bytes. */
237
 
#define STICKY_TIMEOUTS         0x4000000
238
 
#define WHOLE_SECONDS           0x2000000
239
 
 
240
 
/* Personality types. These go in the low byte. Avoid using the top bit,
241
 
 * it will conflict with error returns.
242
 
 */
243
 
#define PER_MASK                (0x00ff)
244
 
#define PER_LINUX               (0x0000)
245
 
#define PER_SVR4                (0x0001 | STICKY_TIMEOUTS)
246
 
#define PER_SVR3                (0x0002 | STICKY_TIMEOUTS)
247
 
#define PER_SCOSVR3             (0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS)
248
 
#define PER_WYSEV386            (0x0004 | STICKY_TIMEOUTS)
249
 
#define PER_ISCR4               (0x0005 | STICKY_TIMEOUTS)
250
 
#define PER_BSD                 (0x0006)
251
 
#define PER_XENIX               (0x0007 | STICKY_TIMEOUTS)
252
 
 
253
523
/* Necessary parameters */
254
 
#define NGROUPS 32
255
 
 
256
524
#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
257
525
#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
258
526
#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
261
529
#define INTERPRETER_AOUT 1
262
530
#define INTERPRETER_ELF 2
263
531
 
264
 
#define DLINFO_ITEMS 11
 
532
#define DLINFO_ITEMS 12
265
533
 
266
534
static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
267
535
{
273
541
static int load_aout_interp(void * exptr, int interp_fd);
274
542
 
275
543
#ifdef BSWAP_NEEDED
276
 
static void bswap_ehdr(Elf32_Ehdr *ehdr)
 
544
static void bswap_ehdr(struct elfhdr *ehdr)
277
545
{
278
546
    bswap16s(&ehdr->e_type);                    /* Object file type */
279
547
    bswap16s(&ehdr->e_machine);         /* Architecture */
280
548
    bswap32s(&ehdr->e_version);         /* Object file version */
281
 
    bswap32s(&ehdr->e_entry);           /* Entry point virtual address */
282
 
    bswap32s(&ehdr->e_phoff);           /* Program header table file offset */
283
 
    bswap32s(&ehdr->e_shoff);           /* Section header table file offset */
 
549
    bswaptls(&ehdr->e_entry);           /* Entry point virtual address */
 
550
    bswaptls(&ehdr->e_phoff);           /* Program header table file offset */
 
551
    bswaptls(&ehdr->e_shoff);           /* Section header table file offset */
284
552
    bswap32s(&ehdr->e_flags);           /* Processor-specific flags */
285
553
    bswap16s(&ehdr->e_ehsize);          /* ELF header size in bytes */
286
554
    bswap16s(&ehdr->e_phentsize);               /* Program header table entry size */
290
558
    bswap16s(&ehdr->e_shstrndx);                /* Section header string table index */
291
559
}
292
560
 
293
 
static void bswap_phdr(Elf32_Phdr *phdr)
 
561
static void bswap_phdr(struct elf_phdr *phdr)
294
562
{
295
563
    bswap32s(&phdr->p_type);                    /* Segment type */
296
 
    bswap32s(&phdr->p_offset);          /* Segment file offset */
297
 
    bswap32s(&phdr->p_vaddr);           /* Segment virtual address */
298
 
    bswap32s(&phdr->p_paddr);           /* Segment physical address */
299
 
    bswap32s(&phdr->p_filesz);          /* Segment size in file */
300
 
    bswap32s(&phdr->p_memsz);           /* Segment size in memory */
 
564
    bswaptls(&phdr->p_offset);          /* Segment file offset */
 
565
    bswaptls(&phdr->p_vaddr);           /* Segment virtual address */
 
566
    bswaptls(&phdr->p_paddr);           /* Segment physical address */
 
567
    bswaptls(&phdr->p_filesz);          /* Segment size in file */
 
568
    bswaptls(&phdr->p_memsz);           /* Segment size in memory */
301
569
    bswap32s(&phdr->p_flags);           /* Segment flags */
302
 
    bswap32s(&phdr->p_align);           /* Segment alignment */
 
570
    bswaptls(&phdr->p_align);           /* Segment alignment */
303
571
}
304
572
 
305
 
static void bswap_shdr(Elf32_Shdr *shdr)
 
573
static void bswap_shdr(struct elf_shdr *shdr)
306
574
{
307
575
    bswap32s(&shdr->sh_name);
308
576
    bswap32s(&shdr->sh_type);
309
 
    bswap32s(&shdr->sh_flags);
310
 
    bswap32s(&shdr->sh_addr);
311
 
    bswap32s(&shdr->sh_offset);
312
 
    bswap32s(&shdr->sh_size);
 
577
    bswaptls(&shdr->sh_flags);
 
578
    bswaptls(&shdr->sh_addr);
 
579
    bswaptls(&shdr->sh_offset);
 
580
    bswaptls(&shdr->sh_size);
313
581
    bswap32s(&shdr->sh_link);
314
582
    bswap32s(&shdr->sh_info);
315
 
    bswap32s(&shdr->sh_addralign);
316
 
    bswap32s(&shdr->sh_entsize);
 
583
    bswaptls(&shdr->sh_addralign);
 
584
    bswaptls(&shdr->sh_entsize);
317
585
}
318
586
 
319
 
static void bswap_sym(Elf32_Sym *sym)
 
587
static void bswap_sym(struct elf_sym *sym)
320
588
{
321
589
    bswap32s(&sym->st_name);
322
 
    bswap32s(&sym->st_value);
323
 
    bswap32s(&sym->st_size);
 
590
    bswaptls(&sym->st_value);
 
591
    bswaptls(&sym->st_size);
324
592
    bswap16s(&sym->st_shndx);
325
593
}
326
594
#endif
327
595
 
328
 
static void * get_free_page(void)
329
 
{
330
 
    void *      retval;
331
 
 
332
 
    /* User-space version of kernel get_free_page.  Returns a page-aligned
333
 
     * page-sized chunk of memory.
334
 
     */
335
 
    retval = (void *)target_mmap(0, host_page_size, PROT_READ|PROT_WRITE, 
336
 
                                 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
337
 
 
338
 
    if((long)retval == -1) {
339
 
        perror("get_free_page");
340
 
        exit(-1);
341
 
    }
342
 
    else {
343
 
        return(retval);
344
 
    }
345
 
}
346
 
 
347
 
static void free_page(void * pageaddr)
348
 
{
349
 
    target_munmap((unsigned long)pageaddr, host_page_size);
350
 
}
351
 
 
352
596
/*
353
 
 * 'copy_string()' copies argument/envelope strings from user
 
597
 * 'copy_elf_strings()' copies argument/envelope strings from user
354
598
 * memory to free pages in kernel mem. These are in a format ready
355
599
 * to be put directly into the top of new user memory.
356
600
 *
357
601
 */
358
 
static unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
359
 
                unsigned long p)
 
602
static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
 
603
                                  abi_ulong p)
360
604
{
361
605
    char *tmp, *tmp1, *pag = NULL;
362
606
    int len, offset = 0;
380
624
            --p; --tmp; --len;
381
625
            if (--offset < 0) {
382
626
                offset = p % TARGET_PAGE_SIZE;
383
 
                pag = (char *) page[p/TARGET_PAGE_SIZE];
 
627
                pag = (char *)page[p/TARGET_PAGE_SIZE];
384
628
                if (!pag) {
385
 
                    pag = (char *)get_free_page();
386
 
                    page[p/TARGET_PAGE_SIZE] = (unsigned long)pag;
 
629
                    pag = (char *)malloc(TARGET_PAGE_SIZE);
 
630
                    memset(pag, 0, TARGET_PAGE_SIZE);
 
631
                    page[p/TARGET_PAGE_SIZE] = pag;
387
632
                    if (!pag)
388
633
                        return 0;
389
634
                }
404
649
    return p;
405
650
}
406
651
 
407
 
static int in_group_p(gid_t g)
408
 
{
409
 
    /* return TRUE if we're in the specified group, FALSE otherwise */
410
 
    int         ngroup;
411
 
    int         i;
412
 
    gid_t       grouplist[NGROUPS];
413
 
 
414
 
    ngroup = getgroups(NGROUPS, grouplist);
415
 
    for(i = 0; i < ngroup; i++) {
416
 
        if(grouplist[i] == g) {
417
 
            return 1;
418
 
        }
419
 
    }
420
 
    return 0;
421
 
}
422
 
 
423
 
static int count(char ** vec)
424
 
{
425
 
    int         i;
426
 
 
427
 
    for(i = 0; *vec; i++) {
428
 
        vec++;
429
 
    }
430
 
 
431
 
    return(i);
432
 
}
433
 
 
434
 
static int prepare_binprm(struct linux_binprm *bprm)
435
 
{
436
 
    struct stat         st;
437
 
    int mode;
438
 
    int retval, id_change;
439
 
 
440
 
    if(fstat(bprm->fd, &st) < 0) {
441
 
        return(-errno);
442
 
    }
443
 
 
444
 
    mode = st.st_mode;
445
 
    if(!S_ISREG(mode)) {        /* Must be regular file */
446
 
        return(-EACCES);
447
 
    }
448
 
    if(!(mode & 0111)) {        /* Must have at least one execute bit set */
449
 
        return(-EACCES);
450
 
    }
451
 
 
452
 
    bprm->e_uid = geteuid();
453
 
    bprm->e_gid = getegid();
454
 
    id_change = 0;
455
 
 
456
 
    /* Set-uid? */
457
 
    if(mode & S_ISUID) {
458
 
        bprm->e_uid = st.st_uid;
459
 
        if(bprm->e_uid != geteuid()) {
460
 
            id_change = 1;
461
 
        }
462
 
    }
463
 
 
464
 
    /* Set-gid? */
465
 
    /*
466
 
     * If setgid is set but no group execute bit then this
467
 
     * is a candidate for mandatory locking, not a setgid
468
 
     * executable.
469
 
     */
470
 
    if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
471
 
        bprm->e_gid = st.st_gid;
472
 
        if (!in_group_p(bprm->e_gid)) {
473
 
                id_change = 1;
474
 
        }
475
 
    }
476
 
 
477
 
    memset(bprm->buf, 0, sizeof(bprm->buf));
478
 
    retval = lseek(bprm->fd, 0L, SEEK_SET);
479
 
    if(retval >= 0) {
480
 
        retval = read(bprm->fd, bprm->buf, 128);
481
 
    }
482
 
    if(retval < 0) {
483
 
        perror("prepare_binprm");
484
 
        exit(-1);
485
 
        /* return(-errno); */
486
 
    }
487
 
    else {
488
 
        return(retval);
489
 
    }
490
 
}
491
 
 
492
 
unsigned long setup_arg_pages(unsigned long p, struct linux_binprm * bprm,
493
 
                                                struct image_info * info)
494
 
{
495
 
    unsigned long stack_base, size, error;
 
652
static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
 
653
                                 struct image_info *info)
 
654
{
 
655
    abi_ulong stack_base, size, error;
496
656
    int i;
497
657
 
498
658
    /* Create enough stack to hold everything.  If we don't use
501
661
    size = x86_stack_size;
502
662
    if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
503
663
        size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
504
 
    error = target_mmap(0, 
505
 
                        size + host_page_size,
 
664
    error = target_mmap(0,
 
665
                        size + qemu_host_page_size,
506
666
                        PROT_READ | PROT_WRITE,
507
667
                        MAP_PRIVATE | MAP_ANONYMOUS,
508
668
                        -1, 0);
511
671
        exit(-1);
512
672
    }
513
673
    /* we reserve one extra page at the top of the stack as guard */
514
 
    target_mprotect(error + size, host_page_size, PROT_NONE);
 
674
    target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
515
675
 
516
676
    stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
517
677
    p += stack_base;
518
678
 
519
 
    if (bprm->loader) {
520
 
        bprm->loader += stack_base;
521
 
    }
522
 
    bprm->exec += stack_base;
523
 
 
524
679
    for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
525
680
        if (bprm->page[i]) {
526
681
            info->rss++;
527
 
 
528
 
            memcpy((void *)stack_base, (void *)bprm->page[i], TARGET_PAGE_SIZE);
529
 
            free_page((void *)bprm->page[i]);
 
682
            /* FIXME - check return value of memcpy_to_target() for failure */
 
683
            memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
 
684
            free(bprm->page[i]);
530
685
        }
531
 
        stack_base += TARGET_PAGE_SIZE;
 
686
        stack_base += TARGET_PAGE_SIZE;
532
687
    }
533
688
    return p;
534
689
}
535
690
 
536
 
static void set_brk(unsigned long start, unsigned long end)
 
691
static void set_brk(abi_ulong start, abi_ulong end)
537
692
{
538
693
        /* page-align the start and end addresses... */
539
694
        start = HOST_PAGE_ALIGN(start);
552
707
/* We need to explicitly zero any fractional pages after the data
553
708
   section (i.e. bss).  This would contain the junk from the file that
554
709
   should not be in memory. */
555
 
static void padzero(unsigned long elf_bss)
 
710
static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
556
711
{
557
 
        unsigned long nbyte;
558
 
        char * fpnt;
 
712
        abi_ulong nbyte;
 
713
 
 
714
        if (elf_bss >= last_bss)
 
715
                return;
559
716
 
560
717
        /* XXX: this is really a hack : if the real host page size is
561
718
           smaller than the target page size, some pages after the end
562
719
           of the file may not be mapped. A better fix would be to
563
720
           patch target_mmap(), but it is more complicated as the file
564
721
           size must be known */
565
 
        if (real_host_page_size < host_page_size) {
566
 
            unsigned long end_addr, end_addr1;
567
 
            end_addr1 = (elf_bss + real_host_page_size - 1) & 
568
 
                ~(real_host_page_size - 1);
 
722
        if (qemu_real_host_page_size < qemu_host_page_size) {
 
723
            abi_ulong end_addr, end_addr1;
 
724
            end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
 
725
                ~(qemu_real_host_page_size - 1);
569
726
            end_addr = HOST_PAGE_ALIGN(elf_bss);
570
727
            if (end_addr1 < end_addr) {
571
 
                mmap((void *)end_addr1, end_addr - end_addr1,
 
728
                mmap((void *)g2h(end_addr1), end_addr - end_addr1,
572
729
                     PROT_READ|PROT_WRITE|PROT_EXEC,
573
730
                     MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
574
731
            }
575
732
        }
576
733
 
577
 
        nbyte = elf_bss & (host_page_size-1);
 
734
        nbyte = elf_bss & (qemu_host_page_size-1);
578
735
        if (nbyte) {
579
 
            nbyte = host_page_size - nbyte;
580
 
            fpnt = (char *) elf_bss;
 
736
            nbyte = qemu_host_page_size - nbyte;
581
737
            do {
582
 
                *fpnt++ = 0;
 
738
                /* FIXME - what to do if put_user() fails? */
 
739
                put_user_u8(0, elf_bss);
 
740
                elf_bss++;
583
741
            } while (--nbyte);
584
742
        }
585
743
}
586
744
 
587
 
static unsigned int * create_elf_tables(char *p, int argc, int envc,
588
 
                                        struct elfhdr * exec,
589
 
                                        unsigned long load_addr,
590
 
                                        unsigned long load_bias,
591
 
                                        unsigned long interp_load_addr, int ibcs,
592
 
                                        struct image_info *info)
 
745
 
 
746
static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
 
747
                                   struct elfhdr * exec,
 
748
                                   abi_ulong load_addr,
 
749
                                   abi_ulong load_bias,
 
750
                                   abi_ulong interp_load_addr, int ibcs,
 
751
                                   struct image_info *info)
593
752
{
594
 
        target_ulong *argv, *envp;
595
 
        target_ulong *sp, *csp;
596
 
        int v;
 
753
        abi_ulong sp;
 
754
        int size;
 
755
        abi_ulong u_platform;
 
756
        const char *k_platform;
 
757
        const int n = sizeof(elf_addr_t);
597
758
 
 
759
        sp = p;
 
760
        u_platform = 0;
 
761
        k_platform = ELF_PLATFORM;
 
762
        if (k_platform) {
 
763
            size_t len = strlen(k_platform) + 1;
 
764
            sp -= (len + n - 1) & ~(n - 1);
 
765
            u_platform = sp;
 
766
            /* FIXME - check return value of memcpy_to_target() for failure */
 
767
            memcpy_to_target(sp, k_platform, len);
 
768
        }
598
769
        /*
599
770
         * Force 16 byte _final_ alignment here for generality.
600
771
         */
601
 
        sp = (unsigned int *) (~15UL & (unsigned long) p);
602
 
        csp = sp;
603
 
        csp -= (DLINFO_ITEMS + 1) * 2;
 
772
        sp = sp &~ (abi_ulong)15;
 
773
        size = (DLINFO_ITEMS + 1) * 2;
 
774
        if (k_platform)
 
775
          size += 2;
604
776
#ifdef DLINFO_ARCH_ITEMS
605
 
        csp -= DLINFO_ARCH_ITEMS*2;
 
777
        size += DLINFO_ARCH_ITEMS * 2;
606
778
#endif
607
 
        csp -= envc+1;
608
 
        csp -= argc+1;
609
 
        csp -= (!ibcs ? 3 : 1); /* argc itself */
610
 
        if ((unsigned long)csp & 15UL)
611
 
            sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
612
 
        
613
 
#define NEW_AUX_ENT(nr, id, val) \
614
 
          put_user (id, sp + (nr * 2)); \
615
 
          put_user (val, sp + (nr * 2 + 1))
616
 
        sp -= 2;
617
 
        NEW_AUX_ENT (0, AT_NULL, 0);
618
 
 
619
 
        sp -= DLINFO_ITEMS*2;
620
 
        NEW_AUX_ENT( 0, AT_PHDR, (target_ulong)(load_addr + exec->e_phoff));
621
 
        NEW_AUX_ENT( 1, AT_PHENT, (target_ulong)(sizeof (struct elf_phdr)));
622
 
        NEW_AUX_ENT( 2, AT_PHNUM, (target_ulong)(exec->e_phnum));
623
 
        NEW_AUX_ENT( 3, AT_PAGESZ, (target_ulong)(TARGET_PAGE_SIZE));
624
 
        NEW_AUX_ENT( 4, AT_BASE, (target_ulong)(interp_load_addr));
625
 
        NEW_AUX_ENT( 5, AT_FLAGS, (target_ulong)0);
626
 
        NEW_AUX_ENT( 6, AT_ENTRY, load_bias + exec->e_entry);
627
 
        NEW_AUX_ENT( 7, AT_UID, (target_ulong) getuid());
628
 
        NEW_AUX_ENT( 8, AT_EUID, (target_ulong) geteuid());
629
 
        NEW_AUX_ENT( 9, AT_GID, (target_ulong) getgid());
630
 
        NEW_AUX_ENT(11, AT_EGID, (target_ulong) getegid());
 
779
        size += envc + argc + 2;
 
780
        size += (!ibcs ? 3 : 1);        /* argc itself */
 
781
        size *= n;
 
782
        if (size & 15)
 
783
            sp -= 16 - (size & 15);
 
784
 
 
785
        /* This is correct because Linux defines
 
786
         * elf_addr_t as Elf32_Off / Elf64_Off
 
787
         */
 
788
#define NEW_AUX_ENT(id, val) do {               \
 
789
            sp -= n; put_user_ual(val, sp);     \
 
790
            sp -= n; put_user_ual(id, sp);      \
 
791
          } while(0)
 
792
 
 
793
        NEW_AUX_ENT (AT_NULL, 0);
 
794
 
 
795
        /* There must be exactly DLINFO_ITEMS entries here.  */
 
796
        NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
 
797
        NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
 
798
        NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
 
799
        NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
 
800
        NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
 
801
        NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
 
802
        NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
 
803
        NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
 
804
        NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
 
805
        NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
 
806
        NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
 
807
        NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
 
808
        if (k_platform)
 
809
            NEW_AUX_ENT(AT_PLATFORM, u_platform);
631
810
#ifdef ARCH_DLINFO
632
 
        /* 
 
811
        /*
633
812
         * ARCH_DLINFO must come last so platform specific code can enforce
634
813
         * special alignment requirements on the AUXV if necessary (eg. PPC).
635
814
         */
637
816
#endif
638
817
#undef NEW_AUX_ENT
639
818
 
640
 
        sp -= envc+1;
641
 
        envp = sp;
642
 
        sp -= argc+1;
643
 
        argv = sp;
644
 
        if (!ibcs) {
645
 
                put_user((target_ulong)envp,--sp);
646
 
                put_user((target_ulong)argv,--sp);
647
 
        }
648
 
        put_user(argc,--sp);
649
 
        info->arg_start = (unsigned int)((unsigned long)p & 0xffffffff);
650
 
        while (argc-->0) {
651
 
                put_user((target_ulong)p,argv++);
652
 
                do {
653
 
                    get_user(v, p);
654
 
                    p++;
655
 
                } while (v != 0);
656
 
        }
657
 
        put_user(0,argv);
658
 
        info->arg_end = info->env_start = (unsigned int)((unsigned long)p & 0xffffffff);
659
 
        while (envc-->0) {
660
 
                put_user((target_ulong)p,envp++);
661
 
                do {
662
 
                    get_user(v, p);
663
 
                    p++;
664
 
                } while (v != 0);
665
 
        }
666
 
        put_user(0,envp);
667
 
        info->env_end = (unsigned int)((unsigned long)p & 0xffffffff);
 
819
        sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
668
820
        return sp;
669
821
}
670
822
 
671
823
 
672
 
 
673
 
static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
674
 
                                     int interpreter_fd,
675
 
                                     unsigned long *interp_load_addr)
 
824
static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
 
825
                                 int interpreter_fd,
 
826
                                 abi_ulong *interp_load_addr)
676
827
{
677
828
        struct elf_phdr *elf_phdata  =  NULL;
678
829
        struct elf_phdr *eppnt;
679
 
        unsigned long load_addr = 0;
 
830
        abi_ulong load_addr = 0;
680
831
        int load_addr_set = 0;
681
832
        int retval;
682
 
        unsigned long last_bss, elf_bss;
683
 
        unsigned long error;
 
833
        abi_ulong last_bss, elf_bss;
 
834
        abi_ulong error;
684
835
        int i;
685
 
        
 
836
 
686
837
        elf_bss = 0;
687
838
        last_bss = 0;
688
839
        error = 0;
691
842
        bswap_ehdr(interp_elf_ex);
692
843
#endif
693
844
        /* First of all, some simple consistency checks */
694
 
        if ((interp_elf_ex->e_type != ET_EXEC && 
695
 
             interp_elf_ex->e_type != ET_DYN) || 
 
845
        if ((interp_elf_ex->e_type != ET_EXEC &&
 
846
             interp_elf_ex->e_type != ET_DYN) ||
696
847
           !elf_check_arch(interp_elf_ex->e_machine)) {
697
 
                return ~0UL;
 
848
                return ~((abi_ulong)0UL);
698
849
        }
699
 
        
 
850
 
700
851
 
701
852
        /* Now read in all of the header information */
702
 
        
 
853
 
703
854
        if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
704
 
            return ~0UL;
705
 
        
706
 
        elf_phdata =  (struct elf_phdr *) 
 
855
            return ~(abi_ulong)0UL;
 
856
 
 
857
        elf_phdata =  (struct elf_phdr *)
707
858
                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
708
859
 
709
860
        if (!elf_phdata)
710
 
          return ~0UL;
711
 
        
 
861
          return ~((abi_ulong)0UL);
 
862
 
712
863
        /*
713
864
         * If the size of this structure has changed, then punt, since
714
865
         * we will be doing the wrong thing.
715
866
         */
716
867
        if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
717
868
            free(elf_phdata);
718
 
            return ~0UL;
 
869
            return ~((abi_ulong)0UL);
719
870
        }
720
871
 
721
872
        retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
738
889
#endif
739
890
 
740
891
        if (interp_elf_ex->e_type == ET_DYN) {
741
 
            /* in order to avoid harcoding the interpreter load
 
892
            /* in order to avoid hardcoding the interpreter load
742
893
               address in qemu, we allocate a big enough memory zone */
743
894
            error = target_mmap(0, INTERP_MAP_SIZE,
744
 
                                PROT_NONE, MAP_PRIVATE | MAP_ANON, 
 
895
                                PROT_NONE, MAP_PRIVATE | MAP_ANON,
745
896
                                -1, 0);
746
897
            if (error == -1) {
747
898
                perror("mmap");
756
907
          if (eppnt->p_type == PT_LOAD) {
757
908
            int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
758
909
            int elf_prot = 0;
759
 
            unsigned long vaddr = 0;
760
 
            unsigned long k;
 
910
            abi_ulong vaddr = 0;
 
911
            abi_ulong k;
761
912
 
762
913
            if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
763
914
            if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
772
923
                 elf_type,
773
924
                 interpreter_fd,
774
925
                 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
775
 
            
776
 
            if (error > -1024UL) {
 
926
 
 
927
            if (error == -1) {
777
928
              /* Real error */
778
929
              close(interpreter_fd);
779
930
              free(elf_phdata);
780
 
              return ~0UL;
 
931
              return ~((abi_ulong)0UL);
781
932
            }
782
933
 
783
934
            if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
799
950
            k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
800
951
            if (k > last_bss) last_bss = k;
801
952
          }
802
 
        
 
953
 
803
954
        /* Now use mmap to map the library into memory. */
804
955
 
805
956
        close(interpreter_fd);
810
961
         * that there are zeromapped pages up to and including the last
811
962
         * bss page.
812
963
         */
813
 
        padzero(elf_bss);
814
 
        elf_bss = TARGET_ELF_PAGESTART(elf_bss + host_page_size - 1); /* What we have mapped so far */
 
964
        padzero(elf_bss, last_bss);
 
965
        elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
815
966
 
816
967
        /* Map the last of the bss segment */
817
968
        if (last_bss > elf_bss) {
822
973
        free(elf_phdata);
823
974
 
824
975
        *interp_load_addr = load_addr;
825
 
        return ((unsigned long) interp_elf_ex->e_entry) + load_addr;
 
976
        return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
826
977
}
827
978
 
828
979
/* Best attempt to load symbols from this ELF object. */
831
982
    unsigned int i;
832
983
    struct elf_shdr sechdr, symtab, strtab;
833
984
    char *strings;
 
985
    struct syminfo *s;
 
986
#if (ELF_CLASS == ELFCLASS64)
 
987
    // Disas uses 32 bit symbols
 
988
    struct elf32_sym *syms32 = NULL;
 
989
    struct elf_sym *sym;
 
990
#endif
834
991
 
835
992
    lseek(fd, hdr->e_shoff, SEEK_SET);
836
993
    for (i = 0; i < hdr->e_shnum; i++) {
856
1013
 
857
1014
 found:
858
1015
    /* Now know where the strtab and symtab are.  Snarf them. */
859
 
    disas_symtab = malloc(symtab.sh_size);
860
 
    disas_strtab = strings = malloc(strtab.sh_size);
861
 
    if (!disas_symtab || !disas_strtab)
 
1016
    s = malloc(sizeof(*s));
 
1017
    s->disas_symtab = malloc(symtab.sh_size);
 
1018
#if (ELF_CLASS == ELFCLASS64)
 
1019
    syms32 = malloc(symtab.sh_size / sizeof(struct elf_sym)
 
1020
                    * sizeof(struct elf32_sym));
 
1021
#endif
 
1022
    s->disas_strtab = strings = malloc(strtab.sh_size);
 
1023
    if (!s->disas_symtab || !s->disas_strtab)
862
1024
        return;
863
 
        
 
1025
 
864
1026
    lseek(fd, symtab.sh_offset, SEEK_SET);
865
 
    if (read(fd, disas_symtab, symtab.sh_size) != symtab.sh_size)
 
1027
    if (read(fd, s->disas_symtab, symtab.sh_size) != symtab.sh_size)
866
1028
        return;
867
1029
 
 
1030
    for (i = 0; i < symtab.sh_size / sizeof(struct elf_sym); i++) {
868
1031
#ifdef BSWAP_NEEDED
869
 
    for (i = 0; i < symtab.sh_size / sizeof(struct elf_sym); i++)
870
 
        bswap_sym(disas_symtab + sizeof(struct elf_sym)*i);
871
 
#endif
 
1032
        bswap_sym(s->disas_symtab + sizeof(struct elf_sym)*i);
 
1033
#endif
 
1034
#if (ELF_CLASS == ELFCLASS64)
 
1035
        sym = s->disas_symtab + sizeof(struct elf_sym)*i;
 
1036
        syms32[i].st_name = sym->st_name;
 
1037
        syms32[i].st_info = sym->st_info;
 
1038
        syms32[i].st_other = sym->st_other;
 
1039
        syms32[i].st_shndx = sym->st_shndx;
 
1040
        syms32[i].st_value = sym->st_value & 0xffffffff;
 
1041
        syms32[i].st_size = sym->st_size & 0xffffffff;
 
1042
#endif
 
1043
    }
872
1044
 
 
1045
#if (ELF_CLASS == ELFCLASS64)
 
1046
    free(s->disas_symtab);
 
1047
    s->disas_symtab = syms32;
 
1048
#endif
873
1049
    lseek(fd, strtab.sh_offset, SEEK_SET);
874
1050
    if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
875
1051
        return;
876
 
    disas_num_syms = symtab.sh_size / sizeof(struct elf_sym);
 
1052
    s->disas_num_syms = symtab.sh_size / sizeof(struct elf_sym);
 
1053
    s->next = syminfos;
 
1054
    syminfos = s;
877
1055
}
878
1056
 
879
 
static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
880
 
                           struct image_info * info)
 
1057
int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
 
1058
                    struct image_info * info)
881
1059
{
882
1060
    struct elfhdr elf_ex;
883
1061
    struct elfhdr interp_elf_ex;
884
1062
    struct exec interp_ex;
885
1063
    int interpreter_fd = -1; /* avoid warning */
886
 
    unsigned long load_addr, load_bias;
 
1064
    abi_ulong load_addr, load_bias;
887
1065
    int load_addr_set = 0;
888
1066
    unsigned int interpreter_type = INTERPRETER_NONE;
889
1067
    unsigned char ibcs2_interpreter;
890
1068
    int i;
891
 
    unsigned long mapped_addr;
 
1069
    abi_ulong mapped_addr;
892
1070
    struct elf_phdr * elf_ppnt;
893
1071
    struct elf_phdr *elf_phdata;
894
 
    unsigned long elf_bss, k, elf_brk;
 
1072
    abi_ulong elf_bss, k, elf_brk;
895
1073
    int retval;
896
1074
    char * elf_interpreter;
897
 
    unsigned long elf_entry, interp_load_addr = 0;
 
1075
    abi_ulong elf_entry, interp_load_addr = 0;
898
1076
    int status;
899
 
    unsigned long start_code, end_code, end_data;
900
 
    unsigned long elf_stack;
 
1077
    abi_ulong start_code, end_code, start_data, end_data;
 
1078
    abi_ulong reloc_func_desc = 0;
 
1079
    abi_ulong elf_stack;
901
1080
    char passed_fileno[6];
902
1081
 
903
1082
    ibcs2_interpreter = 0;
909
1088
    bswap_ehdr(&elf_ex);
910
1089
#endif
911
1090
 
912
 
    if (elf_ex.e_ident[0] != 0x7f ||
913
 
        strncmp(&elf_ex.e_ident[1], "ELF",3) != 0) {
914
 
            return  -ENOEXEC;
915
 
    }
916
 
 
917
1091
    /* First of all, some simple consistency checks */
918
1092
    if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
919
1093
                                (! elf_check_arch(elf_ex.e_machine))) {
920
1094
            return -ENOEXEC;
921
1095
    }
922
1096
 
 
1097
    bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
 
1098
    bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
 
1099
    bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
 
1100
    if (!bprm->p) {
 
1101
        retval = -E2BIG;
 
1102
    }
 
1103
 
923
1104
    /* Now read in all of the header information */
924
1105
    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
925
1106
    if (elf_phdata == NULL) {
928
1109
 
929
1110
    retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
930
1111
    if(retval > 0) {
931
 
        retval = read(bprm->fd, (char *) elf_phdata, 
 
1112
        retval = read(bprm->fd, (char *) elf_phdata,
932
1113
                                elf_ex.e_phentsize * elf_ex.e_phnum);
933
1114
    }
934
1115
 
951
1132
    elf_brk = 0;
952
1133
 
953
1134
 
954
 
    elf_stack = ~0UL;
 
1135
    elf_stack = ~((abi_ulong)0UL);
955
1136
    elf_interpreter = NULL;
956
 
    start_code = ~0UL;
 
1137
    start_code = ~((abi_ulong)0UL);
957
1138
    end_code = 0;
 
1139
    start_data = 0;
958
1140
    end_data = 0;
959
1141
 
960
1142
    for(i=0;i < elf_ex.e_phnum; i++) {
987
1169
            if(retval < 0) {
988
1170
                perror("load_elf_binary2");
989
1171
                exit(-1);
990
 
            }   
 
1172
            }
991
1173
 
992
1174
            /* If the program interpreter is one of these two,
993
1175
               then assume an iBCS2 image. Otherwise assume
1063
1245
    /* OK, we are done with that, now set up the arg stuff,
1064
1246
       and then start this sucker up */
1065
1247
 
1066
 
    if (!bprm->sh_bang) {
 
1248
    {
1067
1249
        char * passed_p;
1068
1250
 
1069
1251
        if (interpreter_type == INTERPRETER_AOUT) {
1070
 
            sprintf(passed_fileno, "%d", bprm->fd);
 
1252
            snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
1071
1253
            passed_p = passed_fileno;
1072
1254
 
1073
1255
            if (elf_interpreter) {
1074
 
                bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p);
 
1256
                bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
1075
1257
                bprm->argc++;
1076
1258
            }
1077
1259
        }
1088
1270
    /* OK, This is the point of no return */
1089
1271
    info->end_data = 0;
1090
1272
    info->end_code = 0;
1091
 
    info->start_mmap = (unsigned long)ELF_START_MMAP;
 
1273
    info->start_mmap = (abi_ulong)ELF_START_MMAP;
1092
1274
    info->mmap = 0;
1093
 
    elf_entry = (unsigned long) elf_ex.e_entry;
 
1275
    elf_entry = (abi_ulong) elf_ex.e_entry;
1094
1276
 
1095
1277
    /* Do this so that we can load the interpreter, if need be.  We will
1096
1278
       change some of these later */
1107
1289
    for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
1108
1290
        int elf_prot = 0;
1109
1291
        int elf_flags = 0;
1110
 
        unsigned long error;
1111
 
        
 
1292
        abi_ulong error;
 
1293
 
1112
1294
        if (elf_ppnt->p_type != PT_LOAD)
1113
1295
            continue;
1114
 
        
 
1296
 
1115
1297
        if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1116
1298
        if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1117
1299
        if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1123
1305
               base, as well as whatever program they might try to exec.  This
1124
1306
               is because the brk will follow the loader, and is not movable.  */
1125
1307
            /* NOTE: for qemu, we do a big mmap to get enough space
1126
 
               without harcoding any address */
 
1308
               without hardcoding any address */
1127
1309
            error = target_mmap(0, ET_DYN_MAP_SIZE,
1128
 
                                PROT_NONE, MAP_PRIVATE | MAP_ANON, 
 
1310
                                PROT_NONE, MAP_PRIVATE | MAP_ANON,
1129
1311
                                -1, 0);
1130
1312
            if (error == -1) {
1131
1313
                perror("mmap");
1133
1315
            }
1134
1316
            load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
1135
1317
        }
1136
 
        
 
1318
 
1137
1319
        error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1138
1320
                            (elf_ppnt->p_filesz +
1139
1321
                             TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1140
1322
                            elf_prot,
1141
1323
                            (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1142
1324
                            bprm->fd,
1143
 
                            (elf_ppnt->p_offset - 
 
1325
                            (elf_ppnt->p_offset -
1144
1326
                             TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
1145
1327
        if (error == -1) {
1146
1328
            perror("mmap");
1151
1333
        if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1152
1334
            elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
1153
1335
#endif
1154
 
        
 
1336
 
1155
1337
        if (!load_addr_set) {
1156
1338
            load_addr_set = 1;
1157
1339
            load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1159
1341
                load_bias += error -
1160
1342
                    TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
1161
1343
                load_addr += load_bias;
 
1344
                reloc_func_desc = load_bias;
1162
1345
            }
1163
1346
        }
1164
1347
        k = elf_ppnt->p_vaddr;
1165
 
        if (k < start_code) 
 
1348
        if (k < start_code)
1166
1349
            start_code = k;
 
1350
        if (start_data < k)
 
1351
            start_data = k;
1167
1352
        k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
1168
 
        if (k > elf_bss) 
 
1353
        if (k > elf_bss)
1169
1354
            elf_bss = k;
1170
1355
        if ((elf_ppnt->p_flags & PF_X) && end_code <  k)
1171
1356
            end_code = k;
1172
 
        if (end_data < k) 
 
1357
        if (end_data < k)
1173
1358
            end_data = k;
1174
1359
        k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1175
1360
        if (k > elf_brk) elf_brk = k;
1180
1365
    elf_brk += load_bias;
1181
1366
    start_code += load_bias;
1182
1367
    end_code += load_bias;
1183
 
    //    start_data += load_bias;
 
1368
    start_data += load_bias;
1184
1369
    end_data += load_bias;
1185
1370
 
1186
1371
    if (elf_interpreter) {
1191
1376
            elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1192
1377
                                            &interp_load_addr);
1193
1378
        }
 
1379
        reloc_func_desc = interp_load_addr;
1194
1380
 
1195
1381
        close(interpreter_fd);
1196
1382
        free(elf_interpreter);
1197
1383
 
1198
 
        if (elf_entry == ~0UL) {
 
1384
        if (elf_entry == ~((abi_ulong)0UL)) {
1199
1385
            printf("Unable to load interpreter\n");
1200
1386
            free(elf_phdata);
1201
1387
            exit(-1);
1214
1400
#ifdef LOW_ELF_STACK
1215
1401
    info->start_stack = bprm->p = elf_stack - 4;
1216
1402
#endif
1217
 
    bprm->p = (unsigned long)
1218
 
      create_elf_tables((char *)bprm->p,
 
1403
    bprm->p = create_elf_tables(bprm->p,
1219
1404
                    bprm->argc,
1220
1405
                    bprm->envc,
1221
1406
                    &elf_ex,
1223
1408
                    interp_load_addr,
1224
1409
                    (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1225
1410
                    info);
1226
 
    if (interpreter_type == INTERPRETER_AOUT)
1227
 
      info->arg_start += strlen(passed_fileno) + 1;
 
1411
    info->load_addr = reloc_func_desc;
1228
1412
    info->start_brk = info->brk = elf_brk;
1229
1413
    info->end_code = end_code;
1230
1414
    info->start_code = start_code;
 
1415
    info->start_data = start_data;
1231
1416
    info->end_data = end_data;
1232
1417
    info->start_stack = bprm->p;
1233
1418
 
1235
1420
       sections */
1236
1421
    set_brk(elf_bss, elf_brk);
1237
1422
 
1238
 
    padzero(elf_bss);
 
1423
    padzero(elf_bss, elf_brk);
1239
1424
 
1240
1425
#if 0
1241
1426
    printf("(start_brk) %x\n" , info->start_brk);
1252
1437
               and some applications "depend" upon this behavior.
1253
1438
               Since we do not have the power to recompile these, we
1254
1439
               emulate the SVr4 behavior.  Sigh.  */
1255
 
            mapped_addr = target_mmap(0, host_page_size, PROT_READ | PROT_EXEC,
 
1440
            mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
1256
1441
                                      MAP_FIXED | MAP_PRIVATE, -1, 0);
1257
1442
    }
1258
1443
 
1259
 
#ifdef ELF_PLAT_INIT
1260
 
    /*
1261
 
     * The ABI may specify that certain registers be set up in special
1262
 
     * ways (on i386 %edx is the address of a DT_FINI function, for
1263
 
     * example.  This macro performs whatever initialization to
1264
 
     * the regs structure is required.
1265
 
     */
1266
 
    ELF_PLAT_INIT(regs);
1267
 
#endif
1268
 
 
1269
 
 
1270
1444
    info->entry = elf_entry;
1271
1445
 
1272
1446
    return 0;
1273
1447
}
1274
1448
 
1275
 
 
1276
 
 
1277
 
int elf_exec(const char * filename, char ** argv, char ** envp, 
1278
 
             struct target_pt_regs * regs, struct image_info *infop)
1279
 
{
1280
 
        struct linux_binprm bprm;
1281
 
        int retval;
1282
 
        int i;
1283
 
 
1284
 
        bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
1285
 
        for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
1286
 
                bprm.page[i] = 0;
1287
 
        retval = open(filename, O_RDONLY);
1288
 
        if (retval < 0)
1289
 
            return retval;
1290
 
        bprm.fd = retval;
1291
 
        bprm.filename = (char *)filename;
1292
 
        bprm.sh_bang = 0;
1293
 
        bprm.loader = 0;
1294
 
        bprm.exec = 0;
1295
 
        bprm.dont_iput = 0;
1296
 
        bprm.argc = count(argv);
1297
 
        bprm.envc = count(envp);
1298
 
 
1299
 
        retval = prepare_binprm(&bprm);
1300
 
 
1301
 
        if(retval>=0) {
1302
 
            bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p);
1303
 
            bprm.exec = bprm.p;
1304
 
            bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p);
1305
 
            bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p);
1306
 
            if (!bprm.p) {
1307
 
                retval = -E2BIG;
1308
 
            }
1309
 
        }
1310
 
 
1311
 
        if(retval>=0) {
1312
 
            retval = load_elf_binary(&bprm,regs,infop);
1313
 
        }
1314
 
        if(retval>=0) {
1315
 
            /* success.  Initialize important registers */
1316
 
            init_thread(regs, infop);
1317
 
            return retval;
1318
 
        }
1319
 
 
1320
 
        /* Something went wrong, return the inode and free the argument pages*/
1321
 
        for (i=0 ; i<MAX_ARG_PAGES ; i++) {
1322
 
            free_page((void *)bprm.page[i]);
1323
 
        }
1324
 
        return(retval);
1325
 
}
1326
 
 
1327
 
 
1328
1449
static int load_aout_interp(void * exptr, int interp_fd)
1329
1450
{
1330
1451
    printf("a.out interpreter not yet supported\n");
1331
1452
    return(0);
1332
1453
}
1333
1454
 
 
1455
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
 
1456
{
 
1457
    init_thread(regs, infop);
 
1458
}