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