added name field to AttributeValue
[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._emitline(' W (write), A (alloc), X (execute), M (merge), S (strings)')
239 self._emitline(' I (info), L (link order), G (group), x (unknown)')
240 self._emitline(' O (extra OS processing required) o (OS specific), p (processor specific)')
241
242 def display_symbol_tables(self):
243 """ Display the symbol tables contained in the file
244 """
245 for section in self.elffile.iter_sections():
246 if not isinstance(section, SymbolTableSection):
247 continue
248
249 if section['sh_entsize'] == 0:
250 self._emitline("\nSymbol table '%s' has a sh_entsize of zero!" % (
251 section.name))
252 continue
253
254 self._emitline("\nSymbol table '%s' contains %s entries:" % (
255 section.name, section.num_symbols()))
256
257 if self.elffile.elfclass == 32:
258 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
259 else: # 64
260 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
261
262 for nsym, symbol in enumerate(section.iter_symbols()):
263 # symbol names are truncated to 25 chars, similarly to readelf
264 self._emitline('%6d: %s %5d %-7s %-6s %-7s %4s %.25s' % (
265 nsym,
266 self._format_hex(symbol['st_value'], fullhex=True, lead0x=False),
267 symbol['st_size'],
268 describe_symbol_type(symbol['st_info']['type']),
269 describe_symbol_bind(symbol['st_info']['bind']),
270 describe_symbol_visibility(symbol['st_other']['visibility']),
271 describe_symbol_shndx(symbol['st_shndx']),
272 symbol.name))
273
274 def display_relocations(self):
275 """ Display the relocations contained in the file
276 """
277 has_relocation_sections = False
278 for section in self.elffile.iter_sections():
279 if not isinstance(section, RelocationSection):
280 continue
281
282 has_relocation_sections = True
283 self._emitline("\nRelocation section '%s' at offset %s contains %s entries:" % (
284 section.name,
285 self._format_hex(section['sh_offset']),
286 section.num_relocations()))
287 if section.is_RELA():
288 self._emitline(" Offset Info Type Sym. Value Sym. Name + Addend")
289 else:
290 self._emitline(" Offset Info Type Sym.Value Sym. Name")
291
292 # The symbol table section pointed to in sh_link
293 symtable = self.elffile.get_section(section['sh_link'])
294
295 for rel in section.iter_relocations():
296 hexwidth = 8 if self.elffile.elfclass == 32 else 12
297 self._emit('%s %s %-17.17s' % (
298 self._format_hex(rel['r_offset'],
299 fieldsize=hexwidth, lead0x=False),
300 self._format_hex(rel['r_info'],
301 fieldsize=hexwidth, lead0x=False),
302 describe_reloc_type(
303 rel['r_info_type'], self.elffile['e_machine'])))
304
305 if rel['r_info_sym'] == 0:
306 self._emitline()
307 continue
308
309 symbol = symtable.get_symbol(rel['r_info_sym'])
310 # Some symbols have zero 'st_name', so instead what's used is
311 # the name of the section they point at
312 if symbol['st_name'] == 0:
313 symsec = self.elffile.get_section(symbol['st_shndx'])
314 symbol_name = symsec.name
315 else:
316 symbol_name = symbol.name
317 self._emit(' %s %s%22.22s' % (
318 self._format_hex(
319 symbol['st_value'],
320 fullhex=True, lead0x=False),
321 ' ' if self.elffile.elfclass == 32 else '',
322 symbol_name))
323 if section.is_RELA():
324 self._emit(' %s %x' % (
325 '+' if rel['r_addend'] >= 0 else '-',
326 abs(rel['r_addend'])))
327 self._emitline()
328
329 if not has_relocation_sections:
330 self._emitline('\nThere are no relocations in this file.')
331
332 def display_hex_dump(self, section_spec):
333 """ Display a hex dump of a section. section_spec is either a section
334 number or a name.
335 """
336 section = self._section_from_spec(section_spec)
337 if section is None:
338 self._emitline("Section '%s' does not exist in the file!" % (
339 section_spec))
340 return
341
342 self._emitline("\nHex dump of section '%s':" % section.name)
343 self._note_relocs_for_section(section)
344 addr = section['sh_addr']
345 data = section.data()
346 dataptr = 0
347
348 while dataptr < len(data):
349 bytesleft = len(data) - dataptr
350 # chunks of 16 bytes per line
351 linebytes = 16 if bytesleft > 16 else bytesleft
352
353 self._emit(' %s ' % self._format_hex(addr, fieldsize=8))
354 for i in range(16):
355 if i < linebytes:
356 self._emit('%2.2x' % ord(data[dataptr + i]))
357 else:
358 self._emit(' ')
359 if i % 4 == 3:
360 self._emit(' ')
361
362 for i in range(linebytes):
363 c = data[dataptr + i]
364 if c >= ' ' and ord(c) < 0x7f:
365 self._emit(c)
366 else:
367 self._emit('.')
368
369 self._emitline()
370 addr += linebytes
371 dataptr += linebytes
372
373 self._emitline()
374
375 def display_string_dump(self, section_spec):
376 """ Display a strings dump of a section. section_spec is either a
377 section number or a name.
378 """
379 section = self._section_from_spec(section_spec)
380 if section is None:
381 self._emitline("Section '%s' does not exist in the file!" % (
382 section_spec))
383 return
384
385 printables = set(string.printable)
386 self._emitline("\nString dump of section '%s':" % section.name)
387
388 found = False
389 data = section.data()
390 dataptr = 0
391
392 while dataptr < len(data):
393 while dataptr < len(data) and data[dataptr] not in printables:
394 dataptr += 1
395
396 if dataptr >= len(data):
397 break
398
399 endptr = dataptr
400 while endptr < len(data) and data[endptr] != '\x00':
401 endptr += 1
402
403 found = True
404 self._emitline(' [%6x] %s' % (
405 dataptr, data[dataptr:endptr]))
406
407 dataptr = endptr
408
409 if not found:
410 self._emitline(' No strings found in this section.')
411 else:
412 self._emitline()
413
414 def display_debug_dump(self, section_name):
415 """ Dump a DWARF section
416 """
417 self._init_dwarfinfo()
418 if self._dwarfinfo is None:
419 return
420
421 if section_name == 'info':
422 self._dump_debug_info()
423 else:
424 self._emitline('debug dump not yet supported for "%s"' % section_name)
425
426 def _format_hex(self, addr, fieldsize=None, fullhex=False, lead0x=True):
427 """ Format an address into a hexadecimal string.
428
429 fieldsize:
430 Size of the hexadecimal field (with leading zeros to fit the
431 address into. For example with fieldsize=8, the format will
432 be %08x
433 If None, the minimal required field size will be used.
434
435 fullhex:
436 If True, override fieldsize to set it to the maximal size
437 needed for the elfclass
438
439 lead0x:
440 If True, leading 0x is added
441 """
442 s = '0x' if lead0x else ''
443 if fullhex:
444 fieldsize = 8 if self.elffile.elfclass == 32 else 16
445 if fieldsize is None:
446 field = '%x'
447 else:
448 field = '%' + '0%sx' % fieldsize
449 return s + field % addr
450
451 def _section_from_spec(self, spec):
452 """ Retrieve a section given a "spec" (either number or name).
453 Return None if no such section exists in the file.
454 """
455 try:
456 num = int(spec)
457 if num < self.elffile.num_sections():
458 return self.elffile.get_section(num)
459 else:
460 return None
461 except ValueError:
462 # Not a number. Must be a name then
463 return self.elffile.get_section_by_name(spec)
464
465 def _note_relocs_for_section(self, section):
466 """ If there are relocation sections pointing to the givne section,
467 emit a note about it.
468 """
469 for relsec in self.elffile.iter_sections():
470 if isinstance(relsec, RelocationSection):
471 info_idx = relsec['sh_info']
472 if self.elffile.get_section(info_idx) == section:
473 self._emitline(' Note: This section has relocations against it, but these have NOT been applied to this dump.')
474 return
475
476 def _init_dwarfinfo(self):
477 """ Initialize the DWARF info contained in the file and assign it to
478 self._dwarfinfo.
479 Leave self._dwarfinfo at None if no DWARF info was found in the file
480 """
481 if self._dwarfinfo is not None:
482 return
483
484 if self.elffile.has_dwarf_info():
485 self._dwarfinfo = self.elffile.get_dwarf_info()
486 else:
487 self._dwarfinfo = None
488
489 def _dump_debug_info(self):
490 """ Dump the debugging info section.
491 """
492 # Offset of the .debug_info section in the stream
493 section_offset = self._dwarfinfo.debug_info_loc.offset
494
495 for cu in self._dwarfinfo.iter_CUs():
496 self._emitline(' Compilation Unit @ offset %s' %
497 self._format_hex(cu.cu_offset - section_offset))
498 self._emitline(' Length: %s (%s)' % (
499 self._format_hex(cu['unit_length']),
500 '%s-bit' % cu.dwarf_format()))
501 self._emitline(' Version: %s' % cu['version']),
502 self._emitline(' Abbrev Offset: %s' % cu['debug_abbrev_offset']),
503 self._emitline(' Pointer Size: %s' % cu['address_size'])
504
505 # The nesting depth of each DIE within the tree of DIEs must be
506 # displayed. To implement this, a counter is incremented each time
507 # the current DIE has children, and decremented when a null die is
508 # encountered. Due to the way the DIE tree is serialized, this will
509 # correctly reflect the nesting depth
510 #
511 die_depth = 0
512 for die in cu.iter_DIEs():
513 if die.is_null():
514 die_depth -= 1
515 continue
516 self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
517 die_depth,
518 die.offset - section_offset,
519 die.abbrev_code,
520 die.tag))
521
522 for attrname, attr in die.attributes.iteritems():
523 self._emitline(' <%2x> %-18s: %s' % (
524 attr.offset - section_offset,
525 attrname,
526 describe_attr_value(
527 attr, die, section_offset)))
528
529 if die.has_children:
530 die_depth += 1
531
532
533 def _emit(self, s=''):
534 """ Emit an object to output
535 """
536 self.output.write(str(s))
537
538 def _emitline(self, s=''):
539 """ Emit an object to output, followed by a newline
540 """
541 self.output.write(str(s) + '\n')
542
543
544 SCRIPT_DESCRIPTION = 'Display information about the contents of ELF format files'
545 VERSION_STRING = '%%prog: based on pyelftools %s' % __version__
546
547
548 def main():
549 # parse the command-line arguments and invoke ReadElf
550 optparser = OptionParser(
551 usage='usage: %prog [options] <elf-file>',
552 description=SCRIPT_DESCRIPTION,
553 add_help_option=False, # -h is a real option of readelf
554 prog='readelf.py',
555 version=VERSION_STRING)
556 optparser.add_option('-H', '--help',
557 action='store_true', dest='help',
558 help='Display this information')
559 optparser.add_option('-h', '--file-header',
560 action='store_true', dest='show_file_header',
561 help='Display the ELF file header')
562 optparser.add_option('-l', '--program-headers', '--segments',
563 action='store_true', dest='show_program_header',
564 help='Display the program headers')
565 optparser.add_option('-S', '--section-headers', '--sections',
566 action='store_true', dest='show_section_header',
567 help="Display the sections' headers")
568 optparser.add_option('-e', '--headers',
569 action='store_true', dest='show_all_headers',
570 help='Equivalent to: -h -l -S')
571 optparser.add_option('-s', '--symbols', '--syms',
572 action='store_true', dest='show_symbols',
573 help='Display the symbol table')
574 optparser.add_option('-r', '--relocs',
575 action='store_true', dest='show_relocs',
576 help='Display the relocations (if present)')
577 optparser.add_option('-x', '--hex-dump',
578 action='store', dest='show_hex_dump', metavar='<number|name>',
579 help='Dump the contents of section <number|name> as bytes')
580 optparser.add_option('-p', '--string-dump',
581 action='store', dest='show_string_dump', metavar='<number|name>',
582 help='Dump the contents of section <number|name> as strings')
583 optparser.add_option('--debug-dump',
584 action='store', dest='debug_dump_section', metavar='<section>',
585 help='Display the contents of DWARF debug sections')
586
587 options, args = optparser.parse_args()
588
589 if options.help or len(args) == 0:
590 optparser.print_help()
591 sys.exit(0)
592
593 if options.show_all_headers:
594 do_file_header = do_section_header = do_program_header = True
595 else:
596 do_file_header = options.show_file_header
597 do_section_header = options.show_section_header
598 do_program_header = options.show_program_header
599
600 with open(args[0], 'rb') as file:
601 try:
602 readelf = ReadElf(file, sys.stdout)
603 if do_file_header:
604 readelf.display_file_header()
605 if do_section_header:
606 readelf.display_section_headers(
607 show_heading=not do_file_header)
608 if do_program_header:
609 readelf.display_program_headers(
610 show_heading=not do_file_header)
611 if options.show_symbols:
612 readelf.display_symbol_tables()
613 if options.show_relocs:
614 readelf.display_relocations()
615 if options.show_hex_dump:
616 readelf.display_hex_dump(options.show_hex_dump)
617 if options.show_string_dump:
618 readelf.display_string_dump(options.show_string_dump)
619 if options.debug_dump_section:
620 readelf.display_debug_dump(options.debug_dump_section)
621 except ELFError as ex:
622 sys.stderr.write('ELF error: %s\n' % ex)
623 sys.exit(1)
624
625
626 #-------------------------------------------------------------------------------
627 if __name__ == '__main__':
628 main()
629