1 /* Code for loading Linux executables. Mostly linux kernel code. */
3 #include "qemu/osdep.h"
8 /* ??? This should really be somewhere else. */
9 abi_long
memcpy_to_target(abi_ulong dest
, const void *src
, unsigned long len
)
13 host_ptr
= lock_user(VERIFY_WRITE
, dest
, len
, 0);
15 return -TARGET_EFAULT
;
17 memcpy(host_ptr
, src
, len
);
18 unlock_user(host_ptr
, dest
, 1);
22 static int count(char **vec
)
26 for (i
= 0; *vec
; i
++) {
32 static int prepare_binprm(struct linux_binprm
*bprm
)
38 if (fstat(bprm
->fd
, &st
) < 0) {
43 if (!S_ISREG(mode
)) { /* Must be regular file */
46 if (!(mode
& 0111)) { /* Must have at least one execute bit set */
50 bprm
->e_uid
= geteuid();
51 bprm
->e_gid
= getegid();
55 bprm
->e_uid
= st
.st_uid
;
60 * If setgid is set but no group execute bit then this
61 * is a candidate for mandatory locking, not a setgid
64 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
65 bprm
->e_gid
= st
.st_gid
;
68 retval
= read(bprm
->fd
, bprm
->buf
, BPRM_BUF_SIZE
);
70 perror("prepare_binprm");
73 if (retval
< BPRM_BUF_SIZE
) {
74 /* Make sure the rest of the loader won't read garbage. */
75 memset(bprm
->buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
80 /* Construct the envp and argv tables on the target stack. */
81 abi_ulong
loader_build_argptr(int envc
, int argc
, abi_ulong sp
,
82 abi_ulong stringp
, int push_ptr
)
84 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
85 int n
= sizeof(abi_ulong
);
94 /* FIXME - handle put_user() failures */
96 put_user_ual(envp
, sp
);
98 put_user_ual(argv
, sp
);
101 /* FIXME - handle put_user() failures */
102 put_user_ual(argc
, sp
);
103 ts
->info
->arg_start
= stringp
;
105 /* FIXME - handle put_user() failures */
106 put_user_ual(stringp
, argv
);
108 stringp
+= target_strlen(stringp
) + 1;
110 ts
->info
->arg_end
= stringp
;
111 /* FIXME - handle put_user() failures */
112 put_user_ual(0, argv
);
114 /* FIXME - handle put_user() failures */
115 put_user_ual(stringp
, envp
);
117 stringp
+= target_strlen(stringp
) + 1;
119 /* FIXME - handle put_user() failures */
120 put_user_ual(0, envp
);
125 int loader_exec(int fdexec
, const char *filename
, char **argv
, char **envp
,
126 struct target_pt_regs
*regs
, struct image_info
*infop
,
127 struct linux_binprm
*bprm
)
132 bprm
->filename
= (char *)filename
;
133 bprm
->argc
= count(argv
);
135 bprm
->envc
= count(envp
);
138 retval
= prepare_binprm(bprm
);
141 if (bprm
->buf
[0] == 0x7f
142 && bprm
->buf
[1] == 'E'
143 && bprm
->buf
[2] == 'L'
144 && bprm
->buf
[3] == 'F') {
145 retval
= load_elf_binary(bprm
, infop
);
146 #if defined(TARGET_HAS_BFLT)
147 } else if (bprm
->buf
[0] == 'b'
148 && bprm
->buf
[1] == 'F'
149 && bprm
->buf
[2] == 'L'
150 && bprm
->buf
[3] == 'T') {
151 retval
= load_flt_binary(bprm
, infop
);
159 /* success. Initialize important registers */
160 do_init_thread(regs
, infop
);