Import 2.3.99pre7-4
[davej-history.git] / fs / binfmt_aout.c
blob4abff232c887feb21264ee787202c38dd0785305
1 /*
2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
5 */
7 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/mman.h>
13 #include <linux/a.out.h>
14 #include <linux/errno.h>
15 #include <linux/signal.h>
16 #include <linux/string.h>
17 #include <linux/fs.h>
18 #include <linux/file.h>
19 #include <linux/stat.h>
20 #include <linux/fcntl.h>
21 #include <linux/ptrace.h>
22 #include <linux/user.h>
23 #include <linux/malloc.h>
24 #include <linux/binfmts.h>
25 #include <linux/personality.h>
26 #include <linux/init.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgalloc.h>
32 static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
33 static int load_aout_library(struct file*);
34 static int aout_core_dump(long signr, struct pt_regs * regs, struct file *file);
36 extern void dump_thread(struct pt_regs *, struct user *);
38 static struct linux_binfmt aout_format = {
39 NULL, THIS_MODULE, load_aout_binary, load_aout_library, aout_core_dump, PAGE_SIZE
42 static void set_brk(unsigned long start, unsigned long end)
44 start = PAGE_ALIGN(start);
45 end = PAGE_ALIGN(end);
46 if (end <= start)
47 return;
48 do_brk(start, end - start);
52 * These are the only things you should do on a core-file: use only these
53 * macros to write out all the necessary info.
56 static int dump_write(struct file *file, const void *addr, int nr)
58 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
61 #define DUMP_WRITE(addr, nr) \
62 if (!dump_write(file, (void *)(addr), (nr))) \
63 goto end_coredump;
65 #define DUMP_SEEK(offset) \
66 if (file->f_op->llseek) { \
67 if (file->f_op->llseek(file,(offset),0) != (offset)) \
68 goto end_coredump; \
69 } else file->f_pos = (offset)
72 * Routine writes a core dump image in the current directory.
73 * Currently only a stub-function.
75 * Note that setuid/setgid files won't make a core-dump if the uid/gid
76 * changed due to the set[u|g]id. It's enforced by the "current->dumpable"
77 * field, which also makes sure the core-dumps won't be recursive if the
78 * dumping of the process results in another error..
81 static int aout_core_dump(long signr, struct pt_regs * regs, struct file *file)
83 mm_segment_t fs;
84 int has_dumped = 0;
85 unsigned long dump_start, dump_size;
86 struct user dump;
87 #if defined(__alpha__)
88 # define START_DATA(u) (u.start_data)
89 #elif defined(__sparc__)
90 # define START_DATA(u) (u.u_tsize)
91 #elif defined(__i386__) || defined(__mc68000__)
92 # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
93 #endif
94 #ifdef __sparc__
95 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
96 #else
97 # define START_STACK(u) (u.start_stack)
98 #endif
100 fs = get_fs();
101 set_fs(KERNEL_DS);
102 has_dumped = 1;
103 current->flags |= PF_DUMPCORE;
104 strncpy(dump.u_comm, current->comm, sizeof(current->comm));
105 #ifndef __sparc__
106 dump.u_ar0 = (void *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
107 #endif
108 dump.signal = signr;
109 dump_thread(regs, &dump);
111 /* If the size of the dump file exceeds the rlimit, then see what would happen
112 if we wrote the stack, but not the data area. */
113 #ifdef __sparc__
114 if ((dump.u_dsize+dump.u_ssize) >
115 current->rlim[RLIMIT_CORE].rlim_cur)
116 dump.u_dsize = 0;
117 #else
118 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
119 current->rlim[RLIMIT_CORE].rlim_cur)
120 dump.u_dsize = 0;
121 #endif
123 /* Make sure we have enough room to write the stack and data areas. */
124 #ifdef __sparc__
125 if ((dump.u_ssize) >
126 current->rlim[RLIMIT_CORE].rlim_cur)
127 dump.u_ssize = 0;
128 #else
129 if ((dump.u_ssize+1) * PAGE_SIZE >
130 current->rlim[RLIMIT_CORE].rlim_cur)
131 dump.u_ssize = 0;
132 #endif
134 /* make sure we actually have a data and stack area to dump */
135 set_fs(USER_DS);
136 #ifdef __sparc__
137 if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize))
138 dump.u_dsize = 0;
139 if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize))
140 dump.u_ssize = 0;
141 #else
142 if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
143 dump.u_dsize = 0;
144 if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
145 dump.u_ssize = 0;
146 #endif
148 set_fs(KERNEL_DS);
149 /* struct user */
150 DUMP_WRITE(&dump,sizeof(dump));
151 /* Now dump all of the user data. Include malloced stuff as well */
152 #ifndef __sparc__
153 DUMP_SEEK(PAGE_SIZE);
154 #endif
155 /* now we start writing out the user space info */
156 set_fs(USER_DS);
157 /* Dump the data area */
158 if (dump.u_dsize != 0) {
159 dump_start = START_DATA(dump);
160 #ifdef __sparc__
161 dump_size = dump.u_dsize;
162 #else
163 dump_size = dump.u_dsize << PAGE_SHIFT;
164 #endif
165 DUMP_WRITE(dump_start,dump_size);
167 /* Now prepare to dump the stack area */
168 if (dump.u_ssize != 0) {
169 dump_start = START_STACK(dump);
170 #ifdef __sparc__
171 dump_size = dump.u_ssize;
172 #else
173 dump_size = dump.u_ssize << PAGE_SHIFT;
174 #endif
175 DUMP_WRITE(dump_start,dump_size);
177 /* Finally dump the task struct. Not be used by gdb, but could be useful */
178 set_fs(KERNEL_DS);
179 DUMP_WRITE(current,sizeof(*current));
180 end_coredump:
181 set_fs(fs);
182 return has_dumped;
186 * create_aout_tables() parses the env- and arg-strings in new user
187 * memory and creates the pointer tables from them, and puts their
188 * addresses on the "stack", returning the new stack pointer value.
190 static unsigned long * create_aout_tables(char * p, struct linux_binprm * bprm)
192 char **argv, **envp;
193 unsigned long * sp;
194 int argc = bprm->argc;
195 int envc = bprm->envc;
197 sp = (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
198 #ifdef __sparc__
199 /* This imposes the proper stack alignment for a new process. */
200 sp = (unsigned long *) (((unsigned long) sp) & ~7);
201 if ((envc+argc+3)&1) --sp;
202 #endif
203 #ifdef __alpha__
204 /* whee.. test-programs are so much fun. */
205 put_user(0, --sp);
206 put_user(0, --sp);
207 if (bprm->loader) {
208 put_user(0, --sp);
209 put_user(0x3eb, --sp);
210 put_user(bprm->loader, --sp);
211 put_user(0x3ea, --sp);
213 put_user(bprm->exec, --sp);
214 put_user(0x3e9, --sp);
215 #endif
216 sp -= envc+1;
217 envp = (char **) sp;
218 sp -= argc+1;
219 argv = (char **) sp;
220 #if defined(__i386__) || defined(__mc68000__)
221 put_user((unsigned long) envp,--sp);
222 put_user((unsigned long) argv,--sp);
223 #endif
224 put_user(argc,--sp);
225 current->mm->arg_start = (unsigned long) p;
226 while (argc-->0) {
227 char c;
228 put_user(p,argv++);
229 do {
230 get_user(c,p++);
231 } while (c);
233 put_user(NULL,argv);
234 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
235 while (envc-->0) {
236 char c;
237 put_user(p,envp++);
238 do {
239 get_user(c,p++);
240 } while (c);
242 put_user(NULL,envp);
243 current->mm->env_end = (unsigned long) p;
244 return sp;
248 * These are the functions used to load a.out style executables and shared
249 * libraries. There is no binary dependent code anywhere else.
252 static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
254 struct exec ex;
255 unsigned long error;
256 unsigned long fd_offset;
257 unsigned long rlim;
258 int retval;
260 ex = *((struct exec *) bprm->buf); /* exec-header */
261 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
262 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
263 N_TRSIZE(ex) || N_DRSIZE(ex) ||
264 bprm->file->f_dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
265 return -ENOEXEC;
268 fd_offset = N_TXTOFF(ex);
270 /* Check initial limits. This avoids letting people circumvent
271 * size limits imposed on them by creating programs with large
272 * arrays in the data or bss.
274 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
275 if (rlim >= RLIM_INFINITY)
276 rlim = ~0;
277 if (ex.a_data + ex.a_bss > rlim)
278 return -ENOMEM;
280 /* Flush all traces of the currently running executable */
281 retval = flush_old_exec(bprm);
282 if (retval)
283 return retval;
285 /* OK, This is the point of no return */
286 #if !defined(__sparc__)
287 set_personality(PER_LINUX);
288 #else
289 set_personality(PER_SUNOS);
290 #if !defined(__sparc_v9__)
291 memcpy(&current->thread.core_exec, &ex, sizeof(struct exec));
292 #endif
293 #endif
295 current->mm->end_code = ex.a_text +
296 (current->mm->start_code = N_TXTADDR(ex));
297 current->mm->end_data = ex.a_data +
298 (current->mm->start_data = N_DATADDR(ex));
299 current->mm->brk = ex.a_bss +
300 (current->mm->start_brk = N_BSSADDR(ex));
302 current->mm->rss = 0;
303 current->mm->mmap = NULL;
304 compute_creds(bprm);
305 current->flags &= ~PF_FORKNOEXEC;
306 #ifdef __sparc__
307 if (N_MAGIC(ex) == NMAGIC) {
308 loff_t pos = fd_offset;
309 /* Fuck me plenty... */
310 /* <AOL></AOL> */
311 error = do_brk(N_TXTADDR(ex), ex.a_text);
312 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
313 ex.a_text, &pos);
314 error = do_brk(N_DATADDR(ex), ex.a_data);
315 bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
316 ex.a_data, &pos);
317 goto beyond_if;
319 #endif
321 if (N_MAGIC(ex) == OMAGIC) {
322 loff_t pos;
323 #if defined(__alpha__) || defined(__sparc__)
324 pos = fd_offset;
325 do_brk(N_TXTADDR(ex) & PAGE_MASK,
326 ex.a_text+ex.a_data + PAGE_SIZE - 1);
327 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
328 ex.a_text+ex.a_data, &pos);
329 #else
330 pos = 32;
331 do_brk(0, ex.a_text+ex.a_data);
332 bprm->file->f_op->read(bprm->file, (char *) 0,
333 ex.a_text+ex.a_data, &pos);
334 #endif
335 flush_icache_range((unsigned long) 0,
336 (unsigned long) ex.a_text+ex.a_data);
337 } else {
338 static unsigned long error_time, error_time2;
339 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
340 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time2) > 5*HZ)
342 printk(KERN_NOTICE "executable not page aligned\n");
343 error_time2 = jiffies;
346 if ((fd_offset & ~PAGE_MASK) != 0 &&
347 (jiffies-error_time) > 5*HZ)
349 printk(KERN_WARNING
350 "fd_offset is not page aligned. Please convert program: %s\n",
351 bprm->file->f_dentry->d_name.name);
352 error_time = jiffies;
355 if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
356 loff_t pos = fd_offset;
357 do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
358 bprm->file->f_op->read(bprm->file,(char *)N_TXTADDR(ex),
359 ex.a_text+ex.a_data, &pos);
360 flush_icache_range((unsigned long) N_TXTADDR(ex),
361 (unsigned long) N_TXTADDR(ex) +
362 ex.a_text+ex.a_data);
363 goto beyond_if;
366 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
367 PROT_READ | PROT_EXEC,
368 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
369 fd_offset);
371 if (error != N_TXTADDR(ex)) {
372 send_sig(SIGKILL, current, 0);
373 return error;
376 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
377 PROT_READ | PROT_WRITE | PROT_EXEC,
378 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
379 fd_offset + ex.a_text);
380 if (error != N_DATADDR(ex)) {
381 send_sig(SIGKILL, current, 0);
382 return error;
385 beyond_if:
386 set_binfmt(&aout_format);
388 set_brk(current->mm->start_brk, current->mm->brk);
390 retval = setup_arg_pages(bprm);
391 if (retval < 0) {
392 /* Someone check-me: is this error path enough? */
393 send_sig(SIGKILL, current, 0);
394 return retval;
397 current->mm->start_stack =
398 (unsigned long) create_aout_tables((char *) bprm->p, bprm);
399 #ifdef __alpha__
400 regs->gp = ex.a_gpvalue;
401 #endif
402 start_thread(regs, ex.a_entry, current->mm->start_stack);
403 if (current->flags & PF_PTRACED)
404 send_sig(SIGTRAP, current, 0);
405 return 0;
408 static int load_aout_library(struct file *file)
410 struct inode * inode;
411 unsigned long bss, start_addr, len;
412 unsigned long error;
413 int retval;
414 struct exec ex;
416 inode = file->f_dentry->d_inode;
418 retval = -ENOEXEC;
419 error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
420 if (error != sizeof(ex))
421 goto out;
423 /* We come in here for the regular a.out style of shared libraries */
424 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
425 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
426 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
427 goto out;
430 if (N_FLAGS(ex))
431 goto out;
433 /* For QMAGIC, the starting address is 0x20 into the page. We mask
434 this off to get the starting address for the page */
436 start_addr = ex.a_entry & 0xfffff000;
438 if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
439 static unsigned long error_time;
440 loff_t pos = N_TXTOFF(ex);
442 if ((jiffies-error_time) > 5*HZ)
444 printk(KERN_WARNING
445 "N_TXTOFF is not page aligned. Please convert library: %s\n",
446 file->f_dentry->d_name.name);
447 error_time = jiffies;
450 do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
452 file->f_op->read(file, (char *)start_addr,
453 ex.a_text + ex.a_data, &pos);
454 flush_icache_range((unsigned long) start_addr,
455 (unsigned long) start_addr + ex.a_text + ex.a_data);
457 retval = 0;
458 goto out;
460 /* Now use mmap to map the library into memory. */
461 down(&current->mm->mmap_sem);
462 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
463 PROT_READ | PROT_WRITE | PROT_EXEC,
464 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
465 N_TXTOFF(ex));
466 up(&current->mm->mmap_sem);
467 retval = error;
468 if (error != start_addr)
469 goto out;
471 len = PAGE_ALIGN(ex.a_text + ex.a_data);
472 bss = ex.a_text + ex.a_data + ex.a_bss;
473 if (bss > len) {
474 error = do_brk(start_addr + len, bss - len);
475 retval = error;
476 if (error != start_addr + len)
477 goto out;
479 retval = 0;
480 out:
481 return retval;
484 static int __init init_aout_binfmt(void)
486 return register_binfmt(&aout_format);
489 static void __exit exit_aout_binfmt(void)
491 unregister_binfmt(&aout_format);
494 EXPORT_NO_SYMBOLS;
496 module_init(init_aout_binfmt);
497 module_exit(exit_aout_binfmt);