Storing the offset of DWARF operations within the expression block (#404)
authorSeva Alekseyev <sevaa@yarxi.ru>
Mon, 16 May 2022 13:58:24 +0000 (09:58 -0400)
committerGitHub <noreply@github.com>
Mon, 16 May 2022 13:58:24 +0000 (06:58 -0700)
* Storing the offset of every DWARF operations within the expression block

* Trivial auto test

elftools/dwarf/dwarf_expr.py
test/test_dwarf_expr.py

index 07c6fa10f11e9ec1dbf3015eb0bedf80ef2af029..39ceee71c154c75f2ad1601b20382d9b1f2a04b6 100644 (file)
@@ -114,7 +114,7 @@ DW_OP_opcode2name = dict((v, k) for k, v in iteritems(DW_OP_name2opcode))
 
 # Each parsed DWARF expression is returned as this type with its numeric opcode,
 # op name (as a string) and a list of arguments.
-DWARFExprOp = namedtuple('DWARFExprOp', 'op op_name args')
+DWARFExprOp = namedtuple('DWARFExprOp', 'op op_name args offset')
 
 
 class DWARFExprParser(object):
@@ -138,6 +138,7 @@ class DWARFExprParser(object):
         while True:
             # Get the next opcode from the stream. If nothing is left in the
             # stream, we're done.
+            offset = stream.tell()
             byte = stream.read(1)
             if len(byte) == 0:
                 break
@@ -150,7 +151,7 @@ class DWARFExprParser(object):
             arg_parser = self._dispatch_table[op]
             args = arg_parser(stream)
 
-            parsed.append(DWARFExprOp(op=op, op_name=op_name, args=args))
+            parsed.append(DWARFExprOp(op=op, op_name=op_name, args=args, offset=offset))
 
         return parsed
 
index 308d87289556d18ec2dd2bbc3ef6b029d1cd51a6..4e5dfc4479e51c6e1ef28a8d933d112ce7a2a1c6 100644 (file)
@@ -72,18 +72,41 @@ class TestParseExpr(unittest.TestCase):
     def test_single(self):
         p = DWARFExprParser(self.structs32)
         lst = p.parse_expr([0x1b])
-        self.assertEqual(lst, [DWARFExprOp(op=0x1B, op_name='DW_OP_div', args=[])])
+        self.assertEqual(lst, [DWARFExprOp(op=0x1B, op_name='DW_OP_div', args=[], offset=0)])
 
         lst = p.parse_expr([0x90, 16])
-        self.assertEqual(lst, [DWARFExprOp(op=0x90, op_name='DW_OP_regx', args=[16])])
+        self.assertEqual(lst, [DWARFExprOp(op=0x90, op_name='DW_OP_regx', args=[16], offset=0)])
 
         lst = p.parse_expr([0xe0])
         self.assertEqual(len(lst), 1)
         # 0xe0 maps to both DW_OP_GNU_push_tls_address and DW_OP_lo_user, so
         # check for both to prevent non-determinism.
         self.assertIn(lst[0], [
-            DWARFExprOp(op=0xe0, op_name='DW_OP_GNU_push_tls_address', args=[]),
-            DWARFExprOp(op=0xe0, op_name='DW_OP_lo_user', args=[])])
+            DWARFExprOp(op=0xe0, op_name='DW_OP_GNU_push_tls_address', args=[], offset=0),
+            DWARFExprOp(op=0xe0, op_name='DW_OP_lo_user', args=[], offset=0)])
+
+        # Real life example:
+        # push_object_address
+        # deref
+        # dup
+        # bra 4
+        # lit0
+        # skip 3
+        # lit4
+        # minus 
+        # deref        
+        lst = p.parse_expr([0x97,0x6,0x12,0x28,0x4,0x0,0x30,0x2F,0x3,0x0,0x34,0x1C,0x6])
+        self.assertEqual(len(lst), 9)
+        self.assertEqual(lst, [
+            DWARFExprOp(op=0x97, op_name='DW_OP_push_object_address', args=[], offset=0),
+            DWARFExprOp(op=0x6,  op_name='DW_OP_deref', args=[], offset=1),
+            DWARFExprOp(op=0x12, op_name='DW_OP_dup', args=[], offset=2),
+            DWARFExprOp(op=0x28, op_name='DW_OP_bra', args=[4], offset=3),
+            DWARFExprOp(op=0x30, op_name='DW_OP_lit0', args=[], offset=6),
+            DWARFExprOp(op=0x2f, op_name='DW_OP_skip', args=[3], offset=7),
+            DWARFExprOp(op=0x34, op_name='DW_OP_lit4', args=[], offset=10),
+            DWARFExprOp(op=0x1c, op_name='DW_OP_minus', args=[], offset=11),
+            DWARFExprOp(op=0x6,  op_name='DW_OP_deref', args=[], offset=12)])
 
 
 if __name__ == '__main__':