1 /* Code for loading Linux executables. Mostly linux kernel code. */
15 /* ??? This should really be somewhere else. */
16 abi_long
memcpy_to_target(abi_ulong dest
, const void *src
,
21 host_ptr
= lock_user(VERIFY_WRITE
, dest
, len
, 0);
23 return -TARGET_EFAULT
;
24 memcpy(host_ptr
, src
, len
);
25 unlock_user(host_ptr
, dest
, 1);
29 static int count(char ** vec
)
33 for(i
= 0; *vec
; i
++) {
40 static int prepare_binprm(struct linux_binprm
*bprm
)
46 if(fstat(bprm
->fd
, &st
) < 0) {
51 if(!S_ISREG(mode
)) { /* Must be regular file */
54 if(!(mode
& 0111)) { /* Must have at least one execute bit set */
58 bprm
->e_uid
= geteuid();
59 bprm
->e_gid
= getegid();
63 bprm
->e_uid
= st
.st_uid
;
68 * If setgid is set but no group execute bit then this
69 * is a candidate for mandatory locking, not a setgid
72 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
73 bprm
->e_gid
= st
.st_gid
;
76 retval
= read(bprm
->fd
, bprm
->buf
, BPRM_BUF_SIZE
);
78 perror("prepare_binprm");
81 if (retval
< BPRM_BUF_SIZE
) {
82 /* Make sure the rest of the loader won't read garbage. */
83 memset(bprm
->buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
88 /* Construct the envp and argv tables on the target stack. */
89 abi_ulong
loader_build_argptr(int envc
, int argc
, abi_ulong sp
,
90 abi_ulong stringp
, int push_ptr
)
92 CPUArchState
*env
= thread_cpu
->env_ptr
;
93 TaskState
*ts
= (TaskState
*)env
->opaque
;
94 int n
= sizeof(abi_ulong
);
100 sp
-= (argc
+ 1) * n
;
103 /* FIXME - handle put_user() failures */
105 put_user_ual(envp
, sp
);
107 put_user_ual(argv
, sp
);
110 /* FIXME - handle put_user() failures */
111 put_user_ual(argc
, sp
);
112 ts
->info
->arg_start
= stringp
;
114 /* FIXME - handle put_user() failures */
115 put_user_ual(stringp
, argv
);
117 stringp
+= target_strlen(stringp
) + 1;
119 ts
->info
->arg_end
= stringp
;
120 /* FIXME - handle put_user() failures */
121 put_user_ual(0, argv
);
123 /* FIXME - handle put_user() failures */
124 put_user_ual(stringp
, envp
);
126 stringp
+= target_strlen(stringp
) + 1;
128 /* FIXME - handle put_user() failures */
129 put_user_ual(0, envp
);
134 int loader_exec(const char * filename
, char ** argv
, char ** envp
,
135 struct target_pt_regs
* regs
, struct image_info
*infop
,
136 struct linux_binprm
*bprm
)
141 bprm
->p
= TARGET_PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(unsigned int);
142 memset(bprm
->page
, 0, sizeof(bprm
->page
));
143 retval
= open(filename
, O_RDONLY
);
148 bprm
->filename
= (char *)filename
;
149 bprm
->argc
= count(argv
);
151 bprm
->envc
= count(envp
);
154 retval
= prepare_binprm(bprm
);
157 if (bprm
->buf
[0] == 0x7f
158 && bprm
->buf
[1] == 'E'
159 && bprm
->buf
[2] == 'L'
160 && bprm
->buf
[3] == 'F') {
161 retval
= load_elf_binary(bprm
, regs
, infop
);
162 #if defined(TARGET_HAS_BFLT)
163 } else if (bprm
->buf
[0] == 'b'
164 && bprm
->buf
[1] == 'F'
165 && bprm
->buf
[2] == 'L'
166 && bprm
->buf
[3] == 'T') {
167 retval
= load_flt_binary(bprm
,regs
,infop
);
175 /* success. Initialize important registers */
176 do_init_thread(regs
, infop
);
180 /* Something went wrong, return the inode and free the argument pages*/
181 for (i
=0 ; i
<MAX_ARG_PAGES
; i
++) {
182 g_free(bprm
->page
[i
]);