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