ld.c
ubixos/types.h
ubixos/ld.h
ubixos/sched.h
ubixos/elf.h
ubixos/kpanic.h
lib/kprintf.h
lib/kmalloc.h
vfs/vfs.h
vmm/vmm.h
string.h
assert.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/types.h
sys/_types.h
vmm/paging.h
string.h
vfs/file.h
src/sys/kernel/ld.c
sys/tss.h
lib/kmalloc.h
vmm/vmm.h
vfs/vfs.h
vfs/mount.h
lib/kprintf.h
ubixos/ld.h
assert.h
ubixfs/dirCache.h
sys/sysproto.h
ubixos/elf.h
sys/signal.h
ubixos/tty.h
uInt32 uInt32 ldEnable () ldEnable _current assert elfHeader::eEntry ELF32_R_SYM ELF32_R_TYPE elfGetRelType elfHeader::ePhnum elfHeader::ePhoff elfHeader::eShnum elfHeader::eShoff elfHeader::eShstrndx fclose fopen fread fseek taskStruct::id K_PANIC kfree kmalloc kprintf LD_START memset PAGE_DEFAULT elfProgramHeader::phMemsz elfProgramHeader::phVaddr PT_DYNAMIC PT_GNU_STACK PT_LOAD PT_PAX_FLAGS R_386_32 R_386_PC32 R_386_RELATIVE elfSectionHeader::shSize strcmp vmm_remapPage vmmFindFreePage x1000 sysExec

    /*****************************************************************************************
    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: ld_8c.xml 88 2016-01-12 00:11:29Z reddawg $

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

    #include<ubixos/types.h>
    #include<ubixos/ld.h>
    #include<ubixos/sched.h>
    #include<ubixos/elf.h>
    #include<ubixos/kpanic.h>
    #include<lib/kprintf.h>
    #include<lib/kmalloc.h>
    #include<vfs/vfs.h>
    #include<vmm/vmm.h>
    #include<string.h>
    #include<assert.h>

    uInt32ldEnable(){
    inti=0x0;
    intx=0x0;
    intrel=0x0;
    intsym=0x0;
    char*newLoc=0x0;
    char*shStr=0x0;
    char*dynStr=0x0;
    uInt32*reMap=0x0;
    fileDescriptor*ldFd=0x0;
    elfHeader*binaryHeader=0x0;
    elfProgramHeader*programHeader=0x0;
    elfSectionHeader*sectionHeader=0x0;
    elfDynSym*relSymTab=0x0;
    elfPltInfo*elfRel=0x0;

    /*Openourdynamiclinker*/
    ldFd=fopen("sys:/lib/ld.so","rb");

    if(ldFd==0x0){
    kprintf("Cannotopenld.so\n");
    }

    fseek(ldFd,0x0,0x0);
    binaryHeader=(elfHeader*)kmalloc(sizeof(elfHeader));
    assert(binaryHeader);
    fread(binaryHeader,sizeof(elfHeader),1,ldFd);

    programHeader=(elfProgramHeader*)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum);
    assert(programHeader);
    fseek(ldFd,binaryHeader->ePhoff,0);
    fread(programHeader,sizeof(elfSectionHeader),binaryHeader->ePhnum,ldFd);

    sectionHeader=(elfSectionHeader*)kmalloc(sizeof(elfSectionHeader)*binaryHeader->eShnum);
    assert(sectionHeader);
    fseek(ldFd,binaryHeader->eShoff,0);
    fread(sectionHeader,sizeof(elfSectionHeader),binaryHeader->eShnum,ldFd);

    shStr=(char*)kmalloc(sectionHeader[binaryHeader->eShstrndx].shSize);
    fseek(ldFd,sectionHeader[binaryHeader->eShstrndx].shOffset,0);
    fread(shStr,sectionHeader[binaryHeader->eShstrndx].shSize,1,ldFd);

    for(i=0x0;i<binaryHeader->ePhnum;i++){
    switch(programHeader[i].phType){
    casePT_LOAD:
    newLoc=(char*)programHeader[i].phVaddr+LD_START;
    /*
    AllocateMemoryImGoingToHaveToMakeThisLoadMemoryWithCorrect
    Settingssoithelpsusinthefuture
    */
    for(x=0;x<(programHeader[i].phMemsz);x+=0x1000){
    /*maker/worro*/
    if((vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr&0xFFFFF000)+x+LD_START),PAGE_DEFAULT))==0x0)
    K_PANIC("vmmRemapPage:ld");
    memset((void*)((programHeader[i].phVaddr&0xFFFFF000)+x+LD_START),0x0,0x1000);
    }
    /*NowLoadSectionToMemory*/
    fseek(ldFd,programHeader[i].phOffset,0x0);
    fread(newLoc,programHeader[i].phFilesz,1,ldFd);

    break;
    casePT_DYNAMIC:
    /*NowLoadSectionToMemory*/
    fseek(ldFd,programHeader[i].phOffset,0x0);
    fread(newLoc,programHeader[i].phFilesz,1,ldFd);
    break;
    casePT_GNU_STACK:
    /*Tellsusifthestackshouldbeexecutable.Failsafetoexecutable
    untilweaddchecking*/
    break;
    casePT_PAX_FLAGS:
    /*Notsure...*/
    break;
    default:
    kprintf("UnhandledHeader(kernel):%08x\n",programHeader[i].phType);
    break;
    }
    }

    for(i=0x0;i<binaryHeader->eShnum;i++){
    switch(sectionHeader[i].shType){
    case3:
    if(!strcmp((shStr+sectionHeader[i].shName),".dynstr")){
    dynStr=(char*)kmalloc(sectionHeader[i].shSize);
    fseek(ldFd,sectionHeader[i].shOffset,0x0);
    fread(dynStr,sectionHeader[i].shSize,1,ldFd);
    }
    break;
    case9:
    elfRel=(elfPltInfo*)kmalloc(sectionHeader[i].shSize);
    fseek(ldFd,sectionHeader[i].shOffset,0x0);
    fread(elfRel,sectionHeader[i].shSize,1,ldFd);

    for(x=0x0;x<sectionHeader[i].shSize/sizeof(elfPltInfo);x++){
    rel=ELF32_R_SYM(elfRel[x].pltInfo);
    reMap=(uInt32*)((uInt32)LD_START+elfRel[x].pltOffset);
    switch(ELF32_R_TYPE(elfRel[x].pltInfo)){
    caseR_386_32:
    *reMap+=((uInt32)LD_START+relSymTab[rel].dynValue);
    break;
    caseR_386_PC32:
    *reMap+=((uInt32)LD_START+relSymTab[rel].dynValue)-(uInt32)reMap;
    break;
    caseR_386_RELATIVE:
    *reMap+=(uInt32)LD_START;
    break;
    default:
    kprintf("[0x%X][0x%X](%i)[%s]\n",elfRel[x].pltOffset,elfRel[x].pltInfo,rel,elfGetRelType(ELF32_R_TYPE(elfRel[x].pltInfo)));
    kprintf("relTab[%s][0x%X][0x%X]\n",dynStr+relSymTab[rel].dynName,relSymTab[rel].dynValue,relSymTab[rel].dynName);
    break;
    }
    }
    kfree(elfRel);
    break;
    case11:
    relSymTab=(elfDynSym*)kmalloc(sectionHeader[i].shSize);
    fseek(ldFd,sectionHeader[i].shOffset,0x0);
    fread(relSymTab,sectionHeader[i].shSize,1,ldFd);
    sym=i;
    break;
    }
    }

    i=binaryHeader->eEntry+LD_START;

    kfree(dynStr);
    kfree(shStr);
    kfree(relSymTab);
    kfree(sectionHeader);
    kfree(programHeader);
    kfree(binaryHeader);
    fclose(ldFd);

    return((uInt32)i);
    }

    /***
    END
    ***/