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