Fix formatting of some dynamic tag fields to match readelf
[pyelftools.git] / examples / elf_show_debug_sections.py
1 #-------------------------------------------------------------------------------
2 # elftools example: elf_show_debug_sections.py
3 #
4 # Show the names of all .debug_* sections in ELF files.
5 #
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from __future__ import print_function
10 import sys
11
12 # If elftools is not installed, maybe we're running from the root or examples
13 # dir of the source distribution
14 try:
15 import elftools
16 except ImportError:
17 sys.path.extend(['.', '..'])
18
19 from elftools.common.py3compat import bytes2str
20 from elftools.elf.elffile import ELFFile
21
22
23 def process_file(filename):
24 print('In file:', filename)
25 with open(filename, 'rb') as f:
26 elffile = ELFFile(f)
27
28 for section in elffile.iter_sections():
29 # Section names are bytes objects
30 if section.name.startswith(b'.debug'):
31 print(' ' + bytes2str(section.name))
32
33
34 if __name__ == '__main__':
35 for filename in sys.argv[1:]:
36 process_file(filename)
37