Fixing r116753 r116756 r116777
[oota-llvm.git] / test / Scripts / elf-dump
1 #!/usr/bin/env python
2
3 import struct
4 import sys
5 import StringIO
6
7 import common_dump
8
9 FormatOutput=hex
10
11 class 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
60 class 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
68 class 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):
82         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)
92         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)
102             data = f.read(self.sh_size)
103             print "   ('_section_data', '%s')" % common_dump.dataToHex(data)
104         print "  ),"
105
106 def 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)
111         print "    # Symbol %s" % common_dump.HexDump(index)
112         name = f.read32()
113         print "    (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name]
114         if not f.is64Bit:
115             print "     ('st_value', %s)" % common_dump.HexDump(f.read32())
116             print "     ('st_size', %s)" % common_dump.HexDump(f.read32())
117         st_info = f.read8()
118         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())
122         if f.is64Bit:
123             print "     ('st_value', %s)" % common_dump.HexDump(f.read64())
124             print "     ('st_size', %s)" % common_dump.HexDump(f.read64())
125         print "    ),"
126
127 def 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)
132         print "    # Relocation %s" % common_dump.HexDump(index)
133         print "    (('r_offset', %s)" % common_dump.HexDump(f.readWord())
134         r_info = f.readWord()
135         if f.is64Bit:
136             print "     ('r_sym', %s)" % common_dump.HexDump((r_info >> 32))
137             print "     ('r_type', %s)" % common_dump.HexDump((r_info & 0xffffffff))
138         else:
139             print "     ('r_sym', %s)" % common_dump.HexDump((r_info >> 8))
140             print "     ('r_type', %s)" % common_dump.HexDump((r_info & 0xff))
141         if dumprela:
142             print "     ('r_addend', %s)" % common_dump.HexDump(f.readWordS())
143         print "    ),"
144
145 def 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:
157         raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
158     print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
159
160     byteordering = f.read8()
161     if byteordering == 1: # ELFDATA2LSB
162         f.isLSB = True
163     elif byteordering == 2: # ELFDATA2MSB
164         f.isLSB = False
165     else:
166         raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
167     print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
168
169     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())
172
173     f.seek(16) # Seek to end of e_ident.
174
175     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())
180     e_shoff = f.readWord()
181     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())
186     e_shentsize = f.read16()
187     print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
188     e_shnum = f.read16()
189     print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
190     e_shstrndx = f.read16()
191     print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
192
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):
214         print "  # Section %s" % common_dump.HexDump(index)
215         sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
216     print "])"
217
218 if __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)