Fix for mixed version loclists, tests (#521)
[pyelftools.git] / examples / elf_notes.py
1 #-------------------------------------------------------------------------------
2 # elftools example: elf_notes.py
3 #
4 # An example of obtaining note sections from an ELF file and examining
5 # the notes it contains.
6 #
7 # Eli Bendersky (eliben@gmail.com)
8 # This code is in the public domain
9 #-------------------------------------------------------------------------------
10 from __future__ import print_function
11 import sys
12
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] = ['.', '..']
16
17 from elftools.elf.elffile import ELFFile
18 from elftools.elf.sections import NoteSection
19 from elftools.common.utils import bytes2hex
20
21
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):
27 continue
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'])
33 desc = note['n_desc']
34 if note['n_type'] == 'NT_GNU_ABI_TAG':
35 print(' Desc: %s, ABI: %d.%d.%d' % (
36 desc['abi_os'],
37 desc['abi_major'],
38 desc['abi_minor'],
39 desc['abi_tiny']))
40 elif note['n_type'] in {'NT_GNU_BUILD_ID', 'NT_GNU_GOLD_VERSION'}:
41 print(' Desc:', desc)
42 else:
43 print(' Desc:', bytes2hex(desc))
44
45
46 if __name__ == '__main__':
47 if sys.argv[1] == '--test':
48 for filename in sys.argv[2:]:
49 process_file(filename)