1 /* This is the Linux kernel elf-loading code, ported into user space */
18 #define ELF_START_MMAP 0x80000000
20 typedef uint32_t elf_greg_t
;
22 #define ELF_NGREG (sizeof (struct target_pt_regs) / sizeof(elf_greg_t))
23 typedef elf_greg_t elf_gregset_t
[ELF_NGREG
];
25 typedef struct user_i387_struct elf_fpregset_t
;
28 * This is used to ensure we don't load something for the wrong architecture.
30 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
33 * These are used to set parameters in the core dumps.
35 #define ELF_CLASS ELFCLASS32
36 #define ELF_DATA ELFDATA2LSB
37 #define ELF_ARCH EM_386
39 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
40 starts %edx contains a pointer to a function which might be
41 registered using `atexit'. This provides a mean for the
42 dynamic linker to call DT_FINI functions for shared libraries
43 that have been loaded before the code runs.
45 A value of 0 tells we have no such handler. */
46 #define ELF_PLAT_INIT(_r) _r->edx = 0
48 #define USE_ELF_CORE_DUMP
49 #define ELF_EXEC_PAGESIZE 4096
56 * MAX_ARG_PAGES defines the number of pages allocated for arguments
57 * and envelope for the new program. 32 should suffice, this gives
58 * a maximum env+arg of 128kB w/4KB pages!
60 #define MAX_ARG_PAGES 32
63 * This structure is used to hold the arguments that are
64 * used when loading binaries.
68 unsigned long page
[MAX_ARG_PAGES
];
74 char * filename
; /* Name of binary */
75 unsigned long loader
, exec
;
76 int dont_iput
; /* binfmt handler has put inode */
81 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
82 unsigned int a_text
; /* length of text, in bytes */
83 unsigned int a_data
; /* length of data, in bytes */
84 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
85 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
86 unsigned int a_entry
; /* start address */
87 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
88 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
92 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
98 /* max code+data+bss space allocated to elf interpreter */
99 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
101 /* max code+data+bss+brk space allocated to ET_DYN executables */
102 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
104 /* from personality.h */
106 /* Flags for bug emulation. These occupy the top three bytes. */
107 #define STICKY_TIMEOUTS 0x4000000
108 #define WHOLE_SECONDS 0x2000000
110 /* Personality types. These go in the low byte. Avoid using the top bit,
111 * it will conflict with error returns.
113 #define PER_MASK (0x00ff)
114 #define PER_LINUX (0x0000)
115 #define PER_SVR4 (0x0001 | STICKY_TIMEOUTS)
116 #define PER_SVR3 (0x0002 | STICKY_TIMEOUTS)
117 #define PER_SCOSVR3 (0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS)
118 #define PER_WYSEV386 (0x0004 | STICKY_TIMEOUTS)
119 #define PER_ISCR4 (0x0005 | STICKY_TIMEOUTS)
120 #define PER_BSD (0x0006)
121 #define PER_XENIX (0x0007 | STICKY_TIMEOUTS)
123 /* Necessary parameters */
126 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
127 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
128 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
130 #define INTERPRETER_NONE 0
131 #define INTERPRETER_AOUT 1
132 #define INTERPRETER_ELF 2
134 #define DLINFO_ITEMS 12
136 #define put_user(x,ptr) (void)(*(ptr) = (typeof(*ptr))(x))
137 #define get_user(ptr) (typeof(*ptr))(*(ptr))
139 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
144 static inline void memcpy_tofs(void * to
, const void * from
, unsigned long n
)
149 extern unsigned long x86_stack_size
;
151 static int load_aout_interp(void * exptr
, int interp_fd
);
154 static void bswap_ehdr(Elf32_Ehdr
*ehdr
)
156 bswap16s(&ehdr
->e_type
); /* Object file type */
157 bswap16s(&ehdr
->e_machine
); /* Architecture */
158 bswap32s(&ehdr
->e_version
); /* Object file version */
159 bswap32s(&ehdr
->e_entry
); /* Entry point virtual address */
160 bswap32s(&ehdr
->e_phoff
); /* Program header table file offset */
161 bswap32s(&ehdr
->e_shoff
); /* Section header table file offset */
162 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
163 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
164 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
165 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
166 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
167 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
168 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
171 static void bswap_phdr(Elf32_Phdr
*phdr
)
173 bswap32s(&phdr
->p_type
); /* Segment type */
174 bswap32s(&phdr
->p_offset
); /* Segment file offset */
175 bswap32s(&phdr
->p_vaddr
); /* Segment virtual address */
176 bswap32s(&phdr
->p_paddr
); /* Segment physical address */
177 bswap32s(&phdr
->p_filesz
); /* Segment size in file */
178 bswap32s(&phdr
->p_memsz
); /* Segment size in memory */
179 bswap32s(&phdr
->p_flags
); /* Segment flags */
180 bswap32s(&phdr
->p_align
); /* Segment alignment */
183 static void bswap_shdr(Elf32_Shdr
*shdr
)
185 bswap32s(&shdr
->sh_name
);
186 bswap32s(&shdr
->sh_type
);
187 bswap32s(&shdr
->sh_flags
);
188 bswap32s(&shdr
->sh_addr
);
189 bswap32s(&shdr
->sh_offset
);
190 bswap32s(&shdr
->sh_size
);
191 bswap32s(&shdr
->sh_link
);
192 bswap32s(&shdr
->sh_info
);
193 bswap32s(&shdr
->sh_addralign
);
194 bswap32s(&shdr
->sh_entsize
);
197 static void bswap_sym(Elf32_Sym
*sym
)
199 bswap32s(&sym
->st_name
);
200 bswap32s(&sym
->st_value
);
201 bswap32s(&sym
->st_size
);
202 bswap16s(&sym
->st_shndx
);
206 static void * get_free_page(void)
210 /* User-space version of kernel get_free_page. Returns a page-aligned
211 * page-sized chunk of memory.
213 retval
= (void *)target_mmap(0, host_page_size
, PROT_READ
|PROT_WRITE
,
214 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
216 if((long)retval
== -1) {
217 perror("get_free_page");
225 static void free_page(void * pageaddr
)
227 target_munmap((unsigned long)pageaddr
, host_page_size
);
231 * 'copy_string()' copies argument/envelope strings from user
232 * memory to free pages in kernel mem. These are in a format ready
233 * to be put directly into the top of new user memory.
236 static unsigned long copy_strings(int argc
,char ** argv
,unsigned long *page
,
239 char *tmp
, *tmp1
, *pag
= NULL
;
243 return 0; /* bullet-proofing */
246 if (!(tmp1
= tmp
= get_user(argv
+argc
))) {
247 fprintf(stderr
, "VFS: argc is wrong");
250 while (get_user(tmp
++));
252 if (p
< len
) { /* this shouldn't happen - 128kB */
258 offset
= p
% TARGET_PAGE_SIZE
;
259 if (!(pag
= (char *) page
[p
/TARGET_PAGE_SIZE
]) &&
260 !(pag
= (char *) page
[p
/TARGET_PAGE_SIZE
] =
261 (unsigned long *) get_free_page())) {
265 if (len
== 0 || offset
== 0) {
266 *(pag
+ offset
) = get_user(tmp
);
269 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
270 tmp
-= bytes_to_copy
;
272 offset
-= bytes_to_copy
;
273 len
-= bytes_to_copy
;
274 memcpy_fromfs(pag
+ offset
, tmp
, bytes_to_copy
+ 1);
281 static int in_group_p(gid_t g
)
283 /* return TRUE if we're in the specified group, FALSE otherwise */
286 gid_t grouplist
[NGROUPS
];
288 ngroup
= getgroups(NGROUPS
, grouplist
);
289 for(i
= 0; i
< ngroup
; i
++) {
290 if(grouplist
[i
] == g
) {
297 static int count(char ** vec
)
301 for(i
= 0; *vec
; i
++) {
308 static int prepare_binprm(struct linux_binprm
*bprm
)
312 int retval
, id_change
;
314 if(fstat(bprm
->fd
, &st
) < 0) {
319 if(!S_ISREG(mode
)) { /* Must be regular file */
322 if(!(mode
& 0111)) { /* Must have at least one execute bit set */
326 bprm
->e_uid
= geteuid();
327 bprm
->e_gid
= getegid();
332 bprm
->e_uid
= st
.st_uid
;
333 if(bprm
->e_uid
!= geteuid()) {
340 * If setgid is set but no group execute bit then this
341 * is a candidate for mandatory locking, not a setgid
344 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
345 bprm
->e_gid
= st
.st_gid
;
346 if (!in_group_p(bprm
->e_gid
)) {
351 memset(bprm
->buf
, 0, sizeof(bprm
->buf
));
352 retval
= lseek(bprm
->fd
, 0L, SEEK_SET
);
354 retval
= read(bprm
->fd
, bprm
->buf
, 128);
357 perror("prepare_binprm");
359 /* return(-errno); */
366 unsigned long setup_arg_pages(unsigned long p
, struct linux_binprm
* bprm
,
367 struct image_info
* info
)
369 unsigned long stack_base
, size
, error
;
372 /* Create enough stack to hold everything. If we don't use
373 * it for args, we'll use it for something else...
375 size
= x86_stack_size
;
376 if (size
< MAX_ARG_PAGES
*TARGET_PAGE_SIZE
)
377 size
= MAX_ARG_PAGES
*TARGET_PAGE_SIZE
;
378 error
= target_mmap(0,
379 size
+ host_page_size
,
380 PROT_READ
| PROT_WRITE
,
381 MAP_PRIVATE
| MAP_ANONYMOUS
,
387 /* we reserve one extra page at the top of the stack as guard */
388 target_mprotect(error
+ size
, host_page_size
, PROT_NONE
);
390 stack_base
= error
+ size
- MAX_ARG_PAGES
*TARGET_PAGE_SIZE
;
394 bprm
->loader
+= stack_base
;
396 bprm
->exec
+= stack_base
;
398 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
402 memcpy((void *)stack_base
, (void *)bprm
->page
[i
], TARGET_PAGE_SIZE
);
403 free_page((void *)bprm
->page
[i
]);
405 stack_base
+= TARGET_PAGE_SIZE
;
410 static void set_brk(unsigned long start
, unsigned long end
)
412 /* page-align the start and end addresses... */
413 start
= HOST_PAGE_ALIGN(start
);
414 end
= HOST_PAGE_ALIGN(end
);
417 if(target_mmap(start
, end
- start
,
418 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
419 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0) == -1) {
420 perror("cannot mmap brk");
426 /* We need to explicitly zero any fractional pages
427 after the data section (i.e. bss). This would
428 contain the junk from the file that should not
432 static void padzero(unsigned long elf_bss
)
437 nbyte
= elf_bss
& (host_page_size
-1); /* was TARGET_PAGE_SIZE - JRP */
439 nbyte
= host_page_size
- nbyte
;
440 fpnt
= (char *) elf_bss
;
447 static unsigned int * create_elf_tables(char *p
, int argc
, int envc
,
448 struct elfhdr
* exec
,
449 unsigned long load_addr
,
450 unsigned long load_bias
,
451 unsigned long interp_load_addr
, int ibcs
,
452 struct image_info
*info
)
454 target_ulong
*argv
, *envp
, *dlinfo
;
458 * Force 16 byte alignment here for generality.
460 sp
= (unsigned int *) (~15UL & (unsigned long) p
);
461 sp
-= exec
? DLINFO_ITEMS
*2 : 2;
468 put_user(tswapl((target_ulong
)envp
),--sp
);
469 put_user(tswapl((target_ulong
)argv
),--sp
);
472 #define NEW_AUX_ENT(id, val) \
473 put_user (tswapl(id), dlinfo++); \
474 put_user (tswapl(val), dlinfo++)
476 if (exec
) { /* Put this here for an ELF program interpreter */
477 NEW_AUX_ENT (AT_PHDR
, (target_ulong
)(load_addr
+ exec
->e_phoff
));
478 NEW_AUX_ENT (AT_PHENT
, (target_ulong
)(sizeof (struct elf_phdr
)));
479 NEW_AUX_ENT (AT_PHNUM
, (target_ulong
)(exec
->e_phnum
));
480 NEW_AUX_ENT (AT_PAGESZ
, (target_ulong
)(TARGET_PAGE_SIZE
));
481 NEW_AUX_ENT (AT_BASE
, (target_ulong
)(interp_load_addr
));
482 NEW_AUX_ENT (AT_FLAGS
, (target_ulong
)0);
483 NEW_AUX_ENT (AT_ENTRY
, load_bias
+ exec
->e_entry
);
484 NEW_AUX_ENT (AT_UID
, (target_ulong
) getuid());
485 NEW_AUX_ENT (AT_EUID
, (target_ulong
) geteuid());
486 NEW_AUX_ENT (AT_GID
, (target_ulong
) getgid());
487 NEW_AUX_ENT (AT_EGID
, (target_ulong
) getegid());
489 NEW_AUX_ENT (AT_NULL
, 0);
491 put_user(tswapl(argc
),--sp
);
492 info
->arg_start
= (unsigned int)((unsigned long)p
& 0xffffffff);
494 put_user(tswapl((target_ulong
)p
),argv
++);
495 while (get_user(p
++)) /* nothing */ ;
498 info
->arg_end
= info
->env_start
= (unsigned int)((unsigned long)p
& 0xffffffff);
500 put_user(tswapl((target_ulong
)p
),envp
++);
501 while (get_user(p
++)) /* nothing */ ;
504 info
->env_end
= (unsigned int)((unsigned long)p
& 0xffffffff);
510 static unsigned long load_elf_interp(struct elfhdr
* interp_elf_ex
,
512 unsigned long *interp_load_addr
)
514 struct elf_phdr
*elf_phdata
= NULL
;
515 struct elf_phdr
*eppnt
;
516 unsigned long load_addr
= 0;
517 int load_addr_set
= 0;
519 unsigned long last_bss
, elf_bss
;
528 bswap_ehdr(interp_elf_ex
);
530 /* First of all, some simple consistency checks */
531 if ((interp_elf_ex
->e_type
!= ET_EXEC
&&
532 interp_elf_ex
->e_type
!= ET_DYN
) ||
533 !elf_check_arch(interp_elf_ex
->e_machine
)) {
538 /* Now read in all of the header information */
540 if (sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
> TARGET_PAGE_SIZE
)
543 elf_phdata
= (struct elf_phdr
*)
544 malloc(sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
);
550 * If the size of this structure has changed, then punt, since
551 * we will be doing the wrong thing.
553 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
)) {
558 retval
= lseek(interpreter_fd
, interp_elf_ex
->e_phoff
, SEEK_SET
);
560 retval
= read(interpreter_fd
,
562 sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
);
565 perror("load_elf_interp");
572 for (i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
577 if (interp_elf_ex
->e_type
== ET_DYN
) {
578 /* in order to avoid harcoding the interpreter load
579 address in qemu, we allocate a big enough memory zone */
580 error
= target_mmap(0, INTERP_MAP_SIZE
,
581 PROT_NONE
, MAP_PRIVATE
| MAP_ANON
,
592 for(i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++)
593 if (eppnt
->p_type
== PT_LOAD
) {
594 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
596 unsigned long vaddr
= 0;
599 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
600 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
601 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
602 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
) {
603 elf_type
|= MAP_FIXED
;
604 vaddr
= eppnt
->p_vaddr
;
606 error
= target_mmap(load_addr
+TARGET_ELF_PAGESTART(vaddr
),
607 eppnt
->p_filesz
+ TARGET_ELF_PAGEOFFSET(eppnt
->p_vaddr
),
611 eppnt
->p_offset
- TARGET_ELF_PAGEOFFSET(eppnt
->p_vaddr
));
613 if (error
> -1024UL) {
615 close(interpreter_fd
);
620 if (!load_addr_set
&& interp_elf_ex
->e_type
== ET_DYN
) {
626 * Find the end of the file mapping for this phdr, and keep
627 * track of the largest address we see for this.
629 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
630 if (k
> elf_bss
) elf_bss
= k
;
633 * Do the same thing for the memory mapping - between
634 * elf_bss and last_bss is the bss section.
636 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
637 if (k
> last_bss
) last_bss
= k
;
640 /* Now use mmap to map the library into memory. */
642 close(interpreter_fd
);
645 * Now fill out the bss section. First pad the last page up
646 * to the page boundary, and then perform a mmap to make sure
647 * that there are zeromapped pages up to and including the last
651 elf_bss
= TARGET_ELF_PAGESTART(elf_bss
+ host_page_size
- 1); /* What we have mapped so far */
653 /* Map the last of the bss segment */
654 if (last_bss
> elf_bss
) {
655 target_mmap(elf_bss
, last_bss
-elf_bss
,
656 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
657 MAP_FIXED
|MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
661 *interp_load_addr
= load_addr
;
662 return ((unsigned long) interp_elf_ex
->e_entry
) + load_addr
;
665 /* Best attempt to load symbols from this ELF object. */
666 static void load_symbols(struct elfhdr
*hdr
, int fd
)
669 struct elf_shdr sechdr
, symtab
, strtab
;
672 lseek(fd
, hdr
->e_shoff
, SEEK_SET
);
673 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
674 if (read(fd
, &sechdr
, sizeof(sechdr
)) != sizeof(sechdr
))
679 if (sechdr
.sh_type
== SHT_SYMTAB
) {
681 lseek(fd
, hdr
->e_shoff
682 + sizeof(sechdr
) * sechdr
.sh_link
, SEEK_SET
);
683 if (read(fd
, &strtab
, sizeof(strtab
))
692 return; /* Shouldn't happen... */
695 /* Now know where the strtab and symtab are. Snarf them. */
696 disas_symtab
= malloc(symtab
.sh_size
);
697 disas_strtab
= strings
= malloc(strtab
.sh_size
);
698 if (!disas_symtab
|| !disas_strtab
)
701 lseek(fd
, symtab
.sh_offset
, SEEK_SET
);
702 if (read(fd
, disas_symtab
, symtab
.sh_size
) != symtab
.sh_size
)
706 for (i
= 0; i
< symtab
.sh_size
/ sizeof(struct elf_sym
); i
++)
707 bswap_sym(disas_symtab
+ sizeof(struct elf_sym
)*i
);
710 lseek(fd
, strtab
.sh_offset
, SEEK_SET
);
711 if (read(fd
, strings
, strtab
.sh_size
) != strtab
.sh_size
)
713 disas_num_syms
= symtab
.sh_size
/ sizeof(struct elf_sym
);
716 static int load_elf_binary(struct linux_binprm
* bprm
, struct target_pt_regs
* regs
,
717 struct image_info
* info
)
719 struct elfhdr elf_ex
;
720 struct elfhdr interp_elf_ex
;
721 struct exec interp_ex
;
722 int interpreter_fd
= -1; /* avoid warning */
723 unsigned long load_addr
, load_bias
;
724 int load_addr_set
= 0;
725 unsigned int interpreter_type
= INTERPRETER_NONE
;
726 unsigned char ibcs2_interpreter
;
728 unsigned long mapped_addr
;
729 struct elf_phdr
* elf_ppnt
;
730 struct elf_phdr
*elf_phdata
;
731 unsigned long elf_bss
, k
, elf_brk
;
733 char * elf_interpreter
;
734 unsigned long elf_entry
, interp_load_addr
= 0;
736 unsigned long start_code
, end_code
, end_data
;
737 unsigned long elf_stack
;
738 char passed_fileno
[6];
740 ibcs2_interpreter
= 0;
744 elf_ex
= *((struct elfhdr
*) bprm
->buf
); /* exec-header */
749 if (elf_ex
.e_ident
[0] != 0x7f ||
750 strncmp(&elf_ex
.e_ident
[1], "ELF",3) != 0) {
754 /* First of all, some simple consistency checks */
755 if ((elf_ex
.e_type
!= ET_EXEC
&& elf_ex
.e_type
!= ET_DYN
) ||
756 (! elf_check_arch(elf_ex
.e_machine
))) {
760 /* Now read in all of the header information */
762 elf_phdata
= (struct elf_phdr
*)malloc(elf_ex
.e_phentsize
*elf_ex
.e_phnum
);
763 if (elf_phdata
== NULL
) {
767 retval
= lseek(bprm
->fd
, elf_ex
.e_phoff
, SEEK_SET
);
769 retval
= read(bprm
->fd
, (char *) elf_phdata
,
770 elf_ex
.e_phentsize
* elf_ex
.e_phnum
);
774 perror("load_elf_binary");
781 elf_ppnt
= elf_phdata
;
782 for (i
=0; i
<elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
783 bswap_phdr(elf_ppnt
);
786 elf_ppnt
= elf_phdata
;
793 elf_interpreter
= NULL
;
798 for(i
=0;i
< elf_ex
.e_phnum
; i
++) {
799 if (elf_ppnt
->p_type
== PT_INTERP
) {
800 if ( elf_interpreter
!= NULL
)
803 free(elf_interpreter
);
808 /* This is the program interpreter used for
809 * shared libraries - for now assume that this
810 * is an a.out format binary
813 elf_interpreter
= (char *)malloc(elf_ppnt
->p_filesz
);
815 if (elf_interpreter
== NULL
) {
821 retval
= lseek(bprm
->fd
, elf_ppnt
->p_offset
, SEEK_SET
);
823 retval
= read(bprm
->fd
, elf_interpreter
, elf_ppnt
->p_filesz
);
826 perror("load_elf_binary2");
830 /* If the program interpreter is one of these two,
831 then assume an iBCS2 image. Otherwise assume
832 a native linux image. */
834 /* JRP - Need to add X86 lib dir stuff here... */
836 if (strcmp(elf_interpreter
,"/usr/lib/libc.so.1") == 0 ||
837 strcmp(elf_interpreter
,"/usr/lib/ld.so.1") == 0) {
838 ibcs2_interpreter
= 1;
842 printf("Using ELF interpreter %s\n", elf_interpreter
);
845 retval
= open(path(elf_interpreter
), O_RDONLY
);
847 interpreter_fd
= retval
;
850 perror(elf_interpreter
);
852 /* retval = -errno; */
857 retval
= lseek(interpreter_fd
, 0, SEEK_SET
);
859 retval
= read(interpreter_fd
,bprm
->buf
,128);
863 interp_ex
= *((struct exec
*) bprm
->buf
); /* aout exec-header */
864 interp_elf_ex
=*((struct elfhdr
*) bprm
->buf
); /* elf exec-header */
867 perror("load_elf_binary3");
870 free(elf_interpreter
);
878 /* Some simple consistency checks for the interpreter */
879 if (elf_interpreter
){
880 interpreter_type
= INTERPRETER_ELF
| INTERPRETER_AOUT
;
882 /* Now figure out which format our binary is */
883 if ((N_MAGIC(interp_ex
) != OMAGIC
) && (N_MAGIC(interp_ex
) != ZMAGIC
) &&
884 (N_MAGIC(interp_ex
) != QMAGIC
)) {
885 interpreter_type
= INTERPRETER_ELF
;
888 if (interp_elf_ex
.e_ident
[0] != 0x7f ||
889 strncmp(&interp_elf_ex
.e_ident
[1], "ELF",3) != 0) {
890 interpreter_type
&= ~INTERPRETER_ELF
;
893 if (!interpreter_type
) {
894 free(elf_interpreter
);
901 /* OK, we are done with that, now set up the arg stuff,
902 and then start this sucker up */
904 if (!bprm
->sh_bang
) {
907 if (interpreter_type
== INTERPRETER_AOUT
) {
908 sprintf(passed_fileno
, "%d", bprm
->fd
);
909 passed_p
= passed_fileno
;
911 if (elf_interpreter
) {
912 bprm
->p
= copy_strings(1,&passed_p
,bprm
->page
,bprm
->p
);
917 if (elf_interpreter
) {
918 free(elf_interpreter
);
926 /* OK, This is the point of no return */
929 info
->start_mmap
= (unsigned long)ELF_START_MMAP
;
931 elf_entry
= (unsigned long) elf_ex
.e_entry
;
933 /* Do this so that we can load the interpreter, if need be. We will
934 change some of these later */
936 bprm
->p
= setup_arg_pages(bprm
->p
, bprm
, info
);
937 info
->start_stack
= bprm
->p
;
939 /* Now we do a little grungy work by mmaping the ELF image into
940 * the correct location in memory. At this point, we assume that
941 * the image should be loaded at fixed address, not at a variable
945 for(i
= 0, elf_ppnt
= elf_phdata
; i
< elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
950 if (elf_ppnt
->p_type
!= PT_LOAD
)
953 if (elf_ppnt
->p_flags
& PF_R
) elf_prot
|= PROT_READ
;
954 if (elf_ppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
955 if (elf_ppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
956 elf_flags
= MAP_PRIVATE
| MAP_DENYWRITE
;
957 if (elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
958 elf_flags
|= MAP_FIXED
;
959 } else if (elf_ex
.e_type
== ET_DYN
) {
960 /* Try and get dynamic programs out of the way of the default mmap
961 base, as well as whatever program they might try to exec. This
962 is because the brk will follow the loader, and is not movable. */
963 /* NOTE: for qemu, we do a big mmap to get enough space
964 without harcoding any address */
965 error
= target_mmap(0, ET_DYN_MAP_SIZE
,
966 PROT_NONE
, MAP_PRIVATE
| MAP_ANON
,
972 load_bias
= TARGET_ELF_PAGESTART(error
- elf_ppnt
->p_vaddr
);
975 error
= target_mmap(TARGET_ELF_PAGESTART(load_bias
+ elf_ppnt
->p_vaddr
),
976 (elf_ppnt
->p_filesz
+
977 TARGET_ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)),
979 (MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
),
981 (elf_ppnt
->p_offset
-
982 TARGET_ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)));
989 if (TARGET_ELF_PAGESTART(elf_ppnt
->p_vaddr
) < elf_stack
)
990 elf_stack
= TARGET_ELF_PAGESTART(elf_ppnt
->p_vaddr
);
993 if (!load_addr_set
) {
995 load_addr
= elf_ppnt
->p_vaddr
- elf_ppnt
->p_offset
;
996 if (elf_ex
.e_type
== ET_DYN
) {
998 TARGET_ELF_PAGESTART(load_bias
+ elf_ppnt
->p_vaddr
);
999 load_addr
+= load_bias
;
1002 k
= elf_ppnt
->p_vaddr
;
1005 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
1008 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
1012 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
1013 if (k
> elf_brk
) elf_brk
= k
;
1016 elf_entry
+= load_bias
;
1017 elf_bss
+= load_bias
;
1018 elf_brk
+= load_bias
;
1019 start_code
+= load_bias
;
1020 end_code
+= load_bias
;
1021 // start_data += load_bias;
1022 end_data
+= load_bias
;
1024 if (elf_interpreter
) {
1025 if (interpreter_type
& 1) {
1026 elf_entry
= load_aout_interp(&interp_ex
, interpreter_fd
);
1028 else if (interpreter_type
& 2) {
1029 elf_entry
= load_elf_interp(&interp_elf_ex
, interpreter_fd
,
1033 close(interpreter_fd
);
1034 free(elf_interpreter
);
1036 if (elf_entry
== ~0UL) {
1037 printf("Unable to load interpreter\n");
1047 load_symbols(&elf_ex
, bprm
->fd
);
1049 if (interpreter_type
!= INTERPRETER_AOUT
) close(bprm
->fd
);
1050 info
->personality
= (ibcs2_interpreter
? PER_SVR4
: PER_LINUX
);
1052 #ifdef LOW_ELF_STACK
1053 info
->start_stack
= bprm
->p
= elf_stack
- 4;
1055 bprm
->p
= (unsigned long)
1056 create_elf_tables((char *)bprm
->p
,
1059 (interpreter_type
== INTERPRETER_ELF
? &elf_ex
: NULL
),
1060 load_addr
, load_bias
,
1062 (interpreter_type
== INTERPRETER_AOUT
? 0 : 1),
1064 if (interpreter_type
== INTERPRETER_AOUT
)
1065 info
->arg_start
+= strlen(passed_fileno
) + 1;
1066 info
->start_brk
= info
->brk
= elf_brk
;
1067 info
->end_code
= end_code
;
1068 info
->start_code
= start_code
;
1069 info
->end_data
= end_data
;
1070 info
->start_stack
= bprm
->p
;
1072 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1074 set_brk(elf_bss
, elf_brk
);
1079 printf("(start_brk) %x\n" , info
->start_brk
);
1080 printf("(end_code) %x\n" , info
->end_code
);
1081 printf("(start_code) %x\n" , info
->start_code
);
1082 printf("(end_data) %x\n" , info
->end_data
);
1083 printf("(start_stack) %x\n" , info
->start_stack
);
1084 printf("(brk) %x\n" , info
->brk
);
1087 if ( info
->personality
== PER_SVR4
)
1089 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1090 and some applications "depend" upon this behavior.
1091 Since we do not have the power to recompile these, we
1092 emulate the SVr4 behavior. Sigh. */
1093 mapped_addr
= target_mmap(0, host_page_size
, PROT_READ
| PROT_EXEC
,
1094 MAP_FIXED
| MAP_PRIVATE
, -1, 0);
1097 #ifdef ELF_PLAT_INIT
1099 * The ABI may specify that certain registers be set up in special
1100 * ways (on i386 %edx is the address of a DT_FINI function, for
1101 * example. This macro performs whatever initialization to
1102 * the regs structure is required.
1104 ELF_PLAT_INIT(regs
);
1108 info
->entry
= elf_entry
;
1115 int elf_exec(const char * filename
, char ** argv
, char ** envp
,
1116 struct target_pt_regs
* regs
, struct image_info
*infop
)
1118 struct linux_binprm bprm
;
1122 bprm
.p
= TARGET_PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(unsigned int);
1123 for (i
=0 ; i
<MAX_ARG_PAGES
; i
++) /* clear page-table */
1125 retval
= open(filename
, O_RDONLY
);
1129 /* return retval; */
1134 bprm
.filename
= (char *)filename
;
1139 bprm
.argc
= count(argv
);
1140 bprm
.envc
= count(envp
);
1142 retval
= prepare_binprm(&bprm
);
1145 bprm
.p
= copy_strings(1, &bprm
.filename
, bprm
.page
, bprm
.p
);
1147 bprm
.p
= copy_strings(bprm
.envc
,envp
,bprm
.page
,bprm
.p
);
1148 bprm
.p
= copy_strings(bprm
.argc
,argv
,bprm
.page
,bprm
.p
);
1155 retval
= load_elf_binary(&bprm
,regs
,infop
);
1158 /* success. Initialize important registers */
1159 regs
->esp
= infop
->start_stack
;
1160 regs
->eip
= infop
->entry
;
1164 /* Something went wrong, return the inode and free the argument pages*/
1165 for (i
=0 ; i
<MAX_ARG_PAGES
; i
++) {
1166 free_page((void *)bprm
.page
[i
]);
1172 static int load_aout_interp(void * exptr
, int interp_fd
)
1174 printf("a.out interpreter not yet supported\n");