master
c 834 lines 24.1 KB
Raw
1 /*
2 * ELF loading code
3 *
4 * Copyright (c) 2013 Stacey D. Son
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21
22 #include "qemu.h"
23 #include "disas/disas.h"
24 #include "qemu/path.h"
25 #include "user/probe-guest-base.h"
26
27 static abi_ulong target_auxents; /* Where the AUX entries are in target */
28 static size_t target_auxents_sz; /* Size of AUX entries including AT_NULL */
29
30 #include "target_arch_reg.h"
31 #include "target_os_elf.h"
32 #include "target_os_stack.h"
33 #include "target_os_thread.h"
34 #include "target_os_user.h"
35
36 abi_ulong target_stksiz;
37 abi_ulong target_stkbas;
38
39 static int elf_core_dump(int signr, CPUArchState *env);
40 static int load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr,
41 int fd, abi_ulong rbase, abi_ulong *baddrp);
42
43 static inline void memcpy_fromfs(void *to, const void *from, unsigned long n)
44 {
45 memcpy(to, from, n);
46 }
47
48 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
49 static void bswap_ehdr(struct elfhdr *ehdr)
50 {
51 bswap16s(&ehdr->e_type); /* Object file type */
52 bswap16s(&ehdr->e_machine); /* Architecture */
53 bswap32s(&ehdr->e_version); /* Object file version */
54 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
55 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
56 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
57 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
58 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
59 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
60 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
61 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
62 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
63 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
64 }
65
66 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
67 {
68 int i;
69
70 for (i = 0; i < phnum; i++, phdr++) {
71 bswap32s(&phdr->p_type); /* Segment type */
72 bswap32s(&phdr->p_flags); /* Segment flags */
73 bswaptls(&phdr->p_offset); /* Segment file offset */
74 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
75 bswaptls(&phdr->p_paddr); /* Segment physical address */
76 bswaptls(&phdr->p_filesz); /* Segment size in file */
77 bswaptls(&phdr->p_memsz); /* Segment size in memory */
78 bswaptls(&phdr->p_align); /* Segment alignment */
79 }
80 }
81
82 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
83 {
84 int i;
85
86 for (i = 0; i < shnum; i++, shdr++) {
87 bswap32s(&shdr->sh_name);
88 bswap32s(&shdr->sh_type);
89 bswaptls(&shdr->sh_flags);
90 bswaptls(&shdr->sh_addr);
91 bswaptls(&shdr->sh_offset);
92 bswaptls(&shdr->sh_size);
93 bswap32s(&shdr->sh_link);
94 bswap32s(&shdr->sh_info);
95 bswaptls(&shdr->sh_addralign);
96 bswaptls(&shdr->sh_entsize);
97 }
98 }
99
100 static void bswap_sym(struct elf_sym *sym)
101 {
102 bswap32s(&sym->st_name);
103 bswaptls(&sym->st_value);
104 bswaptls(&sym->st_size);
105 bswap16s(&sym->st_shndx);
106 }
107
108 static void bswap_note(struct elf_note *en)
109 {
110 bswap32s(&en->n_namesz);
111 bswap32s(&en->n_descsz);
112 bswap32s(&en->n_type);
113 }
114
115 #else
116
117 static void bswap_ehdr(struct elfhdr *ehdr) { }
118 static void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
119 static void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
120 static void bswap_sym(struct elf_sym *sym) { }
121 static void bswap_note(struct elf_note *en) { }
122
123 #endif /* HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN */
124
125 #include "elfcore.c"
126
127 /*
128 * 'copy_elf_strings()' copies argument/envelope strings from user
129 * memory to free pages in kernel mem. These are in a format ready
130 * to be put directly into the top of new user memory.
131 *
132 */
133 static abi_ulong copy_elf_strings(int argc, char **argv, void **page,
134 abi_ulong p)
135 {
136 char *tmp, *tmp1, *pag = NULL;
137 int len, offset = 0;
138
139 if (!p) {
140 return 0; /* bullet-proofing */
141 }
142 while (argc-- > 0) {
143 tmp = argv[argc];
144 if (!tmp) {
145 fprintf(stderr, "VFS: argc is wrong");
146 exit(-1);
147 }
148 tmp1 = tmp;
149 while (*tmp++) {
150 continue;
151 }
152 len = tmp - tmp1;
153 if (p < len) { /* this shouldn't happen - 128kB */
154 return 0;
155 }
156 while (len) {
157 --p; --tmp; --len;
158 if (--offset < 0) {
159 offset = p % TARGET_PAGE_SIZE;
160 pag = page[p / TARGET_PAGE_SIZE];
161 if (!pag) {
162 pag = g_try_malloc0(TARGET_PAGE_SIZE);
163 page[p / TARGET_PAGE_SIZE] = pag;
164 if (!pag) {
165 return 0;
166 }
167 }
168 }
169 if (len == 0 || offset == 0) {
170 *(pag + offset) = *tmp;
171 } else {
172 int bytes_to_copy = (len > offset) ? offset : len;
173 tmp -= bytes_to_copy;
174 p -= bytes_to_copy;
175 offset -= bytes_to_copy;
176 len -= bytes_to_copy;
177 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
178 }
179 }
180 }
181 return p;
182 }
183
184 static void setup_arg_pages(struct bsd_binprm *bprm, struct image_info *info,
185 abi_ulong *stackp, abi_ulong *stringp)
186 {
187 abi_ulong stack_base, size;
188 abi_long addr;
189
190 /*
191 * Create enough stack to hold everything. If we don't use it for args,
192 * we'll use it for something else...
193 */
194 size = target_dflssiz;
195 stack_base = TARGET_USRSTACK - size;
196 addr = target_mmap(stack_base , size + qemu_host_page_size,
197 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
198 if (addr == -1) {
199 perror("stk mmap");
200 exit(-1);
201 }
202 /* we reserve one extra page at the top of the stack as guard */
203 target_mprotect(addr + size, qemu_host_page_size, PROT_NONE);
204
205 target_stksiz = size;
206 target_stkbas = addr;
207
208 if (setup_initial_stack(bprm, stackp, stringp) != 0) {
209 perror("stk setup");
210 exit(-1);
211 }
212 }
213
214 static void set_brk(abi_ulong start, abi_ulong end)
215 {
216 /* page-align the start and end addresses... */
217 start = HOST_PAGE_ALIGN(start);
218 end = HOST_PAGE_ALIGN(end);
219 if (end <= start) {
220 return;
221 }
222 if (target_mmap(start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC,
223 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
224 perror("cannot mmap brk");
225 exit(-1);
226 }
227 }
228
229
230 /*
231 * We need to explicitly zero any fractional pages after the data
232 * section (i.e. bss). This would contain the junk from the file that
233 * should not be in memory.
234 */
235 static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
236 {
237 abi_ulong nbyte;
238
239 if (elf_bss >= last_bss) {
240 return;
241 }
242
243 /*
244 * XXX: this is really a hack : if the real host page size is
245 * smaller than the target page size, some pages after the end
246 * of the file may not be mapped. A better fix would be to
247 * patch target_mmap(), but it is more complicated as the file
248 * size must be known.
249 */
250 if (qemu_real_host_page_size() < qemu_host_page_size) {
251 abi_ulong end_addr, end_addr1;
252 end_addr1 = REAL_HOST_PAGE_ALIGN(elf_bss);
253 end_addr = HOST_PAGE_ALIGN(elf_bss);
254 if (end_addr1 < end_addr) {
255 mmap((void *)g2h_untagged(end_addr1), end_addr - end_addr1,
256 PROT_READ | PROT_WRITE | PROT_EXEC,
257 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
258 }
259 }
260
261 nbyte = elf_bss & (qemu_host_page_size - 1);
262 if (nbyte) {
263 nbyte = qemu_host_page_size - nbyte;
264 do {
265 /* FIXME - what to do if put_user() fails? */
266 put_user_u8(0, elf_bss);
267 elf_bss++;
268 } while (--nbyte);
269 }
270 }
271
272 static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex,
273 int interpreter_fd,
274 abi_ulong *interp_load_addr)
275 {
276 struct elf_phdr *elf_phdata = NULL;
277 abi_ulong rbase;
278 int retval;
279 abi_ulong baddr, error;
280
281 error = 0;
282
283 bswap_ehdr(interp_elf_ex);
284 /* First of all, some simple consistency checks */
285 if ((interp_elf_ex->e_type != ET_EXEC && interp_elf_ex->e_type != ET_DYN) ||
286 !elf_check_arch(interp_elf_ex->e_machine)) {
287 return ~((abi_ulong)0UL);
288 }
289
290
291 /* Now read in all of the header information */
292 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE) {
293 return ~(abi_ulong)0UL;
294 }
295
296 elf_phdata = (struct elf_phdr *) malloc(sizeof(struct elf_phdr) *
297 interp_elf_ex->e_phnum);
298
299 if (!elf_phdata) {
300 return ~((abi_ulong)0UL);
301 }
302
303 /*
304 * If the size of this structure has changed, then punt, since
305 * we will be doing the wrong thing.
306 */
307 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
308 free(elf_phdata);
309 return ~((abi_ulong)0UL);
310 }
311
312 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
313 if (retval >= 0) {
314 retval = read(interpreter_fd, (char *) elf_phdata,
315 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
316 }
317 if (retval < 0) {
318 perror("load_elf_interp");
319 exit(-1);
320 free(elf_phdata);
321 return retval;
322 }
323 bswap_phdr(elf_phdata, interp_elf_ex->e_phnum);
324
325 rbase = 0;
326 if (interp_elf_ex->e_type == ET_DYN) {
327 /*
328 * In order to avoid hardcoding the interpreter load
329 * address in qemu, we allocate a big enough memory zone.
330 */
331 rbase = target_mmap(0, INTERP_MAP_SIZE, PROT_NONE,
332 MAP_PRIVATE | MAP_ANON, -1, 0);
333 if (rbase == -1) {
334 perror("mmap");
335 exit(-1);
336 }
337 }
338
339 error = load_elf_sections(interp_elf_ex, elf_phdata, interpreter_fd, rbase,
340 &baddr);
341 if (error != 0) {
342 perror("load_elf_sections");
343 exit(-1);
344 }
345
346 /* Now use mmap to map the library into memory. */
347 close(interpreter_fd);
348 free(elf_phdata);
349
350 *interp_load_addr = baddr;
351 return ((abi_ulong) interp_elf_ex->e_entry) + rbase;
352 }
353
354 static int symfind(const void *s0, const void *s1)
355 {
356 struct elf_sym *sym = (struct elf_sym *)s1;
357 __typeof(sym->st_value) addr = *(uint64_t *)s0;
358 int result = 0;
359
360 if (addr < sym->st_value) {
361 result = -1;
362 } else if (addr >= sym->st_value + sym->st_size) {
363 result = 1;
364 }
365 return result;
366 }
367
368 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
369 {
370 #if ELF_CLASS == ELFCLASS32
371 struct elf_sym *syms = s->disas_symtab.elf32;
372 #else
373 struct elf_sym *syms = s->disas_symtab.elf64;
374 #endif
375
376 /* binary search */
377 struct elf_sym *sym;
378
379 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
380 if (sym != NULL) {
381 return s->disas_strtab + sym->st_name;
382 }
383
384 return "";
385 }
386
387 /* FIXME: This should use elf_ops.h.inc */
388 static int symcmp(const void *s0, const void *s1)
389 {
390 struct elf_sym *sym0 = (struct elf_sym *)s0;
391 struct elf_sym *sym1 = (struct elf_sym *)s1;
392 return (sym0->st_value < sym1->st_value) ? -1 :
393 ((sym0->st_value > sym1->st_value) ? 1 : 0);
394 }
395
396 /* Best attempt to load symbols from this ELF object. */
397 static void load_symbols(struct elfhdr *hdr, int fd)
398 {
399 unsigned int i, nsyms;
400 struct elf_shdr sechdr, symtab, strtab;
401 char *strings;
402 struct syminfo *s;
403 struct elf_sym *syms, *new_syms;
404
405 lseek(fd, hdr->e_shoff, SEEK_SET);
406 for (i = 0; i < hdr->e_shnum; i++) {
407 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr)) {
408 return;
409 }
410 bswap_shdr(&sechdr, 1);
411 if (sechdr.sh_type == SHT_SYMTAB) {
412 symtab = sechdr;
413 lseek(fd, hdr->e_shoff + sizeof(sechdr) * sechdr.sh_link,
414 SEEK_SET);
415 if (read(fd, &strtab, sizeof(strtab)) != sizeof(strtab)) {
416 return;
417 }
418 bswap_shdr(&strtab, 1);
419 goto found;
420 }
421 }
422 return; /* Shouldn't happen... */
423
424 found:
425 /* Now know where the strtab and symtab are. Snarf them. */
426 s = malloc(sizeof(*s));
427 syms = malloc(symtab.sh_size);
428 if (!syms) {
429 free(s);
430 return;
431 }
432 s->disas_strtab = strings = malloc(strtab.sh_size);
433 if (!s->disas_strtab) {
434 free(s);
435 free(syms);
436 return;
437 }
438
439 lseek(fd, symtab.sh_offset, SEEK_SET);
440 if (read(fd, syms, symtab.sh_size) != symtab.sh_size) {
441 free(s);
442 free(syms);
443 free(strings);
444 return;
445 }
446
447 nsyms = symtab.sh_size / sizeof(struct elf_sym);
448
449 i = 0;
450 while (i < nsyms) {
451 bswap_sym(syms + i);
452 /* Throw away entries which we do not need. */
453 if (syms[i].st_shndx == SHN_UNDEF ||
454 syms[i].st_shndx >= SHN_LORESERVE ||
455 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
456 nsyms--;
457 if (i < nsyms) {
458 syms[i] = syms[nsyms];
459 }
460 continue;
461 }
462 #if defined(TARGET_ARM) || defined(TARGET_MIPS)
463 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
464 syms[i].st_value &= ~(target_ulong)1;
465 #endif
466 i++;
467 }
468
469 /*
470 * Attempt to free the storage associated with the local symbols
471 * that we threw away. Whether or not this has any effect on the
472 * memory allocation depends on the malloc implementation and how
473 * many symbols we managed to discard.
474 */
475 new_syms = realloc(syms, nsyms * sizeof(*syms));
476 if (new_syms == NULL) {
477 free(s);
478 free(syms);
479 free(strings);
480 return;
481 }
482 syms = new_syms;
483
484 qsort(syms, nsyms, sizeof(*syms), symcmp);
485
486 lseek(fd, strtab.sh_offset, SEEK_SET);
487 if (read(fd, strings, strtab.sh_size) != strtab.sh_size) {
488 free(s);
489 free(syms);
490 free(strings);
491 return;
492 }
493 s->disas_num_syms = nsyms;
494 #if ELF_CLASS == ELFCLASS32
495 s->disas_symtab.elf32 = syms;
496 s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
497 #else
498 s->disas_symtab.elf64 = syms;
499 s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
500 #endif
501 s->next = syminfos;
502 syminfos = s;
503 }
504
505 /* Check the elf header and see if this a target elf binary. */
506 int is_target_elf_binary(int fd)
507 {
508 uint8_t buf[128];
509 struct elfhdr elf_ex;
510
511 if (lseek(fd, 0L, SEEK_SET) < 0) {
512 return 0;
513 }
514 if (read(fd, buf, sizeof(buf)) < 0) {
515 return 0;
516 }
517
518 elf_ex = *((struct elfhdr *)buf);
519 bswap_ehdr(&elf_ex);
520
521 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
522 (!elf_check_arch(elf_ex.e_machine))) {
523 return 0;
524 } else {
525 return 1;
526 }
527 }
528
529 static int
530 load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr, int fd,
531 abi_ulong rbase, abi_ulong *baddrp)
532 {
533 struct elf_phdr *elf_ppnt;
534 abi_ulong baddr;
535 int i;
536 bool first;
537
538 /*
539 * Now we do a little grungy work by mmaping the ELF image into
540 * the correct location in memory. At this point, we assume that
541 * the image should be loaded at fixed address, not at a variable
542 * address.
543 */
544 first = true;
545 for (i = 0, elf_ppnt = phdr; i < hdr->e_phnum; i++, elf_ppnt++) {
546 int elf_prot = 0;
547 abi_ulong error;
548
549 /* XXX Skip memsz == 0. */
550 if (elf_ppnt->p_type != PT_LOAD) {
551 continue;
552 }
553
554 if (elf_ppnt->p_flags & PF_R) {
555 elf_prot |= PROT_READ;
556 }
557 if (elf_ppnt->p_flags & PF_W) {
558 elf_prot |= PROT_WRITE;
559 }
560 if (elf_ppnt->p_flags & PF_X) {
561 elf_prot |= PROT_EXEC;
562 }
563
564 error = target_mmap(TARGET_ELF_PAGESTART(rbase + elf_ppnt->p_vaddr),
565 (elf_ppnt->p_filesz +
566 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
567 elf_prot,
568 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
569 fd,
570 (elf_ppnt->p_offset -
571 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
572 if (error == -1) {
573 perror("mmap");
574 exit(-1);
575 } else if (elf_ppnt->p_memsz != elf_ppnt->p_filesz) {
576 abi_ulong start_bss, end_bss;
577
578 start_bss = rbase + elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
579 end_bss = rbase + elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
580
581 /*
582 * Calling set_brk effectively mmaps the pages that we need for the
583 * bss and break sections.
584 */
585 set_brk(start_bss, end_bss);
586 padzero(start_bss, end_bss);
587 }
588
589 if (first) {
590 baddr = TARGET_ELF_PAGESTART(rbase + elf_ppnt->p_vaddr);
591 first = false;
592 }
593 }
594
595 if (baddrp != NULL) {
596 *baddrp = baddr;
597 }
598 return 0;
599 }
600
601 int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
602 struct image_info *info)
603 {
604 struct elfhdr elf_ex;
605 struct elfhdr interp_elf_ex;
606 int interpreter_fd = -1; /* avoid warning */
607 abi_ulong load_addr;
608 int i;
609 struct elf_phdr *elf_ppnt;
610 struct elf_phdr *elf_phdata;
611 abi_ulong elf_brk;
612 int error, retval;
613 char *elf_interpreter;
614 abi_ulong elf_entry, et_dyn_addr, interp_load_addr = 0;
615 abi_ulong reloc_func_desc = 0;
616 PGBRange range = { -1, 0 };
617
618 load_addr = 0;
619 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
620 bswap_ehdr(&elf_ex);
621
622 /* First of all, some simple consistency checks */
623 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
624 (!elf_check_arch(elf_ex.e_machine))) {
625 return -ENOEXEC;
626 }
627
628 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
629 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, bprm->page, bprm->p);
630 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, bprm->page, bprm->p);
631 if (!bprm->p) {
632 retval = -E2BIG;
633 }
634
635 /* Now read in all of the header information */
636 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize * elf_ex.e_phnum);
637 if (elf_phdata == NULL) {
638 return -ENOMEM;
639 }
640
641 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
642 if (retval > 0) {
643 retval = read(bprm->fd, (char *)elf_phdata,
644 elf_ex.e_phentsize * elf_ex.e_phnum);
645 }
646
647 if (retval < 0) {
648 perror("load_elf_binary");
649 exit(-1);
650 free(elf_phdata);
651 return -errno;
652 }
653
654 bswap_phdr(elf_phdata, elf_ex.e_phnum);
655 elf_ppnt = elf_phdata;
656
657 elf_brk = 0;
658
659 elf_interpreter = NULL;
660 for (i = 0; i < elf_ex.e_phnum; i++, elf_ppnt++) {
661 switch (elf_ppnt->p_type) {
662 case PT_INTERP:
663 if (elf_interpreter != NULL) {
664 free(elf_phdata);
665 free(elf_interpreter);
666 close(bprm->fd);
667 return -EINVAL;
668 }
669
670 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
671 if (elf_interpreter == NULL) {
672 free(elf_phdata);
673 close(bprm->fd);
674 return -ENOMEM;
675 }
676
677 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
678 if (retval >= 0) {
679 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
680 }
681 if (retval < 0) {
682 perror("load_elf_binary2");
683 exit(-1);
684 }
685
686 if (retval >= 0) {
687 retval = open(path(elf_interpreter), O_RDONLY);
688 if (retval >= 0) {
689 interpreter_fd = retval;
690 } else {
691 perror(elf_interpreter);
692 exit(-1);
693 /* retval = -errno; */
694 }
695 }
696
697 if (retval >= 0) {
698 retval = lseek(interpreter_fd, 0, SEEK_SET);
699 if (retval >= 0) {
700 retval = read(interpreter_fd, bprm->buf, 128);
701 }
702 }
703 if (retval >= 0) {
704 interp_elf_ex = *((struct elfhdr *) bprm->buf);
705 }
706 if (retval < 0) {
707 perror("load_elf_binary3");
708 exit(-1);
709 free(elf_phdata);
710 free(elf_interpreter);
711 close(bprm->fd);
712 return retval;
713 }
714 break;
715
716 case PT_LOAD:
717 range.lo = MIN(range.lo, elf_ppnt->p_vaddr);
718 range.hi = MAX(range.hi,
719 elf_ppnt->p_vaddr + elf_ppnt->p_memsz - 1);
720 break;
721 }
722 }
723
724 /* Some simple consistency checks for the interpreter */
725 if (elf_interpreter) {
726 if (interp_elf_ex.e_ident[0] != 0x7f ||
727 strncmp((char *)&interp_elf_ex.e_ident[1], "ELF", 3) != 0) {
728 free(elf_interpreter);
729 free(elf_phdata);
730 close(bprm->fd);
731 return -ELIBBAD;
732 }
733 }
734
735 /*
736 * OK, we are done with that, now set up the arg stuff, and then start this
737 * sucker up
738 */
739 if (!bprm->p) {
740 free(elf_interpreter);
741 free(elf_phdata);
742 close(bprm->fd);
743 return -E2BIG;
744 }
745
746 /* OK, This is the point of no return */
747 info->end_data = 0;
748 info->end_code = 0;
749 elf_entry = (abi_ulong) elf_ex.e_entry;
750
751 et_dyn_addr = 0;
752 if (elf_ex.e_type == ET_DYN) {
753 probe_guest_base(bprm->filename, NULL, NULL);
754 et_dyn_addr = ELF_ET_DYN_LOAD_ADDR - range.lo;
755 } else {
756 probe_guest_base(bprm->filename, &range, NULL);
757 }
758
759 /*
760 * Do this so that we can load the interpreter, if need be. We will
761 * change some of these later
762 */
763 info->rss = 0;
764 setup_arg_pages(bprm, info, &bprm->p, &bprm->stringp);
765 info->start_stack = bprm->p;
766
767 info->elf_flags = elf_ex.e_flags;
768
769 error = load_elf_sections(&elf_ex, elf_phdata, bprm->fd, et_dyn_addr,
770 &load_addr);
771 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
772 if (elf_ppnt->p_type != PT_LOAD) {
773 continue;
774 }
775 if (elf_ppnt->p_memsz > elf_ppnt->p_filesz)
776 elf_brk = MAX(elf_brk, et_dyn_addr + elf_ppnt->p_vaddr +
777 elf_ppnt->p_memsz);
778 }
779 if (error != 0) {
780 perror("load_elf_sections");
781 exit(-1);
782 }
783
784 if (elf_interpreter) {
785 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
786 &interp_load_addr);
787 reloc_func_desc = interp_load_addr;
788
789 close(interpreter_fd);
790 free(elf_interpreter);
791
792 if (elf_entry == ~((abi_ulong)0UL)) {
793 printf("Unable to load interpreter\n");
794 free(elf_phdata);
795 exit(-1);
796 return 0;
797 }
798 } else {
799 interp_load_addr = et_dyn_addr;
800 elf_entry += interp_load_addr;
801 }
802
803 free(elf_phdata);
804
805 if (qemu_log_enabled()) {
806 load_symbols(&elf_ex, bprm->fd);
807 }
808
809 close(bprm->fd);
810
811 bprm->p = target_create_elf_tables(bprm->p, bprm->argc, bprm->envc,
812 bprm->stringp, &elf_ex, load_addr,
813 et_dyn_addr, interp_load_addr, info);
814 info->load_addr = reloc_func_desc;
815 info->brk = elf_brk;
816 info->start_stack = bprm->p;
817 info->load_bias = 0;
818
819 info->entry = elf_entry;
820
821 #ifdef USE_ELF_CORE_DUMP
822 bprm->core_dump = &elf_core_dump;
823 #else
824 bprm->core_dump = NULL;
825 #endif
826
827 return 0;
828 }
829
830 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
831 {
832
833 target_thread_init(regs, infop);
834 }