blob: 58ca1773280e9b9eeeb81c9384071a8df62b9f29 [file] [log] [blame]
Benjamin Kramera754be42010-09-09 15:00:41 +00001#!/usr/bin/env python
2
3import struct
4import sys
5import StringIO
6
Rafael Espindola228290c2010-09-11 15:25:58 +00007import common_dump
8
Benjamin Kramera754be42010-09-09 15:00:41 +00009class Reader:
10 def __init__(self, path):
11 if path == "-":
12 # Snarf all the data so we can seek.
13 self.file = StringIO.StringIO(sys.stdin.read())
14 else:
15 self.file = open(path, "rb")
16 self.isLSB = None
17 self.is64Bit = None
18
19 def seek(self, pos):
20 self.file.seek(pos)
21
22 def read(self, N):
23 data = self.file.read(N)
24 if len(data) != N:
25 raise ValueError, "Out of data!"
26 return data
27
28 def read8(self):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000029 return (ord(self.read(1)), 8)
Benjamin Kramera754be42010-09-09 15:00:41 +000030
31 def read16(self):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000032 return (struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0], 16)
Benjamin Kramera754be42010-09-09 15:00:41 +000033
34 def read32(self):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000035 return (struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0], 32)
Benjamin Kramera754be42010-09-09 15:00:41 +000036
Benjamin Kramera754be42010-09-09 15:00:41 +000037 def read64(self):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000038 return (struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0], 64)
Benjamin Kramera754be42010-09-09 15:00:41 +000039
Benjamin Kramera754be42010-09-09 15:00:41 +000040 def readWord(self):
41 if self.is64Bit:
42 return self.read64()
43 else:
44 return self.read32()
45
Benjamin Kramera754be42010-09-09 15:00:41 +000046class StringTable:
47 def __init__(self, strings):
48 self.string_table = strings
49
50 def __getitem__(self, index):
51 end = self.string_table.index('\x00', index)
52 return self.string_table[index:end]
53
54class Section:
55 def __init__(self, f):
56 self.sh_name = f.read32()
57 self.sh_type = f.read32()
58 self.sh_flags = f.readWord()
59 self.sh_addr = f.readWord()
60 self.sh_offset = f.readWord()
61 self.sh_size = f.readWord()
62 self.sh_link = f.read32()
63 self.sh_info = f.read32()
64 self.sh_addralign = f.readWord()
65 self.sh_entsize = f.readWord()
66
67 def dump(self, shstrtab, f, strtab, dumpdata):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000068 print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name[0]]
Jason W Kimf7d52782010-10-19 17:39:10 +000069 print " ('sh_type', %s)" % common_dump.HexDump(self.sh_type)
70 print " ('sh_flags', %s)" % common_dump.HexDump(self.sh_flags)
71 print " ('sh_addr', %s)" % common_dump.HexDump(self.sh_addr)
72 print " ('sh_offset', %s)" % common_dump.HexDump(self.sh_offset)
73 print " ('sh_size', %s)" % common_dump.HexDump(self.sh_size)
74 print " ('sh_link', %s)" % common_dump.HexDump(self.sh_link)
75 print " ('sh_info', %s)" % common_dump.HexDump(self.sh_info)
76 print " ('sh_addralign', %s)" % common_dump.HexDump(self.sh_addralign)
77 print " ('sh_entsize', %s)" % common_dump.HexDump(self.sh_entsize)
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000078 if self.sh_type[0] == 2: # SHT_SYMTAB
Benjamin Kramera754be42010-09-09 15:00:41 +000079 print " ('_symbols', ["
80 dumpSymtab(f, self, strtab)
81 print " ])"
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000082 elif self.sh_type[0] == 4 or self.sh_type[0] == 9: # SHT_RELA / SHT_REL
Benjamin Kramera754be42010-09-09 15:00:41 +000083 print " ('_relocations', ["
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000084 dumpRel(f, self, self.sh_type[0] == 4)
Benjamin Kramera754be42010-09-09 15:00:41 +000085 print " ])"
86 elif dumpdata:
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000087 f.seek(self.sh_offset[0])
Jason W Kim6147bda2010-12-16 00:15:10 +000088 if self.sh_type != 8: # != SHT_NOBITS
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000089 data = f.read(self.sh_size[0])
Jason W Kim6147bda2010-12-16 00:15:10 +000090 print " ('_section_data', '%s')" % common_dump.dataToHex(data)
91 else:
92 print " ('_section_data', '')"
Benjamin Kramera754be42010-09-09 15:00:41 +000093 print " ),"
94
95def dumpSymtab(f, section, strtab):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000096 entries = section.sh_size[0] // section.sh_entsize[0]
Benjamin Kramera754be42010-09-09 15:00:41 +000097
98 for index in range(entries):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +000099 f.seek(section.sh_offset[0] + index * section.sh_entsize[0])
Rafael Espindola65ad8dc2011-08-04 14:27:46 +0000100 print " # Symbol %s" % index
Benjamin Kramera754be42010-09-09 15:00:41 +0000101 name = f.read32()
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000102 print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name[0]]
Benjamin Kramera754be42010-09-09 15:00:41 +0000103 if not f.is64Bit:
Jason W Kimf7d52782010-10-19 17:39:10 +0000104 print " ('st_value', %s)" % common_dump.HexDump(f.read32())
105 print " ('st_size', %s)" % common_dump.HexDump(f.read32())
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000106 st_info = f.read8()[0]
Rafael Espindolad7c27832011-08-04 15:10:35 +0000107 st_bind = (st_info >> 4, 4)
108 st_type = (st_info & 0xf, 4)
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000109 print " ('st_bind', %s)" % common_dump.HexDump(st_bind)
110 print " ('st_type', %s)" % common_dump.HexDump(st_type)
111 print " ('st_other', %s)" % common_dump.HexDump(f.read8())
112 print " ('st_shndx', %s)" % common_dump.HexDump(f.read16())
Benjamin Kramera754be42010-09-09 15:00:41 +0000113 if f.is64Bit:
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000114 print " ('st_value', %s)" % common_dump.HexDump(f.read64())
115 print " ('st_size', %s)" % common_dump.HexDump(f.read64())
Benjamin Kramera754be42010-09-09 15:00:41 +0000116 print " ),"
117
118def dumpRel(f, section, dumprela = False):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000119 entries = section.sh_size[0] // section.sh_entsize[0]
Benjamin Kramera754be42010-09-09 15:00:41 +0000120
121 for index in range(entries):
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000122 f.seek(section.sh_offset[0] + index * section.sh_entsize[0])
Rafael Espindolaf7179de2011-08-04 14:01:03 +0000123 print " # Relocation %s" % index
Jason W Kimf7d52782010-10-19 17:39:10 +0000124 print " (('r_offset', %s)" % common_dump.HexDump(f.readWord())
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000125 r_info = f.readWord()[0]
Benjamin Kramera754be42010-09-09 15:00:41 +0000126 if f.is64Bit:
Rafael Espindolaf81f6752011-08-04 14:39:30 +0000127 r_sym = (r_info >> 32, 32)
128 r_type = (r_info & 0xffffffff, 32)
Benjamin Kramera754be42010-09-09 15:00:41 +0000129 else:
Rafael Espindolaf81f6752011-08-04 14:39:30 +0000130 r_sym = (r_info >> 8, 24)
131 r_type = (r_info & 0xff, 8)
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000132 print " ('r_sym', %s)" % common_dump.HexDump(r_sym)
133 print " ('r_type', %s)" % common_dump.HexDump(r_type)
Benjamin Kramera754be42010-09-09 15:00:41 +0000134 if dumprela:
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000135 print " ('r_addend', %s)" % common_dump.HexDump(f.readWord())
Benjamin Kramera754be42010-09-09 15:00:41 +0000136 print " ),"
137
138def dumpELF(path, opts):
139 f = Reader(path)
140
141 magic = f.read(4)
142 assert magic == '\x7FELF'
143
144 fileclass = f.read8()
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000145 if fileclass[0] == 1: # ELFCLASS32
Benjamin Kramera754be42010-09-09 15:00:41 +0000146 f.is64Bit = False
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000147 elif fileclass[0] == 2: # ELFCLASS64
Benjamin Kramera754be42010-09-09 15:00:41 +0000148 f.is64Bit = True
149 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000150 raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
151 print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
Benjamin Kramera754be42010-09-09 15:00:41 +0000152
153 byteordering = f.read8()
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000154 if byteordering[0] == 1: # ELFDATA2LSB
Benjamin Kramera754be42010-09-09 15:00:41 +0000155 f.isLSB = True
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000156 elif byteordering[0] == 2: # ELFDATA2MSB
Benjamin Kramera754be42010-09-09 15:00:41 +0000157 f.isLSB = False
158 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000159 raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
160 print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
Benjamin Kramera754be42010-09-09 15:00:41 +0000161
Jason W Kimf7d52782010-10-19 17:39:10 +0000162 print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8())
163 print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8())
164 print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8())
Benjamin Kramera754be42010-09-09 15:00:41 +0000165
166 f.seek(16) # Seek to end of e_ident.
167
Jason W Kimf7d52782010-10-19 17:39:10 +0000168 print "('e_type', %s)" % common_dump.HexDump(f.read16())
169 print "('e_machine', %s)" % common_dump.HexDump(f.read16())
170 print "('e_version', %s)" % common_dump.HexDump(f.read32())
171 print "('e_entry', %s)" % common_dump.HexDump(f.readWord())
172 print "('e_phoff', %s)" % common_dump.HexDump(f.readWord())
Benjamin Kramera754be42010-09-09 15:00:41 +0000173 e_shoff = f.readWord()
Jason W Kimf7d52782010-10-19 17:39:10 +0000174 print "('e_shoff', %s)" % common_dump.HexDump(e_shoff)
175 print "('e_flags', %s)" % common_dump.HexDump(f.read32())
176 print "('e_ehsize', %s)" % common_dump.HexDump(f.read16())
177 print "('e_phentsize', %s)" % common_dump.HexDump(f.read16())
178 print "('e_phnum', %s)" % common_dump.HexDump(f.read16())
Benjamin Kramera754be42010-09-09 15:00:41 +0000179 e_shentsize = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000180 print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
Benjamin Kramera754be42010-09-09 15:00:41 +0000181 e_shnum = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000182 print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
Benjamin Kramera754be42010-09-09 15:00:41 +0000183 e_shstrndx = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000184 print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
Benjamin Kramera754be42010-09-09 15:00:41 +0000185
186 # Read all section headers
187 sections = []
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000188 for index in range(e_shnum[0]):
189 f.seek(e_shoff[0] + index * e_shentsize[0])
Benjamin Kramera754be42010-09-09 15:00:41 +0000190 s = Section(f)
191 sections.append(s)
192
193 # Read .shstrtab so we can resolve section names
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000194 f.seek(sections[e_shstrndx[0]].sh_offset[0])
195 shstrtab = StringTable(f.read(sections[e_shstrndx[0]].sh_size[0]))
Benjamin Kramera754be42010-09-09 15:00:41 +0000196
197 # Get the symbol string table
198 strtab = None
199 for section in sections:
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000200 if shstrtab[section.sh_name[0]] == ".strtab":
201 f.seek(section.sh_offset[0])
202 strtab = StringTable(f.read(section.sh_size[0]))
Benjamin Kramera754be42010-09-09 15:00:41 +0000203 break
204
205 print "('_sections', ["
Rafael Espindola5c4e52e2011-08-04 17:00:11 +0000206 for index in range(e_shnum[0]):
Rafael Espindola014180d2011-08-04 13:39:15 +0000207 print " # Section %s" % index
Benjamin Kramera754be42010-09-09 15:00:41 +0000208 sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
209 print "])"
210
211if __name__ == "__main__":
212 from optparse import OptionParser, OptionGroup
213 parser = OptionParser("usage: %prog [options] {files}")
214 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
215 help="Dump the contents of sections",
216 action="store_true", default=False)
217 (opts, args) = parser.parse_args()
218
219 if not args:
220 args.append('-')
221
222 for arg in args:
223 dumpELF(arg, opts)