SHARE
TWEET

Script for PS4 PSP emulator

a guest Aug 21st, 2018 188 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   import struct
  2.  import sys
  3.  
  4.  class ElfPHdr(object):
  5.      FMT = '<8I'
  6.  
  7.      def __init__(self, idx):
  8.          self.idx = idx
  9.          self.type = None
  10.          self.offset = None
  11.          self.vaddr = None
  12.          self.paddr = None
  13.          self.filesz = None
  14.          self.memsz = None
  15.          self.flags = None
  16.          self.align = None
  17.  
  18.      def load(self, f):
  19.          self.type, self.offset, self.vaddr, self.paddr, self.filesz, self.memsz, self.flags, self.align = struct.unpack(ElfPHdr.FMT, f.read(struct.calcsize(ElfPHdr.FMT)))
  20.  
  21.  class ElfSHdr(object):
  22.      FMT = '<10I'
  23.  
  24.      def __init__(self, idx):
  25.          self.idx = idx
  26.          self.name = None
  27.          self.name_end = None
  28.          self.name_len = None
  29.          self.type = None
  30.          self.flags = None
  31.          self.addr = None
  32.          self.offset = None
  33.          self.size = None
  34.          self.link = None
  35.          self.info = None
  36.          self.align = None
  37.          self.entsize = None
  38.  
  39.      def load(self, f):
  40.          self.name, self.type, self.flags, self.addr, self.offset, self.size, self.link, self.info, self.align, self.entsize = struct.unpack(ElfSHdr.FMT, f.read(struct.calcsize(ElfSHdr.FMT)))
  41.  
  42.  class ElfEHdr(object):
  43.      FMT = '<4s5B6xB'
  44.      EX_FMT = '<2HI3II6H'
  45.  
  46.      def __init__(self):
  47.          self.magic = None
  48.          self.machine_class = None
  49.          self.data_encoding = None
  50.          self.version = None
  51.          self.os_abi = None
  52.          self.abi_version = None
  53.          self.nident_size = None
  54.          self.type = None
  55.          self.machine = None
  56.          self.version = None
  57.          self.entry = None
  58.          self.phoff = None
  59.          self.shoff = None
  60.          self.flags = None
  61.          self.ehsize = None
  62.          self.phentsize = None
  63.          self.phnum = None
  64.          self.shentsize = None
  65.          self.shnum = None
  66.          self.shstridx = None
  67.  
  68.      def load(self, f):
  69.          self.magic, self.machine_class, self.data_encoding, self.version, self.os_abi, self.abi_version, self.nident_size = struct.unpack(ElfEHdr.FMT, f.read(struct.calcsize(ElfEHdr.FMT)))
  70.          self.type, self.machine, self.version, self.entry, self.phoff, self.shoff, self.flags, self.ehsize, self.phentsize, self.phnum, self.shentsize, self.shnum, self.shstridx = struct.unpack(ElfEHdr.EX_FMT, f.read(struct.calcsize(ElfEHdr.EX_FMT)))
  71.  
  72.      def has_segments(self):
  73.          return self.phentsize > 0 and self.phnum > 0
  74.  
  75.      def has_sections(self):
  76.          return self.shentsize > 0 and self.shnum > 0
  77.  
  78.  class ElfFile(object):
  79.      def __init__(self):
  80.          self.ehdr = None
  81.          self.phdrs = None
  82.          self.shdrs = None
  83.          self.file_size = None
  84.          self.segments = None
  85.          self.sections = None
  86.          self.shstrtab = None
  87.          self.shstrtab_offset = None
  88.  
  89.      def load(self, f):
  90.          start_offset = f.tell()
  91.          data = f.read()
  92.          self.file_size = len(data)
  93.          f.seek(start_offset)
  94.  
  95.          self.ehdr = ElfEHdr()
  96.          self.ehdr.load(f)
  97.  
  98.          self.phdrs = []
  99.          self.segments = []
  100.          if self.ehdr.has_segments():
  101.              for i in xrange(self.ehdr.phnum):
  102.                  #print "[*] ElfPHdr Offset: %x" % (start_offset + self.ehdr.phoff + i * self.ehdr.phentsize)
  103.                  f.seek(start_offset + self.ehdr.phoff + i * self.ehdr.phentsize)
  104.                  phdr = ElfPHdr(i)
  105.                  phdr.load(f)
  106.                  self.phdrs.append(phdr)
  107.                  if phdr.filesz > 0:
  108.                      f.seek(start_offset + phdr.offset)
  109.                      data = f.read(phdr.filesz)
  110.                  else:
  111.                      data = ''
  112.                  self.segments.append(data)
  113.  
  114.          self.shdrs = []
  115.          self.sections = []
  116.          if self.ehdr.has_sections():
  117.              for i in xrange(self.ehdr.shnum):
  118.                  #print "[*] ElfSHdr Offset: %x" % (start_offset + self.ehdr.shoff + i * self.ehdr.shentsize)
  119.                  f.seek(start_offset + self.ehdr.shoff + i * self.ehdr.shentsize)
  120.                  shdr = ElfSHdr(i)
  121.                  shdr.load(f)
  122.                  self.shdrs.append(shdr)
  123.              for i in xrange(self.ehdr.shnum):
  124.                  if self.shdrs[i].type == 3 and self.shdrs[i].size > 1:#self.shdrs[i].name_len == 9: # shstrtab
  125.                      self.shstrtab_offset = self.shdrs[i].offset
  126.                      print "[*] shstrtab found at: 0x%x" % self.shstrtab_offset
  127.                      f.seek(self.shstrtab_offset)
  128.                      self.shstrtab = f.read(self.shdrs[i].size)
  129.                      #print self.shstrtab
  130.              for i in xrange(self.ehdr.shnum):
  131.                  if i > 0:
  132.                      self.shdrs[i-1].name_end = self.shdrs[i].name - 1
  133.                      self.shdrs[i-1].name_len = self.shdrs[i-1].name_end - self.shdrs[i-1].name
  134.                        #print "%d name_end - name - name_len: %x - %x = %x" % (i,self.shdrs[i-1].name_end, self.shdrs[i-1].name, self.shdrs[i-1].name_len)
  135.                  if i == self.ehdr.shnum-1:
  136.                      self.shdrs[i].name_end = len(self.shstrtab) - 1
  137.                      self.shdrs[i].name_len = self.shdrs[i].name_end - self.shdrs[i].name
  138.                      #print "%d name_end - name - name_len: %x - %x = %x" % (i, self.shdrs[i].name_end, self.shdrs[i].name, self.shdrs[i].name_len)
  139.  
  140.  if len(sys.argv) < 2:
  141.      print "python fix_elf.py <in_elf>"
  142.      sys.exit(0)
  143.  
  144.  print "[.] Opening file %s..." % sys.argv[1]
  145.  f = open(sys.argv[1], "rb")
  146.  #f = open("ULUS10491.BIN", "rb")
  147.  #f = open("NPEZ00311.BIN", "rb")
  148.  #f = open("ULUS10567.BIN", "rb")
  149.  #f = open("NPUG80248.BIN", "rb")
  150.  #f = open("ULES00193.BIN", "rb")
  151.  
  152.  elf_file = ElfFile()
  153.  elf_file.load(f)
  154.  print "[*] magic: %s" % elf_file.ehdr.magic
  155.  print "[*] machine_class: %x" % elf_file.ehdr.machine_class
  156.  print "[*] data_encoding: %x" % elf_file.ehdr.data_encoding
  157.  print "[*] version: %x" % elf_file.ehdr.version
  158.  print "[*] os_abi: %x" % elf_file.ehdr.os_abi
  159.  print "[*] abi_version: %x" % elf_file.ehdr.abi_version
  160.  print "[*] nident_size: %x" % elf_file.ehdr.nident_size
  161.  print "[*] type: 0x%x" % elf_file.ehdr.type
  162.  print "[*] machine: %x" % elf_file.ehdr.machine
  163.  print "[*] version: %x" % elf_file.ehdr.version
  164.  print "[*] entry: 0x%x" % elf_file.ehdr.entry
  165.  print "[*] phoff: 0x%x" % elf_file.ehdr.phoff
  166.  print "[*] shoff: 0x%x" % elf_file.ehdr.shoff
  167.  print "[*] flags: 0x%x" % elf_file.ehdr.flags
  168.  print "[*] ehsize: 0x%x" % elf_file.ehdr.ehsize
  169.  print "[*] phentsize: 0x%x" % elf_file.ehdr.phentsize
  170.  print "[*] phnum: 0x%x" % elf_file.ehdr.phnum
  171.  print "[*] shentsize: 0x%x" % elf_file.ehdr.shentsize
  172.  print "[*] shnum: 0x%x" % elf_file.ehdr.shnum
  173.  print "[*] shstridx: %x" % elf_file.ehdr.shstridx
  174.  # Check first name if empty or not to determine fixing
  175.  if elf_file.shstrtab[elf_file.shdrs[1].name:elf_file.shdrs[1].name_end].replace('\x00',"") == "":
  176.      print "[!] No Section header names found!"
  177.      print "[.] attempting to identify required sections..."
  178.      PF_WRITE = 0x1
  179.      PF_READ = 0x2
  180.      PF_EXEC = 0x4
  181.      PF_READ_EXEC = PF_READ |  PF_EXEC
  182.      PF_READ_WRITE = PF_READ |  PF_WRITE
  183.      to_fix = 5
  184.      for i in xrange(elf_file.ehdr.shnum):
  185.          if (elf_file.shdrs[i].flags & PF_READ_EXEC) == PF_READ_EXEC and elf_file.shdrs[i].name_len == len(".text"):
  186.              print "[!] found .text at section %d" % i
  187.              elf_file.shstrtab = elf_file.shstrtab[:elf_file.shdrs[i].name] + ".text" + elf_file.shstrtab[elf_file.shdrs[i].name_end:]
  188.              to_fix -= 1
  189.          if elf_file.shdrs[i].type == 8 and elf_file.shdrs[i].name_len == len(".bss"):
  190.              print "[!] found .bss at section %d" % i
  191.              elf_file.shstrtab = elf_file.shstrtab[:elf_file.shdrs[i].name] + ".bss" + elf_file.shstrtab[elf_file.shdrs[i].name_end:]
  192.              to_fix -= 1
  193.          if elf_file.shdrs[i].type == 1 and elf_file.shdrs[i].flags == 3 and elf_file.shdrs[i].name_len == len(".data"):
  194.              print "[!] found .data at section %d" % i
  195.              elf_file.shstrtab = elf_file.shstrtab[:elf_file.shdrs[i].name] + ".data" + elf_file.shstrtab[elf_file.shdrs[i].name_end:]
  196.              to_fix -= 1
  197.          if elf_file.shdrs[i].type == 1 and elf_file.shdrs[i].size == 0x34 and elf_file.shdrs[i].name_len == len(".rodata.sceModuleInfo"):
  198.              print "[!] found .rodata.sceModuleInfo at section %d" % i
  199.              elf_file.shstrtab = elf_file.shstrtab[:elf_file.shdrs[i].name] + ".rodata.sceModuleInfo" + elf_file.shstrtab[elf_file.shdrs[i].name_end:]
  200.              to_fix -= 1
  201.          if elf_file.shdrs[i].type == 3 and elf_file.shdrs[i].size > 1 and elf_file.shdrs[i].name_len == len(".shstrtab"):
  202.              print "[!] found .shstrtab at section %d" % i
  203.              elf_file.shstrtab = elf_file.shstrtab[:elf_file.shdrs[i].name] + ".shstrtab" + elf_file.shstrtab[elf_file.shdrs[i].name_end:]
  204.              to_fix -= 1
  205.      if to_fix == 0:
  206.          print "[.] Writing new file..."
  207.          f.seek(0)
  208.          data = f.read()
  209.          data = data[:elf_file.shstrtab_offset] + elf_file.shstrtab + data[elf_file.shstrtab_offset+len(elf_file.shstrtab):]
  210.          open(sys.argv[1] + ".new", "wb").write(data)
  211.      else:
  212.          print "[!] Error. Could not identify required sections."
  213.  print "Section Headers:"
  214.  print " [Nr] Name Type Addr Off Size Flg Lk Inf Al"
  215.  for i in xrange(elf_file.ehdr.shnum):
  216.      print " %02d %-15.15s (%02x) %08x %08x %06x %06x %x" % (i, elf_file.shstrtab[elf_file.shdrs[i].name:elf_file.shdrs[i].name_end].replace('\x00',""),elf_file.shdrs[i].name_len, elf_file.shdrs[i].type,elf_file.shdrs[i].addr,elf_file.shdrs[i].offset,elf_file.shdrs[i].size,elf_file.shdrs[i].flags)
  217.  
  218.  f.close()
  219.  print "[.] done."
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top