⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 machload.c.svn-base

📁 我们自己开发的一个OSEK操作系统!不知道可不可以?
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/* *  Mach-O object file loading * *  Copyright (c) 2006 Pierre d'Herbemont * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <stdio.h>#include <sys/types.h>#include <fcntl.h>#include <sys/stat.h>#include <errno.h>#include <unistd.h>#include <sys/mman.h>#include <stdlib.h>#include <string.h>#include "qemu.h"#include "disas.h"#include <mach-o/loader.h>#include <mach-o/fat.h>#include <mach-o/nlist.h>#include <mach-o/reloc.h>#include <mach-o/ppc/reloc.h>//#define DEBUG_MACHLOAD#ifdef DEBUG_MACHLOAD# define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); printf(__VA_ARGS__); } while(0)#else# define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); } while(0)#endif# define check_mach_header(x) (x.magic == MH_CIGAM)extern const char *interp_prefix;/* we don't have a good implementation for this */#define DONT_USE_DYLD_SHARED_MAP/* Pass extra arg to DYLD for debug *///#define ACTIVATE_DYLD_TRACE//#define OVERRIDE_DYLINKER#ifdef OVERRIDE_DYLINKER# ifdef TARGET_I386#  define DYLINKER_NAME "/Users/steg/qemu/tests/i386-darwin-env/usr/lib/dyld"# else#  define DYLINKER_NAME "/usr/lib/dyld"# endif#endif/* XXX: in an include */struct nlist_extended{    union {        char *n_name;        long  n_strx;    } n_un;    unsigned char n_type;    unsigned char n_sect;    short st_desc;    unsigned long st_value;    unsigned long st_size;};/* Print symbols in gdb */void *macho_text_sect = 0;int   macho_offset = 0;int load_object(const char *filename, struct target_pt_regs * regs, void ** mh);void qerror(const char *format, ...);#ifdef TARGET_I386typedef struct mach_i386_thread_state {    unsigned int    eax;    unsigned int    ebx;    unsigned int    ecx;    unsigned int    edx;    unsigned int    edi;    unsigned int    esi;    unsigned int    ebp;    unsigned int    esp;    unsigned int    ss;    unsigned int    eflags;    unsigned int    eip;    unsigned int    cs;    unsigned int    ds;    unsigned int    es;    unsigned int    fs;    unsigned int    gs;} mach_i386_thread_state_t;void bswap_i386_thread_state(struct mach_i386_thread_state *ts){    bswap32s((uint32_t*)&ts->eax);    bswap32s((uint32_t*)&ts->ebx);    bswap32s((uint32_t*)&ts->ecx);    bswap32s((uint32_t*)&ts->edx);    bswap32s((uint32_t*)&ts->edi);    bswap32s((uint32_t*)&ts->esi);    bswap32s((uint32_t*)&ts->ebp);    bswap32s((uint32_t*)&ts->esp);    bswap32s((uint32_t*)&ts->ss);    bswap32s((uint32_t*)&ts->eflags);    bswap32s((uint32_t*)&ts->eip);    bswap32s((uint32_t*)&ts->cs);    bswap32s((uint32_t*)&ts->ds);    bswap32s((uint32_t*)&ts->es);    bswap32s((uint32_t*)&ts->fs);    bswap32s((uint32_t*)&ts->gs);}#define target_thread_state mach_i386_thread_state#define TARGET_CPU_TYPE CPU_TYPE_I386#define TARGET_CPU_NAME "i386"#endif#ifdef TARGET_PPCstruct mach_ppc_thread_state {    unsigned int srr0;      /* Instruction address register (PC) */    unsigned int srr1;    /* Machine state register (supervisor) */    unsigned int r0;    unsigned int r1;    unsigned int r2;    unsigned int r3;    unsigned int r4;    unsigned int r5;    unsigned int r6;    unsigned int r7;    unsigned int r8;    unsigned int r9;    unsigned int r10;    unsigned int r11;    unsigned int r12;    unsigned int r13;    unsigned int r14;    unsigned int r15;    unsigned int r16;    unsigned int r17;    unsigned int r18;    unsigned int r19;    unsigned int r20;    unsigned int r21;    unsigned int r22;    unsigned int r23;    unsigned int r24;    unsigned int r25;    unsigned int r26;    unsigned int r27;    unsigned int r28;    unsigned int r29;    unsigned int r30;    unsigned int r31;    unsigned int cr;        /* Condition register */    unsigned int xer;    /* User's integer exception register */    unsigned int lr;    /* Link register */    unsigned int ctr;    /* Count register */    unsigned int mq;    /* MQ register (601 only) */    unsigned int vrsave;    /* Vector Save Register */};void bswap_ppc_thread_state(struct mach_ppc_thread_state *ts){    bswap32s((uint32_t*)&ts->srr0);    bswap32s((uint32_t*)&ts->srr1);    bswap32s((uint32_t*)&ts->r0);    bswap32s((uint32_t*)&ts->r1);    bswap32s((uint32_t*)&ts->r2);    bswap32s((uint32_t*)&ts->r3);    bswap32s((uint32_t*)&ts->r4);    bswap32s((uint32_t*)&ts->r5);    bswap32s((uint32_t*)&ts->r6);    bswap32s((uint32_t*)&ts->r7);    bswap32s((uint32_t*)&ts->r8);    bswap32s((uint32_t*)&ts->r9);    bswap32s((uint32_t*)&ts->r10);    bswap32s((uint32_t*)&ts->r11);    bswap32s((uint32_t*)&ts->r12);    bswap32s((uint32_t*)&ts->r13);    bswap32s((uint32_t*)&ts->r14);    bswap32s((uint32_t*)&ts->r15);    bswap32s((uint32_t*)&ts->r16);    bswap32s((uint32_t*)&ts->r17);    bswap32s((uint32_t*)&ts->r18);    bswap32s((uint32_t*)&ts->r19);    bswap32s((uint32_t*)&ts->r20);    bswap32s((uint32_t*)&ts->r21);    bswap32s((uint32_t*)&ts->r22);    bswap32s((uint32_t*)&ts->r23);    bswap32s((uint32_t*)&ts->r24);    bswap32s((uint32_t*)&ts->r25);    bswap32s((uint32_t*)&ts->r26);    bswap32s((uint32_t*)&ts->r27);    bswap32s((uint32_t*)&ts->r28);    bswap32s((uint32_t*)&ts->r29);    bswap32s((uint32_t*)&ts->r30);    bswap32s((uint32_t*)&ts->r31);    bswap32s((uint32_t*)&ts->cr);    bswap32s((uint32_t*)&ts->xer);    bswap32s((uint32_t*)&ts->lr);    bswap32s((uint32_t*)&ts->ctr);    bswap32s((uint32_t*)&ts->mq);    bswap32s((uint32_t*)&ts->vrsave);}#define target_thread_state mach_ppc_thread_state#define TARGET_CPU_TYPE CPU_TYPE_POWERPC#define TARGET_CPU_NAME "PowerPC"#endifstruct target_thread_command {    unsigned long    cmd;    /* LC_THREAD or  LC_UNIXTHREAD */    unsigned long    cmdsize;    /* total size of this command */    unsigned long flavor;    /* flavor of thread state */    unsigned long count;        /* count of longs in thread state */    struct target_thread_state state;  /* thread state for this flavor */};void bswap_tc(struct target_thread_command *tc){    bswap32s((uint32_t*)(&tc->flavor));    bswap32s((uint32_t*)&tc->count);#if defined(TARGET_I386)    bswap_i386_thread_state(&tc->state);#elif defined(TARGET_PPC)    bswap_ppc_thread_state(&tc->state);#else# error unknown TARGET_CPU_TYPE#endif}void bswap_mh(struct mach_header *mh){    bswap32s((uint32_t*)(&mh->magic));    bswap32s((uint32_t*)&mh->cputype);    bswap32s((uint32_t*)&mh->cpusubtype);    bswap32s((uint32_t*)&mh->filetype);    bswap32s((uint32_t*)&mh->ncmds);    bswap32s((uint32_t*)&mh->sizeofcmds);    bswap32s((uint32_t*)&mh->flags);}void bswap_lc(struct load_command *lc){    bswap32s((uint32_t*)&lc->cmd);    bswap32s((uint32_t*)&lc->cmdsize);}void bswap_fh(struct fat_header *fh){    bswap32s((uint32_t*)&fh->magic);    bswap32s((uint32_t*)&fh->nfat_arch);}void bswap_fa(struct fat_arch *fa){    bswap32s((uint32_t*)&fa->cputype);    bswap32s((uint32_t*)&fa->cpusubtype);    bswap32s((uint32_t*)&fa->offset);    bswap32s((uint32_t*)&fa->size);    bswap32s((uint32_t*)&fa->align);}void bswap_segcmd(struct segment_command *sc){    bswap32s((uint32_t*)&sc->vmaddr);    bswap32s((uint32_t*)&sc->vmsize);    bswap32s((uint32_t*)&sc->fileoff);    bswap32s((uint32_t*)&sc->filesize);    bswap32s((uint32_t*)&sc->maxprot);    bswap32s((uint32_t*)&sc->initprot);    bswap32s((uint32_t*)&sc->nsects);    bswap32s((uint32_t*)&sc->flags);}void bswap_symtabcmd(struct symtab_command *stc){    bswap32s((uint32_t*)&stc->cmd);    bswap32s((uint32_t*)&stc->cmdsize);    bswap32s((uint32_t*)&stc->symoff);    bswap32s((uint32_t*)&stc->nsyms);    bswap32s((uint32_t*)&stc->stroff);    bswap32s((uint32_t*)&stc->strsize);}void bswap_sym(struct nlist *n){    bswap32s((uint32_t*)&n->n_un.n_strx);    bswap16s((uint16_t*)&n->n_desc);    bswap32s((uint32_t*)&n->n_value);}int load_thread(struct mach_header *mh, struct target_thread_command *tc, struct target_pt_regs * regs, int fd, int mh_pos, int need_bswap){    int entry;    if(need_bswap)        bswap_tc(tc);#if defined(TARGET_I386)    entry = tc->state.eip;    DPRINTF(" eax 0x%.8x\n ebx 0x%.8x\n ecx 0x%.8x\n edx 0x%.8x\n edi 0x%.8x\n esi 0x%.8x\n ebp 0x%.8x\n esp 0x%.8x\n ss 0x%.8x\n eflags 0x%.8x\n eip 0x%.8x\n cs 0x%.8x\n ds 0x%.8x\n es 0x%.8x\n fs 0x%.8x\n gs 0x%.8x\n",            tc->state.eax, tc->state.ebx, tc->state.ecx, tc->state.edx, tc->state.edi, tc->state.esi, tc->state.ebp,            tc->state.esp, tc->state.ss, tc->state.eflags, tc->state.eip, tc->state.cs, tc->state.ds, tc->state.es,            tc->state.fs, tc->state.gs );#define reg_copy(reg)   regs->reg = tc->state.reg    if(regs)    {        reg_copy(eax);        reg_copy(ebx);        reg_copy(ecx);        reg_copy(edx);        reg_copy(edi);        reg_copy(esi);        reg_copy(ebp);        reg_copy(esp);        reg_copy(eflags);        reg_copy(eip);    /*        reg_copy(ss);        reg_copy(cs);        reg_copy(ds);        reg_copy(es);        reg_copy(fs);        reg_copy(gs);*/    }#undef reg_copy#elif defined(TARGET_PPC)    entry =  tc->state.srr0;#endif    DPRINTF("load_thread: entry 0x%x\n", entry);    return entry;}int load_dylinker(struct mach_header *mh, struct dylinker_command *dc, int fd, int mh_pos, int need_bswap){    int size;    char * dylinker_name;    size = dc->cmdsize - sizeof(struct dylinker_command);    if(need_bswap)        dylinker_name = (char*)(bswap_32(dc->name.offset)+(int)dc);    else        dylinker_name = (char*)((dc->name.offset)+(int)dc);#ifdef OVERRIDE_DYLINKER    dylinker_name = DYLINKER_NAME;#else    if(asprintf(&dylinker_name, "%s%s", interp_prefix, dylinker_name) == -1)        qerror("can't allocate the new dylinker name\n");#endif    DPRINTF("dylinker_name %s\n", dylinker_name);    return load_object(dylinker_name, NULL, NULL);}int load_segment(struct mach_header *mh, struct segment_command *sc, int fd, int mh_pos, int need_bswap, int fixed, int slide){    unsigned long addr = sc->vmaddr;    unsigned long size = sc->filesize;    unsigned long error = 0;    if(need_bswap)        bswap_segcmd(sc);    if(sc->vmaddr == 0)    {        DPRINTF("load_segment: sc->vmaddr == 0 returning\n");        return -1;    }    if (strcmp(sc->segname, "__PAGEZERO") == 0)    {        DPRINTF("load_segment: __PAGEZERO returning\n");        return -1;    }    /* Right now mmap memory */    /* XXX: should check to see that the space is free, because MAP_FIXED is dangerous */    DPRINTF("load_segment: mmaping %s to 0x%x-(0x%x|0x%x) + 0x%x\n", sc->segname, sc->vmaddr, sc->filesize, sc->vmsize, slide);    if(sc->filesize > 0)    {        int opt = 0;        if(fixed)            opt |= MAP_FIXED;        DPRINTF("sc->vmaddr 0x%x slide 0x%x add 0x%x\n", slide, sc->vmaddr, sc->vmaddr+slide);        addr = target_mmap(sc->vmaddr+slide, sc->filesize,  sc->initprot, opt, fd, mh_pos + sc->fileoff);        if(addr==-1)            qerror("load_segment: can't mmap at 0x%x\n", sc->vmaddr+slide);        error = addr-sc->vmaddr;    }    else    {        addr = sc->vmaddr+slide;        error = slide;    }    if(sc->vmsize > sc->filesize)    {        addr += sc->filesize;        size = sc->vmsize-sc->filesize;        addr = target_mmap(addr, size, sc->initprot, MAP_ANONYMOUS | MAP_FIXED, -1, 0);        if(addr==-1)            qerror("load_segment: can't mmap at 0x%x\n", sc->vmaddr+slide);    }    return error;}void *load_data(int fd, long offset, unsigned int size){    char *data;    data = malloc(size);    if (!data)        return NULL;    lseek(fd, offset, SEEK_SET);    if (read(fd, data, size) != size) {        free(data);        return NULL;    }    return data;}/* load a mach-o object file */int load_object(const char *filename, struct target_pt_regs * regs, void ** mh){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -