blob: 5b173392898614fcbeedb4f0bf106ad51c66810e [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 Glass131444f2023-02-23 18:18:04 -070015from u_boot_pylib import command
Simon Glass14d64e32025-04-29 07:21:59 -060016from u_boot_pylib import terminal
Simon Glass131444f2023-02-23 18:18:04 -070017from u_boot_pylib import test_util
18from u_boot_pylib import tools
19from u_boot_pylib import tout
Simon Glass24ad3652017-11-13 18:54:54 -070020
21binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass4ca8e042017-11-13 18:55:01 -070022
Simon Glass4ca8e042017-11-13 18:55:01 -070023
24class FakeEntry:
Simon Glass4114f972018-07-17 13:25:26 -060025 """A fake Entry object, usedfor testing
26
27 This supports an entry with a given size.
28 """
Simon Glass4ca8e042017-11-13 18:55:01 -070029 def __init__(self, contents_size):
30 self.contents_size = contents_size
Simon Glass80025522022-01-29 14:14:04 -070031 self.data = tools.get_bytes(ord('a'), contents_size)
Simon Glass4ca8e042017-11-13 18:55:01 -070032
33 def GetPath(self):
34 return 'entry_path'
35
Simon Glass4114f972018-07-17 13:25:26 -060036
Simon Glass8a6f56e2018-06-01 09:38:13 -060037class FakeSection:
Simon Glass4114f972018-07-17 13:25:26 -060038 """A fake Section object, used for testing
39
40 This has the minimum feature set needed to support testing elf functions.
Simon Glass65cf1ca2024-08-26 13:11:38 -060041 A GetSymbolValue() function is provided which returns a fake value for any
Simon Glass4114f972018-07-17 13:25:26 -060042 symbol requested.
43 """
Simon Glass4ca8e042017-11-13 18:55:01 -070044 def __init__(self, sym_value=1):
45 self.sym_value = sym_value
46
47 def GetPath(self):
Simon Glass8a6f56e2018-06-01 09:38:13 -060048 return 'section_path'
Simon Glass4ca8e042017-11-13 18:55:01 -070049
Simon Glass65cf1ca2024-08-26 13:11:38 -060050 def GetImageSymbolValue(self, name, weak, msg, base_addr):
Simon Glass4114f972018-07-17 13:25:26 -060051 """Fake implementation which returns the same value for all symbols"""
Simon Glass4ca8e042017-11-13 18:55:01 -070052 return self.sym_value
Simon Glass24ad3652017-11-13 18:54:54 -070053
Simon Glassecbe4732021-01-06 21:35:15 -070054 def GetImage(self):
55 return self
Simon Glass4114f972018-07-17 13:25:26 -060056
Simon Glassf6290892019-08-24 07:22:53 -060057def BuildElfTestFiles(target_dir):
58 """Build ELF files used for testing in binman
59
Simon Glass571adc82022-02-08 11:49:55 -070060 This compiles and links the test files into the specified directory. It uses
61 the Makefile and source files in the binman test/ directory.
Simon Glassf6290892019-08-24 07:22:53 -060062
63 Args:
64 target_dir: Directory to put the files into
65 """
66 if not os.path.exists(target_dir):
67 os.mkdir(target_dir)
68 testdir = os.path.join(binman_dir, 'test')
69
70 # If binman is involved from the main U-Boot Makefile the -r and -R
71 # flags are set in MAKEFLAGS. This prevents this Makefile from working
72 # correctly. So drop any make flags here.
73 if 'MAKEFLAGS' in os.environ:
74 del os.environ['MAKEFLAGS']
Simon Glass271fd8f2021-11-03 21:09:15 -060075 try:
Simon Glass80025522022-01-29 14:14:04 -070076 tools.run('make', '-C', target_dir, '-f',
Simon Glass271fd8f2021-11-03 21:09:15 -060077 os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
78 except ValueError as e:
79 # The test system seems to suppress this in a strange way
80 print(e)
Simon Glassf6290892019-08-24 07:22:53 -060081
82
Simon Glass24ad3652017-11-13 18:54:54 -070083class TestElf(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060084 @classmethod
Simon Glass4affd4b2019-08-24 07:22:54 -060085 def setUpClass(cls):
86 cls._indir = tempfile.mkdtemp(prefix='elf.')
Simon Glass80025522022-01-29 14:14:04 -070087 tools.set_input_dirs(['.'])
Simon Glass4affd4b2019-08-24 07:22:54 -060088 BuildElfTestFiles(cls._indir)
89
90 @classmethod
91 def tearDownClass(cls):
92 if cls._indir:
93 shutil.rmtree(cls._indir)
94
95 @classmethod
96 def ElfTestFile(cls, fname):
97 return os.path.join(cls._indir, fname)
Simon Glass752e7552018-10-01 21:12:41 -060098
Simon Glass24ad3652017-11-13 18:54:54 -070099 def testAllSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -0600100 """Test that we can obtain a symbol from the ELF file"""
Simon Glass4affd4b2019-08-24 07:22:54 -0600101 fname = self.ElfTestFile('u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -0700102 syms = elf.GetSymbols(fname, [])
Simon Glass315400e2022-01-09 20:13:37 -0700103 self.assertIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700104
105 def testRegexSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -0600106 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass4affd4b2019-08-24 07:22:54 -0600107 fname = self.ElfTestFile('u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -0700108 syms = elf.GetSymbols(fname, ['ucode'])
Simon Glass315400e2022-01-09 20:13:37 -0700109 self.assertIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700110 syms = elf.GetSymbols(fname, ['missing'])
Simon Glass315400e2022-01-09 20:13:37 -0700111 self.assertNotIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700112 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
Simon Glass315400e2022-01-09 20:13:37 -0700113 self.assertIn('_dt_ucode_base_size', syms)
Simon Glass24ad3652017-11-13 18:54:54 -0700114
Simon Glass4ca8e042017-11-13 18:55:01 -0700115 def testMissingFile(self):
Simon Glass4114f972018-07-17 13:25:26 -0600116 """Test that a missing file is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700117 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600118 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -0700119 with self.assertRaises(ValueError) as e:
Simon Glass2a0fa982022-02-11 13:23:21 -0700120 elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700121 self.assertIn("Filename 'missing-file' not found in input path",
122 str(e.exception))
123
124 def testOutsideFile(self):
Simon Glass4114f972018-07-17 13:25:26 -0600125 """Test a symbol which extends outside the entry area is detected"""
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200126 if not elf.ELF_TOOLS:
127 self.skipTest('Python elftools not available')
Simon Glass4ca8e042017-11-13 18:55:01 -0700128 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600129 section = FakeSection()
Simon Glass5d0c0262019-08-24 07:22:56 -0600130 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass4ca8e042017-11-13 18:55:01 -0700131 with self.assertRaises(ValueError) as e:
Simon Glass2a0fa982022-02-11 13:23:21 -0700132 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300133 self.assertIn('entry_path has offset 8 (size 8) but the contents size '
Simon Glass4ca8e042017-11-13 18:55:01 -0700134 'is a', str(e.exception))
135
136 def testMissingImageStart(self):
Simon Glass4114f972018-07-17 13:25:26 -0600137 """Test that we detect a missing __image_copy_start symbol
138
139 This is needed to mark the start of the image. Without it we cannot
140 locate the offset of a binman symbol within the image.
141 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700142 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600143 section = FakeSection()
Simon Glass46ea6912019-08-24 07:22:58 -0600144 elf_fname = self.ElfTestFile('u_boot_binman_syms_bad')
Simon Glassea1d77d2023-07-18 07:23:56 -0600145 count = elf.LookupAndWriteSymbols(elf_fname, entry, section)
146 self.assertEqual(0, count)
Simon Glass4ca8e042017-11-13 18:55:01 -0700147
148 def testBadSymbolSize(self):
Simon Glass4114f972018-07-17 13:25:26 -0600149 """Test that an attempt to use an 8-bit symbol are detected
150
151 Only 32 and 64 bits are supported, since we need to store an offset
152 into the image.
153 """
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200154 if not elf.ELF_TOOLS:
155 self.skipTest('Python elftools not available')
Simon Glass4ca8e042017-11-13 18:55:01 -0700156 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600157 section = FakeSection()
Simon Glassb8deb122019-08-24 07:22:57 -0600158 elf_fname =self.ElfTestFile('u_boot_binman_syms_size')
Simon Glass4ca8e042017-11-13 18:55:01 -0700159 with self.assertRaises(ValueError) as e:
Simon Glass2a0fa982022-02-11 13:23:21 -0700160 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700161 self.assertIn('has size 1: only 4 and 8 are supported',
162 str(e.exception))
163
164 def testNoValue(self):
Simon Glass4114f972018-07-17 13:25:26 -0600165 """Test the case where we have no value for the symbol
166
Simon Glassea1d77d2023-07-18 07:23:56 -0600167 This should produce -1 values for all three symbols, taking up the
Simon Glass4114f972018-07-17 13:25:26 -0600168 first 16 bytes of the image.
169 """
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200170 if not elf.ELF_TOOLS:
171 self.skipTest('Python elftools not available')
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300172 entry = FakeEntry(28)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600173 section = FakeSection(sym_value=None)
Simon Glass5d0c0262019-08-24 07:22:56 -0600174 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glassea1d77d2023-07-18 07:23:56 -0600175 count = elf.LookupAndWriteSymbols(elf_fname, entry, section)
176 self.assertEqual(5, count)
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300177 expected = (struct.pack('<L', elf.BINMAN_SYM_MAGIC_VALUE) +
178 tools.get_bytes(255, 20) +
179 tools.get_bytes(ord('a'), 4))
180 self.assertEqual(expected, entry.data)
Simon Glass4ca8e042017-11-13 18:55:01 -0700181
182 def testDebug(self):
Simon Glass4114f972018-07-17 13:25:26 -0600183 """Check that enabling debug in the elf module produced debug output"""
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200184 if not elf.ELF_TOOLS:
185 self.skipTest('Python elftools not available')
Simon Glassb6dff4c2019-07-20 12:23:36 -0600186 try:
Simon Glass011f1b32022-01-29 14:14:15 -0700187 tout.init(tout.DEBUG)
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +0300188 entry = FakeEntry(24)
Simon Glassb6dff4c2019-07-20 12:23:36 -0600189 section = FakeSection()
Simon Glass5d0c0262019-08-24 07:22:56 -0600190 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass14d64e32025-04-29 07:21:59 -0600191 with terminal.capture() as (stdout, stderr):
Simon Glass2a0fa982022-02-11 13:23:21 -0700192 elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glassb6dff4c2019-07-20 12:23:36 -0600193 self.assertTrue(len(stdout.getvalue()) > 0)
194 finally:
Simon Glass011f1b32022-01-29 14:14:15 -0700195 tout.init(tout.WARNING)
Simon Glass4ca8e042017-11-13 18:55:01 -0700196
Simon Glass4f379ea2019-07-08 13:18:34 -0600197 def testMakeElf(self):
198 """Test for the MakeElf function"""
199 outdir = tempfile.mkdtemp(prefix='elf.')
200 expected_text = b'1234'
201 expected_data = b'wxyz'
202 elf_fname = os.path.join(outdir, 'elf')
Simon Glassd349ada2019-08-24 07:22:45 -0600203 bin_fname = os.path.join(outdir, 'bin')
Simon Glass4f379ea2019-07-08 13:18:34 -0600204
205 # Make an Elf file and then convert it to a fkat binary file. This
206 # should produce the original data.
207 elf.MakeElf(elf_fname, expected_text, expected_data)
Simon Glass80025522022-01-29 14:14:04 -0700208 objcopy, args = tools.get_target_compile_tool('objcopy')
Alper Nebi Yasak5cd321d2020-09-06 14:46:05 +0300209 args += ['-O', 'binary', elf_fname, bin_fname]
Simon Glass840be732022-01-29 14:14:05 -0700210 stdout = command.output(objcopy, *args)
Simon Glass4f379ea2019-07-08 13:18:34 -0600211 with open(bin_fname, 'rb') as fd:
212 data = fd.read()
213 self.assertEqual(expected_text + expected_data, data)
214 shutil.rmtree(outdir)
215
Simon Glass567b6822019-07-08 13:18:35 -0600216 def testDecodeElf(self):
217 """Test for the MakeElf function"""
218 if not elf.ELF_TOOLS:
219 self.skipTest('Python elftools not available')
220 outdir = tempfile.mkdtemp(prefix='elf.')
221 expected_text = b'1234'
222 expected_data = b'wxyz'
223 elf_fname = os.path.join(outdir, 'elf')
224 elf.MakeElf(elf_fname, expected_text, expected_data)
Simon Glass80025522022-01-29 14:14:04 -0700225 data = tools.read_file(elf_fname)
Simon Glass567b6822019-07-08 13:18:35 -0600226
227 load = 0xfef20000
228 entry = load + 2
229 expected = expected_text + expected_data
230 self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
231 elf.DecodeElf(data, 0))
232 self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
233 load, entry, len(expected)),
234 elf.DecodeElf(data, load + 2))
Simon Glass4affd4b2019-08-24 07:22:54 -0600235 shutil.rmtree(outdir)
Simon Glass567b6822019-07-08 13:18:35 -0600236
Simon Glassa4e259e2021-11-03 21:09:16 -0600237 def testEmbedData(self):
238 """Test for the GetSymbolFileOffset() function"""
239 if not elf.ELF_TOOLS:
240 self.skipTest('Python elftools not available')
241
242 fname = self.ElfTestFile('embed_data')
243 offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
244 start = offset['embed_start'].offset
245 end = offset['embed_end'].offset
Simon Glass80025522022-01-29 14:14:04 -0700246 data = tools.read_file(fname)
Simon Glassa4e259e2021-11-03 21:09:16 -0600247 embed_data = data[start:end]
Simon Glass6490d4b2023-01-23 11:29:41 -0700248 expect = struct.pack('<IIIII', 2, 3, 0x1234, 0x5678, 0)
Simon Glassa4e259e2021-11-03 21:09:16 -0600249 self.assertEqual(expect, embed_data)
250
251 def testEmbedFail(self):
252 """Test calling GetSymbolFileOffset() without elftools"""
Heinrich Schuchardt85902cd2023-12-09 19:50:31 +0100253 old_val = elf.ELF_TOOLS
Simon Glassa4e259e2021-11-03 21:09:16 -0600254 try:
Simon Glassa4e259e2021-11-03 21:09:16 -0600255 elf.ELF_TOOLS = False
256 fname = self.ElfTestFile('embed_data')
257 with self.assertRaises(ValueError) as e:
258 elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
Lukas Funkeabd46262023-07-18 13:53:09 +0200259 with self.assertRaises(ValueError) as e:
260 elf.DecodeElf(tools.read_file(fname), 0xdeadbeef)
261 with self.assertRaises(ValueError) as e:
262 elf.GetFileOffset(fname, 0xdeadbeef)
263 with self.assertRaises(ValueError) as e:
264 elf.GetSymbolFromAddress(fname, 0xdeadbeef)
265 with self.assertRaises(ValueError) as e:
266 entry = FakeEntry(10)
267 section = FakeSection()
268 elf.LookupAndWriteSymbols(fname, entry, section, True)
269
270 self.assertIn(
271 "Section 'section_path': entry 'entry_path': Cannot write symbols to an ELF file without Python elftools",
272 str(e.exception))
Simon Glassa4e259e2021-11-03 21:09:16 -0600273 finally:
274 elf.ELF_TOOLS = old_val
275
276 def testEmbedDataNoSym(self):
277 """Test for GetSymbolFileOffset() getting no symbols"""
278 if not elf.ELF_TOOLS:
279 self.skipTest('Python elftools not available')
280
281 fname = self.ElfTestFile('embed_data')
282 offset = elf.GetSymbolFileOffset(fname, ['missing_sym'])
283 self.assertEqual({}, offset)
284
Simon Glassacc03752022-03-05 20:18:57 -0700285 def test_read_loadable_segments(self):
286 """Test for read_loadable_segments()"""
Simon Glass571adc82022-02-08 11:49:55 -0700287 if not elf.ELF_TOOLS:
288 self.skipTest('Python elftools not available')
289 fname = self.ElfTestFile('embed_data')
Simon Glassacc03752022-03-05 20:18:57 -0700290 segments, entry = elf.read_loadable_segments(tools.read_file(fname))
Simon Glass571adc82022-02-08 11:49:55 -0700291
292 def test_read_segments_fail(self):
Simon Glassacc03752022-03-05 20:18:57 -0700293 """Test for read_loadable_segments() without elftools"""
Heinrich Schuchardt85902cd2023-12-09 19:50:31 +0100294 old_val = elf.ELF_TOOLS
Simon Glass571adc82022-02-08 11:49:55 -0700295 try:
Simon Glass571adc82022-02-08 11:49:55 -0700296 elf.ELF_TOOLS = False
297 fname = self.ElfTestFile('embed_data')
298 with self.assertRaises(ValueError) as e:
Simon Glassacc03752022-03-05 20:18:57 -0700299 elf.read_loadable_segments(tools.read_file(fname))
300 self.assertIn("Python: No module named 'elftools'",
Simon Glass571adc82022-02-08 11:49:55 -0700301 str(e.exception))
302 finally:
303 elf.ELF_TOOLS = old_val
304
305 def test_read_segments_bad_data(self):
Simon Glassacc03752022-03-05 20:18:57 -0700306 """Test for read_loadable_segments() with an invalid ELF file"""
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200307 if not elf.ELF_TOOLS:
308 self.skipTest('Python elftools not available')
Simon Glass571adc82022-02-08 11:49:55 -0700309 fname = self.ElfTestFile('embed_data')
310 with self.assertRaises(ValueError) as e:
Simon Glassacc03752022-03-05 20:18:57 -0700311 elf.read_loadable_segments(tools.get_bytes(100, 100))
Simon Glass571adc82022-02-08 11:49:55 -0700312 self.assertIn('Magic number does not match', str(e.exception))
313
Simon Glassea64c022022-03-18 19:19:49 -0600314 def test_get_file_offset(self):
315 """Test GetFileOffset() gives the correct file offset for a symbol"""
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200316 if not elf.ELF_TOOLS:
317 self.skipTest('Python elftools not available')
Simon Glassea64c022022-03-18 19:19:49 -0600318 fname = self.ElfTestFile('embed_data')
319 syms = elf.GetSymbols(fname, ['embed'])
320 addr = syms['embed'].address
321 offset = elf.GetFileOffset(fname, addr)
322 data = tools.read_file(fname)
323
324 # Just use the first 4 bytes and assume it is little endian
325 embed_data = data[offset:offset + 4]
326 embed_value = struct.unpack('<I', embed_data)[0]
327 self.assertEqual(0x1234, embed_value)
328
329 def test_get_file_offset_fail(self):
330 """Test calling GetFileOffset() without elftools"""
Heinrich Schuchardt85902cd2023-12-09 19:50:31 +0100331 old_val = elf.ELF_TOOLS
Simon Glassea64c022022-03-18 19:19:49 -0600332 try:
Simon Glassea64c022022-03-18 19:19:49 -0600333 elf.ELF_TOOLS = False
334 fname = self.ElfTestFile('embed_data')
335 with self.assertRaises(ValueError) as e:
336 elf.GetFileOffset(fname, 0)
337 self.assertIn("Python: No module named 'elftools'",
338 str(e.exception))
339 finally:
340 elf.ELF_TOOLS = old_val
341
342 def test_get_symbol_from_address(self):
343 """Test GetSymbolFromAddress()"""
Stefan Herbrechtsmeier732742e2022-08-19 16:25:18 +0200344 if not elf.ELF_TOOLS:
345 self.skipTest('Python elftools not available')
Simon Glassea64c022022-03-18 19:19:49 -0600346 fname = self.ElfTestFile('elf_sections')
347 sym_name = 'calculate'
348 syms = elf.GetSymbols(fname, [sym_name])
349 addr = syms[sym_name].address
350 sym = elf.GetSymbolFromAddress(fname, addr)
351 self.assertEqual(sym_name, sym)
352
353 def test_get_symbol_from_address_fail(self):
354 """Test calling GetSymbolFromAddress() without elftools"""
Heinrich Schuchardt85902cd2023-12-09 19:50:31 +0100355 old_val = elf.ELF_TOOLS
Simon Glassea64c022022-03-18 19:19:49 -0600356 try:
Simon Glassea64c022022-03-18 19:19:49 -0600357 elf.ELF_TOOLS = False
358 fname = self.ElfTestFile('embed_data')
359 with self.assertRaises(ValueError) as e:
360 elf.GetSymbolFromAddress(fname, 0x1000)
361 self.assertIn("Python: No module named 'elftools'",
362 str(e.exception))
363 finally:
364 elf.ELF_TOOLS = old_val
365
Simon Glass6e657f62023-01-07 14:07:13 -0700366 def test_is_valid(self):
367 """Test is_valid()"""
368 self.assertEqual(False, elf.is_valid(b''))
369 self.assertEqual(False, elf.is_valid(b'1234'))
370
371 fname = self.ElfTestFile('elf_sections')
372 data = tools.read_file(fname)
373 self.assertEqual(True, elf.is_valid(data))
374 self.assertEqual(False, elf.is_valid(data[4:]))
375
Simon Glass6490d4b2023-01-23 11:29:41 -0700376 def test_get_symbol_offset(self):
377 fname = self.ElfTestFile('embed_data')
378 syms = elf.GetSymbols(fname, ['embed_start', 'embed'])
379 expected = syms['embed'].address - syms['embed_start'].address
380 val = elf.GetSymbolOffset(fname, 'embed', 'embed_start')
381 self.assertEqual(expected, val)
382
383 with self.assertRaises(KeyError) as e:
384 elf.GetSymbolOffset(fname, 'embed')
385 self.assertIn('__image_copy_start', str(e.exception))
386
Simon Glass83b8bfe2023-07-18 07:24:01 -0600387 def test_get_symbol_address(self):
388 fname = self.ElfTestFile('embed_data')
389 addr = elf.GetSymbolAddress(fname, 'region_size')
390 self.assertEqual(0, addr)
391
Simon Glass4ca8e042017-11-13 18:55:01 -0700392
Simon Glass24ad3652017-11-13 18:54:54 -0700393if __name__ == '__main__':
394 unittest.main()