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