1 /* Code for loading BSD executables. Mostly linux kernel code. */
3 #include "qemu/osdep.h"
7 #define TARGET_NGROUPS 32
9 /* ??? This should really be somewhere else. */
10 abi_long memcpy_to_target(abi_ulong dest, const void *src,
15 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
17 return -TARGET_EFAULT;
18 memcpy(host_ptr, src, len);
19 unlock_user(host_ptr, dest, 1);
23 static int count(char ** vec)
27 for(i = 0; *vec; i++) {
34 static int prepare_binprm(struct linux_binprm *bprm)
40 if(fstat(bprm->fd, &st) < 0) {
45 if(!S_ISREG(mode)) { /* Must be regular file */
48 if(!(mode & 0111)) { /* Must have at least one execute bit set */
52 bprm->e_uid = geteuid();
53 bprm->e_gid = getegid();
57 bprm->e_uid = st.st_uid;
62 * If setgid is set but no group execute bit then this
63 * is a candidate for mandatory locking, not a setgid
66 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
67 bprm->e_gid = st.st_gid;
70 memset(bprm->buf, 0, sizeof(bprm->buf));
71 retval = lseek(bprm->fd, 0L, SEEK_SET);
73 retval = read(bprm->fd, bprm->buf, 128);
76 perror("prepare_binprm");
85 /* Construct the envp and argv tables on the target stack. */
86 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
87 abi_ulong stringp, int push_ptr)
89 int n = sizeof(abi_ulong);
98 /* FIXME - handle put_user() failures */
100 put_user_ual(envp, sp);
102 put_user_ual(argv, sp);
105 /* FIXME - handle put_user() failures */
106 put_user_ual(argc, sp);
109 /* FIXME - handle put_user() failures */
110 put_user_ual(stringp, argv);
112 stringp += target_strlen(stringp) + 1;
114 /* FIXME - handle put_user() failures */
115 put_user_ual(0, argv);
117 /* FIXME - handle put_user() failures */
118 put_user_ual(stringp, envp);
120 stringp += target_strlen(stringp) + 1;
122 /* FIXME - handle put_user() failures */
123 put_user_ual(0, envp);
128 int loader_exec(const char * filename, char ** argv, char ** envp,
129 struct target_pt_regs * regs, struct image_info *infop)
131 struct linux_binprm bprm;
135 bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
136 for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
138 retval = open(filename, O_RDONLY);
142 bprm.filename = (char *)filename;
143 bprm.argc = count(argv);
145 bprm.envc = count(envp);
148 retval = prepare_binprm(&bprm);
151 if (bprm.buf[0] == 0x7f
152 && bprm.buf[1] == 'E'
153 && bprm.buf[2] == 'L'
154 && bprm.buf[3] == 'F') {
155 retval = load_elf_binary(&bprm,regs,infop);
157 fprintf(stderr, "Unknown binary format\n");
163 /* success. Initialize important registers */
164 do_init_thread(regs, infop);
168 /* Something went wrong, return the inode and free the argument pages*/
169 for (i=0 ; i<MAX_ARG_PAGES ; i++) {
170 g_free(bprm.page[i]);