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 | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 9 | import sys |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 10 | import tempfile |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 11 | import unittest |
| 12 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 13 | from binman import elf |
Simon Glass | a997ea5 | 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 | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 18 | |
| 19 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 20 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 21 | |
| 22 | class FakeEntry: |
Simon Glass | 4114f97 | 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 | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 27 | def __init__(self, contents_size): |
| 28 | self.contents_size = contents_size |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 29 | self.data = tools.GetBytes(ord('a'), contents_size) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 30 | |
| 31 | def GetPath(self): |
| 32 | return 'entry_path' |
| 33 | |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 34 | |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 35 | class FakeSection: |
Simon Glass | 4114f97 | 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 | 4ca8e04 | 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 | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 46 | return 'section_path' |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 47 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 48 | def LookupImageSymbol(self, name, weak, msg, base_addr): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 49 | """Fake implementation which returns the same value for all symbols""" |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 50 | return self.sym_value |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 51 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 52 | def GetImage(self): |
| 53 | return self |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 54 | |
Simon Glass | f629089 | 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'] |
| 73 | tools.Run('make', '-C', target_dir, '-f', |
Simon Glass | 93a806f | 2019-08-24 07:22:59 -0600 | [diff] [blame] | 74 | os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir) |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 75 | |
| 76 | |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 77 | class TestElf(unittest.TestCase): |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 78 | @classmethod |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 79 | def setUpClass(cls): |
| 80 | cls._indir = tempfile.mkdtemp(prefix='elf.') |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 81 | tools.SetInputDirs(['.']) |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 82 | BuildElfTestFiles(cls._indir) |
| 83 | |
| 84 | @classmethod |
| 85 | def tearDownClass(cls): |
| 86 | if cls._indir: |
| 87 | shutil.rmtree(cls._indir) |
| 88 | |
| 89 | @classmethod |
| 90 | def ElfTestFile(cls, fname): |
| 91 | return os.path.join(cls._indir, fname) |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 92 | |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 93 | def testAllSymbols(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 94 | """Test that we can obtain a symbol from the ELF file""" |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 95 | fname = self.ElfTestFile('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 96 | syms = elf.GetSymbols(fname, []) |
| 97 | self.assertIn('.ucode', syms) |
| 98 | |
| 99 | def testRegexSymbols(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 100 | """Test that we can obtain from the ELF file by regular expression""" |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 101 | fname = self.ElfTestFile('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 102 | syms = elf.GetSymbols(fname, ['ucode']) |
| 103 | self.assertIn('.ucode', syms) |
| 104 | syms = elf.GetSymbols(fname, ['missing']) |
| 105 | self.assertNotIn('.ucode', syms) |
| 106 | syms = elf.GetSymbols(fname, ['missing', 'ucode']) |
| 107 | self.assertIn('.ucode', syms) |
| 108 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 109 | def testMissingFile(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 110 | """Test that a missing file is detected""" |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 111 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 112 | section = FakeSection() |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 113 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 114 | syms = elf.LookupAndWriteSymbols('missing-file', entry, section) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 115 | self.assertIn("Filename 'missing-file' not found in input path", |
| 116 | str(e.exception)) |
| 117 | |
| 118 | def testOutsideFile(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 119 | """Test a symbol which extends outside the entry area is detected""" |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 120 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 121 | section = FakeSection() |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 122 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 123 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 124 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 125 | self.assertIn('entry_path has offset 4 (size 8) but the contents size ' |
| 126 | 'is a', str(e.exception)) |
| 127 | |
| 128 | def testMissingImageStart(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 129 | """Test that we detect a missing __image_copy_start symbol |
| 130 | |
| 131 | This is needed to mark the start of the image. Without it we cannot |
| 132 | locate the offset of a binman symbol within the image. |
| 133 | """ |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 134 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 135 | section = FakeSection() |
Simon Glass | 46ea691 | 2019-08-24 07:22:58 -0600 | [diff] [blame] | 136 | elf_fname = self.ElfTestFile('u_boot_binman_syms_bad') |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 137 | self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section), |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 138 | None) |
| 139 | |
| 140 | def testBadSymbolSize(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 141 | """Test that an attempt to use an 8-bit symbol are detected |
| 142 | |
| 143 | Only 32 and 64 bits are supported, since we need to store an offset |
| 144 | into the image. |
| 145 | """ |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 146 | entry = FakeEntry(10) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 147 | section = FakeSection() |
Simon Glass | b8deb12 | 2019-08-24 07:22:57 -0600 | [diff] [blame] | 148 | elf_fname =self.ElfTestFile('u_boot_binman_syms_size') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 149 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 150 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 151 | self.assertIn('has size 1: only 4 and 8 are supported', |
| 152 | str(e.exception)) |
| 153 | |
| 154 | def testNoValue(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 155 | """Test the case where we have no value for the symbol |
| 156 | |
| 157 | This should produce -1 values for all thress symbols, taking up the |
| 158 | first 16 bytes of the image. |
| 159 | """ |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 160 | entry = FakeEntry(24) |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 161 | section = FakeSection(sym_value=None) |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 162 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 163 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 164 | self.assertEqual(tools.GetBytes(255, 20) + tools.GetBytes(ord('a'), 4), |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 165 | entry.data) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 166 | |
| 167 | def testDebug(self): |
Simon Glass | 4114f97 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 168 | """Check that enabling debug in the elf module produced debug output""" |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 169 | try: |
| 170 | tout.Init(tout.DEBUG) |
| 171 | entry = FakeEntry(20) |
| 172 | section = FakeSection() |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 173 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 174 | with test_util.capture_sys_output() as (stdout, stderr): |
| 175 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
| 176 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 177 | finally: |
| 178 | tout.Init(tout.WARNING) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 179 | |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 180 | def testMakeElf(self): |
| 181 | """Test for the MakeElf function""" |
| 182 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 183 | expected_text = b'1234' |
| 184 | expected_data = b'wxyz' |
| 185 | elf_fname = os.path.join(outdir, 'elf') |
Simon Glass | d349ada | 2019-08-24 07:22:45 -0600 | [diff] [blame] | 186 | bin_fname = os.path.join(outdir, 'bin') |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 187 | |
| 188 | # Make an Elf file and then convert it to a fkat binary file. This |
| 189 | # should produce the original data. |
| 190 | elf.MakeElf(elf_fname, expected_text, expected_data) |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 191 | objcopy, args = tools.GetTargetCompileTool('objcopy') |
| 192 | args += ['-O', 'binary', elf_fname, bin_fname] |
| 193 | stdout = command.Output(objcopy, *args) |
Simon Glass | 4f379ea | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 194 | with open(bin_fname, 'rb') as fd: |
| 195 | data = fd.read() |
| 196 | self.assertEqual(expected_text + expected_data, data) |
| 197 | shutil.rmtree(outdir) |
| 198 | |
Simon Glass | 567b682 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 199 | def testDecodeElf(self): |
| 200 | """Test for the MakeElf function""" |
| 201 | if not elf.ELF_TOOLS: |
| 202 | self.skipTest('Python elftools not available') |
| 203 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 204 | expected_text = b'1234' |
| 205 | expected_data = b'wxyz' |
| 206 | elf_fname = os.path.join(outdir, 'elf') |
| 207 | elf.MakeElf(elf_fname, expected_text, expected_data) |
| 208 | data = tools.ReadFile(elf_fname) |
| 209 | |
| 210 | load = 0xfef20000 |
| 211 | entry = load + 2 |
| 212 | expected = expected_text + expected_data |
| 213 | self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)), |
| 214 | elf.DecodeElf(data, 0)) |
| 215 | self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:], |
| 216 | load, entry, len(expected)), |
| 217 | elf.DecodeElf(data, load + 2)) |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 218 | shutil.rmtree(outdir) |
Simon Glass | 567b682 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 219 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 220 | |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 221 | if __name__ == '__main__': |
| 222 | unittest.main() |