blob: a09f014706c297d013fda07ad6da01e4f4c4aa61 [file] [log] [blame]
Eli Bendersky1a516a32011-12-22 15:22:00 +02001#-------------------------------------------------------------------------------
2# elftools example: elf_relocations.py
3#
4# An example of obtaining a relocation section from an ELF file and examining
5# the relocation entries it contains.
6#
7# Eli Bendersky (eliben@gmail.com)
8# This code is in the public domain
9#-------------------------------------------------------------------------------
10from __future__ import print_function
11import sys
12from elftools.elf.elffile import ELFFile
13from elftools.elf.relocation import RelocationSection
14
15
16def process_file(filename):
17 print('Processing file:', filename)
18 with open(filename) as f:
19 elffile = ELFFile(f)
20
21 # Read the .rela.dyn section from the file, by explicitly asking
22 # ELFFile for this section
23 reladyn_name = '.rela.dyn'
24 reladyn = elffile.get_section_by_name(reladyn_name)
25
26 if not isinstance(reladyn, RelocationSection):
27 print(' The file has no %s section' % reladyn_name)
28
29 print(' %s section with %s relocations' % (
30 reladyn_name, reladyn.num_relocations()))
31
32 for reloc in reladyn.iter_relocations():
33 # Use the Relocation's object ability to pretty-print itself to a
34 # string to examine it
35 print(' ', reloc)
36
37 # Relocation entry attributes are available through item lookup
38 print(' offset = %s' % reloc['r_offset'])
39
40
41if __name__ == '__main__':
42 for filename in sys.argv[1:]:
43 process_file(filename)
44
45
46