From bb03c866acc1a578c67d8fb3087bd5289f127689 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 23 Mar 2013 06:16:56 -0700 Subject: [PATCH] cleanup & updated CHANGES --- CHANGES | 3 +- elftools/dwarf/die.py | 66 ++++++++++++++++++------------------ elftools/elf/descriptions.py | 6 ++-- elftools/elf/dynamic.py | 3 +- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/CHANGES b/CHANGES index 4eab25c..1861253 100644 --- a/CHANGES +++ b/CHANGES @@ -7,7 +7,8 @@ Changelog file & line information from an address. - Issue #7: parsing incorrect DWARF was made a bit more forgiving for cases where serialized DIE trees have extra NULLs at the end. - - Pull request 6: very initial support for ARM ELF files. + - Pull request 6: very initial support for ARM ELF files (Matthew Fernandez). + - Pull request 7: support for dumping the dynamic section (Mike Frysinger). + Version 0.20 (27.01.2012) diff --git a/elftools/dwarf/die.py b/elftools/dwarf/die.py index f0b5eb8..0c5d6c3 100644 --- a/elftools/dwarf/die.py +++ b/elftools/dwarf/die.py @@ -12,12 +12,12 @@ from ..common.py3compat import OrderedDict from ..common.utils import struct_parse, preserve_stream_pos -# AttributeValue - describes an attribute value in the DIE: +# AttributeValue - describes an attribute value in the DIE: # # name: # The name (DW_AT_*) of this attribute -# -# form: +# +# form: # The DW_FORM_* name of this attribute # # value: @@ -39,37 +39,37 @@ AttributeValue = namedtuple( class DIE(object): """ A DWARF debugging information entry. On creation, parses itself from the stream. Each DIE is held by a CU. - + Accessible attributes: - + tag: The DIE tag - + size: The size this DIE occupies in the section - + offset: The offset of this DIE in the stream - + attributes: - An ordered dictionary mapping attribute names to values. It's + An ordered dictionary mapping attribute names to values. It's ordered to preserve the order of attributes in the section - + has_children: Specifies whether this DIE has children - + abbrev_code: The abbreviation code pointing to an abbreviation entry (not - that this is for informational pusposes only - this object + that this is for informational pusposes only - this object interacts with its abbreviation table transparently). - + See also the public methods. """ def __init__(self, cu, stream, offset): """ cu: CompileUnit object this DIE belongs to. Used to obtain context information (structs, abbrev table, etc.) - + stream, offset: The stream and offset into it where this DIE's data is located """ @@ -77,7 +77,7 @@ class DIE(object): self.dwarfinfo = self.cu.dwarfinfo # get DWARFInfo context self.stream = stream self.offset = offset - + self.attributes = OrderedDict() self.tag = None self.has_children = None @@ -85,25 +85,25 @@ class DIE(object): self.size = 0 self._children = [] self._parent = None - - self._parse_DIE() - + + self._parse_DIE() + def is_null(self): """ Is this a null entry? """ return self.tag is None - + def get_parent(self): - """ The parent DIE of this DIE. None if the DIE has no parent (i.e. a + """ The parent DIE of this DIE. None if the DIE has no parent (i.e. a top-level DIE). """ return self._parent - + def iter_children(self): """ Yield all children of this DIE """ return iter(self._children) - + def iter_siblings(self): """ Yield all siblings of this DIE """ @@ -119,41 +119,41 @@ class DIE(object): # def add_child(self, die): self._children.append(die) - + def set_parent(self, die): self._parent = die #------ PRIVATE ------# - + def __repr__(self): s = 'DIE %s, size=%s, has_chidren=%s\n' % ( self.tag, self.size, self.has_children) for attrname, attrval in self.attributes.iteritems(): s += ' |%-18s: %s\n' % (attrname, attrval) return s - + def __str__(self): return self.__repr__() - + def _parse_DIE(self): """ Parses the DIE info from the section, based on the abbreviation table of the CU """ structs = self.cu.structs - - # A DIE begins with the abbreviation code. Read it and use it to + + # A DIE begins with the abbreviation code. Read it and use it to # obtain the abbrev declaration for this DIE. # Note: here and elsewhere, preserve_stream_pos is used on operations # that manipulate the stream by reading data from it. # self.abbrev_code = struct_parse( structs.Dwarf_uleb128(''), self.stream, self.offset) - + # This may be a null entry if self.abbrev_code == 0: self.size = self.stream.tell() - self.offset return - + with preserve_stream_pos(self.stream): abbrev_decl = self.cu.get_abbrev_table().get_abbrev( self.abbrev_code) @@ -167,14 +167,14 @@ class DIE(object): attr_offset = self.stream.tell() raw_value = struct_parse(structs.Dwarf_dw_form[form], self.stream) - value = self._translate_attr_value(form, raw_value) + value = self._translate_attr_value(form, raw_value) self.attributes[name] = AttributeValue( name=name, form=form, value=value, raw_value=raw_value, offset=attr_offset) - + self.size = self.stream.tell() - self.offset def _translate_attr_value(self, form, raw_value): @@ -195,5 +195,5 @@ class DIE(object): else: value = raw_value return value - + diff --git a/elftools/elf/descriptions.py b/elftools/elf/descriptions.py index cfd8f6e..df81000 100644 --- a/elftools/elf/descriptions.py +++ b/elftools/elf/descriptions.py @@ -24,7 +24,7 @@ def describe_ei_version(x): if x == 'EV_CURRENT': s += ' (current)' return s - + def describe_ei_osabi(x): return _DESCR_EI_OSABI.get(x, _unknown) @@ -43,7 +43,7 @@ def describe_p_type(x): def describe_p_flags(x): s = '' for flag in (P_FLAGS.PF_R, P_FLAGS.PF_W, P_FLAGS.PF_X): - s += _DESCR_P_FLAGS[flag] if (x & flag) else ' ' + s += _DESCR_P_FLAGS[flag] if (x & flag) else ' ' return s def describe_sh_type(x): @@ -87,7 +87,7 @@ def describe_dyn_tag(x): #------------------------------------------------------------------------------- _unknown = '' - + _DESCR_EI_CLASS = dict( ELFCLASSNONE='none', ELFCLASS32='ELF32', diff --git a/elftools/elf/dynamic.py b/elftools/elf/dynamic.py index 01c34ea..9d7dfaa 100644 --- a/elftools/elf/dynamic.py +++ b/elftools/elf/dynamic.py @@ -29,7 +29,8 @@ class DynamicTag(object): self.entry = entry if entry.d_tag in self._HANDLED_TAGS: dynstr = elffile.get_section_by_name(b'.dynstr') - setattr(self, entry.d_tag[3:].lower(), dynstr.get_string(self.entry.d_val)) + setattr(self, entry.d_tag[3:].lower(), + dynstr.get_string(self.entry.d_val)) def __getitem__(self, name): """ Implement dict-like access to entries -- 2.30.2