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 TaskState *ts = (TaskState *)thread_cpu->opaque;
93 int n = sizeof(abi_ulong);
102 /* FIXME - handle put_user() failures */
104 put_user_ual(envp, sp);
106 put_user_ual(argv, sp);
109 /* FIXME - handle put_user() failures */
110 put_user_ual(argc, sp);
111 ts->info->arg_start = stringp;
113 /* FIXME - handle put_user() failures */
114 put_user_ual(stringp, argv);
116 stringp += target_strlen(stringp) + 1;
118 ts->info->arg_end = stringp;
119 /* FIXME - handle put_user() failures */
120 put_user_ual(0, argv);
122 /* FIXME - handle put_user() failures */
123 put_user_ual(stringp, envp);
125 stringp += target_strlen(stringp) + 1;
127 /* FIXME - handle put_user() failures */
128 put_user_ual(0, envp);
133 int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
134 struct target_pt_regs * regs, struct image_info *infop,
135 struct linux_binprm *bprm)
140 bprm->filename = (char *)filename;
141 bprm->argc = count(argv);
143 bprm->envc = count(envp);
146 retval = prepare_binprm(bprm);
149 if (bprm->buf[0] == 0x7f
150 && bprm->buf[1] == 'E'
151 && bprm->buf[2] == 'L'
152 && bprm->buf[3] == 'F') {
153 retval = load_elf_binary(bprm, infop);
154 #if defined(TARGET_HAS_BFLT)
155 } else if (bprm->buf[0] == 'b'
156 && bprm->buf[1] == 'F'
157 && bprm->buf[2] == 'L'
158 && bprm->buf[3] == 'T') {
159 retval = load_flt_binary(bprm, infop);
167 /* success. Initialize important registers */
168 do_init_thread(regs, infop);