1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <errno.h>
39#include <sys/mman.h>
40#include <unistd.h>
41
42#include "qemu.h"
43#include "flat.h"
44
45
46
47#ifdef DEBUG
48#define DBG_FLT(...) printf(__VA_ARGS__)
49#else
50#define DBG_FLT(...)
51#endif
52
53#define flat_reloc_valid(reloc, size) ((reloc) <= (size))
54#define flat_old_ram_flag(flag) (flag)
55#ifdef TARGET_WORDS_BIGENDIAN
56#define flat_get_relocate_addr(relval) (relval)
57#else
58#define flat_get_relocate_addr(relval) bswap32(relval)
59#endif
60
61#define RELOC_FAILED 0xff00ff01
62#define UNLOADED_LIB 0x7ff000ff
63
64struct lib_info {
65 abi_ulong start_code;
66 abi_ulong start_data;
67 abi_ulong end_data;
68 abi_ulong start_brk;
69 abi_ulong text_len;
70 abi_ulong entry;
71 abi_ulong build_date;
72 short loaded;
73};
74
75#ifdef CONFIG_BINFMT_SHARED_FLAT
76static int load_flat_shared_library(int id, struct lib_info *p);
77#endif
78
79struct linux_binprm;
80
81#define ntohl(x) be32_to_cpu(x)
82
83
84
85
86
87
88
89
90
91static abi_ulong copy_strings(abi_ulong p, int n, char **s)
92{
93 int len;
94
95 while (n-- > 0) {
96 len = strlen(s[n]) + 1;
97 p -= len;
98 memcpy_to_target(p, s[n], len);
99 }
100
101 return p;
102}
103
104static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
105 abi_ulong offset)
106{
107 void *buf;
108 int ret;
109
110 buf = lock_user(VERIFY_WRITE, ptr, len, 0);
111 ret = pread(fd, buf, len, offset);
112 unlock_user(buf, ptr, len);
113 return ret;
114}
115
116
117#ifdef CONFIG_BINFMT_ZFLAT
118
119#include <linux/zlib.h>
120
121#define LBUFSIZE 4000
122
123
124#define ASCII_FLAG 0x01
125#define CONTINUATION 0x02
126#define EXTRA_FIELD 0x04
127#define ORIG_NAME 0x08
128#define COMMENT 0x10
129#define ENCRYPTED 0x20
130#define RESERVED 0xC0
131
132static int decompress_exec(
133 struct linux_binprm *bprm,
134 unsigned long offset,
135 char *dst,
136 long len,
137 int fd)
138{
139 unsigned char *buf;
140 z_stream strm;
141 loff_t fpos;
142 int ret, retval;
143
144 DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len);
145
146 memset(&strm, 0, sizeof(strm));
147 strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
148 if (strm.workspace == NULL) {
149 DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
150 return -ENOMEM;
151 }
152 buf = kmalloc(LBUFSIZE, GFP_KERNEL);
153 if (buf == NULL) {
154 DBG_FLT("binfmt_flat: no memory for read buffer\n");
155 retval = -ENOMEM;
156 goto out_free;
157 }
158
159
160 fpos = offset;
161 ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
162
163 strm.next_in = buf;
164 strm.avail_in = ret;
165 strm.total_in = 0;
166
167 retval = -ENOEXEC;
168
169
170 if (ret < 10) {
171 DBG_FLT("binfmt_flat: file too small?\n");
172 goto out_free_buf;
173 }
174
175
176 if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
177 DBG_FLT("binfmt_flat: unknown compression magic?\n");
178 goto out_free_buf;
179 }
180
181
182 if (buf[2] != 8) {
183 DBG_FLT("binfmt_flat: unknown compression method?\n");
184 goto out_free_buf;
185 }
186
187 if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
188 (buf[3] & RESERVED)) {
189 DBG_FLT("binfmt_flat: unknown flags?\n");
190 goto out_free_buf;
191 }
192
193 ret = 10;
194 if (buf[3] & EXTRA_FIELD) {
195 ret += 2 + buf[10] + (buf[11] << 8);
196 if (unlikely(LBUFSIZE == ret)) {
197 DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
198 goto out_free_buf;
199 }
200 }
201 if (buf[3] & ORIG_NAME) {
202 for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
203 ;
204 if (unlikely(LBUFSIZE == ret)) {
205 DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
206 goto out_free_buf;
207 }
208 }
209 if (buf[3] & COMMENT) {
210 for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
211 ;
212 if (unlikely(LBUFSIZE == ret)) {
213 DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");
214 goto out_free_buf;
215 }
216 }
217
218 strm.next_in += ret;
219 strm.avail_in -= ret;
220
221 strm.next_out = dst;
222 strm.avail_out = len;
223 strm.total_out = 0;
224
225 if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
226 DBG_FLT("binfmt_flat: zlib init failed?\n");
227 goto out_free_buf;
228 }
229
230 while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
231 ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
232 if (ret <= 0)
233 break;
234 if (ret >= (unsigned long) -4096)
235 break;
236 len -= ret;
237
238 strm.next_in = buf;
239 strm.avail_in = ret;
240 strm.total_in = 0;
241 }
242
243 if (ret < 0) {
244 DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
245 ret, strm.msg);
246 goto out_zlib;
247 }
248
249 retval = 0;
250out_zlib:
251 zlib_inflateEnd(&strm);
252out_free_buf:
253 kfree(buf);
254out_free:
255 kfree(strm.workspace);
256out:
257 return retval;
258}
259
260#endif
261
262
263
264static abi_ulong
265calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
266{
267 abi_ulong addr;
268 int id;
269 abi_ulong start_brk;
270 abi_ulong start_data;
271 abi_ulong text_len;
272 abi_ulong start_code;
273
274#ifdef CONFIG_BINFMT_SHARED_FLAT
275#error needs checking
276 if (r == 0)
277 id = curid;
278 else {
279 id = (r >> 24) & 0xff;
280 r &= 0x00ffffff;
281 }
282 if (id >= MAX_SHARED_LIBS) {
283 fprintf(stderr, "BINFMT_FLAT: reference 0x%x to shared library %d\n",
284 (unsigned) r, id);
285 goto failed;
286 }
287 if (curid != id) {
288 if (internalp) {
289 fprintf(stderr, "BINFMT_FLAT: reloc address 0x%x not "
290 "in same module (%d != %d)\n",
291 (unsigned) r, curid, id);
292 goto failed;
293 } else if ( ! p[id].loaded &&
294 load_flat_shared_library(id, p) > (unsigned long) -4096) {
295 fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
296 goto failed;
297 }
298
299 if (p[id].build_date && p[curid].build_date
300 && p[curid].build_date < p[id].build_date) {
301 fprintf(stderr, "BINFMT_FLAT: library %d is younger than %d\n",
302 id, curid);
303 goto failed;
304 }
305 }
306#else
307 id = 0;
308#endif
309
310 start_brk = p[id].start_brk;
311 start_data = p[id].start_data;
312 start_code = p[id].start_code;
313 text_len = p[id].text_len;
314
315 if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
316 fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x "
317 "(0 - 0x%x/0x%x)\n",
318 (int) r,(int)(start_brk-start_code),(int)text_len);
319 goto failed;
320 }
321
322 if (r < text_len)
323 addr = r + start_code;
324 else
325 addr = r - text_len + start_data;
326
327
328 return(addr);
329
330failed:
331 abort();
332 return RELOC_FAILED;
333}
334
335
336
337
338static void old_reloc(struct lib_info *libinfo, uint32_t rl)
339{
340#ifdef DEBUG
341 char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
342#endif
343 uint32_t *ptr;
344 uint32_t offset;
345 int reloc_type;
346
347 offset = rl & 0x3fffffff;
348 reloc_type = rl >> 30;
349
350#if defined(CONFIG_COLDFIRE)
351 ptr = (uint32_t *) ((unsigned long) libinfo->start_code + offset);
352#else
353 ptr = (uint32_t *) ((unsigned long) libinfo->start_data + offset);
354#endif
355
356#ifdef DEBUG
357 fprintf(stderr, "Relocation of variable at DATASEG+%x "
358 "(address %p, currently %x) into segment %s\n",
359 offset, ptr, (int)*ptr, segment[reloc_type]);
360#endif
361
362 switch (reloc_type) {
363 case OLD_FLAT_RELOC_TYPE_TEXT:
364 *ptr += libinfo->start_code;
365 break;
366 case OLD_FLAT_RELOC_TYPE_DATA:
367 *ptr += libinfo->start_data;
368 break;
369 case OLD_FLAT_RELOC_TYPE_BSS:
370 *ptr += libinfo->end_data;
371 break;
372 default:
373 fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n",
374 reloc_type);
375 break;
376 }
377 DBG_FLT("Relocation became %x\n", (int)*ptr);
378}
379
380
381
382static int load_flat_file(struct linux_binprm * bprm,
383 struct lib_info *libinfo, int id, abi_ulong *extra_stack)
384{
385 struct flat_hdr * hdr;
386 abi_ulong textpos = 0, datapos = 0, result;
387 abi_ulong realdatastart = 0;
388 abi_ulong text_len, data_len, bss_len, stack_len, flags;
389 abi_ulong memp = 0;
390 abi_ulong extra;
391 abi_ulong reloc = 0, rp;
392 int i, rev, relocs = 0;
393 abi_ulong fpos;
394 abi_ulong start_code, end_code;
395 abi_ulong indx_len;
396
397 hdr = ((struct flat_hdr *) bprm->buf);
398
399 text_len = ntohl(hdr->data_start);
400 data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
401 bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
402 stack_len = ntohl(hdr->stack_size);
403 if (extra_stack) {
404 stack_len += *extra_stack;
405 *extra_stack = stack_len;
406 }
407 relocs = ntohl(hdr->reloc_count);
408 flags = ntohl(hdr->flags);
409 rev = ntohl(hdr->rev);
410
411 DBG_FLT("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
412
413 if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
414 fprintf(stderr, "BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
415 rev, (int) FLAT_VERSION);
416 return -ENOEXEC;
417 }
418
419
420 if (rev == OLD_FLAT_VERSION && id != 0) {
421 fprintf(stderr, "BINFMT_FLAT: shared libraries are not available\n");
422 return -ENOEXEC;
423 }
424
425
426
427
428
429 if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
430 flags = FLAT_FLAG_RAM;
431
432#ifndef CONFIG_BINFMT_ZFLAT
433 if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
434 fprintf(stderr, "Support for ZFLAT executables is not enabled\n");
435 return -ENOEXEC;
436 }
437#endif
438
439
440
441
442 extra = relocs * sizeof(abi_ulong);
443 if (extra < bss_len + stack_len)
444 extra = bss_len + stack_len;
445
446
447
448 indx_len = MAX_SHARED_LIBS * sizeof(abi_ulong);
449 indx_len = (indx_len + 15) & ~(abi_ulong)15;
450
451
452
453
454
455
456 if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
457
458
459
460
461 DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
462
463 textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC,
464 MAP_PRIVATE, bprm->fd, 0);
465 if (textpos == -1) {
466 fprintf(stderr, "Unable to mmap process text\n");
467 return -1;
468 }
469
470 realdatastart = target_mmap(0, data_len + extra + indx_len,
471 PROT_READ|PROT_WRITE|PROT_EXEC,
472 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
473
474 if (realdatastart == -1) {
475 fprintf(stderr, "Unable to allocate RAM for process data\n");
476 return realdatastart;
477 }
478 datapos = realdatastart + indx_len;
479
480 DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
481 (int)(data_len + bss_len + stack_len), (int)datapos);
482
483 fpos = ntohl(hdr->data_start);
484#ifdef CONFIG_BINFMT_ZFLAT
485 if (flags & FLAT_FLAG_GZDATA) {
486 result = decompress_exec(bprm, fpos, (char *) datapos,
487 data_len + (relocs * sizeof(abi_ulong)))
488 } else
489#endif
490 {
491 result = target_pread(bprm->fd, datapos,
492 data_len + (relocs * sizeof(abi_ulong)),
493 fpos);
494 }
495 if (result < 0) {
496 fprintf(stderr, "Unable to read data+bss\n");
497 return result;
498 }
499
500 reloc = datapos + (ntohl(hdr->reloc_start) - text_len);
501 memp = realdatastart;
502
503 } else {
504
505 textpos = target_mmap(0, text_len + data_len + extra + indx_len,
506 PROT_READ | PROT_EXEC | PROT_WRITE,
507 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
508 if (textpos == -1 ) {
509 fprintf(stderr, "Unable to allocate RAM for process text/data\n");
510 return -1;
511 }
512
513 realdatastart = textpos + ntohl(hdr->data_start);
514 datapos = realdatastart + indx_len;
515 reloc = (textpos + ntohl(hdr->reloc_start) + indx_len);
516 memp = textpos;
517
518#ifdef CONFIG_BINFMT_ZFLAT
519#error code needs checking
520
521
522
523 if (flags & FLAT_FLAG_GZIP) {
524 result = decompress_exec(bprm, sizeof (struct flat_hdr),
525 (((char *) textpos) + sizeof (struct flat_hdr)),
526 (text_len + data_len + (relocs * sizeof(unsigned long))
527 - sizeof (struct flat_hdr)),
528 0);
529 memmove((void *) datapos, (void *) realdatastart,
530 data_len + (relocs * sizeof(unsigned long)));
531 } else if (flags & FLAT_FLAG_GZDATA) {
532 fpos = 0;
533 result = bprm->file->f_op->read(bprm->file,
534 (char *) textpos, text_len, &fpos);
535 if (result < (unsigned long) -4096)
536 result = decompress_exec(bprm, text_len, (char *) datapos,
537 data_len + (relocs * sizeof(unsigned long)), 0);
538 }
539 else
540#endif
541 {
542 result = target_pread(bprm->fd, textpos,
543 text_len, 0);
544 if (result >= 0) {
545 result = target_pread(bprm->fd, datapos,
546 data_len + (relocs * sizeof(abi_ulong)),
547 ntohl(hdr->data_start));
548 }
549 }
550 if (result < 0) {
551 fprintf(stderr, "Unable to read code+data+bss\n");
552 return result;
553 }
554 }
555
556 DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n",
557 (int)textpos, 0x00ffffff&ntohl(hdr->entry),
558 ntohl(hdr->data_start));
559
560
561 start_code = textpos + sizeof (struct flat_hdr);
562 end_code = textpos + text_len;
563
564 DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
565 id ? "Lib" : "Load", bprm->filename,
566 (int) start_code, (int) end_code,
567 (int) datapos,
568 (int) (datapos + data_len),
569 (int) (datapos + data_len),
570 (int) (((datapos + data_len + bss_len) + 3) & ~3));
571
572 text_len -= sizeof(struct flat_hdr);
573
574
575 libinfo[id].start_code = start_code;
576 libinfo[id].start_data = datapos;
577 libinfo[id].end_data = datapos + data_len;
578 libinfo[id].start_brk = datapos + data_len + bss_len;
579 libinfo[id].text_len = text_len;
580 libinfo[id].loaded = 1;
581 libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
582 libinfo[id].build_date = ntohl(hdr->build_date);
583
584
585
586
587
588
589
590
591
592
593
594
595
596 if (flags & FLAT_FLAG_GOTPIC) {
597 rp = datapos;
598 while (1) {
599 abi_ulong addr;
600 if (get_user_ual(addr, rp))
601 return -EFAULT;
602 if (addr == -1)
603 break;
604 if (addr) {
605 addr = calc_reloc(addr, libinfo, id, 0);
606 if (addr == RELOC_FAILED)
607 return -ENOEXEC;
608 if (put_user_ual(addr, rp))
609 return -EFAULT;
610 }
611 rp += sizeof(abi_ulong);
612 }
613 }
614
615
616
617
618
619
620
621
622
623
624
625
626 if (rev > OLD_FLAT_VERSION) {
627 for (i = 0; i < relocs; i++) {
628 abi_ulong addr, relval;
629
630
631
632
633 if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
634 return -EFAULT;
635 addr = flat_get_relocate_addr(relval);
636 rp = calc_reloc(addr, libinfo, id, 1);
637 if (rp == RELOC_FAILED)
638 return -ENOEXEC;
639
640
641 if (get_user_ual(addr, rp))
642 return -EFAULT;
643 if (addr != 0) {
644
645
646
647
648
649#ifndef TARGET_WORDS_BIGENDIAN
650 if ((flags & FLAT_FLAG_GOTPIC) == 0)
651 addr = bswap32(addr);
652#endif
653 addr = calc_reloc(addr, libinfo, id, 0);
654 if (addr == RELOC_FAILED)
655 return -ENOEXEC;
656
657
658 if (put_user_ual(addr, rp))
659 return -EFAULT;
660 }
661 }
662 } else {
663 for (i = 0; i < relocs; i++) {
664 abi_ulong relval;
665 if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
666 return -EFAULT;
667 old_reloc(&libinfo[0], relval);
668 }
669 }
670
671
672 memset((void *)((unsigned long)datapos + data_len), 0, bss_len);
673
674 return 0;
675}
676
677
678
679#ifdef CONFIG_BINFMT_SHARED_FLAT
680
681
682
683
684
685
686static int load_flat_shared_library(int id, struct lib_info *libs)
687{
688 struct linux_binprm bprm;
689 int res;
690 char buf[16];
691
692
693 sprintf(buf, "/lib/lib%d.so", id);
694
695
696 bprm.filename = buf;
697 bprm.file = open_exec(bprm.filename);
698 res = PTR_ERR(bprm.file);
699 if (IS_ERR(bprm.file))
700 return res;
701
702 res = prepare_binprm(&bprm);
703
704 if (res <= (unsigned long)-4096)
705 res = load_flat_file(&bprm, libs, id, NULL);
706 if (bprm.file) {
707 allow_write_access(bprm.file);
708 fput(bprm.file);
709 bprm.file = NULL;
710 }
711 return(res);
712}
713
714#endif
715
716int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
717 struct image_info * info)
718{
719 struct lib_info libinfo[MAX_SHARED_LIBS];
720 abi_ulong p = bprm->p;
721 abi_ulong stack_len;
722 abi_ulong start_addr;
723 abi_ulong sp;
724 int res;
725 int i, j;
726
727 memset(libinfo, 0, sizeof(libinfo));
728
729
730
731
732
733
734
735#define TOP_OF_ARGS (TARGET_PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *))
736 stack_len = TOP_OF_ARGS - bprm->p;
737 stack_len += (bprm->argc + 1) * 4;
738 stack_len += (bprm->envc + 1) * 4;
739
740
741 res = load_flat_file(bprm, libinfo, 0, &stack_len);
742 if (res > (unsigned long)-4096)
743 return res;
744
745
746 for (i=0; i<MAX_SHARED_LIBS; i++) {
747 if (libinfo[i].loaded) {
748 abi_ulong p;
749 p = libinfo[i].start_data;
750 for (j=0; j<MAX_SHARED_LIBS; j++) {
751 p -= 4;
752
753 if (put_user_ual(libinfo[j].loaded
754 ? libinfo[j].start_data
755 : UNLOADED_LIB,
756 p))
757 return -EFAULT;
758 }
759 }
760 }
761
762 p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4;
763 DBG_FLT("p=%x\n", (int)p);
764
765
766 p = copy_strings(p, bprm->envc, bprm->envp);
767 p = copy_strings(p, bprm->argc, bprm->argv);
768
769 sp = p & ~(abi_ulong)(sizeof(abi_ulong) - 1);
770
771
772 stack_len = bprm->envc + bprm->argc + 2;
773 stack_len += 3;
774 stack_len *= sizeof(abi_ulong);
775 if ((sp + stack_len) & 15)
776 sp -= 16 - ((sp + stack_len) & 15);
777 sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p, 1);
778
779
780
781
782
783 start_addr = libinfo[0].entry;
784
785#ifdef CONFIG_BINFMT_SHARED_FLAT
786#error here
787 for (i = MAX_SHARED_LIBS-1; i>0; i--) {
788 if (libinfo[i].loaded) {
789
790 --sp;
791 if (put_user_ual(start_addr, sp))
792 return -EFAULT;
793 start_addr = libinfo[i].entry;
794 }
795 }
796#endif
797
798
799 info->start_code = libinfo[0].start_code;
800 info->end_code = libinfo[0].start_code = libinfo[0].text_len;
801 info->start_data = libinfo[0].start_data;
802 info->end_data = libinfo[0].end_data;
803 info->start_brk = libinfo[0].start_brk;
804 info->start_stack = sp;
805 info->entry = start_addr;
806 info->code_offset = info->start_code;
807 info->data_offset = info->start_data - libinfo[0].text_len;
808
809 DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n",
810 (int)info->entry, (int)info->start_stack);
811
812 return 0;
813}
814