blob: 8a76c6f902cd6145235516a20a36be392be3eecf [file] [log] [blame]
Eli Bendersky3edefab2011-09-16 14:52:54 +03001#!/usr/bin/env python
Eli Bendersky933f6992011-09-09 08:11:06 +03002#-------------------------------------------------------------------------------
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +03003# scripts/readelf.py
Eli Bendersky933f6992011-09-09 08:11:06 +03004#
5# A clone of 'readelf' in Python, based on the pyelftools library
6#
7# Eli Bendersky (eliben@gmail.com)
8# This code is in the public domain
9#-------------------------------------------------------------------------------
Eli Bendersky53e86db2011-09-18 06:04:30 +030010import os, sys
Eli Bendersky933f6992011-09-09 08:11:06 +030011from optparse import OptionParser
Eli Bendersky53e86db2011-09-18 06:04:30 +030012import string
13
Eli Bendersky933f6992011-09-09 08:11:06 +030014
15# If elftools is not installed, maybe we're running from the root or scripts
16# dir of the source distribution
17#
18try:
19 import elftools
20except ImportError:
21 sys.path.extend(['.', '..'])
22
Eli Bendersky0b27ba42011-09-17 06:44:02 +030023from elftools import __version__
Eli Bendersky933f6992011-09-09 08:11:06 +030024from elftools.common.exceptions import ELFError
25from elftools.elf.elffile import ELFFile
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030026from elftools.elf.segments import InterpSegment
Eli Bendersky149315a2011-11-24 08:12:37 +020027from elftools.elf.sections import SymbolTableSection
28from elftools.elf.relocation import RelocationSection
Eli Bendersky933f6992011-09-09 08:11:06 +030029from elftools.elf.descriptions import (
30 describe_ei_class, describe_ei_data, describe_ei_version,
Eli Benderskyde8d71e2011-09-09 08:22:35 +030031 describe_ei_osabi, describe_e_type, describe_e_machine,
Eli Bendersky26de2ac2011-09-13 06:50:28 +030032 describe_e_version_numeric, describe_p_type, describe_p_flags,
Eli Bendersky377bd862011-09-16 11:10:44 +030033 describe_sh_type, describe_sh_flags,
Eli Bendersky3edefab2011-09-16 14:52:54 +030034 describe_symbol_type, describe_symbol_bind, describe_symbol_visibility,
Eli Bendersky89a824f2011-09-23 10:59:59 +030035 describe_symbol_shndx, describe_reloc_type,
Eli Bendersky933f6992011-09-09 08:11:06 +030036 )
Eli Bendersky149315a2011-11-24 08:12:37 +020037from elftools.dwarf.dwarfinfo import DWARFInfo
elibenaee11d22011-10-26 10:42:34 +020038from elftools.dwarf.descriptions import describe_attr_value
Eli Bendersky933f6992011-09-09 08:11:06 +030039
40
41class ReadElf(object):
42 """ display_* methods are used to emit output into the output stream
43 """
44 def __init__(self, file, output):
45 """ file:
46 stream object with the ELF file to read
47
48 output:
49 output stream to write to
50 """
51 self.elffile = ELFFile(file)
52 self.output = output
eliben0fc47af2011-10-02 13:47:12 +020053
54 # Lazily initialized if a debug dump is requested
55 self._dwarfinfo = None
Eli Bendersky933f6992011-09-09 08:11:06 +030056
57 def display_file_header(self):
58 """ Display the ELF file header
59 """
60 self._emitline('ELF Header:')
61 self._emit(' Magic: ')
62 self._emitline(' '.join('%2.2x' % ord(b)
63 for b in self.elffile.e_ident_raw))
64 header = self.elffile.header
65 e_ident = header['e_ident']
66 self._emitline(' Class: %s' %
67 describe_ei_class(e_ident['EI_CLASS']))
68 self._emitline(' Data: %s' %
69 describe_ei_data(e_ident['EI_DATA']))
70 self._emitline(' Version: %s' %
71 describe_ei_version(e_ident['EI_VERSION']))
72 self._emitline(' OS/ABI: %s' %
73 describe_ei_osabi(e_ident['EI_OSABI']))
74 self._emitline(' ABI Version: %d' %
75 e_ident['EI_ABIVERSION'])
76 self._emitline(' Type: %s' %
77 describe_e_type(header['e_type']))
Eli Benderskyde8d71e2011-09-09 08:22:35 +030078 self._emitline(' Machine: %s' %
79 describe_e_machine(header['e_machine']))
80 self._emitline(' Version: %s' %
81 describe_e_version_numeric(header['e_version']))
Eli Benderskyd62928d2011-09-09 10:05:57 +030082 self._emitline(' Entry point address: %s' %
Eli Bendersky26de2ac2011-09-13 06:50:28 +030083 self._format_hex(header['e_entry']))
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +030084 self._emit(' Start of program headers: %s' %
Eli Benderskyd62928d2011-09-09 10:05:57 +030085 header['e_phoff'])
86 self._emitline(' (bytes into file)')
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +030087 self._emit(' Start of section headers: %s' %
Eli Benderskyd62928d2011-09-09 10:05:57 +030088 header['e_shoff'])
89 self._emitline(' (bytes into file)')
90 self._emitline(' Flags: %s' %
Eli Bendersky26de2ac2011-09-13 06:50:28 +030091 self._format_hex(header['e_flags']))
Eli Benderskyd62928d2011-09-09 10:05:57 +030092 self._emitline(' Size of this header: %s (bytes)' %
93 header['e_ehsize'])
94 self._emitline(' Size of program headers: %s (bytes)' %
95 header['e_phentsize'])
96 self._emitline(' Number of program headers: %s' %
97 header['e_phnum'])
98 self._emitline(' Size of section headers: %s (bytes)' %
99 header['e_shentsize'])
100 self._emitline(' Number of section headers: %s' %
101 header['e_shnum'])
102 self._emitline(' Section header string table index: %s' %
103 header['e_shstrndx'])
Eli Bendersky933f6992011-09-09 08:11:06 +0300104
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300105 def display_program_headers(self, show_heading=True):
106 """ Display the ELF program headers.
107 If show_heading is True, displays the heading for this information
108 (Elf file type is...)
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300109 """
110 self._emitline()
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300111 if self.elffile.num_segments() == 0:
112 self._emitline('There are no program headers in this file.')
113 return
114
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300115 elfheader = self.elffile.header
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300116 if show_heading:
117 self._emitline('Elf file type is %s' %
118 describe_e_type(elfheader['e_type']))
119 self._emitline('Entry point is %s' %
120 self._format_hex(elfheader['e_entry']))
121 # readelf weirness - why isn't e_phoff printed as hex? (for section
122 # headers, it is...)
123 self._emitline('There are %s program headers, starting at offset %s' % (
124 elfheader['e_phnum'], elfheader['e_phoff']))
125 self._emitline()
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300126
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300127 self._emitline('Program Headers:')
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300128
129 # Now comes the table of program headers with their attributes. Note
130 # that due to different formatting constraints of 32-bit and 64-bit
131 # addresses, there are some conditions on elfclass here.
132 #
133 # First comes the table heading
134 #
135 if self.elffile.elfclass == 32:
136 self._emitline(' Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align')
137 else:
138 self._emitline(' Type Offset VirtAddr PhysAddr')
139 self._emitline(' FileSiz MemSiz Flags Align')
140
141 # Now the entries
142 #
143 for segment in self.elffile.iter_segments():
144 self._emit(' %-14s ' % describe_p_type(segment['p_type']))
145
146 if self.elffile.elfclass == 32:
147 self._emitline('%s %s %s %s %s %-3s %s' % (
148 self._format_hex(segment['p_offset'], fieldsize=6),
149 self._format_hex(segment['p_vaddr'], fullhex=True),
150 self._format_hex(segment['p_paddr'], fullhex=True),
151 self._format_hex(segment['p_filesz'], fieldsize=5),
152 self._format_hex(segment['p_memsz'], fieldsize=5),
153 describe_p_flags(segment['p_flags']),
154 self._format_hex(segment['p_align'])))
Eli Benderskya41c3c02011-09-14 06:18:28 +0300155 else: # 64
156 self._emitline('%s %s %s' % (
157 self._format_hex(segment['p_offset'], fullhex=True),
158 self._format_hex(segment['p_vaddr'], fullhex=True),
159 self._format_hex(segment['p_paddr'], fullhex=True)))
160 self._emitline(' %s %s %-3s %s' % (
161 self._format_hex(segment['p_filesz'], fullhex=True),
162 self._format_hex(segment['p_memsz'], fullhex=True),
163 describe_p_flags(segment['p_flags']),
164 # lead0x set to False for p_align, to mimic readelf.
165 # No idea why the difference from 32-bit mode :-|
166 self._format_hex(segment['p_align'], lead0x=False)))
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300167
Eli Bendersky3f4de3e2011-09-14 05:58:06 +0300168 if isinstance(segment, InterpSegment):
169 self._emitline(' [Requesting program interpreter: %s]' %
170 segment.get_interp_name())
171
Eli Bendersky58585b02011-09-15 07:07:54 +0300172 # Sections to segments mapping
173 #
174 if self.elffile.num_sections() == 0:
175 # No sections? We're done
176 return
177
178 self._emitline('\n Section to Segment mapping:')
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300179 self._emitline(' Segment Sections...')
Eli Bendersky58585b02011-09-15 07:07:54 +0300180
181 for nseg, segment in enumerate(self.elffile.iter_segments()):
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300182 self._emit(' %2.2d ' % nseg)
Eli Bendersky58585b02011-09-15 07:07:54 +0300183
184 for section in self.elffile.iter_sections():
185 if ( not section.is_null() and
186 segment.section_in_segment(section)):
187 self._emit('%s ' % section.name)
188
189 self._emitline('')
190
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300191 def display_section_headers(self, show_heading=True):
Eli Bendersky377bd862011-09-16 11:10:44 +0300192 """ Display the ELF section headers
193 """
194 elfheader = self.elffile.header
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300195 if show_heading:
196 self._emitline('There are %s section headers, starting at offset %s' % (
197 elfheader['e_shnum'], self._format_hex(elfheader['e_shoff'])))
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300198
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300199 self._emitline('\nSection Header%s:' % (
Eli Bendersky377bd862011-09-16 11:10:44 +0300200 's' if elfheader['e_shnum'] > 1 else ''))
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300201
Eli Bendersky377bd862011-09-16 11:10:44 +0300202 # Different formatting constraints of 32-bit and 64-bit addresses
203 #
204 if self.elffile.elfclass == 32:
205 self._emitline(' [Nr] Name Type Addr Off Size ES Flg Lk Inf Al')
206 else:
207 self._emitline(' [Nr] Name Type Address Offset')
208 self._emitline(' Size EntSize Flags Link Info Align')
209
210 # Now the entries
211 #
212 for nsec, section in enumerate(self.elffile.iter_sections()):
213 self._emit(' [%2u] %-17.17s %-15.15s ' % (
214 nsec, section.name, describe_sh_type(section['sh_type'])))
215
216 if self.elffile.elfclass == 32:
217 self._emitline('%s %s %s %s %3s %2s %3s %2s' % (
218 self._format_hex(section['sh_addr'], fieldsize=8, lead0x=False),
219 self._format_hex(section['sh_offset'], fieldsize=6, lead0x=False),
220 self._format_hex(section['sh_size'], fieldsize=6, lead0x=False),
221 self._format_hex(section['sh_entsize'], fieldsize=2, lead0x=False),
222 describe_sh_flags(section['sh_flags']),
223 section['sh_link'], section['sh_info'],
224 section['sh_addralign']))
225 else: # 64
226 self._emitline(' %s %s' % (
227 self._format_hex(section['sh_addr'], fullhex=True, lead0x=False),
228 self._format_hex(section['sh_offset'],
229 fieldsize=16 if section['sh_offset'] > 0xffffffff else 8,
230 lead0x=False)))
231 self._emitline(' %s %s %3s %2s %3s %s' % (
232 self._format_hex(section['sh_size'], fullhex=True, lead0x=False),
233 self._format_hex(section['sh_entsize'], fullhex=True, lead0x=False),
234 describe_sh_flags(section['sh_flags']),
235 section['sh_link'], section['sh_info'],
236 section['sh_addralign']))
237
238 self._emitline('Key to Flags:')
Eli Bendersky93e630d2011-11-16 07:22:57 +0200239 self._emit(' W (write), A (alloc), X (execute), M (merge), S (strings)')
240 if self.elffile['e_machine'] in ('EM_X86_64', 'EM_L10M'):
241 self._emitline(', l (large)')
242 else:
243 self._emitline()
244 self._emitline(' I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)')
Eli Bendersky377bd862011-09-16 11:10:44 +0300245 self._emitline(' O (extra OS processing required) o (OS specific), p (processor specific)')
246
Eli Bendersky3edefab2011-09-16 14:52:54 +0300247 def display_symbol_tables(self):
248 """ Display the symbol tables contained in the file
249 """
250 for section in self.elffile.iter_sections():
251 if not isinstance(section, SymbolTableSection):
252 continue
253
254 if section['sh_entsize'] == 0:
255 self._emitline("\nSymbol table '%s' has a sh_entsize of zero!" % (
256 section.name))
257 continue
258
259 self._emitline("\nSymbol table '%s' contains %s entries:" % (
260 section.name, section.num_symbols()))
261
262 if self.elffile.elfclass == 32:
263 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
264 else: # 64
265 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
266
267 for nsym, symbol in enumerate(section.iter_symbols()):
Eli Benderskyb6fa3652011-09-16 15:20:20 +0300268 # symbol names are truncated to 25 chars, similarly to readelf
269 self._emitline('%6d: %s %5d %-7s %-6s %-7s %4s %.25s' % (
Eli Bendersky3edefab2011-09-16 14:52:54 +0300270 nsym,
271 self._format_hex(symbol['st_value'], fullhex=True, lead0x=False),
272 symbol['st_size'],
273 describe_symbol_type(symbol['st_info']['type']),
274 describe_symbol_bind(symbol['st_info']['bind']),
275 describe_symbol_visibility(symbol['st_other']['visibility']),
276 describe_symbol_shndx(symbol['st_shndx']),
277 symbol.name))
278
Eli Bendersky84066b22011-09-20 06:48:52 +0300279 def display_relocations(self):
280 """ Display the relocations contained in the file
281 """
282 has_relocation_sections = False
283 for section in self.elffile.iter_sections():
284 if not isinstance(section, RelocationSection):
285 continue
286
287 has_relocation_sections = True
288 self._emitline("\nRelocation section '%s' at offset %s contains %s entries:" % (
Eli Bendersky7c1ffa62011-09-22 06:37:07 +0300289 section.name,
290 self._format_hex(section['sh_offset']),
291 section.num_relocations()))
Eli Bendersky89a824f2011-09-23 10:59:59 +0300292 if section.is_RELA():
293 self._emitline(" Offset Info Type Sym. Value Sym. Name + Addend")
294 else:
295 self._emitline(" Offset Info Type Sym.Value Sym. Name")
296
297 # The symbol table section pointed to in sh_link
298 symtable = self.elffile.get_section(section['sh_link'])
299
Eli Bendersky84066b22011-09-20 06:48:52 +0300300 for rel in section.iter_relocations():
Eli Bendersky89a824f2011-09-23 10:59:59 +0300301 hexwidth = 8 if self.elffile.elfclass == 32 else 12
Eli Bendersky099d48f2011-09-23 12:03:48 +0300302 self._emit('%s %s %-17.17s' % (
Eli Bendersky89a824f2011-09-23 10:59:59 +0300303 self._format_hex(rel['r_offset'],
304 fieldsize=hexwidth, lead0x=False),
305 self._format_hex(rel['r_info'],
306 fieldsize=hexwidth, lead0x=False),
307 describe_reloc_type(
Eli Bendersky067b3fd2011-11-18 12:02:57 +0200308 rel['r_info_type'], self.elffile)))
Eli Bendersky099d48f2011-09-23 12:03:48 +0300309
310 if rel['r_info_sym'] == 0:
311 self._emitline()
312 continue
313
314 symbol = symtable.get_symbol(rel['r_info_sym'])
Eli Benderskyadf707a2011-09-23 15:23:41 +0300315 # Some symbols have zero 'st_name', so instead what's used is
316 # the name of the section they point at
317 if symbol['st_name'] == 0:
318 symsec = self.elffile.get_section(symbol['st_shndx'])
319 symbol_name = symsec.name
320 else:
321 symbol_name = symbol.name
Eli Bendersky6434a962011-09-23 17:14:08 +0300322 self._emit(' %s %s%22.22s' % (
Eli Bendersky89a824f2011-09-23 10:59:59 +0300323 self._format_hex(
324 symbol['st_value'],
325 fullhex=True, lead0x=False),
326 ' ' if self.elffile.elfclass == 32 else '',
Eli Benderskyadf707a2011-09-23 15:23:41 +0300327 symbol_name))
Eli Bendersky89a824f2011-09-23 10:59:59 +0300328 if section.is_RELA():
329 self._emit(' %s %x' % (
330 '+' if rel['r_addend'] >= 0 else '-',
331 abs(rel['r_addend'])))
332 self._emitline()
Eli Bendersky84066b22011-09-20 06:48:52 +0300333
334 if not has_relocation_sections:
335 self._emitline('\nThere are no relocations in this file.')
336
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300337 def display_hex_dump(self, section_spec):
338 """ Display a hex dump of a section. section_spec is either a section
339 number or a name.
340 """
eliben54e39b22011-09-19 13:10:57 +0300341 section = self._section_from_spec(section_spec)
342 if section is None:
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300343 self._emitline("Section '%s' does not exist in the file!" % (
344 section_spec))
345 return
346
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300347 self._emitline("\nHex dump of section '%s':" % section.name)
Eli Benderskyadf707a2011-09-23 15:23:41 +0300348 self._note_relocs_for_section(section)
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300349 addr = section['sh_addr']
350 data = section.data()
351 dataptr = 0
352
353 while dataptr < len(data):
354 bytesleft = len(data) - dataptr
355 # chunks of 16 bytes per line
356 linebytes = 16 if bytesleft > 16 else bytesleft
357
358 self._emit(' %s ' % self._format_hex(addr, fieldsize=8))
359 for i in range(16):
360 if i < linebytes:
361 self._emit('%2.2x' % ord(data[dataptr + i]))
362 else:
363 self._emit(' ')
364 if i % 4 == 3:
365 self._emit(' ')
366
367 for i in range(linebytes):
368 c = data[dataptr + i]
Eli Bendersky099d48f2011-09-23 12:03:48 +0300369 if c >= ' ' and ord(c) < 0x7f:
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300370 self._emit(c)
371 else:
372 self._emit('.')
373
374 self._emitline()
375 addr += linebytes
376 dataptr += linebytes
377
378 self._emitline()
379
Eli Bendersky53e86db2011-09-18 06:04:30 +0300380 def display_string_dump(self, section_spec):
381 """ Display a strings dump of a section. section_spec is either a
382 section number or a name.
383 """
eliben54e39b22011-09-19 13:10:57 +0300384 section = self._section_from_spec(section_spec)
385 if section is None:
Eli Bendersky53e86db2011-09-18 06:04:30 +0300386 self._emitline("Section '%s' does not exist in the file!" % (
387 section_spec))
388 return
389
390 printables = set(string.printable)
Eli Bendersky53e86db2011-09-18 06:04:30 +0300391 self._emitline("\nString dump of section '%s':" % section.name)
392
393 found = False
394 data = section.data()
395 dataptr = 0
396
397 while dataptr < len(data):
398 while dataptr < len(data) and data[dataptr] not in printables:
399 dataptr += 1
400
401 if dataptr >= len(data):
402 break
403
404 endptr = dataptr
405 while endptr < len(data) and data[endptr] != '\x00':
406 endptr += 1
407
408 found = True
409 self._emitline(' [%6x] %s' % (
410 dataptr, data[dataptr:endptr]))
411
412 dataptr = endptr
413
414 if not found:
415 self._emitline(' No strings found in this section.')
416 else:
417 self._emitline()
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300418
eliben0fc47af2011-10-02 13:47:12 +0200419 def display_debug_dump(self, section_name):
420 """ Dump a DWARF section
421 """
422 self._init_dwarfinfo()
423 if self._dwarfinfo is None:
424 return
425
426 if section_name == 'info':
427 self._dump_debug_info()
428 else:
429 self._emitline('debug dump not yet supported for "%s"' % section_name)
430
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300431 def _format_hex(self, addr, fieldsize=None, fullhex=False, lead0x=True):
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300432 """ Format an address into a hexadecimal string.
Eli Benderskyd62928d2011-09-09 10:05:57 +0300433
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300434 fieldsize:
435 Size of the hexadecimal field (with leading zeros to fit the
436 address into. For example with fieldsize=8, the format will
437 be %08x
438 If None, the minimal required field size will be used.
439
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300440 fullhex:
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300441 If True, override fieldsize to set it to the maximal size
442 needed for the elfclass
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300443
444 lead0x:
445 If True, leading 0x is added
Eli Benderskyd62928d2011-09-09 10:05:57 +0300446 """
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300447 s = '0x' if lead0x else ''
448 if fullhex:
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300449 fieldsize = 8 if self.elffile.elfclass == 32 else 16
450 if fieldsize is None:
451 field = '%x'
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300452 else:
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300453 field = '%' + '0%sx' % fieldsize
454 return s + field % addr
Eli Benderskyd62928d2011-09-09 10:05:57 +0300455
eliben54e39b22011-09-19 13:10:57 +0300456 def _section_from_spec(self, spec):
457 """ Retrieve a section given a "spec" (either number or name).
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300458 Return None if no such section exists in the file.
459 """
460 try:
461 num = int(spec)
eliben54e39b22011-09-19 13:10:57 +0300462 if num < self.elffile.num_sections():
463 return self.elffile.get_section(num)
464 else:
465 return None
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300466 except ValueError:
467 # Not a number. Must be a name then
eliben54e39b22011-09-19 13:10:57 +0300468 return self.elffile.get_section_by_name(spec)
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300469
Eli Benderskyadf707a2011-09-23 15:23:41 +0300470 def _note_relocs_for_section(self, section):
471 """ If there are relocation sections pointing to the givne section,
472 emit a note about it.
473 """
474 for relsec in self.elffile.iter_sections():
475 if isinstance(relsec, RelocationSection):
476 info_idx = relsec['sh_info']
477 if self.elffile.get_section(info_idx) == section:
478 self._emitline(' Note: This section has relocations against it, but these have NOT been applied to this dump.')
479 return
480
eliben0fc47af2011-10-02 13:47:12 +0200481 def _init_dwarfinfo(self):
482 """ Initialize the DWARF info contained in the file and assign it to
483 self._dwarfinfo.
484 Leave self._dwarfinfo at None if no DWARF info was found in the file
485 """
486 if self._dwarfinfo is not None:
487 return
488
489 if self.elffile.has_dwarf_info():
490 self._dwarfinfo = self.elffile.get_dwarf_info()
491 else:
492 self._dwarfinfo = None
493
494 def _dump_debug_info(self):
495 """ Dump the debugging info section.
496 """
eliben985c2c12011-11-14 17:53:23 +0200497 self._emitline('Contents of the .debug_info section:\n')
498
eliben0fc47af2011-10-02 13:47:12 +0200499 # Offset of the .debug_info section in the stream
Eli Bendersky149315a2011-11-24 08:12:37 +0200500 section_offset = self._dwarfinfo.debug_info_sec.global_offset
Eli Bendersky6062bf72011-11-23 06:54:40 +0200501
eliben0fc47af2011-10-02 13:47:12 +0200502 for cu in self._dwarfinfo.iter_CUs():
eliben985c2c12011-11-14 17:53:23 +0200503 self._emitline(' Compilation Unit @ offset %s:' %
Eli Bendersky149315a2011-11-24 08:12:37 +0200504 self._format_hex(cu.cu_offset))
eliben0fc47af2011-10-02 13:47:12 +0200505 self._emitline(' Length: %s (%s)' % (
506 self._format_hex(cu['unit_length']),
507 '%s-bit' % cu.dwarf_format()))
508 self._emitline(' Version: %s' % cu['version']),
509 self._emitline(' Abbrev Offset: %s' % cu['debug_abbrev_offset']),
510 self._emitline(' Pointer Size: %s' % cu['address_size'])
511
512 # The nesting depth of each DIE within the tree of DIEs must be
513 # displayed. To implement this, a counter is incremented each time
514 # the current DIE has children, and decremented when a null die is
515 # encountered. Due to the way the DIE tree is serialized, this will
516 # correctly reflect the nesting depth
517 #
518 die_depth = 0
519 for die in cu.iter_DIEs():
520 if die.is_null():
521 die_depth -= 1
522 continue
elibenaee11d22011-10-26 10:42:34 +0200523 self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
524 die_depth,
Eli Bendersky149315a2011-11-24 08:12:37 +0200525 die.offset,
elibenaee11d22011-10-26 10:42:34 +0200526 die.abbrev_code,
527 die.tag))
528
Eli Bendersky41c971a2011-10-27 14:29:32 +0200529 for attr in die.attributes.itervalues():
elibenaee11d22011-10-26 10:42:34 +0200530 self._emitline(' <%2x> %-18s: %s' % (
Eli Bendersky149315a2011-11-24 08:12:37 +0200531 attr.offset,
Eli Bendersky41c971a2011-10-27 14:29:32 +0200532 attr.name,
eliben3bc9c342011-10-26 13:10:58 +0200533 describe_attr_value(
Eli Bendersky5be3be82011-10-27 14:28:12 +0200534 attr, die, section_offset)))
eliben0fc47af2011-10-02 13:47:12 +0200535
536 if die.has_children:
537 die_depth += 1
elibenaee11d22011-10-26 10:42:34 +0200538
Eli Benderskyf5670e82011-11-24 09:05:44 +0200539 self._emitline()
eliben0fc47af2011-10-02 13:47:12 +0200540
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300541 def _emit(self, s=''):
Eli Bendersky933f6992011-09-09 08:11:06 +0300542 """ Emit an object to output
543 """
544 self.output.write(str(s))
Eli Benderskyd62928d2011-09-09 10:05:57 +0300545
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300546 def _emitline(self, s=''):
Eli Bendersky933f6992011-09-09 08:11:06 +0300547 """ Emit an object to output, followed by a newline
548 """
549 self.output.write(str(s) + '\n')
550
551
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300552SCRIPT_DESCRIPTION = 'Display information about the contents of ELF format files'
553VERSION_STRING = '%%prog: based on pyelftools %s' % __version__
554
555
Eli Bendersky933f6992011-09-09 08:11:06 +0300556def main():
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300557 # parse the command-line arguments and invoke ReadElf
Eli Bendersky40eb1702011-09-16 16:59:52 +0300558 optparser = OptionParser(
Eli Benderskyecde41b2011-09-16 17:16:20 +0300559 usage='usage: %prog [options] <elf-file>',
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300560 description=SCRIPT_DESCRIPTION,
Eli Bendersky40eb1702011-09-16 16:59:52 +0300561 add_help_option=False, # -h is a real option of readelf
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300562 prog='readelf.py',
563 version=VERSION_STRING)
Eli Bendersky40eb1702011-09-16 16:59:52 +0300564 optparser.add_option('-H', '--help',
565 action='store_true', dest='help',
566 help='Display this information')
567 optparser.add_option('-h', '--file-header',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300568 action='store_true', dest='show_file_header',
569 help='Display the ELF file header')
Eli Bendersky40eb1702011-09-16 16:59:52 +0300570 optparser.add_option('-l', '--program-headers', '--segments',
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300571 action='store_true', dest='show_program_header',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300572 help='Display the program headers')
Eli Bendersky40eb1702011-09-16 16:59:52 +0300573 optparser.add_option('-S', '--section-headers', '--sections',
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300574 action='store_true', dest='show_section_header',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300575 help="Display the sections' headers")
Eli Bendersky40eb1702011-09-16 16:59:52 +0300576 optparser.add_option('-e', '--headers',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300577 action='store_true', dest='show_all_headers',
578 help='Equivalent to: -h -l -S')
Eli Bendersky40eb1702011-09-16 16:59:52 +0300579 optparser.add_option('-s', '--symbols', '--syms',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300580 action='store_true', dest='show_symbols',
581 help='Display the symbol table')
Eli Bendersky84066b22011-09-20 06:48:52 +0300582 optparser.add_option('-r', '--relocs',
583 action='store_true', dest='show_relocs',
584 help='Display the relocations (if present)')
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300585 optparser.add_option('-x', '--hex-dump',
586 action='store', dest='show_hex_dump', metavar='<number|name>',
587 help='Dump the contents of section <number|name> as bytes')
Eli Bendersky53e86db2011-09-18 06:04:30 +0300588 optparser.add_option('-p', '--string-dump',
589 action='store', dest='show_string_dump', metavar='<number|name>',
590 help='Dump the contents of section <number|name> as strings')
eliben0fc47af2011-10-02 13:47:12 +0200591 optparser.add_option('--debug-dump',
592 action='store', dest='debug_dump_section', metavar='<section>',
593 help='Display the contents of DWARF debug sections')
Eli Bendersky53e86db2011-09-18 06:04:30 +0300594
Eli Bendersky933f6992011-09-09 08:11:06 +0300595 options, args = optparser.parse_args()
596
Eli Bendersky40eb1702011-09-16 16:59:52 +0300597 if options.help or len(args) == 0:
598 optparser.print_help()
599 sys.exit(0)
600
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300601 if options.show_all_headers:
602 do_file_header = do_section_header = do_program_header = True
603 else:
604 do_file_header = options.show_file_header
605 do_section_header = options.show_section_header
606 do_program_header = options.show_program_header
607
Eli Bendersky933f6992011-09-09 08:11:06 +0300608 with open(args[0], 'rb') as file:
609 try:
610 readelf = ReadElf(file, sys.stdout)
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300611 if do_file_header:
612 readelf.display_file_header()
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300613 if do_section_header:
614 readelf.display_section_headers(
615 show_heading=not do_file_header)
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300616 if do_program_header:
617 readelf.display_program_headers(
618 show_heading=not do_file_header)
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300619 if options.show_symbols:
620 readelf.display_symbol_tables()
Eli Bendersky84066b22011-09-20 06:48:52 +0300621 if options.show_relocs:
622 readelf.display_relocations()
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300623 if options.show_hex_dump:
624 readelf.display_hex_dump(options.show_hex_dump)
Eli Bendersky53e86db2011-09-18 06:04:30 +0300625 if options.show_string_dump:
626 readelf.display_string_dump(options.show_string_dump)
eliben0fc47af2011-10-02 13:47:12 +0200627 if options.debug_dump_section:
628 readelf.display_debug_dump(options.debug_dump_section)
Eli Bendersky933f6992011-09-09 08:11:06 +0300629 except ELFError as ex:
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300630 sys.stderr.write('ELF error: %s\n' % ex)
Eli Bendersky933f6992011-09-09 08:11:06 +0300631 sys.exit(1)
632
633
634#-------------------------------------------------------------------------------
635if __name__ == '__main__':
636 main()
637