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