1 /* Code for loading Linux executables. Mostly linux kernel code. */
3 #include "qemu/osdep.h"
13 /* ??? This should really be somewhere else. */
14 abi_long
memcpy_to_target(abi_ulong dest
, const void *src
,
19 host_ptr
= lock_user(VERIFY_WRITE
, dest
, len
, 0);
21 return -TARGET_EFAULT
;
22 memcpy(host_ptr
, src
, len
);
23 unlock_user(host_ptr
, dest
, 1);
27 static int count(char ** vec
)
31 for(i
= 0; *vec
; i
++) {
38 static int prepare_binprm(struct linux_binprm
*bprm
)
44 if(fstat(bprm
->fd
, &st
) < 0) {
49 if(!S_ISREG(mode
)) { /* Must be regular file */
52 if(!(mode
& 0111)) { /* Must have at least one execute bit set */
56 bprm
->e_uid
= geteuid();
57 bprm
->e_gid
= getegid();
61 bprm
->e_uid
= st
.st_uid
;
66 * If setgid is set but no group execute bit then this
67 * is a candidate for mandatory locking, not a setgid
70 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
71 bprm
->e_gid
= st
.st_gid
;
74 retval
= read(bprm
->fd
, bprm
->buf
, BPRM_BUF_SIZE
);
76 perror("prepare_binprm");
79 if (retval
< BPRM_BUF_SIZE
) {
80 /* Make sure the rest of the loader won't read garbage. */
81 memset(bprm
->buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
86 /* Construct the envp and argv tables on the target stack. */
87 abi_ulong
loader_build_argptr(int envc
, int argc
, abi_ulong sp
,
88 abi_ulong stringp
, int push_ptr
)
90 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
91 int n
= sizeof(abi_ulong
);
100 /* FIXME - handle put_user() failures */
102 put_user_ual(envp
, sp
);
104 put_user_ual(argv
, sp
);
107 /* FIXME - handle put_user() failures */
108 put_user_ual(argc
, sp
);
109 ts
->info
->arg_start
= stringp
;
111 /* FIXME - handle put_user() failures */
112 put_user_ual(stringp
, argv
);
114 stringp
+= target_strlen(stringp
) + 1;
116 ts
->info
->arg_end
= stringp
;
117 /* FIXME - handle put_user() failures */
118 put_user_ual(0, argv
);
120 /* FIXME - handle put_user() failures */
121 put_user_ual(stringp
, envp
);
123 stringp
+= target_strlen(stringp
) + 1;
125 /* FIXME - handle put_user() failures */
126 put_user_ual(0, envp
);
131 int loader_exec(int fdexec
, const char *filename
, char **argv
, char **envp
,
132 struct target_pt_regs
* regs
, struct image_info
*infop
,
133 struct linux_binprm
*bprm
)
138 bprm
->filename
= (char *)filename
;
139 bprm
->argc
= count(argv
);
141 bprm
->envc
= count(envp
);
144 retval
= prepare_binprm(bprm
);
147 if (bprm
->buf
[0] == 0x7f
148 && bprm
->buf
[1] == 'E'
149 && bprm
->buf
[2] == 'L'
150 && bprm
->buf
[3] == 'F') {
151 retval
= load_elf_binary(bprm
, infop
);
152 #if defined(TARGET_HAS_BFLT)
153 } else if (bprm
->buf
[0] == 'b'
154 && bprm
->buf
[1] == 'F'
155 && bprm
->buf
[2] == 'L'
156 && bprm
->buf
[3] == 'T') {
157 retval
= load_flt_binary(bprm
, infop
);
165 /* success. Initialize important registers */
166 do_init_thread(regs
, infop
);