]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/elfload.c
bsd-user: Eliminate elf personality
[mirror_qemu.git] / bsd-user / elfload.c
CommitLineData
310df056
WL
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 */
84778508 19
2231197c 20#include "qemu/osdep.h"
84778508
BS
21
22#include "qemu.h"
76cad711 23#include "disas/disas.h"
f348b6d1 24#include "qemu/path.h"
84778508 25
66ef252f
WL
26#include "target_arch_elf.h"
27
84778508
BS
28/* this flag is uneffective under linux too, should be deleted */
29#ifndef MAP_DENYWRITE
30#define MAP_DENYWRITE 0
31#endif
32
33/* should probably go in elf.h */
34#ifndef ELIBBAD
35#define ELIBBAD 80
36#endif
37
84778508
BS
38#ifndef ELF_PLATFORM
39#define ELF_PLATFORM (NULL)
40#endif
41
42#ifndef ELF_HWCAP
43#define ELF_HWCAP 0
44#endif
45
46#ifdef TARGET_ABI32
47#undef ELF_CLASS
48#define ELF_CLASS ELFCLASS32
49#undef bswaptls
50#define bswaptls(ptr) bswap32s(ptr)
51#endif
52
53#include "elf.h"
54
55struct exec
56{
57 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
58 unsigned int a_text; /* length of text, in bytes */
59 unsigned int a_data; /* length of data, in bytes */
60 unsigned int a_bss; /* length of uninitialized data area, in bytes */
61 unsigned int a_syms; /* length of symbol table data in file, in bytes */
62 unsigned int a_entry; /* start address */
63 unsigned int a_trsize; /* length of relocation info for text, in bytes */
64 unsigned int a_drsize; /* length of relocation info for data, in bytes */
65};
66
67
68#define N_MAGIC(exec) ((exec).a_info & 0xffff)
69#define OMAGIC 0407
70#define NMAGIC 0410
71#define ZMAGIC 0413
72#define QMAGIC 0314
73
74/* max code+data+bss space allocated to elf interpreter */
75#define INTERP_MAP_SIZE (32 * 1024 * 1024)
76
77/* max code+data+bss+brk space allocated to ET_DYN executables */
78#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
79
80/* Necessary parameters */
81#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
b4bebeee
WL
82#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
83#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
84778508
BS
84
85#define INTERPRETER_NONE 0
86#define INTERPRETER_AOUT 1
87#define INTERPRETER_ELF 2
88
89#define DLINFO_ITEMS 12
90
b4bebeee 91static inline void memcpy_fromfs(void *to, const void *from, unsigned long n)
84778508
BS
92{
93 memcpy(to, from, n);
94}
95
b4bebeee 96static int load_aout_interp(void *exptr, int interp_fd);
84778508
BS
97
98#ifdef BSWAP_NEEDED
99static void bswap_ehdr(struct elfhdr *ehdr)
100{
101 bswap16s(&ehdr->e_type); /* Object file type */
102 bswap16s(&ehdr->e_machine); /* Architecture */
103 bswap32s(&ehdr->e_version); /* Object file version */
104 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
105 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
106 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
107 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
108 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
109 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
110 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
111 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
112 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
113 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
114}
115
116static void bswap_phdr(struct elf_phdr *phdr)
117{
118 bswap32s(&phdr->p_type); /* Segment type */
119 bswaptls(&phdr->p_offset); /* Segment file offset */
120 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
121 bswaptls(&phdr->p_paddr); /* Segment physical address */
122 bswaptls(&phdr->p_filesz); /* Segment size in file */
123 bswaptls(&phdr->p_memsz); /* Segment size in memory */
124 bswap32s(&phdr->p_flags); /* Segment flags */
125 bswaptls(&phdr->p_align); /* Segment alignment */
126}
127
128static void bswap_shdr(struct elf_shdr *shdr)
129{
130 bswap32s(&shdr->sh_name);
131 bswap32s(&shdr->sh_type);
132 bswaptls(&shdr->sh_flags);
133 bswaptls(&shdr->sh_addr);
134 bswaptls(&shdr->sh_offset);
135 bswaptls(&shdr->sh_size);
136 bswap32s(&shdr->sh_link);
137 bswap32s(&shdr->sh_info);
138 bswaptls(&shdr->sh_addralign);
139 bswaptls(&shdr->sh_entsize);
140}
141
142static void bswap_sym(struct elf_sym *sym)
143{
144 bswap32s(&sym->st_name);
145 bswaptls(&sym->st_value);
146 bswaptls(&sym->st_size);
147 bswap16s(&sym->st_shndx);
148}
149#endif
150
151/*
152 * 'copy_elf_strings()' copies argument/envelope strings from user
153 * memory to free pages in kernel mem. These are in a format ready
154 * to be put directly into the top of new user memory.
155 *
156 */
b4bebeee 157static abi_ulong copy_elf_strings(int argc, char **argv, void **page,
84778508
BS
158 abi_ulong p)
159{
160 char *tmp, *tmp1, *pag = NULL;
161 int len, offset = 0;
162
163 if (!p) {
164 return 0; /* bullet-proofing */
165 }
166 while (argc-- > 0) {
167 tmp = argv[argc];
168 if (!tmp) {
9bb93180 169 fprintf(stderr, "VFS: argc is wrong");
84778508
BS
170 exit(-1);
171 }
172 tmp1 = tmp;
173 while (*tmp++);
174 len = tmp - tmp1;
175 if (p < len) { /* this shouldn't happen - 128kB */
176 return 0;
177 }
178 while (len) {
179 --p; --tmp; --len;
180 if (--offset < 0) {
181 offset = p % TARGET_PAGE_SIZE;
b4bebeee 182 pag = (char *)page[p / TARGET_PAGE_SIZE];
84778508 183 if (!pag) {
c580dee4 184 pag = g_try_malloc0(TARGET_PAGE_SIZE);
b4bebeee 185 page[p / TARGET_PAGE_SIZE] = pag;
84778508
BS
186 if (!pag)
187 return 0;
188 }
189 }
190 if (len == 0 || offset == 0) {
191 *(pag + offset) = *tmp;
192 }
193 else {
194 int bytes_to_copy = (len > offset) ? offset : len;
195 tmp -= bytes_to_copy;
196 p -= bytes_to_copy;
197 offset -= bytes_to_copy;
198 len -= bytes_to_copy;
199 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
200 }
201 }
202 }
203 return p;
204}
205
afcbcff8 206static abi_ulong setup_arg_pages(abi_ulong p, struct bsd_binprm *bprm,
84778508
BS
207 struct image_info *info)
208{
209 abi_ulong stack_base, size, error;
210 int i;
211
212 /* Create enough stack to hold everything. If we don't use
213 * it for args, we'll use it for something else...
214 */
215 size = x86_stack_size;
b4bebeee
WL
216 if (size < MAX_ARG_PAGES * TARGET_PAGE_SIZE)
217 size = MAX_ARG_PAGES * TARGET_PAGE_SIZE;
84778508
BS
218 error = target_mmap(0,
219 size + qemu_host_page_size,
220 PROT_READ | PROT_WRITE,
221 MAP_PRIVATE | MAP_ANON,
222 -1, 0);
223 if (error == -1) {
224 perror("stk mmap");
225 exit(-1);
226 }
227 /* we reserve one extra page at the top of the stack as guard */
228 target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
229
b4bebeee 230 stack_base = error + size - MAX_ARG_PAGES * TARGET_PAGE_SIZE;
84778508
BS
231 p += stack_base;
232
233 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
234 if (bprm->page[i]) {
235 info->rss++;
236 /* FIXME - check return value of memcpy_to_target() for failure */
237 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
c580dee4 238 g_free(bprm->page[i]);
84778508
BS
239 }
240 stack_base += TARGET_PAGE_SIZE;
241 }
242 return p;
243}
244
245static void set_brk(abi_ulong start, abi_ulong end)
246{
247 /* page-align the start and end addresses... */
248 start = HOST_PAGE_ALIGN(start);
249 end = HOST_PAGE_ALIGN(end);
250 if (end <= start)
251 return;
b4bebeee 252 if (target_mmap(start, end - start,
84778508
BS
253 PROT_READ | PROT_WRITE | PROT_EXEC,
254 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
255 perror("cannot mmap brk");
256 exit(-1);
257 }
258}
259
260
261/* We need to explicitly zero any fractional pages after the data
262 section (i.e. bss). This would contain the junk from the file that
263 should not be in memory. */
264static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
265{
266 abi_ulong nbyte;
267
268 if (elf_bss >= last_bss)
269 return;
270
271 /* XXX: this is really a hack : if the real host page size is
272 smaller than the target page size, some pages after the end
273 of the file may not be mapped. A better fix would be to
274 patch target_mmap(), but it is more complicated as the file
275 size must be known */
276 if (qemu_real_host_page_size < qemu_host_page_size) {
277 abi_ulong end_addr, end_addr1;
0c2d70c4 278 end_addr1 = REAL_HOST_PAGE_ALIGN(elf_bss);
84778508
BS
279 end_addr = HOST_PAGE_ALIGN(elf_bss);
280 if (end_addr1 < end_addr) {
3e8f1628 281 mmap((void *)g2h_untagged(end_addr1), end_addr - end_addr1,
b4bebeee
WL
282 PROT_READ | PROT_WRITE | PROT_EXEC,
283 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
84778508
BS
284 }
285 }
286
b4bebeee 287 nbyte = elf_bss & (qemu_host_page_size - 1);
84778508
BS
288 if (nbyte) {
289 nbyte = qemu_host_page_size - nbyte;
290 do {
291 /* FIXME - what to do if put_user() fails? */
292 put_user_u8(0, elf_bss);
293 elf_bss++;
294 } while (--nbyte);
295 }
296}
297
298
299static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
300 struct elfhdr * exec,
301 abi_ulong load_addr,
302 abi_ulong load_bias,
303 abi_ulong interp_load_addr, int ibcs,
304 struct image_info *info)
305{
306 abi_ulong sp;
307 int size;
308 abi_ulong u_platform;
309 const char *k_platform;
310 const int n = sizeof(elf_addr_t);
311
312 sp = p;
313 u_platform = 0;
314 k_platform = ELF_PLATFORM;
315 if (k_platform) {
316 size_t len = strlen(k_platform) + 1;
317 sp -= (len + n - 1) & ~(n - 1);
318 u_platform = sp;
319 /* FIXME - check return value of memcpy_to_target() for failure */
320 memcpy_to_target(sp, k_platform, len);
321 }
322 /*
323 * Force 16 byte _final_ alignment here for generality.
324 */
b4bebeee 325 sp = sp & ~(abi_ulong)15;
84778508
BS
326 size = (DLINFO_ITEMS + 1) * 2;
327 if (k_platform)
b4bebeee 328 size += 2;
84778508
BS
329#ifdef DLINFO_ARCH_ITEMS
330 size += DLINFO_ARCH_ITEMS * 2;
331#endif
332 size += envc + argc + 2;
333 size += (!ibcs ? 3 : 1); /* argc itself */
334 size *= n;
335 if (size & 15)
b4bebeee 336 sp -= 16 - (size & 15);
84778508
BS
337
338 /* This is correct because Linux defines
339 * elf_addr_t as Elf32_Off / Elf64_Off
340 */
341#define NEW_AUX_ENT(id, val) do { \
342 sp -= n; put_user_ual(val, sp); \
343 sp -= n; put_user_ual(id, sp); \
b4bebeee 344 } while (0)
84778508 345
b4bebeee 346 NEW_AUX_ENT(AT_NULL, 0);
84778508
BS
347
348 /* There must be exactly DLINFO_ITEMS entries here. */
349 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
b4bebeee 350 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
84778508
BS
351 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
352 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
353 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
354 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
355 NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
356 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
357 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
358 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
359 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
360 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
361 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
362 if (k_platform)
363 NEW_AUX_ENT(AT_PLATFORM, u_platform);
364#ifdef ARCH_DLINFO
365 /*
366 * ARCH_DLINFO must come last so platform specific code can enforce
367 * special alignment requirements on the AUXV if necessary (eg. PPC).
368 */
369 ARCH_DLINFO;
370#endif
371#undef NEW_AUX_ENT
372
373 sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
374 return sp;
375}
376
377
b4bebeee 378static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex,
84778508
BS
379 int interpreter_fd,
380 abi_ulong *interp_load_addr)
381{
b4bebeee
WL
382 struct elf_phdr *elf_phdata = NULL;
383 struct elf_phdr *eppnt;
384 abi_ulong load_addr = 0;
385 int load_addr_set = 0;
386 int retval;
387 abi_ulong last_bss, elf_bss;
388 abi_ulong error;
389 int i;
84778508 390
b4bebeee
WL
391 elf_bss = 0;
392 last_bss = 0;
393 error = 0;
84778508
BS
394
395#ifdef BSWAP_NEEDED
b4bebeee 396 bswap_ehdr(interp_elf_ex);
84778508 397#endif
b4bebeee
WL
398 /* First of all, some simple consistency checks */
399 if ((interp_elf_ex->e_type != ET_EXEC &&
400 interp_elf_ex->e_type != ET_DYN) ||
401 !elf_check_arch(interp_elf_ex->e_machine)) {
402 return ~((abi_ulong)0UL);
403 }
84778508
BS
404
405
b4bebeee 406 /* Now read in all of the header information */
84778508 407
b4bebeee
WL
408 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
409 return ~(abi_ulong)0UL;
84778508 410
b4bebeee
WL
411 elf_phdata = (struct elf_phdr *)
412 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
84778508 413
b4bebeee
WL
414 if (!elf_phdata)
415 return ~((abi_ulong)0UL);
84778508 416
b4bebeee
WL
417 /*
418 * If the size of this structure has changed, then punt, since
419 * we will be doing the wrong thing.
420 */
421 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
422 free(elf_phdata);
423 return ~((abi_ulong)0UL);
424 }
84778508 425
b4bebeee
WL
426 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
427 if (retval >= 0) {
428 retval = read(interpreter_fd,
429 (char *) elf_phdata,
430 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
431 }
432 if (retval < 0) {
433 perror("load_elf_interp");
434 exit(-1);
435 free (elf_phdata);
436 return retval;
437 }
84778508 438#ifdef BSWAP_NEEDED
b4bebeee
WL
439 eppnt = elf_phdata;
440 for (i = 0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
441 bswap_phdr(eppnt);
442 }
84778508
BS
443#endif
444
b4bebeee
WL
445 if (interp_elf_ex->e_type == ET_DYN) {
446 /* in order to avoid hardcoding the interpreter load
447 address in qemu, we allocate a big enough memory zone */
448 error = target_mmap(0, INTERP_MAP_SIZE,
449 PROT_NONE, MAP_PRIVATE | MAP_ANON,
450 -1, 0);
451 if (error == -1) {
452 perror("mmap");
453 exit(-1);
84778508 454 }
b4bebeee
WL
455 load_addr = error;
456 load_addr_set = 1;
457 }
84778508 458
b4bebeee
WL
459 eppnt = elf_phdata;
460 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++)
461 if (eppnt->p_type == PT_LOAD) {
84778508
BS
462 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
463 int elf_prot = 0;
464 abi_ulong vaddr = 0;
465 abi_ulong k;
466
467 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
468 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
469 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
470 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
471 elf_type |= MAP_FIXED;
472 vaddr = eppnt->p_vaddr;
473 }
b4bebeee
WL
474 error = target_mmap(load_addr + TARGET_ELF_PAGESTART(vaddr),
475 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
476 elf_prot,
477 elf_type,
478 interpreter_fd,
479 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
84778508
BS
480
481 if (error == -1) {
b4bebeee
WL
482 /* Real error */
483 close(interpreter_fd);
484 free(elf_phdata);
485 return ~((abi_ulong)0UL);
84778508
BS
486 }
487
488 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
b4bebeee
WL
489 load_addr = error;
490 load_addr_set = 1;
84778508
BS
491 }
492
493 /*
494 * Find the end of the file mapping for this phdr, and keep
495 * track of the largest address we see for this.
496 */
497 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
498 if (k > elf_bss) elf_bss = k;
499
500 /*
501 * Do the same thing for the memory mapping - between
502 * elf_bss and last_bss is the bss section.
503 */
504 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
505 if (k > last_bss) last_bss = k;
b4bebeee 506 }
84778508 507
b4bebeee 508 /* Now use mmap to map the library into memory. */
84778508 509
b4bebeee 510 close(interpreter_fd);
84778508 511
b4bebeee
WL
512 /*
513 * Now fill out the bss section. First pad the last page up
514 * to the page boundary, and then perform a mmap to make sure
515 * that there are zeromapped pages up to and including the last
516 * bss page.
517 */
518 padzero(elf_bss, last_bss);
519 elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
520
521 /* Map the last of the bss segment */
522 if (last_bss > elf_bss) {
523 target_mmap(elf_bss, last_bss - elf_bss,
524 PROT_READ | PROT_WRITE | PROT_EXEC,
525 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
526 }
527 free(elf_phdata);
84778508 528
b4bebeee
WL
529 *interp_load_addr = load_addr;
530 return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
84778508
BS
531}
532
533static int symfind(const void *s0, const void *s1)
534{
c7c530cd 535 target_ulong addr = *(target_ulong *)s0;
84778508
BS
536 struct elf_sym *sym = (struct elf_sym *)s1;
537 int result = 0;
c7c530cd 538 if (addr < sym->st_value) {
84778508 539 result = -1;
c7c530cd 540 } else if (addr >= sym->st_value + sym->st_size) {
84778508
BS
541 result = 1;
542 }
543 return result;
544}
545
546static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
547{
548#if ELF_CLASS == ELFCLASS32
549 struct elf_sym *syms = s->disas_symtab.elf32;
550#else
551 struct elf_sym *syms = s->disas_symtab.elf64;
552#endif
553
554 // binary search
84778508
BS
555 struct elf_sym *sym;
556
c7c530cd 557 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
7cba04f6 558 if (sym != NULL) {
84778508
BS
559 return s->disas_strtab + sym->st_name;
560 }
561
562 return "";
563}
564
565/* FIXME: This should use elf_ops.h */
566static int symcmp(const void *s0, const void *s1)
567{
568 struct elf_sym *sym0 = (struct elf_sym *)s0;
569 struct elf_sym *sym1 = (struct elf_sym *)s1;
570 return (sym0->st_value < sym1->st_value)
571 ? -1
572 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
573}
574
575/* Best attempt to load symbols from this ELF object. */
576static void load_symbols(struct elfhdr *hdr, int fd)
577{
578 unsigned int i, nsyms;
579 struct elf_shdr sechdr, symtab, strtab;
580 char *strings;
581 struct syminfo *s;
29718712 582 struct elf_sym *syms, *new_syms;
84778508
BS
583
584 lseek(fd, hdr->e_shoff, SEEK_SET);
585 for (i = 0; i < hdr->e_shnum; i++) {
586 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
587 return;
588#ifdef BSWAP_NEEDED
589 bswap_shdr(&sechdr);
590#endif
591 if (sechdr.sh_type == SHT_SYMTAB) {
592 symtab = sechdr;
593 lseek(fd, hdr->e_shoff
594 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
595 if (read(fd, &strtab, sizeof(strtab))
596 != sizeof(strtab))
597 return;
598#ifdef BSWAP_NEEDED
599 bswap_shdr(&strtab);
600#endif
601 goto found;
602 }
603 }
604 return; /* Shouldn't happen... */
605
606 found:
607 /* Now know where the strtab and symtab are. Snarf them. */
608 s = malloc(sizeof(*s));
609 syms = malloc(symtab.sh_size);
29718712
SW
610 if (!syms) {
611 free(s);
84778508 612 return;
29718712 613 }
84778508 614 s->disas_strtab = strings = malloc(strtab.sh_size);
29718712
SW
615 if (!s->disas_strtab) {
616 free(s);
617 free(syms);
84778508 618 return;
29718712 619 }
84778508
BS
620
621 lseek(fd, symtab.sh_offset, SEEK_SET);
29718712
SW
622 if (read(fd, syms, symtab.sh_size) != symtab.sh_size) {
623 free(s);
624 free(syms);
625 free(strings);
84778508 626 return;
29718712 627 }
84778508
BS
628
629 nsyms = symtab.sh_size / sizeof(struct elf_sym);
630
631 i = 0;
632 while (i < nsyms) {
633#ifdef BSWAP_NEEDED
634 bswap_sym(syms + i);
635#endif
636 // Throw away entries which we do not need.
637 if (syms[i].st_shndx == SHN_UNDEF ||
638 syms[i].st_shndx >= SHN_LORESERVE ||
639 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
640 nsyms--;
641 if (i < nsyms) {
642 syms[i] = syms[nsyms];
643 }
644 continue;
645 }
84778508
BS
646 i++;
647 }
29718712
SW
648
649 /* Attempt to free the storage associated with the local symbols
650 that we threw away. Whether or not this has any effect on the
651 memory allocation depends on the malloc implementation and how
652 many symbols we managed to discard. */
653 new_syms = realloc(syms, nsyms * sizeof(*syms));
654 if (new_syms == NULL) {
655 free(s);
656 free(syms);
657 free(strings);
658 return;
659 }
660 syms = new_syms;
84778508
BS
661
662 qsort(syms, nsyms, sizeof(*syms), symcmp);
663
664 lseek(fd, strtab.sh_offset, SEEK_SET);
29718712
SW
665 if (read(fd, strings, strtab.sh_size) != strtab.sh_size) {
666 free(s);
667 free(syms);
668 free(strings);
84778508 669 return;
29718712 670 }
84778508
BS
671 s->disas_num_syms = nsyms;
672#if ELF_CLASS == ELFCLASS32
673 s->disas_symtab.elf32 = syms;
032e51d7 674 s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
84778508
BS
675#else
676 s->disas_symtab.elf64 = syms;
032e51d7 677 s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
84778508
BS
678#endif
679 s->next = syminfos;
680 syminfos = s;
681}
682
afcbcff8 683int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
b4bebeee 684 struct image_info *info)
84778508
BS
685{
686 struct elfhdr elf_ex;
687 struct elfhdr interp_elf_ex;
688 struct exec interp_ex;
689 int interpreter_fd = -1; /* avoid warning */
690 abi_ulong load_addr, load_bias;
691 int load_addr_set = 0;
692 unsigned int interpreter_type = INTERPRETER_NONE;
84778508 693 int i;
84778508
BS
694 struct elf_phdr * elf_ppnt;
695 struct elf_phdr *elf_phdata;
696 abi_ulong elf_bss, k, elf_brk;
697 int retval;
698 char * elf_interpreter;
699 abi_ulong elf_entry, interp_load_addr = 0;
84778508
BS
700 abi_ulong start_code, end_code, start_data, end_data;
701 abi_ulong reloc_func_desc = 0;
71025956
PM
702#ifdef LOW_ELF_STACK
703 abi_ulong elf_stack = ~((abi_ulong)0UL);
704#endif
84778508
BS
705 char passed_fileno[6];
706
84778508
BS
707 load_addr = 0;
708 load_bias = 0;
709 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
710#ifdef BSWAP_NEEDED
711 bswap_ehdr(&elf_ex);
712#endif
713
714 /* First of all, some simple consistency checks */
715 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
b4bebeee 716 (!elf_check_arch(elf_ex.e_machine))) {
84778508
BS
717 return -ENOEXEC;
718 }
719
720 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
b4bebeee
WL
721 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, bprm->page,bprm->p);
722 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, bprm->page,bprm->p);
84778508
BS
723 if (!bprm->p) {
724 retval = -E2BIG;
725 }
726
727 /* Now read in all of the header information */
728 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
729 if (elf_phdata == NULL) {
730 return -ENOMEM;
731 }
732
733 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
b4bebeee
WL
734 if (retval > 0) {
735 retval = read(bprm->fd, (char *)elf_phdata,
84778508
BS
736 elf_ex.e_phentsize * elf_ex.e_phnum);
737 }
738
739 if (retval < 0) {
740 perror("load_elf_binary");
741 exit(-1);
b4bebeee 742 free(elf_phdata);
84778508
BS
743 return -errno;
744 }
745
746#ifdef BSWAP_NEEDED
747 elf_ppnt = elf_phdata;
b4bebeee 748 for (i = 0; i < elf_ex.e_phnum; i++, elf_ppnt++) {
84778508
BS
749 bswap_phdr(elf_ppnt);
750 }
751#endif
752 elf_ppnt = elf_phdata;
753
754 elf_bss = 0;
755 elf_brk = 0;
756
757
84778508
BS
758 elf_interpreter = NULL;
759 start_code = ~((abi_ulong)0UL);
760 end_code = 0;
761 start_data = 0;
762 end_data = 0;
763 interp_ex.a_info = 0;
764
b4bebeee 765 for (i = 0;i < elf_ex.e_phnum; i++) {
84778508 766 if (elf_ppnt->p_type == PT_INTERP) {
b4bebeee 767 if (elf_interpreter != NULL)
84778508 768 {
b4bebeee 769 free(elf_phdata);
84778508
BS
770 free(elf_interpreter);
771 close(bprm->fd);
772 return -EINVAL;
773 }
774
775 /* This is the program interpreter used for
776 * shared libraries - for now assume that this
777 * is an a.out format binary
778 */
779
780 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
781
782 if (elf_interpreter == NULL) {
b4bebeee 783 free(elf_phdata);
84778508
BS
784 close(bprm->fd);
785 return -ENOMEM;
786 }
787
788 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
b4bebeee 789 if (retval >= 0) {
84778508
BS
790 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
791 }
b4bebeee 792 if (retval < 0) {
84778508
BS
793 perror("load_elf_binary2");
794 exit(-1);
795 }
796
84778508
BS
797 if (retval >= 0) {
798 retval = open(path(elf_interpreter), O_RDONLY);
b4bebeee 799 if (retval >= 0) {
84778508
BS
800 interpreter_fd = retval;
801 }
802 else {
803 perror(elf_interpreter);
804 exit(-1);
805 /* retval = -errno; */
806 }
807 }
808
809 if (retval >= 0) {
810 retval = lseek(interpreter_fd, 0, SEEK_SET);
b4bebeee
WL
811 if (retval >= 0) {
812 retval = read(interpreter_fd, bprm->buf, 128);
84778508
BS
813 }
814 }
815 if (retval >= 0) {
816 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
6ece4df6 817 interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */
84778508
BS
818 }
819 if (retval < 0) {
820 perror("load_elf_binary3");
821 exit(-1);
b4bebeee 822 free(elf_phdata);
84778508
BS
823 free(elf_interpreter);
824 close(bprm->fd);
825 return retval;
826 }
827 }
828 elf_ppnt++;
829 }
830
831 /* Some simple consistency checks for the interpreter */
b4bebeee 832 if (elf_interpreter) {
84778508
BS
833 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
834
835 /* Now figure out which format our binary is */
836 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
837 (N_MAGIC(interp_ex) != QMAGIC)) {
b4bebeee 838 interpreter_type = INTERPRETER_ELF;
84778508
BS
839 }
840
841 if (interp_elf_ex.e_ident[0] != 0x7f ||
b4bebeee 842 strncmp((char *)&interp_elf_ex.e_ident[1], "ELF", 3) != 0) {
84778508
BS
843 interpreter_type &= ~INTERPRETER_ELF;
844 }
845
846 if (!interpreter_type) {
847 free(elf_interpreter);
848 free(elf_phdata);
849 close(bprm->fd);
850 return -ELIBBAD;
851 }
852 }
853
854 /* OK, we are done with that, now set up the arg stuff,
855 and then start this sucker up */
856
857 {
b4bebeee 858 char *passed_p;
84778508
BS
859
860 if (interpreter_type == INTERPRETER_AOUT) {
861 snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
862 passed_p = passed_fileno;
863
864 if (elf_interpreter) {
b4bebeee 865 bprm->p = copy_elf_strings(1, &passed_p, bprm->page, bprm->p);
84778508
BS
866 bprm->argc++;
867 }
868 }
869 if (!bprm->p) {
ef1e1e07 870 free(elf_interpreter);
b4bebeee 871 free(elf_phdata);
84778508
BS
872 close(bprm->fd);
873 return -E2BIG;
874 }
875 }
876
877 /* OK, This is the point of no return */
878 info->end_data = 0;
879 info->end_code = 0;
880 info->start_mmap = (abi_ulong)ELF_START_MMAP;
881 info->mmap = 0;
882 elf_entry = (abi_ulong) elf_ex.e_entry;
883
2fa5d9ba
BS
884 /*
885 * In case where user has not explicitly set the guest_base, we
886 * probe here that should we set it automatically.
887 */
888 if (!have_guest_base) {
889 /*
890 * Go through ELF program header table and find out whether
7d37435b 891 * any of the segments drop below our current mmap_min_addr and
2fa5d9ba
BS
892 * in that case set guest_base to corresponding address.
893 */
894 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
895 i++, elf_ppnt++) {
896 if (elf_ppnt->p_type != PT_LOAD)
897 continue;
898 if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
899 guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
900 break;
901 }
902 }
903 }
2fa5d9ba 904
84778508
BS
905 /* Do this so that we can load the interpreter, if need be. We will
906 change some of these later */
907 info->rss = 0;
908 bprm->p = setup_arg_pages(bprm->p, bprm, info);
909 info->start_stack = bprm->p;
910
911 /* Now we do a little grungy work by mmaping the ELF image into
912 * the correct location in memory. At this point, we assume that
913 * the image should be loaded at fixed address, not at a variable
914 * address.
915 */
916
b4bebeee 917 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
84778508
BS
918 int elf_prot = 0;
919 int elf_flags = 0;
920 abi_ulong error;
921
922 if (elf_ppnt->p_type != PT_LOAD)
923 continue;
924
925 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
926 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
927 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
928 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
929 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
930 elf_flags |= MAP_FIXED;
931 } else if (elf_ex.e_type == ET_DYN) {
932 /* Try and get dynamic programs out of the way of the default mmap
933 base, as well as whatever program they might try to exec. This
934 is because the brk will follow the loader, and is not movable. */
935 /* NOTE: for qemu, we do a big mmap to get enough space
936 without hardcoding any address */
937 error = target_mmap(0, ET_DYN_MAP_SIZE,
938 PROT_NONE, MAP_PRIVATE | MAP_ANON,
939 -1, 0);
940 if (error == -1) {
941 perror("mmap");
942 exit(-1);
943 }
944 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
945 }
946
947 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
948 (elf_ppnt->p_filesz +
949 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
950 elf_prot,
951 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
952 bprm->fd,
953 (elf_ppnt->p_offset -
954 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
955 if (error == -1) {
956 perror("mmap");
957 exit(-1);
958 }
959
960#ifdef LOW_ELF_STACK
961 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
962 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
963#endif
964
965 if (!load_addr_set) {
966 load_addr_set = 1;
967 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
968 if (elf_ex.e_type == ET_DYN) {
969 load_bias += error -
970 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
971 load_addr += load_bias;
972 reloc_func_desc = load_bias;
973 }
974 }
975 k = elf_ppnt->p_vaddr;
976 if (k < start_code)
977 start_code = k;
978 if (start_data < k)
979 start_data = k;
980 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
981 if (k > elf_bss)
982 elf_bss = k;
983 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
984 end_code = k;
985 if (end_data < k)
986 end_data = k;
987 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
988 if (k > elf_brk) elf_brk = k;
989 }
990
991 elf_entry += load_bias;
992 elf_bss += load_bias;
993 elf_brk += load_bias;
994 start_code += load_bias;
995 end_code += load_bias;
996 start_data += load_bias;
997 end_data += load_bias;
998
999 if (elf_interpreter) {
1000 if (interpreter_type & 1) {
1001 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1002 }
1003 else if (interpreter_type & 2) {
1004 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1005 &interp_load_addr);
1006 }
1007 reloc_func_desc = interp_load_addr;
1008
1009 close(interpreter_fd);
1010 free(elf_interpreter);
1011
1012 if (elf_entry == ~((abi_ulong)0UL)) {
1013 printf("Unable to load interpreter\n");
1014 free(elf_phdata);
1015 exit(-1);
1016 return 0;
1017 }
1018 }
1019
1020 free(elf_phdata);
1021
93fcfe39 1022 if (qemu_log_enabled())
84778508
BS
1023 load_symbols(&elf_ex, bprm->fd);
1024
1025 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
84778508
BS
1026
1027#ifdef LOW_ELF_STACK
1028 info->start_stack = bprm->p = elf_stack - 4;
1029#endif
1030 bprm->p = create_elf_tables(bprm->p,
1031 bprm->argc,
1032 bprm->envc,
1033 &elf_ex,
1034 load_addr, load_bias,
1035 interp_load_addr,
1036 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1037 info);
1038 info->load_addr = reloc_func_desc;
1039 info->start_brk = info->brk = elf_brk;
1040 info->end_code = end_code;
1041 info->start_code = start_code;
1042 info->start_data = start_data;
1043 info->end_data = end_data;
1044 info->start_stack = bprm->p;
1045
1046 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1047 sections */
1048 set_brk(elf_bss, elf_brk);
1049
1050 padzero(elf_bss, elf_brk);
1051
1052#if 0
1053 printf("(start_brk) %x\n" , info->start_brk);
1054 printf("(end_code) %x\n" , info->end_code);
1055 printf("(start_code) %x\n" , info->start_code);
1056 printf("(end_data) %x\n" , info->end_data);
1057 printf("(start_stack) %x\n" , info->start_stack);
1058 printf("(brk) %x\n" , info->brk);
1059#endif
1060
84778508
BS
1061 info->entry = elf_entry;
1062
1063 return 0;
1064}
1065
b4bebeee 1066static int load_aout_interp(void *exptr, int interp_fd)
84778508
BS
1067{
1068 printf("a.out interpreter not yet supported\n");
1069 return(0);
1070}
1071
1072void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
1073{
1074 init_thread(regs, infop);
1075}