MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / binfmt_aout.c
blob3d99e70d205a56a23cc541901b500691ba1cc59b
1 /*
2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
5 */
7 #include <linux/module.h>
9 #include <linux/time.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.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>
17 #include <linux/fs.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/cacheflush.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 .module = THIS_MODULE,
40 .load_binary = load_aout_binary,
41 .load_shlib = load_aout_library,
42 .core_dump = aout_core_dump,
43 .min_coredump = PAGE_SIZE
46 static void set_brk(unsigned long start, unsigned long end)
48 start = PAGE_ALIGN(start);
49 end = PAGE_ALIGN(end);
50 if (end <= start)
51 return;
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))) \
67 goto end_coredump;
69 #define DUMP_SEEK(offset) \
70 if (file->f_op->llseek) { \
71 if (file->f_op->llseek(file,(offset),0) != (offset)) \
72 goto end_coredump; \
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 aout_core_dump(long signr, struct pt_regs * regs, struct file *file)
87 mm_segment_t fs;
88 int has_dumped = 0;
89 unsigned long dump_start, dump_size;
90 struct user dump;
91 #if defined(__alpha__)
92 # define START_DATA(u) (u.start_data)
93 #elif defined(__arm__)
94 # define START_DATA(u) ((u.u_tsize << PAGE_SHIFT) + u.start_code)
95 #elif defined(__sparc__)
96 # define START_DATA(u) (u.u_tsize)
97 #elif defined(__i386__) || defined(__mc68000__) || defined(__arch_um__)
98 # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
99 #endif
100 #ifdef __sparc__
101 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
102 #else
103 # define START_STACK(u) (u.start_stack)
104 #endif
106 fs = get_fs();
107 set_fs(KERNEL_DS);
108 has_dumped = 1;
109 current->flags |= PF_DUMPCORE;
110 strncpy(dump.u_comm, current->comm, sizeof(current->comm));
111 #ifndef __sparc__
112 dump.u_ar0 = (void *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
113 #endif
114 dump.signal = signr;
115 dump_thread(regs, &dump);
117 /* If the size of the dump file exceeds the rlimit, then see what would happen
118 if we wrote the stack, but not the data area. */
119 #ifdef __sparc__
120 if ((dump.u_dsize+dump.u_ssize) >
121 current->rlim[RLIMIT_CORE].rlim_cur)
122 dump.u_dsize = 0;
123 #else
124 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
125 current->rlim[RLIMIT_CORE].rlim_cur)
126 dump.u_dsize = 0;
127 #endif
129 /* Make sure we have enough room to write the stack and data areas. */
130 #ifdef __sparc__
131 if ((dump.u_ssize) >
132 current->rlim[RLIMIT_CORE].rlim_cur)
133 dump.u_ssize = 0;
134 #else
135 if ((dump.u_ssize+1) * PAGE_SIZE >
136 current->rlim[RLIMIT_CORE].rlim_cur)
137 dump.u_ssize = 0;
138 #endif
140 /* make sure we actually have a data and stack area to dump */
141 set_fs(USER_DS);
142 #ifdef __sparc__
143 if (verify_area(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize))
144 dump.u_dsize = 0;
145 if (verify_area(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize))
146 dump.u_ssize = 0;
147 #else
148 if (verify_area(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
149 dump.u_dsize = 0;
150 if (verify_area(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
151 dump.u_ssize = 0;
152 #endif
154 set_fs(KERNEL_DS);
155 /* struct user */
156 DUMP_WRITE(&dump,sizeof(dump));
157 /* Now dump all of the user data. Include malloced stuff as well */
158 #ifndef __sparc__
159 DUMP_SEEK(PAGE_SIZE);
160 #endif
161 /* now we start writing out the user space info */
162 set_fs(USER_DS);
163 /* Dump the data area */
164 if (dump.u_dsize != 0) {
165 dump_start = START_DATA(dump);
166 #ifdef __sparc__
167 dump_size = dump.u_dsize;
168 #else
169 dump_size = dump.u_dsize << PAGE_SHIFT;
170 #endif
171 DUMP_WRITE(dump_start,dump_size);
173 /* Now prepare to dump the stack area */
174 if (dump.u_ssize != 0) {
175 dump_start = START_STACK(dump);
176 #ifdef __sparc__
177 dump_size = dump.u_ssize;
178 #else
179 dump_size = dump.u_ssize << PAGE_SHIFT;
180 #endif
181 DUMP_WRITE(dump_start,dump_size);
183 /* Finally dump the task struct. Not be used by gdb, but could be useful */
184 set_fs(KERNEL_DS);
185 DUMP_WRITE(current,sizeof(*current));
186 end_coredump:
187 set_fs(fs);
188 return has_dumped;
192 * create_aout_tables() parses the env- and arg-strings in new user
193 * memory and creates the pointer tables from them, and puts their
194 * addresses on the "stack", returning the new stack pointer value.
196 static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
198 char __user * __user *argv;
199 char __user * __user *envp;
200 unsigned long __user *sp;
201 int argc = bprm->argc;
202 int envc = bprm->envc;
204 sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
205 #ifdef __sparc__
206 /* This imposes the proper stack alignment for a new process. */
207 sp = (void __user *) (((unsigned long) sp) & ~7);
208 if ((envc+argc+3)&1) --sp;
209 #endif
210 #ifdef __alpha__
211 /* whee.. test-programs are so much fun. */
212 put_user(0, --sp);
213 put_user(0, --sp);
214 if (bprm->loader) {
215 put_user(0, --sp);
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);
222 #endif
223 sp -= envc+1;
224 envp = (char __user * __user *) sp;
225 sp -= argc+1;
226 argv = (char __user * __user *) 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);
230 #endif
231 put_user(argc,--sp);
232 current->mm->arg_start = (unsigned long) p;
233 while (argc-->0) {
234 char c;
235 put_user(p,argv++);
236 do {
237 get_user(c,p++);
238 } while (c);
240 put_user(NULL,argv);
241 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
242 while (envc-->0) {
243 char c;
244 put_user(p,envp++);
245 do {
246 get_user(c,p++);
247 } while (c);
249 put_user(NULL,envp);
250 current->mm->env_end = (unsigned long) p;
251 return sp;
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)
261 struct exec ex;
262 unsigned long error;
263 unsigned long fd_offset;
264 unsigned long rlim;
265 int retval;
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)) {
272 return -ENOEXEC;
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)
283 rlim = ~0;
284 if (ex.a_data + ex.a_bss > rlim)
285 return -ENOMEM;
287 /* Flush all traces of the currently running executable */
288 retval = flush_old_exec(bprm);
289 if (retval)
290 return retval;
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(&current->thread.core_exec, &ex, sizeof(struct exec));
299 #endif
300 #else
301 set_personality(PER_LINUX);
302 #endif
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 = current->mm->mmap_base;
312 current->mm->rss = 0;
313 current->mm->mmap = NULL;
314 compute_creds(bprm);
315 current->flags &= ~PF_FORKNOEXEC;
316 #ifdef __sparc__
317 if (N_MAGIC(ex) == NMAGIC) {
318 loff_t pos = fd_offset;
319 /* Fuck me plenty... */
320 /* <AOL></AOL> */
321 error = do_brk(N_TXTADDR(ex), ex.a_text);
322 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
323 ex.a_text, &pos);
324 error = do_brk(N_DATADDR(ex), ex.a_data);
325 bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
326 ex.a_data, &pos);
327 goto beyond_if;
329 #endif
331 if (N_MAGIC(ex) == OMAGIC) {
332 unsigned long text_addr, map_size;
333 loff_t pos;
335 text_addr = N_TXTADDR(ex);
337 #if defined(__alpha__) || defined(__sparc__)
338 pos = fd_offset;
339 map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
340 #else
341 pos = 32;
342 map_size = ex.a_text+ex.a_data;
343 #endif
345 error = do_brk(text_addr & PAGE_MASK, map_size);
346 if (error != (text_addr & PAGE_MASK)) {
347 send_sig(SIGKILL, current, 0);
348 return error;
351 error = bprm->file->f_op->read(bprm->file,
352 (char __user *)text_addr,
353 ex.a_text+ex.a_data, &pos);
354 if ((signed long)error < 0) {
355 send_sig(SIGKILL, current, 0);
356 return error;
359 flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
360 } else {
361 static unsigned long error_time, error_time2;
362 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
363 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time2) > 5*HZ)
365 printk(KERN_NOTICE "executable not page aligned\n");
366 error_time2 = jiffies;
369 if ((fd_offset & ~PAGE_MASK) != 0 &&
370 (jiffies-error_time) > 5*HZ)
372 printk(KERN_WARNING
373 "fd_offset is not page aligned. Please convert program: %s\n",
374 bprm->file->f_dentry->d_name.name);
375 error_time = jiffies;
378 if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
379 loff_t pos = fd_offset;
380 do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
381 bprm->file->f_op->read(bprm->file,
382 (char __user *)N_TXTADDR(ex),
383 ex.a_text+ex.a_data, &pos);
384 flush_icache_range((unsigned long) N_TXTADDR(ex),
385 (unsigned long) N_TXTADDR(ex) +
386 ex.a_text+ex.a_data);
387 goto beyond_if;
390 down_write(&current->mm->mmap_sem);
391 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
392 PROT_READ | PROT_EXEC,
393 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
394 fd_offset);
395 up_write(&current->mm->mmap_sem);
397 if (error != N_TXTADDR(ex)) {
398 send_sig(SIGKILL, current, 0);
399 return error;
402 down_write(&current->mm->mmap_sem);
403 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
404 PROT_READ | PROT_WRITE | PROT_EXEC,
405 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
406 fd_offset + ex.a_text);
407 up_write(&current->mm->mmap_sem);
408 if (error != N_DATADDR(ex)) {
409 send_sig(SIGKILL, current, 0);
410 return error;
413 beyond_if:
414 set_binfmt(&aout_format);
416 set_brk(current->mm->start_brk, current->mm->brk);
418 retval = setup_arg_pages(bprm, EXSTACK_DEFAULT);
419 if (retval < 0) {
420 /* Someone check-me: is this error path enough? */
421 send_sig(SIGKILL, current, 0);
422 return retval;
425 current->mm->start_stack =
426 (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
427 #ifdef __alpha__
428 regs->gp = ex.a_gpvalue;
429 #endif
430 start_thread(regs, ex.a_entry, current->mm->start_stack);
431 if (unlikely(current->ptrace & PT_PTRACED)) {
432 if (current->ptrace & PT_TRACE_EXEC)
433 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
434 else
435 send_sig(SIGTRAP, current, 0);
437 return 0;
440 static int load_aout_library(struct file *file)
442 struct inode * inode;
443 unsigned long bss, start_addr, len;
444 unsigned long error;
445 int retval;
446 struct exec ex;
448 inode = file->f_dentry->d_inode;
450 retval = -ENOEXEC;
451 error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
452 if (error != sizeof(ex))
453 goto out;
455 /* We come in here for the regular a.out style of shared libraries */
456 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
457 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
458 i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
459 goto out;
462 if (N_FLAGS(ex))
463 goto out;
465 /* For QMAGIC, the starting address is 0x20 into the page. We mask
466 this off to get the starting address for the page */
468 start_addr = ex.a_entry & 0xfffff000;
470 if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
471 static unsigned long error_time;
472 loff_t pos = N_TXTOFF(ex);
474 if ((jiffies-error_time) > 5*HZ)
476 printk(KERN_WARNING
477 "N_TXTOFF is not page aligned. Please convert library: %s\n",
478 file->f_dentry->d_name.name);
479 error_time = jiffies;
482 do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
484 file->f_op->read(file, (char __user *)start_addr,
485 ex.a_text + ex.a_data, &pos);
486 flush_icache_range((unsigned long) start_addr,
487 (unsigned long) start_addr + ex.a_text + ex.a_data);
489 retval = 0;
490 goto out;
492 /* Now use mmap to map the library into memory. */
493 down_write(&current->mm->mmap_sem);
494 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
495 PROT_READ | PROT_WRITE | PROT_EXEC,
496 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
497 N_TXTOFF(ex));
498 up_write(&current->mm->mmap_sem);
499 retval = error;
500 if (error != start_addr)
501 goto out;
503 len = PAGE_ALIGN(ex.a_text + ex.a_data);
504 bss = ex.a_text + ex.a_data + ex.a_bss;
505 if (bss > len) {
506 error = do_brk(start_addr + len, bss - len);
507 retval = error;
508 if (error != start_addr + len)
509 goto out;
511 retval = 0;
512 out:
513 return retval;
516 static int __init init_aout_binfmt(void)
518 return register_binfmt(&aout_format);
521 static void __exit exit_aout_binfmt(void)
523 unregister_binfmt(&aout_format);
526 core_initcall(init_aout_binfmt);
527 module_exit(exit_aout_binfmt);
528 MODULE_LICENSE("GPL");