2 * Load BSD executables.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "qemu/osdep.h"
22 #define TARGET_NGROUPS 32
24 /* ??? This should really be somewhere else. */
25 abi_long memcpy_to_target(abi_ulong dest, const void *src,
30 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
32 return -TARGET_EFAULT;
34 memcpy(host_ptr, src, len);
35 unlock_user(host_ptr, dest, 1);
39 static int count(char **vec)
43 for (i = 0; *vec; i++) {
50 static int prepare_binprm(struct bsd_binprm *bprm)
56 if (fstat(bprm->fd, &st) < 0) {
61 if (!S_ISREG(mode)) { /* Must be regular file */
64 if (!(mode & 0111)) { /* Must have at least one execute bit set */
68 bprm->e_uid = geteuid();
69 bprm->e_gid = getegid();
73 bprm->e_uid = st.st_uid;
78 * If setgid is set but no group execute bit then this
79 * is a candidate for mandatory locking, not a setgid
82 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
83 bprm->e_gid = st.st_gid;
86 memset(bprm->buf, 0, sizeof(bprm->buf));
87 retval = lseek(bprm->fd, 0L, SEEK_SET);
89 retval = read(bprm->fd, bprm->buf, 128);
92 perror("prepare_binprm");
99 /* Construct the envp and argv tables on the target stack. */
100 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
101 abi_ulong stringp, int push_ptr)
103 int n = sizeof(abi_ulong);
107 sp -= (envc + 1) * n;
109 sp -= (argc + 1) * n;
112 /* FIXME - handle put_user() failures */
114 put_user_ual(envp, sp);
116 put_user_ual(argv, sp);
119 /* FIXME - handle put_user() failures */
120 put_user_ual(argc, sp);
123 /* FIXME - handle put_user() failures */
124 put_user_ual(stringp, argv);
126 stringp += target_strlen(stringp) + 1;
128 /* FIXME - handle put_user() failures */
129 put_user_ual(0, argv);
131 /* FIXME - handle put_user() failures */
132 put_user_ual(stringp, envp);
134 stringp += target_strlen(stringp) + 1;
136 /* FIXME - handle put_user() failures */
137 put_user_ual(0, envp);
142 int loader_exec(const char *filename, char **argv, char **envp,
143 struct target_pt_regs *regs, struct image_info *infop,
144 struct bsd_binprm *bprm)
148 bprm->p = TARGET_PAGE_SIZE * MAX_ARG_PAGES;
149 for (i = 0; i < MAX_ARG_PAGES; i++) { /* clear page-table */
150 bprm->page[i] = NULL;
152 retval = open(filename, O_RDONLY);
158 bprm->filename = (char *)filename;
159 bprm->argc = count(argv);
161 bprm->envc = count(envp);
164 retval = prepare_binprm(bprm);
167 if (bprm->buf[0] == 0x7f
168 && bprm->buf[1] == 'E'
169 && bprm->buf[2] == 'L'
170 && bprm->buf[3] == 'F') {
171 retval = load_elf_binary(bprm, regs, infop);
173 fprintf(stderr, "Unknown binary format\n");
179 /* success. Initialize important registers */
180 do_init_thread(regs, infop);
184 /* Something went wrong, return the inode and free the argument pages*/
185 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
186 g_free(bprm->page[i]);