2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
7 #include <linux/module.h>
9 #include <linux/sched.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/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/pgtable.h>
32 static int load_aout_binary(struct linux_binprm
*, struct pt_regs
* regs
);
33 static int load_aout_library(int fd
);
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
= {
45 load_aout_binary
, load_aout_library
, aout_core_dump
, PAGE_SIZE
48 static void set_brk(unsigned long start
, unsigned long end
)
50 start
= PAGE_ALIGN(start
);
51 end
= PAGE_ALIGN(end
);
54 do_brk(start
, end
- start
);
58 * These are the only things you should do on a core-file: use only these
59 * macros to write out all the necessary info.
62 static int dump_write(struct file
*file
, const void *addr
, int nr
)
64 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
67 #define DUMP_WRITE(addr, nr) \
68 if (!dump_write(file, (void *)(addr), (nr))) \
71 #define DUMP_SEEK(offset) \
72 if (file->f_op->llseek) { \
73 if (file->f_op->llseek(file,(offset),0) != (offset)) \
75 } else file->f_pos = (offset)
78 * Routine writes a core dump image in the current directory.
79 * Currently only a stub-function.
81 * Note that setuid/setgid files won't make a core-dump if the uid/gid
82 * changed due to the set[u|g]id. It's enforced by the "current->dumpable"
83 * field, which also makes sure the core-dumps won't be recursive if the
84 * dumping of the process results in another error..
88 do_aout_core_dump(long signr
, struct pt_regs
* regs
, struct file
*file
)
92 unsigned long dump_start
, dump_size
;
94 #if defined(__alpha__)
95 # define START_DATA(u) (u.start_data)
96 #elif defined(__sparc__)
97 # define START_DATA(u) (u.u_tsize)
98 #elif defined(__i386__) || defined(__mc68000__)
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 aout_core_dump(long signr
, struct pt_regs
* regs
, struct file
*file
)
198 retval
= do_aout_core_dump(signr
, regs
, file
);
204 * create_aout_tables() parses the env- and arg-strings in new user
205 * memory and creates the pointer tables from them, and puts their
206 * addresses on the "stack", returning the new stack pointer value.
208 static unsigned long * create_aout_tables(char * p
, struct linux_binprm
* bprm
)
212 int argc
= bprm
->argc
;
213 int envc
= bprm
->envc
;
215 sp
= (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p
);
217 /* This imposes the proper stack alignment for a new process. */
218 sp
= (unsigned long *) (((unsigned long) sp
) & ~7);
219 if ((envc
+argc
+3)&1) --sp
;
222 /* whee.. test-programs are so much fun. */
227 put_user(0x3eb, --sp
);
228 put_user(bprm
->loader
, --sp
);
229 put_user(0x3ea, --sp
);
231 put_user(bprm
->exec
, --sp
);
232 put_user(0x3e9, --sp
);
238 #if defined(__i386__) || defined(__mc68000__)
239 put_user((unsigned long) envp
,--sp
);
240 put_user((unsigned long) argv
,--sp
);
243 current
->mm
->arg_start
= (unsigned long) p
;
252 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
261 current
->mm
->env_end
= (unsigned long) p
;
266 * These are the functions used to load a.out style executables and shared
267 * libraries. There is no binary dependent code anywhere else.
270 static inline int do_load_aout_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
276 unsigned long fd_offset
;
280 ex
= *((struct exec
*) bprm
->buf
); /* exec-header */
281 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != OMAGIC
&&
282 N_MAGIC(ex
) != QMAGIC
&& N_MAGIC(ex
) != NMAGIC
) ||
283 N_TRSIZE(ex
) || N_DRSIZE(ex
) ||
284 bprm
->dentry
->d_inode
->i_size
< ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
288 fd_offset
= N_TXTOFF(ex
);
291 if (N_MAGIC(ex
) == ZMAGIC
&& fd_offset
!= BLOCK_SIZE
) {
292 printk(KERN_NOTICE
"N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
296 if (N_MAGIC(ex
) == ZMAGIC
&& ex
.a_text
&&
297 bprm
->dentry
->d_inode
->i_op
&&
298 bprm
->dentry
->d_inode
->i_op
->get_block
&&
299 (fd_offset
< bprm
->dentry
->d_inode
->i_sb
->s_blocksize
)) {
300 printk(KERN_NOTICE
"N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
305 /* Check initial limits. This avoids letting people circumvent
306 * size limits imposed on them by creating programs with large
307 * arrays in the data or bss.
309 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
310 if (rlim
>= RLIM_INFINITY
)
312 if (ex
.a_data
+ ex
.a_bss
> rlim
)
315 /* Flush all traces of the currently running executable */
316 retval
= flush_old_exec(bprm
);
320 /* OK, This is the point of no return */
321 current
->personality
= PER_LINUX
;
323 #if defined(__sparc__) && !defined(__sparc_v9__)
324 memcpy(¤t
->tss
.core_exec
, &ex
, sizeof(struct exec
));
327 current
->mm
->end_code
= ex
.a_text
+
328 (current
->mm
->start_code
= N_TXTADDR(ex
));
329 current
->mm
->end_data
= ex
.a_data
+
330 (current
->mm
->start_data
= N_DATADDR(ex
));
331 current
->mm
->brk
= ex
.a_bss
+
332 (current
->mm
->start_brk
= N_BSSADDR(ex
));
334 current
->mm
->rss
= 0;
335 current
->mm
->mmap
= NULL
;
337 current
->flags
&= ~PF_FORKNOEXEC
;
339 if (N_MAGIC(ex
) == NMAGIC
) {
340 /* Fuck me plenty... */
341 error
= do_brk(N_TXTADDR(ex
), ex
.a_text
);
342 read_exec(bprm
->dentry
, fd_offset
, (char *) N_TXTADDR(ex
),
344 error
= do_brk(N_DATADDR(ex
), ex
.a_data
);
345 read_exec(bprm
->dentry
, fd_offset
+ ex
.a_text
, (char *) N_DATADDR(ex
),
351 if (N_MAGIC(ex
) == OMAGIC
) {
352 #if defined(__alpha__) || defined(__sparc__)
353 do_brk(N_TXTADDR(ex
) & PAGE_MASK
,
354 ex
.a_text
+ex
.a_data
+ PAGE_SIZE
- 1);
355 read_exec(bprm
->dentry
, fd_offset
, (char *) N_TXTADDR(ex
),
356 ex
.a_text
+ex
.a_data
, 0);
358 do_brk(0, ex
.a_text
+ex
.a_data
);
359 read_exec(bprm
->dentry
, 32, (char *) 0, ex
.a_text
+ex
.a_data
, 0);
361 flush_icache_range((unsigned long) 0,
362 (unsigned long) ex
.a_text
+ex
.a_data
);
364 if ((ex
.a_text
& 0xfff || ex
.a_data
& 0xfff) &&
365 (N_MAGIC(ex
) != NMAGIC
))
366 printk(KERN_NOTICE
"executable not page aligned\n");
368 fd
= open_dentry(bprm
->dentry
, O_RDONLY
);
373 if ((fd_offset
& ~PAGE_MASK
) != 0) {
375 "fd_offset is not page aligned. Please convert program: %s\n",
376 file
->f_dentry
->d_name
.name
380 if (!file
->f_op
|| !file
->f_op
->mmap
|| ((fd_offset
& ~PAGE_MASK
) != 0)) {
383 do_brk(0, ex
.a_text
+ex
.a_data
);
384 read_exec(bprm
->dentry
, fd_offset
,
385 (char *) N_TXTADDR(ex
), ex
.a_text
+ex
.a_data
, 0);
386 flush_icache_range((unsigned long) N_TXTADDR(ex
),
387 (unsigned long) N_TXTADDR(ex
) +
388 ex
.a_text
+ex
.a_data
);
392 error
= do_mmap(file
, N_TXTADDR(ex
), ex
.a_text
,
393 PROT_READ
| PROT_EXEC
,
394 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
,
397 if (error
!= N_TXTADDR(ex
)) {
400 send_sig(SIGKILL
, current
, 0);
404 error
= do_mmap(file
, N_DATADDR(ex
), ex
.a_data
,
405 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
406 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
,
407 fd_offset
+ ex
.a_text
);
410 if (error
!= N_DATADDR(ex
)) {
411 send_sig(SIGKILL
, current
, 0);
416 if (current
->exec_domain
&& current
->exec_domain
->module
)
417 __MOD_DEC_USE_COUNT(current
->exec_domain
->module
);
418 if (current
->binfmt
&& current
->binfmt
->module
)
419 __MOD_DEC_USE_COUNT(current
->binfmt
->module
);
420 current
->exec_domain
= lookup_exec_domain(current
->personality
);
421 current
->binfmt
= &aout_format
;
422 if (current
->exec_domain
&& current
->exec_domain
->module
)
423 __MOD_INC_USE_COUNT(current
->exec_domain
->module
);
424 if (current
->binfmt
&& current
->binfmt
->module
)
425 __MOD_INC_USE_COUNT(current
->binfmt
->module
);
427 set_brk(current
->mm
->start_brk
, current
->mm
->brk
);
429 retval
= setup_arg_pages(bprm
);
431 /* Someone check-me: is this error path enough? */
432 send_sig(SIGKILL
, current
, 0);
436 current
->mm
->start_stack
=
437 (unsigned long) create_aout_tables((char *) bprm
->p
, bprm
);
439 regs
->gp
= ex
.a_gpvalue
;
441 start_thread(regs
, ex
.a_entry
, current
->mm
->start_stack
);
442 if (current
->flags
& PF_PTRACED
)
443 send_sig(SIGTRAP
, current
, 0);
449 load_aout_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
454 retval
= do_load_aout_binary(bprm
, regs
);
460 do_load_aout_library(int fd
)
463 struct inode
* inode
;
464 unsigned long bss
, start_addr
, len
;
476 inode
= file
->f_dentry
->d_inode
;
479 /* N.B. Save current fs? */
481 error
= file
->f_op
->read(file
, (char *) &ex
, sizeof(ex
), &offset
);
483 if (error
!= sizeof(ex
))
486 /* We come in here for the regular a.out style of shared libraries */
487 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != QMAGIC
) || N_TRSIZE(ex
) ||
488 N_DRSIZE(ex
) || ((ex
.a_entry
& 0xfff) && N_MAGIC(ex
) == ZMAGIC
) ||
489 inode
->i_size
< ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
493 if (N_MAGIC(ex
) == ZMAGIC
&& N_TXTOFF(ex
) &&
494 (N_TXTOFF(ex
) < inode
->i_sb
->s_blocksize
)) {
495 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
502 /* For QMAGIC, the starting address is 0x20 into the page. We mask
503 this off to get the starting address for the page */
505 start_addr
= ex
.a_entry
& 0xfffff000;
507 if ((N_TXTOFF(ex
) & ~PAGE_MASK
) != 0) {
509 "N_TXTOFF is not page aligned. Please convert library: %s\n",
510 file
->f_dentry
->d_name
.name
513 do_mmap(NULL
, start_addr
& PAGE_MASK
, ex
.a_text
+ ex
.a_data
+ ex
.a_bss
,
514 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
515 MAP_FIXED
| MAP_PRIVATE
, 0);
517 read_exec(file
->f_dentry
, N_TXTOFF(ex
),
518 (char *)start_addr
, ex
.a_text
+ ex
.a_data
, 0);
519 flush_icache_range((unsigned long) start_addr
,
520 (unsigned long) start_addr
+ ex
.a_text
+ ex
.a_data
);
525 /* Now use mmap to map the library into memory. */
526 error
= do_mmap(file
, start_addr
, ex
.a_text
+ ex
.a_data
,
527 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
528 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
531 if (error
!= start_addr
)
534 len
= PAGE_ALIGN(ex
.a_text
+ ex
.a_data
);
535 bss
= ex
.a_text
+ ex
.a_data
+ ex
.a_bss
;
537 error
= do_brk(start_addr
+ len
, bss
- len
);
539 if (error
!= start_addr
+ len
)
551 load_aout_library(int fd
)
556 retval
= do_load_aout_library(fd
);
562 int __init
init_aout_binfmt(void)
564 return register_binfmt(&aout_format
);
568 int init_module(void) {
569 return init_aout_binfmt();
572 void cleanup_module( void) {
573 unregister_binfmt(&aout_format
);