CoolFace
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes838downloads
KLoad.txt252 linesDownload Raw Back to Kernel
1 2U0 LoadOneImport(U8 **_src,U8 *module_base,I64 ld_flags)3{4  U8 *src=*_src,*ptr2,*st_ptr;5  I64 i,etype;6  CHashExport *tmpex=NULL;7  CHashImport *tmpiss;8  Bool first=TRUE;9 10  while (etype=*src++) {11    i=*src(U32 *)++;12    st_ptr=src;13    src+=StrLen(st_ptr)+1;14    if (*st_ptr) {15      if (!first) {16        *_src=st_ptr-5;17        return;18      } else {19        first=FALSE;20        if (!(tmpex=HashFind(st_ptr,21              Fs->hash_table,HTG_ALL-HTT_IMPORT_SYS_SYM))) {22          if (!(ld_flags & LDF_SILENT))23            "Unresolved Reference:%s\n",st_ptr;24          tmpiss=CAlloc(sizeof(CHashImport));25          tmpiss->str=StrNew(st_ptr);26          tmpiss->type=HTT_IMPORT_SYS_SYM;27          tmpiss->module_header_entry=st_ptr-5;28          tmpiss->module_base=module_base;29          HashAdd(tmpiss,Fs->hash_table);30        }31      }32    }33    if (tmpex) {34      ptr2=module_base+i;35      if (tmpex->type & HTT_FUN)36        i=tmpex(CHashFun *)->exe_addr;37      else if (tmpex->type & HTT_GLBL_VAR)38        i=tmpex(CHashGlblVar *)->data_addr;39      else40        i=tmpex->val;41      switch (etype) {42        case IET_REL_I8:  *ptr2(U8 *) =i-ptr2-1; break;43        case IET_IMM_U8:  *ptr2(U8 *) =i;  break;44        case IET_REL_I16: *ptr2(U16 *)=i-ptr2-2; break;45        case IET_IMM_U16: *ptr2(U16 *)=i; break;46        case IET_REL_I32: *ptr2(U32 *)=i-ptr2-4; break;47        case IET_IMM_U32: *ptr2(U32 *)=i; break;48        case IET_REL_I64: *ptr2(I64 *)=i-ptr2-8; break;49        case IET_IMM_I64: *ptr2(I64 *)=i; break;50      }51    }52  }53  *_src=src-1;54}55 56U0 SysSymImportsResolve(U8 *st_ptr,I64 ld_flags)57{58  CHashImport *tmpiss;59  U8 *ptr;60  while (tmpiss=HashSingleTableFind(st_ptr,61        Fs->hash_table,HTT_IMPORT_SYS_SYM)) {62    ptr=tmpiss->module_header_entry;63    LoadOneImport(&ptr,tmpiss->module_base,ld_flags);64    tmpiss->type=HTT_INVALID;65  }66}67 68U0 LoadPass1(U8 *src,U8 *module_base,I64 ld_flags)69{70  U8 *ptr2,*ptr3,*st_ptr;71  I64 i,j,cnt,etype;72  CHashExport *tmpex=NULL;73  while (etype=*src++) {74    i=*src(U32 *)++;75    st_ptr=src;76    src+=StrLen(st_ptr)+1;77    switch (etype) {78      case IET_REL32_EXPORT:79      case IET_IMM32_EXPORT:80      case IET_REL64_EXPORT:81      case IET_IMM64_EXPORT:82        tmpex=CAlloc(sizeof(CHashExport));83        tmpex->str=StrNew(st_ptr);84        tmpex->type=HTT_EXPORT_SYS_SYM|HTF_IMM;85        if (etype==IET_IMM32_EXPORT||etype==IET_IMM64_EXPORT)86          tmpex->val=i;87        else88          tmpex->val=i+module_base;89        HashAdd(tmpex,Fs->hash_table);90        SysSymImportsResolve(st_ptr,ld_flags);91        break;92      case IET_REL_I0...IET_IMM_I64:93        src=st_ptr-5;94        LoadOneImport(&src,module_base,ld_flags);95        break;96      case IET_ABS_ADDR:97        if (ld_flags & LDF_NO_ABSS)98          src+=i*sizeof(U32);99        else {100          cnt=i;101          for (j=0;j<cnt;j++) {102            ptr2=module_base+*src(U32 *)++;103            *ptr2(U32 *)+=module_base;104          }105        }106        break;107 108      start:109        case IET_CODE_HEAP:110          ptr3=MAlloc(*src(I32 *)++,Fs->code_heap);111          break;112        case IET_ZEROED_CODE_HEAP:113          ptr3=CAlloc(*src(I32 *)++,Fs->code_heap);114          break;115      end:116        if (*st_ptr) {117          tmpex=CAlloc(sizeof(CHashExport));118          tmpex->str=StrNew(st_ptr);119          tmpex->type=HTT_EXPORT_SYS_SYM|HTF_IMM;120          tmpex->val=ptr3;121          HashAdd(tmpex,Fs->hash_table);122        }123        cnt=i;124        for (j=0;j<cnt;j++) {125          ptr2=module_base+*src(U32 *)++;126          *ptr2(I32 *)+=ptr3;127        }128        break;129 130      start:131        case IET_DATA_HEAP:132          ptr3=MAlloc(*src(I64 *)++);133          break;134        case IET_ZEROED_DATA_HEAP:135          ptr3=CAlloc(*src(I64 *)++);136          break;137      end:138        if (*st_ptr) {139          tmpex=CAlloc(sizeof(CHashExport));140          tmpex->str=StrNew(st_ptr);141          tmpex->type=HTT_EXPORT_SYS_SYM|HTF_IMM;142          tmpex->val=ptr3;143          HashAdd(tmpex,Fs->hash_table);144        }145        cnt=i;146        for (j=0;j<cnt;j++) {147          ptr2=module_base+*src(U32 *)++;148          *ptr2(I64 *)+=ptr3;149        }150        break;151    }152  }153}154 155U0 LoadPass2(U8 *src,U8 *module_base,I64)156{157  U8 *st_ptr;158  I64 i,etype;159  while (etype=*src++) {160    i=*src(U32 *)++;161    st_ptr=src;162    src+=StrLen(st_ptr)+1;163    switch (etype) {164      case IET_MAIN:165        Call(i+module_base);166        break;167      case IET_ABS_ADDR:168        src+=sizeof(U32)*i;169        break;170      case IET_CODE_HEAP:171      case IET_ZEROED_CODE_HEAP:172        src+=4+sizeof(U32)*i;173        break;174      case IET_DATA_HEAP:175      case IET_ZEROED_DATA_HEAP:176        src+=8+sizeof(U32)*i;177        break;178    }179  }180}181 182CBinFile *Load(U8 *filename,I64 ld_flags=0,CBinFile *bfh_addr=INVALID_PTR)183{//Load a .BIN file module into memory.184//bfh_addr==INVALID_PTR means don't care what load addr.185  U8 *fbuf,*module_base,*absname;186  I64 size,module_align,misalignment;187  CBinFile *bfh;188 189  fbuf=ExtDft(filename,"BIN.Z");190  if (!(bfh=FileRead(fbuf,&size))) {191    Free(fbuf);192    return NULL;193  }194 195  //See Patch Table Generation196  module_align=1<<bfh->module_align_bits;197  if (!module_align || bfh->bin_signature!=BIN_SIGNATURE_VAL) {198    Free(bfh);199    Free(fbuf);200    throw('BinModul');201  }202 203  if (bfh_addr==INVALID_PTR) {204    if (bfh->org==INVALID_PTR) {205      misalignment=module_align-sizeof(CBinFile);206      if (misalignment<0)207        misalignment&=module_align-1;208      if (Fs->code_heap!=Fs->data_heap) {209        if (module_align<16)210          module_align=16;211        bfh_addr=MAllocAligned(size,module_align,Fs->code_heap,misalignment);212      } else if (module_align>8)213        bfh_addr=MAllocAligned(size,module_align,,misalignment);214      else {//Less than 2Gig system memory215        bfh_addr=bfh;216        goto lo_skip; //File is already in code heap area, don't copy.217      }218    } else219      bfh_addr=bfh->org;220  }221  MemCpy(bfh_addr,bfh,size);222  Free(bfh);223 224  lo_skip:225  module_base=bfh_addr(U8 *)+sizeof(CBinFile);226 227  absname=FileNameAbs(fbuf);228  Free(fbuf);229  fbuf=StrNew(absname);230  FileExtRem(fbuf);231  if (fbuf[1]==':' && StrLen(fbuf)>2)232    HashGenericAdd(fbuf+2,HTT_MODULE|HTF_PUBLIC,bfh_addr);233  LoadPass1(bfh_addr(U8 *)+bfh_addr->patch_table_offset,module_base,ld_flags);234  if (!(ld_flags&LDF_JUST_LOAD))235    LoadPass2(bfh_addr(U8 *)+bfh_addr->patch_table_offset,module_base,ld_flags);236  Free(absname);237  Free(fbuf);238  return bfh_addr;239}240 241U0 LoadKernel()242{243  HashGenericAdd(KERNEL_MODULE_NAME,HTT_MODULE|HTF_PUBLIC,244        mem_boot_base-sizeof(CBinFile));245 246  //Abs patches done here CPatchTableAbsAddr.247  LoadPass1(sys_boot_patch_table_base,mem_boot_base,LDF_NO_ABSS|LDF_SILENT);248 249  //No main routines250  //  LoadPass2(sys_boot_patch_table_base,mem_boot_base,0);251}252