00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
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   
00126   if ((binaryHeader = (elfHeader *)kmalloc(sizeof(elfHeader))) == 0x0) 
00127     K_PANIC("malloc failed!");
00128   fread(binaryHeader,sizeof(elfHeader),1,exec_fd);
00129 
00130   
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   
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 
00155 
00156 
00157         for (x = 0x0;x < (programHeader[i].phMemsz);x += 0x1000) {
00158           
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         
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 
00189 
00190