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