1 #-------------------------------------------------------------------------------
4 # Eli Bendersky (eliben@gmail.com)
5 # This code is in the public domain
6 #-------------------------------------------------------------------------------
8 import unittest2
as unittest
12 from utils
import setup_syspath
; setup_syspath()
13 from elftools
.common
.py3compat
import BytesIO
14 from elftools
.dwarf
.callframe
import (
15 CallFrameInfo
, CIE
, FDE
, instruction_name
, CallFrameInstruction
,
17 from elftools
.dwarf
.structs
import DWARFStructs
18 from elftools
.dwarf
.descriptions
import (describe_CFI_instructions
,
19 set_global_machine_arch
)
22 class TestCallFrame(unittest
.TestCase
):
23 def assertInstruction(self
, instr
, name
, args
):
24 self
.assertIsInstance(instr
, CallFrameInstruction
)
25 self
.assertEqual(instruction_name(instr
.opcode
), name
)
26 self
.assertEqual(instr
.args
, args
)
28 def test_spec_sample_d6(self
):
29 # D.6 sample in DWARFv3
33 b
'\x20\x00\x00\x00' + # length
34 b
'\xff\xff\xff\xff' + # CIE_id
35 b
'\x03\x00\x04\x7c' + # version, augmentation, caf, daf
36 b
'\x08' + # return address
50 b
'\x28\x00\x00\x00' + # length
51 b
'\x00\x00\x00\x00' + # CIE_pointer (to CIE at 0)
52 b
'\x44\x33\x22\x11' + # initial_location
53 b
'\x54\x00\x00\x00' + # address range
55 b
'\x0e\x0c' + b
'\x41' +
56 b
'\x88\x01' + b
'\x41' +
57 b
'\x86\x02' + b
'\x41' +
58 b
'\x0d\x06' + b
'\x41' +
59 b
'\x84\x03' + b
'\x4b' +
62 b
'\x0d\x07' + b
'\x41' +
69 structs
= DWARFStructs(little_endian
=True, dwarf_format
=32, address_size
=4)
70 cfi
= CallFrameInfo(s
, len(data
), structs
)
71 entries
= cfi
.get_entries()
73 self
.assertEqual(len(entries
), 2)
74 self
.assertIsInstance(entries
[0], CIE
)
75 self
.assertEqual(entries
[0]['length'], 32)
76 self
.assertEqual(entries
[0]['data_alignment_factor'], -4)
77 self
.assertEqual(entries
[0]['return_address_register'], 8)
78 self
.assertEqual(len(entries
[0].instructions
), 11)
79 self
.assertInstruction(entries
[0].instructions
[0],
80 'DW_CFA_def_cfa', [7, 0])
81 self
.assertInstruction(entries
[0].instructions
[8],
82 'DW_CFA_same_value', [7])
83 self
.assertInstruction(entries
[0].instructions
[9],
84 'DW_CFA_register', [8, 1])
86 self
.assertTrue(isinstance(entries
[1], FDE
))
87 self
.assertEqual(entries
[1]['length'], 40)
88 self
.assertEqual(entries
[1]['CIE_pointer'], 0)
89 self
.assertEqual(entries
[1]['address_range'], 84)
90 self
.assertIs(entries
[1].cie
, entries
[0])
91 self
.assertEqual(len(entries
[1].instructions
), 21)
92 self
.assertInstruction(entries
[1].instructions
[0],
93 'DW_CFA_advance_loc', [1])
94 self
.assertInstruction(entries
[1].instructions
[1],
95 'DW_CFA_def_cfa_offset', [12])
96 self
.assertInstruction(entries
[1].instructions
[9],
97 'DW_CFA_offset', [4, 3])
98 self
.assertInstruction(entries
[1].instructions
[18],
99 'DW_CFA_def_cfa_offset', [0])
100 self
.assertInstruction(entries
[1].instructions
[20],
103 # Now let's decode it...
104 decoded_CIE
= entries
[0].get_decoded()
105 self
.assertEqual(decoded_CIE
.reg_order
, list(range(9)))
106 self
.assertEqual(len(decoded_CIE
.table
), 1)
107 self
.assertEqual(decoded_CIE
.table
[0]['cfa'].reg
, 7)
108 self
.assertEqual(decoded_CIE
.table
[0]['pc'], 0)
109 self
.assertEqual(decoded_CIE
.table
[0]['cfa'].offset
, 0)
110 self
.assertEqual(decoded_CIE
.table
[0][4].type, RegisterRule
.SAME_VALUE
)
111 self
.assertEqual(decoded_CIE
.table
[0][8].type, RegisterRule
.REGISTER
)
112 self
.assertEqual(decoded_CIE
.table
[0][8].arg
, 1)
114 decoded_FDE
= entries
[1].get_decoded()
115 self
.assertEqual(decoded_FDE
.reg_order
, list(range(9)))
116 self
.assertEqual(decoded_FDE
.table
[0]['cfa'].reg
, 7)
117 self
.assertEqual(decoded_FDE
.table
[0]['cfa'].offset
, 0)
118 self
.assertEqual(decoded_FDE
.table
[0]['pc'], 0x11223344)
119 self
.assertEqual(decoded_FDE
.table
[0][8].type, RegisterRule
.REGISTER
)
120 self
.assertEqual(decoded_FDE
.table
[0][8].arg
, 1)
121 self
.assertEqual(decoded_FDE
.table
[1]['cfa'].reg
, 7)
122 self
.assertEqual(decoded_FDE
.table
[1]['cfa'].offset
, 12)
123 self
.assertEqual(decoded_FDE
.table
[2][8].type, RegisterRule
.OFFSET
)
124 self
.assertEqual(decoded_FDE
.table
[2][8].arg
, -4)
125 self
.assertEqual(decoded_FDE
.table
[2][4].type, RegisterRule
.SAME_VALUE
)
126 self
.assertEqual(decoded_FDE
.table
[5]['pc'], 0x11223344 + 20)
127 self
.assertEqual(decoded_FDE
.table
[5][4].type, RegisterRule
.OFFSET
)
128 self
.assertEqual(decoded_FDE
.table
[5][4].arg
, -12)
129 self
.assertEqual(decoded_FDE
.table
[6]['pc'], 0x11223344 + 64)
130 self
.assertEqual(decoded_FDE
.table
[9]['pc'], 0x11223344 + 76)
132 def test_describe_CFI_instructions(self
):
133 # The data here represents a single CIE
135 b
'\x16\x00\x00\x00' + # length
136 b
'\xff\xff\xff\xff' + # CIE_id
137 b
'\x03\x00\x04\x7c' + # version, augmentation, caf, daf
138 b
'\x08' + # return address
140 b
'\x10\x02\x07\x03\x01\x02\x00\x00\x06\x06')
143 structs
= DWARFStructs(little_endian
=True, dwarf_format
=32, address_size
=4)
144 cfi
= CallFrameInfo(s
, len(data
), structs
)
145 entries
= cfi
.get_entries()
147 set_global_machine_arch('x86')
148 self
.assertEqual(describe_CFI_instructions(entries
[0]),
149 ( ' DW_CFA_def_cfa: r7 (edi) ofs 2\n' +
150 ' DW_CFA_expression: r2 (edx) (DW_OP_addr: 201; DW_OP_deref; DW_OP_deref)\n'))
153 if __name__
== '__main__':