blob: 5a51c64cfee32812bf1a94d5b43050a725a9c119 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass24ad3652017-11-13 18:54:54 -07002# Copyright (c) 2017 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass24ad3652017-11-13 18:54:54 -07005# Test for the elf module
6
7import os
Simon Glass4f379ea2019-07-08 13:18:34 -06008import shutil
Simon Glassa4e259e2021-11-03 21:09:16 -06009import struct
Simon Glass24ad3652017-11-13 18:54:54 -070010import sys
Simon Glass4f379ea2019-07-08 13:18:34 -060011import tempfile
Simon Glass24ad3652017-11-13 18:54:54 -070012import unittest
13
Simon Glassc585dd42020-04-17 18:09:03 -060014from binman import elf
Simon Glassa997ea52020-04-17 18:09:04 -060015from patman import command
16from patman import test_util
17from patman import tools
18from patman import tout
Simon Glass24ad3652017-11-13 18:54:54 -070019
20binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass4ca8e042017-11-13 18:55:01 -070021
Simon Glass4ca8e042017-11-13 18:55:01 -070022
23class FakeEntry:
Simon Glass4114f972018-07-17 13:25:26 -060024 """A fake Entry object, usedfor testing
25
26 This supports an entry with a given size.
27 """
Simon Glass4ca8e042017-11-13 18:55:01 -070028 def __init__(self, contents_size):
29 self.contents_size = contents_size
Simon Glass80025522022-01-29 14:14:04 -070030 self.data = tools.get_bytes(ord('a'), contents_size)
Simon Glass4ca8e042017-11-13 18:55:01 -070031
32 def GetPath(self):
33 return 'entry_path'
34
Simon Glass4114f972018-07-17 13:25:26 -060035
Simon Glass8a6f56e2018-06-01 09:38:13 -060036class FakeSection:
Simon Glass4114f972018-07-17 13:25:26 -060037 """A fake Section object, used for testing
38
39 This has the minimum feature set needed to support testing elf functions.
40 A LookupSymbol() function is provided which returns a fake value for amu
41 symbol requested.
42 """
Simon Glass4ca8e042017-11-13 18:55:01 -070043 def __init__(self, sym_value=1):
44 self.sym_value = sym_value
45
46 def GetPath(self):
Simon Glass8a6f56e2018-06-01 09:38:13 -060047 return 'section_path'
Simon Glass4ca8e042017-11-13 18:55:01 -070048
Simon Glassecbe4732021-01-06 21:35:15 -070049 def LookupImageSymbol(self, name, weak, msg, base_addr):
Simon Glass4114f972018-07-17 13:25:26 -060050 """Fake implementation which returns the same value for all symbols"""
Simon Glass4ca8e042017-11-13 18:55:01 -070051 return self.sym_value
Simon Glass24ad3652017-11-13 18:54:54 -070052
Simon Glassecbe4732021-01-06 21:35:15 -070053 def GetImage(self):
54 return self
Simon Glass4114f972018-07-17 13:25:26 -060055
Simon Glassf6290892019-08-24 07:22:53 -060056def BuildElfTestFiles(target_dir):
57 """Build ELF files used for testing in binman
58
Simon Glass571adc82022-02-08 11:49:55 -070059 This compiles and links the test files into the specified directory. It uses
60 the Makefile and source files in the binman test/ directory.
Simon Glassf6290892019-08-24 07:22:53 -060061
62 Args:
63 target_dir: Directory to put the files into
64 """
65 if not os.path.exists(target_dir):
66 os.mkdir(target_dir)
67 testdir = os.path.join(binman_dir, 'test')
68
69 # If binman is involved from the main U-Boot Makefile the -r and -R
70 # flags are set in MAKEFLAGS. This prevents this Makefile from working
71 # correctly. So drop any make flags here.
72 if 'MAKEFLAGS' in os.environ:
73 del os.environ['MAKEFLAGS']
Simon Glass271fd8f2021-11-03 21:09:15 -060074 try:
Simon Glass80025522022-01-29 14:14:04 -070075 tools.run('make', '-C', target_dir, '-f',
Simon Glass271fd8f2021-11-03 21:09:15 -060076 os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
77 except ValueError as e:
78 # The test system seems to suppress this in a strange way
79 print(e)
Simon Glassf6290892019-08-24 07:22:53 -060080
81
Simon Glass24ad3652017-11-13 18:54:54 -070082class TestElf(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060083 @classmethod
Simon Glass4affd4b2019-08-24 07:22:54 -060084 def setUpClass(cls):
85 cls._indir = tempfile.mkdtemp(prefix='elf.')
Simon Glass80025522022-01-29 14:14:04 -070086 tools.set_input_dirs(['.'])
Simon Glass4affd4b2019-08-24 07:22:54 -060087 BuildElfTestFiles(cls._indir)
88
89 @classmethod
90 def tearDownClass(cls):
91 if cls._indir:
92 shutil.rmtree(cls._indir)
93
94 @classmethod
95 def ElfTestFile(cls, fname):
96 return os.path.join(cls._indir, fname)
Simon Glass752e7552018-10-01 21:12:41 -060097
Simon Glass24ad3652017-11-13 18:54:54 -070098 def testAllSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -060099 """Test that we can obtain a symbol from the ELF file"""
Simon Glass4affd4b2019-08-24 07:22:54 -0600100 fname = self.ElfTestFile('u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -0700101 syms = elf.GetSymbols(fname, [])
Simon Glass315400e2022-01-09 20:13:37 -0700102 self.assertIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700103
104 def testRegexSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -0600105 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass4affd4b2019-08-24 07:22:54 -0600106 fname = self.ElfTestFile('u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -0700107 syms = elf.GetSymbols(fname, ['ucode'])
Simon Glass315400e2022-01-09 20:13:37 -0700108 self.assertIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700109 syms = elf.GetSymbols(fname, ['missing'])
Simon Glass315400e2022-01-09 20:13:37 -0700110 self.assertNotIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700111 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
Simon Glass315400e2022-01-09 20:13:37 -0700112 self.assertIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700113
Simon Glass4ca8e042017-11-13 18:55:01 -0700114 def testMissingFile(self):
Simon Glass4114f972018-07-17 13:25:26 -0600115 """Test that a missing file is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700116 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600117 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -0700118 with self.assertRaises(ValueError) as e:
Simon Glass2a0fa982022-02-11 13:23:21 -0700119 elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700120 self.assertIn("Filename 'missing-file' not found in input path",
121 str(e.exception))
122
123 def testOutsideFile(self):
Simon Glass4114f972018-07-17 13:25:26 -0600124 """Test a symbol which extends outside the entry area is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700125 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600126 section = FakeSection()
Simon Glass5d0c0262019-08-24 07:22:56 -0600127 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass4ca8e042017-11-13 18:55:01 -0700128 with self.assertRaises(ValueError) as e:
Simon Glass2a0fa982022-02-11 13:23:21 -0700129 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300130 self.assertIn('entry_path has offset 8 (size 8) but the contents size '
Simon Glass4ca8e042017-11-13 18:55:01 -0700131 'is a', str(e.exception))
132
133 def testMissingImageStart(self):
Simon Glass4114f972018-07-17 13:25:26 -0600134 """Test that we detect a missing __image_copy_start symbol
135
136 This is needed to mark the start of the image. Without it we cannot
137 locate the offset of a binman symbol within the image.
138 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700139 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600140 section = FakeSection()
Simon Glass46ea6912019-08-24 07:22:58 -0600141 elf_fname = self.ElfTestFile('u_boot_binman_syms_bad')
Simon Glass2a0fa982022-02-11 13:23:21 -0700142 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700143
144 def testBadSymbolSize(self):
Simon Glass4114f972018-07-17 13:25:26 -0600145 """Test that an attempt to use an 8-bit symbol are detected
146
147 Only 32 and 64 bits are supported, since we need to store an offset
148 into the image.
149 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700150 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600151 section = FakeSection()
Simon Glassb8deb122019-08-24 07:22:57 -0600152 elf_fname =self.ElfTestFile('u_boot_binman_syms_size')
Simon Glass4ca8e042017-11-13 18:55:01 -0700153 with self.assertRaises(ValueError) as e:
Simon Glass2a0fa982022-02-11 13:23:21 -0700154 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700155 self.assertIn('has size 1: only 4 and 8 are supported',
156 str(e.exception))
157
158 def testNoValue(self):
Simon Glass4114f972018-07-17 13:25:26 -0600159 """Test the case where we have no value for the symbol
160
161 This should produce -1 values for all thress symbols, taking up the
162 first 16 bytes of the image.
163 """
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300164 entry = FakeEntry(28)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600165 section = FakeSection(sym_value=None)
Simon Glass5d0c0262019-08-24 07:22:56 -0600166 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass2a0fa982022-02-11 13:23:21 -0700167 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300168 expected = (struct.pack('<L', elf.BINMAN_SYM_MAGIC_VALUE) +
169 tools.get_bytes(255, 20) +
170 tools.get_bytes(ord('a'), 4))
171 self.assertEqual(expected, entry.data)
Simon Glass4ca8e042017-11-13 18:55:01 -0700172
173 def testDebug(self):
Simon Glass4114f972018-07-17 13:25:26 -0600174 """Check that enabling debug in the elf module produced debug output"""
Simon Glassb6dff4c2019-07-20 12:23:36 -0600175 try:
Simon Glass011f1b32022-01-29 14:14:15 -0700176 tout.init(tout.DEBUG)
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300177 entry = FakeEntry(24)
Simon Glassb6dff4c2019-07-20 12:23:36 -0600178 section = FakeSection()
Simon Glass5d0c0262019-08-24 07:22:56 -0600179 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glassb6dff4c2019-07-20 12:23:36 -0600180 with test_util.capture_sys_output() as (stdout, stderr):
Simon Glass2a0fa982022-02-11 13:23:21 -0700181 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glassb6dff4c2019-07-20 12:23:36 -0600182 self.assertTrue(len(stdout.getvalue()) > 0)
183 finally:
Simon Glass011f1b32022-01-29 14:14:15 -0700184 tout.init(tout.WARNING)
Simon Glass4ca8e042017-11-13 18:55:01 -0700185
Simon Glass4f379ea2019-07-08 13:18:34 -0600186 def testMakeElf(self):
187 """Test for the MakeElf function"""
188 outdir = tempfile.mkdtemp(prefix='elf.')
189 expected_text = b'1234'
190 expected_data = b'wxyz'
191 elf_fname = os.path.join(outdir, 'elf')
Simon Glassd349ada2019-08-24 07:22:45 -0600192 bin_fname = os.path.join(outdir, 'bin')
Simon Glass4f379ea2019-07-08 13:18:34 -0600193
194 # Make an Elf file and then convert it to a fkat binary file. This
195 # should produce the original data.
196 elf.MakeElf(elf_fname, expected_text, expected_data)
Simon Glass80025522022-01-29 14:14:04 -0700197 objcopy, args = tools.get_target_compile_tool('objcopy')
Alper Nebi Yasak5cd321d2020-09-06 14:46:05 +0300198 args += ['-O', 'binary', elf_fname, bin_fname]
Simon Glass840be732022-01-29 14:14:05 -0700199 stdout = command.output(objcopy, *args)
Simon Glass4f379ea2019-07-08 13:18:34 -0600200 with open(bin_fname, 'rb') as fd:
201 data = fd.read()
202 self.assertEqual(expected_text + expected_data, data)
203 shutil.rmtree(outdir)
204
Simon Glass567b6822019-07-08 13:18:35 -0600205 def testDecodeElf(self):
206 """Test for the MakeElf function"""
207 if not elf.ELF_TOOLS:
208 self.skipTest('Python elftools not available')
209 outdir = tempfile.mkdtemp(prefix='elf.')
210 expected_text = b'1234'
211 expected_data = b'wxyz'
212 elf_fname = os.path.join(outdir, 'elf')
213 elf.MakeElf(elf_fname, expected_text, expected_data)
Simon Glass80025522022-01-29 14:14:04 -0700214 data = tools.read_file(elf_fname)
Simon Glass567b6822019-07-08 13:18:35 -0600215
216 load = 0xfef20000
217 entry = load + 2
218 expected = expected_text + expected_data
219 self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
220 elf.DecodeElf(data, 0))
221 self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
222 load, entry, len(expected)),
223 elf.DecodeElf(data, load + 2))
Simon Glass4affd4b2019-08-24 07:22:54 -0600224 shutil.rmtree(outdir)
Simon Glass567b6822019-07-08 13:18:35 -0600225
Simon Glassa4e259e2021-11-03 21:09:16 -0600226 def testEmbedData(self):
227 """Test for the GetSymbolFileOffset() function"""
228 if not elf.ELF_TOOLS:
229 self.skipTest('Python elftools not available')
230
231 fname = self.ElfTestFile('embed_data')
232 offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
233 start = offset['embed_start'].offset
234 end = offset['embed_end'].offset
Simon Glass80025522022-01-29 14:14:04 -0700235 data = tools.read_file(fname)
Simon Glassa4e259e2021-11-03 21:09:16 -0600236 embed_data = data[start:end]
237 expect = struct.pack('<III', 0x1234, 0x5678, 0)
238 self.assertEqual(expect, embed_data)
239
240 def testEmbedFail(self):
241 """Test calling GetSymbolFileOffset() without elftools"""
242 try:
243 old_val = elf.ELF_TOOLS
244 elf.ELF_TOOLS = False
245 fname = self.ElfTestFile('embed_data')
246 with self.assertRaises(ValueError) as e:
247 elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
Simon Glassacc03752022-03-05 20:18:57 -0700248 self.assertIn("Python: No module named 'elftools'",
Simon Glassa4e259e2021-11-03 21:09:16 -0600249 str(e.exception))
250 finally:
251 elf.ELF_TOOLS = old_val
252
253 def testEmbedDataNoSym(self):
254 """Test for GetSymbolFileOffset() getting no symbols"""
255 if not elf.ELF_TOOLS:
256 self.skipTest('Python elftools not available')
257
258 fname = self.ElfTestFile('embed_data')
259 offset = elf.GetSymbolFileOffset(fname, ['missing_sym'])
260 self.assertEqual({}, offset)
261
Simon Glassacc03752022-03-05 20:18:57 -0700262 def test_read_loadable_segments(self):
263 """Test for read_loadable_segments()"""
Simon Glass571adc82022-02-08 11:49:55 -0700264 if not elf.ELF_TOOLS:
265 self.skipTest('Python elftools not available')
266 fname = self.ElfTestFile('embed_data')
Simon Glassacc03752022-03-05 20:18:57 -0700267 segments, entry = elf.read_loadable_segments(tools.read_file(fname))
Simon Glass571adc82022-02-08 11:49:55 -0700268
269 def test_read_segments_fail(self):
Simon Glassacc03752022-03-05 20:18:57 -0700270 """Test for read_loadable_segments() without elftools"""
Simon Glass571adc82022-02-08 11:49:55 -0700271 try:
272 old_val = elf.ELF_TOOLS
273 elf.ELF_TOOLS = False
274 fname = self.ElfTestFile('embed_data')
275 with self.assertRaises(ValueError) as e:
Simon Glassacc03752022-03-05 20:18:57 -0700276 elf.read_loadable_segments(tools.read_file(fname))
277 self.assertIn("Python: No module named 'elftools'",
Simon Glass571adc82022-02-08 11:49:55 -0700278 str(e.exception))
279 finally:
280 elf.ELF_TOOLS = old_val
281
282 def test_read_segments_bad_data(self):
Simon Glassacc03752022-03-05 20:18:57 -0700283 """Test for read_loadable_segments() with an invalid ELF file"""
Simon Glass571adc82022-02-08 11:49:55 -0700284 fname = self.ElfTestFile('embed_data')
285 with self.assertRaises(ValueError) as e:
Simon Glassacc03752022-03-05 20:18:57 -0700286 elf.read_loadable_segments(tools.get_bytes(100, 100))
Simon Glass571adc82022-02-08 11:49:55 -0700287 self.assertIn('Magic number does not match', str(e.exception))
288
Simon Glassea64c022022-03-18 19:19:49 -0600289 def test_get_file_offset(self):
290 """Test GetFileOffset() gives the correct file offset for a symbol"""
291 fname = self.ElfTestFile('embed_data')
292 syms = elf.GetSymbols(fname, ['embed'])
293 addr = syms['embed'].address
294 offset = elf.GetFileOffset(fname, addr)
295 data = tools.read_file(fname)
296
297 # Just use the first 4 bytes and assume it is little endian
298 embed_data = data[offset:offset + 4]
299 embed_value = struct.unpack('<I', embed_data)[0]
300 self.assertEqual(0x1234, embed_value)
301
302 def test_get_file_offset_fail(self):
303 """Test calling GetFileOffset() without elftools"""
304 try:
305 old_val = elf.ELF_TOOLS
306 elf.ELF_TOOLS = False
307 fname = self.ElfTestFile('embed_data')
308 with self.assertRaises(ValueError) as e:
309 elf.GetFileOffset(fname, 0)
310 self.assertIn("Python: No module named 'elftools'",
311 str(e.exception))
312 finally:
313 elf.ELF_TOOLS = old_val
314
315 def test_get_symbol_from_address(self):
316 """Test GetSymbolFromAddress()"""
317 fname = self.ElfTestFile('elf_sections')
318 sym_name = 'calculate'
319 syms = elf.GetSymbols(fname, [sym_name])
320 addr = syms[sym_name].address
321 sym = elf.GetSymbolFromAddress(fname, addr)
322 self.assertEqual(sym_name, sym)
323
324 def test_get_symbol_from_address_fail(self):
325 """Test calling GetSymbolFromAddress() without elftools"""
326 try:
327 old_val = elf.ELF_TOOLS
328 elf.ELF_TOOLS = False
329 fname = self.ElfTestFile('embed_data')
330 with self.assertRaises(ValueError) as e:
331 elf.GetSymbolFromAddress(fname, 0x1000)
332 self.assertIn("Python: No module named 'elftools'",
333 str(e.exception))
334 finally:
335 elf.ELF_TOOLS = old_val
336
Simon Glass4ca8e042017-11-13 18:55:01 -0700337
Simon Glass24ad3652017-11-13 18:54:54 -0700338if __name__ == '__main__':
339 unittest.main()