Merge tag 'dm-pull-9jul19-take2' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[oweals/u-boot.git] / tools / binman / elf_test.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2017 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
4 #
5 # Test for the elf module
6
7 import os
8 import sys
9 import unittest
10
11 import elf
12 import test_util
13 import tools
14
15 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
16
17
18 class FakeEntry:
19     """A fake Entry object, usedfor testing
20
21     This supports an entry with a given size.
22     """
23     def __init__(self, contents_size):
24         self.contents_size = contents_size
25         self.data = tools.GetBytes(ord('a'), contents_size)
26
27     def GetPath(self):
28         return 'entry_path'
29
30
31 class FakeSection:
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     """
38     def __init__(self, sym_value=1):
39         self.sym_value = sym_value
40
41     def GetPath(self):
42         return 'section_path'
43
44     def LookupSymbol(self, name, weak, msg):
45         """Fake implementation which returns the same value for all symbols"""
46         return self.sym_value
47
48
49 class TestElf(unittest.TestCase):
50     @classmethod
51     def setUpClass(self):
52         tools.SetInputDirs(['.'])
53
54     def testAllSymbols(self):
55         """Test that we can obtain a symbol from the ELF file"""
56         fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
57         syms = elf.GetSymbols(fname, [])
58         self.assertIn('.ucode', syms)
59
60     def testRegexSymbols(self):
61         """Test that we can obtain from the ELF file by regular expression"""
62         fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
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
70     def testMissingFile(self):
71         """Test that a missing file is detected"""
72         entry = FakeEntry(10)
73         section = FakeSection()
74         with self.assertRaises(ValueError) as e:
75             syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
76         self.assertIn("Filename 'missing-file' not found in input path",
77                       str(e.exception))
78
79     def testOutsideFile(self):
80         """Test a symbol which extends outside the entry area is detected"""
81         entry = FakeEntry(10)
82         section = FakeSection()
83         elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
84         with self.assertRaises(ValueError) as e:
85             syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
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):
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         """
95         entry = FakeEntry(10)
96         section = FakeSection()
97         elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
98         self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
99                          None)
100
101     def testBadSymbolSize(self):
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         """
107         entry = FakeEntry(10)
108         section = FakeSection()
109         elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
110         with self.assertRaises(ValueError) as e:
111             syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
112         self.assertIn('has size 1: only 4 and 8 are supported',
113                       str(e.exception))
114
115     def testNoValue(self):
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         """
121         entry = FakeEntry(20)
122         section = FakeSection(sym_value=None)
123         elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
124         syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
125         self.assertEqual(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4),
126                                                                   entry.data)
127
128     def testDebug(self):
129         """Check that enabling debug in the elf module produced debug output"""
130         elf.debug = True
131         entry = FakeEntry(20)
132         section = FakeSection()
133         elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
134         with test_util.capture_sys_output() as (stdout, stderr):
135             syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
136         elf.debug = False
137         self.assertTrue(len(stdout.getvalue()) > 0)
138
139
140 if __name__ == '__main__':
141     unittest.main()