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 |
| 8 | import sys |
| 9 | import unittest |
| 10 | |
| 11 | import elf |
Simon Glass | c3f9454 | 2018-07-06 10:27:34 -0600 | [diff] [blame^] | 12 | import test_util |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 13 | |
| 14 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 15 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 16 | |
| 17 | class FakeEntry: |
| 18 | def __init__(self, contents_size): |
| 19 | self.contents_size = contents_size |
| 20 | self.data = 'a' * contents_size |
| 21 | |
| 22 | def GetPath(self): |
| 23 | return 'entry_path' |
| 24 | |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 25 | class FakeSection: |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 26 | def __init__(self, sym_value=1): |
| 27 | self.sym_value = sym_value |
| 28 | |
| 29 | def GetPath(self): |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 30 | return 'section_path' |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 31 | |
| 32 | def LookupSymbol(self, name, weak, msg): |
| 33 | return self.sym_value |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 34 | |
| 35 | class TestElf(unittest.TestCase): |
| 36 | def testAllSymbols(self): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 37 | fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 38 | syms = elf.GetSymbols(fname, []) |
| 39 | self.assertIn('.ucode', syms) |
| 40 | |
| 41 | def testRegexSymbols(self): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 42 | fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 43 | syms = elf.GetSymbols(fname, ['ucode']) |
| 44 | self.assertIn('.ucode', syms) |
| 45 | syms = elf.GetSymbols(fname, ['missing']) |
| 46 | self.assertNotIn('.ucode', syms) |
| 47 | syms = elf.GetSymbols(fname, ['missing', 'ucode']) |
| 48 | self.assertIn('.ucode', syms) |
| 49 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 50 | def testMissingFile(self): |
| 51 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 52 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 53 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 54 | syms = elf.LookupAndWriteSymbols('missing-file', entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 55 | self.assertIn("Filename 'missing-file' not found in input path", |
| 56 | str(e.exception)) |
| 57 | |
| 58 | def testOutsideFile(self): |
| 59 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 60 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 61 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') |
| 62 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 63 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 64 | self.assertIn('entry_path has offset 4 (size 8) but the contents size ' |
| 65 | 'is a', str(e.exception)) |
| 66 | |
| 67 | def testMissingImageStart(self): |
| 68 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 69 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 70 | 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] | 71 | self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section), |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 72 | None) |
| 73 | |
| 74 | def testBadSymbolSize(self): |
| 75 | entry = FakeEntry(10) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 76 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 77 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size') |
| 78 | with self.assertRaises(ValueError) as e: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 79 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 80 | self.assertIn('has size 1: only 4 and 8 are supported', |
| 81 | str(e.exception)) |
| 82 | |
| 83 | def testNoValue(self): |
| 84 | entry = FakeEntry(20) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 85 | section = FakeSection(sym_value=None) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 86 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 87 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 88 | self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data) |
| 89 | |
| 90 | def testDebug(self): |
| 91 | elf.debug = True |
| 92 | entry = FakeEntry(20) |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 93 | section = FakeSection() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 94 | elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') |
Simon Glass | c3f9454 | 2018-07-06 10:27:34 -0600 | [diff] [blame^] | 95 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 96 | syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 97 | elf.debug = False |
| 98 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 99 | |
| 100 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 101 | if __name__ == '__main__': |
| 102 | unittest.main() |