return s
+def describe_CFI_register_rule(rule):
+ s = _DESCR_CFI_REGISTER_RULE_TYPE[rule.type]
+ if rule.type in ('OFFSET', 'VAL_OFFSET'):
+ s += '%+d' % rule.arg
+ elif rule.type == 'REGISTER':
+ s += describe_reg_name(rule.arg)
+ return s
+
+
+def describe_CFI_CFA_rule(rule):
+ if rule.expr:
+ return 'exp'
+ else:
+ return '%s%+d' % (describe_reg_name(rule.reg), rule.offset)
+
+
def describe_reg_name(regnum, machine_arch=None):
""" Provide a textual description for a register name, given its serial
number. The number is expected to be valid.
DW_ORD_col_major: '(column major)',
}
+_DESCR_CFI_REGISTER_RULE_TYPE = dict(
+ UNDEFINED='u',
+ SAME_VALUE='s',
+ OFFSET='c',
+ VAL_OFFSET='v',
+ REGISTER='',
+ EXPRESSION='exp',
+ VAL_EXPRESSION='vexp',
+ ARCHITECTURAL='a',
+)
def _make_extra_mapper(mapping, default, default_interpolate_value=False):
""" Create a mapping function from attribute parameters to an extra
#-------------------------------------------------------------------------------
import os, sys
from optparse import OptionParser
+from itertools import ifilter
import string
)
from elftools.dwarf.dwarfinfo import DWARFInfo
from elftools.dwarf.descriptions import (
- describe_reg_name,
- describe_attr_value, set_global_machine_arch, describe_CFI_instructions)
+ describe_reg_name, describe_attr_value, set_global_machine_arch,
+ describe_CFI_instructions, describe_CFI_register_rule,
+ describe_CFI_CFA_rule,
+ )
from elftools.dwarf.constants import (
DW_LNS_copy, DW_LNS_set_file, DW_LNE_define_file)
from elftools.dwarf.callframe import CIE, FDE
entry['code_alignment_factor'],
entry['data_alignment_factor'],
entry['return_address_register']))
+ ra_regnum = entry['return_address_register']
else: # FDE
self._emitline('\n%08x %08x %08x FDE cie=%08x pc=%08x..%08x' % (
entry.offset,
entry.cie.offset,
entry['initial_location'],
entry['initial_location'] + entry['address_range']))
+ ra_regnum = entry.cie['return_address_register']
# Print the heading row for the decoded table
self._emit(' LOC')
self._emit(' ' if entry.structs.address_size == 4 else ' ')
- self._emit('CFA ')
+ self._emit(' CFA ')
+ # Decode the table nad look at the registers it describes.
+ # We build reg_order here to match readelf's order. In particular,
+ # registers are sorted by their number, and the register matching
+ # ra_regnum is always listed last with a special heading.
decoded_table = entry.get_decoded()
- for regnum in decoded_table.reg_order:
+ reg_order = sorted(ifilter(
+ lambda r: r != ra_regnum,
+ decoded_table.reg_order))
+
+ # Headings for the registers
+ for regnum in reg_order:
self._emit('%-6s' % describe_reg_name(regnum))
self._emitline('ra ')
-
-
-
+
+ # Now include ra_regnum in reg_order to print its values similarly
+ # to the other registers.
+ reg_order.append(ra_regnum)
+ for line in decoded_table.table:
+ self._emit(self._format_hex(
+ line['pc'], fullhex=True, lead0x=False))
+ self._emit(' %-9s' % describe_CFI_CFA_rule(line['cfa']))
+
+ for regnum in reg_order:
+ if regnum in line:
+ s = describe_CFI_register_rule(line[regnum])
+ else:
+ s = 'u'
+ self._emit('%-6s' % s)
+ self._emitline()
+ self._emitline()
def _emit(self, s=''):
""" Emit an object to output
decoded_FDE = entries[1].get_decoded()
self.assertEqual(decoded_FDE.reg_order, list(range(9)))
- #self.assertEqual(len(decoded_FDE.table), 1)
self.assertEqual(decoded_FDE.table[0]['cfa'].reg, 7)
self.assertEqual(decoded_FDE.table[0]['cfa'].offset, 0)
self.assertEqual(decoded_FDE.table[0]['pc'], 0x11223344)
self.assertEqual(decoded_FDE.table[5][4].arg, -12)
self.assertEqual(decoded_FDE.table[6]['pc'], 0x11223344 + 64)
self.assertEqual(decoded_FDE.table[9]['pc'], 0x11223344 + 76)
- self.assertEqual(decoded_FDE.table[9][6].type, RegisterRule.SAME_VALUE)
- self.assertEqual(decoded_FDE.table[10]['pc'], 0x11223344 + 80)
def test_describe_CFI_instructions(self):
# The data here represents a single CIE