Handle absence of .debug_ranges gracefully
authorSanthosh Kumar Mani <santhoshmani@gmail.com>
Thu, 23 Jan 2014 15:55:24 +0000 (21:25 +0530)
committerSanthosh Kumar Mani <santhoshmani@gmail.com>
Thu, 23 Jan 2014 15:55:24 +0000 (21:25 +0530)
elftools/dwarf/dwarfinfo.py
examples/dwarf_range_lists.py
test/test_dwarf_range_lists.py [new file with mode: 0644]
test/testfiles_for_unittests/sample_exe64.elf [new file with mode: 0644]

index e5c0e71263761bbd9512d5799b5e620961726901..f9d1e326267985a1d04d5e91e1032fdec46ed341 100644 (file)
@@ -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 ------#
 
index fced6a6a1b36a83f8c23cacbfc8f1199c2a97f9f..f722d11a4340d18577cba7ba505f3a39a305d1d3 100644 (file)
@@ -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 (file)
index 0000000..1ebd0c7
--- /dev/null
@@ -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 (file)
index 0000000..ccfa6ae
Binary files /dev/null and b/test/testfiles_for_unittests/sample_exe64.elf differ