elf.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: elf_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
00027 
00028 *****************************************************************************************/
00029 
00030 #include <ubixos/elf.h>
00031 #include <ubixos/kpanic.h>
00032 #include <lib/kmalloc.h>
00033 #include <vmm/vmm.h>
00034 
00035 const struct {
00036   char  *elfTypeName;
00037   uInt32 id;
00038   } elfType[] = {
00039     { "ET_NONE",         0 },
00040     { "ET_REL",          1 },
00041     { "ET_EXEC",         2 },
00042     { "ET_DYN",          3 },
00043     { "ET_CORE",         4 },
00044     { "ET_LOPROC",  0xff00 },
00045     { "ET_HIPROC",  0xffff },
00046     };
00047 
00048 const struct {
00049   char   *phTypeName;
00050   uInt32 id;
00051   } elfPhType[] = {
00052     { "PT_NULL",              0 },
00053     { "PT_LOAD",              1 },
00054     { "PT_DYNAMIC",           2 },
00055     { "PT_INTERP",            3 },
00056     { "PT_NOTE",              4 },
00057     { "PT_SHLIB",             5 },
00058     { "PT_PHDR",              6 },
00059     { "PT_LOPROC",   0x70000000 },
00060     { "PT_HIPROC",   0x7fffffff },
00061     };
00062 
00063 const struct {
00064   char   *shTypeName;
00065   uInt32 id;
00066   } elfShType[] = {
00067     {"SHT_NULL",     0 },
00068     {"SHT_PROGBITS", 1 },
00069     {"SHT_SYMTAB",   2 },
00070     {"SHT_STRTAB",   3 },
00071     {"SHT_RELA",     4 },
00072     {"SHT_HASH",     5 },
00073     {"SHT_DYNAMIC",  6 },
00074     {"SHT_NOTE",     7 },
00075     {"SHT_NOBITS",   8 },
00076     {"SHT_REL",      9 },
00077     {"SHT_SHLIB",   10 },
00078     {"SHT_DYNSYM",  11 },
00079     };
00080 
00081 const struct {
00082   char *relTypeName;
00083   uInt32 id;
00084   } elfRelType[] = {
00085     {"R_386_NONE",        0 },
00086     {"R_386_32",          1 },
00087     {"R_386_PC32",        2 },
00088     {"R_386_GOT32",       3 },
00089     {"R_386_PLT32",       4 },
00090     {"R_386_COPY",        5 },
00091     {"R_386_GLOB_DAT",    6 },
00092     {"R_386_JMP_SLOT",    7 },
00093     {"R_386_RELATIVE",    8 },
00094     {"R_386_GOTOFF",      9 },
00095     {"R_386_GOTPC",      10 },
00096     };
00097 
00098 
00099 char *elfGetShType(int shType) {
00100   return((char *)elfShType[shType].shTypeName);
00101   }
00102 
00103 char *elfGetPhType(int phType) {
00104   return((char *)elfPhType[phType].phTypeName);
00105   }
00106 
00107 char *elfGetRelType(int relType) {
00108   return((char *)elfRelType[relType].relTypeName);
00109   }
00110 
00111 int elf_loadfile(kTask_t *p,const char *file,u_int32_t *addr,u_int32_t *entry) {
00112   int                i             = 0x0;
00113   int                x             = 0x0;
00114   int                numsegs       = 0x0;
00115   u_int32_t          base          = 0x0;
00116   u_int32_t          base_addr     = 0x0;
00117   elfHeader         *binaryHeader  = 0x0;
00118   elfProgramHeader  *programHeader = 0x0;
00119   fileDescriptor    *exec_fd       = 0x0;
00120 
00121   exec_fd = fopen(file,"r");
00122   if (exec_fd == 0x0)
00123     return(-1);
00124 kprintf("MOO");
00125   /* Load the ELF header */
00126   if ((binaryHeader = (elfHeader *)kmalloc(sizeof(elfHeader))) == 0x0) 
00127     K_PANIC("malloc failed!");
00128   fread(binaryHeader,sizeof(elfHeader),1,exec_fd);
00129 
00130   /* Check If App Is A Real Application */
00131   if ((binaryHeader->eIdent[1] != 'E') && (binaryHeader->eIdent[2] != 'L') && (binaryHeader->eIdent[3] != 'F')) {
00132     kfree(binaryHeader);
00133     fclose(exec_fd);
00134     return(-1);
00135     }
00136 
00137   if (binaryHeader->eType == ET_DYN)
00138     base = *addr;
00139   else if (binaryHeader->eType == ET_EXEC)
00140     base = 0x0;
00141   else
00142     return(-1);
00143 
00144   /* Load The Program Header(s) */
00145   if ((programHeader = (elfProgramHeader *)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum)) == 0x0)
00146     K_PANIC("malloc failed!");
00147   fseek(exec_fd,binaryHeader->ePhoff,0);
00148   fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,exec_fd);
00149 kprintf("MEW: [0x%X]",base);
00150   for (i = 0x0;i < binaryHeader->ePhnum;i++) {
00151     switch (programHeader[i].phType) {
00152       case PT_LOAD:
00153         /*
00154         Allocate Memory Im Going To Have To Make This Load Memory With Correct
00155         Settings so it helps us in the future
00156         */
00157         for (x = 0x0;x < (programHeader[i].phMemsz);x += 0x1000) {
00158           /* Make readonly and read/write */
00159           if (vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + x + base),PAGE_DEFAULT) == 0x0)
00160             K_PANIC("Error: Remap Page Failed");
00161           memset((void *)((programHeader[i].phVaddr & 0xFFFFF000) + x + base),0x0,0x1000);
00162           }
00163 
00164         /* Now Load Section To Memory */
00165         fseek(exec_fd,programHeader[i].phOffset,0);
00166         fread((void *)programHeader[i].phVaddr,programHeader[i].phFilesz,1,exec_fd);
00167 
00168         if ((programHeader[i].phFlags & 0x2) != 0x2) {
00169           for (x = 0x0;x < (programHeader[i].phMemsz);x += 0x1000) {
00170             if ((vmm_setPageAttributes((programHeader[i].phVaddr & 0xFFFFF000) + x + base,PAGE_PRESENT | PAGE_USER)) != 0x0)
00171               K_PANIC("vmm_setPageAttributes failed");
00172             }
00173           }
00174         if (numsegs == 0x0)
00175           base_addr = (programHeader[i].phVaddr & 0xFFFFF000) + base;
00176         numsegs++;
00177         break;
00178       }
00179     }
00180   *addr  = base_addr;
00181   kprintf("entry: [0x%X]\n",*entry);
00182   *entry = binaryHeader->eEntry + base;
00183   kprintf("entry: [0x%X]\n",*entry);
00184   return(0x0);
00185   }
00186 
00187 /***
00188  END
00189  ***/
00190 

Generated on Fri Dec 15 11:18:55 2006 for UbixOS V2 by  doxygen 1.4.7