Fix for issue #22: handle DW_FORM_indirect correctly
authorEli Bendersky <eliben@gmail.com>
Sat, 18 Jan 2014 14:24:23 +0000 (06:24 -0800)
committerEli Bendersky <eliben@gmail.com>
Sat, 18 Jan 2014 14:24:23 +0000 (06:24 -0800)
elftools/common/utils.py
elftools/dwarf/die.py
elftools/dwarf/enums.py

index 7bd1d5b1fef76133809756e1b46c212c702261c9..e6fc6b65874efe47295546c0f4f9e92eb4c2e689 100644 (file)
@@ -32,7 +32,7 @@ def struct_parse(struct, stream, stream_pos=None):
         return struct.parse_stream(stream)
     except ConstructError as e:
         raise ELFParseError(e.message)
-    
+
 
 def parse_cstring_from_stream(stream, stream_pos=None):
     """ Parse a C-string from the given stream. The string is returned without
@@ -78,11 +78,10 @@ def dwarf_assert(cond, msg=''):
 @contextmanager
 def preserve_stream_pos(stream):
     """ Usage:
-            
-            # stream has some position FOO (return value of stream.tell())
-            with preserve_stream_pos(stream):
-                # do stuff that manipulates the stream
-            # stream still has position FOO
+        # stream has some position FOO (return value of stream.tell())
+        with preserve_stream_pos(stream):
+            # do stuff that manipulates the stream
+        # stream still has position FOO
     """
     saved_pos = stream.tell()
     yield
index e329e5c7165fdd9935901648611413965944ee87..86830fc3bedaf9a2a0538f4f29adcc08ae1f5791 100644 (file)
@@ -9,8 +9,10 @@
 from collections import namedtuple
 import os
 
+from ..common.exceptions import DWARFError
 from ..common.py3compat import OrderedDict, bytes2str, iteritems
 from ..common.utils import struct_parse, preserve_stream_pos
+from .enums import DW_FORM_raw2name
 
 
 # AttributeValue - describes an attribute value in the DIE:
@@ -202,7 +204,13 @@ class DIE(object):
         elif form == 'DW_FORM_flag':
             value = not raw_value == 0
         elif form == 'DW_FORM_indirect':
-            form = raw_value
+            try:
+                form = DW_FORM_raw2name[raw_value]
+            except KeyError as err:
+                raise DWARFError(
+                        'Found DW_FORM_indirect with unknown raw_value=' +
+                        str(raw_value))
+
             raw_value = struct_parse(
                 self.cu.structs.Dwarf_dw_form[form], self.stream)
             # Let's hope this doesn't get too deep :-)
index 2167c6800b8b148b516542df675673f35021bba9..d576dffb5055fa56e1df044c7b8672d4e6199f44 100644 (file)
@@ -7,6 +7,7 @@
 # This code is in the public domain
 #-------------------------------------------------------------------------------
 from ..construct import Pass
+from ..common.py3compat import iteritems
 
 
 ENUM_DW_TAG = dict(
@@ -277,3 +278,6 @@ ENUM_DW_FORM = dict(
     _default_               = Pass,
 )
 
+# Inverse mapping for ENUM_DW_FORM
+DW_FORM_raw2name = dict((v, k) for k, v in iteritems(ENUM_DW_FORM))
+