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