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
, iteritems
14 from elftools
.dwarf
.lineprogram
import LineProgram
, LineState
, LineProgramEntry
15 from elftools
.dwarf
.structs
import DWARFStructs
16 from elftools
.dwarf
.constants
import *
19 class TestLineProgram(unittest
.TestCase
):
20 def _make_program_in_stream(self
, stream
):
21 """ Create a LineProgram from the given program encoded in a stream
23 ds
= DWARFStructs(little_endian
=True, dwarf_format
=32, address_size
=4)
24 header
= ds
.Dwarf_lineprog_header
.parse(
25 b
'\x04\x10\x00\x00' + # initial lenght
26 b
'\x03\x00' + # version
27 b
'\x20\x00\x00\x00' + # header length
28 b
'\x01\x01\x01\x0F' + # flags
29 b
'\x0A' + # opcode_base
30 b
'\x00\x01\x04\x08\x0C\x01\x01\x01\x00' + # standard_opcode_lengths
31 # 2 dir names followed by a NULL
32 b
'\x61\x62\x00\x70\x00\x00' +
34 b
'\x61\x72\x00\x0C\x0D\x0F' +
36 b
'\x45\x50\x51\x00\x86\x12\x07\x08' +
40 lp
= LineProgram(header
, stream
, ds
, 0, len(stream
.getvalue()))
43 def assertLineState(self
, state
, **kwargs
):
44 """ Assert that the state attributes specified in kwargs have the given
45 values (the rest are default).
47 for k
, v
in iteritems(kwargs
):
48 self
.assertEqual(getattr(state
, k
), v
)
50 def test_spec_sample_59(self
):
51 # Sample in figure 59 of DWARFv3
62 lp
= self
._make
_program
_in
_stream
(s
)
63 linetable
= lp
.get_entries()
65 self
.assertEqual(len(linetable
), 7)
66 self
.assertIs(linetable
[0].state
, None) # doesn't modify state
67 self
.assertEqual(linetable
[0].command
, DW_LNS_advance_pc
)
68 self
.assertEqual(linetable
[0].args
, [0x239])
69 self
.assertLineState(linetable
[1].state
, address
=0x239, line
=3)
70 self
.assertEqual(linetable
[1].command
, 0xb)
71 self
.assertEqual(linetable
[1].args
, [2, 0])
72 self
.assertLineState(linetable
[2].state
, address
=0x23c, line
=5)
73 self
.assertLineState(linetable
[3].state
, address
=0x244, line
=6)
74 self
.assertLineState(linetable
[4].state
, address
=0x24b, line
=7, end_sequence
=False)
75 self
.assertEqual(linetable
[5].command
, DW_LNS_advance_pc
)
76 self
.assertEqual(linetable
[5].args
, [2])
77 self
.assertLineState(linetable
[6].state
, address
=0x24d, line
=7, end_sequence
=True)
79 def test_spec_sample_60(self
):
80 # Sample in figure 60 of DWARFv3
94 lp
= self
._make
_program
_in
_stream
(s
)
95 linetable
= lp
.get_entries()
97 self
.assertEqual(len(linetable
), 10)
98 self
.assertIs(linetable
[0].state
, None) # doesn't modify state
99 self
.assertEqual(linetable
[0].command
, DW_LNS_fixed_advance_pc
)
100 self
.assertEqual(linetable
[0].args
, [0x239])
101 self
.assertLineState(linetable
[1].state
, address
=0x239, line
=3)
102 self
.assertLineState(linetable
[3].state
, address
=0x23c, line
=5)
103 self
.assertLineState(linetable
[5].state
, address
=0x244, line
=6)
104 self
.assertLineState(linetable
[7].state
, address
=0x24b, line
=7, end_sequence
=False)
105 self
.assertLineState(linetable
[9].state
, address
=0x24d, line
=7, end_sequence
=True)
108 if __name__
== '__main__':