blob: 31bcae764046042b627d05ba8a2de5592a96c932 [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
Jason W Kimf7d52782010-10-19 17:39:10 +00009FormatOutput=hex
10
Benjamin Kramera754be42010-09-09 15:00:41 +000011class Reader:
12 def __init__(self, path):
13 if path == "-":
14 # Snarf all the data so we can seek.
15 self.file = StringIO.StringIO(sys.stdin.read())
16 else:
17 self.file = open(path, "rb")
18 self.isLSB = None
19 self.is64Bit = None
20
21 def seek(self, pos):
22 self.file.seek(pos)
23
24 def read(self, N):
25 data = self.file.read(N)
26 if len(data) != N:
27 raise ValueError, "Out of data!"
28 return data
29
30 def read8(self):
31 return ord(self.read(1))
32
33 def read16(self):
34 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
35
36 def read32(self):
37 return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
38
39 def read32S(self):
40 return struct.unpack('><'[self.isLSB] + 'i', self.read(4))[0]
41
42 def read64(self):
43 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
44
45 def read64S(self):
46 return struct.unpack('><'[self.isLSB] + 'q', self.read(8))[0]
47
48 def readWord(self):
49 if self.is64Bit:
50 return self.read64()
51 else:
52 return self.read32()
53
54 def readWordS(self):
55 if self.is64Bit:
56 return self.read64S()
57 else:
58 return self.read32S()
59
60class StringTable:
61 def __init__(self, strings):
62 self.string_table = strings
63
64 def __getitem__(self, index):
65 end = self.string_table.index('\x00', index)
66 return self.string_table[index:end]
67
68class Section:
69 def __init__(self, f):
70 self.sh_name = f.read32()
71 self.sh_type = f.read32()
72 self.sh_flags = f.readWord()
73 self.sh_addr = f.readWord()
74 self.sh_offset = f.readWord()
75 self.sh_size = f.readWord()
76 self.sh_link = f.read32()
77 self.sh_info = f.read32()
78 self.sh_addralign = f.readWord()
79 self.sh_entsize = f.readWord()
80
81 def dump(self, shstrtab, f, strtab, dumpdata):
Jason W Kimf7d52782010-10-19 17:39:10 +000082 print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name]
83 print " ('sh_type', %s)" % common_dump.HexDump(self.sh_type)
84 print " ('sh_flags', %s)" % common_dump.HexDump(self.sh_flags)
85 print " ('sh_addr', %s)" % common_dump.HexDump(self.sh_addr)
86 print " ('sh_offset', %s)" % common_dump.HexDump(self.sh_offset)
87 print " ('sh_size', %s)" % common_dump.HexDump(self.sh_size)
88 print " ('sh_link', %s)" % common_dump.HexDump(self.sh_link)
89 print " ('sh_info', %s)" % common_dump.HexDump(self.sh_info)
90 print " ('sh_addralign', %s)" % common_dump.HexDump(self.sh_addralign)
91 print " ('sh_entsize', %s)" % common_dump.HexDump(self.sh_entsize)
Benjamin Kramera754be42010-09-09 15:00:41 +000092 if self.sh_type == 2: # SHT_SYMTAB
93 print " ('_symbols', ["
94 dumpSymtab(f, self, strtab)
95 print " ])"
96 elif self.sh_type == 4 or self.sh_type == 9: # SHT_RELA / SHT_REL
97 print " ('_relocations', ["
98 dumpRel(f, self, self.sh_type == 4)
99 print " ])"
100 elif dumpdata:
101 f.seek(self.sh_offset)
Rafael Espindola228290c2010-09-11 15:25:58 +0000102 data = f.read(self.sh_size)
Rafael Espindola12649372010-09-11 15:45:48 +0000103 print " ('_section_data', '%s')" % common_dump.dataToHex(data)
Benjamin Kramera754be42010-09-09 15:00:41 +0000104 print " ),"
105
106def dumpSymtab(f, section, strtab):
107 entries = section.sh_size // section.sh_entsize
108
109 for index in range(entries):
110 f.seek(section.sh_offset + index * section.sh_entsize)
Jason W Kimf7d52782010-10-19 17:39:10 +0000111 print " # Symbol %s" % common_dump.HexDump(index)
Benjamin Kramera754be42010-09-09 15:00:41 +0000112 name = f.read32()
Jason W Kimf7d52782010-10-19 17:39:10 +0000113 print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name]
Benjamin Kramera754be42010-09-09 15:00:41 +0000114 if not f.is64Bit:
Jason W Kimf7d52782010-10-19 17:39:10 +0000115 print " ('st_value', %s)" % common_dump.HexDump(f.read32())
116 print " ('st_size', %s)" % common_dump.HexDump(f.read32())
Benjamin Kramera754be42010-09-09 15:00:41 +0000117 st_info = f.read8()
Jason W Kimf7d52782010-10-19 17:39:10 +0000118 print " ('st_bind', %s)" % common_dump.HexDump((st_info >> 4))
119 print " ('st_type', %s)" % common_dump.HexDump((st_info & 0xf))
120 print " ('st_other', %s)" % common_dump.HexDump(f.read8())
121 print " ('st_shndx', %s)" % common_dump.HexDump(f.read16())
Benjamin Kramera754be42010-09-09 15:00:41 +0000122 if f.is64Bit:
Jason W Kimf7d52782010-10-19 17:39:10 +0000123 print " ('st_value', %s)" % common_dump.HexDump(f.read64())
124 print " ('st_size', %s)" % common_dump.HexDump(f.read64())
Benjamin Kramera754be42010-09-09 15:00:41 +0000125 print " ),"
126
127def dumpRel(f, section, dumprela = False):
128 entries = section.sh_size // section.sh_entsize
129
130 for index in range(entries):
131 f.seek(section.sh_offset + index * section.sh_entsize)
Jason W Kimf7d52782010-10-19 17:39:10 +0000132 print " # Relocation %s" % common_dump.HexDump(index)
133 print " (('r_offset', %s)" % common_dump.HexDump(f.readWord())
Benjamin Kramera754be42010-09-09 15:00:41 +0000134 r_info = f.readWord()
135 if f.is64Bit:
Jason W Kimf7d52782010-10-19 17:39:10 +0000136 print " ('r_sym', %s)" % common_dump.HexDump((r_info >> 32))
137 print " ('r_type', %s)" % common_dump.HexDump((r_info & 0xffffffff))
Benjamin Kramera754be42010-09-09 15:00:41 +0000138 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000139 print " ('r_sym', %s)" % common_dump.HexDump((r_info >> 8))
140 print " ('r_type', %s)" % common_dump.HexDump((r_info & 0xff))
Benjamin Kramera754be42010-09-09 15:00:41 +0000141 if dumprela:
Jason W Kimf7d52782010-10-19 17:39:10 +0000142 print " ('r_addend', %s)" % common_dump.HexDump(f.readWordS())
Benjamin Kramera754be42010-09-09 15:00:41 +0000143 print " ),"
144
145def dumpELF(path, opts):
146 f = Reader(path)
147
148 magic = f.read(4)
149 assert magic == '\x7FELF'
150
151 fileclass = f.read8()
152 if fileclass == 1: # ELFCLASS32
153 f.is64Bit = False
154 elif fileclass == 2: # ELFCLASS64
155 f.is64Bit = True
156 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000157 raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
158 print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
Benjamin Kramera754be42010-09-09 15:00:41 +0000159
160 byteordering = f.read8()
161 if byteordering == 1: # ELFDATA2LSB
162 f.isLSB = True
163 elif byteordering == 2: # ELFDATA2MSB
164 f.isLSB = False
165 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000166 raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
167 print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
Benjamin Kramera754be42010-09-09 15:00:41 +0000168
Jason W Kimf7d52782010-10-19 17:39:10 +0000169 print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8())
170 print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8())
171 print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8())
Benjamin Kramera754be42010-09-09 15:00:41 +0000172
173 f.seek(16) # Seek to end of e_ident.
174
Jason W Kimf7d52782010-10-19 17:39:10 +0000175 print "('e_type', %s)" % common_dump.HexDump(f.read16())
176 print "('e_machine', %s)" % common_dump.HexDump(f.read16())
177 print "('e_version', %s)" % common_dump.HexDump(f.read32())
178 print "('e_entry', %s)" % common_dump.HexDump(f.readWord())
179 print "('e_phoff', %s)" % common_dump.HexDump(f.readWord())
Benjamin Kramera754be42010-09-09 15:00:41 +0000180 e_shoff = f.readWord()
Jason W Kimf7d52782010-10-19 17:39:10 +0000181 print "('e_shoff', %s)" % common_dump.HexDump(e_shoff)
182 print "('e_flags', %s)" % common_dump.HexDump(f.read32())
183 print "('e_ehsize', %s)" % common_dump.HexDump(f.read16())
184 print "('e_phentsize', %s)" % common_dump.HexDump(f.read16())
185 print "('e_phnum', %s)" % common_dump.HexDump(f.read16())
Benjamin Kramera754be42010-09-09 15:00:41 +0000186 e_shentsize = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000187 print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
Benjamin Kramera754be42010-09-09 15:00:41 +0000188 e_shnum = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000189 print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
Benjamin Kramera754be42010-09-09 15:00:41 +0000190 e_shstrndx = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000191 print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
Benjamin Kramera754be42010-09-09 15:00:41 +0000192
193 # Read all section headers
194 sections = []
195 for index in range(e_shnum):
196 f.seek(e_shoff + index * e_shentsize)
197 s = Section(f)
198 sections.append(s)
199
200 # Read .shstrtab so we can resolve section names
201 f.seek(sections[e_shstrndx].sh_offset)
202 shstrtab = StringTable(f.read(sections[e_shstrndx].sh_size))
203
204 # Get the symbol string table
205 strtab = None
206 for section in sections:
207 if shstrtab[section.sh_name] == ".strtab":
208 f.seek(section.sh_offset)
209 strtab = StringTable(f.read(section.sh_size))
210 break
211
212 print "('_sections', ["
213 for index in range(e_shnum):
Jason W Kimf7d52782010-10-19 17:39:10 +0000214 print " # Section %s" % common_dump.HexDump(index)
Benjamin Kramera754be42010-09-09 15:00:41 +0000215 sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
216 print "])"
217
218if __name__ == "__main__":
219 from optparse import OptionParser, OptionGroup
220 parser = OptionParser("usage: %prog [options] {files}")
221 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
222 help="Dump the contents of sections",
223 action="store_true", default=False)
224 (opts, args) = parser.parse_args()
225
226 if not args:
227 args.append('-')
228
229 for arg in args:
230 dumpELF(arg, opts)