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 | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 13 | import command |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 14 | import elf |
Simon Glass | c3f9454 | 2018-07-06 10:27:34 -0600 | [diff] [blame] | 15 | import test_util |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 16 | import tools |
Simon Glass | 9f297b0 | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 17 | 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 | |
| 48 | def LookupSymbol(self, name, weak, msg): |
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 | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 52 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 53 | class TestElf(unittest.TestCase): |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 54 | @classmethod |
| 55 | def setUpClass(self): |
| 56 | tools.SetInputDirs(['.']) |
| 57 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 58 | def testAllSymbols(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 59 | """Test that we can obtain a symbol from the ELF file""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 60 | fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 61 | syms = elf.GetSymbols(fname, []) |
| 62 | self.assertIn('.ucode', syms) |
| 63 | |
| 64 | def testRegexSymbols(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 65 | """Test that we can obtain from the ELF file by regular expression""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 66 | fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 67 | syms = elf.GetSymbols(fname, ['ucode']) |
| 68 | self.assertIn('.ucode', syms) |
| 69 | syms = elf.GetSymbols(fname, ['missing']) |
| 70 | self.assertNotIn('.ucode', syms) |
| 71 | syms = elf.GetSymbols(fname, ['missing', 'ucode']) |
| 72 | self.assertIn('.ucode', syms) |
| 73 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 74 | def testMissingFile(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 75 | """Test that a missing file is detected""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 76 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 77 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 78 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 79 | syms = elf.LookupAndWriteSymbols('missing-file', entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 80 | self.assertIn("Filename 'missing-file' not found in input path", |
| 81 | str(e.exception)) |
| 82 | |
| 83 | def testOutsideFile(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 84 | """Test a symbol which extends outside the entry area is detected""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 85 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 86 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 87 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') |
| 88 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 89 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 90 | self.assertIn('entry_path has offset 4 (size 8) but the contents size ' |
| 91 | 'is a', str(e.exception)) |
| 92 | |
| 93 | def testMissingImageStart(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 94 | """Test that we detect a missing __image_copy_start symbol |
| 95 | |
| 96 | This is needed to mark the start of the image. Without it we cannot |
| 97 | locate the offset of a binman symbol within the image. |
| 98 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 99 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 100 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 101 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad') |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 102 | self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section), |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 103 | None) |
| 104 | |
| 105 | def testBadSymbolSize(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 106 | """Test that an attempt to use an 8-bit symbol are detected |
| 107 | |
| 108 | Only 32 and 64 bits are supported, since we need to store an offset |
| 109 | into the image. |
| 110 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 111 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 112 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 113 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size') |
| 114 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 115 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 116 | self.assertIn('has size 1: only 4 and 8 are supported', |
| 117 | str(e.exception)) |
| 118 | |
| 119 | def testNoValue(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 120 | """Test the case where we have no value for the symbol |
| 121 | |
| 122 | This should produce -1 values for all thress symbols, taking up the |
| 123 | first 16 bytes of the image. |
| 124 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 125 | entry = FakeEntry(20) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 126 | section = FakeSection(sym_value=None) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 127 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 128 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 129 | self.assertEqual(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4), |
| 130 | entry.data) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 131 | |
| 132 | def testDebug(self): |
Simon Glass | b2b0df8 | 2018-07-17 13:25:26 -0600 | [diff] [blame] | 133 | """Check that enabling debug in the elf module produced debug output""" |
Simon Glass | 9f297b0 | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 134 | try: |
| 135 | tout.Init(tout.DEBUG) |
| 136 | entry = FakeEntry(20) |
| 137 | section = FakeSection() |
| 138 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') |
| 139 | with test_util.capture_sys_output() as (stdout, stderr): |
| 140 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
| 141 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 142 | finally: |
| 143 | tout.Init(tout.WARNING) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 144 | |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 145 | def testMakeElf(self): |
| 146 | """Test for the MakeElf function""" |
| 147 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 148 | expected_text = b'1234' |
| 149 | expected_data = b'wxyz' |
| 150 | elf_fname = os.path.join(outdir, 'elf') |
Simon Glass | 9d44a7e | 2019-08-24 07:22:45 -0600 | [diff] [blame] | 151 | bin_fname = os.path.join(outdir, 'bin') |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 152 | |
| 153 | # Make an Elf file and then convert it to a fkat binary file. This |
| 154 | # should produce the original data. |
| 155 | elf.MakeElf(elf_fname, expected_text, expected_data) |
| 156 | stdout = command.Output('objcopy', '-O', 'binary', elf_fname, bin_fname) |
| 157 | with open(bin_fname, 'rb') as fd: |
| 158 | data = fd.read() |
| 159 | self.assertEqual(expected_text + expected_data, data) |
| 160 | shutil.rmtree(outdir) |
| 161 | |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 162 | def testDecodeElf(self): |
| 163 | """Test for the MakeElf function""" |
| 164 | if not elf.ELF_TOOLS: |
| 165 | self.skipTest('Python elftools not available') |
| 166 | outdir = tempfile.mkdtemp(prefix='elf.') |
| 167 | expected_text = b'1234' |
| 168 | expected_data = b'wxyz' |
| 169 | elf_fname = os.path.join(outdir, 'elf') |
| 170 | elf.MakeElf(elf_fname, expected_text, expected_data) |
| 171 | data = tools.ReadFile(elf_fname) |
| 172 | |
| 173 | load = 0xfef20000 |
| 174 | entry = load + 2 |
| 175 | expected = expected_text + expected_data |
| 176 | self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)), |
| 177 | elf.DecodeElf(data, 0)) |
| 178 | self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:], |
| 179 | load, entry, len(expected)), |
| 180 | elf.DecodeElf(data, load + 2)) |
| 181 | #shutil.rmtree(outdir) |
| 182 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 183 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 184 | if __name__ == '__main__': |
| 185 | unittest.main() |