Commit e5fe0c5230cc7780de852cefb5df57e05e2c613e
1 parent
ac62f715
bFLT loader (for uClinux binaries).
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1951 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
8 changed files
with
1147 additions
and
272 deletions
Makefile.target
| ... | ... | @@ -171,7 +171,12 @@ LDFLAGS+=-p |
| 171 | 171 | main.o: CFLAGS+=-p |
| 172 | 172 | endif |
| 173 | 173 | |
| 174 | -OBJS= elfload.o main.o syscall.o mmap.o signal.o path.o osdep.o thunk.o | |
| 174 | +OBJS= main.o syscall.o mmap.o signal.o path.o osdep.o thunk.o \ | |
| 175 | + elfload.o linuxload.o | |
| 176 | +ifdef TARGET_HAS_BFLT | |
| 177 | +OBJS+= flatload.o | |
| 178 | +endif | |
| 179 | + | |
| 175 | 180 | ifeq ($(TARGET_ARCH), i386) |
| 176 | 181 | OBJS+= vm86.o |
| 177 | 182 | endif | ... | ... |
configure
| ... | ... | @@ -773,6 +773,7 @@ echo "/* Automatically generated by configure - do not modify */" > $config_h |
| 773 | 773 | echo "include ../config-host.mak" >> $config_mak |
| 774 | 774 | echo "#include \"../config-host.h\"" >> $config_h |
| 775 | 775 | |
| 776 | +bflt="no" | |
| 776 | 777 | interp_prefix1=`echo "$interp_prefix" | sed "s/%M/$target_cpu/g"` |
| 777 | 778 | echo "#define CONFIG_QEMU_PREFIX \"$interp_prefix1\"" >> $config_h |
| 778 | 779 | |
| ... | ... | @@ -787,6 +788,7 @@ elif test "$target_cpu" = "arm" -o "$target_cpu" = "armeb" ; then |
| 787 | 788 | echo "TARGET_ARCH=arm" >> $config_mak |
| 788 | 789 | echo "#define TARGET_ARCH \"arm\"" >> $config_h |
| 789 | 790 | echo "#define TARGET_ARM 1" >> $config_h |
| 791 | + bflt="yes" | |
| 790 | 792 | elif test "$target_cpu" = "sparc" ; then |
| 791 | 793 | echo "TARGET_ARCH=sparc" >> $config_mak |
| 792 | 794 | echo "#define TARGET_ARCH \"sparc\"" >> $config_h |
| ... | ... | @@ -842,6 +844,10 @@ if test "$target_cpu" = "arm" -o "$target_cpu" = "armeb" ; then |
| 842 | 844 | echo "CONFIG_SOFTFLOAT=yes" >> $config_mak |
| 843 | 845 | echo "#define CONFIG_SOFTFLOAT 1" >> $config_h |
| 844 | 846 | fi |
| 847 | +if test "$target_user_only" = "yes" -a "$bflt" = "yes"; then | |
| 848 | + echo "TARGET_HAS_BFLT=yes" >> $config_mak | |
| 849 | + echo "#define TARGET_HAS_BFLT 1" >> $config_h | |
| 850 | +fi | |
| 845 | 851 | # sdl defines |
| 846 | 852 | |
| 847 | 853 | if test "$target_user_only" = "no"; then | ... | ... |
linux-user/elfload.c
| ... | ... | @@ -3,7 +3,6 @@ |
| 3 | 3 | #include <stdio.h> |
| 4 | 4 | #include <sys/types.h> |
| 5 | 5 | #include <fcntl.h> |
| 6 | -#include <sys/stat.h> | |
| 7 | 6 | #include <errno.h> |
| 8 | 7 | #include <unistd.h> |
| 9 | 8 | #include <sys/mman.h> |
| ... | ... | @@ -59,19 +58,19 @@ static uint32_t get_elf_hwcap(void) |
| 59 | 58 | #define ELF_DATA ELFDATA2LSB |
| 60 | 59 | #define ELF_ARCH EM_386 |
| 61 | 60 | |
| 62 | - /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program | |
| 63 | - starts %edx contains a pointer to a function which might be | |
| 64 | - registered using `atexit'. This provides a mean for the | |
| 65 | - dynamic linker to call DT_FINI functions for shared libraries | |
| 66 | - that have been loaded before the code runs. | |
| 67 | - | |
| 68 | - A value of 0 tells we have no such handler. */ | |
| 69 | -#define ELF_PLAT_INIT(_r) _r->edx = 0 | |
| 70 | - | |
| 71 | 61 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 72 | 62 | { |
| 73 | 63 | regs->esp = infop->start_stack; |
| 74 | 64 | regs->eip = infop->entry; |
| 65 | + | |
| 66 | + /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program | |
| 67 | + starts %edx contains a pointer to a function which might be | |
| 68 | + registered using `atexit'. This provides a mean for the | |
| 69 | + dynamic linker to call DT_FINI functions for shared libraries | |
| 70 | + that have been loaded before the code runs. | |
| 71 | + | |
| 72 | + A value of 0 tells we have no such handler. */ | |
| 73 | + regs->edx = 0; | |
| 75 | 74 | } |
| 76 | 75 | |
| 77 | 76 | #define USE_ELF_CORE_DUMP |
| ... | ... | @@ -93,8 +92,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i |
| 93 | 92 | #endif |
| 94 | 93 | #define ELF_ARCH EM_ARM |
| 95 | 94 | |
| 96 | -#define ELF_PLAT_INIT(_r) _r->ARM_r0 = 0 | |
| 97 | - | |
| 98 | 95 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 99 | 96 | { |
| 100 | 97 | target_long stack = infop->start_stack; |
| ... | ... | @@ -107,7 +104,9 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i |
| 107 | 104 | regs->ARM_r2 = tgetl(stack + 8); /* envp */ |
| 108 | 105 | regs->ARM_r1 = tgetl(stack + 4); /* envp */ |
| 109 | 106 | /* XXX: it seems that r0 is zeroed after ! */ |
| 110 | - // regs->ARM_r0 = tgetl(stack); /* argc */ | |
| 107 | + regs->ARM_r0 = 0; | |
| 108 | + /* For uClinux PIC binaries. */ | |
| 109 | + regs->ARM_r10 = infop->start_data; | |
| 111 | 110 | } |
| 112 | 111 | |
| 113 | 112 | #define USE_ELF_CORE_DUMP |
| ... | ... | @@ -142,9 +141,6 @@ enum |
| 142 | 141 | #define ELF_DATA ELFDATA2MSB |
| 143 | 142 | #define ELF_ARCH EM_SPARC |
| 144 | 143 | |
| 145 | -/*XXX*/ | |
| 146 | -#define ELF_PLAT_INIT(_r) | |
| 147 | - | |
| 148 | 144 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 149 | 145 | { |
| 150 | 146 | regs->tstate = 0; |
| ... | ... | @@ -163,9 +159,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i |
| 163 | 159 | #define ELF_DATA ELFDATA2MSB |
| 164 | 160 | #define ELF_ARCH EM_SPARC |
| 165 | 161 | |
| 166 | -/*XXX*/ | |
| 167 | -#define ELF_PLAT_INIT(_r) | |
| 168 | - | |
| 169 | 162 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 170 | 163 | { |
| 171 | 164 | regs->psr = 0; |
| ... | ... | @@ -192,20 +185,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i |
| 192 | 185 | #endif |
| 193 | 186 | #define ELF_ARCH EM_PPC |
| 194 | 187 | |
| 195 | -/* Note that isn't exactly what regular kernel does | |
| 196 | - * but this is what the ABI wants and is needed to allow | |
| 197 | - * execution of PPC BSD programs. | |
| 198 | - */ | |
| 199 | -#define ELF_PLAT_INIT(_r) \ | |
| 200 | -do { \ | |
| 201 | - target_ulong *pos = (target_ulong *)bprm->p, tmp = 1; \ | |
| 202 | - _r->gpr[3] = bprm->argc; \ | |
| 203 | - _r->gpr[4] = (unsigned long)++pos; \ | |
| 204 | - for (; tmp != 0; pos++) \ | |
| 205 | - tmp = ldl(pos); \ | |
| 206 | - _r->gpr[5] = (unsigned long)pos; \ | |
| 207 | -} while (0) | |
| 208 | - | |
| 209 | 188 | /* |
| 210 | 189 | * We need to put in some extra aux table entries to tell glibc what |
| 211 | 190 | * the cache block size is, so it can use the dcbz instruction safely. |
| ... | ... | @@ -239,9 +218,22 @@ do { \ |
| 239 | 218 | |
| 240 | 219 | static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop) |
| 241 | 220 | { |
| 221 | + target_ulong pos = infop->start_stack; | |
| 222 | + target_ulong tmp; | |
| 223 | + | |
| 242 | 224 | _regs->msr = 1 << MSR_PR; /* Set user mode */ |
| 243 | 225 | _regs->gpr[1] = infop->start_stack; |
| 244 | 226 | _regs->nip = infop->entry; |
| 227 | + /* Note that isn't exactly what regular kernel does | |
| 228 | + * but this is what the ABI wants and is needed to allow | |
| 229 | + * execution of PPC BSD programs. | |
| 230 | + */ | |
| 231 | + _regs->gpr[3] = tgetl(pos); | |
| 232 | + pos += sizeof(target_ulong); | |
| 233 | + _regs->gpr[4] = pos; | |
| 234 | + for (tmp = 1; tmp != 0; pos += sizeof(target_ulong)) | |
| 235 | + tmp = ldl(pos); | |
| 236 | + _regs->gpr[5] = pos; | |
| 245 | 237 | } |
| 246 | 238 | |
| 247 | 239 | #define USE_ELF_CORE_DUMP |
| ... | ... | @@ -263,8 +255,6 @@ static inline void init_thread(struct target_pt_regs *_regs, struct image_info * |
| 263 | 255 | #endif |
| 264 | 256 | #define ELF_ARCH EM_MIPS |
| 265 | 257 | |
| 266 | -#define ELF_PLAT_INIT(_r) | |
| 267 | - | |
| 268 | 258 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 269 | 259 | { |
| 270 | 260 | regs->cp0_status = CP0St_UM; |
| ... | ... | @@ -284,8 +274,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i |
| 284 | 274 | #define ELF_DATA ELFDATA2LSB |
| 285 | 275 | #define ELF_ARCH EM_SH |
| 286 | 276 | |
| 287 | -#define ELF_PLAT_INIT(_r) /* XXXXX */ | |
| 288 | - | |
| 289 | 277 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) |
| 290 | 278 | { |
| 291 | 279 | /* Check other registers XXXXX */ |
| ... | ... | @@ -308,30 +296,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i |
| 308 | 296 | |
| 309 | 297 | #include "elf.h" |
| 310 | 298 | |
| 311 | -/* | |
| 312 | - * MAX_ARG_PAGES defines the number of pages allocated for arguments | |
| 313 | - * and envelope for the new program. 32 should suffice, this gives | |
| 314 | - * a maximum env+arg of 128kB w/4KB pages! | |
| 315 | - */ | |
| 316 | -#define MAX_ARG_PAGES 32 | |
| 317 | - | |
| 318 | -/* | |
| 319 | - * This structure is used to hold the arguments that are | |
| 320 | - * used when loading binaries. | |
| 321 | - */ | |
| 322 | -struct linux_binprm { | |
| 323 | - char buf[128]; | |
| 324 | - void *page[MAX_ARG_PAGES]; | |
| 325 | - unsigned long p; | |
| 326 | - int sh_bang; | |
| 327 | - int fd; | |
| 328 | - int e_uid, e_gid; | |
| 329 | - int argc, envc; | |
| 330 | - char * filename; /* Name of binary */ | |
| 331 | - unsigned long loader, exec; | |
| 332 | - int dont_iput; /* binfmt handler has put inode */ | |
| 333 | -}; | |
| 334 | - | |
| 335 | 299 | struct exec |
| 336 | 300 | { |
| 337 | 301 | unsigned int a_info; /* Use macros N_MAGIC, etc for access */ |
| ... | ... | @@ -377,8 +341,6 @@ struct exec |
| 377 | 341 | #define PER_XENIX (0x0007 | STICKY_TIMEOUTS) |
| 378 | 342 | |
| 379 | 343 | /* Necessary parameters */ |
| 380 | -#define NGROUPS 32 | |
| 381 | - | |
| 382 | 344 | #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE |
| 383 | 345 | #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1)) |
| 384 | 346 | #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1)) |
| ... | ... | @@ -452,13 +414,13 @@ static void bswap_sym(Elf32_Sym *sym) |
| 452 | 414 | #endif |
| 453 | 415 | |
| 454 | 416 | /* |
| 455 | - * 'copy_string()' copies argument/envelope strings from user | |
| 417 | + * 'copy_elf_strings()' copies argument/envelope strings from user | |
| 456 | 418 | * memory to free pages in kernel mem. These are in a format ready |
| 457 | 419 | * to be put directly into the top of new user memory. |
| 458 | 420 | * |
| 459 | 421 | */ |
| 460 | -static unsigned long copy_strings(int argc,char ** argv, void **page, | |
| 461 | - unsigned long p) | |
| 422 | +static unsigned long copy_elf_strings(int argc,char ** argv, void **page, | |
| 423 | + unsigned long p) | |
| 462 | 424 | { |
| 463 | 425 | char *tmp, *tmp1, *pag = NULL; |
| 464 | 426 | int len, offset = 0; |
| ... | ... | @@ -506,101 +468,6 @@ static unsigned long copy_strings(int argc,char ** argv, void **page, |
| 506 | 468 | return p; |
| 507 | 469 | } |
| 508 | 470 | |
| 509 | -static int in_group_p(gid_t g) | |
| 510 | -{ | |
| 511 | - /* return TRUE if we're in the specified group, FALSE otherwise */ | |
| 512 | - int ngroup; | |
| 513 | - int i; | |
| 514 | - gid_t grouplist[NGROUPS]; | |
| 515 | - | |
| 516 | - ngroup = getgroups(NGROUPS, grouplist); | |
| 517 | - for(i = 0; i < ngroup; i++) { | |
| 518 | - if(grouplist[i] == g) { | |
| 519 | - return 1; | |
| 520 | - } | |
| 521 | - } | |
| 522 | - return 0; | |
| 523 | -} | |
| 524 | - | |
| 525 | -static int count(char ** vec) | |
| 526 | -{ | |
| 527 | - int i; | |
| 528 | - | |
| 529 | - for(i = 0; *vec; i++) { | |
| 530 | - vec++; | |
| 531 | - } | |
| 532 | - | |
| 533 | - return(i); | |
| 534 | -} | |
| 535 | - | |
| 536 | -static int prepare_binprm(struct linux_binprm *bprm) | |
| 537 | -{ | |
| 538 | - struct stat st; | |
| 539 | - int mode; | |
| 540 | - int retval, id_change; | |
| 541 | - | |
| 542 | - if(fstat(bprm->fd, &st) < 0) { | |
| 543 | - return(-errno); | |
| 544 | - } | |
| 545 | - | |
| 546 | - mode = st.st_mode; | |
| 547 | - if(!S_ISREG(mode)) { /* Must be regular file */ | |
| 548 | - return(-EACCES); | |
| 549 | - } | |
| 550 | - if(!(mode & 0111)) { /* Must have at least one execute bit set */ | |
| 551 | - return(-EACCES); | |
| 552 | - } | |
| 553 | - | |
| 554 | - bprm->e_uid = geteuid(); | |
| 555 | - bprm->e_gid = getegid(); | |
| 556 | - id_change = 0; | |
| 557 | - | |
| 558 | - /* Set-uid? */ | |
| 559 | - if(mode & S_ISUID) { | |
| 560 | - bprm->e_uid = st.st_uid; | |
| 561 | - if(bprm->e_uid != geteuid()) { | |
| 562 | - id_change = 1; | |
| 563 | - } | |
| 564 | - } | |
| 565 | - | |
| 566 | - /* Set-gid? */ | |
| 567 | - /* | |
| 568 | - * If setgid is set but no group execute bit then this | |
| 569 | - * is a candidate for mandatory locking, not a setgid | |
| 570 | - * executable. | |
| 571 | - */ | |
| 572 | - if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { | |
| 573 | - bprm->e_gid = st.st_gid; | |
| 574 | - if (!in_group_p(bprm->e_gid)) { | |
| 575 | - id_change = 1; | |
| 576 | - } | |
| 577 | - } | |
| 578 | - | |
| 579 | - memset(bprm->buf, 0, sizeof(bprm->buf)); | |
| 580 | - retval = lseek(bprm->fd, 0L, SEEK_SET); | |
| 581 | - if(retval >= 0) { | |
| 582 | - retval = read(bprm->fd, bprm->buf, 128); | |
| 583 | - } | |
| 584 | - if(retval < 0) { | |
| 585 | - perror("prepare_binprm"); | |
| 586 | - exit(-1); | |
| 587 | - /* return(-errno); */ | |
| 588 | - } | |
| 589 | - else { | |
| 590 | - return(retval); | |
| 591 | - } | |
| 592 | -} | |
| 593 | - | |
| 594 | -static inline void memcpy_to_target(target_ulong dest, const void *src, | |
| 595 | - unsigned long len) | |
| 596 | -{ | |
| 597 | - void *host_ptr; | |
| 598 | - | |
| 599 | - host_ptr = lock_user(dest, len, 0); | |
| 600 | - memcpy(host_ptr, src, len); | |
| 601 | - unlock_user(host_ptr, dest, 1); | |
| 602 | -} | |
| 603 | - | |
| 604 | 471 | unsigned long setup_arg_pages(target_ulong p, struct linux_binprm * bprm, |
| 605 | 472 | struct image_info * info) |
| 606 | 473 | { |
| ... | ... | @@ -628,11 +495,6 @@ unsigned long setup_arg_pages(target_ulong p, struct linux_binprm * bprm, |
| 628 | 495 | stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE; |
| 629 | 496 | p += stack_base; |
| 630 | 497 | |
| 631 | - if (bprm->loader) { | |
| 632 | - bprm->loader += stack_base; | |
| 633 | - } | |
| 634 | - bprm->exec += stack_base; | |
| 635 | - | |
| 636 | 498 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
| 637 | 499 | if (bprm->page[i]) { |
| 638 | 500 | info->rss++; |
| ... | ... | @@ -703,7 +565,6 @@ static unsigned long create_elf_tables(target_ulong p, int argc, int envc, |
| 703 | 565 | unsigned long interp_load_addr, int ibcs, |
| 704 | 566 | struct image_info *info) |
| 705 | 567 | { |
| 706 | - target_ulong argv, envp; | |
| 707 | 568 | target_ulong sp; |
| 708 | 569 | int size; |
| 709 | 570 | target_ulong u_platform; |
| ... | ... | @@ -765,28 +626,7 @@ static unsigned long create_elf_tables(target_ulong p, int argc, int envc, |
| 765 | 626 | #endif |
| 766 | 627 | #undef NEW_AUX_ENT |
| 767 | 628 | |
| 768 | - sp -= (envc + 1) * n; | |
| 769 | - envp = sp; | |
| 770 | - sp -= (argc + 1) * n; | |
| 771 | - argv = sp; | |
| 772 | - if (!ibcs) { | |
| 773 | - sp -= n; tputl(sp, envp); | |
| 774 | - sp -= n; tputl(sp, argv); | |
| 775 | - } | |
| 776 | - sp -= n; tputl(sp, argc); | |
| 777 | - info->arg_start = p; | |
| 778 | - while (argc-->0) { | |
| 779 | - tputl(argv, p); argv += n; | |
| 780 | - p += target_strlen(p) + 1; | |
| 781 | - } | |
| 782 | - tputl(argv, 0); | |
| 783 | - info->arg_end = info->env_start = p; | |
| 784 | - while (envc-->0) { | |
| 785 | - tputl(envp, p); envp += n; | |
| 786 | - p += target_strlen(p) + 1; | |
| 787 | - } | |
| 788 | - tputl(envp, 0); | |
| 789 | - info->env_end = p; | |
| 629 | + sp = loader_build_argptr(envc, argc, sp, p, !ibcs); | |
| 790 | 630 | return sp; |
| 791 | 631 | } |
| 792 | 632 | |
| ... | ... | @@ -1001,8 +841,8 @@ static void load_symbols(struct elfhdr *hdr, int fd) |
| 1001 | 841 | syminfos = s; |
| 1002 | 842 | } |
| 1003 | 843 | |
| 1004 | -static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, | |
| 1005 | - struct image_info * info) | |
| 844 | +int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, | |
| 845 | + struct image_info * info) | |
| 1006 | 846 | { |
| 1007 | 847 | struct elfhdr elf_ex; |
| 1008 | 848 | struct elfhdr interp_elf_ex; |
| ... | ... | @@ -1034,17 +874,19 @@ static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * r |
| 1034 | 874 | bswap_ehdr(&elf_ex); |
| 1035 | 875 | #endif |
| 1036 | 876 | |
| 1037 | - if (elf_ex.e_ident[0] != 0x7f || | |
| 1038 | - strncmp(&elf_ex.e_ident[1], "ELF",3) != 0) { | |
| 1039 | - return -ENOEXEC; | |
| 1040 | - } | |
| 1041 | - | |
| 1042 | 877 | /* First of all, some simple consistency checks */ |
| 1043 | 878 | if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) || |
| 1044 | 879 | (! elf_check_arch(elf_ex.e_machine))) { |
| 1045 | 880 | return -ENOEXEC; |
| 1046 | 881 | } |
| 1047 | 882 | |
| 883 | + bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p); | |
| 884 | + bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p); | |
| 885 | + bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p); | |
| 886 | + if (!bprm->p) { | |
| 887 | + retval = -E2BIG; | |
| 888 | + } | |
| 889 | + | |
| 1048 | 890 | /* Now read in all of the header information */ |
| 1049 | 891 | elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum); |
| 1050 | 892 | if (elf_phdata == NULL) { |
| ... | ... | @@ -1188,7 +1030,7 @@ static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * r |
| 1188 | 1030 | /* OK, we are done with that, now set up the arg stuff, |
| 1189 | 1031 | and then start this sucker up */ |
| 1190 | 1032 | |
| 1191 | - if (!bprm->sh_bang) { | |
| 1033 | + { | |
| 1192 | 1034 | char * passed_p; |
| 1193 | 1035 | |
| 1194 | 1036 | if (interpreter_type == INTERPRETER_AOUT) { |
| ... | ... | @@ -1196,7 +1038,7 @@ static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * r |
| 1196 | 1038 | passed_p = passed_fileno; |
| 1197 | 1039 | |
| 1198 | 1040 | if (elf_interpreter) { |
| 1199 | - bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p); | |
| 1041 | + bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p); | |
| 1200 | 1042 | bprm->argc++; |
| 1201 | 1043 | } |
| 1202 | 1044 | } |
| ... | ... | @@ -1347,11 +1189,10 @@ static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * r |
| 1347 | 1189 | interp_load_addr, |
| 1348 | 1190 | (interpreter_type == INTERPRETER_AOUT ? 0 : 1), |
| 1349 | 1191 | info); |
| 1350 | - if (interpreter_type == INTERPRETER_AOUT) | |
| 1351 | - info->arg_start += strlen(passed_fileno) + 1; | |
| 1352 | 1192 | info->start_brk = info->brk = elf_brk; |
| 1353 | 1193 | info->end_code = end_code; |
| 1354 | 1194 | info->start_code = start_code; |
| 1195 | + info->start_data = end_code; | |
| 1355 | 1196 | info->end_data = end_data; |
| 1356 | 1197 | info->start_stack = bprm->p; |
| 1357 | 1198 | |
| ... | ... | @@ -1380,79 +1221,18 @@ static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * r |
| 1380 | 1221 | MAP_FIXED | MAP_PRIVATE, -1, 0); |
| 1381 | 1222 | } |
| 1382 | 1223 | |
| 1383 | -#ifdef ELF_PLAT_INIT | |
| 1384 | - /* | |
| 1385 | - * The ABI may specify that certain registers be set up in special | |
| 1386 | - * ways (on i386 %edx is the address of a DT_FINI function, for | |
| 1387 | - * example. This macro performs whatever initialization to | |
| 1388 | - * the regs structure is required. | |
| 1389 | - */ | |
| 1390 | - ELF_PLAT_INIT(regs); | |
| 1391 | -#endif | |
| 1392 | - | |
| 1393 | - | |
| 1394 | 1224 | info->entry = elf_entry; |
| 1395 | 1225 | |
| 1396 | 1226 | return 0; |
| 1397 | 1227 | } |
| 1398 | 1228 | |
| 1399 | - | |
| 1400 | - | |
| 1401 | -int elf_exec(const char * filename, char ** argv, char ** envp, | |
| 1402 | - struct target_pt_regs * regs, struct image_info *infop) | |
| 1403 | -{ | |
| 1404 | - struct linux_binprm bprm; | |
| 1405 | - int retval; | |
| 1406 | - int i; | |
| 1407 | - | |
| 1408 | - bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); | |
| 1409 | - for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ | |
| 1410 | - bprm.page[i] = 0; | |
| 1411 | - retval = open(filename, O_RDONLY); | |
| 1412 | - if (retval < 0) | |
| 1413 | - return retval; | |
| 1414 | - bprm.fd = retval; | |
| 1415 | - bprm.filename = (char *)filename; | |
| 1416 | - bprm.sh_bang = 0; | |
| 1417 | - bprm.loader = 0; | |
| 1418 | - bprm.exec = 0; | |
| 1419 | - bprm.dont_iput = 0; | |
| 1420 | - bprm.argc = count(argv); | |
| 1421 | - bprm.envc = count(envp); | |
| 1422 | - | |
| 1423 | - retval = prepare_binprm(&bprm); | |
| 1424 | - | |
| 1425 | - if(retval>=0) { | |
| 1426 | - bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p); | |
| 1427 | - bprm.exec = bprm.p; | |
| 1428 | - bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p); | |
| 1429 | - bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p); | |
| 1430 | - if (!bprm.p) { | |
| 1431 | - retval = -E2BIG; | |
| 1432 | - } | |
| 1433 | - } | |
| 1434 | - | |
| 1435 | - if(retval>=0) { | |
| 1436 | - retval = load_elf_binary(&bprm,regs,infop); | |
| 1437 | - } | |
| 1438 | - | |
| 1439 | - if(retval>=0) { | |
| 1440 | - /* success. Initialize important registers */ | |
| 1441 | - init_thread(regs, infop); | |
| 1442 | - return retval; | |
| 1443 | - } | |
| 1444 | - | |
| 1445 | - /* Something went wrong, return the inode and free the argument pages*/ | |
| 1446 | - for (i=0 ; i<MAX_ARG_PAGES ; i++) { | |
| 1447 | - free(bprm.page[i]); | |
| 1448 | - } | |
| 1449 | - return(retval); | |
| 1450 | -} | |
| 1451 | - | |
| 1452 | - | |
| 1453 | 1229 | static int load_aout_interp(void * exptr, int interp_fd) |
| 1454 | 1230 | { |
| 1455 | 1231 | printf("a.out interpreter not yet supported\n"); |
| 1456 | 1232 | return(0); |
| 1457 | 1233 | } |
| 1458 | 1234 | |
| 1235 | +void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) | |
| 1236 | +{ | |
| 1237 | + init_thread(regs, infop); | |
| 1238 | +} | ... | ... |
linux-user/flat.h
0 โ 100644
| 1 | +/* | |
| 2 | + * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com> | |
| 3 | + * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com> | |
| 4 | + * The Silver Hammer Group, Ltd. | |
| 5 | + * | |
| 6 | + * This file provides the definitions and structures needed to | |
| 7 | + * support uClinux flat-format executables. | |
| 8 | + */ | |
| 9 | + | |
| 10 | +#define FLAT_VERSION 0x00000004L | |
| 11 | + | |
| 12 | +#ifdef CONFIG_BINFMT_SHARED_FLAT | |
| 13 | +#define MAX_SHARED_LIBS (4) | |
| 14 | +#else | |
| 15 | +#define MAX_SHARED_LIBS (1) | |
| 16 | +#endif | |
| 17 | + | |
| 18 | +/* | |
| 19 | + * To make everything easier to port and manage cross platform | |
| 20 | + * development, all fields are in network byte order. | |
| 21 | + */ | |
| 22 | + | |
| 23 | +struct flat_hdr { | |
| 24 | + char magic[4]; | |
| 25 | + unsigned long rev; /* version (as above) */ | |
| 26 | + unsigned long entry; /* Offset of first executable instruction | |
| 27 | + with text segment from beginning of file */ | |
| 28 | + unsigned long data_start; /* Offset of data segment from beginning of | |
| 29 | + file */ | |
| 30 | + unsigned long data_end; /* Offset of end of data segment | |
| 31 | + from beginning of file */ | |
| 32 | + unsigned long bss_end; /* Offset of end of bss segment from beginning | |
| 33 | + of file */ | |
| 34 | + | |
| 35 | + /* (It is assumed that data_end through bss_end forms the bss segment.) */ | |
| 36 | + | |
| 37 | + unsigned long stack_size; /* Size of stack, in bytes */ | |
| 38 | + unsigned long reloc_start; /* Offset of relocation records from | |
| 39 | + beginning of file */ | |
| 40 | + unsigned long reloc_count; /* Number of relocation records */ | |
| 41 | + unsigned long flags; | |
| 42 | + unsigned long build_date; /* When the program/library was built */ | |
| 43 | + unsigned long filler[5]; /* Reservered, set to zero */ | |
| 44 | +}; | |
| 45 | + | |
| 46 | +#define FLAT_FLAG_RAM 0x0001 /* load program entirely into RAM */ | |
| 47 | +#define FLAT_FLAG_GOTPIC 0x0002 /* program is PIC with GOT */ | |
| 48 | +#define FLAT_FLAG_GZIP 0x0004 /* all but the header is compressed */ | |
| 49 | +#define FLAT_FLAG_GZDATA 0x0008 /* only data/relocs are compressed (for XIP) */ | |
| 50 | +#define FLAT_FLAG_KTRACE 0x0010 /* output useful kernel trace for debugging */ | |
| 51 | + | |
| 52 | + | |
| 53 | +/* | |
| 54 | + * While it would be nice to keep this header clean, users of older | |
| 55 | + * tools still need this support in the kernel. So this section is | |
| 56 | + * purely for compatibility with old tool chains. | |
| 57 | + * | |
| 58 | + * DO NOT make changes or enhancements to the old format please, just work | |
| 59 | + * with the format above, except to fix bugs with old format support. | |
| 60 | + */ | |
| 61 | + | |
| 62 | +#define OLD_FLAT_VERSION 0x00000002L | |
| 63 | +#define OLD_FLAT_RELOC_TYPE_TEXT 0 | |
| 64 | +#define OLD_FLAT_RELOC_TYPE_DATA 1 | |
| 65 | +#define OLD_FLAT_RELOC_TYPE_BSS 2 | |
| 66 | + | |
| 67 | +# define OLD_FLAT_FLAG_RAM 0x1 /* load program entirely into RAM */ | ... | ... |
linux-user/flatload.c
0 โ 100644
| 1 | +/****************************************************************************/ | |
| 2 | +/* | |
| 3 | + * QEMU bFLT binary loader. Based on linux/fs/binfmt_flat.c | |
| 4 | + * | |
| 5 | + * This program is free software; you can redistribute it and/or modify | |
| 6 | + * it under the terms of the GNU General Public License as published by | |
| 7 | + * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + * (at your option) any later version. | |
| 9 | + * | |
| 10 | + * This program is distributed in the hope that it will be useful, | |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + * GNU General Public License for more details. | |
| 14 | + * | |
| 15 | + * You should have received a copy of the GNU General Public License | |
| 16 | + * along with this program; if not, write to the Free Software | |
| 17 | + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 18 | + * | |
| 19 | + * Copyright (C) 2006 CodeSourcery. | |
| 20 | + * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com> | |
| 21 | + * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com> | |
| 22 | + * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com> | |
| 23 | + * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com> | |
| 24 | + * based heavily on: | |
| 25 | + * | |
| 26 | + * linux/fs/binfmt_aout.c: | |
| 27 | + * Copyright (C) 1991, 1992, 1996 Linus Torvalds | |
| 28 | + * linux/fs/binfmt_flat.c for 2.0 kernel | |
| 29 | + * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com> | |
| 30 | + * JAN/99 -- coded full program relocation (gerg@snapgear.com) | |
| 31 | + */ | |
| 32 | + | |
| 33 | +/* ??? ZFLAT and shared library support is currently disabled. */ | |
| 34 | + | |
| 35 | +/****************************************************************************/ | |
| 36 | + | |
| 37 | +#include <stdio.h> | |
| 38 | +#include <stdlib.h> | |
| 39 | +#include <errno.h> | |
| 40 | +#include <sys/mman.h> | |
| 41 | +#include <unistd.h> | |
| 42 | + | |
| 43 | +#include "qemu.h" | |
| 44 | +#include "flat.h" | |
| 45 | + | |
| 46 | +//#define DEBUG | |
| 47 | + | |
| 48 | +#ifdef DEBUG | |
| 49 | +#define DBG_FLT(a...) printf(a) | |
| 50 | +#else | |
| 51 | +#define DBG_FLT(a...) | |
| 52 | +#endif | |
| 53 | + | |
| 54 | +#define flat_reloc_valid(reloc, size) ((reloc) <= (size)) | |
| 55 | +#define flat_old_ram_flag(flag) (flag) | |
| 56 | +#ifdef TARGET_WORDS_BIGENDIAN | |
| 57 | +#define flat_get_relocate_addr(relval) (relval) | |
| 58 | +#else | |
| 59 | +#define flat_get_relocate_addr(relval) bswap32(relval) | |
| 60 | +#endif | |
| 61 | + | |
| 62 | +#define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */ | |
| 63 | +#define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */ | |
| 64 | + | |
| 65 | +struct lib_info { | |
| 66 | + target_ulong start_code; /* Start of text segment */ | |
| 67 | + target_ulong start_data; /* Start of data segment */ | |
| 68 | + target_ulong end_data; /* Start of bss section */ | |
| 69 | + target_ulong start_brk; /* End of data segment */ | |
| 70 | + target_ulong text_len; /* Length of text segment */ | |
| 71 | + target_ulong entry; /* Start address for this module */ | |
| 72 | + target_ulong build_date; /* When this one was compiled */ | |
| 73 | + short loaded; /* Has this library been loaded? */ | |
| 74 | +}; | |
| 75 | + | |
| 76 | +#ifdef CONFIG_BINFMT_SHARED_FLAT | |
| 77 | +static int load_flat_shared_library(int id, struct lib_info *p); | |
| 78 | +#endif | |
| 79 | + | |
| 80 | +struct linux_binprm; | |
| 81 | + | |
| 82 | +#define ntohl(x) be32_to_cpu(x) | |
| 83 | + | |
| 84 | +/****************************************************************************/ | |
| 85 | +/* | |
| 86 | + * create_flat_tables() parses the env- and arg-strings in new user | |
| 87 | + * memory and creates the pointer tables from them, and puts their | |
| 88 | + * addresses on the "stack", returning the new stack pointer value. | |
| 89 | + */ | |
| 90 | + | |
| 91 | +/* Push a block of strings onto the guest stack. */ | |
| 92 | +static target_ulong copy_strings(target_ulong p, int n, char **s) | |
| 93 | +{ | |
| 94 | + int len; | |
| 95 | + | |
| 96 | + while (n-- > 0) { | |
| 97 | + len = strlen(s[n]) + 1; | |
| 98 | + p -= len; | |
| 99 | + memcpy_to_target(p, s[n], len); | |
| 100 | + } | |
| 101 | + | |
| 102 | + return p; | |
| 103 | +} | |
| 104 | + | |
| 105 | +int target_pread(int fd, target_ulong ptr, target_ulong len, | |
| 106 | + target_ulong offset) | |
| 107 | +{ | |
| 108 | + void *buf; | |
| 109 | + int ret; | |
| 110 | + | |
| 111 | + buf = lock_user(ptr, len, 0); | |
| 112 | + ret = pread(fd, buf, len, offset); | |
| 113 | + unlock_user(buf, ptr, len); | |
| 114 | + return ret; | |
| 115 | +} | |
| 116 | +/****************************************************************************/ | |
| 117 | + | |
| 118 | +#ifdef CONFIG_BINFMT_ZFLAT | |
| 119 | + | |
| 120 | +#include <linux/zlib.h> | |
| 121 | + | |
| 122 | +#define LBUFSIZE 4000 | |
| 123 | + | |
| 124 | +/* gzip flag byte */ | |
| 125 | +#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */ | |
| 126 | +#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ | |
| 127 | +#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ | |
| 128 | +#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ | |
| 129 | +#define COMMENT 0x10 /* bit 4 set: file comment present */ | |
| 130 | +#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ | |
| 131 | +#define RESERVED 0xC0 /* bit 6,7: reserved */ | |
| 132 | + | |
| 133 | +static int decompress_exec( | |
| 134 | + struct linux_binprm *bprm, | |
| 135 | + unsigned long offset, | |
| 136 | + char *dst, | |
| 137 | + long len, | |
| 138 | + int fd) | |
| 139 | +{ | |
| 140 | + unsigned char *buf; | |
| 141 | + z_stream strm; | |
| 142 | + loff_t fpos; | |
| 143 | + int ret, retval; | |
| 144 | + | |
| 145 | + DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len); | |
| 146 | + | |
| 147 | + memset(&strm, 0, sizeof(strm)); | |
| 148 | + strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL); | |
| 149 | + if (strm.workspace == NULL) { | |
| 150 | + DBG_FLT("binfmt_flat: no memory for decompress workspace\n"); | |
| 151 | + return -ENOMEM; | |
| 152 | + } | |
| 153 | + buf = kmalloc(LBUFSIZE, GFP_KERNEL); | |
| 154 | + if (buf == NULL) { | |
| 155 | + DBG_FLT("binfmt_flat: no memory for read buffer\n"); | |
| 156 | + retval = -ENOMEM; | |
| 157 | + goto out_free; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /* Read in first chunk of data and parse gzip header. */ | |
| 161 | + fpos = offset; | |
| 162 | + ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos); | |
| 163 | + | |
| 164 | + strm.next_in = buf; | |
| 165 | + strm.avail_in = ret; | |
| 166 | + strm.total_in = 0; | |
| 167 | + | |
| 168 | + retval = -ENOEXEC; | |
| 169 | + | |
| 170 | + /* Check minimum size -- gzip header */ | |
| 171 | + if (ret < 10) { | |
| 172 | + DBG_FLT("binfmt_flat: file too small?\n"); | |
| 173 | + goto out_free_buf; | |
| 174 | + } | |
| 175 | + | |
| 176 | + /* Check gzip magic number */ | |
| 177 | + if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) { | |
| 178 | + DBG_FLT("binfmt_flat: unknown compression magic?\n"); | |
| 179 | + goto out_free_buf; | |
| 180 | + } | |
| 181 | + | |
| 182 | + /* Check gzip method */ | |
| 183 | + if (buf[2] != 8) { | |
| 184 | + DBG_FLT("binfmt_flat: unknown compression method?\n"); | |
| 185 | + goto out_free_buf; | |
| 186 | + } | |
| 187 | + /* Check gzip flags */ | |
| 188 | + if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) || | |
| 189 | + (buf[3] & RESERVED)) { | |
| 190 | + DBG_FLT("binfmt_flat: unknown flags?\n"); | |
| 191 | + goto out_free_buf; | |
| 192 | + } | |
| 193 | + | |
| 194 | + ret = 10; | |
| 195 | + if (buf[3] & EXTRA_FIELD) { | |
| 196 | + ret += 2 + buf[10] + (buf[11] << 8); | |
| 197 | + if (unlikely(LBUFSIZE == ret)) { | |
| 198 | + DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n"); | |
| 199 | + goto out_free_buf; | |
| 200 | + } | |
| 201 | + } | |
| 202 | + if (buf[3] & ORIG_NAME) { | |
| 203 | + for (; ret < LBUFSIZE && (buf[ret] != 0); ret++) | |
| 204 | + ; | |
| 205 | + if (unlikely(LBUFSIZE == ret)) { | |
| 206 | + DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n"); | |
| 207 | + goto out_free_buf; | |
| 208 | + } | |
| 209 | + } | |
| 210 | + if (buf[3] & COMMENT) { | |
| 211 | + for (; ret < LBUFSIZE && (buf[ret] != 0); ret++) | |
| 212 | + ; | |
| 213 | + if (unlikely(LBUFSIZE == ret)) { | |
| 214 | + DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n"); | |
| 215 | + goto out_free_buf; | |
| 216 | + } | |
| 217 | + } | |
| 218 | + | |
| 219 | + strm.next_in += ret; | |
| 220 | + strm.avail_in -= ret; | |
| 221 | + | |
| 222 | + strm.next_out = dst; | |
| 223 | + strm.avail_out = len; | |
| 224 | + strm.total_out = 0; | |
| 225 | + | |
| 226 | + if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) { | |
| 227 | + DBG_FLT("binfmt_flat: zlib init failed?\n"); | |
| 228 | + goto out_free_buf; | |
| 229 | + } | |
| 230 | + | |
| 231 | + while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) { | |
| 232 | + ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos); | |
| 233 | + if (ret <= 0) | |
| 234 | + break; | |
| 235 | + if (ret >= (unsigned long) -4096) | |
| 236 | + break; | |
| 237 | + len -= ret; | |
| 238 | + | |
| 239 | + strm.next_in = buf; | |
| 240 | + strm.avail_in = ret; | |
| 241 | + strm.total_in = 0; | |
| 242 | + } | |
| 243 | + | |
| 244 | + if (ret < 0) { | |
| 245 | + DBG_FLT("binfmt_flat: decompression failed (%d), %s\n", | |
| 246 | + ret, strm.msg); | |
| 247 | + goto out_zlib; | |
| 248 | + } | |
| 249 | + | |
| 250 | + retval = 0; | |
| 251 | +out_zlib: | |
| 252 | + zlib_inflateEnd(&strm); | |
| 253 | +out_free_buf: | |
| 254 | + kfree(buf); | |
| 255 | +out_free: | |
| 256 | + kfree(strm.workspace); | |
| 257 | +out: | |
| 258 | + return retval; | |
| 259 | +} | |
| 260 | + | |
| 261 | +#endif /* CONFIG_BINFMT_ZFLAT */ | |
| 262 | + | |
| 263 | +/****************************************************************************/ | |
| 264 | + | |
| 265 | +static target_ulong | |
| 266 | +calc_reloc(target_ulong r, struct lib_info *p, int curid, int internalp) | |
| 267 | +{ | |
| 268 | + target_ulong addr; | |
| 269 | + int id; | |
| 270 | + target_ulong start_brk; | |
| 271 | + target_ulong start_data; | |
| 272 | + target_ulong text_len; | |
| 273 | + target_ulong start_code; | |
| 274 | + | |
| 275 | +#ifdef CONFIG_BINFMT_SHARED_FLAT | |
| 276 | +#error needs checking | |
| 277 | + if (r == 0) | |
| 278 | + id = curid; /* Relocs of 0 are always self referring */ | |
| 279 | + else { | |
| 280 | + id = (r >> 24) & 0xff; /* Find ID for this reloc */ | |
| 281 | + r &= 0x00ffffff; /* Trim ID off here */ | |
| 282 | + } | |
| 283 | + if (id >= MAX_SHARED_LIBS) { | |
| 284 | + fprintf(stderr, "BINFMT_FLAT: reference 0x%x to shared library %d\n", | |
| 285 | + (unsigned) r, id); | |
| 286 | + goto failed; | |
| 287 | + } | |
| 288 | + if (curid != id) { | |
| 289 | + if (internalp) { | |
| 290 | + fprintf(stderr, "BINFMT_FLAT: reloc address 0x%x not " | |
| 291 | + "in same module (%d != %d)\n", | |
| 292 | + (unsigned) r, curid, id); | |
| 293 | + goto failed; | |
| 294 | + } else if ( ! p[id].loaded && | |
| 295 | + load_flat_shared_library(id, p) > (unsigned long) -4096) { | |
| 296 | + fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id); | |
| 297 | + goto failed; | |
| 298 | + } | |
| 299 | + /* Check versioning information (i.e. time stamps) */ | |
| 300 | + if (p[id].build_date && p[curid].build_date | |
| 301 | + && p[curid].build_date < p[id].build_date) { | |
| 302 | + fprintf(stderr, "BINFMT_FLAT: library %d is younger than %d\n", | |
| 303 | + id, curid); | |
| 304 | + goto failed; | |
| 305 | + } | |
| 306 | + } | |
| 307 | +#else | |
| 308 | + id = 0; | |
| 309 | +#endif | |
| 310 | + | |
| 311 | + start_brk = p[id].start_brk; | |
| 312 | + start_data = p[id].start_data; | |
| 313 | + start_code = p[id].start_code; | |
| 314 | + text_len = p[id].text_len; | |
| 315 | + | |
| 316 | + if (!flat_reloc_valid(r, start_brk - start_data + text_len)) { | |
| 317 | + fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x " | |
| 318 | + "(0 - 0x%x/0x%x)\n", | |
| 319 | + (int) r,(int)(start_brk-start_code),(int)text_len); | |
| 320 | + goto failed; | |
| 321 | + } | |
| 322 | + | |
| 323 | + if (r < text_len) /* In text segment */ | |
| 324 | + addr = r + start_code; | |
| 325 | + else /* In data segment */ | |
| 326 | + addr = r - text_len + start_data; | |
| 327 | + | |
| 328 | + /* Range checked already above so doing the range tests is redundant...*/ | |
| 329 | + return(addr); | |
| 330 | + | |
| 331 | +failed: | |
| 332 | + abort(); | |
| 333 | + return RELOC_FAILED; | |
| 334 | +} | |
| 335 | + | |
| 336 | +/****************************************************************************/ | |
| 337 | + | |
| 338 | +/* ??? This does not handle endianness correctly. */ | |
| 339 | +void old_reloc(struct lib_info *libinfo, uint32_t rl) | |
| 340 | +{ | |
| 341 | +#ifdef DEBUG | |
| 342 | + char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" }; | |
| 343 | +#endif | |
| 344 | + uint32_t *ptr; | |
| 345 | + uint32_t offset; | |
| 346 | + int reloc_type; | |
| 347 | + | |
| 348 | + offset = rl & 0x3fffffff; | |
| 349 | + reloc_type = rl >> 30; | |
| 350 | + /* ??? How to handle this? */ | |
| 351 | +#if defined(CONFIG_COLDFIRE) | |
| 352 | + ptr = (uint32_t *) (libinfo->start_code + offset); | |
| 353 | +#else | |
| 354 | + ptr = (uint32_t *) (libinfo->start_data + offset); | |
| 355 | +#endif | |
| 356 | + | |
| 357 | +#ifdef DEBUG | |
| 358 | + fprintf(stderr, "Relocation of variable at DATASEG+%x " | |
| 359 | + "(address %p, currently %x) into segment %s\n", | |
| 360 | + offset, ptr, (int)*ptr, segment[reloc_type]); | |
| 361 | +#endif | |
| 362 | + | |
| 363 | + switch (reloc_type) { | |
| 364 | + case OLD_FLAT_RELOC_TYPE_TEXT: | |
| 365 | + *ptr += libinfo->start_code; | |
| 366 | + break; | |
| 367 | + case OLD_FLAT_RELOC_TYPE_DATA: | |
| 368 | + *ptr += libinfo->start_data; | |
| 369 | + break; | |
| 370 | + case OLD_FLAT_RELOC_TYPE_BSS: | |
| 371 | + *ptr += libinfo->end_data; | |
| 372 | + break; | |
| 373 | + default: | |
| 374 | + fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n", | |
| 375 | + reloc_type); | |
| 376 | + break; | |
| 377 | + } | |
| 378 | + DBG_FLT("Relocation became %x\n", (int)*ptr); | |
| 379 | +} | |
| 380 | + | |
| 381 | +/****************************************************************************/ | |
| 382 | + | |
| 383 | +static int load_flat_file(struct linux_binprm * bprm, | |
| 384 | + struct lib_info *libinfo, int id, target_ulong *extra_stack) | |
| 385 | +{ | |
| 386 | + struct flat_hdr * hdr; | |
| 387 | + target_ulong textpos = 0, datapos = 0, result; | |
| 388 | + target_ulong realdatastart = 0; | |
| 389 | + target_ulong text_len, data_len, bss_len, stack_len, flags; | |
| 390 | + target_ulong memp = 0; /* for finding the brk area */ | |
| 391 | + target_ulong extra; | |
| 392 | + target_ulong reloc = 0, rp; | |
| 393 | + int i, rev, relocs = 0; | |
| 394 | + target_ulong fpos; | |
| 395 | + target_ulong start_code, end_code; | |
| 396 | + | |
| 397 | + hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */ | |
| 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 | + /* Don't allow old format executables to use shared libraries */ | |
| 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 | + * fix up the flags for the older format, there were all kinds | |
| 427 | + * of endian hacks, this only works for the simple cases | |
| 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 | + * calculate the extra space we need to map in | |
| 441 | + */ | |
| 442 | + extra = relocs * sizeof(target_ulong); | |
| 443 | + if (extra < bss_len + stack_len) | |
| 444 | + extra = bss_len + stack_len; | |
| 445 | + | |
| 446 | + /* | |
| 447 | + * there are a couple of cases here, the separate code/data | |
| 448 | + * case, and then the fully copied to RAM case which lumps | |
| 449 | + * it all together. | |
| 450 | + */ | |
| 451 | + if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) { | |
| 452 | + /* | |
| 453 | + * this should give us a ROM ptr, but if it doesn't we don't | |
| 454 | + * really care | |
| 455 | + */ | |
| 456 | + DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n"); | |
| 457 | + | |
| 458 | + textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC, | |
| 459 | + MAP_PRIVATE, bprm->fd, 0); | |
| 460 | + if (textpos == -1) { | |
| 461 | + fprintf(stderr, "Unable to mmap process text\n"); | |
| 462 | + return -1; | |
| 463 | + } | |
| 464 | + | |
| 465 | + realdatastart = target_mmap(0, data_len + extra + | |
| 466 | + MAX_SHARED_LIBS * sizeof(target_ulong), | |
| 467 | + PROT_READ|PROT_WRITE|PROT_EXEC, | |
| 468 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 469 | + | |
| 470 | + if (realdatastart == -1) { | |
| 471 | + fprintf(stderr, "Unable to allocate RAM for process data\n"); | |
| 472 | + return realdatastart; | |
| 473 | + } | |
| 474 | + datapos = realdatastart + MAX_SHARED_LIBS * sizeof(target_ulong); | |
| 475 | + | |
| 476 | + DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n", | |
| 477 | + (int)(data_len + bss_len + stack_len), (int)datapos); | |
| 478 | + | |
| 479 | + fpos = ntohl(hdr->data_start); | |
| 480 | +#ifdef CONFIG_BINFMT_ZFLAT | |
| 481 | + if (flags & FLAT_FLAG_GZDATA) { | |
| 482 | + result = decompress_exec(bprm, fpos, (char *) datapos, | |
| 483 | + data_len + (relocs * sizeof(target_ulong))) | |
| 484 | + } else | |
| 485 | +#endif | |
| 486 | + { | |
| 487 | + result = target_pread(bprm->fd, datapos, | |
| 488 | + data_len + (relocs * sizeof(target_ulong)), | |
| 489 | + fpos); | |
| 490 | + } | |
| 491 | + if (result < 0) { | |
| 492 | + fprintf(stderr, "Unable to read data+bss\n"); | |
| 493 | + return result; | |
| 494 | + } | |
| 495 | + | |
| 496 | + reloc = datapos + (ntohl(hdr->reloc_start) - text_len); | |
| 497 | + memp = realdatastart; | |
| 498 | + | |
| 499 | + } else { | |
| 500 | + | |
| 501 | + textpos = target_mmap(0, text_len + data_len + extra + | |
| 502 | + MAX_SHARED_LIBS * sizeof(target_ulong), | |
| 503 | + PROT_READ | PROT_EXEC | PROT_WRITE, | |
| 504 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 505 | + if (textpos == -1 ) { | |
| 506 | + fprintf(stderr, "Unable to allocate RAM for process text/data\n"); | |
| 507 | + return -1; | |
| 508 | + } | |
| 509 | + | |
| 510 | + realdatastart = textpos + ntohl(hdr->data_start); | |
| 511 | + datapos = realdatastart + MAX_SHARED_LIBS * sizeof(target_ulong); | |
| 512 | + reloc = (textpos + ntohl(hdr->reloc_start) + | |
| 513 | + MAX_SHARED_LIBS * sizeof(target_ulong)); | |
| 514 | + memp = textpos; | |
| 515 | + | |
| 516 | +#ifdef CONFIG_BINFMT_ZFLAT | |
| 517 | +#error code needs checking | |
| 518 | + /* | |
| 519 | + * load it all in and treat it like a RAM load from now on | |
| 520 | + */ | |
| 521 | + if (flags & FLAT_FLAG_GZIP) { | |
| 522 | + result = decompress_exec(bprm, sizeof (struct flat_hdr), | |
| 523 | + (((char *) textpos) + sizeof (struct flat_hdr)), | |
| 524 | + (text_len + data_len + (relocs * sizeof(unsigned long)) | |
| 525 | + - sizeof (struct flat_hdr)), | |
| 526 | + 0); | |
| 527 | + memmove((void *) datapos, (void *) realdatastart, | |
| 528 | + data_len + (relocs * sizeof(unsigned long))); | |
| 529 | + } else if (flags & FLAT_FLAG_GZDATA) { | |
| 530 | + fpos = 0; | |
| 531 | + result = bprm->file->f_op->read(bprm->file, | |
| 532 | + (char *) textpos, text_len, &fpos); | |
| 533 | + if (result < (unsigned long) -4096) | |
| 534 | + result = decompress_exec(bprm, text_len, (char *) datapos, | |
| 535 | + data_len + (relocs * sizeof(unsigned long)), 0); | |
| 536 | + } | |
| 537 | + else | |
| 538 | +#endif | |
| 539 | + { | |
| 540 | + result = target_pread(bprm->fd, textpos, | |
| 541 | + text_len, 0); | |
| 542 | + if (result >= 0) { | |
| 543 | + result = target_pread(bprm->fd, datapos, | |
| 544 | + data_len + (relocs * sizeof(target_ulong)), | |
| 545 | + ntohl(hdr->data_start)); | |
| 546 | + } | |
| 547 | + } | |
| 548 | + if (result < 0) { | |
| 549 | + fprintf(stderr, "Unable to read code+data+bss\n"); | |
| 550 | + return result; | |
| 551 | + } | |
| 552 | + } | |
| 553 | + | |
| 554 | + DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n", | |
| 555 | + (int)textpos, 0x00ffffff&ntohl(hdr->entry), | |
| 556 | + ntohl(hdr->data_start)); | |
| 557 | + | |
| 558 | + /* The main program needs a little extra setup in the task structure */ | |
| 559 | + start_code = textpos + sizeof (struct flat_hdr); | |
| 560 | + end_code = textpos + text_len; | |
| 561 | + | |
| 562 | + DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n", | |
| 563 | + id ? "Lib" : "Load", bprm->filename, | |
| 564 | + (int) start_code, (int) end_code, | |
| 565 | + (int) datapos, | |
| 566 | + (int) (datapos + data_len), | |
| 567 | + (int) (datapos + data_len), | |
| 568 | + (int) (((datapos + data_len + bss_len) + 3) & ~3)); | |
| 569 | + | |
| 570 | + text_len -= sizeof(struct flat_hdr); /* the real code len */ | |
| 571 | + | |
| 572 | + /* Store the current module values into the global library structure */ | |
| 573 | + libinfo[id].start_code = start_code; | |
| 574 | + libinfo[id].start_data = datapos; | |
| 575 | + libinfo[id].end_data = datapos + data_len; | |
| 576 | + libinfo[id].start_brk = datapos + data_len + bss_len; | |
| 577 | + libinfo[id].text_len = text_len; | |
| 578 | + libinfo[id].loaded = 1; | |
| 579 | + libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos; | |
| 580 | + libinfo[id].build_date = ntohl(hdr->build_date); | |
| 581 | + | |
| 582 | + /* | |
| 583 | + * We just load the allocations into some temporary memory to | |
| 584 | + * help simplify all this mumbo jumbo | |
| 585 | + * | |
| 586 | + * We've got two different sections of relocation entries. | |
| 587 | + * The first is the GOT which resides at the begining of the data segment | |
| 588 | + * and is terminated with a -1. This one can be relocated in place. | |
| 589 | + * The second is the extra relocation entries tacked after the image's | |
| 590 | + * data segment. These require a little more processing as the entry is | |
| 591 | + * really an offset into the image which contains an offset into the | |
| 592 | + * image. | |
| 593 | + */ | |
| 594 | + if (flags & FLAT_FLAG_GOTPIC) { | |
| 595 | + rp = datapos; | |
| 596 | + while (1) { | |
| 597 | + target_ulong addr; | |
| 598 | + addr = tgetl(rp); | |
| 599 | + if (addr == -1) | |
| 600 | + break; | |
| 601 | + if (addr) { | |
| 602 | + addr = calc_reloc(addr, libinfo, id, 0); | |
| 603 | + if (addr == RELOC_FAILED) | |
| 604 | + return -ENOEXEC; | |
| 605 | + tputl(rp, addr); | |
| 606 | + } | |
| 607 | + rp += sizeof(target_ulong); | |
| 608 | + } | |
| 609 | + } | |
| 610 | + | |
| 611 | + /* | |
| 612 | + * Now run through the relocation entries. | |
| 613 | + * We've got to be careful here as C++ produces relocatable zero | |
| 614 | + * entries in the constructor and destructor tables which are then | |
| 615 | + * tested for being not zero (which will always occur unless we're | |
| 616 | + * based from address zero). This causes an endless loop as __start | |
| 617 | + * is at zero. The solution used is to not relocate zero addresses. | |
| 618 | + * This has the negative side effect of not allowing a global data | |
| 619 | + * reference to be statically initialised to _stext (I've moved | |
| 620 | + * __start to address 4 so that is okay). | |
| 621 | + */ | |
| 622 | + if (rev > OLD_FLAT_VERSION) { | |
| 623 | + for (i = 0; i < relocs; i++) { | |
| 624 | + target_ulong addr, relval; | |
| 625 | + | |
| 626 | + /* Get the address of the pointer to be | |
| 627 | + relocated (of course, the address has to be | |
| 628 | + relocated first). */ | |
| 629 | + relval = tgetl(reloc + i * sizeof (target_ulong)); | |
| 630 | + addr = flat_get_relocate_addr(relval); | |
| 631 | + rp = calc_reloc(addr, libinfo, id, 1); | |
| 632 | + if (rp == RELOC_FAILED) | |
| 633 | + return -ENOEXEC; | |
| 634 | + | |
| 635 | + /* Get the pointer's value. */ | |
| 636 | + addr = tgetl(rp); | |
| 637 | + if (addr != 0) { | |
| 638 | + /* | |
| 639 | + * Do the relocation. PIC relocs in the data section are | |
| 640 | + * already in target order | |
| 641 | + */ | |
| 642 | + | |
| 643 | +#ifndef TARGET_WORDS_BIGENDIAN | |
| 644 | + if ((flags & FLAT_FLAG_GOTPIC) == 0) | |
| 645 | + addr = bswap32(addr); | |
| 646 | +#endif | |
| 647 | + addr = calc_reloc(addr, libinfo, id, 0); | |
| 648 | + if (addr == RELOC_FAILED) | |
| 649 | + return -ENOEXEC; | |
| 650 | + | |
| 651 | + /* Write back the relocated pointer. */ | |
| 652 | + tputl(rp, addr); | |
| 653 | + } | |
| 654 | + } | |
| 655 | + } else { | |
| 656 | + for (i = 0; i < relocs; i++) { | |
| 657 | + target_ulong relval; | |
| 658 | + relval = tgetl(reloc + i * sizeof (target_ulong)); | |
| 659 | + old_reloc(&libinfo[0], relval); | |
| 660 | + } | |
| 661 | + } | |
| 662 | + | |
| 663 | + /* zero the BSS. */ | |
| 664 | + memset((void*)(datapos + data_len), 0, bss_len); | |
| 665 | + | |
| 666 | + return 0; | |
| 667 | +} | |
| 668 | + | |
| 669 | + | |
| 670 | +/****************************************************************************/ | |
| 671 | +#ifdef CONFIG_BINFMT_SHARED_FLAT | |
| 672 | + | |
| 673 | +/* | |
| 674 | + * Load a shared library into memory. The library gets its own data | |
| 675 | + * segment (including bss) but not argv/argc/environ. | |
| 676 | + */ | |
| 677 | + | |
| 678 | +static int load_flat_shared_library(int id, struct lib_info *libs) | |
| 679 | +{ | |
| 680 | + struct linux_binprm bprm; | |
| 681 | + int res; | |
| 682 | + char buf[16]; | |
| 683 | + | |
| 684 | + /* Create the file name */ | |
| 685 | + sprintf(buf, "/lib/lib%d.so", id); | |
| 686 | + | |
| 687 | + /* Open the file up */ | |
| 688 | + bprm.filename = buf; | |
| 689 | + bprm.file = open_exec(bprm.filename); | |
| 690 | + res = PTR_ERR(bprm.file); | |
| 691 | + if (IS_ERR(bprm.file)) | |
| 692 | + return res; | |
| 693 | + | |
| 694 | + res = prepare_binprm(&bprm); | |
| 695 | + | |
| 696 | + if (res <= (unsigned long)-4096) | |
| 697 | + res = load_flat_file(&bprm, libs, id, NULL); | |
| 698 | + if (bprm.file) { | |
| 699 | + allow_write_access(bprm.file); | |
| 700 | + fput(bprm.file); | |
| 701 | + bprm.file = NULL; | |
| 702 | + } | |
| 703 | + return(res); | |
| 704 | +} | |
| 705 | + | |
| 706 | +#endif /* CONFIG_BINFMT_SHARED_FLAT */ | |
| 707 | + | |
| 708 | +int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, | |
| 709 | + struct image_info * info) | |
| 710 | +{ | |
| 711 | + struct lib_info libinfo[MAX_SHARED_LIBS]; | |
| 712 | + target_ulong p = bprm->p; | |
| 713 | + target_ulong stack_len; | |
| 714 | + target_ulong start_addr; | |
| 715 | + target_ulong sp; | |
| 716 | + int res; | |
| 717 | + int i, j; | |
| 718 | + | |
| 719 | + memset(libinfo, 0, sizeof(libinfo)); | |
| 720 | + /* | |
| 721 | + * We have to add the size of our arguments to our stack size | |
| 722 | + * otherwise it's too easy for users to create stack overflows | |
| 723 | + * by passing in a huge argument list. And yes, we have to be | |
| 724 | + * pedantic and include space for the argv/envp array as it may have | |
| 725 | + * a lot of entries. | |
| 726 | + */ | |
| 727 | +#define TOP_OF_ARGS (TARGET_PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *)) | |
| 728 | + stack_len = TOP_OF_ARGS - bprm->p; /* the strings */ | |
| 729 | + stack_len += (bprm->argc + 1) * 4; /* the argv array */ | |
| 730 | + stack_len += (bprm->envc + 1) * 4; /* the envp array */ | |
| 731 | + | |
| 732 | + | |
| 733 | + res = load_flat_file(bprm, libinfo, 0, &stack_len); | |
| 734 | + if (res > (unsigned long)-4096) | |
| 735 | + return res; | |
| 736 | + | |
| 737 | + /* Update data segment pointers for all libraries */ | |
| 738 | + for (i=0; i<MAX_SHARED_LIBS; i++) { | |
| 739 | + if (libinfo[i].loaded) { | |
| 740 | + target_ulong p; | |
| 741 | + p = libinfo[i].start_data; | |
| 742 | + for (j=0; j<MAX_SHARED_LIBS; j++) { | |
| 743 | + p -= 4; | |
| 744 | + tput32(p, libinfo[j].loaded | |
| 745 | + ? libinfo[j].start_data | |
| 746 | + : UNLOADED_LIB); | |
| 747 | + } | |
| 748 | + } | |
| 749 | + } | |
| 750 | + | |
| 751 | + p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4; | |
| 752 | + DBG_FLT("p=%x\n", (int)p); | |
| 753 | + | |
| 754 | + /* Copy argv/envp. */ | |
| 755 | + p = copy_strings(p, bprm->argc, bprm->argv); | |
| 756 | + p = copy_strings(p, bprm->envc, bprm->envp); | |
| 757 | + /* Align stack. */ | |
| 758 | + sp = p & ~(target_ulong)(sizeof(target_ulong) - 1); | |
| 759 | + sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p, 1); | |
| 760 | + | |
| 761 | + /* Fake some return addresses to ensure the call chain will | |
| 762 | + * initialise library in order for us. We are required to call | |
| 763 | + * lib 1 first, then 2, ... and finally the main program (id 0). | |
| 764 | + */ | |
| 765 | + start_addr = libinfo[0].entry; | |
| 766 | + | |
| 767 | +#ifdef CONFIG_BINFMT_SHARED_FLAT | |
| 768 | +#error here | |
| 769 | + for (i = MAX_SHARED_LIBS-1; i>0; i--) { | |
| 770 | + if (libinfo[i].loaded) { | |
| 771 | + /* Push previos first to call address */ | |
| 772 | + --sp; put_user(start_addr, sp); | |
| 773 | + start_addr = libinfo[i].entry; | |
| 774 | + } | |
| 775 | + } | |
| 776 | +#endif | |
| 777 | + | |
| 778 | + /* Stash our initial stack pointer into the mm structure */ | |
| 779 | + info->start_code = libinfo[0].start_code; | |
| 780 | + info->end_code = libinfo[0].start_code = libinfo[0].text_len; | |
| 781 | + info->start_data = libinfo[0].start_data; | |
| 782 | + info->end_data = libinfo[0].end_data; | |
| 783 | + info->start_brk = libinfo[0].start_brk; | |
| 784 | + info->start_stack = sp; | |
| 785 | + info->entry = start_addr; | |
| 786 | + DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n", | |
| 787 | + (int)info->entry, (int)info->start_stack); | |
| 788 | + | |
| 789 | + return 0; | |
| 790 | +} | ... | ... |
linux-user/linuxload.c
0 โ 100644
| 1 | +/* Code for loading Linux executables. Mostly linux kenrel code. */ | |
| 2 | + | |
| 3 | +#include <sys/types.h> | |
| 4 | +#include <sys/stat.h> | |
| 5 | +#include <fcntl.h> | |
| 6 | +#include <errno.h> | |
| 7 | +#include <unistd.h> | |
| 8 | +#include <stdio.h> | |
| 9 | +#include <stdlib.h> | |
| 10 | + | |
| 11 | +#include "qemu.h" | |
| 12 | + | |
| 13 | +#define NGROUPS 32 | |
| 14 | + | |
| 15 | +/* ??? This should really be somewhere else. */ | |
| 16 | +void memcpy_to_target(target_ulong dest, const void *src, | |
| 17 | + unsigned long len) | |
| 18 | +{ | |
| 19 | + void *host_ptr; | |
| 20 | + | |
| 21 | + host_ptr = lock_user(dest, len, 0); | |
| 22 | + memcpy(host_ptr, src, len); | |
| 23 | + unlock_user(host_ptr, dest, 1); | |
| 24 | +} | |
| 25 | + | |
| 26 | +static int in_group_p(gid_t g) | |
| 27 | +{ | |
| 28 | + /* return TRUE if we're in the specified group, FALSE otherwise */ | |
| 29 | + int ngroup; | |
| 30 | + int i; | |
| 31 | + gid_t grouplist[NGROUPS]; | |
| 32 | + | |
| 33 | + ngroup = getgroups(NGROUPS, grouplist); | |
| 34 | + for(i = 0; i < ngroup; i++) { | |
| 35 | + if(grouplist[i] == g) { | |
| 36 | + return 1; | |
| 37 | + } | |
| 38 | + } | |
| 39 | + return 0; | |
| 40 | +} | |
| 41 | + | |
| 42 | +static int count(char ** vec) | |
| 43 | +{ | |
| 44 | + int i; | |
| 45 | + | |
| 46 | + for(i = 0; *vec; i++) { | |
| 47 | + vec++; | |
| 48 | + } | |
| 49 | + | |
| 50 | + return(i); | |
| 51 | +} | |
| 52 | + | |
| 53 | +static int prepare_binprm(struct linux_binprm *bprm) | |
| 54 | +{ | |
| 55 | + struct stat st; | |
| 56 | + int mode; | |
| 57 | + int retval, id_change; | |
| 58 | + | |
| 59 | + if(fstat(bprm->fd, &st) < 0) { | |
| 60 | + return(-errno); | |
| 61 | + } | |
| 62 | + | |
| 63 | + mode = st.st_mode; | |
| 64 | + if(!S_ISREG(mode)) { /* Must be regular file */ | |
| 65 | + return(-EACCES); | |
| 66 | + } | |
| 67 | + if(!(mode & 0111)) { /* Must have at least one execute bit set */ | |
| 68 | + return(-EACCES); | |
| 69 | + } | |
| 70 | + | |
| 71 | + bprm->e_uid = geteuid(); | |
| 72 | + bprm->e_gid = getegid(); | |
| 73 | + id_change = 0; | |
| 74 | + | |
| 75 | + /* Set-uid? */ | |
| 76 | + if(mode & S_ISUID) { | |
| 77 | + bprm->e_uid = st.st_uid; | |
| 78 | + if(bprm->e_uid != geteuid()) { | |
| 79 | + id_change = 1; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + /* Set-gid? */ | |
| 84 | + /* | |
| 85 | + * If setgid is set but no group execute bit then this | |
| 86 | + * is a candidate for mandatory locking, not a setgid | |
| 87 | + * executable. | |
| 88 | + */ | |
| 89 | + if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { | |
| 90 | + bprm->e_gid = st.st_gid; | |
| 91 | + if (!in_group_p(bprm->e_gid)) { | |
| 92 | + id_change = 1; | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + memset(bprm->buf, 0, sizeof(bprm->buf)); | |
| 97 | + retval = lseek(bprm->fd, 0L, SEEK_SET); | |
| 98 | + if(retval >= 0) { | |
| 99 | + retval = read(bprm->fd, bprm->buf, 128); | |
| 100 | + } | |
| 101 | + if(retval < 0) { | |
| 102 | + perror("prepare_binprm"); | |
| 103 | + exit(-1); | |
| 104 | + /* return(-errno); */ | |
| 105 | + } | |
| 106 | + else { | |
| 107 | + return(retval); | |
| 108 | + } | |
| 109 | +} | |
| 110 | + | |
| 111 | +/* Construct the envp and argv tables on the target stack. */ | |
| 112 | +target_ulong loader_build_argptr(int envc, int argc, target_ulong sp, | |
| 113 | + target_ulong stringp, int push_ptr) | |
| 114 | +{ | |
| 115 | + int n = sizeof(target_ulong); | |
| 116 | + target_ulong envp; | |
| 117 | + target_ulong argv; | |
| 118 | + | |
| 119 | + sp -= (envc + 1) * n; | |
| 120 | + envp = sp; | |
| 121 | + sp -= (argc + 1) * n; | |
| 122 | + argv = sp; | |
| 123 | + if (push_ptr) { | |
| 124 | + sp -= n; tputl(sp, envp); | |
| 125 | + sp -= n; tputl(sp, argv); | |
| 126 | + } | |
| 127 | + sp -= n; tputl(sp, argc); | |
| 128 | + | |
| 129 | + while (argc-- > 0) { | |
| 130 | + tputl(argv, stringp); argv += n; | |
| 131 | + stringp += target_strlen(stringp) + 1; | |
| 132 | + } | |
| 133 | + tputl(argv, 0); | |
| 134 | + while (envc-- > 0) { | |
| 135 | + tputl(envp, stringp); envp += n; | |
| 136 | + stringp += target_strlen(stringp) + 1; | |
| 137 | + } | |
| 138 | + tputl(envp, 0); | |
| 139 | + | |
| 140 | + return sp; | |
| 141 | +} | |
| 142 | + | |
| 143 | +int loader_exec(const char * filename, char ** argv, char ** envp, | |
| 144 | + struct target_pt_regs * regs, struct image_info *infop) | |
| 145 | +{ | |
| 146 | + struct linux_binprm bprm; | |
| 147 | + int retval; | |
| 148 | + int i; | |
| 149 | + | |
| 150 | + bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); | |
| 151 | + for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ | |
| 152 | + bprm.page[i] = 0; | |
| 153 | + retval = open(filename, O_RDONLY); | |
| 154 | + if (retval < 0) | |
| 155 | + return retval; | |
| 156 | + bprm.fd = retval; | |
| 157 | + bprm.filename = (char *)filename; | |
| 158 | + bprm.argc = count(argv); | |
| 159 | + bprm.argv = argv; | |
| 160 | + bprm.envc = count(envp); | |
| 161 | + bprm.envp = envp; | |
| 162 | + | |
| 163 | + retval = prepare_binprm(&bprm); | |
| 164 | + | |
| 165 | + if(retval>=0) { | |
| 166 | + if (bprm.buf[0] == 0x7f | |
| 167 | + && bprm.buf[1] == 'E' | |
| 168 | + && bprm.buf[2] == 'L' | |
| 169 | + && bprm.buf[3] == 'F') { | |
| 170 | + retval = load_elf_binary(&bprm,regs,infop); | |
| 171 | +#if defined(TARGET_HAS_BFLT) | |
| 172 | + } else if (bprm.buf[0] == 'b' | |
| 173 | + && bprm.buf[1] == 'F' | |
| 174 | + && bprm.buf[2] == 'L' | |
| 175 | + && bprm.buf[3] == 'T') { | |
| 176 | + retval = load_flt_binary(&bprm,regs,infop); | |
| 177 | +#endif | |
| 178 | + } else { | |
| 179 | + fprintf(stderr, "Unknown binary format\n"); | |
| 180 | + return -1; | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + if(retval>=0) { | |
| 185 | + /* success. Initialize important registers */ | |
| 186 | + do_init_thread(regs, infop); | |
| 187 | + return retval; | |
| 188 | + } | |
| 189 | + | |
| 190 | + /* Something went wrong, return the inode and free the argument pages*/ | |
| 191 | + for (i=0 ; i<MAX_ARG_PAGES ; i++) { | |
| 192 | + free(bprm.page[i]); | |
| 193 | + } | |
| 194 | + return(retval); | |
| 195 | +} | ... | ... |
linux-user/main.c
| ... | ... | @@ -1545,7 +1545,7 @@ int main(int argc, char **argv) |
| 1545 | 1545 | env = cpu_init(); |
| 1546 | 1546 | global_env = env; |
| 1547 | 1547 | |
| 1548 | - if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { | |
| 1548 | + if (loader_exec(filename, argv+optind, environ, regs, info) != 0) { | |
| 1549 | 1549 | printf("Error loading %s\n", filename); |
| 1550 | 1550 | _exit(1); |
| 1551 | 1551 | } |
| ... | ... | @@ -1556,6 +1556,7 @@ int main(int argc, char **argv) |
| 1556 | 1556 | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk); |
| 1557 | 1557 | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code); |
| 1558 | 1558 | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code); |
| 1559 | + fprintf(logfile, "start_data 0x%08lx\n" , info->start_data); | |
| 1559 | 1560 | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data); |
| 1560 | 1561 | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack); |
| 1561 | 1562 | fprintf(logfile, "brk 0x%08lx\n" , info->brk); | ... | ... |
linux-user/qemu.h
| ... | ... | @@ -18,6 +18,7 @@ |
| 18 | 18 | struct image_info { |
| 19 | 19 | unsigned long start_code; |
| 20 | 20 | unsigned long end_code; |
| 21 | + unsigned long start_data; | |
| 21 | 22 | unsigned long end_data; |
| 22 | 23 | unsigned long start_brk; |
| 23 | 24 | unsigned long brk; |
| ... | ... | @@ -25,10 +26,6 @@ struct image_info { |
| 25 | 26 | unsigned long mmap; |
| 26 | 27 | unsigned long rss; |
| 27 | 28 | unsigned long start_stack; |
| 28 | - unsigned long arg_start; | |
| 29 | - unsigned long arg_end; | |
| 30 | - unsigned long env_start; | |
| 31 | - unsigned long env_end; | |
| 32 | 29 | unsigned long entry; |
| 33 | 30 | int personality; |
| 34 | 31 | }; |
| ... | ... | @@ -82,9 +79,43 @@ typedef struct TaskState { |
| 82 | 79 | extern TaskState *first_task_state; |
| 83 | 80 | extern const char *qemu_uname_release; |
| 84 | 81 | |
| 85 | -int elf_exec(const char * filename, char ** argv, char ** envp, | |
| 82 | +/* ??? See if we can avoid exposing so much of the loader internals. */ | |
| 83 | +/* | |
| 84 | + * MAX_ARG_PAGES defines the number of pages allocated for arguments | |
| 85 | + * and envelope for the new program. 32 should suffice, this gives | |
| 86 | + * a maximum env+arg of 128kB w/4KB pages! | |
| 87 | + */ | |
| 88 | +#define MAX_ARG_PAGES 32 | |
| 89 | + | |
| 90 | +/* | |
| 91 | + * This structure is used to hold the arguments that are | |
| 92 | + * used when loading binaries. | |
| 93 | + */ | |
| 94 | +struct linux_binprm { | |
| 95 | + char buf[128]; | |
| 96 | + void *page[MAX_ARG_PAGES]; | |
| 97 | + unsigned long p; | |
| 98 | + int fd; | |
| 99 | + int e_uid, e_gid; | |
| 100 | + int argc, envc; | |
| 101 | + char **argv; | |
| 102 | + char **envp; | |
| 103 | + char * filename; /* Name of binary */ | |
| 104 | +}; | |
| 105 | + | |
| 106 | +void do_init_thread(struct target_pt_regs *regs, struct image_info *infop); | |
| 107 | +target_ulong loader_build_argptr(int envc, int argc, target_ulong sp, | |
| 108 | + target_ulong stringp, int push_ptr); | |
| 109 | +int loader_exec(const char * filename, char ** argv, char ** envp, | |
| 86 | 110 | struct target_pt_regs * regs, struct image_info *infop); |
| 87 | 111 | |
| 112 | +int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, | |
| 113 | + struct image_info * info); | |
| 114 | +int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, | |
| 115 | + struct image_info * info); | |
| 116 | + | |
| 117 | +void memcpy_to_target(target_ulong dest, const void *src, | |
| 118 | + unsigned long len); | |
| 88 | 119 | void target_set_brk(target_ulong new_brk); |
| 89 | 120 | long do_brk(target_ulong new_brk); |
| 90 | 121 | void syscall_init(void); | ... | ... |