dwarf/die: Handle DW_FORM_flag_present in value translation (#246)
authorWilliam Woodruff <william@trailofbits.com>
Fri, 18 Oct 2019 16:17:27 +0000 (12:17 -0400)
committerEli Bendersky <eliben@users.noreply.github.com>
Fri, 18 Oct 2019 16:17:27 +0000 (09:17 -0700)
* dwarf/die: Handle DW_FORM_flag_present in value translation

When an attribute has form DW_FORM_flag_present it is implicitly
indicated as present, with no actual value.

Ref. DWARFv4, section 7.

* test: Add DW_FORM_flag_present value test

* test: Fix iteration

* test: Remove old assert

elftools/dwarf/die.py
test/test_dwarf_attr_form_flag_present.py [new file with mode: 0644]
test/testfiles_for_unittests/lambda.elf [new file with mode: 0755]

index 5d3ad995d0b72314426c6a2e9760412cda154a5e..ec50fb1d3d55e1d8b69459ca438b3d3c70917524 100755 (executable)
@@ -199,6 +199,8 @@ class DIE(object):
                 value = self.dwarfinfo.get_string_from_table(raw_value)
         elif form == 'DW_FORM_flag':
             value = not raw_value == 0
+        elif form == 'DW_FORM_flag_present':
+            value = True
         elif form == 'DW_FORM_indirect':
             try:
                 form = DW_FORM_raw2name[raw_value]
diff --git a/test/test_dwarf_attr_form_flag_present.py b/test/test_dwarf_attr_form_flag_present.py
new file mode 100644 (file)
index 0000000..9ec9ce5
--- /dev/null
@@ -0,0 +1,25 @@
+#-------------------------------------------------------------------------------
+# elftools tests
+#
+# Eli Bendersky (eliben@gmail.com), Santhosh Kumar Mani (santhoshmani@gmail.com)
+# This code is in the public domain
+#-------------------------------------------------------------------------------
+import os
+import unittest
+
+from elftools.elf.elffile import ELFFile
+
+
+class TestAttrFormFlagPresent(unittest.TestCase):
+    def test_form_flag_present_value_is_true(self):
+        with open(os.path.join('test', 'testfiles_for_unittests',
+                               'lambda.elf'), 'rb') as f:
+            elffile = ELFFile(f)
+            self.assertTrue(elffile.has_dwarf_info())
+
+            dwarf = elffile.get_dwarf_info()
+            for cu in dwarf.iter_CUs():
+                for die in cu.iter_DIEs():
+                    for _, attr in die.attributes.items():
+                        if attr.form == "DW_FORM_flag_present":
+                            self.assertTrue(attr.value)
diff --git a/test/testfiles_for_unittests/lambda.elf b/test/testfiles_for_unittests/lambda.elf
new file mode 100755 (executable)
index 0000000..d232a05
Binary files /dev/null and b/test/testfiles_for_unittests/lambda.elf differ