nothing works because we're in the middle of a relocation revamp - BUT IT WILL!!
[pyelftools.git] / scripts / readelf.py
1 #!/usr/bin/env python
2 #-------------------------------------------------------------------------------
3 # scripts/readelf.py
4 #
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 #-------------------------------------------------------------------------------
10 import os, sys
11 from optparse import OptionParser
12 import string
13
14
15 # If elftools is not installed, maybe we're running from the root or scripts
16 # dir of the source distribution
17 #
18 try:
19 import elftools
20 except ImportError:
21 sys.path.extend(['.', '..'])
22
23 from elftools import __version__
24 from elftools.common.exceptions import ELFError
25 from elftools.elf.elffile import ELFFile
26 from elftools.elf.segments import InterpSegment
27 from elftools.elf.sections import SymbolTableSection, RelocationSection
28 from elftools.elf.descriptions import (
29 describe_ei_class, describe_ei_data, describe_ei_version,
30 describe_ei_osabi, describe_e_type, describe_e_machine,
31 describe_e_version_numeric, describe_p_type, describe_p_flags,
32 describe_sh_type, describe_sh_flags,
33 describe_symbol_type, describe_symbol_bind, describe_symbol_visibility,
34 describe_symbol_shndx, describe_reloc_type,
35 )
36 from elftools.dwarf.dwarfinfo import DWARFInfo, DebugSectionLocator
37 from elftools.dwarf.descriptions import describe_attr_value
38
39
40 class ReadElf(object):
41 """ display_* methods are used to emit output into the output stream
42 """
43 def __init__(self, file, output):
44 """ file:
45 stream object with the ELF file to read
46
47 output:
48 output stream to write to
49 """
50 self.elffile = ELFFile(file)
51 self.output = output
52
53 # Lazily initialized if a debug dump is requested
54 self._dwarfinfo = None
55
56 def display_file_header(self):
57 """ Display the ELF file header
58 """
59 self._emitline('ELF Header:')
60 self._emit(' Magic: ')
61 self._emitline(' '.join('%2.2x' % ord(b)
62 for b in self.elffile.e_ident_raw))
63 header = self.elffile.header
64 e_ident = header['e_ident']
65 self._emitline(' Class: %s' %
66 describe_ei_class(e_ident['EI_CLASS']))
67 self._emitline(' Data: %s' %
68 describe_ei_data(e_ident['EI_DATA']))
69 self._emitline(' Version: %s' %
70 describe_ei_version(e_ident['EI_VERSION']))
71 self._emitline(' OS/ABI: %s' %
72 describe_ei_osabi(e_ident['EI_OSABI']))
73 self._emitline(' ABI Version: %d' %
74 e_ident['EI_ABIVERSION'])
75 self._emitline(' Type: %s' %
76 describe_e_type(header['e_type']))
77 self._emitline(' Machine: %s' %
78 describe_e_machine(header['e_machine']))
79 self._emitline(' Version: %s' %
80 describe_e_version_numeric(header['e_version']))
81 self._emitline(' Entry point address: %s' %
82 self._format_hex(header['e_entry']))
83 self._emit(' Start of program headers: %s' %
84 header['e_phoff'])
85 self._emitline(' (bytes into file)')
86 self._emit(' Start of section headers: %s' %
87 header['e_shoff'])
88 self._emitline(' (bytes into file)')
89 self._emitline(' Flags: %s' %
90 self._format_hex(header['e_flags']))
91 self._emitline(' Size of this header: %s (bytes)' %
92 header['e_ehsize'])
93 self._emitline(' Size of program headers: %s (bytes)' %
94 header['e_phentsize'])
95 self._emitline(' Number of program headers: %s' %
96 header['e_phnum'])
97 self._emitline(' Size of section headers: %s (bytes)' %
98 header['e_shentsize'])
99 self._emitline(' Number of section headers: %s' %
100 header['e_shnum'])
101 self._emitline(' Section header string table index: %s' %
102 header['e_shstrndx'])
103
104 def display_program_headers(self, show_heading=True):
105 """ Display the ELF program headers.
106 If show_heading is True, displays the heading for this information
107 (Elf file type is...)
108 """
109 self._emitline()
110 if self.elffile.num_segments() == 0:
111 self._emitline('There are no program headers in this file.')
112 return
113
114 elfheader = self.elffile.header
115 if show_heading:
116 self._emitline('Elf file type is %s' %
117 describe_e_type(elfheader['e_type']))
118 self._emitline('Entry point is %s' %
119 self._format_hex(elfheader['e_entry']))
120 # readelf weirness - why isn't e_phoff printed as hex? (for section
121 # headers, it is...)
122 self._emitline('There are %s program headers, starting at offset %s' % (
123 elfheader['e_phnum'], elfheader['e_phoff']))
124 self._emitline()
125
126 self._emitline('Program Headers:')
127
128 # Now comes the table of program headers with their attributes. Note
129 # that due to different formatting constraints of 32-bit and 64-bit
130 # addresses, there are some conditions on elfclass here.
131 #
132 # First comes the table heading
133 #
134 if self.elffile.elfclass == 32:
135 self._emitline(' Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align')
136 else:
137 self._emitline(' Type Offset VirtAddr PhysAddr')
138 self._emitline(' FileSiz MemSiz Flags Align')
139
140 # Now the entries
141 #
142 for segment in self.elffile.iter_segments():
143 self._emit(' %-14s ' % describe_p_type(segment['p_type']))
144
145 if self.elffile.elfclass == 32:
146 self._emitline('%s %s %s %s %s %-3s %s' % (
147 self._format_hex(segment['p_offset'], fieldsize=6),
148 self._format_hex(segment['p_vaddr'], fullhex=True),
149 self._format_hex(segment['p_paddr'], fullhex=True),
150 self._format_hex(segment['p_filesz'], fieldsize=5),
151 self._format_hex(segment['p_memsz'], fieldsize=5),
152 describe_p_flags(segment['p_flags']),
153 self._format_hex(segment['p_align'])))
154 else: # 64
155 self._emitline('%s %s %s' % (
156 self._format_hex(segment['p_offset'], fullhex=True),
157 self._format_hex(segment['p_vaddr'], fullhex=True),
158 self._format_hex(segment['p_paddr'], fullhex=True)))
159 self._emitline(' %s %s %-3s %s' % (
160 self._format_hex(segment['p_filesz'], fullhex=True),
161 self._format_hex(segment['p_memsz'], fullhex=True),
162 describe_p_flags(segment['p_flags']),
163 # lead0x set to False for p_align, to mimic readelf.
164 # No idea why the difference from 32-bit mode :-|
165 self._format_hex(segment['p_align'], lead0x=False)))
166
167 if isinstance(segment, InterpSegment):
168 self._emitline(' [Requesting program interpreter: %s]' %
169 segment.get_interp_name())
170
171 # Sections to segments mapping
172 #
173 if self.elffile.num_sections() == 0:
174 # No sections? We're done
175 return
176
177 self._emitline('\n Section to Segment mapping:')
178 self._emitline(' Segment Sections...')
179
180 for nseg, segment in enumerate(self.elffile.iter_segments()):
181 self._emit(' %2.2d ' % nseg)
182
183 for section in self.elffile.iter_sections():
184 if ( not section.is_null() and
185 segment.section_in_segment(section)):
186 self._emit('%s ' % section.name)
187
188 self._emitline('')
189
190 def display_section_headers(self, show_heading=True):
191 """ Display the ELF section headers
192 """
193 elfheader = self.elffile.header
194 if show_heading:
195 self._emitline('There are %s section headers, starting at offset %s' % (
196 elfheader['e_shnum'], self._format_hex(elfheader['e_shoff'])))
197
198 self._emitline('\nSection Header%s:' % (
199 's' if elfheader['e_shnum'] > 1 else ''))
200
201 # Different formatting constraints of 32-bit and 64-bit addresses
202 #
203 if self.elffile.elfclass == 32:
204 self._emitline(' [Nr] Name Type Addr Off Size ES Flg Lk Inf Al')
205 else:
206 self._emitline(' [Nr] Name Type Address Offset')
207 self._emitline(' Size EntSize Flags Link Info Align')
208
209 # Now the entries
210 #
211 for nsec, section in enumerate(self.elffile.iter_sections()):
212 self._emit(' [%2u] %-17.17s %-15.15s ' % (
213 nsec, section.name, describe_sh_type(section['sh_type'])))
214
215 if self.elffile.elfclass == 32:
216 self._emitline('%s %s %s %s %3s %2s %3s %2s' % (
217 self._format_hex(section['sh_addr'], fieldsize=8, lead0x=False),
218 self._format_hex(section['sh_offset'], fieldsize=6, lead0x=False),
219 self._format_hex(section['sh_size'], fieldsize=6, lead0x=False),
220 self._format_hex(section['sh_entsize'], fieldsize=2, lead0x=False),
221 describe_sh_flags(section['sh_flags']),
222 section['sh_link'], section['sh_info'],
223 section['sh_addralign']))
224 else: # 64
225 self._emitline(' %s %s' % (
226 self._format_hex(section['sh_addr'], fullhex=True, lead0x=False),
227 self._format_hex(section['sh_offset'],
228 fieldsize=16 if section['sh_offset'] > 0xffffffff else 8,
229 lead0x=False)))
230 self._emitline(' %s %s %3s %2s %3s %s' % (
231 self._format_hex(section['sh_size'], fullhex=True, lead0x=False),
232 self._format_hex(section['sh_entsize'], fullhex=True, lead0x=False),
233 describe_sh_flags(section['sh_flags']),
234 section['sh_link'], section['sh_info'],
235 section['sh_addralign']))
236
237 self._emitline('Key to Flags:')
238 self._emit(' W (write), A (alloc), X (execute), M (merge), S (strings)')
239 if self.elffile['e_machine'] in ('EM_X86_64', 'EM_L10M'):
240 self._emitline(', l (large)')
241 else:
242 self._emitline()
243 self._emitline(' I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)')
244 self._emitline(' O (extra OS processing required) o (OS specific), p (processor specific)')
245
246 def display_symbol_tables(self):
247 """ Display the symbol tables contained in the file
248 """
249 for section in self.elffile.iter_sections():
250 if not isinstance(section, SymbolTableSection):
251 continue
252
253 if section['sh_entsize'] == 0:
254 self._emitline("\nSymbol table '%s' has a sh_entsize of zero!" % (
255 section.name))
256 continue
257
258 self._emitline("\nSymbol table '%s' contains %s entries:" % (
259 section.name, section.num_symbols()))
260
261 if self.elffile.elfclass == 32:
262 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
263 else: # 64
264 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
265
266 for nsym, symbol in enumerate(section.iter_symbols()):
267 # symbol names are truncated to 25 chars, similarly to readelf
268 self._emitline('%6d: %s %5d %-7s %-6s %-7s %4s %.25s' % (
269 nsym,
270 self._format_hex(symbol['st_value'], fullhex=True, lead0x=False),
271 symbol['st_size'],
272 describe_symbol_type(symbol['st_info']['type']),
273 describe_symbol_bind(symbol['st_info']['bind']),
274 describe_symbol_visibility(symbol['st_other']['visibility']),
275 describe_symbol_shndx(symbol['st_shndx']),
276 symbol.name))
277
278 def display_relocations(self):
279 """ Display the relocations contained in the file
280 """
281 has_relocation_sections = False
282 for section in self.elffile.iter_sections():
283 if not isinstance(section, RelocationSection):
284 continue
285
286 has_relocation_sections = True
287 self._emitline("\nRelocation section '%s' at offset %s contains %s entries:" % (
288 section.name,
289 self._format_hex(section['sh_offset']),
290 section.num_relocations()))
291 if section.is_RELA():
292 self._emitline(" Offset Info Type Sym. Value Sym. Name + Addend")
293 else:
294 self._emitline(" Offset Info Type Sym.Value Sym. Name")
295
296 # The symbol table section pointed to in sh_link
297 symtable = self.elffile.get_section(section['sh_link'])
298
299 for rel in section.iter_relocations():
300 hexwidth = 8 if self.elffile.elfclass == 32 else 12
301 self._emit('%s %s %-17.17s' % (
302 self._format_hex(rel['r_offset'],
303 fieldsize=hexwidth, lead0x=False),
304 self._format_hex(rel['r_info'],
305 fieldsize=hexwidth, lead0x=False),
306 describe_reloc_type(
307 rel['r_info_type'], self.elffile)))
308
309 if rel['r_info_sym'] == 0:
310 self._emitline()
311 continue
312
313 symbol = symtable.get_symbol(rel['r_info_sym'])
314 # Some symbols have zero 'st_name', so instead what's used is
315 # the name of the section they point at
316 if symbol['st_name'] == 0:
317 symsec = self.elffile.get_section(symbol['st_shndx'])
318 symbol_name = symsec.name
319 else:
320 symbol_name = symbol.name
321 self._emit(' %s %s%22.22s' % (
322 self._format_hex(
323 symbol['st_value'],
324 fullhex=True, lead0x=False),
325 ' ' if self.elffile.elfclass == 32 else '',
326 symbol_name))
327 if section.is_RELA():
328 self._emit(' %s %x' % (
329 '+' if rel['r_addend'] >= 0 else '-',
330 abs(rel['r_addend'])))
331 self._emitline()
332
333 if not has_relocation_sections:
334 self._emitline('\nThere are no relocations in this file.')
335
336 def display_hex_dump(self, section_spec):
337 """ Display a hex dump of a section. section_spec is either a section
338 number or a name.
339 """
340 section = self._section_from_spec(section_spec)
341 if section is None:
342 self._emitline("Section '%s' does not exist in the file!" % (
343 section_spec))
344 return
345
346 self._emitline("\nHex dump of section '%s':" % section.name)
347 self._note_relocs_for_section(section)
348 addr = section['sh_addr']
349 data = section.data()
350 dataptr = 0
351
352 while dataptr < len(data):
353 bytesleft = len(data) - dataptr
354 # chunks of 16 bytes per line
355 linebytes = 16 if bytesleft > 16 else bytesleft
356
357 self._emit(' %s ' % self._format_hex(addr, fieldsize=8))
358 for i in range(16):
359 if i < linebytes:
360 self._emit('%2.2x' % ord(data[dataptr + i]))
361 else:
362 self._emit(' ')
363 if i % 4 == 3:
364 self._emit(' ')
365
366 for i in range(linebytes):
367 c = data[dataptr + i]
368 if c >= ' ' and ord(c) < 0x7f:
369 self._emit(c)
370 else:
371 self._emit('.')
372
373 self._emitline()
374 addr += linebytes
375 dataptr += linebytes
376
377 self._emitline()
378
379 def display_string_dump(self, section_spec):
380 """ Display a strings dump of a section. section_spec is either a
381 section number or a name.
382 """
383 section = self._section_from_spec(section_spec)
384 if section is None:
385 self._emitline("Section '%s' does not exist in the file!" % (
386 section_spec))
387 return
388
389 printables = set(string.printable)
390 self._emitline("\nString dump of section '%s':" % section.name)
391
392 found = False
393 data = section.data()
394 dataptr = 0
395
396 while dataptr < len(data):
397 while dataptr < len(data) and data[dataptr] not in printables:
398 dataptr += 1
399
400 if dataptr >= len(data):
401 break
402
403 endptr = dataptr
404 while endptr < len(data) and data[endptr] != '\x00':
405 endptr += 1
406
407 found = True
408 self._emitline(' [%6x] %s' % (
409 dataptr, data[dataptr:endptr]))
410
411 dataptr = endptr
412
413 if not found:
414 self._emitline(' No strings found in this section.')
415 else:
416 self._emitline()
417
418 def display_debug_dump(self, section_name):
419 """ Dump a DWARF section
420 """
421 self._init_dwarfinfo()
422 if self._dwarfinfo is None:
423 return
424
425 if section_name == 'info':
426 self._dump_debug_info()
427 else:
428 self._emitline('debug dump not yet supported for "%s"' % section_name)
429
430 def _format_hex(self, addr, fieldsize=None, fullhex=False, lead0x=True):
431 """ Format an address into a hexadecimal string.
432
433 fieldsize:
434 Size of the hexadecimal field (with leading zeros to fit the
435 address into. For example with fieldsize=8, the format will
436 be %08x
437 If None, the minimal required field size will be used.
438
439 fullhex:
440 If True, override fieldsize to set it to the maximal size
441 needed for the elfclass
442
443 lead0x:
444 If True, leading 0x is added
445 """
446 s = '0x' if lead0x else ''
447 if fullhex:
448 fieldsize = 8 if self.elffile.elfclass == 32 else 16
449 if fieldsize is None:
450 field = '%x'
451 else:
452 field = '%' + '0%sx' % fieldsize
453 return s + field % addr
454
455 def _section_from_spec(self, spec):
456 """ Retrieve a section given a "spec" (either number or name).
457 Return None if no such section exists in the file.
458 """
459 try:
460 num = int(spec)
461 if num < self.elffile.num_sections():
462 return self.elffile.get_section(num)
463 else:
464 return None
465 except ValueError:
466 # Not a number. Must be a name then
467 return self.elffile.get_section_by_name(spec)
468
469 def _note_relocs_for_section(self, section):
470 """ If there are relocation sections pointing to the givne section,
471 emit a note about it.
472 """
473 for relsec in self.elffile.iter_sections():
474 if isinstance(relsec, RelocationSection):
475 info_idx = relsec['sh_info']
476 if self.elffile.get_section(info_idx) == section:
477 self._emitline(' Note: This section has relocations against it, but these have NOT been applied to this dump.')
478 return
479
480 def _init_dwarfinfo(self):
481 """ Initialize the DWARF info contained in the file and assign it to
482 self._dwarfinfo.
483 Leave self._dwarfinfo at None if no DWARF info was found in the file
484 """
485 if self._dwarfinfo is not None:
486 return
487
488 if self.elffile.has_dwarf_info():
489 self._dwarfinfo = self.elffile.get_dwarf_info()
490 else:
491 self._dwarfinfo = None
492
493 def _dump_debug_info(self):
494 """ Dump the debugging info section.
495 """
496 self._emitline('Contents of the .debug_info section:\n')
497
498 # Offset of the .debug_info section in the stream
499 section_offset = self._dwarfinfo.debug_info_loc.offset
500
501 print '&&& section_offset', section_offset
502
503 for cu in self._dwarfinfo.iter_CUs():
504 self._emitline(' Compilation Unit @ offset %s:' %
505 self._format_hex(cu.cu_offset - section_offset))
506 self._emitline(' Length: %s (%s)' % (
507 self._format_hex(cu['unit_length']),
508 '%s-bit' % cu.dwarf_format()))
509 self._emitline(' Version: %s' % cu['version']),
510 self._emitline(' Abbrev Offset: %s' % cu['debug_abbrev_offset']),
511 self._emitline(' Pointer Size: %s' % cu['address_size'])
512
513 # The nesting depth of each DIE within the tree of DIEs must be
514 # displayed. To implement this, a counter is incremented each time
515 # the current DIE has children, and decremented when a null die is
516 # encountered. Due to the way the DIE tree is serialized, this will
517 # correctly reflect the nesting depth
518 #
519 die_depth = 0
520 for die in cu.iter_DIEs():
521 if die.is_null():
522 die_depth -= 1
523 continue
524 self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
525 die_depth,
526 die.offset - section_offset,
527 die.abbrev_code,
528 die.tag))
529
530 for attr in die.attributes.itervalues():
531 self._emitline(' <%2x> %-18s: %s' % (
532 attr.offset - section_offset,
533 attr.name,
534 describe_attr_value(
535 attr, die, section_offset)))
536
537 if die.has_children:
538 die_depth += 1
539
540
541 def _emit(self, s=''):
542 """ Emit an object to output
543 """
544 self.output.write(str(s))
545
546 def _emitline(self, s=''):
547 """ Emit an object to output, followed by a newline
548 """
549 self.output.write(str(s) + '\n')
550
551
552 SCRIPT_DESCRIPTION = 'Display information about the contents of ELF format files'
553 VERSION_STRING = '%%prog: based on pyelftools %s' % __version__
554
555
556 def main():
557 # parse the command-line arguments and invoke ReadElf
558 optparser = OptionParser(
559 usage='usage: %prog [options] <elf-file>',
560 description=SCRIPT_DESCRIPTION,
561 add_help_option=False, # -h is a real option of readelf
562 prog='readelf.py',
563 version=VERSION_STRING)
564 optparser.add_option('-H', '--help',
565 action='store_true', dest='help',
566 help='Display this information')
567 optparser.add_option('-h', '--file-header',
568 action='store_true', dest='show_file_header',
569 help='Display the ELF file header')
570 optparser.add_option('-l', '--program-headers', '--segments',
571 action='store_true', dest='show_program_header',
572 help='Display the program headers')
573 optparser.add_option('-S', '--section-headers', '--sections',
574 action='store_true', dest='show_section_header',
575 help="Display the sections' headers")
576 optparser.add_option('-e', '--headers',
577 action='store_true', dest='show_all_headers',
578 help='Equivalent to: -h -l -S')
579 optparser.add_option('-s', '--symbols', '--syms',
580 action='store_true', dest='show_symbols',
581 help='Display the symbol table')
582 optparser.add_option('-r', '--relocs',
583 action='store_true', dest='show_relocs',
584 help='Display the relocations (if present)')
585 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')
588 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')
591 optparser.add_option('--debug-dump',
592 action='store', dest='debug_dump_section', metavar='<section>',
593 help='Display the contents of DWARF debug sections')
594
595 options, args = optparser.parse_args()
596
597 if options.help or len(args) == 0:
598 optparser.print_help()
599 sys.exit(0)
600
601 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
608 with open(args[0], 'rb') as file:
609 try:
610 readelf = ReadElf(file, sys.stdout)
611 if do_file_header:
612 readelf.display_file_header()
613 if do_section_header:
614 readelf.display_section_headers(
615 show_heading=not do_file_header)
616 if do_program_header:
617 readelf.display_program_headers(
618 show_heading=not do_file_header)
619 if options.show_symbols:
620 readelf.display_symbol_tables()
621 if options.show_relocs:
622 readelf.display_relocations()
623 if options.show_hex_dump:
624 readelf.display_hex_dump(options.show_hex_dump)
625 if options.show_string_dump:
626 readelf.display_string_dump(options.show_string_dump)
627 if options.debug_dump_section:
628 readelf.display_debug_dump(options.debug_dump_section)
629 except ELFError as ex:
630 sys.stderr.write('ELF error: %s\n' % ex)
631 sys.exit(1)
632
633
634 #-------------------------------------------------------------------------------
635 if __name__ == '__main__':
636 main()
637