1 /* This is the Linux kernel elf-loading code, ported into user space */
18 #define ELF_START_MMAP 0x80000000
21 * This is used to ensure we don't load something for the wrong architecture.
23 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
26 * These are used to set parameters in the core dumps.
28 #define ELF_CLASS ELFCLASS32
29 #define ELF_DATA ELFDATA2LSB
30 #define ELF_ARCH EM_386
32 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
33 starts %edx contains a pointer to a function which might be
34 registered using `atexit'. This provides a mean for the
35 dynamic linker to call DT_FINI functions for shared libraries
36 that have been loaded before the code runs.
38 A value of 0 tells we have no such handler. */
39 #define ELF_PLAT_INIT(_r) _r->edx = 0
41 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
43 regs->esp = infop->start_stack;
44 regs->eip = infop->entry;
47 #define USE_ELF_CORE_DUMP
48 #define ELF_EXEC_PAGESIZE 4096
54 #define ELF_START_MMAP 0x80000000
56 #define elf_check_arch(x) ( (x) == EM_ARM )
58 #define ELF_CLASS ELFCLASS32
59 #ifdef TARGET_WORDS_BIGENDIAN
60 #define ELF_DATA ELFDATA2MSB
62 #define ELF_DATA ELFDATA2LSB
64 #define ELF_ARCH EM_ARM
66 #define ELF_PLAT_INIT(_r) _r->ARM_r0 = 0
68 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
70 target_long *stack = (void *)infop->start_stack;
71 memset(regs, 0, sizeof(*regs));
72 regs->ARM_cpsr = 0x10;
73 regs->ARM_pc = infop->entry;
74 regs->ARM_sp = infop->start_stack;
75 regs->ARM_r2 = tswapl(stack[2]); /* envp */
76 regs->ARM_r1 = tswapl(stack[1]); /* argv */
77 /* XXX: it seems that r0 is zeroed after ! */
78 // regs->ARM_r0 = tswapl(stack[0]); /* argc */
81 #define USE_ELF_CORE_DUMP
82 #define ELF_EXEC_PAGESIZE 4096
88 #define ELF_START_MMAP 0x80000000
90 #define elf_check_arch(x) ( (x) == EM_SPARC )
92 #define ELF_CLASS ELFCLASS32
93 #define ELF_DATA ELFDATA2MSB
94 #define ELF_ARCH EM_SPARC
97 #define ELF_PLAT_INIT(_r)
99 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
102 regs->pc = infop->entry;
103 regs->npc = regs->pc + 4;
105 regs->u_regs[14] = infop->start_stack - 16 * 4;
112 #define ELF_START_MMAP 0x80000000
114 #define elf_check_arch(x) ( (x) == EM_PPC )
116 #define ELF_CLASS ELFCLASS32
117 #ifdef TARGET_WORDS_BIGENDIAN
118 #define ELF_DATA ELFDATA2MSB
120 #define ELF_DATA ELFDATA2LSB
122 #define ELF_ARCH EM_PPC
124 /* Note that isn't exactly what regular kernel does
125 * but this is what the ABI wants and is needed to allow
126 * execution of PPC BSD programs.
128 #define ELF_PLAT_INIT(_r) \
130 unsigned long *pos = (unsigned long *)bprm->p, tmp = 1; \
131 _r->gpr[3] = bprm->argc; \
132 _r->gpr[4] = (unsigned long)++pos; \
133 for (; tmp != 0; pos++) \
135 _r->gpr[5] = (unsigned long)pos; \
139 * We need to put in some extra aux table entries to tell glibc what
140 * the cache block size is, so it can use the dcbz instruction safely.
142 #define AT_DCACHEBSIZE 19
143 #define AT_ICACHEBSIZE 20
144 #define AT_UCACHEBSIZE 21
145 /* A special ignored type value for PPC, for glibc compatibility. */
146 #define AT_IGNOREPPC 22
148 * The requirements here are:
149 * - keep the final alignment of sp (sp & 0xf)
150 * - make sure the 32-bit value at the first 16 byte aligned position of
151 * AUXV is greater than 16 for glibc compatibility.
152 * AT_IGNOREPPC is used for that.
153 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
154 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
156 #define DLINFO_ARCH_ITEMS 3
157 #define ARCH_DLINFO \
159 sp -= DLINFO_ARCH_ITEMS * 2; \
160 NEW_AUX_ENT(0, AT_DCACHEBSIZE, 0x20); \
161 NEW_AUX_ENT(1, AT_ICACHEBSIZE, 0x20); \
162 NEW_AUX_ENT(2, AT_UCACHEBSIZE, 0); \
164 * Now handle glibc compatibility. \
167 NEW_AUX_ENT(0, AT_IGNOREPPC, AT_IGNOREPPC); \
168 NEW_AUX_ENT(1, AT_IGNOREPPC, AT_IGNOREPPC); \
171 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
173 _regs->msr = 1 << MSR_PR; /* Set user mode */
174 _regs->gpr[1] = infop->start_stack;
175 _regs->nip = infop->entry;
178 #define USE_ELF_CORE_DUMP
179 #define ELF_EXEC_PAGESIZE 4096
186 * MAX_ARG_PAGES defines the number of pages allocated for arguments
187 * and envelope for the new program. 32 should suffice, this gives
188 * a maximum env+arg of 128kB w/4KB pages!
190 #define MAX_ARG_PAGES 32
193 * This structure is used to hold the arguments that are
194 * used when loading binaries.
196 struct linux_binprm {
198 unsigned long page[MAX_ARG_PAGES];
204 char * filename; /* Name of binary */
205 unsigned long loader, exec;
206 int dont_iput; /* binfmt handler has put inode */
211 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
212 unsigned int a_text; /* length of text, in bytes */
213 unsigned int a_data; /* length of data, in bytes */
214 unsigned int a_bss; /* length of uninitialized data area, in bytes */
215 unsigned int a_syms; /* length of symbol table data in file, in bytes */
216 unsigned int a_entry; /* start address */
217 unsigned int a_trsize; /* length of relocation info for text, in bytes */
218 unsigned int a_drsize; /* length of relocation info for data, in bytes */
222 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
228 /* max code+data+bss space allocated to elf interpreter */
229 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
231 /* max code+data+bss+brk space allocated to ET_DYN executables */
232 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
234 /* from personality.h */
236 /* Flags for bug emulation. These occupy the top three bytes. */
237 #define STICKY_TIMEOUTS 0x4000000
238 #define WHOLE_SECONDS 0x2000000
240 /* Personality types. These go in the low byte. Avoid using the top bit,
241 * it will conflict with error returns.
243 #define PER_MASK (0x00ff)
244 #define PER_LINUX (0x0000)
245 #define PER_SVR4 (0x0001 | STICKY_TIMEOUTS)
246 #define PER_SVR3 (0x0002 | STICKY_TIMEOUTS)
247 #define PER_SCOSVR3 (0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS)
248 #define PER_WYSEV386 (0x0004 | STICKY_TIMEOUTS)
249 #define PER_ISCR4 (0x0005 | STICKY_TIMEOUTS)
250 #define PER_BSD (0x0006)
251 #define PER_XENIX (0x0007 | STICKY_TIMEOUTS)
253 /* Necessary parameters */
256 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
257 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
258 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
260 #define INTERPRETER_NONE 0
261 #define INTERPRETER_AOUT 1
262 #define INTERPRETER_ELF 2
264 #define DLINFO_ITEMS 11
266 #define put_user(x,ptr) (void)(*(ptr) = (typeof(*ptr))(x))
267 #define get_user(ptr) (typeof(*ptr))(*(ptr))
269 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
274 static inline void memcpy_tofs(void * to, const void * from, unsigned long n)
279 extern unsigned long x86_stack_size;
281 static int load_aout_interp(void * exptr, int interp_fd);
284 static void bswap_ehdr(Elf32_Ehdr *ehdr)
286 bswap16s(&ehdr->e_type); /* Object file type */
287 bswap16s(&ehdr->e_machine); /* Architecture */
288 bswap32s(&ehdr->e_version); /* Object file version */
289 bswap32s(&ehdr->e_entry); /* Entry point virtual address */
290 bswap32s(&ehdr->e_phoff); /* Program header table file offset */
291 bswap32s(&ehdr->e_shoff); /* Section header table file offset */
292 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
293 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
294 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
295 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
296 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
297 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
298 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
301 static void bswap_phdr(Elf32_Phdr *phdr)
303 bswap32s(&phdr->p_type); /* Segment type */
304 bswap32s(&phdr->p_offset); /* Segment file offset */
305 bswap32s(&phdr->p_vaddr); /* Segment virtual address */
306 bswap32s(&phdr->p_paddr); /* Segment physical address */
307 bswap32s(&phdr->p_filesz); /* Segment size in file */
308 bswap32s(&phdr->p_memsz); /* Segment size in memory */
309 bswap32s(&phdr->p_flags); /* Segment flags */
310 bswap32s(&phdr->p_align); /* Segment alignment */
313 static void bswap_shdr(Elf32_Shdr *shdr)
315 bswap32s(&shdr->sh_name);
316 bswap32s(&shdr->sh_type);
317 bswap32s(&shdr->sh_flags);
318 bswap32s(&shdr->sh_addr);
319 bswap32s(&shdr->sh_offset);
320 bswap32s(&shdr->sh_size);
321 bswap32s(&shdr->sh_link);
322 bswap32s(&shdr->sh_info);
323 bswap32s(&shdr->sh_addralign);
324 bswap32s(&shdr->sh_entsize);
327 static void bswap_sym(Elf32_Sym *sym)
329 bswap32s(&sym->st_name);
330 bswap32s(&sym->st_value);
331 bswap32s(&sym->st_size);
332 bswap16s(&sym->st_shndx);
336 static void * get_free_page(void)
340 /* User-space version of kernel get_free_page. Returns a page-aligned
341 * page-sized chunk of memory.
343 retval = (void *)target_mmap(0, host_page_size, PROT_READ|PROT_WRITE,
344 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
346 if((long)retval == -1) {
347 perror("get_free_page");
355 static void free_page(void * pageaddr)
357 target_munmap((unsigned long)pageaddr, host_page_size);
361 * 'copy_string()' copies argument/envelope strings from user
362 * memory to free pages in kernel mem. These are in a format ready
363 * to be put directly into the top of new user memory.
366 static unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
369 char *tmp, *tmp1, *pag = NULL;
373 return 0; /* bullet-proofing */
376 if (!(tmp1 = tmp = get_user(argv+argc))) {
377 fprintf(stderr, "VFS: argc is wrong");
380 while (get_user(tmp++));
382 if (p < len) { /* this shouldn't happen - 128kB */
388 offset = p % TARGET_PAGE_SIZE;
389 pag = (char *) page[p/TARGET_PAGE_SIZE];
391 pag = (char *)get_free_page();
392 page[p/TARGET_PAGE_SIZE] = (unsigned long)pag;
397 if (len == 0 || offset == 0) {
398 *(pag + offset) = get_user(tmp);
401 int bytes_to_copy = (len > offset) ? offset : len;
402 tmp -= bytes_to_copy;
404 offset -= bytes_to_copy;
405 len -= bytes_to_copy;
406 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
413 static int in_group_p(gid_t g)
415 /* return TRUE if we're in the specified group, FALSE otherwise */
418 gid_t grouplist[NGROUPS];
420 ngroup = getgroups(NGROUPS, grouplist);
421 for(i = 0; i < ngroup; i++) {
422 if(grouplist[i] == g) {
429 static int count(char ** vec)
433 for(i = 0; *vec; i++) {
440 static int prepare_binprm(struct linux_binprm *bprm)
444 int retval, id_change;
446 if(fstat(bprm->fd, &st) < 0) {
451 if(!S_ISREG(mode)) { /* Must be regular file */
454 if(!(mode & 0111)) { /* Must have at least one execute bit set */
458 bprm->e_uid = geteuid();
459 bprm->e_gid = getegid();
464 bprm->e_uid = st.st_uid;
465 if(bprm->e_uid != geteuid()) {
472 * If setgid is set but no group execute bit then this
473 * is a candidate for mandatory locking, not a setgid
476 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
477 bprm->e_gid = st.st_gid;
478 if (!in_group_p(bprm->e_gid)) {
483 memset(bprm->buf, 0, sizeof(bprm->buf));
484 retval = lseek(bprm->fd, 0L, SEEK_SET);
486 retval = read(bprm->fd, bprm->buf, 128);
489 perror("prepare_binprm");
491 /* return(-errno); */
498 unsigned long setup_arg_pages(unsigned long p, struct linux_binprm * bprm,
499 struct image_info * info)
501 unsigned long stack_base, size, error;
504 /* Create enough stack to hold everything. If we don't use
505 * it for args, we'll use it for something else...
507 size = x86_stack_size;
508 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
509 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
510 error = target_mmap(0,
511 size + host_page_size,
512 PROT_READ | PROT_WRITE,
513 MAP_PRIVATE | MAP_ANONYMOUS,
519 /* we reserve one extra page at the top of the stack as guard */
520 target_mprotect(error + size, host_page_size, PROT_NONE);
522 stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
526 bprm->loader += stack_base;
528 bprm->exec += stack_base;
530 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
534 memcpy((void *)stack_base, (void *)bprm->page[i], TARGET_PAGE_SIZE);
535 free_page((void *)bprm->page[i]);
537 stack_base += TARGET_PAGE_SIZE;
542 static void set_brk(unsigned long start, unsigned long end)
544 /* page-align the start and end addresses... */
545 start = HOST_PAGE_ALIGN(start);
546 end = HOST_PAGE_ALIGN(end);
549 if(target_mmap(start, end - start,
550 PROT_READ | PROT_WRITE | PROT_EXEC,
551 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
552 perror("cannot mmap brk");
558 /* We need to explicitly zero any fractional pages after the data
559 section (i.e. bss). This would contain the junk from the file that
560 should not be in memory. */
561 static void padzero(unsigned long elf_bss)
566 /* XXX: this is really a hack : if the real host page size is
567 smaller than the target page size, some pages after the end
568 of the file may not be mapped. A better fix would be to
569 patch target_mmap(), but it is more complicated as the file
570 size must be known */
571 if (real_host_page_size < host_page_size) {
572 unsigned long end_addr, end_addr1;
573 end_addr1 = (elf_bss + real_host_page_size - 1) &
574 ~(real_host_page_size - 1);
575 end_addr = HOST_PAGE_ALIGN(elf_bss);
576 if (end_addr1 < end_addr) {
577 mmap((void *)end_addr1, end_addr - end_addr1,
578 PROT_READ|PROT_WRITE|PROT_EXEC,
579 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
583 nbyte = elf_bss & (host_page_size-1);
585 nbyte = host_page_size - nbyte;
586 fpnt = (char *) elf_bss;
593 static unsigned int * create_elf_tables(char *p, int argc, int envc,
594 struct elfhdr * exec,
595 unsigned long load_addr,
596 unsigned long load_bias,
597 unsigned long interp_load_addr, int ibcs,
598 struct image_info *info)
600 target_ulong *argv, *envp;
601 target_ulong *sp, *csp;
604 * Force 16 byte _final_ alignment here for generality.
606 sp = (unsigned int *) (~15UL & (unsigned long) p);
608 csp -= (DLINFO_ITEMS + 1) * 2;
609 #ifdef DLINFO_ARCH_ITEMS
610 csp -= DLINFO_ARCH_ITEMS*2;
614 csp -= (!ibcs ? 3 : 1); /* argc itself */
615 if ((unsigned long)csp & 15UL)
616 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
618 #define NEW_AUX_ENT(nr, id, val) \
619 put_user (tswapl(id), sp + (nr * 2)); \
620 put_user (tswapl(val), sp + (nr * 2 + 1))
622 NEW_AUX_ENT (0, AT_NULL, 0);
624 sp -= DLINFO_ITEMS*2;
625 NEW_AUX_ENT( 0, AT_PHDR, (target_ulong)(load_addr + exec->e_phoff));
626 NEW_AUX_ENT( 1, AT_PHENT, (target_ulong)(sizeof (struct elf_phdr)));
627 NEW_AUX_ENT( 2, AT_PHNUM, (target_ulong)(exec->e_phnum));
628 NEW_AUX_ENT( 3, AT_PAGESZ, (target_ulong)(TARGET_PAGE_SIZE));
629 NEW_AUX_ENT( 4, AT_BASE, (target_ulong)(interp_load_addr));
630 NEW_AUX_ENT( 5, AT_FLAGS, (target_ulong)0);
631 NEW_AUX_ENT( 6, AT_ENTRY, load_bias + exec->e_entry);
632 NEW_AUX_ENT( 7, AT_UID, (target_ulong) getuid());
633 NEW_AUX_ENT( 8, AT_EUID, (target_ulong) geteuid());
634 NEW_AUX_ENT( 9, AT_GID, (target_ulong) getgid());
635 NEW_AUX_ENT(11, AT_EGID, (target_ulong) getegid());
638 * ARCH_DLINFO must come last so platform specific code can enforce
639 * special alignment requirements on the AUXV if necessary (eg. PPC).
650 put_user(tswapl((target_ulong)envp),--sp);
651 put_user(tswapl((target_ulong)argv),--sp);
653 put_user(tswapl(argc),--sp);
654 info->arg_start = (unsigned int)((unsigned long)p & 0xffffffff);
656 put_user(tswapl((target_ulong)p),argv++);
657 while (get_user(p++)) /* nothing */ ;
660 info->arg_end = info->env_start = (unsigned int)((unsigned long)p & 0xffffffff);
662 put_user(tswapl((target_ulong)p),envp++);
663 while (get_user(p++)) /* nothing */ ;
666 info->env_end = (unsigned int)((unsigned long)p & 0xffffffff);
672 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
674 unsigned long *interp_load_addr)
676 struct elf_phdr *elf_phdata = NULL;
677 struct elf_phdr *eppnt;
678 unsigned long load_addr = 0;
679 int load_addr_set = 0;
681 unsigned long last_bss, elf_bss;
690 bswap_ehdr(interp_elf_ex);
692 /* First of all, some simple consistency checks */
693 if ((interp_elf_ex->e_type != ET_EXEC &&
694 interp_elf_ex->e_type != ET_DYN) ||
695 !elf_check_arch(interp_elf_ex->e_machine)) {
700 /* Now read in all of the header information */
702 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
705 elf_phdata = (struct elf_phdr *)
706 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
712 * If the size of this structure has changed, then punt, since
713 * we will be doing the wrong thing.
715 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
720 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
722 retval = read(interpreter_fd,
724 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
727 perror("load_elf_interp");
734 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
739 if (interp_elf_ex->e_type == ET_DYN) {
740 /* in order to avoid harcoding the interpreter load
741 address in qemu, we allocate a big enough memory zone */
742 error = target_mmap(0, INTERP_MAP_SIZE,
743 PROT_NONE, MAP_PRIVATE | MAP_ANON,
754 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
755 if (eppnt->p_type == PT_LOAD) {
756 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
758 unsigned long vaddr = 0;
761 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
762 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
763 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
764 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
765 elf_type |= MAP_FIXED;
766 vaddr = eppnt->p_vaddr;
768 error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
769 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
773 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
775 if (error > -1024UL) {
777 close(interpreter_fd);
782 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
788 * Find the end of the file mapping for this phdr, and keep
789 * track of the largest address we see for this.
791 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
792 if (k > elf_bss) elf_bss = k;
795 * Do the same thing for the memory mapping - between
796 * elf_bss and last_bss is the bss section.
798 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
799 if (k > last_bss) last_bss = k;
802 /* Now use mmap to map the library into memory. */
804 close(interpreter_fd);
807 * Now fill out the bss section. First pad the last page up
808 * to the page boundary, and then perform a mmap to make sure
809 * that there are zeromapped pages up to and including the last
813 elf_bss = TARGET_ELF_PAGESTART(elf_bss + host_page_size - 1); /* What we have mapped so far */
815 /* Map the last of the bss segment */
816 if (last_bss > elf_bss) {
817 target_mmap(elf_bss, last_bss-elf_bss,
818 PROT_READ|PROT_WRITE|PROT_EXEC,
819 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
823 *interp_load_addr = load_addr;
824 return ((unsigned long) interp_elf_ex->e_entry) + load_addr;
827 /* Best attempt to load symbols from this ELF object. */
828 static void load_symbols(struct elfhdr *hdr, int fd)
831 struct elf_shdr sechdr, symtab, strtab;
834 lseek(fd, hdr->e_shoff, SEEK_SET);
835 for (i = 0; i < hdr->e_shnum; i++) {
836 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
841 if (sechdr.sh_type == SHT_SYMTAB) {
843 lseek(fd, hdr->e_shoff
844 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
845 if (read(fd, &strtab, sizeof(strtab))
854 return; /* Shouldn't happen... */
857 /* Now know where the strtab and symtab are. Snarf them. */
858 disas_symtab = malloc(symtab.sh_size);
859 disas_strtab = strings = malloc(strtab.sh_size);
860 if (!disas_symtab || !disas_strtab)
863 lseek(fd, symtab.sh_offset, SEEK_SET);
864 if (read(fd, disas_symtab, symtab.sh_size) != symtab.sh_size)
868 for (i = 0; i < symtab.sh_size / sizeof(struct elf_sym); i++)
869 bswap_sym(disas_symtab + sizeof(struct elf_sym)*i);
872 lseek(fd, strtab.sh_offset, SEEK_SET);
873 if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
875 disas_num_syms = symtab.sh_size / sizeof(struct elf_sym);
878 static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
879 struct image_info * info)
881 struct elfhdr elf_ex;
882 struct elfhdr interp_elf_ex;
883 struct exec interp_ex;
884 int interpreter_fd = -1; /* avoid warning */
885 unsigned long load_addr, load_bias;
886 int load_addr_set = 0;
887 unsigned int interpreter_type = INTERPRETER_NONE;
888 unsigned char ibcs2_interpreter;
890 unsigned long mapped_addr;
891 struct elf_phdr * elf_ppnt;
892 struct elf_phdr *elf_phdata;
893 unsigned long elf_bss, k, elf_brk;
895 char * elf_interpreter;
896 unsigned long elf_entry, interp_load_addr = 0;
898 unsigned long start_code, end_code, end_data;
899 unsigned long elf_stack;
900 char passed_fileno[6];
902 ibcs2_interpreter = 0;
906 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
911 if (elf_ex.e_ident[0] != 0x7f ||
912 strncmp(&elf_ex.e_ident[1], "ELF",3) != 0) {
916 /* First of all, some simple consistency checks */
917 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
918 (! elf_check_arch(elf_ex.e_machine))) {
922 /* Now read in all of the header information */
923 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
924 if (elf_phdata == NULL) {
928 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
930 retval = read(bprm->fd, (char *) elf_phdata,
931 elf_ex.e_phentsize * elf_ex.e_phnum);
935 perror("load_elf_binary");
942 elf_ppnt = elf_phdata;
943 for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
944 bswap_phdr(elf_ppnt);
947 elf_ppnt = elf_phdata;
954 elf_interpreter = NULL;
959 for(i=0;i < elf_ex.e_phnum; i++) {
960 if (elf_ppnt->p_type == PT_INTERP) {
961 if ( elf_interpreter != NULL )
964 free(elf_interpreter);
969 /* This is the program interpreter used for
970 * shared libraries - for now assume that this
971 * is an a.out format binary
974 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
976 if (elf_interpreter == NULL) {
982 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
984 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
987 perror("load_elf_binary2");
991 /* If the program interpreter is one of these two,
992 then assume an iBCS2 image. Otherwise assume
993 a native linux image. */
995 /* JRP - Need to add X86 lib dir stuff here... */
997 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
998 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
999 ibcs2_interpreter = 1;
1003 printf("Using ELF interpreter %s\n", elf_interpreter);
1006 retval = open(path(elf_interpreter), O_RDONLY);
1008 interpreter_fd = retval;
1011 perror(elf_interpreter);
1013 /* retval = -errno; */
1018 retval = lseek(interpreter_fd, 0, SEEK_SET);
1020 retval = read(interpreter_fd,bprm->buf,128);
1024 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
1025 interp_elf_ex=*((struct elfhdr *) bprm->buf); /* elf exec-header */
1028 perror("load_elf_binary3");
1031 free(elf_interpreter);
1039 /* Some simple consistency checks for the interpreter */
1040 if (elf_interpreter){
1041 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1043 /* Now figure out which format our binary is */
1044 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1045 (N_MAGIC(interp_ex) != QMAGIC)) {
1046 interpreter_type = INTERPRETER_ELF;
1049 if (interp_elf_ex.e_ident[0] != 0x7f ||
1050 strncmp(&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
1051 interpreter_type &= ~INTERPRETER_ELF;
1054 if (!interpreter_type) {
1055 free(elf_interpreter);
1062 /* OK, we are done with that, now set up the arg stuff,
1063 and then start this sucker up */
1065 if (!bprm->sh_bang) {
1068 if (interpreter_type == INTERPRETER_AOUT) {
1069 sprintf(passed_fileno, "%d", bprm->fd);
1070 passed_p = passed_fileno;
1072 if (elf_interpreter) {
1073 bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p);
1078 if (elf_interpreter) {
1079 free(elf_interpreter);
1087 /* OK, This is the point of no return */
1090 info->start_mmap = (unsigned long)ELF_START_MMAP;
1092 elf_entry = (unsigned long) elf_ex.e_entry;
1094 /* Do this so that we can load the interpreter, if need be. We will
1095 change some of these later */
1097 bprm->p = setup_arg_pages(bprm->p, bprm, info);
1098 info->start_stack = bprm->p;
1100 /* Now we do a little grungy work by mmaping the ELF image into
1101 * the correct location in memory. At this point, we assume that
1102 * the image should be loaded at fixed address, not at a variable
1106 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
1109 unsigned long error;
1111 if (elf_ppnt->p_type != PT_LOAD)
1114 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1115 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1116 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1117 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1118 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1119 elf_flags |= MAP_FIXED;
1120 } else if (elf_ex.e_type == ET_DYN) {
1121 /* Try and get dynamic programs out of the way of the default mmap
1122 base, as well as whatever program they might try to exec. This
1123 is because the brk will follow the loader, and is not movable. */
1124 /* NOTE: for qemu, we do a big mmap to get enough space
1125 without harcoding any address */
1126 error = target_mmap(0, ET_DYN_MAP_SIZE,
1127 PROT_NONE, MAP_PRIVATE | MAP_ANON,
1133 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
1136 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1137 (elf_ppnt->p_filesz +
1138 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1140 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1142 (elf_ppnt->p_offset -
1143 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
1149 #ifdef LOW_ELF_STACK
1150 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1151 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
1154 if (!load_addr_set) {
1156 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1157 if (elf_ex.e_type == ET_DYN) {
1158 load_bias += error -
1159 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
1160 load_addr += load_bias;
1163 k = elf_ppnt->p_vaddr;
1166 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
1169 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1173 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1174 if (k > elf_brk) elf_brk = k;
1177 elf_entry += load_bias;
1178 elf_bss += load_bias;
1179 elf_brk += load_bias;
1180 start_code += load_bias;
1181 end_code += load_bias;
1182 // start_data += load_bias;
1183 end_data += load_bias;
1185 if (elf_interpreter) {
1186 if (interpreter_type & 1) {
1187 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1189 else if (interpreter_type & 2) {
1190 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1194 close(interpreter_fd);
1195 free(elf_interpreter);
1197 if (elf_entry == ~0UL) {
1198 printf("Unable to load interpreter\n");
1208 load_symbols(&elf_ex, bprm->fd);
1210 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1211 info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1213 #ifdef LOW_ELF_STACK
1214 info->start_stack = bprm->p = elf_stack - 4;
1216 bprm->p = (unsigned long)
1217 create_elf_tables((char *)bprm->p,
1221 load_addr, load_bias,
1223 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1225 if (interpreter_type == INTERPRETER_AOUT)
1226 info->arg_start += strlen(passed_fileno) + 1;
1227 info->start_brk = info->brk = elf_brk;
1228 info->end_code = end_code;
1229 info->start_code = start_code;
1230 info->end_data = end_data;
1231 info->start_stack = bprm->p;
1233 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1235 set_brk(elf_bss, elf_brk);
1240 printf("(start_brk) %x\n" , info->start_brk);
1241 printf("(end_code) %x\n" , info->end_code);
1242 printf("(start_code) %x\n" , info->start_code);
1243 printf("(end_data) %x\n" , info->end_data);
1244 printf("(start_stack) %x\n" , info->start_stack);
1245 printf("(brk) %x\n" , info->brk);
1248 if ( info->personality == PER_SVR4 )
1250 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1251 and some applications "depend" upon this behavior.
1252 Since we do not have the power to recompile these, we
1253 emulate the SVr4 behavior. Sigh. */
1254 mapped_addr = target_mmap(0, host_page_size, PROT_READ | PROT_EXEC,
1255 MAP_FIXED | MAP_PRIVATE, -1, 0);
1258 #ifdef ELF_PLAT_INIT
1260 * The ABI may specify that certain registers be set up in special
1261 * ways (on i386 %edx is the address of a DT_FINI function, for
1262 * example. This macro performs whatever initialization to
1263 * the regs structure is required.
1265 ELF_PLAT_INIT(regs);
1269 info->entry = elf_entry;
1276 int elf_exec(const char * filename, char ** argv, char ** envp,
1277 struct target_pt_regs * regs, struct image_info *infop)
1279 struct linux_binprm bprm;
1283 bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
1284 for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
1286 retval = open(filename, O_RDONLY);
1290 bprm.filename = (char *)filename;
1295 bprm.argc = count(argv);
1296 bprm.envc = count(envp);
1298 retval = prepare_binprm(&bprm);
1301 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p);
1303 bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p);
1304 bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p);
1311 retval = load_elf_binary(&bprm,regs,infop);
1314 /* success. Initialize important registers */
1315 init_thread(regs, infop);
1319 /* Something went wrong, return the inode and free the argument pages*/
1320 for (i=0 ; i<MAX_ARG_PAGES ; i++) {
1321 free_page((void *)bprm.page[i]);
1327 static int load_aout_interp(void * exptr, int interp_fd)
1329 printf("a.out interpreter not yet supported\n");