Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 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 | #------------------------------------------------------------------------------- |
| 10 | from __future__ import print_function |
| 11 | import sys |
Eli Bendersky | ce5d187 | 2011-12-22 20:03:06 +0200 | [diff] [blame] | 12 | |
| 13 | # If elftools is not installed, maybe we're running from the root or examples |
| 14 | # dir of the source distribution |
| 15 | try: |
| 16 | import elftools |
| 17 | except ImportError: |
| 18 | sys.path.extend(['.', '..']) |
| 19 | |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame^] | 20 | from elftools.common.py3compat import bytes2str |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 21 | from elftools.elf.elffile import ELFFile |
| 22 | from elftools.elf.relocation import RelocationSection |
| 23 | |
| 24 | |
| 25 | def process_file(filename): |
| 26 | print('Processing file:', filename) |
eli.bendersky | 3bd3ecc | 2012-01-11 15:56:41 +0200 | [diff] [blame] | 27 | with open(filename, 'rb') as f: |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 28 | elffile = ELFFile(f) |
| 29 | |
| 30 | # Read the .rela.dyn section from the file, by explicitly asking |
| 31 | # ELFFile for this section |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame^] | 32 | # Recall that section names are bytes objects |
| 33 | reladyn_name = b'.rela.dyn' |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 34 | reladyn = elffile.get_section_by_name(reladyn_name) |
| 35 | |
| 36 | if not isinstance(reladyn, RelocationSection): |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame^] | 37 | print(' The file has no %s section' % bytes2str(reladyn_name)) |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 38 | |
| 39 | print(' %s section with %s relocations' % ( |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame^] | 40 | bytes2str(reladyn_name), reladyn.num_relocations())) |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 41 | |
| 42 | for reloc in reladyn.iter_relocations(): |
Eli Bendersky | bd1a09f | 2012-01-26 06:49:19 +0200 | [diff] [blame] | 43 | print(' Relocation (%s)' % 'RELA' if reloc.is_RELA() else 'REL') |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 44 | # Relocation entry attributes are available through item lookup |
Eli Bendersky | bd1a09f | 2012-01-26 06:49:19 +0200 | [diff] [blame] | 45 | print(' offset = %s' % reloc['r_offset']) |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 46 | |
| 47 | |
| 48 | if __name__ == '__main__': |
| 49 | for filename in sys.argv[1:]: |
| 50 | process_file(filename) |
| 51 | |