meson: remove dead dictionary access
[qemu/ar7.git] / linux-user / linuxload.c
blob37f132be4aff09729b28cd9543ef7e949b623937
1 /* Code for loading Linux executables. Mostly linux kernel code. */
3 #include "qemu/osdep.h"
4 #include "qemu.h"
5 #include "user-internals.h"
6 #include "user-mmap.h"
7 #include "loader.h"
8 #include "qapi/error.h"
10 #define NGROUPS 32
12 /* ??? This should really be somewhere else. */
13 abi_long memcpy_to_target(abi_ulong dest, const void *src, unsigned long len)
15 void *host_ptr;
17 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
18 if (!host_ptr) {
19 return -TARGET_EFAULT;
21 memcpy(host_ptr, src, len);
22 unlock_user(host_ptr, dest, 1);
23 return 0;
26 static int count(char **vec)
28 int i;
30 for (i = 0; *vec; i++) {
31 vec++;
33 return i;
36 static int prepare_binprm(struct linux_binprm *bprm)
38 struct stat st;
39 int mode;
40 int retval;
42 if (fstat(bprm->src.fd, &st) < 0) {
43 return -errno;
46 mode = st.st_mode;
47 if (!S_ISREG(mode)) { /* Must be regular file */
48 return -EACCES;
50 if (!(mode & 0111)) { /* Must have at least one execute bit set */
51 return -EACCES;
54 bprm->e_uid = geteuid();
55 bprm->e_gid = getegid();
57 /* Set-uid? */
58 if (mode & S_ISUID) {
59 bprm->e_uid = st.st_uid;
62 /* Set-gid? */
64 * If setgid is set but no group execute bit then this
65 * is a candidate for mandatory locking, not a setgid
66 * executable.
68 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
69 bprm->e_gid = st.st_gid;
72 retval = read(bprm->src.fd, bprm->buf, BPRM_BUF_SIZE);
73 if (retval < 0) {
74 perror("prepare_binprm");
75 exit(-1);
77 if (retval < BPRM_BUF_SIZE) {
78 /* Make sure the rest of the loader won't read garbage. */
79 memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
82 bprm->src.cache = bprm->buf;
83 bprm->src.cache_size = retval;
85 return 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 = get_task_state(thread_cpu);
93 int n = sizeof(abi_ulong);
94 abi_ulong envp;
95 abi_ulong argv;
97 sp -= (envc + 1) * n;
98 envp = sp;
99 sp -= (argc + 1) * n;
100 argv = sp;
101 ts->info->envp = envp;
102 ts->info->envc = envc;
103 ts->info->argv = argv;
104 ts->info->argc = argc;
106 if (push_ptr) {
107 /* FIXME - handle put_user() failures */
108 sp -= n;
109 put_user_ual(envp, sp);
110 sp -= n;
111 put_user_ual(argv, sp);
114 sp -= n;
115 /* FIXME - handle put_user() failures */
116 put_user_ual(argc, sp);
118 ts->info->arg_strings = stringp;
119 while (argc-- > 0) {
120 /* FIXME - handle put_user() failures */
121 put_user_ual(stringp, argv);
122 argv += n;
123 stringp += target_strlen(stringp) + 1;
125 /* FIXME - handle put_user() failures */
126 put_user_ual(0, argv);
128 ts->info->env_strings = stringp;
129 while (envc-- > 0) {
130 /* FIXME - handle put_user() failures */
131 put_user_ual(stringp, envp);
132 envp += n;
133 stringp += target_strlen(stringp) + 1;
135 /* FIXME - handle put_user() failures */
136 put_user_ual(0, envp);
138 return sp;
141 int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
142 struct target_pt_regs *regs, struct image_info *infop,
143 struct linux_binprm *bprm)
145 int retval;
147 bprm->src.fd = fdexec;
148 bprm->filename = (char *)filename;
149 bprm->argc = count(argv);
150 bprm->argv = argv;
151 bprm->envc = count(envp);
152 bprm->envp = envp;
154 retval = prepare_binprm(bprm);
156 if (retval < 4) {
157 return -ENOEXEC;
159 if (bprm->buf[0] == 0x7f
160 && bprm->buf[1] == 'E'
161 && bprm->buf[2] == 'L'
162 && bprm->buf[3] == 'F') {
163 retval = load_elf_binary(bprm, infop);
164 #if defined(TARGET_HAS_BFLT)
165 } else if (bprm->buf[0] == 'b'
166 && bprm->buf[1] == 'F'
167 && bprm->buf[2] == 'L'
168 && bprm->buf[3] == 'T') {
169 retval = load_flt_binary(bprm, infop);
170 #endif
171 } else {
172 return -ENOEXEC;
174 if (retval < 0) {
175 return retval;
178 /* Success. Initialize important registers. */
179 do_init_thread(regs, infop);
180 return 0;
183 bool imgsrc_read(void *dst, off_t offset, size_t len,
184 const ImageSource *img, Error **errp)
186 ssize_t ret;
188 if (offset + len <= img->cache_size) {
189 memcpy(dst, img->cache + offset, len);
190 return true;
193 if (img->fd < 0) {
194 error_setg(errp, "read past end of buffer");
195 return false;
198 ret = pread(img->fd, dst, len, offset);
199 if (ret == len) {
200 return true;
202 if (ret < 0) {
203 error_setg_errno(errp, errno, "Error reading file header");
204 } else {
205 error_setg(errp, "Incomplete read of file header");
207 return false;
210 void *imgsrc_read_alloc(off_t offset, size_t len,
211 const ImageSource *img, Error **errp)
213 void *alloc = g_malloc(len);
214 bool ok = imgsrc_read(alloc, offset, len, img, errp);
216 if (!ok) {
217 g_free(alloc);
218 alloc = NULL;
220 return alloc;
223 abi_long imgsrc_mmap(abi_ulong start, abi_ulong len, int prot,
224 int flags, const ImageSource *src, abi_ulong offset)
226 const int prot_write = PROT_READ | PROT_WRITE;
227 abi_long ret;
228 void *haddr;
230 assert(flags == (MAP_PRIVATE | MAP_FIXED));
232 if (src->fd >= 0) {
233 return target_mmap(start, len, prot, flags, src->fd, offset);
237 * This case is for the vdso; we don't expect bad images.
238 * The mmap may extend beyond the end of the image, especially
239 * to the end of the page. Zero fill.
241 assert(offset < src->cache_size);
243 ret = target_mmap(start, len, prot_write, flags | MAP_ANON, -1, 0);
244 if (ret == -1) {
245 return ret;
248 haddr = lock_user(VERIFY_WRITE, start, len, 0);
249 assert(haddr != NULL);
250 if (offset + len <= src->cache_size) {
251 memcpy(haddr, src->cache + offset, len);
252 } else {
253 size_t rest = src->cache_size - offset;
254 memcpy(haddr, src->cache + offset, rest);
255 memset(haddr + rest, 0, len - rest);
257 unlock_user(haddr, start, len);
259 if (prot != prot_write) {
260 target_mprotect(start, len, prot);
263 return ret;