Fix - ELFFile#address_offsets should consider PT_LOAD only (#159)
authordavid942j <david942j@users.noreply.github.com>
Fri, 13 Oct 2017 02:56:29 +0000 (10:56 +0800)
committerEli Bendersky <eliben@users.noreply.github.com>
Fri, 13 Oct 2017 02:56:29 +0000 (19:56 -0700)
* fix - ELFFile#address_offsets should consider PT_LOAD only

elftools/elf/elffile.py
test/test_elffile.py

index 02a279c5abefd28d3cc2eb112826b7403b6c4450..584e016b3384d5e590949c37af1bc49224b0f76c 100644 (file)
@@ -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']
index f23abf0a33663bffe5e531052a493c9fc0805ac6..654b2021f76196f47312e5da90ba16c2ba955f22 100644 (file)
@@ -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()