1 #-------------------------------------------------------------------------------
2 # elftools example: elfclass_address_size.py
4 # This example explores the ELF class (32 or 64-bit) and address size in each
5 # of the CUs in the DWARF information.
7 # Eli Bendersky (eliben@gmail.com)
8 # This code is in the public domain
9 #-------------------------------------------------------------------------------
10 from __future__
import print_function
13 # If pyelftools is not installed, the example can also run from the root or
14 # examples/ dir of the source distribution.
15 sys
.path
[0:0] = ['.', '..']
17 from elftools
.elf
.elffile
import ELFFile
20 def process_file(filename
):
21 with
open(filename
, 'rb') as f
:
23 # elfclass is a public attribute of ELFFile, read from its header
24 print('%s: elfclass is %s' % (filename
, elffile
.elfclass
))
26 if elffile
.has_dwarf_info():
27 dwarfinfo
= elffile
.get_dwarf_info()
28 for CU
in dwarfinfo
.iter_CUs():
29 # cu_offset is a public attribute of CU
30 # address_size is part of the CU header
31 print(' CU at offset 0x%x. address_size is %s' % (
32 CU
.cu_offset
, CU
['address_size']))
35 if __name__
== '__main__':
36 if sys
.argv
[1] == '--test':
37 for filename
in sys
.argv
[2:]:
38 process_file(filename
)