1 #-------------------------------------------------------------------------------
2 # elftools example: elf_notes.py
4 # An example of obtaining note sections from an ELF file and examining
5 # the notes it contains.
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
18 from elftools
.elf
.sections
import NoteSection
19 from elftools
.common
.utils
import bytes2hex
22 def process_file(filename
):
23 print('Processing file:', filename
)
24 with
open(filename
, 'rb') as f
:
25 for sect
in ELFFile(f
).iter_sections():
26 if not isinstance(sect
, NoteSection
):
28 print(' Note section "%s" at offset 0x%.8x with size %d' % (
29 sect
.name
, sect
.header
['sh_offset'], sect
.header
['sh_size']))
30 for note
in sect
.iter_notes():
31 print(' Name:', note
['n_name'])
32 print(' Type:', note
['n_type'])
34 if note
['n_type'] == 'NT_GNU_ABI_TAG':
35 print(' Desc: %s, ABI: %d.%d.%d' % (
40 elif note
['n_type'] in {'NT_GNU_BUILD_ID', 'NT_GNU_GOLD_VERSION'}:
43 print(' Desc:', bytes2hex(desc
))
46 if __name__
== '__main__':
47 if sys
.argv
[1] == '--test':
48 for filename
in sys
.argv
[2:]:
49 process_file(filename
)