Results 1 to 1 of 1

Thread: ELF loader that doesn't load

  1. #1
    New Member 540_Slider's Avatar
    Join Date
    May 2010
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    ELF loader that doesn't load

    This ELF loader that I made can reads hackmii installer from the root of an SD card, but it can't load the sections to the appropriate places for executing

    elf.c
    Code:
    #include <stdio.h>
    #include <gccore.h>
    #include <fat.h>
    #include <sdcard/wiisd_io.h>
    #include <ogc/machine/processor.h>
    
    extern void __exception_closeall();
    typedef void (*EP) (void);
    
    typedef struct {
            u8 e_ident[16];     /* ELF Identification */
            u16 e_type;         /* object file type */
            u16 e_machine;      /* machine */
            u32 e_version;      /* object file version */
            u32 e_entry;        /* virtual entry point */
            u32 e_phoff;        /* program header table offset */
            u32 e_shoff;        /* section header table offset */
            u32 e_flags;        /* processor-specific flags */
            u16 e_ehsize;       /* ELF header size */
            u16 e_phentsize;    /* program header entry size */
            u16 e_phnum;        /* number of program header entries */
            u16 e_shentsize;    /* section header entry size */
            u16 e_shnum;        /* number of section header entries */
            u16 e_shstrndx;     /* section header table's "section
                                   header string table" entry offset */
    } Elf32_Ehdr;
    
    typedef struct {
            u32 sh_name;        /* name - index into section header
                                   string table section */
            u32 sh_type;        /* type */
            u32 sh_flags;       /* flags */
            u32 sh_addr;        /* address */
            u32 sh_offset;      /* file offset */
            u32 sh_size;        /* section size */
            u32 sh_link;        /* section header table index link */
            u32 sh_info;        /* extra information */
            u32 sh_addralign;   /* address alignment */
            u32 sh_entsize;     /* section entry size */
    } Elf32_Shdr;
    
    void *memset(void *b, int c, u32 len) {
        u32 i;
        for (i = 0; i < len; i++)
            ((u8*)b)[i] = c;
        return b;
    }
    
    void *memcpy(void *dst, const void *src, u32 len) {
        u32 i;
        for (i = 0; i < len; i++)
            ((u8*)dst)[i] = ((u8*)src)[i];
        return dst;
    }
    
    int valid_elf_image(void *addr) {
        u32 *header = addr;
        return header[0] == 0x7f454c46        // ELF
            && header[1] == 0x01020100    // 32-bit, BE, ELF v1, SVR
            && header[4] == 0x00020014    // executable, PowerPC
            && header[5] == 1        // object file v1
            && (header[10] & 0xffff) == 32;    // PHDR size
    }
    
    void *load_elf_image(void *addr) {
        Elf32_Ehdr *ehdr;
        Elf32_Shdr *shdr;
        u8 *image;
        int i;
        ehdr = (Elf32_Ehdr *) addr;
        shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
        for (i = 0; i < ehdr->e_shnum; ++i) {
            shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
                        (i * sizeof (Elf32_Shdr)));
            if (!(shdr->sh_flags & 2) ||
                shdr->sh_addr == 0 ||
                shdr->sh_size == 0)
                continue;
            shdr->sh_addr &= 0x3FFFFFFF;
            shdr->sh_addr |= 0x80000000;
            if (shdr->sh_type == 8)
                memset ((void *) shdr->sh_addr, 0, shdr->sh_size);
            else {
                image = (u8 *) addr + shdr->sh_offset;
                memcpy ((void *) shdr->sh_addr,
                    (const void *) image,
                    shdr->sh_size);
            } DCFlushRangeNoSync ((void *) shdr->sh_addr, shdr->sh_size);
        } return (void*)((ehdr->e_entry & 0x3FFFFFFF) | 0x80000000);
    }
    
    int main() {
        u32 *xfb;
        GXRModeObj *rmode;
        VIDEO_Init();
        rmode = VIDEO_GetPreferredMode(NULL);
        xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
        console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
        VIDEO_Configure(rmode);
        VIDEO_SetNextFramebuffer(xfb);
        VIDEO_SetBlack(FALSE);
        VIDEO_Flush();
        VIDEO_WaitVSync();
        if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
        printf("Another Front SD ELF Loader\n");
        u32 level;
        void *buf = (void *)0x92000000;
        u32 *tr = (u32 *)0x80001800;
        tr[0] = 0x3c209000;
        tr[1] = 0x60210020;
        tr[2] = 0x7c2903a6;
        tr[3] = 0x4e800420;
        EP ep;
        __io_wiisd.startup();
        int sd_ok = fatMountSimple("sd", &__io_wiisd);
        if (!sd_ok) {
            printf("Could not find SD Card");
            return 1;
        } FILE *f = fopen("sd:/boot.elf", "rb");
        if (!f) {
            printf("Could not find \"sd:/boot.elf\"");
            return 2;
        } printf("Found \"sd:/boot.elf\". Checking if it is an ELF...\n");
        fseek(f, 0, SEEK_END);
        int sz = ftell(f);
        fseek(f, 0, SEEK_SET);
        fread(buf, 1, sz, f);
        fclose(f);
        fatUnmount("sd");
        __io_wiisd.shutdown();
        if (!valid_elf_image(buf)) {
            printf("It isn't");
            return 3;
        } printf("It is! Loading executable sections...\n");
        ep = load_elf_image(buf);
        printf("Done. Entrypoint address: %08X. JUMP!", (u32)ep);
        SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
        __IOS_ShutdownSubsystems();
        _CPU_ISR_Disable(level);
        __exception_closeall();
        ep();
        _CPU_ISR_Restore(level);
        return 0;
    }
    Any suggestions as to how to fix the code would be greatly appreciated

    Note: would it help if I used this function from team twiizers to load the code instead?

    Spoiler



    Answering my own question: yes, with anything BUT the hackmii installer. Strange.

    Please Use The Edit Function.
    Last edited by Jax; 06-16-2012 at 05:42 AM. Reason: Merged Post.
    I am also known as Jwop.

  2. 1 User Says Thank You 540_Slider For This Useful Post


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •