elf.c
ubixos/elf.h
ubixos/kpanic.h
lib/kmalloc.h
vmm/vmm.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
src/sys/kernel/elf.c
ubixos/types.h
sys/_types.h
vmm/paging.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
vmm/vmm.h
vfs/mount.h
ubixfs/dirCache.h
sys/sysproto.h
ubixos/elf.h
sys/signal.h
ubixos/tty.h
int int elf_loadfile (kTask_t *p, const char *file, uint32_t *addr, uint32_t *entry) elf_loadfile kTask_t * p const char * file uint32_t * addr uint32_t * entry _current elfHeader::eEntry elfHeader::eIdent elfHeader::ePhnum elfHeader::ePhoff ET_DYN ET_EXEC elfHeader::eType fclose fopen fread fseek taskStruct::id K_PANIC kfree kmalloc kprintf memset PAGE_DEFAULT PAGE_PRESENT PAGE_USER elfProgramHeader::phMemsz elfProgramHeader::phVaddr PT_LOAD vmm_remapPage vmm_setPageAttributes vmmFindFreePage x1000
char * char* elfGetPhType (int phType) elfGetPhType int phType elfPhType phTypeName
char * char* elfGetRelType (int relType) elfGetRelType int relType elfRelType relTypeName kmod_load ldEnable
char * char* elfGetShType (int shType) elfGetShType int shType elfShType shTypeName
@4 struct @4 elfPhType[] [] elfPhType elfGetPhType
@6 struct @6 elfRelType[] [] elfRelType elfGetRelType
@5 struct @5 elfShType[] [] elfShType elfGetShType
@3 struct @3 elfType[] [] elfType
char * char* elfTypeName elfTypeName
uInt32 uInt32 id id
uInt32 uInt32 id id
uInt32 uInt32 id id
uInt32 uInt32 id id
char * char* phTypeName phTypeName elfGetPhType
char * char* relTypeName relTypeName elfGetRelType
char * char* shTypeName shTypeName elfGetShType

    /*****************************************************************************************
    Copyright(c)2002-2004TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Id: elf_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    #include<ubixos/elf.h>
    #include<ubixos/kpanic.h>
    #include<lib/kmalloc.h>
    #include<vmm/vmm.h>

    conststruct{
    char*elfTypeName;
    uInt32id;
    }elfType[]={
    {"ET_NONE",0},
    {"ET_REL",1},
    {"ET_EXEC",2},
    {"ET_DYN",3},
    {"ET_CORE",4},
    {"ET_LOPROC",0xff00},
    {"ET_HIPROC",0xffff},
    };

    conststruct{
    char*phTypeName;
    uInt32id;
    }elfPhType[]={
    {"PT_NULL",0},
    {"PT_LOAD",1},
    {"PT_DYNAMIC",2},
    {"PT_INTERP",3},
    {"PT_NOTE",4},
    {"PT_SHLIB",5},
    {"PT_PHDR",6},
    {"PT_LOPROC",0x70000000},
    {"PT_HIPROC",0x7fffffff},
    };

    conststruct{
    char*shTypeName;
    uInt32id;
    }elfShType[]={
    {"SHT_NULL",0},
    {"SHT_PROGBITS",1},
    {"SHT_SYMTAB",2},
    {"SHT_STRTAB",3},
    {"SHT_RELA",4},
    {"SHT_HASH",5},
    {"SHT_DYNAMIC",6},
    {"SHT_NOTE",7},
    {"SHT_NOBITS",8},
    {"SHT_REL",9},
    {"SHT_SHLIB",10},
    {"SHT_DYNSYM",11},
    };

    conststruct{
    char*relTypeName;
    uInt32id;
    }elfRelType[]={
    {"R_386_NONE",0},
    {"R_386_32",1},
    {"R_386_PC32",2},
    {"R_386_GOT32",3},
    {"R_386_PLT32",4},
    {"R_386_COPY",5},
    {"R_386_GLOB_DAT",6},
    {"R_386_JMP_SLOT",7},
    {"R_386_RELATIVE",8},
    {"R_386_GOTOFF",9},
    {"R_386_GOTPC",10},
    };

    char*elfGetShType(intshType){
    return((char*)elfShType[shType].shTypeName);
    }

    char*elfGetPhType(intphType){
    return((char*)elfPhType[phType].phTypeName);
    }

    char*elfGetRelType(intrelType){
    return((char*)elfRelType[relType].relTypeName);
    }

    intelf_loadfile(kTask_t*p,constchar*file,uint32_t*addr,uint32_t*entry){
    inti=0x0;
    intx=0x0;
    intnumsegs=0x0;
    uint32_tbase=0x0;
    uint32_tbase_addr=0x0;
    elfHeader*binaryHeader=0x0;
    elfProgramHeader*programHeader=0x0;
    fileDescriptor*exec_fd=0x0;

    exec_fd=fopen(file,"r");
    if(exec_fd==0x0)
    return(-1);
    kprintf("MOO");
    /*LoadtheELFheader*/
    if((binaryHeader=(elfHeader*)kmalloc(sizeof(elfHeader)))==0x0)
    K_PANIC("mallocfailed!");
    fread(binaryHeader,sizeof(elfHeader),1,exec_fd);

    /*CheckIfAppIsARealApplication*/
    if((binaryHeader->eIdent[1]!='E')&&(binaryHeader->eIdent[2]!='L')&&(binaryHeader->eIdent[3]!='F')){
    kfree(binaryHeader);
    fclose(exec_fd);
    return(-1);
    }

    if(binaryHeader->eType==ET_DYN)
    base=*addr;
    elseif(binaryHeader->eType==ET_EXEC)
    base=0x0;
    else
    return(-1);

    /*LoadTheProgramHeader(s)*/
    if((programHeader=(elfProgramHeader*)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum))==0x0)
    K_PANIC("mallocfailed!");
    fseek(exec_fd,binaryHeader->ePhoff,0);
    fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,exec_fd);
    kprintf("MEW:[0x%X]",base);
    for(i=0x0;i<binaryHeader->ePhnum;i++){
    switch(programHeader[i].phType){
    casePT_LOAD:
    /*
    AllocateMemoryImGoingToHaveToMakeThisLoadMemoryWithCorrect
    Settingssoithelpsusinthefuture
    */
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    /*Makereadonlyandread/write*/
    if(vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr&0xFFFFF000)+x+base),PAGE_DEFAULT)==0x0)
    K_PANIC("Error:RemapPageFailed");
    memset((void*)((programHeader[i].phVaddr&0xFFFFF000)+x+base),0x0,0x1000);
    }

    /*NowLoadSectionToMemory*/
    fseek(exec_fd,programHeader[i].phOffset,0);
    fread((void*)programHeader[i].phVaddr,programHeader[i].phFilesz,1,exec_fd);

    if((programHeader[i].phFlags&0x2)!=0x2){
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    if((vmm_setPageAttributes((programHeader[i].phVaddr&0xFFFFF000)+x+base,PAGE_PRESENT|PAGE_USER))!=0x0)
    K_PANIC("vmm_setPageAttributesfailed");
    }
    }
    if(numsegs==0x0)
    base_addr=(programHeader[i].phVaddr&0xFFFFF000)+base;
    numsegs++;
    break;
    }
    }
    *addr=base_addr;
    kprintf("entry:[0x%X]\n",*entry);
    *entry=binaryHeader->eEntry+base;
    kprintf("entry:[0x%X]\n",*entry);
    return(0x0);
    }

    /***
    END
    ***/