blob: 33581f45bc5b200762dc2e724ab834d76bbf9001 [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):
29 return ord(self.read(1))
30
31 def read16(self):
32 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
33
34 def read32(self):
35 return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
36
Benjamin Kramera754be42010-09-09 15:00:41 +000037 def read64(self):
38 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
39
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):
Jason W Kimf7d52782010-10-19 17:39:10 +000068 print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name]
69 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)
Benjamin Kramera754be42010-09-09 15:00:41 +000078 if self.sh_type == 2: # SHT_SYMTAB
79 print " ('_symbols', ["
80 dumpSymtab(f, self, strtab)
81 print " ])"
82 elif self.sh_type == 4 or self.sh_type == 9: # SHT_RELA / SHT_REL
83 print " ('_relocations', ["
84 dumpRel(f, self, self.sh_type == 4)
85 print " ])"
86 elif dumpdata:
87 f.seek(self.sh_offset)
Jason W Kim6147bda2010-12-16 00:15:10 +000088 if self.sh_type != 8: # != SHT_NOBITS
89 data = f.read(self.sh_size)
90 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):
96 entries = section.sh_size // section.sh_entsize
97
98 for index in range(entries):
99 f.seek(section.sh_offset + index * section.sh_entsize)
Rafael Espindola65ad8dc2011-08-04 14:27:46 +0000100 print " # Symbol %s" % index
Benjamin Kramera754be42010-09-09 15:00:41 +0000101 name = f.read32()
Jason W Kimf7d52782010-10-19 17:39:10 +0000102 print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name]
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())
Benjamin Kramera754be42010-09-09 15:00:41 +0000106 st_info = f.read8()
Jason W Kimf7d52782010-10-19 17:39:10 +0000107 print " ('st_bind', %s)" % common_dump.HexDump((st_info >> 4))
108 print " ('st_type', %s)" % common_dump.HexDump((st_info & 0xf))
109 print " ('st_other', %s)" % common_dump.HexDump(f.read8())
110 print " ('st_shndx', %s)" % common_dump.HexDump(f.read16())
Benjamin Kramera754be42010-09-09 15:00:41 +0000111 if f.is64Bit:
Roman Divacky1ae3c162010-12-20 20:49:43 +0000112 print " ('st_value', %s)" % common_dump.HexDump(f.read64(), 64)
113 print " ('st_size', %s)" % common_dump.HexDump(f.read64(), 64)
Benjamin Kramera754be42010-09-09 15:00:41 +0000114 print " ),"
115
116def dumpRel(f, section, dumprela = False):
117 entries = section.sh_size // section.sh_entsize
118
119 for index in range(entries):
120 f.seek(section.sh_offset + index * section.sh_entsize)
Rafael Espindolaf7179de2011-08-04 14:01:03 +0000121 print " # Relocation %s" % index
Jason W Kimf7d52782010-10-19 17:39:10 +0000122 print " (('r_offset', %s)" % common_dump.HexDump(f.readWord())
Benjamin Kramera754be42010-09-09 15:00:41 +0000123 r_info = f.readWord()
124 if f.is64Bit:
Rafael Espindolaf81f6752011-08-04 14:39:30 +0000125 r_sym = (r_info >> 32, 32)
126 r_type = (r_info & 0xffffffff, 32)
Benjamin Kramera754be42010-09-09 15:00:41 +0000127 else:
Rafael Espindolaf81f6752011-08-04 14:39:30 +0000128 r_sym = (r_info >> 8, 24)
129 r_type = (r_info & 0xff, 8)
Rafael Espindolaa83f8ef2011-08-04 14:48:27 +0000130 print " ('r_sym', %s)" % common_dump.HexDump(r_sym[0], r_sym[1])
Rafael Espindolaf81f6752011-08-04 14:39:30 +0000131 print " ('r_type', %s)" % common_dump.HexDump(r_type[0], r_type[1])
Benjamin Kramera754be42010-09-09 15:00:41 +0000132 if dumprela:
Rafael Espindolad7c9b632011-08-04 13:00:24 +0000133 val = f.readWord()
134 if f.is64Bit:
135 numBits = 64
136 else:
137 numBits = 32
138 print " ('r_addend', %s)" % common_dump.HexDump(val, numBits)
Benjamin Kramera754be42010-09-09 15:00:41 +0000139 print " ),"
140
141def dumpELF(path, opts):
142 f = Reader(path)
143
144 magic = f.read(4)
145 assert magic == '\x7FELF'
146
147 fileclass = f.read8()
148 if fileclass == 1: # ELFCLASS32
149 f.is64Bit = False
150 elif fileclass == 2: # ELFCLASS64
151 f.is64Bit = True
152 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000153 raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
154 print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
Benjamin Kramera754be42010-09-09 15:00:41 +0000155
156 byteordering = f.read8()
157 if byteordering == 1: # ELFDATA2LSB
158 f.isLSB = True
159 elif byteordering == 2: # ELFDATA2MSB
160 f.isLSB = False
161 else:
Jason W Kimf7d52782010-10-19 17:39:10 +0000162 raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
163 print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
Benjamin Kramera754be42010-09-09 15:00:41 +0000164
Jason W Kimf7d52782010-10-19 17:39:10 +0000165 print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8())
166 print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8())
167 print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8())
Benjamin Kramera754be42010-09-09 15:00:41 +0000168
169 f.seek(16) # Seek to end of e_ident.
170
Jason W Kimf7d52782010-10-19 17:39:10 +0000171 print "('e_type', %s)" % common_dump.HexDump(f.read16())
172 print "('e_machine', %s)" % common_dump.HexDump(f.read16())
173 print "('e_version', %s)" % common_dump.HexDump(f.read32())
174 print "('e_entry', %s)" % common_dump.HexDump(f.readWord())
175 print "('e_phoff', %s)" % common_dump.HexDump(f.readWord())
Benjamin Kramera754be42010-09-09 15:00:41 +0000176 e_shoff = f.readWord()
Jason W Kimf7d52782010-10-19 17:39:10 +0000177 print "('e_shoff', %s)" % common_dump.HexDump(e_shoff)
178 print "('e_flags', %s)" % common_dump.HexDump(f.read32())
179 print "('e_ehsize', %s)" % common_dump.HexDump(f.read16())
180 print "('e_phentsize', %s)" % common_dump.HexDump(f.read16())
181 print "('e_phnum', %s)" % common_dump.HexDump(f.read16())
Benjamin Kramera754be42010-09-09 15:00:41 +0000182 e_shentsize = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000183 print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
Benjamin Kramera754be42010-09-09 15:00:41 +0000184 e_shnum = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000185 print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
Benjamin Kramera754be42010-09-09 15:00:41 +0000186 e_shstrndx = f.read16()
Jason W Kimf7d52782010-10-19 17:39:10 +0000187 print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
Benjamin Kramera754be42010-09-09 15:00:41 +0000188
189 # Read all section headers
190 sections = []
191 for index in range(e_shnum):
192 f.seek(e_shoff + index * e_shentsize)
193 s = Section(f)
194 sections.append(s)
195
196 # Read .shstrtab so we can resolve section names
197 f.seek(sections[e_shstrndx].sh_offset)
198 shstrtab = StringTable(f.read(sections[e_shstrndx].sh_size))
199
200 # Get the symbol string table
201 strtab = None
202 for section in sections:
203 if shstrtab[section.sh_name] == ".strtab":
204 f.seek(section.sh_offset)
205 strtab = StringTable(f.read(section.sh_size))
206 break
207
208 print "('_sections', ["
209 for index in range(e_shnum):
Rafael Espindola014180d2011-08-04 13:39:15 +0000210 print " # Section %s" % index
Benjamin Kramera754be42010-09-09 15:00:41 +0000211 sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
212 print "])"
213
214if __name__ == "__main__":
215 from optparse import OptionParser, OptionGroup
216 parser = OptionParser("usage: %prog [options] {files}")
217 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
218 help="Dump the contents of sections",
219 action="store_true", default=False)
220 (opts, args) = parser.parse_args()
221
222 if not args:
223 args.append('-')
224
225 for arg in args:
226 dumpELF(arg, opts)