Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b50e561 | 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 | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 5 | # Test for the elf module |
| 6 | |
| 7 | import os |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 8 | import shutil |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 9 | import sys |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 10 | import tempfile |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 11 | import unittest |
| 12 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 13 | from binman import elf |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 14 | from patman import command |
| 15 | from patman import test_util |
| 16 | from patman import tools |
| 17 | from patman import tout |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 18 | |
| 19 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 20 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 21 | |
| 22 | class FakeEntry: |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 23 | """A fake Entry object, usedfor testing |
| 24 | |
| 25 | This supports an entry with a given size. |
| 26 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 27 | def __init__(self, contents_size): |
| 28 | self.contents_size = contents_size |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 29 | self.data = tools.GetBytes(ord('a'), contents_size) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 30 | |
| 31 | def GetPath(self): |
| 32 | return 'entry_path' |
| 33 | |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 34 | |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 35 | class FakeSection: |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 36 | """A fake Section object, used for testing |
| 37 | |
| 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 |
| 40 | symbol requested. |
| 41 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 42 | def __init__(self, sym_value=1): |
| 43 | self.sym_value = sym_value |
| 44 | |
| 45 | def GetPath(self): |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 46 | return 'section_path' |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 47 | |
Simon Glass | 870a9ea | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 48 | def LookupImageSymbol(self, name, weak, msg, base_addr): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 49 | """Fake implementation which returns the same value for all symbols""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 50 | return self.sym_value |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 51 | |
Simon Glass | 870a9ea | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 52 | def GetImage(self): |
| 53 | return self |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 54 | |
Simon Glass | 53e22bf | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 55 | def BuildElfTestFiles(target_dir): |
| 56 | """Build ELF files used for testing in binman |
| 57 | |
| 58 | This compiles and links the test files into the specified directory. It the |
| 59 | Makefile and source files in the binman test/ directory. |
| 60 | |
| 61 | Args: |
| 62 | target_dir: Directory to put the files into |
| 63 | """ |
| 64 | if not os.path.exists(target_dir): |
| 65 | os.mkdir(target_dir) |
| 66 | testdir = os.path.join(binman_dir, 'test') |
| 67 | |
| 68 | # If binman is involved from the main U-Boot Makefile the -r and -R |
| 69 | # flags are set in MAKEFLAGS. This prevents this Makefile from working |
| 70 | # correctly. So drop any make flags here. |
| 71 | if 'MAKEFLAGS' in os.environ: |
| 72 | del os.environ['MAKEFLAGS'] |
Simon Glass | 2fb2cd7 | 2021-11-03 21:09:15 -0600 | [diff] [blame^] | 73 | try: |
| 74 | tools.Run('make', '-C', target_dir, '-f', |
| 75 | os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir) |
| 76 | except ValueError as e: |
| 77 | # The test system seems to suppress this in a strange way |
| 78 | print(e) |
Simon Glass | 53e22bf | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 79 | |
| 80 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 81 | class TestElf(unittest.TestCase): |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 82 | @classmethod |
Simon Glass | f514d8f | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 83 | def setUpClass(cls): |
| 84 | cls._indir = tempfile.mkdtemp(prefix='elf.') |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 85 | tools.SetInputDirs(['.']) |
Simon Glass | f514d8f | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 86 | BuildElfTestFiles(cls._indir) |
| 87 | |
| 88 | @classmethod |
| 89 | def tearDownClass(cls): |
| 90 | if cls._indir: |
| 91 | shutil.rmtree(cls._indir) |
| 92 | |
| 93 | @classmethod |
| 94 | def ElfTestFile(cls, fname): |
| 95 | return os.path.join(cls._indir, fname) |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 96 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 97 | def testAllSymbols(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 98 | """Test that we can obtain a symbol from the ELF file""" |
Simon Glass | f514d8f | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 99 | fname = self.ElfTestFile('u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 100 | syms = elf.GetSymbols(fname, []) |
| 101 | self.assertIn('.ucode', syms) |
| 102 | |
| 103 | def testRegexSymbols(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 104 | """Test that we can obtain from the ELF file by regular expression""" |
Simon Glass | f514d8f | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 105 | fname = self.ElfTestFile('u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 106 | syms = elf.GetSymbols(fname, ['ucode']) |
| 107 | self.assertIn('.ucode', syms) |
| 108 | syms = elf.GetSymbols(fname, ['missing']) |
| 109 | self.assertNotIn('.ucode', syms) |
| 110 | syms = elf.GetSymbols(fname, ['missing', 'ucode']) |
| 111 | self.assertIn('.ucode', syms) |
| 112 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 113 | def testMissingFile(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 114 | """Test that a missing file is detected""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 115 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 116 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 117 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 118 | syms = elf.LookupAndWriteSymbols('missing-file', entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 119 | self.assertIn("Filename 'missing-file' not found in input path", |
| 120 | str(e.exception)) |
| 121 | |
| 122 | def testOutsideFile(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 123 | """Test a symbol which extends outside the entry area is detected""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 124 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 125 | section = FakeSection() |
Simon Glass | 1542c8b | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 126 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 127 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 128 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 129 | self.assertIn('entry_path has offset 4 (size 8) but the contents size ' |
| 130 | 'is a', str(e.exception)) |
| 131 | |
| 132 | def testMissingImageStart(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 133 | """Test that we detect a missing __image_copy_start symbol |
| 134 | |
| 135 | This is needed to mark the start of the image. Without it we cannot |
| 136 | locate the offset of a binman symbol within the image. |
| 137 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 138 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 139 | section = FakeSection() |
Simon Glass | 8dc60f9 | 2019-08-24 07:22:58 -0600 | [diff] [blame] | 140 | elf_fname = self.ElfTestFile('u_boot_binman_syms_bad') |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 141 | self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section), |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 142 | None) |
| 143 | |
| 144 | def testBadSymbolSize(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 145 | """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 Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 150 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 151 | section = FakeSection() |
Simon Glass | e9d2ee3 | 2019-08-24 07:22:57 -0600 | [diff] [blame] | 152 | elf_fname =self.ElfTestFile('u_boot_binman_syms_size') |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 153 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 154 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 155 | self.assertIn('has size 1: only 4 and 8 are supported', |
| 156 | str(e.exception)) |
| 157 | |
| 158 | def testNoValue(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 159 | """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 | """ |
Simon Glass | b87064c | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 164 | entry = FakeEntry(24) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 165 | section = FakeSection(sym_value=None) |
Simon Glass | 1542c8b | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 166 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 167 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | b87064c | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 168 | self.assertEqual(tools.GetBytes(255, 20) + tools.GetBytes(ord('a'), 4), |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 169 | entry.data) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 170 | |
| 171 | def testDebug(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 172 | """Check that enabling debug in the elf module produced debug output""" |
Simon Glass | 9f297b0 | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 173 | try: |
| 174 | tout.Init(tout.DEBUG) |
| 175 | entry = FakeEntry(20) |
| 176 | section = FakeSection() |
Simon Glass | 1542c8b | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 177 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 9f297b0 | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 178 | with test_util.capture_sys_output() as (stdout, stderr): |
| 179 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
| 180 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 181 | finally: |
| 182 | tout.Init(tout.WARNING) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 183 | |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 184 | def testMakeElf(self): |
| 185 | """Test for the MakeElf function""" |
| 186 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 187 | expected_text = b'1234' |
| 188 | expected_data = b'wxyz' |
| 189 | elf_fname = os.path.join(outdir, 'elf') |
Simon Glass | 9d44a7e | 2019-08-24 07:22:45 -0600 | [diff] [blame] | 190 | bin_fname = os.path.join(outdir, 'bin') |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 191 | |
| 192 | # Make an Elf file and then convert it to a fkat binary file. This |
| 193 | # should produce the original data. |
| 194 | elf.MakeElf(elf_fname, expected_text, expected_data) |
Alper Nebi Yasak | 1e4687a | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 195 | objcopy, args = tools.GetTargetCompileTool('objcopy') |
| 196 | args += ['-O', 'binary', elf_fname, bin_fname] |
| 197 | stdout = command.Output(objcopy, *args) |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 198 | with open(bin_fname, 'rb') as fd: |
| 199 | data = fd.read() |
| 200 | self.assertEqual(expected_text + expected_data, data) |
| 201 | shutil.rmtree(outdir) |
| 202 | |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 203 | def testDecodeElf(self): |
| 204 | """Test for the MakeElf function""" |
| 205 | if not elf.ELF_TOOLS: |
| 206 | self.skipTest('Python elftools not available') |
| 207 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 208 | expected_text = b'1234' |
| 209 | expected_data = b'wxyz' |
| 210 | elf_fname = os.path.join(outdir, 'elf') |
| 211 | elf.MakeElf(elf_fname, expected_text, expected_data) |
| 212 | data = tools.ReadFile(elf_fname) |
| 213 | |
| 214 | load = 0xfef20000 |
| 215 | entry = load + 2 |
| 216 | expected = expected_text + expected_data |
| 217 | self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)), |
| 218 | elf.DecodeElf(data, 0)) |
| 219 | self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:], |
| 220 | load, entry, len(expected)), |
| 221 | elf.DecodeElf(data, load + 2)) |
Simon Glass | f514d8f | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 222 | shutil.rmtree(outdir) |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 223 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 224 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 225 | if __name__ == '__main__': |
| 226 | unittest.main() |