2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
7 #include <linux/module.h>
9 #include <linux/time.h>
10 #include <linux/kernel.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>
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/slab.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>
31 #include <asm/cacheflush.h>
33 static int load_aout_binary(struct linux_binprm
*, struct pt_regs
* regs
);
34 static int load_aout_library(struct file
*);
35 static int aout_core_dump(long signr
, struct pt_regs
* regs
, struct file
*file
);
37 extern void dump_thread(struct pt_regs
*, struct user
*);
39 static struct linux_binfmt aout_format
= {
40 .module
= THIS_MODULE
,
41 .load_binary
= load_aout_binary
,
42 .load_shlib
= load_aout_library
,
43 .core_dump
= aout_core_dump
,
44 .min_coredump
= PAGE_SIZE
47 static void set_brk(unsigned long start
, unsigned long end
)
49 start
= PAGE_ALIGN(start
);
50 end
= PAGE_ALIGN(end
);
53 do_brk(start
, end
- start
);
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 aout_core_dump(long signr
, struct pt_regs
* regs
, struct file
*file
)
90 unsigned long dump_start
, dump_size
;
92 #if defined(__alpha__)
93 # define START_DATA(u) (u.start_data)
94 #elif defined(__arm__)
95 # define START_DATA(u) ((u.u_tsize << PAGE_SHIFT) + u.start_code)
96 #elif defined(__sparc__)
97 # define START_DATA(u) (u.u_tsize)
98 #elif defined(__i386__) || defined(__mc68000__) || defined(__arch_um__)
99 # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
102 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
104 # define START_STACK(u) (u.start_stack)
110 current
->flags
|= PF_DUMPCORE
;
111 strncpy(dump
.u_comm
, current
->comm
, sizeof(current
->comm
));
113 dump
.u_ar0
= (void *)(((unsigned long)(&dump
.regs
)) - ((unsigned long)(&dump
)));
116 dump_thread(regs
, &dump
);
118 /* If the size of the dump file exceeds the rlimit, then see what would happen
119 if we wrote the stack, but not the data area. */
121 if ((dump
.u_dsize
+dump
.u_ssize
) >
122 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
125 if ((dump
.u_dsize
+dump
.u_ssize
+1) * PAGE_SIZE
>
126 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
130 /* Make sure we have enough room to write the stack and data areas. */
133 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
136 if ((dump
.u_ssize
+1) * PAGE_SIZE
>
137 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
141 /* make sure we actually have a data and stack area to dump */
144 if (verify_area(VERIFY_READ
, (void *) START_DATA(dump
), dump
.u_dsize
))
146 if (verify_area(VERIFY_READ
, (void *) START_STACK(dump
), dump
.u_ssize
))
149 if (verify_area(VERIFY_READ
, (void *) START_DATA(dump
), dump
.u_dsize
<< PAGE_SHIFT
))
151 if (verify_area(VERIFY_READ
, (void *) START_STACK(dump
), dump
.u_ssize
<< PAGE_SHIFT
))
157 DUMP_WRITE(&dump
,sizeof(dump
));
158 /* Now dump all of the user data. Include malloced stuff as well */
160 DUMP_SEEK(PAGE_SIZE
);
162 /* now we start writing out the user space info */
164 /* Dump the data area */
165 if (dump
.u_dsize
!= 0) {
166 dump_start
= START_DATA(dump
);
168 dump_size
= dump
.u_dsize
;
170 dump_size
= dump
.u_dsize
<< PAGE_SHIFT
;
172 DUMP_WRITE(dump_start
,dump_size
);
174 /* Now prepare to dump the stack area */
175 if (dump
.u_ssize
!= 0) {
176 dump_start
= START_STACK(dump
);
178 dump_size
= dump
.u_ssize
;
180 dump_size
= dump
.u_ssize
<< PAGE_SHIFT
;
182 DUMP_WRITE(dump_start
,dump_size
);
184 /* Finally dump the task struct. Not be used by gdb, but could be useful */
186 DUMP_WRITE(current
,sizeof(*current
));
193 * create_aout_tables() parses the env- and arg-strings in new user
194 * memory and creates the pointer tables from them, and puts their
195 * addresses on the "stack", returning the new stack pointer value.
197 static unsigned long * create_aout_tables(char * p
, struct linux_binprm
* bprm
)
201 int argc
= bprm
->argc
;
202 int envc
= bprm
->envc
;
204 sp
= (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p
);
206 /* This imposes the proper stack alignment for a new process. */
207 sp
= (unsigned long *) (((unsigned long) sp
) & ~7);
208 if ((envc
+argc
+3)&1) --sp
;
211 /* whee.. test-programs are so much fun. */
216 put_user(0x3eb, --sp
);
217 put_user(bprm
->loader
, --sp
);
218 put_user(0x3ea, --sp
);
220 put_user(bprm
->exec
, --sp
);
221 put_user(0x3e9, --sp
);
227 #if defined(__i386__) || defined(__mc68000__) || defined(__arm__) || defined(__arch_um__)
228 put_user((unsigned long) envp
,--sp
);
229 put_user((unsigned long) argv
,--sp
);
232 current
->mm
->arg_start
= (unsigned long) p
;
241 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
250 current
->mm
->env_end
= (unsigned long) p
;
255 * These are the functions used to load a.out style executables and shared
256 * libraries. There is no binary dependent code anywhere else.
259 static int load_aout_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
263 unsigned long fd_offset
;
267 ex
= *((struct exec
*) bprm
->buf
); /* exec-header */
268 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != OMAGIC
&&
269 N_MAGIC(ex
) != QMAGIC
&& N_MAGIC(ex
) != NMAGIC
) ||
270 N_TRSIZE(ex
) || N_DRSIZE(ex
) ||
271 i_size_read(bprm
->file
->f_dentry
->d_inode
) < ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
275 fd_offset
= N_TXTOFF(ex
);
277 /* Check initial limits. This avoids letting people circumvent
278 * size limits imposed on them by creating programs with large
279 * arrays in the data or bss.
281 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
282 if (rlim
>= RLIM_INFINITY
)
284 if (ex
.a_data
+ ex
.a_bss
> rlim
)
287 /* Flush all traces of the currently running executable */
288 retval
= flush_old_exec(bprm
);
292 /* OK, This is the point of no return */
293 #if defined(__alpha__)
294 SET_AOUT_PERSONALITY(bprm
, ex
);
295 #elif defined(__sparc__)
296 set_personality(PER_SUNOS
);
297 #if !defined(__sparc_v9__)
298 memcpy(¤t
->thread
.core_exec
, &ex
, sizeof(struct exec
));
301 set_personality(PER_LINUX
);
304 current
->mm
->end_code
= ex
.a_text
+
305 (current
->mm
->start_code
= N_TXTADDR(ex
));
306 current
->mm
->end_data
= ex
.a_data
+
307 (current
->mm
->start_data
= N_DATADDR(ex
));
308 current
->mm
->brk
= ex
.a_bss
+
309 (current
->mm
->start_brk
= N_BSSADDR(ex
));
310 current
->mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
312 current
->mm
->rss
= 0;
313 current
->mm
->mmap
= NULL
;
315 current
->flags
&= ~PF_FORKNOEXEC
;
317 if (N_MAGIC(ex
) == NMAGIC
) {
318 loff_t pos
= fd_offset
;
319 /* Fuck me plenty... */
321 error
= do_brk(N_TXTADDR(ex
), ex
.a_text
);
322 bprm
->file
->f_op
->read(bprm
->file
, (char *) N_TXTADDR(ex
),
324 error
= do_brk(N_DATADDR(ex
), ex
.a_data
);
325 bprm
->file
->f_op
->read(bprm
->file
, (char *) N_DATADDR(ex
),
331 if (N_MAGIC(ex
) == OMAGIC
) {
332 unsigned long text_addr
, map_size
;
335 text_addr
= N_TXTADDR(ex
);
337 #if defined(__alpha__) || defined(__sparc__)
339 map_size
= ex
.a_text
+ex
.a_data
+ PAGE_SIZE
- 1;
342 map_size
= ex
.a_text
+ex
.a_data
;
345 error
= do_brk(text_addr
& PAGE_MASK
, map_size
);
346 if (error
!= (text_addr
& PAGE_MASK
)) {
347 send_sig(SIGKILL
, current
, 0);
351 error
= bprm
->file
->f_op
->read(bprm
->file
, (char *)text_addr
,
352 ex
.a_text
+ex
.a_data
, &pos
);
353 if ((signed long)error
< 0) {
354 send_sig(SIGKILL
, current
, 0);
358 flush_icache_range(text_addr
, text_addr
+ex
.a_text
+ex
.a_data
);
360 static unsigned long error_time
, error_time2
;
361 if ((ex
.a_text
& 0xfff || ex
.a_data
& 0xfff) &&
362 (N_MAGIC(ex
) != NMAGIC
) && (jiffies
-error_time2
) > 5*HZ
)
364 printk(KERN_NOTICE
"executable not page aligned\n");
365 error_time2
= jiffies
;
368 if ((fd_offset
& ~PAGE_MASK
) != 0 &&
369 (jiffies
-error_time
) > 5*HZ
)
372 "fd_offset is not page aligned. Please convert program: %s\n",
373 bprm
->file
->f_dentry
->d_name
.name
);
374 error_time
= jiffies
;
377 if (!bprm
->file
->f_op
->mmap
||((fd_offset
& ~PAGE_MASK
) != 0)) {
378 loff_t pos
= fd_offset
;
379 do_brk(N_TXTADDR(ex
), ex
.a_text
+ex
.a_data
);
380 bprm
->file
->f_op
->read(bprm
->file
,(char *)N_TXTADDR(ex
),
381 ex
.a_text
+ex
.a_data
, &pos
);
382 flush_icache_range((unsigned long) N_TXTADDR(ex
),
383 (unsigned long) N_TXTADDR(ex
) +
384 ex
.a_text
+ex
.a_data
);
388 down_write(¤t
->mm
->mmap_sem
);
389 error
= do_mmap(bprm
->file
, N_TXTADDR(ex
), ex
.a_text
,
390 PROT_READ
| PROT_EXEC
,
391 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
,
393 up_write(¤t
->mm
->mmap_sem
);
395 if (error
!= N_TXTADDR(ex
)) {
396 send_sig(SIGKILL
, current
, 0);
400 down_write(¤t
->mm
->mmap_sem
);
401 error
= do_mmap(bprm
->file
, N_DATADDR(ex
), ex
.a_data
,
402 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
403 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
,
404 fd_offset
+ ex
.a_text
);
405 up_write(¤t
->mm
->mmap_sem
);
406 if (error
!= N_DATADDR(ex
)) {
407 send_sig(SIGKILL
, current
, 0);
412 set_binfmt(&aout_format
);
414 set_brk(current
->mm
->start_brk
, current
->mm
->brk
);
416 retval
= setup_arg_pages(bprm
);
418 /* Someone check-me: is this error path enough? */
419 send_sig(SIGKILL
, current
, 0);
423 current
->mm
->start_stack
=
424 (unsigned long) create_aout_tables((char *) bprm
->p
, bprm
);
426 regs
->gp
= ex
.a_gpvalue
;
428 start_thread(regs
, ex
.a_entry
, current
->mm
->start_stack
);
429 if (unlikely(current
->ptrace
& PT_PTRACED
)) {
430 if (current
->ptrace
& PT_TRACE_EXEC
)
431 ptrace_notify ((PTRACE_EVENT_EXEC
<< 8) | SIGTRAP
);
433 send_sig(SIGTRAP
, current
, 0);
438 static int load_aout_library(struct file
*file
)
440 struct inode
* inode
;
441 unsigned long bss
, start_addr
, len
;
446 inode
= file
->f_dentry
->d_inode
;
449 error
= kernel_read(file
, 0, (char *) &ex
, sizeof(ex
));
450 if (error
!= sizeof(ex
))
453 /* We come in here for the regular a.out style of shared libraries */
454 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != QMAGIC
) || N_TRSIZE(ex
) ||
455 N_DRSIZE(ex
) || ((ex
.a_entry
& 0xfff) && N_MAGIC(ex
) == ZMAGIC
) ||
456 i_size_read(inode
) < ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
463 /* For QMAGIC, the starting address is 0x20 into the page. We mask
464 this off to get the starting address for the page */
466 start_addr
= ex
.a_entry
& 0xfffff000;
468 if ((N_TXTOFF(ex
) & ~PAGE_MASK
) != 0) {
469 static unsigned long error_time
;
470 loff_t pos
= N_TXTOFF(ex
);
472 if ((jiffies
-error_time
) > 5*HZ
)
475 "N_TXTOFF is not page aligned. Please convert library: %s\n",
476 file
->f_dentry
->d_name
.name
);
477 error_time
= jiffies
;
480 do_brk(start_addr
, ex
.a_text
+ ex
.a_data
+ ex
.a_bss
);
482 file
->f_op
->read(file
, (char *)start_addr
,
483 ex
.a_text
+ ex
.a_data
, &pos
);
484 flush_icache_range((unsigned long) start_addr
,
485 (unsigned long) start_addr
+ ex
.a_text
+ ex
.a_data
);
490 /* Now use mmap to map the library into memory. */
491 down_write(¤t
->mm
->mmap_sem
);
492 error
= do_mmap(file
, start_addr
, ex
.a_text
+ ex
.a_data
,
493 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
494 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
496 up_write(¤t
->mm
->mmap_sem
);
498 if (error
!= start_addr
)
501 len
= PAGE_ALIGN(ex
.a_text
+ ex
.a_data
);
502 bss
= ex
.a_text
+ ex
.a_data
+ ex
.a_bss
;
504 error
= do_brk(start_addr
+ len
, bss
- len
);
506 if (error
!= start_addr
+ len
)
514 static int __init
init_aout_binfmt(void)
516 return register_binfmt(&aout_format
);
519 static void __exit
exit_aout_binfmt(void)
521 unregister_binfmt(&aout_format
);
524 module_init(init_aout_binfmt
);
525 module_exit(exit_aout_binfmt
);
526 MODULE_LICENSE("GPL");