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
15 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
19 """A fake Entry object, usedfor testing
21 This supports an entry with a given size.
23 def __init__(self, contents_size):
24 self.contents_size = contents_size
25 self.data = tools.GetBytes(ord('a'), contents_size)
32 """A fake Section object, used for testing
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
38 def __init__(self, sym_value=1):
39 self.sym_value = sym_value
44 def LookupSymbol(self, name, weak, msg):
45 """Fake implementation which returns the same value for all symbols"""
49 class TestElf(unittest.TestCase):
52 tools.SetInputDirs(['.'])
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)
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)
70 def testMissingFile(self):
71 """Test that a missing file is detected"""
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",
79 def testOutsideFile(self):
80 """Test a symbol which extends outside the entry area is detected"""
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))
89 def testMissingImageStart(self):
90 """Test that we detect a missing __image_copy_start symbol
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.
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),
101 def testBadSymbolSize(self):
102 """Test that an attempt to use an 8-bit symbol are detected
104 Only 32 and 64 bits are supported, since we need to store an offset
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',
115 def testNoValue(self):
116 """Test the case where we have no value for the symbol
118 This should produce -1 values for all thress symbols, taking up the
119 first 16 bytes of the image.
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(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4),
129 """Check that enabling debug in the elf module produced debug output"""
131 entry = FakeEntry(20)
132 section = FakeSection()
133 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
134 with test_util.capture_sys_output() as (stdout, stderr):
135 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
137 self.assertTrue(len(stdout.getvalue()) > 0)
140 if __name__ == '__main__':