1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2017 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
5 # Test for the elf module
14 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
18 """A fake Entry object, usedfor testing
20 This supports an entry with a given size.
22 def __init__(self, contents_size):
23 self.contents_size = contents_size
24 self.data = 'a' * contents_size
31 """A fake Section object, used for testing
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
37 def __init__(self, sym_value=1):
38 self.sym_value = sym_value
43 def LookupSymbol(self, name, weak, msg):
44 """Fake implementation which returns the same value for all symbols"""
48 class 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)
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)
65 def testMissingFile(self):
66 """Test that a missing file is detected"""
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",
74 def testOutsideFile(self):
75 """Test a symbol which extends outside the entry area is detected"""
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))
84 def testMissingImageStart(self):
85 """Test that we detect a missing __image_copy_start symbol
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.
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),
96 def testBadSymbolSize(self):
97 """Test that an attempt to use an 8-bit symbol are detected
99 Only 32 and 64 bits are supported, since we need to store an offset
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',
110 def testNoValue(self):
111 """Test the case where we have no value for the symbol
113 This should produce -1 values for all thress symbols, taking up the
114 first 16 bytes of the image.
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)
123 """Check that enabling debug in the elf module produced debug output"""
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)
131 self.assertTrue(len(stdout.getvalue()) > 0)
134 if __name__ == '__main__':