From 5f9416fa6b6da2b4a7b5ed7305d012ab71b1fb30 Mon Sep 17 00:00:00 2001 From: Anders Dellien Date: Thu, 20 Dec 2018 14:21:35 +0100 Subject: [PATCH] Simplify handling of null DIEs (#209) The code that is intended to coalesce null DIEs into the DIE that precedes them does not do that and is actually not needed as the 'unflattening' procedure takes care of any unexpected null DIEs. Also added a unit test for verifying the DIE size calculation. --- elftools/dwarf/die.py | 13 ------- test/test_die_size.py | 32 ++++++++++++++++++ .../trailing_null_dies.elf | Bin 0 -> 330 bytes 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 test/test_die_size.py create mode 100644 test/testfiles_for_unittests/trailing_null_dies.elf diff --git a/elftools/dwarf/die.py b/elftools/dwarf/die.py index 546d000..5d3ad99 100755 --- a/elftools/dwarf/die.py +++ b/elftools/dwarf/die.py @@ -188,19 +188,6 @@ class DIE(object): raw_value=raw_value, offset=attr_offset) - # Count and then consume any null termination bytes to avoid wrong die - # size calculation. - num_zero_terminators = 0 - with preserve_stream_pos(self.stream): - while True: - if self.stream.read(1) == 0: - num_zero_terminators += 1 - else: - break - if num_zero_terminators > 0: - # There was at least one zero termination -> consume all of them. - self.stream.read(num_zero_terminators) - self.size = self.stream.tell() - self.offset def _translate_attr_value(self, form, raw_value): diff --git a/test/test_die_size.py b/test/test_die_size.py new file mode 100644 index 0000000..7579ce2 --- /dev/null +++ b/test/test_die_size.py @@ -0,0 +1,32 @@ +#------------------------------------------------------------------------------ +# elftools tests +# +# Anders Dellien (anders@andersdellien.se) +# This code is in the public domain +#------------------------------------------------------------------------------ +import unittest +import os + +from elftools.elf.elffile import ELFFile + +class TestDieSize(unittest.TestCase): + """ This test verifies that null DIEs are treated correctly - i.e. + removed when we 'unflatten' the linear list and build a tree. + The test file contains a CU with two non-null DIEs (both three bytes big), + where the second one is followed by three null DIEs. + We verify that the null DIEs are discarded and that the length of the second DIE + does not include the null entries that follow it. + """ + def test_die_size(self): + with open(os.path.join('test', + 'testfiles_for_unittests', 'trailing_null_dies.elf'), + 'rb') as f: + elffile = ELFFile(f) + self.assertTrue(elffile.has_dwarf_info()) + dwarfinfo = elffile.get_dwarf_info() + for CU in dwarfinfo.iter_CUs(): + for child in CU.get_top_DIE().iter_children(): + self.assertEquals(child.size, 3) + +if __name__ == '__main__': + unittest.main() diff --git a/test/testfiles_for_unittests/trailing_null_dies.elf b/test/testfiles_for_unittests/trailing_null_dies.elf new file mode 100644 index 0000000000000000000000000000000000000000..1bc7f5eeb312169c21252b0d94575e4cc2f52d60 GIT binary patch literal 330 zcmb<-^>JfjWMqH=CI&kOFptFnCIqIDc@7K~KrsgfW(Fn(E+7k`8>|#e7()fc5kg=V zx>_-yD7qL(p$b%xAFK;OU{lMCkVdee3RMsyU>3T5z2c1GlA@BtBnG{d)TGk%_{_Yt rd>A`1DXA#6jDb;*k(q;mfk}k{L@@{hF%#G@4n{`?CMO0aXAlnn_w*6J literal 0 HcmV?d00001 -- 2.30.2