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