elf.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. class elf_file:
  2. class elfheader:
  3. pass
  4. def __init__(self,filename):
  5. self.filename = filename
  6. self.header = None
  7. self.mem = {}
  8. def int_from_file(self,file,length):
  9. num = 0
  10. for i in range(length):
  11. num += file.read(1)[0] * (256 ** i)
  12. return num
  13. def load_header(self,infile):
  14. self.header = elf_file.elfheader()
  15. self.header.EI_MAG = infile.read(4)
  16. if self.header.EI_MAG != b'\x7fELF':
  17. raise Exception('bad magic')
  18. self.header.EI_CLASS = self.int_from_file(infile,1)
  19. self.header.EI_DATA = self.int_from_file(infile,1)
  20. self.header.EI_VERSION = self.int_from_file(infile,1)
  21. self.header.EI_OSABI = self.int_from_file(infile,1)
  22. self.header.EI_ABIVERSION = self.int_from_file(infile,1)
  23. self.header.EI_PAD = infile.read(7)
  24. self.header.e_type = self.int_from_file(infile,2)
  25. self.header.e_machine = self.int_from_file(infile,2)
  26. self.header.e_version = self.int_from_file(infile,4)
  27. if self.header.EI_CLASS == 1:
  28. self.header.e_entry = self.int_from_file(infile,4)
  29. self.header.e_phoff = self.int_from_file(infile,4)
  30. self.header.e_shoff = self.int_from_file(infile,4)
  31. if self.header.EI_CLASS == 2:
  32. self.header.e_entry = self.int_from_file(infile,8)
  33. self.header.e_phoff = self.int_from_file(infile,8)
  34. self.header.e_shoff = self.int_from_file(infile,8)
  35. self.header.e_flags = self.int_from_file(infile,4)
  36. self.header.e_ehsize = self.int_from_file(infile,2)
  37. self.header.e_phentsize = self.int_from_file(infile,2)
  38. self.header.e_phnum = self.int_from_file(infile,2)
  39. self.header.e_shentsize = self.int_from_file(infile,2)
  40. self.header.e_shnum = self.int_from_file(infile,2)
  41. self.header.e_shstrndx = self.int_from_file(infile,2)
  42. def load_pheader(self,infile):
  43. infile.seek(self.header.e_phoff)
  44. self.pheaders = []
  45. for i in range(self.header.e_phnum):
  46. h = elf_file.elfheader()
  47. h.p_type = self.int_from_file(infile,4)
  48. if self.header.EI_CLASS == 1:
  49. h.p_offset = self.int_from_file(infile,4)
  50. h.p_vaddr = self.int_from_file(infile,4)
  51. h.p_paddr = self.int_from_file(infile,4)
  52. h.p_filesz = self.int_from_file(infile,4)
  53. h.p_memsz = self.int_from_file(infile,4)
  54. h.p_flags = self.int_from_file(infile,4)
  55. h.p_align = self.int_from_file(infile,4)
  56. if self.header.EI_CLASS == 2:
  57. h.p_flags = self.int_from_file(infile,4)
  58. h.p_offset = self.int_from_file(infile,8)
  59. h.p_vaddr = self.int_from_file(infile,8)
  60. h.p_paddr = self.int_from_file(infile,8)
  61. h.p_filesz = self.int_from_file(infile,8)
  62. h.p_memsz = self.int_from_file(infile,8)
  63. h.p_align = self.int_from_file(infile,8)
  64. self.pheaders.append(h)
  65. def load_section_headers(self,infile):
  66. infile.seek(self.header.e_shoff)
  67. self.sheaders = []
  68. for i in range(self.header.e_shnum):
  69. h = elf_file.elfheader()
  70. h.sh_name = self.int_from_file(infile,4)
  71. h.sh_type = self.int_from_file(infile,4)
  72. if self.header.EI_CLASS == 1:
  73. h.sh_flags = self.int_from_file(infile,4)
  74. h.sh_addr = self.int_from_file(infile,4)
  75. h.sh_offset = self.int_from_file(infile,4)
  76. h.sh_size = self.int_from_file(infile,4)
  77. h.sh_link = self.int_from_file(infile,4)
  78. h.sh_info = self.int_from_file(infile,4)
  79. h.sh_addralign = self.int_from_file(infile,4)
  80. h.sh_entsize = self.int_from_file(infile,4)
  81. if self.header.EI_CLASS == 2:
  82. h.sh_flags = self.int_from_file(infile,8)
  83. h.sh_addr = self.int_from_file(infile,8)
  84. h.sh_offset = self.int_from_file(infile,8)
  85. h.sh_size = self.int_from_file(infile,8)
  86. h.sh_link = self.int_from_file(infile,4)
  87. h.sh_info = self.int_from_file(infile,4)
  88. h.sh_addralign = self.int_from_file(infile,8)
  89. h.sh_entsize = self.int_from_file(infile,8)
  90. self.sheaders.append(h)
  91. def load_names(self,infile):
  92. infile.seek(self.sheaders[self.header.e_shstrndx].sh_offset)
  93. self.names = infile.read(self.sheaders[self.header.e_shstrndx].sh_size)
  94. for h in self.sheaders:
  95. h.name_text = ''
  96. i = h.sh_name
  97. while self.names[i] != 0:
  98. h.name_text += chr(self.names[i])
  99. i += 1
  100. def load(self):
  101. infile = open(self.filename,'rb')
  102. self.load_header(infile)
  103. self.load_pheader(infile)
  104. self.load_section_headers(infile)
  105. self.load_names(infile)
  106. def read_segment(self,index):
  107. infile = open(self.filename,'rb')
  108. infile.seek(self.sheaders[index].sh_offset)
  109. return infile.read(self.sheaders[index].sh_size)
  110. if __name__ == '__main__':
  111. import sys
  112. infile = sys.argv[1]
  113. e = elf_file(infile)
  114. e.load()
  115. print('elf header:')
  116. for elem in e.header.__dict__:
  117. try:
  118. print(' {} = {}'.format(elem,hex(e.header.__dict__[elem])))
  119. except:
  120. print(' {} = {}'.format(elem,e.header.__dict__[elem]))
  121. for h in e.pheaders:
  122. print('Program Header:')
  123. for elem in h.__dict__:
  124. try:
  125. print(' {} = {}'.format(elem,hex(h.__dict__[elem])))
  126. except:
  127. print(' {} = {}'.format(elem,h.__dict__[elem]))
  128. i=0
  129. for h in e.sheaders:
  130. print('Section Header {}:'.format(i))
  131. i = i + 1
  132. for elem in h.__dict__:
  133. try:
  134. print(' {} = {}'.format(elem,hex(h.__dict__[elem])))
  135. except:
  136. print(' {} = {}'.format(elem,h.__dict__[elem]))