Support for DW_FORM_data16, unit test (#437)
authorSeva Alekseyev <sevaa@yarxi.ru>
Tue, 16 Aug 2022 13:40:14 +0000 (09:40 -0400)
committerGitHub <noreply@github.com>
Tue, 16 Aug 2022 13:40:14 +0000 (06:40 -0700)
Fixes #436

elftools/dwarf/structs.py
test/test_dwarf_formdata16.py [new file with mode: 0644]
test/testfiles_for_unittests/dwarf_lineprog_data16.elf [new file with mode: 0644]

index 1a5ae45743013f2636111a8ced11ad9798cbd24d..a74d5dead567a5f2a093cba44f67209f62301655 100644 (file)
@@ -225,6 +225,7 @@ class DWARFStructs(object):
             DW_FORM_data2=self.Dwarf_uint16(''),
             DW_FORM_data4=self.Dwarf_uint32(''),
             DW_FORM_data8=self.Dwarf_uint64(''),
+            DW_FORM_data16=Array(16, self.Dwarf_uint8('')), # Used for hashes and such, not for integers
             DW_FORM_sdata=self.Dwarf_sleb128(''),
             DW_FORM_udata=self.Dwarf_uleb128(''),
 
diff --git a/test/test_dwarf_formdata16.py b/test/test_dwarf_formdata16.py
new file mode 100644 (file)
index 0000000..898ca9b
--- /dev/null
@@ -0,0 +1,34 @@
+#-------------------------------------------------------------------------------
+# elftools tests
+#
+# Seva Alekseyev (sevaa@sprynet.com)
+# This code is in the public domain
+#-------------------------------------------------------------------------------
+import unittest
+import os
+
+from elftools.elf.elffile import ELFFile
+
+class TestFormData16(unittest.TestCase):
+    def test_formdata16(self):
+        path = os.path.join('test', 'testfiles_for_unittests',
+                            'dwarf_lineprog_data16.elf')
+        with open(path, 'rb') as f:
+            elffile = ELFFile(f)
+            dwarfinfo = elffile.get_dwarf_info(follow_links=False)
+            cu = next(dwarfinfo.iter_CUs())
+            # Without DW_FORM_data16, the following line errors out:
+            lp = dwarfinfo.line_program_for_CU(cu)
+            # Make sure the hashes come out right
+            self.assertEqual(lp.header.version, 5)
+            # The following interrogates the DWARFv5 specific header structures
+            self.assertEqual(lp.header.file_name_entry_format[2].content_type, 'DW_LNCT_MD5')
+            # The correct hash value was taken from llvm-dwarfdump output
+            hash = lp.header.file_names[0]['DW_LNCT_MD5']
+            hash = ''.join("%02x" % b for b in hash)
+            self.assertEqual(hash, '00dbc7f4edc56417c80f1aa512c4c051')
+
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/test/testfiles_for_unittests/dwarf_lineprog_data16.elf b/test/testfiles_for_unittests/dwarf_lineprog_data16.elf
new file mode 100644 (file)
index 0000000..16d4d38
Binary files /dev/null and b/test/testfiles_for_unittests/dwarf_lineprog_data16.elf differ