readelf --debug-dump=frames-interp initial implementation completed. some tests vs...
[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 from itertools import ifilter
13 import string
14
15
16 # If elftools is not installed, maybe we're running from the root or scripts
17 # dir of the source distribution
18 #
19 try:
20 import elftools
21 except ImportError:
22 sys.path.extend(['.', '..'])
23
24 from elftools import __version__
25 from elftools.common.exceptions import ELFError
26 from elftools.elf.elffile import ELFFile
27 from elftools.elf.segments import InterpSegment
28 from elftools.elf.sections import SymbolTableSection
29 from elftools.elf.relocation import RelocationSection
30 from elftools.elf.descriptions import (
31 describe_ei_class, describe_ei_data, describe_ei_version,
32 describe_ei_osabi, describe_e_type, describe_e_machine,
33 describe_e_version_numeric, describe_p_type, describe_p_flags,
34 describe_sh_type, describe_sh_flags,
35 describe_symbol_type, describe_symbol_bind, describe_symbol_visibility,
36 describe_symbol_shndx, describe_reloc_type,
37 )
38 from elftools.dwarf.dwarfinfo import DWARFInfo
39 from elftools.dwarf.descriptions import (
40 describe_reg_name, describe_attr_value, set_global_machine_arch,
41 describe_CFI_instructions, describe_CFI_register_rule,
42 describe_CFI_CFA_rule,
43 )
44 from elftools.dwarf.constants import (
45 DW_LNS_copy, DW_LNS_set_file, DW_LNE_define_file)
46 from elftools.dwarf.callframe import CIE, FDE
47
48
49 class ReadElf(object):
50 """ display_* methods are used to emit output into the output stream
51 """
52 def __init__(self, file, output):
53 """ file:
54 stream object with the ELF file to read
55
56 output:
57 output stream to write to
58 """
59 self.elffile = ELFFile(file)
60 self.output = output
61
62 # Lazily initialized if a debug dump is requested
63 self._dwarfinfo = None
64
65 def display_file_header(self):
66 """ Display the ELF file header
67 """
68 self._emitline('ELF Header:')
69 self._emit(' Magic: ')
70 self._emitline(' '.join('%2.2x' % ord(b)
71 for b in self.elffile.e_ident_raw))
72 header = self.elffile.header
73 e_ident = header['e_ident']
74 self._emitline(' Class: %s' %
75 describe_ei_class(e_ident['EI_CLASS']))
76 self._emitline(' Data: %s' %
77 describe_ei_data(e_ident['EI_DATA']))
78 self._emitline(' Version: %s' %
79 describe_ei_version(e_ident['EI_VERSION']))
80 self._emitline(' OS/ABI: %s' %
81 describe_ei_osabi(e_ident['EI_OSABI']))
82 self._emitline(' ABI Version: %d' %
83 e_ident['EI_ABIVERSION'])
84 self._emitline(' Type: %s' %
85 describe_e_type(header['e_type']))
86 self._emitline(' Machine: %s' %
87 describe_e_machine(header['e_machine']))
88 self._emitline(' Version: %s' %
89 describe_e_version_numeric(header['e_version']))
90 self._emitline(' Entry point address: %s' %
91 self._format_hex(header['e_entry']))
92 self._emit(' Start of program headers: %s' %
93 header['e_phoff'])
94 self._emitline(' (bytes into file)')
95 self._emit(' Start of section headers: %s' %
96 header['e_shoff'])
97 self._emitline(' (bytes into file)')
98 self._emitline(' Flags: %s' %
99 self._format_hex(header['e_flags']))
100 self._emitline(' Size of this header: %s (bytes)' %
101 header['e_ehsize'])
102 self._emitline(' Size of program headers: %s (bytes)' %
103 header['e_phentsize'])
104 self._emitline(' Number of program headers: %s' %
105 header['e_phnum'])
106 self._emitline(' Size of section headers: %s (bytes)' %
107 header['e_shentsize'])
108 self._emitline(' Number of section headers: %s' %
109 header['e_shnum'])
110 self._emitline(' Section header string table index: %s' %
111 header['e_shstrndx'])
112
113 def display_program_headers(self, show_heading=True):
114 """ Display the ELF program headers.
115 If show_heading is True, displays the heading for this information
116 (Elf file type is...)
117 """
118 self._emitline()
119 if self.elffile.num_segments() == 0:
120 self._emitline('There are no program headers in this file.')
121 return
122
123 elfheader = self.elffile.header
124 if show_heading:
125 self._emitline('Elf file type is %s' %
126 describe_e_type(elfheader['e_type']))
127 self._emitline('Entry point is %s' %
128 self._format_hex(elfheader['e_entry']))
129 # readelf weirness - why isn't e_phoff printed as hex? (for section
130 # headers, it is...)
131 self._emitline('There are %s program headers, starting at offset %s' % (
132 elfheader['e_phnum'], elfheader['e_phoff']))
133 self._emitline()
134
135 self._emitline('Program Headers:')
136
137 # Now comes the table of program headers with their attributes. Note
138 # that due to different formatting constraints of 32-bit and 64-bit
139 # addresses, there are some conditions on elfclass here.
140 #
141 # First comes the table heading
142 #
143 if self.elffile.elfclass == 32:
144 self._emitline(' Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align')
145 else:
146 self._emitline(' Type Offset VirtAddr PhysAddr')
147 self._emitline(' FileSiz MemSiz Flags Align')
148
149 # Now the entries
150 #
151 for segment in self.elffile.iter_segments():
152 self._emit(' %-14s ' % describe_p_type(segment['p_type']))
153
154 if self.elffile.elfclass == 32:
155 self._emitline('%s %s %s %s %s %-3s %s' % (
156 self._format_hex(segment['p_offset'], fieldsize=6),
157 self._format_hex(segment['p_vaddr'], fullhex=True),
158 self._format_hex(segment['p_paddr'], fullhex=True),
159 self._format_hex(segment['p_filesz'], fieldsize=5),
160 self._format_hex(segment['p_memsz'], fieldsize=5),
161 describe_p_flags(segment['p_flags']),
162 self._format_hex(segment['p_align'])))
163 else: # 64
164 self._emitline('%s %s %s' % (
165 self._format_hex(segment['p_offset'], fullhex=True),
166 self._format_hex(segment['p_vaddr'], fullhex=True),
167 self._format_hex(segment['p_paddr'], fullhex=True)))
168 self._emitline(' %s %s %-3s %s' % (
169 self._format_hex(segment['p_filesz'], fullhex=True),
170 self._format_hex(segment['p_memsz'], fullhex=True),
171 describe_p_flags(segment['p_flags']),
172 # lead0x set to False for p_align, to mimic readelf.
173 # No idea why the difference from 32-bit mode :-|
174 self._format_hex(segment['p_align'], lead0x=False)))
175
176 if isinstance(segment, InterpSegment):
177 self._emitline(' [Requesting program interpreter: %s]' %
178 segment.get_interp_name())
179
180 # Sections to segments mapping
181 #
182 if self.elffile.num_sections() == 0:
183 # No sections? We're done
184 return
185
186 self._emitline('\n Section to Segment mapping:')
187 self._emitline(' Segment Sections...')
188
189 for nseg, segment in enumerate(self.elffile.iter_segments()):
190 self._emit(' %2.2d ' % nseg)
191
192 for section in self.elffile.iter_sections():
193 if ( not section.is_null() and
194 segment.section_in_segment(section)):
195 self._emit('%s ' % section.name)
196
197 self._emitline('')
198
199 def display_section_headers(self, show_heading=True):
200 """ Display the ELF section headers
201 """
202 elfheader = self.elffile.header
203 if show_heading:
204 self._emitline('There are %s section headers, starting at offset %s' % (
205 elfheader['e_shnum'], self._format_hex(elfheader['e_shoff'])))
206
207 self._emitline('\nSection Header%s:' % (
208 's' if elfheader['e_shnum'] > 1 else ''))
209
210 # Different formatting constraints of 32-bit and 64-bit addresses
211 #
212 if self.elffile.elfclass == 32:
213 self._emitline(' [Nr] Name Type Addr Off Size ES Flg Lk Inf Al')
214 else:
215 self._emitline(' [Nr] Name Type Address Offset')
216 self._emitline(' Size EntSize Flags Link Info Align')
217
218 # Now the entries
219 #
220 for nsec, section in enumerate(self.elffile.iter_sections()):
221 self._emit(' [%2u] %-17.17s %-15.15s ' % (
222 nsec, section.name, describe_sh_type(section['sh_type'])))
223
224 if self.elffile.elfclass == 32:
225 self._emitline('%s %s %s %s %3s %2s %3s %2s' % (
226 self._format_hex(section['sh_addr'], fieldsize=8, lead0x=False),
227 self._format_hex(section['sh_offset'], fieldsize=6, lead0x=False),
228 self._format_hex(section['sh_size'], fieldsize=6, lead0x=False),
229 self._format_hex(section['sh_entsize'], fieldsize=2, lead0x=False),
230 describe_sh_flags(section['sh_flags']),
231 section['sh_link'], section['sh_info'],
232 section['sh_addralign']))
233 else: # 64
234 self._emitline(' %s %s' % (
235 self._format_hex(section['sh_addr'], fullhex=True, lead0x=False),
236 self._format_hex(section['sh_offset'],
237 fieldsize=16 if section['sh_offset'] > 0xffffffff else 8,
238 lead0x=False)))
239 self._emitline(' %s %s %3s %2s %3s %s' % (
240 self._format_hex(section['sh_size'], fullhex=True, lead0x=False),
241 self._format_hex(section['sh_entsize'], fullhex=True, lead0x=False),
242 describe_sh_flags(section['sh_flags']),
243 section['sh_link'], section['sh_info'],
244 section['sh_addralign']))
245
246 self._emitline('Key to Flags:')
247 self._emit(' W (write), A (alloc), X (execute), M (merge), S (strings)')
248 if self.elffile['e_machine'] in ('EM_X86_64', 'EM_L10M'):
249 self._emitline(', l (large)')
250 else:
251 self._emitline()
252 self._emitline(' I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)')
253 self._emitline(' O (extra OS processing required) o (OS specific), p (processor specific)')
254
255 def display_symbol_tables(self):
256 """ Display the symbol tables contained in the file
257 """
258 for section in self.elffile.iter_sections():
259 if not isinstance(section, SymbolTableSection):
260 continue
261
262 if section['sh_entsize'] == 0:
263 self._emitline("\nSymbol table '%s' has a sh_entsize of zero!" % (
264 section.name))
265 continue
266
267 self._emitline("\nSymbol table '%s' contains %s entries:" % (
268 section.name, section.num_symbols()))
269
270 if self.elffile.elfclass == 32:
271 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
272 else: # 64
273 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
274
275 for nsym, symbol in enumerate(section.iter_symbols()):
276 # symbol names are truncated to 25 chars, similarly to readelf
277 self._emitline('%6d: %s %5d %-7s %-6s %-7s %4s %.25s' % (
278 nsym,
279 self._format_hex(symbol['st_value'], fullhex=True, lead0x=False),
280 symbol['st_size'],
281 describe_symbol_type(symbol['st_info']['type']),
282 describe_symbol_bind(symbol['st_info']['bind']),
283 describe_symbol_visibility(symbol['st_other']['visibility']),
284 describe_symbol_shndx(symbol['st_shndx']),
285 symbol.name))
286
287 def display_relocations(self):
288 """ Display the relocations contained in the file
289 """
290 has_relocation_sections = False
291 for section in self.elffile.iter_sections():
292 if not isinstance(section, RelocationSection):
293 continue
294
295 has_relocation_sections = True
296 self._emitline("\nRelocation section '%s' at offset %s contains %s entries:" % (
297 section.name,
298 self._format_hex(section['sh_offset']),
299 section.num_relocations()))
300 if section.is_RELA():
301 self._emitline(" Offset Info Type Sym. Value Sym. Name + Addend")
302 else:
303 self._emitline(" Offset Info Type Sym.Value Sym. Name")
304
305 # The symbol table section pointed to in sh_link
306 symtable = self.elffile.get_section(section['sh_link'])
307
308 for rel in section.iter_relocations():
309 hexwidth = 8 if self.elffile.elfclass == 32 else 12
310 self._emit('%s %s %-17.17s' % (
311 self._format_hex(rel['r_offset'],
312 fieldsize=hexwidth, lead0x=False),
313 self._format_hex(rel['r_info'],
314 fieldsize=hexwidth, lead0x=False),
315 describe_reloc_type(
316 rel['r_info_type'], self.elffile)))
317
318 if rel['r_info_sym'] == 0:
319 self._emitline()
320 continue
321
322 symbol = symtable.get_symbol(rel['r_info_sym'])
323 # Some symbols have zero 'st_name', so instead what's used is
324 # the name of the section they point at
325 if symbol['st_name'] == 0:
326 symsec = self.elffile.get_section(symbol['st_shndx'])
327 symbol_name = symsec.name
328 else:
329 symbol_name = symbol.name
330 self._emit(' %s %s%22.22s' % (
331 self._format_hex(
332 symbol['st_value'],
333 fullhex=True, lead0x=False),
334 ' ' if self.elffile.elfclass == 32 else '',
335 symbol_name))
336 if section.is_RELA():
337 self._emit(' %s %x' % (
338 '+' if rel['r_addend'] >= 0 else '-',
339 abs(rel['r_addend'])))
340 self._emitline()
341
342 if not has_relocation_sections:
343 self._emitline('\nThere are no relocations in this file.')
344
345 def display_hex_dump(self, section_spec):
346 """ Display a hex dump of a section. section_spec is either a section
347 number or a name.
348 """
349 section = self._section_from_spec(section_spec)
350 if section is None:
351 self._emitline("Section '%s' does not exist in the file!" % (
352 section_spec))
353 return
354
355 self._emitline("\nHex dump of section '%s':" % section.name)
356 self._note_relocs_for_section(section)
357 addr = section['sh_addr']
358 data = section.data()
359 dataptr = 0
360
361 while dataptr < len(data):
362 bytesleft = len(data) - dataptr
363 # chunks of 16 bytes per line
364 linebytes = 16 if bytesleft > 16 else bytesleft
365
366 self._emit(' %s ' % self._format_hex(addr, fieldsize=8))
367 for i in range(16):
368 if i < linebytes:
369 self._emit('%2.2x' % ord(data[dataptr + i]))
370 else:
371 self._emit(' ')
372 if i % 4 == 3:
373 self._emit(' ')
374
375 for i in range(linebytes):
376 c = data[dataptr + i]
377 if c >= ' ' and ord(c) < 0x7f:
378 self._emit(c)
379 else:
380 self._emit('.')
381
382 self._emitline()
383 addr += linebytes
384 dataptr += linebytes
385
386 self._emitline()
387
388 def display_string_dump(self, section_spec):
389 """ Display a strings dump of a section. section_spec is either a
390 section number or a name.
391 """
392 section = self._section_from_spec(section_spec)
393 if section is None:
394 self._emitline("Section '%s' does not exist in the file!" % (
395 section_spec))
396 return
397
398 printables = set(string.printable)
399 self._emitline("\nString dump of section '%s':" % section.name)
400
401 found = False
402 data = section.data()
403 dataptr = 0
404
405 while dataptr < len(data):
406 while dataptr < len(data) and data[dataptr] not in printables:
407 dataptr += 1
408
409 if dataptr >= len(data):
410 break
411
412 endptr = dataptr
413 while endptr < len(data) and data[endptr] != '\x00':
414 endptr += 1
415
416 found = True
417 self._emitline(' [%6x] %s' % (
418 dataptr, data[dataptr:endptr]))
419
420 dataptr = endptr
421
422 if not found:
423 self._emitline(' No strings found in this section.')
424 else:
425 self._emitline()
426
427 def display_debug_dump(self, dump_what):
428 """ Dump a DWARF section
429 """
430 self._init_dwarfinfo()
431 if self._dwarfinfo is None:
432 return
433
434 set_global_machine_arch(self.elffile.get_machine_arch())
435
436 if dump_what == 'info':
437 self._dump_debug_info()
438 elif dump_what == 'decodedline':
439 self._dump_debug_line_programs()
440 elif dump_what == 'frames':
441 self._dump_debug_frames()
442 elif dump_what == 'frames-interp':
443 self._dump_debug_frames_interp()
444 else:
445 self._emitline('debug dump not yet supported for "%s"' % dump_what)
446
447 def _format_hex(self, addr, fieldsize=None, fullhex=False, lead0x=True):
448 """ Format an address into a hexadecimal string.
449
450 fieldsize:
451 Size of the hexadecimal field (with leading zeros to fit the
452 address into. For example with fieldsize=8, the format will
453 be %08x
454 If None, the minimal required field size will be used.
455
456 fullhex:
457 If True, override fieldsize to set it to the maximal size
458 needed for the elfclass
459
460 lead0x:
461 If True, leading 0x is added
462 """
463 s = '0x' if lead0x else ''
464 if fullhex:
465 fieldsize = 8 if self.elffile.elfclass == 32 else 16
466 if fieldsize is None:
467 field = '%x'
468 else:
469 field = '%' + '0%sx' % fieldsize
470 return s + field % addr
471
472 def _section_from_spec(self, spec):
473 """ Retrieve a section given a "spec" (either number or name).
474 Return None if no such section exists in the file.
475 """
476 try:
477 num = int(spec)
478 if num < self.elffile.num_sections():
479 return self.elffile.get_section(num)
480 else:
481 return None
482 except ValueError:
483 # Not a number. Must be a name then
484 return self.elffile.get_section_by_name(spec)
485
486 def _note_relocs_for_section(self, section):
487 """ If there are relocation sections pointing to the givne section,
488 emit a note about it.
489 """
490 for relsec in self.elffile.iter_sections():
491 if isinstance(relsec, RelocationSection):
492 info_idx = relsec['sh_info']
493 if self.elffile.get_section(info_idx) == section:
494 self._emitline(' Note: This section has relocations against it, but these have NOT been applied to this dump.')
495 return
496
497 def _init_dwarfinfo(self):
498 """ Initialize the DWARF info contained in the file and assign it to
499 self._dwarfinfo.
500 Leave self._dwarfinfo at None if no DWARF info was found in the file
501 """
502 if self._dwarfinfo is not None:
503 return
504
505 if self.elffile.has_dwarf_info():
506 self._dwarfinfo = self.elffile.get_dwarf_info()
507 else:
508 self._dwarfinfo = None
509
510 def _dump_debug_info(self):
511 """ Dump the debugging info section.
512 """
513 self._emitline('Contents of the .debug_info section:\n')
514
515 # Offset of the .debug_info section in the stream
516 section_offset = self._dwarfinfo.debug_info_sec.global_offset
517
518 for cu in self._dwarfinfo.iter_CUs():
519 self._emitline(' Compilation Unit @ offset %s:' %
520 self._format_hex(cu.cu_offset))
521 self._emitline(' Length: %s (%s)' % (
522 self._format_hex(cu['unit_length']),
523 '%s-bit' % cu.dwarf_format()))
524 self._emitline(' Version: %s' % cu['version']),
525 self._emitline(' Abbrev Offset: %s' % cu['debug_abbrev_offset']),
526 self._emitline(' Pointer Size: %s' % cu['address_size'])
527
528 # The nesting depth of each DIE within the tree of DIEs must be
529 # displayed. To implement this, a counter is incremented each time
530 # the current DIE has children, and decremented when a null die is
531 # encountered. Due to the way the DIE tree is serialized, this will
532 # correctly reflect the nesting depth
533 #
534 die_depth = 0
535 for die in cu.iter_DIEs():
536 if die.is_null():
537 die_depth -= 1
538 continue
539 self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
540 die_depth,
541 die.offset,
542 die.abbrev_code,
543 die.tag))
544
545 for attr in die.attributes.itervalues():
546 self._emitline(' <%2x> %-18s: %s' % (
547 attr.offset,
548 attr.name,
549 describe_attr_value(
550 attr, die, section_offset)))
551
552 if die.has_children:
553 die_depth += 1
554
555 self._emitline()
556
557 def _dump_debug_line_programs(self):
558 """ Dump the (decoded) line programs from .debug_line
559 The programs are dumped in the order of the CUs they belong to.
560 """
561 self._emitline('Decoded dump of debug contents of section .debug_line:\n')
562
563 for cu in self._dwarfinfo.iter_CUs():
564 lineprogram = self._dwarfinfo.line_program_for_CU(cu)
565
566 cu_filename = ''
567 if len(lineprogram['include_directory']) > 0:
568 cu_filename = '%s/%s' % (
569 lineprogram['include_directory'][0],
570 lineprogram['file_entry'][0].name)
571 else:
572 cu_filename = lineprogram['file_entry'][0].name
573
574 self._emitline('CU: %s:' % cu_filename)
575 self._emitline('File name Line number Starting address')
576
577 # Print each state's file, line and address information. For some
578 # instructions other output is needed to be compatible with
579 # readelf.
580 for entry in lineprogram.get_entries():
581 state = entry.state
582 if state is None:
583 # Special handling for commands that don't set a new state
584 if entry.command == DW_LNS_set_file:
585 file_entry = lineprogram['file_entry'][entry.args[0] - 1]
586 if file_entry.dir_index == 0:
587 # current directory
588 self._emitline('\n./%s:[++]' % (
589 file_entry.name))
590 else:
591 self._emitline('\n%s/%s:' % (
592 lineprogram['include_directory'][file_entry.dir_index - 1],
593 file_entry.name))
594 elif entry.command == DW_LNE_define_file:
595 self._emitline('%s:' % (
596 lineprogram['include_directory'][entry.args[0].dir_index]))
597 elif not state.end_sequence:
598 # readelf doesn't print the state after end_sequence
599 # instructions. I think it's a bug but to be compatible
600 # I don't print them too.
601 self._emitline('%-35s %11d %18s' % (
602 lineprogram['file_entry'][state.file - 1].name,
603 state.line,
604 '0' if state.address == 0 else
605 self._format_hex(state.address)))
606 if entry.command == DW_LNS_copy:
607 # Another readelf oddity...
608 self._emitline()
609
610 def _dump_debug_frames(self):
611 """ Dump the raw frame information from .debug_frame
612 """
613 if not self._dwarfinfo.has_CFI():
614 return
615 self._emitline('Contents of the .debug_frame section:')
616
617 for entry in self._dwarfinfo.CFI_entries():
618 if isinstance(entry, CIE):
619 self._emitline('\n%08x %08x %08x CIE' % (
620 entry.offset, entry['length'], entry['CIE_id']))
621 self._emitline(' Version: %d' % entry['version'])
622 self._emitline(' Augmentation: "%s"' % entry['augmentation'])
623 self._emitline(' Code alignment factor: %u' % entry['code_alignment_factor'])
624 self._emitline(' Data alignment factor: %d' % entry['data_alignment_factor'])
625 self._emitline(' Return address column: %d' % entry['return_address_register'])
626 self._emitline()
627 else: # FDE
628 self._emitline('\n%08x %08x %08x FDE cie=%08x pc=%08x..%08x' % (
629 entry.offset,
630 entry['length'],
631 entry['CIE_pointer'],
632 entry.cie.offset,
633 entry['initial_location'],
634 entry['initial_location'] + entry['address_range']))
635
636 self._emit(describe_CFI_instructions(entry))
637 self._emitline()
638
639 def _dump_debug_frames_interp(self):
640 """ Dump the interpreted (decoded) frame information from .debug_frame
641 """
642 if not self._dwarfinfo.has_CFI():
643 return
644
645 self._emitline('Contents of the .debug_frame section:')
646
647 for entry in self._dwarfinfo.CFI_entries():
648 if isinstance(entry, CIE):
649 self._emitline('\n%08x %08x %08x CIE "%s" cf=%d df=%d ra=%d' % (
650 entry.offset,
651 entry['length'],
652 entry['CIE_id'],
653 entry['augmentation'],
654 entry['code_alignment_factor'],
655 entry['data_alignment_factor'],
656 entry['return_address_register']))
657 ra_regnum = entry['return_address_register']
658 else: # FDE
659 self._emitline('\n%08x %08x %08x FDE cie=%08x pc=%08x..%08x' % (
660 entry.offset,
661 entry['length'],
662 entry['CIE_pointer'],
663 entry.cie.offset,
664 entry['initial_location'],
665 entry['initial_location'] + entry['address_range']))
666 ra_regnum = entry.cie['return_address_register']
667
668 # Print the heading row for the decoded table
669 self._emit(' LOC')
670 self._emit(' ' if entry.structs.address_size == 4 else ' ')
671 self._emit(' CFA ')
672
673 # Decode the table nad look at the registers it describes.
674 # We build reg_order here to match readelf's order. In particular,
675 # registers are sorted by their number, and the register matching
676 # ra_regnum is always listed last with a special heading.
677 decoded_table = entry.get_decoded()
678 reg_order = sorted(ifilter(
679 lambda r: r != ra_regnum,
680 decoded_table.reg_order))
681
682 # Headings for the registers
683 for regnum in reg_order:
684 self._emit('%-6s' % describe_reg_name(regnum))
685 self._emitline('ra ')
686
687 # Now include ra_regnum in reg_order to print its values similarly
688 # to the other registers.
689 reg_order.append(ra_regnum)
690 for line in decoded_table.table:
691 self._emit(self._format_hex(
692 line['pc'], fullhex=True, lead0x=False))
693 self._emit(' %-9s' % describe_CFI_CFA_rule(line['cfa']))
694
695 for regnum in reg_order:
696 if regnum in line:
697 s = describe_CFI_register_rule(line[regnum])
698 else:
699 s = 'u'
700 self._emit('%-6s' % s)
701 self._emitline()
702 self._emitline()
703
704 def _emit(self, s=''):
705 """ Emit an object to output
706 """
707 self.output.write(str(s))
708
709 def _emitline(self, s=''):
710 """ Emit an object to output, followed by a newline
711 """
712 self.output.write(str(s) + '\n')
713
714
715 SCRIPT_DESCRIPTION = 'Display information about the contents of ELF format files'
716 VERSION_STRING = '%%prog: based on pyelftools %s' % __version__
717
718
719 def main(stream=None):
720 # parse the command-line arguments and invoke ReadElf
721 optparser = OptionParser(
722 usage='usage: %prog [options] <elf-file>',
723 description=SCRIPT_DESCRIPTION,
724 add_help_option=False, # -h is a real option of readelf
725 prog='readelf.py',
726 version=VERSION_STRING)
727 optparser.add_option('-H', '--help',
728 action='store_true', dest='help',
729 help='Display this information')
730 optparser.add_option('-h', '--file-header',
731 action='store_true', dest='show_file_header',
732 help='Display the ELF file header')
733 optparser.add_option('-l', '--program-headers', '--segments',
734 action='store_true', dest='show_program_header',
735 help='Display the program headers')
736 optparser.add_option('-S', '--section-headers', '--sections',
737 action='store_true', dest='show_section_header',
738 help="Display the sections' headers")
739 optparser.add_option('-e', '--headers',
740 action='store_true', dest='show_all_headers',
741 help='Equivalent to: -h -l -S')
742 optparser.add_option('-s', '--symbols', '--syms',
743 action='store_true', dest='show_symbols',
744 help='Display the symbol table')
745 optparser.add_option('-r', '--relocs',
746 action='store_true', dest='show_relocs',
747 help='Display the relocations (if present)')
748 optparser.add_option('-x', '--hex-dump',
749 action='store', dest='show_hex_dump', metavar='<number|name>',
750 help='Dump the contents of section <number|name> as bytes')
751 optparser.add_option('-p', '--string-dump',
752 action='store', dest='show_string_dump', metavar='<number|name>',
753 help='Dump the contents of section <number|name> as strings')
754 optparser.add_option('--debug-dump',
755 action='store', dest='debug_dump_what', metavar='<section>',
756 help='Display the contents of DWARF debug sections')
757
758 options, args = optparser.parse_args()
759
760 if options.help or len(args) == 0:
761 optparser.print_help()
762 sys.exit(0)
763
764 if options.show_all_headers:
765 do_file_header = do_section_header = do_program_header = True
766 else:
767 do_file_header = options.show_file_header
768 do_section_header = options.show_section_header
769 do_program_header = options.show_program_header
770
771 with open(args[0], 'rb') as file:
772 try:
773 readelf = ReadElf(file, stream or sys.stdout)
774 if do_file_header:
775 readelf.display_file_header()
776 if do_section_header:
777 readelf.display_section_headers(
778 show_heading=not do_file_header)
779 if do_program_header:
780 readelf.display_program_headers(
781 show_heading=not do_file_header)
782 if options.show_symbols:
783 readelf.display_symbol_tables()
784 if options.show_relocs:
785 readelf.display_relocations()
786 if options.show_hex_dump:
787 readelf.display_hex_dump(options.show_hex_dump)
788 if options.show_string_dump:
789 readelf.display_string_dump(options.show_string_dump)
790 if options.debug_dump_what:
791 readelf.display_debug_dump(options.debug_dump_what)
792 except ELFError as ex:
793 sys.stderr.write('ELF error: %s\n' % ex)
794 sys.exit(1)
795
796
797 def profile_main():
798 # Run 'main' redirecting its output to readelfout.txt
799 # Saves profiling information in readelf.profile
800 PROFFILE = 'readelf.profile'
801 import cProfile
802 cProfile.run('main(open("readelfout.txt", "w"))', PROFFILE)
803
804 # Dig in some profiling stats
805 import pstats
806 p = pstats.Stats(PROFFILE)
807 p.sort_stats('cumulative').print_stats(25)
808
809
810 #-------------------------------------------------------------------------------
811 if __name__ == '__main__':
812 main()
813 #profile_main()
814
815