Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 2 | # Copyright (c) 2017 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 5 | # Test for the elf module |
| 6 | |
| 7 | import os |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 8 | import shutil |
Simon Glass | a4e259e | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 9 | import struct |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 10 | import sys |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 11 | import tempfile |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 12 | import unittest |
| 13 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 14 | from binman import elf |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 15 | from u_boot_pylib import command |
| 16 | from u_boot_pylib import test_util |
| 17 | from u_boot_pylib import tools |
| 18 | from u_boot_pylib import tout |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 19 | |
| 20 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 21 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 22 | |
| 23 | class FakeEntry: |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 24 | """A fake Entry object, usedfor testing |
| 25 | |
| 26 | This supports an entry with a given size. |
| 27 | """ |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 28 | def __init__(self, contents_size): |
| 29 | self.contents_size = contents_size |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 30 | self.data = tools.get_bytes(ord('a'), contents_size) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 31 | |
| 32 | def GetPath(self): |
| 33 | return 'entry_path' |
| 34 | |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 35 | |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 36 | class FakeSection: |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 37 | """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 Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 43 | def __init__(self, sym_value=1): |
| 44 | self.sym_value = sym_value |
| 45 | |
| 46 | def GetPath(self): |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 47 | return 'section_path' |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 48 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 49 | def LookupImageSymbol(self, name, weak, msg, base_addr): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 50 | """Fake implementation which returns the same value for all symbols""" |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 51 | return self.sym_value |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 52 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 53 | def GetImage(self): |
| 54 | return self |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 55 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 56 | def BuildElfTestFiles(target_dir): |
| 57 | """Build ELF files used for testing in binman |
| 58 | |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 59 | 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 Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 61 | |
| 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 Glass | 271fd8f | 2021-11-03 21:09:15 -0600 | [diff] [blame] | 74 | try: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 75 | tools.run('make', '-C', target_dir, '-f', |
Simon Glass | 271fd8f | 2021-11-03 21:09:15 -0600 | [diff] [blame] | 76 | 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 Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 80 | |
| 81 | |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 82 | class TestElf(unittest.TestCase): |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 83 | @classmethod |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 84 | def setUpClass(cls): |
| 85 | cls._indir = tempfile.mkdtemp(prefix='elf.') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 86 | tools.set_input_dirs(['.']) |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 87 | 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 Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 97 | |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 98 | def testAllSymbols(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 99 | """Test that we can obtain a symbol from the ELF file""" |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 100 | fname = self.ElfTestFile('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 101 | syms = elf.GetSymbols(fname, []) |
Simon Glass | 315400e | 2022-01-09 20:13:37 -0700 | [diff] [blame] | 102 | self.assertIn('_dt_ucode_base_size', syms) |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 103 | |
| 104 | def testRegexSymbols(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 105 | """Test that we can obtain from the ELF file by regular expression""" |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 106 | fname = self.ElfTestFile('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 107 | syms = elf.GetSymbols(fname, ['ucode']) |
Simon Glass | 315400e | 2022-01-09 20:13:37 -0700 | [diff] [blame] | 108 | self.assertIn('_dt_ucode_base_size', syms) |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 109 | syms = elf.GetSymbols(fname, ['missing']) |
Simon Glass | 315400e | 2022-01-09 20:13:37 -0700 | [diff] [blame] | 110 | self.assertNotIn('_dt_ucode_base_size', syms) |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 111 | syms = elf.GetSymbols(fname, ['missing', 'ucode']) |
Simon Glass | 315400e | 2022-01-09 20:13:37 -0700 | [diff] [blame] | 112 | self.assertIn('_dt_ucode_base_size', syms) |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 113 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 114 | def testMissingFile(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 115 | """Test that a missing file is detected""" |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 116 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 117 | section = FakeSection() |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 118 | with self.assertRaises(ValueError) as e: |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 119 | elf.LookupAndWriteSymbols('missing-file', entry, section) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 120 | self.assertIn("Filename 'missing-file' not found in input path", |
| 121 | str(e.exception)) |
| 122 | |
| 123 | def testOutsideFile(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 124 | """Test a symbol which extends outside the entry area is detected""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 125 | if not elf.ELF_TOOLS: |
| 126 | self.skipTest('Python elftools not available') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 127 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 128 | section = FakeSection() |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 129 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 130 | with self.assertRaises(ValueError) as e: |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 131 | elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 132 | self.assertIn('entry_path has offset 8 (size 8) but the contents size ' |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 133 | 'is a', str(e.exception)) |
| 134 | |
| 135 | def testMissingImageStart(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 136 | """Test that we detect a missing __image_copy_start symbol |
| 137 | |
| 138 | This is needed to mark the start of the image. Without it we cannot |
| 139 | locate the offset of a binman symbol within the image. |
| 140 | """ |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 141 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 142 | section = FakeSection() |
Simon Glass | 46ea691 | 2019-08-24 07:22:58 -0600 | [diff] [blame] | 143 | elf_fname = self.ElfTestFile('u_boot_binman_syms_bad') |
Simon Glass | ea1d77d | 2023-07-18 07:23:56 -0600 | [diff] [blame] | 144 | count = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
| 145 | self.assertEqual(0, count) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 146 | |
| 147 | def testBadSymbolSize(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 148 | """Test that an attempt to use an 8-bit symbol are detected |
| 149 | |
| 150 | Only 32 and 64 bits are supported, since we need to store an offset |
| 151 | into the image. |
| 152 | """ |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 153 | if not elf.ELF_TOOLS: |
| 154 | self.skipTest('Python elftools not available') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 155 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 156 | section = FakeSection() |
Simon Glass | b8deb12 | 2019-08-24 07:22:57 -0600 | [diff] [blame] | 157 | elf_fname =self.ElfTestFile('u_boot_binman_syms_size') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 158 | with self.assertRaises(ValueError) as e: |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 159 | elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 160 | self.assertIn('has size 1: only 4 and 8 are supported', |
| 161 | str(e.exception)) |
| 162 | |
| 163 | def testNoValue(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 164 | """Test the case where we have no value for the symbol |
| 165 | |
Simon Glass | ea1d77d | 2023-07-18 07:23:56 -0600 | [diff] [blame] | 166 | This should produce -1 values for all three symbols, taking up the |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 167 | first 16 bytes of the image. |
| 168 | """ |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 169 | if not elf.ELF_TOOLS: |
| 170 | self.skipTest('Python elftools not available') |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 171 | entry = FakeEntry(28) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 172 | section = FakeSection(sym_value=None) |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 173 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | ea1d77d | 2023-07-18 07:23:56 -0600 | [diff] [blame] | 174 | count = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
| 175 | self.assertEqual(5, count) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 176 | expected = (struct.pack('<L', elf.BINMAN_SYM_MAGIC_VALUE) + |
| 177 | tools.get_bytes(255, 20) + |
| 178 | tools.get_bytes(ord('a'), 4)) |
| 179 | self.assertEqual(expected, entry.data) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 180 | |
| 181 | def testDebug(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 182 | """Check that enabling debug in the elf module produced debug output""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 183 | if not elf.ELF_TOOLS: |
| 184 | self.skipTest('Python elftools not available') |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 185 | try: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 186 | tout.init(tout.DEBUG) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 187 | entry = FakeEntry(24) |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 188 | section = FakeSection() |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 189 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 190 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 191 | elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 192 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 193 | finally: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 194 | tout.init(tout.WARNING) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 195 | |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 196 | def testMakeElf(self): |
| 197 | """Test for the MakeElf function""" |
| 198 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 199 | expected_text = b'1234' |
| 200 | expected_data = b'wxyz' |
| 201 | elf_fname = os.path.join(outdir, 'elf') |
Simon Glass | d349ada | 2019-08-24 07:22:45 -0600 | [diff] [blame] | 202 | bin_fname = os.path.join(outdir, 'bin') |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 203 | |
| 204 | # Make an Elf file and then convert it to a fkat binary file. This |
| 205 | # should produce the original data. |
| 206 | elf.MakeElf(elf_fname, expected_text, expected_data) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 207 | objcopy, args = tools.get_target_compile_tool('objcopy') |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 208 | args += ['-O', 'binary', elf_fname, bin_fname] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 209 | stdout = command.output(objcopy, *args) |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 210 | with open(bin_fname, 'rb') as fd: |
| 211 | data = fd.read() |
| 212 | self.assertEqual(expected_text + expected_data, data) |
| 213 | shutil.rmtree(outdir) |
| 214 | |
Simon Glass | 567b682 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 215 | def testDecodeElf(self): |
| 216 | """Test for the MakeElf function""" |
| 217 | if not elf.ELF_TOOLS: |
| 218 | self.skipTest('Python elftools not available') |
| 219 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 220 | expected_text = b'1234' |
| 221 | expected_data = b'wxyz' |
| 222 | elf_fname = os.path.join(outdir, 'elf') |
| 223 | elf.MakeElf(elf_fname, expected_text, expected_data) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 224 | data = tools.read_file(elf_fname) |
Simon Glass | 567b682 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 225 | |
| 226 | load = 0xfef20000 |
| 227 | entry = load + 2 |
| 228 | expected = expected_text + expected_data |
| 229 | self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)), |
| 230 | elf.DecodeElf(data, 0)) |
| 231 | self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:], |
| 232 | load, entry, len(expected)), |
| 233 | elf.DecodeElf(data, load + 2)) |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 234 | shutil.rmtree(outdir) |
Simon Glass | 567b682 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 235 | |
Simon Glass | a4e259e | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 236 | def testEmbedData(self): |
| 237 | """Test for the GetSymbolFileOffset() function""" |
| 238 | if not elf.ELF_TOOLS: |
| 239 | self.skipTest('Python elftools not available') |
| 240 | |
| 241 | fname = self.ElfTestFile('embed_data') |
| 242 | offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end']) |
| 243 | start = offset['embed_start'].offset |
| 244 | end = offset['embed_end'].offset |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 245 | data = tools.read_file(fname) |
Simon Glass | a4e259e | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 246 | embed_data = data[start:end] |
Simon Glass | 6490d4b | 2023-01-23 11:29:41 -0700 | [diff] [blame] | 247 | expect = struct.pack('<IIIII', 2, 3, 0x1234, 0x5678, 0) |
Simon Glass | a4e259e | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 248 | self.assertEqual(expect, embed_data) |
| 249 | |
| 250 | def testEmbedFail(self): |
| 251 | """Test calling GetSymbolFileOffset() without elftools""" |
| 252 | try: |
| 253 | old_val = elf.ELF_TOOLS |
| 254 | elf.ELF_TOOLS = False |
| 255 | fname = self.ElfTestFile('embed_data') |
| 256 | with self.assertRaises(ValueError) as e: |
| 257 | elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end']) |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 258 | self.assertIn("Python: No module named 'elftools'", |
Simon Glass | a4e259e | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 259 | str(e.exception)) |
| 260 | finally: |
| 261 | elf.ELF_TOOLS = old_val |
| 262 | |
| 263 | def testEmbedDataNoSym(self): |
| 264 | """Test for GetSymbolFileOffset() getting no symbols""" |
| 265 | if not elf.ELF_TOOLS: |
| 266 | self.skipTest('Python elftools not available') |
| 267 | |
| 268 | fname = self.ElfTestFile('embed_data') |
| 269 | offset = elf.GetSymbolFileOffset(fname, ['missing_sym']) |
| 270 | self.assertEqual({}, offset) |
| 271 | |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 272 | def test_read_loadable_segments(self): |
| 273 | """Test for read_loadable_segments()""" |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 274 | if not elf.ELF_TOOLS: |
| 275 | self.skipTest('Python elftools not available') |
| 276 | fname = self.ElfTestFile('embed_data') |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 277 | segments, entry = elf.read_loadable_segments(tools.read_file(fname)) |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 278 | |
| 279 | def test_read_segments_fail(self): |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 280 | """Test for read_loadable_segments() without elftools""" |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 281 | try: |
| 282 | old_val = elf.ELF_TOOLS |
| 283 | elf.ELF_TOOLS = False |
| 284 | fname = self.ElfTestFile('embed_data') |
| 285 | with self.assertRaises(ValueError) as e: |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 286 | elf.read_loadable_segments(tools.read_file(fname)) |
| 287 | self.assertIn("Python: No module named 'elftools'", |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 288 | str(e.exception)) |
| 289 | finally: |
| 290 | elf.ELF_TOOLS = old_val |
| 291 | |
| 292 | def test_read_segments_bad_data(self): |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 293 | """Test for read_loadable_segments() with an invalid ELF file""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 294 | if not elf.ELF_TOOLS: |
| 295 | self.skipTest('Python elftools not available') |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 296 | fname = self.ElfTestFile('embed_data') |
| 297 | with self.assertRaises(ValueError) as e: |
Simon Glass | acc0375 | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 298 | elf.read_loadable_segments(tools.get_bytes(100, 100)) |
Simon Glass | 571adc8 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 299 | self.assertIn('Magic number does not match', str(e.exception)) |
| 300 | |
Simon Glass | ea64c02 | 2022-03-18 19:19:49 -0600 | [diff] [blame] | 301 | def test_get_file_offset(self): |
| 302 | """Test GetFileOffset() gives the correct file offset for a symbol""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 303 | if not elf.ELF_TOOLS: |
| 304 | self.skipTest('Python elftools not available') |
Simon Glass | ea64c02 | 2022-03-18 19:19:49 -0600 | [diff] [blame] | 305 | fname = self.ElfTestFile('embed_data') |
| 306 | syms = elf.GetSymbols(fname, ['embed']) |
| 307 | addr = syms['embed'].address |
| 308 | offset = elf.GetFileOffset(fname, addr) |
| 309 | data = tools.read_file(fname) |
| 310 | |
| 311 | # Just use the first 4 bytes and assume it is little endian |
| 312 | embed_data = data[offset:offset + 4] |
| 313 | embed_value = struct.unpack('<I', embed_data)[0] |
| 314 | self.assertEqual(0x1234, embed_value) |
| 315 | |
| 316 | def test_get_file_offset_fail(self): |
| 317 | """Test calling GetFileOffset() without elftools""" |
| 318 | try: |
| 319 | old_val = elf.ELF_TOOLS |
| 320 | elf.ELF_TOOLS = False |
| 321 | fname = self.ElfTestFile('embed_data') |
| 322 | with self.assertRaises(ValueError) as e: |
| 323 | elf.GetFileOffset(fname, 0) |
| 324 | self.assertIn("Python: No module named 'elftools'", |
| 325 | str(e.exception)) |
| 326 | finally: |
| 327 | elf.ELF_TOOLS = old_val |
| 328 | |
| 329 | def test_get_symbol_from_address(self): |
| 330 | """Test GetSymbolFromAddress()""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 331 | if not elf.ELF_TOOLS: |
| 332 | self.skipTest('Python elftools not available') |
Simon Glass | ea64c02 | 2022-03-18 19:19:49 -0600 | [diff] [blame] | 333 | fname = self.ElfTestFile('elf_sections') |
| 334 | sym_name = 'calculate' |
| 335 | syms = elf.GetSymbols(fname, [sym_name]) |
| 336 | addr = syms[sym_name].address |
| 337 | sym = elf.GetSymbolFromAddress(fname, addr) |
| 338 | self.assertEqual(sym_name, sym) |
| 339 | |
| 340 | def test_get_symbol_from_address_fail(self): |
| 341 | """Test calling GetSymbolFromAddress() without elftools""" |
| 342 | try: |
| 343 | old_val = elf.ELF_TOOLS |
| 344 | elf.ELF_TOOLS = False |
| 345 | fname = self.ElfTestFile('embed_data') |
| 346 | with self.assertRaises(ValueError) as e: |
| 347 | elf.GetSymbolFromAddress(fname, 0x1000) |
| 348 | self.assertIn("Python: No module named 'elftools'", |
| 349 | str(e.exception)) |
| 350 | finally: |
| 351 | elf.ELF_TOOLS = old_val |
| 352 | |
Simon Glass | 6e657f6 | 2023-01-07 14:07:13 -0700 | [diff] [blame] | 353 | def test_is_valid(self): |
| 354 | """Test is_valid()""" |
| 355 | self.assertEqual(False, elf.is_valid(b'')) |
| 356 | self.assertEqual(False, elf.is_valid(b'1234')) |
| 357 | |
| 358 | fname = self.ElfTestFile('elf_sections') |
| 359 | data = tools.read_file(fname) |
| 360 | self.assertEqual(True, elf.is_valid(data)) |
| 361 | self.assertEqual(False, elf.is_valid(data[4:])) |
| 362 | |
Simon Glass | 6490d4b | 2023-01-23 11:29:41 -0700 | [diff] [blame] | 363 | def test_get_symbol_offset(self): |
| 364 | fname = self.ElfTestFile('embed_data') |
| 365 | syms = elf.GetSymbols(fname, ['embed_start', 'embed']) |
| 366 | expected = syms['embed'].address - syms['embed_start'].address |
| 367 | val = elf.GetSymbolOffset(fname, 'embed', 'embed_start') |
| 368 | self.assertEqual(expected, val) |
| 369 | |
| 370 | with self.assertRaises(KeyError) as e: |
| 371 | elf.GetSymbolOffset(fname, 'embed') |
| 372 | self.assertIn('__image_copy_start', str(e.exception)) |
| 373 | |
Simon Glass | 83b8bfe | 2023-07-18 07:24:01 -0600 | [diff] [blame] | 374 | def test_get_symbol_address(self): |
| 375 | fname = self.ElfTestFile('embed_data') |
| 376 | addr = elf.GetSymbolAddress(fname, 'region_size') |
| 377 | self.assertEqual(0, addr) |
| 378 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 379 | |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 380 | if __name__ == '__main__': |
| 381 | unittest.main() |