--- /dev/null
+#-------------------------------------------------------------------------------\r
+# elftools: dwarf/descriptions.py\r
+#\r
+# Textual descriptions of the various values and enums of DWARF\r
+#\r
+# Eli Bendersky (eliben@gmail.com)\r
+# This code is in the public domain\r
+#-------------------------------------------------------------------------------\r
+from collections import defaultdict\r
+\r
+\r
+def describe_attr_value(attr, die, section_offset):\r
+ """ Given an AttributeValue extracted, return the textual representation of\r
+ its value, suitable for tools like readelf.\r
+ \r
+ To cover all cases, this function needs some extra arguments:\r
+\r
+ die: the DIE this attribute was extracted from\r
+ section_offset: offset in the stream of the section the DIE belongs to\r
+ """\r
+ descr_func = _ATTR_DESCRIPTION_MAP[attr.form]\r
+ return descr_func(attr, die, section_offset)\r
+\r
+\r
+#-------------------------------------------------------------------------------\r
+\r
+def _describe_attr_ref(attr, die, section_offset):
+ return '<0x%x>' % (attr.value + die.cu.cu_offset - section_offset)\r
+\r
+def _describe_attr_value_passthrough(attr, die, section_offset):
+ return attr.value\r
+\r
+def _describe_attr_hex(attr, die, section_offset):
+ return '0x%x' % (attr.value)\r
+\r
+def _describe_attr_hex_addr(attr, die, section_offset):\r
+ return '<0x%x>' % (attr.value)\r
+\r
+def _describe_attr_split_64bit(attr, die, section_offset):
+ low_word = attr.value & 0xFFFFFFFF\r
+ high_word = (attr.value >> 32) & 0xFFFFFFFF\r
+ return '0x%x 0x%x' % (low_word, high_word)\r
+\r
+def _describe_attr_strp(attr, die, section_offset):
+ return '(indirect string, offset: 0x%x): %s' % (attr.raw_value, attr.value)\r
+\r
+def _describe_attr_block(attr, die, section_offset):
+ s = '%s byte block: ' % len(attr.value)\r
+ s += ' '.join('%x' % item for item in attr.value)\r
+ return s\r
+ \r
+\r
+_ATTR_DESCRIPTION_MAP = defaultdict(\r
+ lambda: _describe_attr_value_passthrough, # default_factory\r
+ \r
+ DW_FORM_ref1=_describe_attr_ref,\r
+ DW_FORM_ref2=_describe_attr_ref,\r
+ DW_FORM_ref4=_describe_attr_ref,\r
+ DW_FORM_ref8=_describe_attr_split_64bit,\r
+ DW_FORM_ref_udata=_describe_attr_ref, \r
+ DW_FORM_ref_addr=_describe_attr_hex_addr,\r
+ DW_FORM_data4=_describe_attr_hex,\r
+ DW_FORM_data8=_describe_attr_split_64bit,\r
+ DW_FORM_addr=_describe_attr_hex,\r
+ DW_FORM_sec_offset=_describe_attr_hex,\r
+ DW_FORM_flag_present=_describe_attr_value_passthrough,\r
+ DW_FORM_flag=_describe_attr_value_passthrough,\r
+ DW_FORM_data1=_describe_attr_value_passthrough,\r
+ DW_FORM_data2=_describe_attr_value_passthrough,\r
+ DW_FORM_sdata=_describe_attr_value_passthrough,\r
+ DW_FORM_udata=_describe_attr_value_passthrough,\r
+ DW_FORM_string=_describe_attr_value_passthrough,\r
+ DW_FORM_strp=_describe_attr_strp,\r
+ DW_FORM_block1=_describe_attr_block,\r
+ DW_FORM_block2=_describe_attr_block,\r
+ DW_FORM_block4=_describe_attr_block,\r
+ DW_FORM_block=_describe_attr_block,\r
+)\r
+\r
describe_symbol_shndx, describe_reloc_type,
)
from elftools.dwarf.dwarfinfo import DWARFInfo, DebugSectionLocator
+from elftools.dwarf.descriptions import describe_attr_value
class ReadElf(object):
if die.is_null():
die_depth -= 1
continue
- self._emitline(' <%s><%x>: Abbrev Number: %s' % (
- die_depth, die.offset - section_offset, die.abbrev_code))
+ self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
+ die_depth,
+ die.offset - section_offset,
+ die.abbrev_code,
+ die.tag))
+
+ for attrname, attr in die.attributes.iteritems():
+ self._emitline(' <%2x> %-18s: %s' % (
+ attr.offset - section_offset,
+ attrname,
+ describe_attr_value(attr, die, section_offset)))
if die.has_children:
die_depth += 1
+
def _emit(self, s=''):
""" Emit an object to output