xref: /openbmc/u-boot/tools/binman/elf_test.py (revision f77d4410)
1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2017 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# Test for the elf module
6
7import os
8import sys
9import unittest
10
11import elf
12import test_util
13
14binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
15
16
17class FakeEntry:
18    """A fake Entry object, usedfor testing
19
20    This supports an entry with a given size.
21    """
22    def __init__(self, contents_size):
23        self.contents_size = contents_size
24        self.data = 'a' * contents_size
25
26    def GetPath(self):
27        return 'entry_path'
28
29
30class FakeSection:
31    """A fake Section object, used for testing
32
33    This has the minimum feature set needed to support testing elf functions.
34    A LookupSymbol() function is provided which returns a fake value for amu
35    symbol requested.
36    """
37    def __init__(self, sym_value=1):
38        self.sym_value = sym_value
39
40    def GetPath(self):
41        return 'section_path'
42
43    def LookupSymbol(self, name, weak, msg):
44        """Fake implementation which returns the same value for all symbols"""
45        return self.sym_value
46
47
48class TestElf(unittest.TestCase):
49    def testAllSymbols(self):
50        """Test that we can obtain a symbol from the ELF file"""
51        fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
52        syms = elf.GetSymbols(fname, [])
53        self.assertIn('.ucode', syms)
54
55    def testRegexSymbols(self):
56        """Test that we can obtain from the ELF file by regular expression"""
57        fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
58        syms = elf.GetSymbols(fname, ['ucode'])
59        self.assertIn('.ucode', syms)
60        syms = elf.GetSymbols(fname, ['missing'])
61        self.assertNotIn('.ucode', syms)
62        syms = elf.GetSymbols(fname, ['missing', 'ucode'])
63        self.assertIn('.ucode', syms)
64
65    def testMissingFile(self):
66        """Test that a missing file is detected"""
67        entry = FakeEntry(10)
68        section = FakeSection()
69        with self.assertRaises(ValueError) as e:
70            syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
71        self.assertIn("Filename 'missing-file' not found in input path",
72                      str(e.exception))
73
74    def testOutsideFile(self):
75        """Test a symbol which extends outside the entry area is detected"""
76        entry = FakeEntry(10)
77        section = FakeSection()
78        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
79        with self.assertRaises(ValueError) as e:
80            syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
81        self.assertIn('entry_path has offset 4 (size 8) but the contents size '
82                      'is a', str(e.exception))
83
84    def testMissingImageStart(self):
85        """Test that we detect a missing __image_copy_start symbol
86
87        This is needed to mark the start of the image. Without it we cannot
88        locate the offset of a binman symbol within the image.
89        """
90        entry = FakeEntry(10)
91        section = FakeSection()
92        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
93        self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
94                         None)
95
96    def testBadSymbolSize(self):
97        """Test that an attempt to use an 8-bit symbol are detected
98
99        Only 32 and 64 bits are supported, since we need to store an offset
100        into the image.
101        """
102        entry = FakeEntry(10)
103        section = FakeSection()
104        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
105        with self.assertRaises(ValueError) as e:
106            syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
107        self.assertIn('has size 1: only 4 and 8 are supported',
108                      str(e.exception))
109
110    def testNoValue(self):
111        """Test the case where we have no value for the symbol
112
113        This should produce -1 values for all thress symbols, taking up the
114        first 16 bytes of the image.
115        """
116        entry = FakeEntry(20)
117        section = FakeSection(sym_value=None)
118        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
119        syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
120        self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
121
122    def testDebug(self):
123        """Check that enabling debug in the elf module produced debug output"""
124        elf.debug = True
125        entry = FakeEntry(20)
126        section = FakeSection()
127        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
128        with test_util.capture_sys_output() as (stdout, stderr):
129            syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
130        elf.debug = False
131        self.assertTrue(len(stdout.getvalue()) > 0)
132
133
134if __name__ == '__main__':
135    unittest.main()
136