1#!/usr/bin/env python
2#
3# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
4#
5# SPDX-License-Identifier: BSD-2-Clause
6#
7
8from __future__ import absolute_import, division, print_function, \
9    unicode_literals
10
11import unittest
12
13from capdl import ELF
14from tests import CapdlTestCase
15
16
17class TestElf(CapdlTestCase):
18
19    def test_elf(self):
20        elf = ELF('resources/arm-hello.bin')
21        assert elf.get_arch() in [40, 'EM_ARM', 'ARM']
22        elf.get_spec()
23
24    def test_ia32_elf(self):
25        elf = ELF('resources/ia32-hello.bin')
26        assert elf.get_arch() == 'x86'
27
28        elf.get_spec()
29
30    def test_symbol_lookup(self):
31        elf = ELF('resources/unstripped.bin')
32        assert elf.get_arch() == 'x86'
33
34        # Confirm that the address concurs with the one we get from objdump.
35        assert elf.get_symbol_vaddr('_start') == 0x08048d48
36
37        elf = ELF('resources/stripped.bin')
38        assert elf.get_arch() == 'x86'
39
40        # We shouldn't be able to get the symbol from the stripped binary.
41        try:
42            vaddr = elf.get_symbol_vaddr('_start')
43            assert not ('Symbol lookup on a stripped binary returned _start == 0x%0.8x' % vaddr)
44        except:
45            # Expected
46            pass
47
48
49if __name__ == '__main__':
50    unittest.main()
51