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/pgalloc.h>
32 static int load_aout_binary(struct linux_binprm
*, struct pt_regs
* regs
);
33 static int load_aout_library(struct file
*);
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
= {
39 NULL
, THIS_MODULE
, load_aout_binary
, load_aout_library
, aout_core_dump
, PAGE_SIZE
42 static void set_brk(unsigned long start
, unsigned long end
)
44 start
= PAGE_ALIGN(start
);
45 end
= PAGE_ALIGN(end
);
48 do_brk(start
, end
- start
);
52 * These are the only things you should do on a core-file: use only these
53 * macros to write out all the necessary info.
56 static int dump_write(struct file
*file
, const void *addr
, int nr
)
58 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
61 #define DUMP_WRITE(addr, nr) \
62 if (!dump_write(file, (void *)(addr), (nr))) \
65 #define DUMP_SEEK(offset) \
66 if (file->f_op->llseek) { \
67 if (file->f_op->llseek(file,(offset),0) != (offset)) \
69 } else file->f_pos = (offset)
72 * Routine writes a core dump image in the current directory.
73 * Currently only a stub-function.
75 * Note that setuid/setgid files won't make a core-dump if the uid/gid
76 * changed due to the set[u|g]id. It's enforced by the "current->dumpable"
77 * field, which also makes sure the core-dumps won't be recursive if the
78 * dumping of the process results in another error..
81 static int aout_core_dump(long signr
, struct pt_regs
* regs
, struct file
*file
)
85 unsigned long dump_start
, dump_size
;
87 #if defined(__alpha__)
88 # define START_DATA(u) (u.start_data)
89 #elif defined(__sparc__)
90 # define START_DATA(u) (u.u_tsize)
91 #elif defined(__i386__) || defined(__mc68000__)
92 # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
95 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
97 # define START_STACK(u) (u.start_stack)
103 current
->flags
|= PF_DUMPCORE
;
104 strncpy(dump
.u_comm
, current
->comm
, sizeof(current
->comm
));
106 dump
.u_ar0
= (void *)(((unsigned long)(&dump
.regs
)) - ((unsigned long)(&dump
)));
109 dump_thread(regs
, &dump
);
111 /* If the size of the dump file exceeds the rlimit, then see what would happen
112 if we wrote the stack, but not the data area. */
114 if ((dump
.u_dsize
+dump
.u_ssize
) >
115 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
118 if ((dump
.u_dsize
+dump
.u_ssize
+1) * PAGE_SIZE
>
119 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
123 /* Make sure we have enough room to write the stack and data areas. */
126 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
129 if ((dump
.u_ssize
+1) * PAGE_SIZE
>
130 current
->rlim
[RLIMIT_CORE
].rlim_cur
)
134 /* make sure we actually have a data and stack area to dump */
137 if (verify_area(VERIFY_READ
, (void *) START_DATA(dump
), dump
.u_dsize
))
139 if (verify_area(VERIFY_READ
, (void *) START_STACK(dump
), dump
.u_ssize
))
142 if (verify_area(VERIFY_READ
, (void *) START_DATA(dump
), dump
.u_dsize
<< PAGE_SHIFT
))
144 if (verify_area(VERIFY_READ
, (void *) START_STACK(dump
), dump
.u_ssize
<< PAGE_SHIFT
))
150 DUMP_WRITE(&dump
,sizeof(dump
));
151 /* Now dump all of the user data. Include malloced stuff as well */
153 DUMP_SEEK(PAGE_SIZE
);
155 /* now we start writing out the user space info */
157 /* Dump the data area */
158 if (dump
.u_dsize
!= 0) {
159 dump_start
= START_DATA(dump
);
161 dump_size
= dump
.u_dsize
;
163 dump_size
= dump
.u_dsize
<< PAGE_SHIFT
;
165 DUMP_WRITE(dump_start
,dump_size
);
167 /* Now prepare to dump the stack area */
168 if (dump
.u_ssize
!= 0) {
169 dump_start
= START_STACK(dump
);
171 dump_size
= dump
.u_ssize
;
173 dump_size
= dump
.u_ssize
<< PAGE_SHIFT
;
175 DUMP_WRITE(dump_start
,dump_size
);
177 /* Finally dump the task struct. Not be used by gdb, but could be useful */
179 DUMP_WRITE(current
,sizeof(*current
));
186 * create_aout_tables() parses the env- and arg-strings in new user
187 * memory and creates the pointer tables from them, and puts their
188 * addresses on the "stack", returning the new stack pointer value.
190 static unsigned long * create_aout_tables(char * p
, struct linux_binprm
* bprm
)
194 int argc
= bprm
->argc
;
195 int envc
= bprm
->envc
;
197 sp
= (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p
);
199 /* This imposes the proper stack alignment for a new process. */
200 sp
= (unsigned long *) (((unsigned long) sp
) & ~7);
201 if ((envc
+argc
+3)&1) --sp
;
204 /* whee.. test-programs are so much fun. */
209 put_user(0x3eb, --sp
);
210 put_user(bprm
->loader
, --sp
);
211 put_user(0x3ea, --sp
);
213 put_user(bprm
->exec
, --sp
);
214 put_user(0x3e9, --sp
);
220 #if defined(__i386__) || defined(__mc68000__)
221 put_user((unsigned long) envp
,--sp
);
222 put_user((unsigned long) argv
,--sp
);
225 current
->mm
->arg_start
= (unsigned long) p
;
234 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
243 current
->mm
->env_end
= (unsigned long) p
;
248 * These are the functions used to load a.out style executables and shared
249 * libraries. There is no binary dependent code anywhere else.
252 static int load_aout_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
256 unsigned long fd_offset
;
260 ex
= *((struct exec
*) bprm
->buf
); /* exec-header */
261 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != OMAGIC
&&
262 N_MAGIC(ex
) != QMAGIC
&& N_MAGIC(ex
) != NMAGIC
) ||
263 N_TRSIZE(ex
) || N_DRSIZE(ex
) ||
264 bprm
->file
->f_dentry
->d_inode
->i_size
< ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
268 fd_offset
= N_TXTOFF(ex
);
270 /* Check initial limits. This avoids letting people circumvent
271 * size limits imposed on them by creating programs with large
272 * arrays in the data or bss.
274 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
275 if (rlim
>= RLIM_INFINITY
)
277 if (ex
.a_data
+ ex
.a_bss
> rlim
)
280 /* Flush all traces of the currently running executable */
281 retval
= flush_old_exec(bprm
);
285 /* OK, This is the point of no return */
286 #if !defined(__sparc__)
287 set_personality(PER_LINUX
);
289 set_personality(PER_SUNOS
);
290 #if !defined(__sparc_v9__)
291 memcpy(¤t
->thread
.core_exec
, &ex
, sizeof(struct exec
));
295 current
->mm
->end_code
= ex
.a_text
+
296 (current
->mm
->start_code
= N_TXTADDR(ex
));
297 current
->mm
->end_data
= ex
.a_data
+
298 (current
->mm
->start_data
= N_DATADDR(ex
));
299 current
->mm
->brk
= ex
.a_bss
+
300 (current
->mm
->start_brk
= N_BSSADDR(ex
));
302 current
->mm
->rss
= 0;
303 current
->mm
->mmap
= NULL
;
305 current
->flags
&= ~PF_FORKNOEXEC
;
307 if (N_MAGIC(ex
) == NMAGIC
) {
308 loff_t pos
= fd_offset
;
309 /* Fuck me plenty... */
311 error
= do_brk(N_TXTADDR(ex
), ex
.a_text
);
312 bprm
->file
->f_op
->read(bprm
->file
, (char *) N_TXTADDR(ex
),
314 error
= do_brk(N_DATADDR(ex
), ex
.a_data
);
315 bprm
->file
->f_op
->read(bprm
->file
, (char *) N_DATADDR(ex
),
321 if (N_MAGIC(ex
) == OMAGIC
) {
323 #if defined(__alpha__) || defined(__sparc__)
325 do_brk(N_TXTADDR(ex
) & PAGE_MASK
,
326 ex
.a_text
+ex
.a_data
+ PAGE_SIZE
- 1);
327 bprm
->file
->f_op
->read(bprm
->file
, (char *) N_TXTADDR(ex
),
328 ex
.a_text
+ex
.a_data
, &pos
);
331 do_brk(0, ex
.a_text
+ex
.a_data
);
332 bprm
->file
->f_op
->read(bprm
->file
, (char *) 0,
333 ex
.a_text
+ex
.a_data
, &pos
);
335 flush_icache_range((unsigned long) 0,
336 (unsigned long) ex
.a_text
+ex
.a_data
);
338 static unsigned long error_time
, error_time2
;
339 if ((ex
.a_text
& 0xfff || ex
.a_data
& 0xfff) &&
340 (N_MAGIC(ex
) != NMAGIC
) && (jiffies
-error_time2
) > 5*HZ
)
342 printk(KERN_NOTICE
"executable not page aligned\n");
343 error_time2
= jiffies
;
346 if ((fd_offset
& ~PAGE_MASK
) != 0 &&
347 (jiffies
-error_time
) > 5*HZ
)
350 "fd_offset is not page aligned. Please convert program: %s\n",
351 bprm
->file
->f_dentry
->d_name
.name
);
352 error_time
= jiffies
;
355 if (!bprm
->file
->f_op
->mmap
||((fd_offset
& ~PAGE_MASK
) != 0)) {
356 loff_t pos
= fd_offset
;
357 do_brk(N_TXTADDR(ex
), ex
.a_text
+ex
.a_data
);
358 bprm
->file
->f_op
->read(bprm
->file
,(char *)N_TXTADDR(ex
),
359 ex
.a_text
+ex
.a_data
, &pos
);
360 flush_icache_range((unsigned long) N_TXTADDR(ex
),
361 (unsigned long) N_TXTADDR(ex
) +
362 ex
.a_text
+ex
.a_data
);
366 error
= do_mmap(bprm
->file
, N_TXTADDR(ex
), ex
.a_text
,
367 PROT_READ
| PROT_EXEC
,
368 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
,
371 if (error
!= N_TXTADDR(ex
)) {
372 send_sig(SIGKILL
, current
, 0);
376 error
= do_mmap(bprm
->file
, N_DATADDR(ex
), ex
.a_data
,
377 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
378 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
,
379 fd_offset
+ ex
.a_text
);
380 if (error
!= N_DATADDR(ex
)) {
381 send_sig(SIGKILL
, current
, 0);
386 set_binfmt(&aout_format
);
388 set_brk(current
->mm
->start_brk
, current
->mm
->brk
);
390 retval
= setup_arg_pages(bprm
);
392 /* Someone check-me: is this error path enough? */
393 send_sig(SIGKILL
, current
, 0);
397 current
->mm
->start_stack
=
398 (unsigned long) create_aout_tables((char *) bprm
->p
, bprm
);
400 regs
->gp
= ex
.a_gpvalue
;
402 start_thread(regs
, ex
.a_entry
, current
->mm
->start_stack
);
403 if (current
->flags
& PF_PTRACED
)
404 send_sig(SIGTRAP
, current
, 0);
408 static int load_aout_library(struct file
*file
)
410 struct inode
* inode
;
411 unsigned long bss
, start_addr
, len
;
416 inode
= file
->f_dentry
->d_inode
;
419 error
= kernel_read(file
, 0, (char *) &ex
, sizeof(ex
));
420 if (error
!= sizeof(ex
))
423 /* We come in here for the regular a.out style of shared libraries */
424 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != QMAGIC
) || N_TRSIZE(ex
) ||
425 N_DRSIZE(ex
) || ((ex
.a_entry
& 0xfff) && N_MAGIC(ex
) == ZMAGIC
) ||
426 inode
->i_size
< ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
433 /* For QMAGIC, the starting address is 0x20 into the page. We mask
434 this off to get the starting address for the page */
436 start_addr
= ex
.a_entry
& 0xfffff000;
438 if ((N_TXTOFF(ex
) & ~PAGE_MASK
) != 0) {
439 static unsigned long error_time
;
440 loff_t pos
= N_TXTOFF(ex
);
442 if ((jiffies
-error_time
) > 5*HZ
)
445 "N_TXTOFF is not page aligned. Please convert library: %s\n",
446 file
->f_dentry
->d_name
.name
);
447 error_time
= jiffies
;
450 do_brk(start_addr
, ex
.a_text
+ ex
.a_data
+ ex
.a_bss
);
452 file
->f_op
->read(file
, (char *)start_addr
,
453 ex
.a_text
+ ex
.a_data
, &pos
);
454 flush_icache_range((unsigned long) start_addr
,
455 (unsigned long) start_addr
+ ex
.a_text
+ ex
.a_data
);
460 /* Now use mmap to map the library into memory. */
461 down(¤t
->mm
->mmap_sem
);
462 error
= do_mmap(file
, start_addr
, ex
.a_text
+ ex
.a_data
,
463 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
464 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
466 up(¤t
->mm
->mmap_sem
);
468 if (error
!= start_addr
)
471 len
= PAGE_ALIGN(ex
.a_text
+ ex
.a_data
);
472 bss
= ex
.a_text
+ ex
.a_data
+ ex
.a_bss
;
474 error
= do_brk(start_addr
+ len
, bss
- len
);
476 if (error
!= start_addr
+ len
)
484 static int __init
init_aout_binfmt(void)
486 return register_binfmt(&aout_format
);
489 static void __exit
exit_aout_binfmt(void)
491 unregister_binfmt(&aout_format
);
496 module_init(init_aout_binfmt
);
497 module_exit(exit_aout_binfmt
);