2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
6 * Hacked a bit by DaveM to make it work with 32-bit SunOS
7 * binaries on the sparc64 port.
10 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
15 #include <linux/mman.h>
16 #include <linux/a.out.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/string.h>
21 #include <linux/file.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/slab.h>
27 #include <linux/binfmts.h>
28 #include <linux/personality.h>
29 #include <linux/init.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgalloc.h>
34 #include <asm/mmu_context.h>
36 static int load_aout32_binary(struct linux_binprm *, struct pt_regs * regs);
37 static int load_aout32_library(struct file*);
38 static int aout32_core_dump(long signr, struct pt_regs * regs, struct file *file);
40 static struct linux_binfmt aout32_format = {
41 NULL, THIS_MODULE, load_aout32_binary, load_aout32_library, aout32_core_dump,
45 static void set_brk(unsigned long start, unsigned long end)
47 start = PAGE_ALIGN(start);
48 end = PAGE_ALIGN(end);
51 down_write(¤t->mm->mmap_sem);
52 do_brk(start, end - start);
53 up_write(¤t->mm->mmap_sem);
57 * These are the only things you should do on a core-file: use only these
58 * macros to write out all the necessary info.
61 static int dump_write(struct file *file, const void *addr, int nr)
63 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
66 #define DUMP_WRITE(addr, nr) \
67 if (!dump_write(file, (void *)(addr), (nr))) \
70 #define DUMP_SEEK(offset) \
71 if (file->f_op->llseek) { \
72 if (file->f_op->llseek(file,(offset),0) != (offset)) \
74 } else file->f_pos = (offset)
77 * Routine writes a core dump image in the current directory.
78 * Currently only a stub-function.
80 * Note that setuid/setgid files won't make a core-dump if the uid/gid
81 * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
82 * field, which also makes sure the core-dumps won't be recursive if the
83 * dumping of the process results in another error..
86 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file)
90 unsigned long dump_start, dump_size;
92 # define START_DATA(u) (u.u_tsize)
93 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
98 current->flags |= PF_DUMPCORE;
99 strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
101 dump_thread(regs, &dump);
103 /* If the size of the dump file exceeds the rlimit, then see what would happen
104 if we wrote the stack, but not the data area. */
105 if ((dump.u_dsize+dump.u_ssize) >
106 current->signal->rlim[RLIMIT_CORE].rlim_cur)
109 /* Make sure we have enough room to write the stack and data areas. */
111 current->signal->rlim[RLIMIT_CORE].rlim_cur)
114 /* make sure we actually have a data and stack area to dump */
116 if (!access_ok(VERIFY_READ, (void __user *) START_DATA(dump), dump.u_dsize))
118 if (!access_ok(VERIFY_READ, (void __user *) START_STACK(dump), dump.u_ssize))
123 DUMP_WRITE(&dump,sizeof(dump));
124 /* now we start writing out the user space info */
126 /* Dump the data area */
127 if (dump.u_dsize != 0) {
128 dump_start = START_DATA(dump);
129 dump_size = dump.u_dsize;
130 DUMP_WRITE(dump_start,dump_size);
132 /* Now prepare to dump the stack area */
133 if (dump.u_ssize != 0) {
134 dump_start = START_STACK(dump);
135 dump_size = dump.u_ssize;
136 DUMP_WRITE(dump_start,dump_size);
138 /* Finally dump the task struct. Not be used by gdb, but could be useful */
140 DUMP_WRITE(current,sizeof(*current));
147 * create_aout32_tables() parses the env- and arg-strings in new user
148 * memory and creates the pointer tables from them, and puts their
149 * addresses on the "stack", returning the new stack pointer value.
152 static u32 __user *create_aout32_tables(char __user *p, struct linux_binprm *bprm)
157 int argc = bprm->argc;
158 int envc = bprm->envc;
160 sp = (u32 __user *)((-(unsigned long)sizeof(char *))&(unsigned long)p);
162 /* This imposes the proper stack alignment for a new process. */
163 sp = (u32 __user *) (((unsigned long) sp) & ~7);
172 current->mm->arg_start = (unsigned long) p;
175 put_user(((u32)(unsigned long)(p)),argv++);
181 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
184 put_user(((u32)(unsigned long)(p)),envp++);
190 current->mm->env_end = (unsigned long) p;
195 * These are the functions used to load a.out style executables and shared
196 * libraries. There is no binary dependent code anywhere else.
199 static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
203 unsigned long fd_offset;
205 unsigned long orig_thr_flags;
208 ex = *((struct exec *) bprm->buf); /* exec-header */
209 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
210 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
211 N_TRSIZE(ex) || N_DRSIZE(ex) ||
212 bprm->file->f_dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
216 fd_offset = N_TXTOFF(ex);
218 /* Check initial limits. This avoids letting people circumvent
219 * size limits imposed on them by creating programs with large
220 * arrays in the data or bss.
222 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
223 if (rlim >= RLIM_INFINITY)
225 if (ex.a_data + ex.a_bss > rlim)
228 /* Flush all traces of the currently running executable */
229 retval = flush_old_exec(bprm);
233 /* OK, This is the point of no return */
234 set_personality(PER_SUNOS);
236 current->mm->end_code = ex.a_text +
237 (current->mm->start_code = N_TXTADDR(ex));
238 current->mm->end_data = ex.a_data +
239 (current->mm->start_data = N_DATADDR(ex));
240 current->mm->brk = ex.a_bss +
241 (current->mm->start_brk = N_BSSADDR(ex));
243 current->mm->mmap = NULL;
245 current->flags &= ~PF_FORKNOEXEC;
246 if (N_MAGIC(ex) == NMAGIC) {
247 loff_t pos = fd_offset;
248 /* Fuck me plenty... */
249 down_write(¤t->mm->mmap_sem);
250 error = do_brk(N_TXTADDR(ex), ex.a_text);
251 up_write(¤t->mm->mmap_sem);
252 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
254 down_write(¤t->mm->mmap_sem);
255 error = do_brk(N_DATADDR(ex), ex.a_data);
256 up_write(¤t->mm->mmap_sem);
257 bprm->file->f_op->read(bprm->file, (char __user *)N_DATADDR(ex),
262 if (N_MAGIC(ex) == OMAGIC) {
263 loff_t pos = fd_offset;
264 down_write(¤t->mm->mmap_sem);
265 do_brk(N_TXTADDR(ex) & PAGE_MASK,
266 ex.a_text+ex.a_data + PAGE_SIZE - 1);
267 up_write(¤t->mm->mmap_sem);
268 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
269 ex.a_text+ex.a_data, &pos);
271 static unsigned long error_time;
272 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
273 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
275 printk(KERN_NOTICE "executable not page aligned\n");
276 error_time = jiffies;
279 if (!bprm->file->f_op->mmap) {
280 loff_t pos = fd_offset;
281 down_write(¤t->mm->mmap_sem);
282 do_brk(0, ex.a_text+ex.a_data);
283 up_write(¤t->mm->mmap_sem);
284 bprm->file->f_op->read(bprm->file,
285 (char __user *)N_TXTADDR(ex),
286 ex.a_text+ex.a_data, &pos);
290 down_write(¤t->mm->mmap_sem);
291 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
292 PROT_READ | PROT_EXEC,
293 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
295 up_write(¤t->mm->mmap_sem);
297 if (error != N_TXTADDR(ex)) {
298 send_sig(SIGKILL, current, 0);
302 down_write(¤t->mm->mmap_sem);
303 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
304 PROT_READ | PROT_WRITE | PROT_EXEC,
305 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
306 fd_offset + ex.a_text);
307 up_write(¤t->mm->mmap_sem);
308 if (error != N_DATADDR(ex)) {
309 send_sig(SIGKILL, current, 0);
314 set_binfmt(&aout32_format);
316 set_brk(current->mm->start_brk, current->mm->brk);
318 /* Make sure STACK_TOP returns the right thing. */
319 orig_thr_flags = current_thread_info()->flags;
320 current_thread_info()->flags |= _TIF_32BIT;
322 retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
324 current_thread_info()->flags = orig_thr_flags;
326 /* Someone check-me: is this error path enough? */
327 send_sig(SIGKILL, current, 0);
331 current->mm->start_stack =
332 (unsigned long) create_aout32_tables((char __user *)bprm->p, bprm);
333 tsb_context_switch(mm);
335 start_thread32(regs, ex.a_entry, current->mm->start_stack);
336 if (current->ptrace & PT_PTRACED)
337 send_sig(SIGTRAP, current, 0);
341 /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
342 static int load_aout32_library(struct file *file)
344 struct inode * inode;
345 unsigned long bss, start_addr, len;
350 inode = file->f_dentry->d_inode;
353 error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
354 if (error != sizeof(ex))
357 /* We come in here for the regular a.out style of shared libraries */
358 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
359 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
360 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
364 if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
365 (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
366 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
373 /* For QMAGIC, the starting address is 0x20 into the page. We mask
374 this off to get the starting address for the page */
376 start_addr = ex.a_entry & 0xfffff000;
378 /* Now use mmap to map the library into memory. */
379 down_write(¤t->mm->mmap_sem);
380 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
381 PROT_READ | PROT_WRITE | PROT_EXEC,
382 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
384 up_write(¤t->mm->mmap_sem);
386 if (error != start_addr)
389 len = PAGE_ALIGN(ex.a_text + ex.a_data);
390 bss = ex.a_text + ex.a_data + ex.a_bss;
392 down_write(¤t->mm->mmap_sem);
393 error = do_brk(start_addr + len, bss - len);
394 up_write(¤t->mm->mmap_sem);
396 if (error != start_addr + len)
404 static int __init init_aout32_binfmt(void)
406 return register_binfmt(&aout32_format);
409 static void __exit exit_aout32_binfmt(void)
411 unregister_binfmt(&aout32_format);
414 module_init(init_aout32_binfmt);
415 module_exit(exit_aout32_binfmt);