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>
30 #include <linux/vs_memory.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/pgalloc.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 extern void dump_thread(struct pt_regs *, struct user *);
42 static struct linux_binfmt aout32_format = {
43 NULL, THIS_MODULE, load_aout32_binary, load_aout32_library, aout32_core_dump,
47 static void set_brk(unsigned long start, unsigned long end)
49 start = PAGE_ALIGN(start);
50 end = PAGE_ALIGN(end);
53 down_write(¤t->mm->mmap_sem);
54 do_brk(start, end - start);
55 up_write(¤t->mm->mmap_sem);
59 * These are the only things you should do on a core-file: use only these
60 * macros to write out all the necessary info.
63 static int dump_write(struct file *file, const void *addr, int nr)
65 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
68 #define DUMP_WRITE(addr, nr) \
69 if (!dump_write(file, (void *)(addr), (nr))) \
72 #define DUMP_SEEK(offset) \
73 if (file->f_op->llseek) { \
74 if (file->f_op->llseek(file,(offset),0) != (offset)) \
76 } else file->f_pos = (offset)
79 * Routine writes a core dump image in the current directory.
80 * Currently only a stub-function.
82 * Note that setuid/setgid files won't make a core-dump if the uid/gid
83 * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
84 * field, which also makes sure the core-dumps won't be recursive if the
85 * dumping of the process results in another error..
88 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file)
92 unsigned long dump_start, dump_size;
94 # define START_DATA(u) (u.u_tsize)
95 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
100 current->flags |= PF_DUMPCORE;
101 strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
103 dump_thread(regs, &dump);
105 /* If the size of the dump file exceeds the rlimit, then see what would happen
106 if we wrote the stack, but not the data area. */
107 if ((dump.u_dsize+dump.u_ssize) >
108 current->signal->rlim[RLIMIT_CORE].rlim_cur)
111 /* Make sure we have enough room to write the stack and data areas. */
113 current->signal->rlim[RLIMIT_CORE].rlim_cur)
116 /* make sure we actually have a data and stack area to dump */
118 if (!access_ok(VERIFY_READ, (void __user *) START_DATA(dump), dump.u_dsize))
120 if (!access_ok(VERIFY_READ, (void __user *) START_STACK(dump), dump.u_ssize))
125 DUMP_WRITE(&dump,sizeof(dump));
126 /* now we start writing out the user space info */
128 /* Dump the data area */
129 if (dump.u_dsize != 0) {
130 dump_start = START_DATA(dump);
131 dump_size = dump.u_dsize;
132 DUMP_WRITE(dump_start,dump_size);
134 /* Now prepare to dump the stack area */
135 if (dump.u_ssize != 0) {
136 dump_start = START_STACK(dump);
137 dump_size = dump.u_ssize;
138 DUMP_WRITE(dump_start,dump_size);
140 /* Finally dump the task struct. Not be used by gdb, but could be useful */
142 DUMP_WRITE(current,sizeof(*current));
149 * create_aout32_tables() parses the env- and arg-strings in new user
150 * memory and creates the pointer tables from them, and puts their
151 * addresses on the "stack", returning the new stack pointer value.
154 static u32 __user *create_aout32_tables(char __user *p, struct linux_binprm *bprm)
159 int argc = bprm->argc;
160 int envc = bprm->envc;
162 sp = (u32 __user *)((-(unsigned long)sizeof(char *))&(unsigned long)p);
164 /* This imposes the proper stack alignment for a new process. */
165 sp = (u32 __user *) (((unsigned long) sp) & ~7);
174 current->mm->arg_start = (unsigned long) p;
177 put_user(((u32)(unsigned long)(p)),argv++);
183 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
186 put_user(((u32)(unsigned long)(p)),envp++);
192 current->mm->env_end = (unsigned long) p;
197 * These are the functions used to load a.out style executables and shared
198 * libraries. There is no binary dependent code anywhere else.
201 static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
205 unsigned long fd_offset;
207 unsigned long orig_thr_flags;
210 ex = *((struct exec *) bprm->buf); /* exec-header */
211 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
212 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
213 N_TRSIZE(ex) || N_DRSIZE(ex) ||
214 bprm->file->f_dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
218 fd_offset = N_TXTOFF(ex);
220 /* Check initial limits. This avoids letting people circumvent
221 * size limits imposed on them by creating programs with large
222 * arrays in the data or bss.
224 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
225 if (rlim >= RLIM_INFINITY)
227 if (ex.a_data + ex.a_bss > rlim)
230 /* Flush all traces of the currently running executable */
231 retval = flush_old_exec(bprm);
235 /* OK, This is the point of no return */
236 set_personality(PER_SUNOS);
238 current->mm->end_code = ex.a_text +
239 (current->mm->start_code = N_TXTADDR(ex));
240 current->mm->end_data = ex.a_data +
241 (current->mm->start_data = N_DATADDR(ex));
242 current->mm->brk = ex.a_bss +
243 (current->mm->start_brk = N_BSSADDR(ex));
245 set_mm_counter(current->mm, rss, 0);
246 current->mm->mmap = NULL;
248 current->flags &= ~PF_FORKNOEXEC;
249 if (N_MAGIC(ex) == NMAGIC) {
250 loff_t pos = fd_offset;
251 /* Fuck me plenty... */
252 down_write(¤t->mm->mmap_sem);
253 error = do_brk(N_TXTADDR(ex), ex.a_text);
254 up_write(¤t->mm->mmap_sem);
255 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
257 down_write(¤t->mm->mmap_sem);
258 error = do_brk(N_DATADDR(ex), ex.a_data);
259 up_write(¤t->mm->mmap_sem);
260 bprm->file->f_op->read(bprm->file, (char __user *)N_DATADDR(ex),
265 if (N_MAGIC(ex) == OMAGIC) {
266 loff_t pos = fd_offset;
267 down_write(¤t->mm->mmap_sem);
268 do_brk(N_TXTADDR(ex) & PAGE_MASK,
269 ex.a_text+ex.a_data + PAGE_SIZE - 1);
270 up_write(¤t->mm->mmap_sem);
271 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
272 ex.a_text+ex.a_data, &pos);
274 static unsigned long error_time;
275 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
276 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
278 printk(KERN_NOTICE "executable not page aligned\n");
279 error_time = jiffies;
282 if (!bprm->file->f_op->mmap) {
283 loff_t pos = fd_offset;
284 down_write(¤t->mm->mmap_sem);
285 do_brk(0, ex.a_text+ex.a_data);
286 up_write(¤t->mm->mmap_sem);
287 bprm->file->f_op->read(bprm->file,
288 (char __user *)N_TXTADDR(ex),
289 ex.a_text+ex.a_data, &pos);
293 down_write(¤t->mm->mmap_sem);
294 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
295 PROT_READ | PROT_EXEC,
296 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
298 up_write(¤t->mm->mmap_sem);
300 if (error != N_TXTADDR(ex)) {
301 send_sig(SIGKILL, current, 0);
305 down_write(¤t->mm->mmap_sem);
306 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
307 PROT_READ | PROT_WRITE | PROT_EXEC,
308 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
309 fd_offset + ex.a_text);
310 up_write(¤t->mm->mmap_sem);
311 if (error != N_DATADDR(ex)) {
312 send_sig(SIGKILL, current, 0);
317 set_binfmt(&aout32_format);
319 set_brk(current->mm->start_brk, current->mm->brk);
321 /* Make sure STACK_TOP returns the right thing. */
322 orig_thr_flags = current_thread_info()->flags;
323 current_thread_info()->flags |= _TIF_32BIT;
325 retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
327 current_thread_info()->flags = orig_thr_flags;
329 /* Someone check-me: is this error path enough? */
330 send_sig(SIGKILL, current, 0);
334 current->mm->start_stack =
335 (unsigned long) create_aout32_tables((char __user *)bprm->p, bprm);
336 if (!(orig_thr_flags & _TIF_32BIT)) {
337 unsigned long pgd_cache = get_pgd_cache(current->mm->pgd);
339 __asm__ __volatile__("stxa\t%0, [%1] %2\n\t"
343 "r" (TSB_REG), "i" (ASI_DMMU));
345 start_thread32(regs, ex.a_entry, current->mm->start_stack);
346 if (current->ptrace & PT_PTRACED)
347 send_sig(SIGTRAP, current, 0);
351 /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
352 static int load_aout32_library(struct file *file)
354 struct inode * inode;
355 unsigned long bss, start_addr, len;
360 inode = file->f_dentry->d_inode;
363 error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
364 if (error != sizeof(ex))
367 /* We come in here for the regular a.out style of shared libraries */
368 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
369 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
370 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
374 if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
375 (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
376 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
383 /* For QMAGIC, the starting address is 0x20 into the page. We mask
384 this off to get the starting address for the page */
386 start_addr = ex.a_entry & 0xfffff000;
388 /* Now use mmap to map the library into memory. */
389 down_write(¤t->mm->mmap_sem);
390 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
391 PROT_READ | PROT_WRITE | PROT_EXEC,
392 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
394 up_write(¤t->mm->mmap_sem);
396 if (error != start_addr)
399 len = PAGE_ALIGN(ex.a_text + ex.a_data);
400 bss = ex.a_text + ex.a_data + ex.a_bss;
402 down_write(¤t->mm->mmap_sem);
403 error = do_brk(start_addr + len, bss - len);
404 up_write(¤t->mm->mmap_sem);
406 if (error != start_addr + len)
414 static int __init init_aout32_binfmt(void)
416 return register_binfmt(&aout32_format);
419 static void __exit exit_aout32_binfmt(void)
421 unregister_binfmt(&aout32_format);
424 module_init(init_aout32_binfmt);
425 module_exit(exit_aout32_binfmt);