Partial rewrite.
[linux-2.6/linux-mips.git] / fs / binfmt_flat.c
blob1c7699cea9143f5692e451b8bd63800e7b9628bb
1 /*
2 * linux/fs/binfmt_flat.c
4 * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@uclinux.org>
5 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
7 * based heavily on:
9 * linux/fs/binfmt_aout.c:
10 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
11 * linux/fs/binfmt_flat.c for 2.0 kernel
12 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
13 * JAN/99 -- coded full program relocation (gerg@snapgear.com)
16 #include <linux/module.h>
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/mman.h>
22 #include <linux/a.out.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/string.h>
26 #include <linux/fs.h>
27 #include <linux/file.h>
28 #include <linux/stat.h>
29 #include <linux/fcntl.h>
30 #include <linux/ptrace.h>
31 #include <linux/user.h>
32 #include <linux/slab.h>
33 #include <linux/binfmts.h>
34 #include <linux/personality.h>
35 #include <linux/init.h>
36 #include <linux/flat.h>
38 #include <asm/byteorder.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgalloc.h>
42 #include <asm/unaligned.h>
43 #include <asm/cacheflush.h>
45 #undef DEBUG
46 #ifdef DEBUG
47 #define DBG_FLT(a...) printk(##a)
48 #else
49 #define DBG_FLT(a...)
50 #endif
52 static int load_flat_binary(struct linux_binprm *, struct pt_regs * regs);
53 static int load_flat_library(struct file*);
54 extern void dump_thread(struct pt_regs *, struct user *);
56 static struct linux_binfmt flat_format = {
57 NULL, THIS_MODULE, load_flat_binary, load_flat_library, NULL, PAGE_SIZE
62 * create_flat_tables() parses the env- and arg-strings in new user
63 * memory and creates the pointer tables from them, and puts their
64 * addresses on the "stack", returning the new stack pointer value.
66 static unsigned long create_flat_tables(
67 unsigned long pp,
68 struct linux_binprm * bprm)
70 unsigned long *argv,*envp;
71 unsigned long * sp;
72 char * p = (char*)pp;
73 int argc = bprm->argc;
74 int envc = bprm->envc;
75 char dummy;
77 sp = (unsigned long *) ((-(unsigned long)sizeof(char *))&(unsigned long) p);
79 sp -= envc+1;
80 envp = sp;
81 sp -= argc+1;
82 argv = sp;
83 if (flat_argvp_envp_on_stack()) {
84 put_user((unsigned long) envp, --sp);
85 put_user((unsigned long) argv, --sp);
87 put_user(argc,--sp);
88 current->mm->arg_start = (unsigned long) p;
89 while (argc-->0) {
90 put_user((unsigned long) p, argv++);
91 do {
92 get_user(dummy, p); p++;
93 } while (dummy);
95 put_user((unsigned long) NULL, argv);
96 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
97 while (envc-->0) {
98 put_user((unsigned long)p, envp); envp++;
99 do {
100 get_user(dummy, p); p++;
101 } while (dummy);
103 put_user((unsigned long) NULL, envp);
104 current->mm->env_end = (unsigned long) p;
105 return (unsigned long)sp;
109 #ifdef CONFIG_BINFMT_ZFLAT
111 #include <linux/zlib.h>
113 #define LBUFSIZE 4000
115 /* gzip flag byte */
116 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
117 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
118 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
119 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
120 #define COMMENT 0x10 /* bit 4 set: file comment present */
121 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
122 #define RESERVED 0xC0 /* bit 6,7: reserved */
124 static int decompress_exec(
125 struct linux_binprm *bprm,
126 unsigned long offset,
127 char *dst,
128 long len,
129 int fd)
131 unsigned char *buf;
132 z_stream strm;
133 loff_t fpos;
134 int ret;
136 DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len);
138 memset(&strm, 0, sizeof(strm));
139 strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
140 if (strm.workspace == NULL) {
141 DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
142 return -ENOMEM;
144 buf = kmalloc(LBUFSIZE, GFP_KERNEL);
145 if (buf == NULL) {
146 DBG_FLT("binfmt_flat: no memory for read buffer\n");
147 return -ENOMEM;
150 /* Read in first chunk of data and parse gzip header. */
151 fpos = offset;
152 ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
154 strm.next_in = buf;
155 strm.avail_in = ret;
156 strm.total_in = 0;
158 /* Check minimum size -- gzip header */
159 if (ret < 10) {
160 DBG_FLT("binfmt_flat: file too small?\n");
161 return -ENOEXEC;
164 /* Check gzip magic number */
165 if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
166 DBG_FLT("binfmt_flat: unknown compression magic?\n");
167 return -ENOEXEC;
170 /* Check gzip method */
171 if (buf[2] != 8) {
172 DBG_FLT("binfmt_flat: unknown compression method?\n");
173 return -ENOEXEC;
175 /* Check gzip flags */
176 if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
177 (buf[3] & RESERVED)) {
178 DBG_FLT("binfmt_flat: unknown flags?\n");
179 return -ENOEXEC;
182 ret = 10;
183 if (buf[3] & EXTRA_FIELD) {
184 ret += 2 + buf[10] + (buf[11] << 8);
185 if (unlikely(LBUFSIZE == ret)) {
186 DBG_FLAT("binfmt_flat: buffer overflow (EXTRA)?\n");
187 return -ENOEXEC;
190 if (buf[3] & ORIG_NAME) {
191 for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
193 if (unlikely(LBUFSIZE == ret)) {
194 DBG_FLAT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
195 return -ENOEXEC;
198 if (buf[3] & COMMENT) {
199 for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
201 if (unlikely(LBUFSIZE == ret)) {
202 DBG_FLAT("binfmt_flat: buffer overflow (COMMENT)?\n");
203 return -ENOEXEC;
207 strm.next_in += ret;
208 strm.avail_in -= ret;
210 strm.next_out = dst;
211 strm.avail_out = len;
212 strm.total_out = 0;
214 if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
215 DBG_FLT("binfmt_flat: zlib init failed?\n");
216 return -ENOEXEC;
219 while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
220 ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
221 if (ret <= 0)
222 break;
223 if (ret >= (unsigned long) -4096)
224 break;
225 len -= ret;
227 strm.next_in = buf;
228 strm.avail_in = ret;
229 strm.total_in = 0;
232 if (ret < 0) {
233 DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
234 ret, strm.msg);
235 return -ENOEXEC;
238 zlib_inflateEnd(&strm);
239 kfree(buf);
240 kfree(strm.workspace);
241 return 0;
244 #endif /* CONFIG_BINFMT_ZFLAT */
247 static unsigned long
248 calc_reloc(unsigned long r, unsigned long text_len)
250 unsigned long addr;
252 if (r > current->mm->start_brk - current->mm->start_data + text_len) {
253 printk("BINFMT_FLAT: reloc outside program 0x%x (0 - 0x%x), killing!\n",
254 (int) r,(int)(current->mm->start_brk-current->mm->start_code));
255 send_sig(SIGSEGV, current, 0);
256 return(current->mm->start_brk); /* return something safe to write to */
259 if (r < text_len) {
260 /* In text segment */
261 return r + current->mm->start_code;
265 * we allow inclusive ranges here so that programs may do things
266 * like reference the end of data (_end) without failing these tests
268 addr = r - text_len + current->mm->start_data;
269 if (addr >= current->mm->start_code &&
270 addr <= current->mm->start_code + text_len)
271 return(addr);
273 if (addr >= current->mm->start_data &&
274 addr <= current->mm->start_brk)
275 return(addr);
277 printk("BINFMT_FLAT: reloc addr outside text/data 0x%x "
278 "code(0x%x - 0x%x) data(0x%x - 0x%x) killing\n", (int) addr,
279 (int) current->mm->start_code,
280 (int) (current->mm->start_code + text_len),
281 (int) current->mm->start_data,
282 (int) current->mm->start_brk);
283 send_sig(SIGSEGV, current, 0);
285 return(current->mm->start_brk); /* return something safe to write to */
289 void old_reloc(unsigned long rl)
291 #ifdef DEBUG
292 char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
293 #endif
294 flat_v2_reloc_t r;
295 unsigned long *ptr;
297 r.value = rl;
298 #if defined(CONFIG_COLDFIRE)
299 ptr = (unsigned long *) (current->mm->start_code + r.reloc.offset);
300 #else
301 ptr = (unsigned long *) (current->mm->start_data + r.reloc.offset);
302 #endif
304 #ifdef DEBUG
305 printk("Relocation of variable at DATASEG+%x "
306 "(address %p, currently %x) into segment %s\n",
307 r.reloc.offset, ptr, (int)*ptr, segment[r.reloc.type]);
308 #endif
310 switch (r.reloc.type) {
311 case OLD_FLAT_RELOC_TYPE_TEXT:
312 *ptr += current->mm->start_code;
313 break;
314 case OLD_FLAT_RELOC_TYPE_DATA:
315 *ptr += current->mm->start_data;
316 break;
317 case OLD_FLAT_RELOC_TYPE_BSS:
318 *ptr += current->mm->end_data;
319 break;
320 default:
321 printk("BINFMT_FLAT: Unknown relocation type=%x\n", r.reloc.type);
322 break;
325 #ifdef DEBUG
326 printk("Relocation became %x\n", (int)*ptr);
327 #endif
332 * These are the functions used to load flat style executables and shared
333 * libraries. There is no binary dependent code anywhere else.
336 static int load_flat_binary(struct linux_binprm * bprm, struct pt_regs * regs)
338 struct flat_hdr * hdr;
339 unsigned long textpos = 0, datapos = 0, result;
340 unsigned long text_len, data_len, bss_len, stack_len, flags;
341 unsigned long memp = 0, memkasked = 0; /* for finding the brk area */
342 unsigned long extra, rlim;
343 unsigned long p = bprm->p;
344 unsigned long *reloc = 0, *rp;
345 struct inode *inode;
346 int i, rev, relocs = 0;
347 loff_t fpos;
349 DBG_FLT("BINFMT_FLAT: Loading file: %x\n", bprm->file);
351 hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */
352 inode = bprm->file->f_dentry->d_inode;
354 text_len = ntohl(hdr->data_start);
355 data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
356 bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
357 stack_len = ntohl(hdr->stack_size);
358 relocs = ntohl(hdr->reloc_count);
359 flags = ntohl(hdr->flags);
360 rev = ntohl(hdr->rev);
363 * We have to add the size of our arguments to our stack size
364 * otherwise it's too easy for users to create stack overflows
365 * by passing in a huge argument list. And yes, we have to be
366 * pedantic and include space for the argv/envp array as it may have
367 * a lot of entries.
369 #define TOP_OF_ARGS (PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *))
370 stack_len += TOP_OF_ARGS - bprm->p; /* the strings */
371 stack_len += (bprm->argc + 1) * sizeof(char *); /* the argv array */
372 stack_len += (bprm->envc + 1) * sizeof(char *); /* the envp array */
374 if (strncmp(hdr->magic, "bFLT", 4) ||
375 (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION)) {
377 * because a lot of people do not manage to produce good
378 * flat binaries, we leave this printk to help them realise
379 * the problem. We only print the error if it's
380 * not a script file.
382 if (strncmp(hdr->magic, "#!", 2))
383 printk("BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
384 rev, (int) FLAT_VERSION);
385 return -ENOEXEC;
389 * fix up the flags for the older format, there were all kinds
390 * of endian hacks, this only works for the simple cases
392 if (rev == OLD_FLAT_VERSION && flags)
393 flags = FLAT_FLAG_RAM;
395 #ifndef CONFIG_BINFMT_ZFLAT
396 if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
397 printk("Support for ZFLAT executables is not enabled.\n");
398 return -ENOEXEC;
400 #endif
403 * Check initial limits. This avoids letting people circumvent
404 * size limits imposed on them by creating programs with large
405 * arrays in the data or bss.
407 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
408 if (rlim >= RLIM_INFINITY)
409 rlim = ~0;
410 if (data_len + bss_len > rlim)
411 return -ENOMEM;
413 /* Flush all traces of the currently running executable */
414 result = flush_old_exec(bprm);
415 if (result)
416 return result;
418 /* OK, This is the point of no return */
419 set_personality(PER_LINUX);
422 * there are a couple of cases here, the separate code/data
423 * case, and then the fully copied to RAM case which lumps
424 * it all together.
426 if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
428 * this should give us a ROM ptr, but if it doesn't we don't
429 * really care
431 DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
433 down_write(&current->mm->mmap_sem);
434 textpos = do_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC, 0, 0);
435 up_write(&current->mm->mmap_sem);
436 if (!textpos || textpos >= (unsigned long) -4096) {
437 if (!textpos)
438 textpos = (unsigned long) -ENOMEM;
439 printk("Unable to mmap process text, errno %d\n", (int)-textpos);
442 extra = max(bss_len + stack_len, relocs * sizeof(unsigned long)),
444 down_write(&current->mm->mmap_sem);
445 datapos = do_mmap(0, 0, data_len + extra,
446 PROT_READ|PROT_WRITE|PROT_EXEC, 0, 0);
447 up_write(&current->mm->mmap_sem);
449 if (datapos == 0 || datapos >= (unsigned long)-4096) {
450 if (!datapos)
451 datapos = (unsigned long) -ENOMEM;
452 printk("Unable to allocate RAM for process data, errno %d\n",
453 (int)-datapos);
454 do_munmap(current->mm, textpos, text_len);
455 return datapos;
458 DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
459 data_len + bss_len + stack_len, datapos);
461 fpos = ntohl(hdr->data_start);
462 #ifdef CONFIG_BINFMT_ZFLAT
463 if (flags & FLAT_FLAG_GZDATA) {
464 result = decompress_exec(bprm, fpos, (char *) datapos,
465 data_len + (relocs * sizeof(unsigned long)), 0);
466 } else
467 #endif
469 result = bprm->file->f_op->read(bprm->file,
470 (char *) datapos, data_len + extra, &fpos);
472 if (result >= (unsigned long)-4096) {
473 printk("Unable to read data+bss, errno %d\n", (int)-result);
474 do_munmap(current->mm, textpos, text_len);
475 do_munmap(current->mm, datapos, data_len + extra);
476 return result;
479 reloc = (unsigned long *) (datapos+(ntohl(hdr->reloc_start)-text_len));
480 memp = datapos;
481 memkasked = data_len + extra;
483 } else {
486 * calculate the extra space we need to map in
489 extra = max(bss_len + stack_len, relocs * sizeof(unsigned long)),
491 down_write(&current->mm->mmap_sem);
492 textpos = do_mmap(0, 0, text_len + data_len + extra,
493 PROT_READ | PROT_EXEC | PROT_WRITE, 0, 0);
494 up_write(&current->mm->mmap_sem);
495 if (!textpos || textpos >= (unsigned long) -4096) {
496 if (!textpos)
497 textpos = (unsigned long) -ENOMEM;
498 printk("Unable to allocate RAM for process text/data, errno %d\n",
499 (int)-textpos);
502 datapos = textpos + ntohl (hdr->data_start);
503 reloc = (unsigned long *) (textpos + ntohl(hdr->reloc_start));
504 memp = textpos;
505 memkasked = text_len + data_len + extra;
507 #ifdef CONFIG_BINFMT_ZFLAT
509 * load it all in and treat it like a RAM load from now on
511 if (flags & FLAT_FLAG_GZIP) {
512 result = decompress_exec(bprm, sizeof (struct flat_hdr),
513 (((char *) textpos) + sizeof (struct flat_hdr)),
514 (text_len + data_len + (relocs * sizeof(unsigned long))
515 - sizeof (struct flat_hdr)),
517 } else if (flags & FLAT_FLAG_GZDATA) {
518 fpos = 0;
519 result = bprm->file->f_op->read(bprm->file,
520 (char *) textpos, text_len, &fpos);
521 if (result < (unsigned long) -4096)
522 result = decompress_exec(bprm, text_len, (char *) datapos,
523 data_len + (relocs * sizeof(unsigned long)), 0);
525 else
526 #endif
528 fpos = 0;
529 result = bprm->file->f_op->read(bprm->file,
530 (char *) textpos, text_len + data_len + extra, &fpos);
532 if (result >= (unsigned long)-4096) {
533 printk("Unable to read code+data+bss, errno %d\n",(int)-result);
534 do_munmap(current->mm, textpos, text_len + data_len + extra);
535 return result;
539 DBG_FLT("Mapping is %x, Entry point is %x, data_start is %x\n",
540 textpos, ntohl(hdr->entry), ntohl(hdr->data_start));
542 current->mm->start_code = textpos + sizeof (struct flat_hdr);
543 current->mm->end_code = textpos + text_len;
544 current->mm->start_data = datapos;
545 current->mm->end_data = datapos + data_len;
547 * set up the brk stuff (uses any slack left in data/bss/stack allocation
548 * We put the brk after the bss (between the bss and stack) like other
549 * platforms.
551 current->mm->start_brk = datapos + data_len + bss_len;
552 current->mm->brk = (current->mm->start_brk + 3) & ~3;
553 current->mm->context.end_brk = memp + ksize((void *) memp) - stack_len;
554 current->mm->rss = 0;
556 DBG_FLT("Load %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
557 bprm->filename,
558 (int) current->mm->start_code, (int) current->mm->end_code,
559 (int) current->mm->start_data, (int) current->mm->end_data,
560 (int) current->mm->end_data, (int) current->mm->brk);
562 text_len -= sizeof(struct flat_hdr); /* the real code len */
565 * We just load the allocations into some temporary memory to
566 * help simplify all this mumbo jumbo
568 * We've got two different sections of relocation entries.
569 * The first is the GOT which resides at the begining of the data segment
570 * and is terminated with a -1. This one can be relocated in place.
571 * The second is the extra relocation entries tacked after the image's
572 * data segment. These require a little more processing as the entry is
573 * really an offset into the image which contains an offset into the
574 * image.
577 if (flags & FLAT_FLAG_GOTPIC) {
578 for (rp = (unsigned long *)datapos; *rp != 0xffffffff; rp++)
579 *rp = calc_reloc(*rp, text_len);
583 * Now run through the relocation entries.
584 * We've got to be careful here as C++ produces relocatable zero
585 * entries in the constructor and destructor tables which are then
586 * tested for being not zero (which will always occur unless we're
587 * based from address zero). This causes an endless loop as __start
588 * is at zero. The solution used is to not relocate zero addresses.
589 * This has the negative side effect of not allowing a global data
590 * reference to be statically initialised to _stext (I've moved
591 * __start to address 4 so that is okay).
594 if (rev > OLD_FLAT_VERSION) {
595 for (i=0; i < relocs; i++) {
596 unsigned long addr;
598 /* Get the address of the pointer to be
599 relocated (of course, the address has to be
600 relocated first). */
601 rp = (unsigned long *) calc_reloc(ntohl(reloc[i]), text_len);
603 /* Get the pointer's value. */
604 addr = get_unaligned (rp);
606 if (addr != 0) {
608 * Do the relocation. PIC relocs in the data section are
609 * already in target order
611 addr = calc_reloc(
612 (flags & FLAT_FLAG_GOTPIC) ? addr : ntohl(addr),
613 text_len);
614 /* Write back the relocated pointer. */
615 put_unaligned (addr, rp);
618 } else {
619 for (i=0; i < relocs; i++)
620 old_reloc(ntohl(reloc[i]));
623 /* zero the BSS, BRK and stack areas */
624 memset((void*)(datapos + data_len), 0, bss_len +
625 (current->mm->context.end_brk - current->mm->start_brk) +
626 stack_len);
628 compute_creds(bprm);
629 current->flags &= ~PF_FORKNOEXEC;
631 flush_icache_range(current->mm->start_code, current->mm->end_code);
633 set_binfmt(&flat_format);
635 p = ((current->mm->context.end_brk + stack_len + 3) & ~3) - 4;
636 DBG_FLT("p=%x\n", p);
638 /* copy the arg pages onto the stack, this could be more efficient :-) */
639 for (i = TOP_OF_ARGS - 1; i >= bprm->p; i--)
640 * (char *) --p =
641 ((char *) page_address(bprm->page[i/PAGE_SIZE]))[i % PAGE_SIZE];
643 current->mm->start_stack = (unsigned long) create_flat_tables(p, bprm);
645 DBG_FLT("start_thread(regs=0x%x, entry=0x%x, start_stack=0x%x)\n",
646 regs, textpos + ntohl(hdr->entry), current->mm->start_stack);
647 start_thread(regs,
648 textpos + ntohl(hdr->entry),
649 current->mm->start_stack);
651 if (current->ptrace & PT_PTRACED)
652 send_sig(SIGTRAP, current, 0);
654 return 0;
657 static int load_flat_library(struct file *file)
659 return(-ENOEXEC);
662 static int __init init_flat_binfmt(void)
664 return register_binfmt(&flat_format);
667 static void __exit exit_flat_binfmt(void)
669 unregister_binfmt(&flat_format);
672 module_init(init_flat_binfmt);
673 module_exit(exit_flat_binfmt);