From 89b280a1d4c1bd6c206b030439595797e0a93c9d Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 18 Jan 2014 06:24:23 -0800 Subject: [PATCH] Fix for issue #22: handle DW_FORM_indirect correctly --- elftools/common/utils.py | 11 +++++------ elftools/dwarf/die.py | 10 +++++++++- elftools/dwarf/enums.py | 4 ++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/elftools/common/utils.py b/elftools/common/utils.py index 7bd1d5b..e6fc6b6 100644 --- a/elftools/common/utils.py +++ b/elftools/common/utils.py @@ -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 diff --git a/elftools/dwarf/die.py b/elftools/dwarf/die.py index e329e5c..86830fc 100644 --- a/elftools/dwarf/die.py +++ b/elftools/dwarf/die.py @@ -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 :-) diff --git a/elftools/dwarf/enums.py b/elftools/dwarf/enums.py index 2167c68..d576dff 100644 --- a/elftools/dwarf/enums.py +++ b/elftools/dwarf/enums.py @@ -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)) + -- 2.30.2