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
19 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
23 """A fake Entry object, usedfor testing
25 This supports an entry with a given size.
27 def __init__(self, contents_size):
28 self.contents_size = contents_size
29 self.data = tools.GetBytes(ord('a'), contents_size)
36 """A fake Section object, used for testing
38 This has the minimum feature set needed to support testing elf functions.
39 A LookupSymbol() function is provided which returns a fake value for amu
42 def __init__(self, sym_value=1):
43 self.sym_value = sym_value
48 def LookupSymbol(self, name, weak, msg):
49 """Fake implementation which returns the same value for all symbols"""
53 class TestElf(unittest.TestCase):
56 tools.SetInputDirs(['.'])
58 def testAllSymbols(self):
59 """Test that we can obtain a symbol from the ELF file"""
60 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
61 syms = elf.GetSymbols(fname, [])
62 self.assertIn('.ucode', syms)
64 def testRegexSymbols(self):
65 """Test that we can obtain from the ELF file by regular expression"""
66 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
67 syms = elf.GetSymbols(fname, ['ucode'])
68 self.assertIn('.ucode', syms)
69 syms = elf.GetSymbols(fname, ['missing'])
70 self.assertNotIn('.ucode', syms)
71 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
72 self.assertIn('.ucode', syms)
74 def testMissingFile(self):
75 """Test that a missing file is detected"""
77 section = FakeSection()
78 with self.assertRaises(ValueError) as e:
79 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
80 self.assertIn("Filename 'missing-file' not found in input path",
83 def testOutsideFile(self):
84 """Test a symbol which extends outside the entry area is detected"""
86 section = FakeSection()
87 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
88 with self.assertRaises(ValueError) as e:
89 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
90 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
91 'is a', str(e.exception))
93 def testMissingImageStart(self):
94 """Test that we detect a missing __image_copy_start symbol
96 This is needed to mark the start of the image. Without it we cannot
97 locate the offset of a binman symbol within the image.
100 section = FakeSection()
101 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
102 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
105 def testBadSymbolSize(self):
106 """Test that an attempt to use an 8-bit symbol are detected
108 Only 32 and 64 bits are supported, since we need to store an offset
111 entry = FakeEntry(10)
112 section = FakeSection()
113 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
114 with self.assertRaises(ValueError) as e:
115 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
116 self.assertIn('has size 1: only 4 and 8 are supported',
119 def testNoValue(self):
120 """Test the case where we have no value for the symbol
122 This should produce -1 values for all thress symbols, taking up the
123 first 16 bytes of the image.
125 entry = FakeEntry(20)
126 section = FakeSection(sym_value=None)
127 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
128 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
129 self.assertEqual(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4),
133 """Check that enabling debug in the elf module produced debug output"""
135 tout.Init(tout.DEBUG)
136 entry = FakeEntry(20)
137 section = FakeSection()
138 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
139 with test_util.capture_sys_output() as (stdout, stderr):
140 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
141 self.assertTrue(len(stdout.getvalue()) > 0)
143 tout.Init(tout.WARNING)
145 def testMakeElf(self):
146 """Test for the MakeElf function"""
147 outdir = tempfile.mkdtemp(prefix='elf.')
148 expected_text = b'1234'
149 expected_data = b'wxyz'
150 elf_fname = os.path.join(outdir, 'elf')
151 bin_fname = os.path.join(outdir, 'bin')
153 # Make an Elf file and then convert it to a fkat binary file. This
154 # should produce the original data.
155 elf.MakeElf(elf_fname, expected_text, expected_data)
156 stdout = command.Output('objcopy', '-O', 'binary', elf_fname, bin_fname)
157 with open(bin_fname, 'rb') as fd:
159 self.assertEqual(expected_text + expected_data, data)
160 shutil.rmtree(outdir)
162 def testDecodeElf(self):
163 """Test for the MakeElf function"""
164 if not elf.ELF_TOOLS:
165 self.skipTest('Python elftools not available')
166 outdir = tempfile.mkdtemp(prefix='elf.')
167 expected_text = b'1234'
168 expected_data = b'wxyz'
169 elf_fname = os.path.join(outdir, 'elf')
170 elf.MakeElf(elf_fname, expected_text, expected_data)
171 data = tools.ReadFile(elf_fname)
175 expected = expected_text + expected_data
176 self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
177 elf.DecodeElf(data, 0))
178 self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
179 load, entry, len(expected)),
180 elf.DecodeElf(data, load + 2))
181 #shutil.rmtree(outdir)
184 if __name__ == '__main__':