From: Santhosh Kumar Mani Date: Thu, 23 Jan 2014 15:55:24 +0000 (+0530) Subject: Handle absence of .debug_ranges gracefully X-Git-Tag: v0.22~3^2 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=111b8f8b77974a966eb6dc1e0f08c1b224bbd183;p=pyelftools.git Handle absence of .debug_ranges gracefully --- diff --git a/elftools/dwarf/dwarfinfo.py b/elftools/dwarf/dwarfinfo.py index e5c0e71..f9d1e32 100644 --- a/elftools/dwarf/dwarfinfo.py +++ b/elftools/dwarf/dwarfinfo.py @@ -178,7 +178,11 @@ class DWARFInfo(object): """ Get a RangeLists object representing the .debug_ranges section of the DWARF data, or None if this section doesn't exist. """ - return RangeLists(self.debug_ranges_sec.stream, self.structs) + # ".debug_ranges" section is optional + if self.debug_ranges_sec == None: + return None + else: + return RangeLists(self.debug_ranges_sec.stream, self.structs) #------ PRIVATE ------# diff --git a/examples/dwarf_range_lists.py b/examples/dwarf_range_lists.py index fced6a6..f722d11 100644 --- a/examples/dwarf_range_lists.py +++ b/examples/dwarf_range_lists.py @@ -37,6 +37,9 @@ def process_file(filename): # The range lists are extracted by DWARFInfo from the .debug_ranges # section, and returned here as a RangeLists object. range_lists = dwarfinfo.range_lists() + if range_lists == None: + print(' file has no .debug_ranges section') + return for CU in dwarfinfo.iter_CUs(): # DWARFInfo allows to iterate over the compile units contained in diff --git a/test/test_dwarf_range_lists.py b/test/test_dwarf_range_lists.py new file mode 100644 index 0000000..1ebd0c7 --- /dev/null +++ b/test/test_dwarf_range_lists.py @@ -0,0 +1,38 @@ +#------------------------------------------------------------------------------- +# elftools tests +# +# Eli Bendersky (eliben@gmail.com), Santhosh Kumar Mani (santhoshmani@gmail.com) +# This code is in the public domain +#------------------------------------------------------------------------------- +try: + import unittest2 as unittest +except ImportError: + import unittest +import os + +from utils import setup_syspath; setup_syspath() +from elftools.elf.elffile import ELFFile + +class TestRangeLists(unittest.TestCase): + # Test the absence of .debug_ranges section + def test_range_list_absence(self): + with open(os.path.join('test', 'testfiles_for_unittests', + 'arm_with_form_indirect.elf'), 'rb') as f: + elffile = ELFFile(f) + self.assertTrue(elffile.has_dwarf_info()) + + dwarfinfo = elffile.get_dwarf_info() + self.assertEqual(dwarfinfo.range_lists(), None) + + # Test the presence of .debug_ranges section + def test_range_list_presence(self): + with open(os.path.join('test', 'testfiles_for_unittests', + 'sample_exe64.elf'), 'rb') as f: + elffile = ELFFile(f) + self.assertTrue(elffile.has_dwarf_info()) + + dwarfinfo = elffile.get_dwarf_info() + self.assertTrue(dwarfinfo.range_lists() != None) + +if __name__ == '__main__': + unittest.main() diff --git a/test/testfiles_for_unittests/sample_exe64.elf b/test/testfiles_for_unittests/sample_exe64.elf new file mode 100644 index 0000000..ccfa6ae Binary files /dev/null and b/test/testfiles_for_unittests/sample_exe64.elf differ