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>
35 static int load_aout32_binary(struct linux_binprm *, struct pt_regs * regs);
36 static int load_aout32_library(struct file*);
37 static int aout32_core_dump(long signr, struct pt_regs * regs, struct file *file);
39 extern void dump_thread(struct pt_regs *, struct user *);
41 static struct linux_binfmt aout32_format = {
42 NULL, THIS_MODULE, load_aout32_binary, load_aout32_library, aout32_core_dump,
46 static void set_brk(unsigned long start, unsigned long end)
48 start = PAGE_ALIGN(start);
49 end = PAGE_ALIGN(end);
52 do_brk(start, end - start);
56 * These are the only things you should do on a core-file: use only these
57 * macros to write out all the necessary info.
60 static int dump_write(struct file *file, const void *addr, int nr)
62 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
65 #define DUMP_WRITE(addr, nr) \
66 if (!dump_write(file, (void *)(addr), (nr))) \
69 #define DUMP_SEEK(offset) \
70 if (file->f_op->llseek) { \
71 if (file->f_op->llseek(file,(offset),0) != (offset)) \
73 } else file->f_pos = (offset)
76 * Routine writes a core dump image in the current directory.
77 * Currently only a stub-function.
79 * Note that setuid/setgid files won't make a core-dump if the uid/gid
80 * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
81 * field, which also makes sure the core-dumps won't be recursive if the
82 * dumping of the process results in another error..
85 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file)
89 unsigned long dump_start, dump_size;
91 # define START_DATA(u) (u.u_tsize)
92 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
97 current->flags |= PF_DUMPCORE;
98 strncpy(dump.u_comm, current->comm, sizeof(current->comm));
100 dump_thread(regs, &dump);
102 /* If the size of the dump file exceeds the rlimit, then see what would happen
103 if we wrote the stack, but not the data area. */
104 if ((dump.u_dsize+dump.u_ssize) >
105 current->rlim[RLIMIT_CORE].rlim_cur)
108 /* Make sure we have enough room to write the stack and data areas. */
110 current->rlim[RLIMIT_CORE].rlim_cur)
113 /* make sure we actually have a data and stack area to dump */
115 if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize))
117 if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize))
122 DUMP_WRITE(&dump,sizeof(dump));
123 /* now we start writing out the user space info */
125 /* Dump the data area */
126 if (dump.u_dsize != 0) {
127 dump_start = START_DATA(dump);
128 dump_size = dump.u_dsize;
129 DUMP_WRITE(dump_start,dump_size);
131 /* Now prepare to dump the stack area */
132 if (dump.u_ssize != 0) {
133 dump_start = START_STACK(dump);
134 dump_size = dump.u_ssize;
135 DUMP_WRITE(dump_start,dump_size);
137 /* Finally dump the task struct. Not be used by gdb, but could be useful */
139 DUMP_WRITE(current,sizeof(*current));
146 * create_aout32_tables() parses the env- and arg-strings in new user
147 * memory and creates the pointer tables from them, and puts their
148 * addresses on the "stack", returning the new stack pointer value.
151 static u32 *create_aout32_tables(char * p, struct linux_binprm * bprm)
155 int argc = bprm->argc;
156 int envc = bprm->envc;
158 sp = (u32 *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
160 /* This imposes the proper stack alignment for a new process. */
161 sp = (u32 *) (((unsigned long) sp) & ~7);
170 current->mm->arg_start = (unsigned long) p;
173 put_user(((u32)(unsigned long)(p)),argv++);
179 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
182 put_user(((u32)(unsigned long)(p)),envp++);
188 current->mm->env_end = (unsigned long) p;
193 * These are the functions used to load a.out style executables and shared
194 * libraries. There is no binary dependent code anywhere else.
197 static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
201 unsigned long fd_offset;
203 unsigned long orig_thr_flags;
206 ex = *((struct exec *) bprm->buf); /* exec-header */
207 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
208 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
209 N_TRSIZE(ex) || N_DRSIZE(ex) ||
210 bprm->file->f_dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
214 fd_offset = N_TXTOFF(ex);
216 /* Check initial limits. This avoids letting people circumvent
217 * size limits imposed on them by creating programs with large
218 * arrays in the data or bss.
220 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
221 if (rlim >= RLIM_INFINITY)
223 if (ex.a_data + ex.a_bss > rlim)
226 /* Flush all traces of the currently running executable */
227 retval = flush_old_exec(bprm);
231 /* OK, This is the point of no return */
232 set_personality(PER_SUNOS);
234 current->mm->end_code = ex.a_text +
235 (current->mm->start_code = N_TXTADDR(ex));
236 current->mm->end_data = ex.a_data +
237 (current->mm->start_data = N_DATADDR(ex));
238 current->mm->brk = ex.a_bss +
239 (current->mm->start_brk = N_BSSADDR(ex));
241 current->mm->rss = 0;
242 current->mm->mmap = NULL;
244 current->flags &= ~PF_FORKNOEXEC;
245 if (N_MAGIC(ex) == NMAGIC) {
246 loff_t pos = fd_offset;
247 /* Fuck me plenty... */
248 error = do_brk(N_TXTADDR(ex), ex.a_text);
249 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
251 error = do_brk(N_DATADDR(ex), ex.a_data);
252 bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
257 if (N_MAGIC(ex) == OMAGIC) {
258 loff_t pos = fd_offset;
259 do_brk(N_TXTADDR(ex) & PAGE_MASK,
260 ex.a_text+ex.a_data + PAGE_SIZE - 1);
261 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
262 ex.a_text+ex.a_data, &pos);
264 static unsigned long error_time;
265 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
266 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
268 printk(KERN_NOTICE "executable not page aligned\n");
269 error_time = jiffies;
272 if (!bprm->file->f_op->mmap) {
273 loff_t pos = fd_offset;
274 do_brk(0, ex.a_text+ex.a_data);
275 bprm->file->f_op->read(bprm->file,(char *)N_TXTADDR(ex),
276 ex.a_text+ex.a_data, &pos);
280 down_write(¤t->mm->mmap_sem);
281 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
282 PROT_READ | PROT_EXEC,
283 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
285 up_write(¤t->mm->mmap_sem);
287 if (error != N_TXTADDR(ex)) {
288 send_sig(SIGKILL, current, 0);
292 down_write(¤t->mm->mmap_sem);
293 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
294 PROT_READ | PROT_WRITE | PROT_EXEC,
295 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
296 fd_offset + ex.a_text);
297 up_write(¤t->mm->mmap_sem);
298 if (error != N_DATADDR(ex)) {
299 send_sig(SIGKILL, current, 0);
304 set_binfmt(&aout32_format);
306 set_brk(current->mm->start_brk, current->mm->brk);
308 /* Make sure STACK_TOP returns the right thing. */
309 orig_thr_flags = current_thread_info()->flags;
310 current_thread_info()->flags |= _TIF_32BIT;
312 retval = setup_arg_pages(bprm, EXSTACK_DEFAULT);
314 current_thread_info()->flags = orig_thr_flags;
316 /* Someone check-me: is this error path enough? */
317 send_sig(SIGKILL, current, 0);
321 current->mm->start_stack =
322 (unsigned long) create_aout32_tables((char *)bprm->p, bprm);
323 if (!(orig_thr_flags & _TIF_32BIT)) {
324 unsigned long pgd_cache;
326 pgd_cache = ((unsigned long)current->mm->pgd[0])<<11UL;
327 __asm__ __volatile__("stxa\t%0, [%1] %2\n\t"
331 "r" (TSB_REG), "i" (ASI_DMMU));
333 start_thread32(regs, ex.a_entry, current->mm->start_stack);
334 if (current->ptrace & PT_PTRACED)
335 send_sig(SIGTRAP, current, 0);
339 /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
340 static int load_aout32_library(struct file *file)
342 struct inode * inode;
343 unsigned long bss, start_addr, len;
348 inode = file->f_dentry->d_inode;
351 error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
352 if (error != sizeof(ex))
355 /* We come in here for the regular a.out style of shared libraries */
356 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
357 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
358 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
362 if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
363 (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
364 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
371 /* For QMAGIC, the starting address is 0x20 into the page. We mask
372 this off to get the starting address for the page */
374 start_addr = ex.a_entry & 0xfffff000;
376 /* Now use mmap to map the library into memory. */
377 down_write(¤t->mm->mmap_sem);
378 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
379 PROT_READ | PROT_WRITE | PROT_EXEC,
380 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
382 up_write(¤t->mm->mmap_sem);
384 if (error != start_addr)
387 len = PAGE_ALIGN(ex.a_text + ex.a_data);
388 bss = ex.a_text + ex.a_data + ex.a_bss;
390 error = do_brk(start_addr + len, bss - len);
392 if (error != start_addr + len)
400 static int __init init_aout32_binfmt(void)
402 return register_binfmt(&aout32_format);
405 static void __exit exit_aout32_binfmt(void)
407 unregister_binfmt(&aout32_format);
410 module_init(init_aout32_binfmt);
411 module_exit(exit_aout32_binfmt);