- Main Page
- Data Structures
- Files
- Directories

- Search for |

- File List
- Globals

# ld.c

Go to the documentation of this file.

    00001 /*****************************************************************************************
    00002  Copyright (c) 2002-2004 The UbixOS Project
    00003  All rights reserved.
    00004
    00005  Redistribution and use in source and binary forms, with or without modification, are
    00006  permitted provided that the following conditions are met:
    00007
    00008  Redistributions of source code must retain the above copyright notice, this list of
    00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
    00010  form must reproduce the above copyright notice, this list of conditions, the following
    00011  disclaimer and the list of authors in the documentation and/or other materials provided
    00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
    00013  contributors may be used to endorse or promote products derived from this software
    00014  without specific prior written permission.
    00015
    00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00025
    00026  $Id: ld_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
    00027
    00028 *****************************************************************************************/
    00029
    00030 #include <ubixos/types.h>
    00031 #include <ubixos/ld.h>
    00032 #include <ubixos/sched.h>
    00033 #include <ubixos/elf.h>
    00034 #include <ubixos/kpanic.h>
    00035 #include <lib/kprintf.h>
    00036 #include <lib/kmalloc.h>
    00037 #include <vfs/vfs.h>
    00038 #include <vmm/vmm.h>
    00039 #include <string.h>
    00040 #include <assert.h>
    00041
    00042 uInt32 ldEnable() {
    00043   int               i             = 0x0;
    00044   int               x             = 0x0;
    00045   int               rel           = 0x0;
    00046   int               sym           = 0x0;
    00047   char             *newLoc        = 0x0;
    00048   char             *shStr         = 0x0;
    00049   char             *dynStr        = 0x0;
    00050   uInt32           *reMap         = 0x0;
    00051   fileDescriptor   *ldFd          = 0x0;
    00052   elfHeader        *binaryHeader  = 0x0;
    00053   elfProgramHeader *programHeader = 0x0;
    00054   elfSectionHeader *sectionHeader = 0x0;
    00055   elfDynSym        *relSymTab     = 0x0;
    00056   elfPltInfo       *elfRel        = 0x0;
    00057
    00058   /* Open our dynamic linker */
    00059   ldFd = fopen("sys:/lib/ld.so","rb");
    00060
    00061   if (ldFd == 0x0) {
    00062     kprintf("Can not open ld.so\n");
    00063     }
    00064
    00065   fseek(ldFd,0x0,0x0);
    00066   binaryHeader = (elfHeader *)kmalloc(sizeof(elfHeader));
    00067   assert(binaryHeader);
    00068   fread(binaryHeader,sizeof(elfHeader),1,ldFd);
    00069
    00070   programHeader = (elfProgramHeader *)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum);
    00071   assert(programHeader);
    00072   fseek(ldFd,binaryHeader->ePhoff,0);
    00073   fread(programHeader,sizeof(elfSectionHeader),binaryHeader->ePhnum,ldFd);
    00074
    00075   sectionHeader = (elfSectionHeader *)kmalloc(sizeof(elfSectionHeader)*binaryHeader->eShnum);
    00076   assert(sectionHeader);
    00077   fseek(ldFd,binaryHeader->eShoff,0);
    00078   fread(sectionHeader,sizeof(elfSectionHeader),binaryHeader->eShnum,ldFd);
    00079
    00080   shStr = (char *)kmalloc(sectionHeader[binaryHeader->eShstrndx].shSize);
    00081   fseek(ldFd,sectionHeader[binaryHeader->eShstrndx].shOffset,0);
    00082   fread(shStr,sectionHeader[binaryHeader->eShstrndx].shSize,1,ldFd);
    00083
    00084   for (i = 0x0;i < binaryHeader->ePhnum;i++) {
    00085     switch (programHeader[i].phType) {
    00086       case PT_LOAD:
    00087         newLoc = (char *)programHeader[i].phVaddr + LD_START;
    00088         /*
    00089         Allocate Memory Im Going To Have To Make This Load Memory With Correct
    00090         Settings so it helps us in the future
    00091         */
    00092         for (x=0;x < (programHeader[i].phMemsz);x += 0x1000) {
    00093           /* make r/w or ro */
    00094           if ((vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + x + LD_START),PAGE_DEFAULT)) == 0x0)
    00095             K_PANIC("vmmRemapPage: ld");
    00096           memset((void *)((programHeader[i].phVaddr & 0xFFFFF000) + x + LD_START),0x0,0x1000);
    00097           }
    00098         /* Now Load Section To Memory */
    00099         fseek(ldFd,programHeader[i].phOffset,0x0);
    00100         fread(newLoc,programHeader[i].phFilesz,1,ldFd);
    00101
    00102         break;
    00103       case PT_DYNAMIC:
    00104         /* Now Load Section To Memory */
    00105         fseek(ldFd,programHeader[i].phOffset,0x0);
    00106         fread(newLoc,programHeader[i].phFilesz,1,ldFd);
    00107         break;
    00108       case PT_GNU_STACK:
    00109         /* Tells us if the stack should be executable.  Failsafe to executable
    00110            until we add checking */
    00111         break;
    00112       case PT_PAX_FLAGS:
    00113         /* Not sure... */
    00114         break;
    00115       default:
    00116         kprintf("Unhandled Header (kernel) : %08x\n", programHeader[i].phType);
    00117         break;
    00118       }
    00119     }
    00120
    00121   for (i=0x0;i<binaryHeader->eShnum;i++) {
    00122     switch (sectionHeader[i].shType) {
    00123      case 3:
    00124         if (!strcmp((shStr + sectionHeader[i].shName),".dynstr")) {
    00125           dynStr = (char *)kmalloc(sectionHeader[i].shSize);
    00126           fseek(ldFd,sectionHeader[i].shOffset,0x0);
    00127           fread(dynStr,sectionHeader[i].shSize,1,ldFd);
    00128           }
    00129         break;
    00130       case 9:
    00131         elfRel = (elfPltInfo *)kmalloc(sectionHeader[i].shSize);
    00132         fseek(ldFd,sectionHeader[i].shOffset,0x0);
    00133         fread(elfRel,sectionHeader[i].shSize,1,ldFd);
    00134
    00135         for (x=0x0;x<sectionHeader[i].shSize/sizeof(elfPltInfo);x++) {
    00136           rel = ELF32_R_SYM(elfRel[x].pltInfo);
    00137           reMap = (uInt32 *)((uInt32)LD_START + elfRel[x].pltOffset);
    00138           switch (ELF32_R_TYPE(elfRel[x].pltInfo)) {
    00139             case R_386_32:
    00140               *reMap += ((uInt32)LD_START + relSymTab[rel].dynValue);
    00141               break;
    00142             case R_386_PC32:
    00143               *reMap += ((uInt32)LD_START + relSymTab[rel].dynValue) - (uInt32)reMap;
    00144               break;
    00145             case R_386_RELATIVE:
    00146               *reMap += (uInt32)LD_START;
    00147               break;
    00148             default:
    00149               kprintf("[0x%X][0x%X](%i)[%s]\n",elfRel[x].pltOffset,elfRel[x].pltInfo,rel,elfGetRelType(ELF32_R_TYPE(elfRel[x].pltInfo)));
    00150               kprintf("relTab [%s][0x%X][0x%X]\n",dynStr + relSymTab[rel].dynName,relSymTab[rel].dynValue,relSymTab[rel].dynName);
    00151               break;
    00152             }
    00153           }
    00154         kfree(elfRel);
    00155         break;
    00156       case 11:
    00157         relSymTab = (elfDynSym *)kmalloc(sectionHeader[i].shSize);
    00158         fseek(ldFd,sectionHeader[i].shOffset,0x0);
    00159         fread(relSymTab,sectionHeader[i].shSize,1,ldFd);
    00160         sym = i;
    00161         break;
    00162       }
    00163     }
    00164
    00165   i = binaryHeader->eEntry + LD_START;
    00166
    00167   kfree(dynStr);
    00168   kfree(shStr);
    00169   kfree(relSymTab);
    00170   kfree(sectionHeader);
    00171   kfree(programHeader);
    00172   kfree(binaryHeader);
    00173   fclose(ldFd);
    00174
    00175   return((uInt32)i);
    00176   }
    00177
    00178 /***
    00179  END
    00180  ***/
    00181