?? machload.c
字號:
int need_bswap = 0; int entry_point = 0; int dyld_entry_point = 0; int slide, mmapfixed; int fd; struct load_command *lcmds, *lc; int is_fat = 0; unsigned int i, magic; int mach_hdr_pos = 0; struct mach_header mach_hdr; /* for symbol lookup whith -d flag. */ struct symtab_command * symtabcmd = 0; struct nlist_extended *symtab, *sym; struct nlist *symtab_std, *syment; char *strtab; fd = open(filename, O_RDONLY); if (fd < 0) qerror("can't open file '%s'", filename); /* Read magic header. */ if (read(fd, &magic, sizeof (magic)) != sizeof (magic)) qerror("unable to read Magic of '%s'", filename); /* Check Mach identification. */ if(magic == MH_MAGIC) { is_fat = 0; need_bswap = 0; } else if (magic == MH_CIGAM) { is_fat = 0; need_bswap = 1; } else if (magic == FAT_MAGIC) { is_fat = 1; need_bswap = 0; } else if (magic == FAT_CIGAM) { is_fat = 1; need_bswap = 1; } else qerror("Not a Mach-O file.", filename); DPRINTF("loading %s %s...\n", filename, is_fat ? "[FAT]": "[REGULAR]"); if(is_fat) { int found = 0; struct fat_header fh; struct fat_arch *fa; lseek(fd, 0, SEEK_SET); /* Read Fat header. */ if (read(fd, &fh, sizeof (fh)) != sizeof (fh)) qerror("unable to read file header"); if(need_bswap) bswap_fh(&fh); /* Read Fat Arch. */ fa = malloc(sizeof(struct fat_arch)*fh.nfat_arch); if (read(fd, fa, sizeof(struct fat_arch)*fh.nfat_arch) != sizeof(struct fat_arch)*fh.nfat_arch) qerror("unable to read file header"); for( i = 0; i < fh.nfat_arch; i++, fa++) { if(need_bswap) bswap_fa(fa); if(fa->cputype == TARGET_CPU_TYPE) { mach_hdr_pos = fa->offset; lseek(fd, mach_hdr_pos, SEEK_SET); /* Read Mach header. */ if (read(fd, &mach_hdr, sizeof(struct mach_header)) != sizeof (struct mach_header)) qerror("unable to read file header"); if(mach_hdr.magic == MH_MAGIC) need_bswap = 0; else if (mach_hdr.magic == MH_CIGAM) need_bswap = 1; else qerror("Invalid mach header in Fat Mach-O File"); found = 1; break; } } if(!found) qerror("%s: No %s CPU found in FAT Header", filename, TARGET_CPU_NAME); } else { lseek(fd, 0, SEEK_SET); /* Read Mach header */ if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr)) qerror("%s: unable to read file header", filename); } if(need_bswap) bswap_mh(&mach_hdr); if ((mach_hdr.cputype) != TARGET_CPU_TYPE) qerror("%s: Unsupported CPU 0x%x (only 0x%x(%s) supported)", filename, mach_hdr.cputype, TARGET_CPU_TYPE, TARGET_CPU_NAME); switch(mach_hdr.filetype) { case MH_EXECUTE: break; case MH_FVMLIB: case MH_DYLIB: case MH_DYLINKER: break; default: qerror("%s: Unsupported Mach type (0x%x)", filename, mach_hdr.filetype); } /* read segment headers */ lcmds = malloc(mach_hdr.sizeofcmds); if(read(fd, lcmds, mach_hdr.sizeofcmds) != mach_hdr.sizeofcmds) qerror("%s: unable to read load_command", filename); slide = 0; mmapfixed = 0; for(i=0, lc = lcmds; i < (mach_hdr.ncmds) ; i++) { if(need_bswap) bswap_lc(lc); switch(lc->cmd) { case LC_SEGMENT: /* The main_exe can't be relocated */ if(mach_hdr.filetype == MH_EXECUTE) mmapfixed = 1; slide = load_segment(&mach_hdr, (struct segment_command*)lc, fd, mach_hdr_pos, need_bswap, mmapfixed, slide); /* other segment must be mapped according to slide exactly, if load_segment did something */ if(slide != -1) mmapfixed = 1; else slide = 0; /* load_segment didn't map the segment */ if(mach_hdr.filetype == MH_EXECUTE && slide != 0) qerror("%s: Warning executable can't be mapped at the right address (offset: 0x%x)\n", filename, slide); if(strcmp(((struct segment_command*)(lc))->segname, "__TEXT") == 0) { /* Text section */ if(mach_hdr.filetype == MH_EXECUTE) { /* return the mach_header */ *mh = (void*)(((struct segment_command*)(lc))->vmaddr + slide); } else { /* it is dyld save the section for gdb, we will be interested in dyld symbol while debuging */ macho_text_sect = (void*)(((struct segment_command*)(lc))->vmaddr + slide); macho_offset = slide; } } break; case LC_LOAD_DYLINKER: dyld_entry_point = load_dylinker( &mach_hdr, (struct dylinker_command*)lc, fd, mach_hdr_pos, need_bswap ); break; case LC_LOAD_DYLIB: /* dyld will do that for us */ break; case LC_THREAD: case LC_UNIXTHREAD: { struct target_pt_regs * _regs; if(mach_hdr.filetype == MH_DYLINKER) _regs = regs; else _regs = 0; entry_point = load_thread( &mach_hdr, (struct target_thread_command*)lc, _regs, fd, mach_hdr_pos, need_bswap ); } break; case LC_SYMTAB: /* Save the symtab and strtab */ symtabcmd = (struct symtab_command *)lc; break; case LC_ID_DYLINKER: case LC_ID_DYLIB: case LC_UUID: case LC_DYSYMTAB: case LC_TWOLEVEL_HINTS: case LC_PREBIND_CKSUM: case LC_SUB_LIBRARY: break; default: fprintf(stderr, "warning: unkown command 0x%x in '%s'\n", lc->cmd, filename); } lc = (struct load_command*)((int)(lc)+(lc->cmdsize)); } if(symtabcmd) { if(need_bswap) bswap_symtabcmd(symtabcmd); symtab_std = load_data(fd, symtabcmd->symoff+mach_hdr_pos, symtabcmd->nsyms * sizeof(struct nlist)); strtab = load_data(fd, symtabcmd->stroff+mach_hdr_pos, symtabcmd->strsize); symtab = malloc(sizeof(struct nlist_extended) * symtabcmd->nsyms); if(need_bswap) { for(i = 0, syment = symtab_std; i < symtabcmd->nsyms; i++, syment++) bswap_sym(syment); } for(i = 0, sym = symtab, syment = symtab_std; i < symtabcmd->nsyms; i++, sym++, syment++) { struct nlist *sym_follow, *sym_next = 0; unsigned int j; memset(sym, 0, sizeof(*sym)); sym->n_type = syment->n_type; if ( syment->n_type & N_STAB ) /* Debug symbols are skipped */ continue; memcpy(sym, syment, sizeof(*syment)); /* Find the following symbol in order to get the current symbol size */ for(j = 0, sym_follow = symtab_std; j < symtabcmd->nsyms; j++, sym_follow++) { if ( sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value)) continue; if(!sym_next) { sym_next = sym_follow; continue; } if(!(sym_next->n_value > sym_follow->n_value)) continue; sym_next = sym_follow; } if(sym_next) sym->st_size = sym_next->n_value - sym->st_value; else sym->st_size = 10; /* XXX: text_sec_hdr->size + text_sec_hdr->offset - sym->st_value; */ sym->st_value += slide; } free((void*)symtab_std); { DPRINTF("saving symtab of %s (%d symbol(s))\n", filename, symtabcmd->nsyms); struct syminfo *s; s = malloc(sizeof(*s)); s->disas_symtab = symtab; s->disas_strtab = strtab; s->disas_num_syms = symtabcmd->nsyms; s->next = syminfos; syminfos = s; } } close(fd); if(mach_hdr.filetype == MH_EXECUTE && dyld_entry_point) return dyld_entry_point; else return entry_point+slide;}extern unsigned long stack_size;unsigned long setup_arg_pages(void * mh, char ** argv, char ** env){ unsigned long stack_base, error, size; int i; int * stack; int argc, envc; /* Create enough stack to hold everything. If we don't use * it for args, we'll use it for something else... */ size = stack_size; error = target_mmap(0, size + qemu_host_page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (error == -1) qerror("stk mmap"); /* we reserve one extra page at the top of the stack as guard */ target_mprotect(error + size, qemu_host_page_size, PROT_NONE); stack_base = error + size; stack = (void*)stack_base;/* * | STRING AREA | * +-------------+ * | 0 |* +-------------+ * | apple[n] | * +-------------+ * : * +-------------+ * | apple[0] | * +-------------+ * | 0 | * +-------------+ * | env[n] | * +-------------+ * : * : * +-------------+ * | env[0] | * +-------------+ * | 0 | * +-------------+ * | arg[argc-1] | * +-------------+ * : * : * +-------------+ * | arg[0] | * +-------------+ * | argc | * +-------------+ * sp-> | mh | address of where the a.out's file offset 0 is in memory * +-------------+*/ /* Construct the stack Stack grows down */ stack--; /* XXX: string should go up there */ *stack = 0; stack--; /* Push the absolute path of our executable */ DPRINTF("pushing apple %s (0x%x)\n", (char*)argv[0], (int)argv[0]); stl(stack, (int) argv[0]); stack--; stl(stack, 0); stack--; /* Get envc */ for(envc = 0; env[envc]; envc++); for(i = envc-1; i >= 0; i--) { DPRINTF("pushing env %s (0x%x)\n", (char*)env[i], (int)env[i]); stl(stack, (int)env[i]); stack--; /* XXX: remove that when string will be on top of the stack */ page_set_flags((int)env[i], (int)(env[i]+strlen(env[i])), PROT_READ | PAGE_VALID); } /* Add on the stack the interp_prefix choosen if so */ if(interp_prefix[0]) { char *dyld_root; asprintf(&dyld_root, "DYLD_ROOT_PATH=%s", interp_prefix); page_set_flags((int)dyld_root, (int)(dyld_root+strlen(interp_prefix)+1), PROT_READ | PAGE_VALID); stl(stack, (int)dyld_root); stack--; }#ifdef DONT_USE_DYLD_SHARED_MAP { char *shared_map_mode; asprintf(&shared_map_mode, "DYLD_SHARED_REGION=avoid"); page_set_flags((int)shared_map_mode, (int)(shared_map_mode+strlen(shared_map_mode)+1), PROT_READ | PAGE_VALID); stl(stack, (int)shared_map_mode); stack--; }#endif#ifdef ACTIVATE_DYLD_TRACE char * extra_env_static[] = {"DYLD_DEBUG_TRACE=yes", "DYLD_PREBIND_DEBUG=3", "DYLD_UNKNOW_TRACE=yes", "DYLD_PRINT_INITIALIZERS=yes", "DYLD_PRINT_SEGMENTS=yes", "DYLD_PRINT_REBASINGS=yes", "DYLD_PRINT_BINDINGS=yes", "DYLD_PRINT_INITIALIZERS=yes", "DYLD_PRINT_WARNINGS=yes" }; char ** extra_env = malloc(sizeof(extra_env_static)); bcopy(extra_env_static, extra_env, sizeof(extra_env_static)); page_set_flags((int)extra_env, (int)((void*)extra_env+sizeof(extra_env_static)), PROT_READ | PAGE_VALID); for(i = 0; i<9; i++) { DPRINTF("pushing (extra) env %s (0x%x)\n", (char*)extra_env[i], (int)extra_env[i]); stl(stack, (int) extra_env[i]); stack--; }#endif stl(stack, 0); stack--; /* Get argc */ for(argc = 0; argv[argc]; argc++); for(i = argc-1; i >= 0; i--) { DPRINTF("pushing arg %s (0x%x)\n", (char*)argv[i], (int)argv[i]); stl(stack, (int) argv[i]); stack--; /* XXX: remove that when string will be on top of the stack */ page_set_flags((int)argv[i], (int)(argv[i]+strlen(argv[i])), PROT_READ | PAGE_VALID); } DPRINTF("pushing argc %d \n", argc); stl(stack, argc); stack--; DPRINTF("pushing mh 0x%x \n", (int)mh); stl(stack, (int) mh); /* Stack points on the mh */ return (unsigned long)stack;}int mach_exec(const char * filename, char ** argv, char ** envp, struct target_pt_regs * regs){ int entrypoint, stack; void * mh; /* the Mach Header that will be used by dyld */ DPRINTF("mach_exec at 0x%x\n", (int)mach_exec); entrypoint = load_object(filename, regs, &mh); stack = setup_arg_pages(mh, argv, envp);#if defined(TARGET_I386) regs->eip = entrypoint; regs->esp = stack;#elif defined(TARGET_PPC) regs->nip = entrypoint; regs->gpr[1] = stack;#endif DPRINTF("mach_exec returns eip set to 0x%x esp 0x%x mh 0x%x\n", entrypoint, stack, (int)mh); if(!entrypoint) qerror("%s: no entry point!\n", filename); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -