private void LoadELFProgram(ByteBuffer f,SceModule module,int baseAddress,Elf32 elf,int elfOffset,boolean analyzeOnly) throws IOException {
  List<Elf32ProgramHeader> programHeaderList=elf.getProgramHeaderList();
  Memory mem=Memory.getInstance();
  int i=0;
  for (  Elf32ProgramHeader phdr : programHeaderList) {
    if (log.isTraceEnabled()) {
      log.trace(String.format("ELF Program Header: %s",phdr.toString()));
    }
    if (phdr.getP_type() == 0x00000001L) {
      int fileOffset=(int)phdr.getP_offset();
      int memOffset=baseAddress + (int)phdr.getP_vaddr();
      if (!Memory.isAddressGood(memOffset)) {
        memOffset=(int)phdr.getP_vaddr();
        if (!Memory.isAddressGood(memOffset)) {
          log.warn(String.format("Program header has invalid memory offset 0x%08X!",memOffset));
        }
      }
      int fileLen=(int)phdr.getP_filesz();
      int memLen=(int)phdr.getP_memsz();
      if (log.isDebugEnabled()) {
        log.debug(String.format("PH#%d: loading program %08X - file %08X - mem %08X",i,memOffset,memOffset + fileLen,memOffset + memLen));
        log.debug(String.format("PH#%d:\n%s",i,phdr));
      }
      f.position(elfOffset + fileOffset);
      if (f.position() + fileLen > f.limit()) {
        int newLen=f.limit() - f.position();
        log.warn(String.format("PH#%d: program overflow clamping len %08X to %08X",i,fileLen,newLen));
        fileLen=newLen;
      }
      if (!analyzeOnly) {
        if (memLen > fileLen) {
          mem.memset(memOffset + fileLen,(byte)0,memLen - fileLen);
        }
        mem.copyToMemory(memOffset,f,fileLen);
      }
      if (memOffset < module.loadAddressLow) {
        module.loadAddressLow=memOffset;
        if (log.isDebugEnabled()) {
          log.debug(String.format("PH#%d: new loadAddressLow %08X",i,module.loadAddressLow));
        }
      }
      if (memOffset + memLen > module.loadAddressHigh) {
        module.loadAddressHigh=memOffset + memLen;
        if (log.isTraceEnabled()) {
          log.trace(String.format("PH#%d: new loadAddressHigh %08X",i,module.loadAddressHigh));
        }
      }
      module.segmentaddr[module.nsegment]=memOffset;
      module.segmentsize[module.nsegment]=memLen;
      module.nsegment++;
    }
    i++;
  }
  if (log.isDebugEnabled()) {
    log.debug(String.format("PH alloc consumption %08X (mem %08X)",(module.loadAddressHigh - module.loadAddressLow),module.bss_size));
  }
}

private void LoadELFProgram ( ByteBuffer f , SceModule module , int baseAddress , Elf32 elf , int elfOffset , boolean analyzeOnly ) throws IOException { List < Elf32ProgramHeader > programHeaderList = elf . getProgramHeaderList ( ) ; Memory mem = Memory . getInstance ( ) ; int i = 0 ; for ( Elf32ProgramHeader phdr : programHeaderList ) { if ( log . isTraceEnabled ( ) ) { log . trace ( String . format ( "ELF Program Header: %s" , phdr . toString ( ) ) ) ; } if ( phdr . getP_type ( ) == 0x00000001L ) { int fileOffset = ( int ) phdr . getP_offset ( ) ; int memOffset = baseAddress + ( int ) phdr . getP_vaddr ( ) ; if ( ! Memory . isAddressGood ( memOffset ) ) { memOffset = ( int ) phdr . getP_vaddr ( ) ; if ( ! Memory . isAddressGood ( memOffset ) ) { log . warn ( String . format ( "Program header has invalid memory offset 0x%08X!" , memOffset ) ) ; } } int fileLen = ( int ) phdr . getP_filesz ( ) ; int memLen = ( int ) phdr . getP_memsz ( ) ; if ( log . isDebugEnabled ( ) ) { log . debug ( String . format ( "PH#%d: loading program %08X - file %08X - mem %08X" , i , memOffset , memOffset + fileLen , memOffset + memLen ) ) ; log . debug ( String . format ( "PH#%d:\n%s" , i , phdr ) ) ; } f . position ( elfOffset + fileOffset ) ; if ( f . position ( ) + fileLen > f . limit ( ) ) { int newLen = f . limit ( ) - f . position ( ) ; log . warn ( String . format ( "PH#%d: program overflow clamping len %08X to %08X" , i , fileLen , newLen ) ) ; fileLen = newLen ; } if ( ! analyzeOnly ) { if ( memLen > fileLen ) { mem . memset ( memOffset + fileLen , ( byte ) 0 , memLen - fileLen ) ; } mem . copyToMemory ( memOffset , f , fileLen ) ; } if ( memOffset < module . loadAddressLow ) { module . loadAddressLow = memOffset ; if ( log . isDebugEnabled ( ) ) { log . debug ( String . format ( "PH#%d: new loadAddressLow %08X" , i , module . loadAddressLow ) ) ; } } if ( memOffset + memLen > module . loadAddressHigh ) { module . loadAddressHigh = memOffset + memLen ; if ( log . isTraceEnabled ( ) ) { log . trace ( String . format ( "PH#%d: new loadAddressHigh %08X" , i , module . loadAddressHigh ) ) ; } } module . segmentaddr [ module . nsegment ] = memOffset ; module . segmentsize [ module . nsegment ] = memLen ; module . nsegment ++ ; } i ++ ; } if ( log . isDebugEnabled ( ) ) { log . debug ( String . format ( "PH alloc consumption %08X (mem %08X)" , ( module . loadAddressHigh - module . loadAddressLow ) , module . bss_size ) ) ; } }
load some programs into memory
