From: david942j Date: Fri, 13 Oct 2017 02:56:29 +0000 (+0800) Subject: Fix - ELFFile#address_offsets should consider PT_LOAD only (#159) X-Git-Tag: v0.25~30 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e3e73ccc967c77f283362e2381ba09224ce24ac7;p=pyelftools.git Fix - ELFFile#address_offsets should consider PT_LOAD only (#159) * fix - ELFFile#address_offsets should consider PT_LOAD only --- 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()