From e3e73ccc967c77f283362e2381ba09224ce24ac7 Mon Sep 17 00:00:00 2001 From: david942j Date: Fri, 13 Oct 2017 10:56:29 +0800 Subject: [PATCH] Fix - ELFFile#address_offsets should consider PT_LOAD only (#159) * fix - ELFFile#address_offsets should consider PT_LOAD only --- elftools/elf/elffile.py | 3 +++ test/test_elffile.py | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/elftools/elf/elffile.py b/elftools/elf/elffile.py index 02a279c..584e016 100644 --- a/elftools/elf/elffile.py +++ b/elftools/elf/elffile.py @@ -136,6 +136,9 @@ class ELFFile(object): """ end = start + size for seg in self.iter_segments(): + # consider LOAD only to prevent same address being yielded twice + if seg['p_type'] != 'PT_LOAD': + continue if (start >= seg['p_vaddr'] and end <= seg['p_vaddr'] + seg['p_filesz']): yield start - seg['p_vaddr'] + seg['p_offset'] diff --git a/test/test_elffile.py b/test/test_elffile.py index f23abf0..654b202 100644 --- a/test/test_elffile.py +++ b/test/test_elffile.py @@ -15,8 +15,9 @@ class TestMap(unittest.TestCase): __init__ = object.__init__ def iter_segments(self): return iter(( - dict(p_vaddr=0x10200, p_filesz=0x200, p_offset=0x100), - dict(p_vaddr=0x10100, p_filesz=0x100, p_offset=0x400), + dict(p_type='PT_PHDR', p_vaddr=0x10100, p_filesz=0x100, p_offset=0x400), + dict(p_type='PT_LOAD', p_vaddr=0x10200, p_filesz=0x200, p_offset=0x100), + dict(p_type='PT_LOAD', p_vaddr=0x10100, p_filesz=0x100, p_offset=0x400), )) elf = MockELF() -- 2.30.2