blob: 7a128018d9f0b9488c72f354dac86930338bd547 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass24ad3652017-11-13 18:54:54 -07002# Copyright (c) 2017 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass24ad3652017-11-13 18:54:54 -07005# Test for the elf module
6
7import os
Simon Glass4f379ea2019-07-08 13:18:34 -06008import shutil
Simon Glass24ad3652017-11-13 18:54:54 -07009import sys
Simon Glass4f379ea2019-07-08 13:18:34 -060010import tempfile
Simon Glass24ad3652017-11-13 18:54:54 -070011import unittest
12
Simon Glassc585dd42020-04-17 18:09:03 -060013from binman import elf
Simon Glassa997ea52020-04-17 18:09:04 -060014from patman import command
15from patman import test_util
16from patman import tools
17from patman import tout
Simon Glass24ad3652017-11-13 18:54:54 -070018
19binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass4ca8e042017-11-13 18:55:01 -070020
Simon Glass4ca8e042017-11-13 18:55:01 -070021
22class FakeEntry:
Simon Glass4114f972018-07-17 13:25:26 -060023 """A fake Entry object, usedfor testing
24
25 This supports an entry with a given size.
26 """
Simon Glass4ca8e042017-11-13 18:55:01 -070027 def __init__(self, contents_size):
28 self.contents_size = contents_size
Simon Glass303f62f2019-05-17 22:00:46 -060029 self.data = tools.GetBytes(ord('a'), contents_size)
Simon Glass4ca8e042017-11-13 18:55:01 -070030
31 def GetPath(self):
32 return 'entry_path'
33
Simon Glass4114f972018-07-17 13:25:26 -060034
Simon Glass8a6f56e2018-06-01 09:38:13 -060035class FakeSection:
Simon Glass4114f972018-07-17 13:25:26 -060036 """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 """
Simon Glass4ca8e042017-11-13 18:55:01 -070042 def __init__(self, sym_value=1):
43 self.sym_value = sym_value
44
45 def GetPath(self):
Simon Glass8a6f56e2018-06-01 09:38:13 -060046 return 'section_path'
Simon Glass4ca8e042017-11-13 18:55:01 -070047
Simon Glassecbe4732021-01-06 21:35:15 -070048 def LookupImageSymbol(self, name, weak, msg, base_addr):
Simon Glass4114f972018-07-17 13:25:26 -060049 """Fake implementation which returns the same value for all symbols"""
Simon Glass4ca8e042017-11-13 18:55:01 -070050 return self.sym_value
Simon Glass24ad3652017-11-13 18:54:54 -070051
Simon Glassecbe4732021-01-06 21:35:15 -070052 def GetImage(self):
53 return self
Simon Glass4114f972018-07-17 13:25:26 -060054
Simon Glassf6290892019-08-24 07:22:53 -060055def BuildElfTestFiles(target_dir):
56 """Build ELF files used for testing in binman
57
58 This compiles and links the test files into the specified directory. It the
59 Makefile and source files in the binman test/ directory.
60
61 Args:
62 target_dir: Directory to put the files into
63 """
64 if not os.path.exists(target_dir):
65 os.mkdir(target_dir)
66 testdir = os.path.join(binman_dir, 'test')
67
68 # If binman is involved from the main U-Boot Makefile the -r and -R
69 # flags are set in MAKEFLAGS. This prevents this Makefile from working
70 # correctly. So drop any make flags here.
71 if 'MAKEFLAGS' in os.environ:
72 del os.environ['MAKEFLAGS']
73 tools.Run('make', '-C', target_dir, '-f',
Simon Glass93a806f2019-08-24 07:22:59 -060074 os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
Simon Glassf6290892019-08-24 07:22:53 -060075
76
Simon Glass24ad3652017-11-13 18:54:54 -070077class TestElf(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060078 @classmethod
Simon Glass4affd4b2019-08-24 07:22:54 -060079 def setUpClass(cls):
80 cls._indir = tempfile.mkdtemp(prefix='elf.')
Simon Glass752e7552018-10-01 21:12:41 -060081 tools.SetInputDirs(['.'])
Simon Glass4affd4b2019-08-24 07:22:54 -060082 BuildElfTestFiles(cls._indir)
83
84 @classmethod
85 def tearDownClass(cls):
86 if cls._indir:
87 shutil.rmtree(cls._indir)
88
89 @classmethod
90 def ElfTestFile(cls, fname):
91 return os.path.join(cls._indir, fname)
Simon Glass752e7552018-10-01 21:12:41 -060092
Simon Glass24ad3652017-11-13 18:54:54 -070093 def testAllSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -060094 """Test that we can obtain a symbol from the ELF file"""
Simon Glass4affd4b2019-08-24 07:22:54 -060095 fname = self.ElfTestFile('u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -070096 syms = elf.GetSymbols(fname, [])
97 self.assertIn('.ucode', syms)
98
99 def testRegexSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -0600100 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass4affd4b2019-08-24 07:22:54 -0600101 fname = self.ElfTestFile('u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -0700102 syms = elf.GetSymbols(fname, ['ucode'])
103 self.assertIn('.ucode', syms)
104 syms = elf.GetSymbols(fname, ['missing'])
105 self.assertNotIn('.ucode', syms)
106 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
107 self.assertIn('.ucode', syms)
108
Simon Glass4ca8e042017-11-13 18:55:01 -0700109 def testMissingFile(self):
Simon Glass4114f972018-07-17 13:25:26 -0600110 """Test that a missing file is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700111 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600112 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -0700113 with self.assertRaises(ValueError) as e:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600114 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700115 self.assertIn("Filename 'missing-file' not found in input path",
116 str(e.exception))
117
118 def testOutsideFile(self):
Simon Glass4114f972018-07-17 13:25:26 -0600119 """Test a symbol which extends outside the entry area is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700120 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600121 section = FakeSection()
Simon Glass5d0c0262019-08-24 07:22:56 -0600122 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass4ca8e042017-11-13 18:55:01 -0700123 with self.assertRaises(ValueError) as e:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600124 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700125 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
126 'is a', str(e.exception))
127
128 def testMissingImageStart(self):
Simon Glass4114f972018-07-17 13:25:26 -0600129 """Test that we detect a missing __image_copy_start symbol
130
131 This is needed to mark the start of the image. Without it we cannot
132 locate the offset of a binman symbol within the image.
133 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700134 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600135 section = FakeSection()
Simon Glass46ea6912019-08-24 07:22:58 -0600136 elf_fname = self.ElfTestFile('u_boot_binman_syms_bad')
Simon Glass8a6f56e2018-06-01 09:38:13 -0600137 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass4ca8e042017-11-13 18:55:01 -0700138 None)
139
140 def testBadSymbolSize(self):
Simon Glass4114f972018-07-17 13:25:26 -0600141 """Test that an attempt to use an 8-bit symbol are detected
142
143 Only 32 and 64 bits are supported, since we need to store an offset
144 into the image.
145 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700146 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600147 section = FakeSection()
Simon Glassb8deb122019-08-24 07:22:57 -0600148 elf_fname =self.ElfTestFile('u_boot_binman_syms_size')
Simon Glass4ca8e042017-11-13 18:55:01 -0700149 with self.assertRaises(ValueError) as e:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600150 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700151 self.assertIn('has size 1: only 4 and 8 are supported',
152 str(e.exception))
153
154 def testNoValue(self):
Simon Glass4114f972018-07-17 13:25:26 -0600155 """Test the case where we have no value for the symbol
156
157 This should produce -1 values for all thress symbols, taking up the
158 first 16 bytes of the image.
159 """
Simon Glass3f8ff012019-08-24 07:23:05 -0600160 entry = FakeEntry(24)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600161 section = FakeSection(sym_value=None)
Simon Glass5d0c0262019-08-24 07:22:56 -0600162 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass8a6f56e2018-06-01 09:38:13 -0600163 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass3f8ff012019-08-24 07:23:05 -0600164 self.assertEqual(tools.GetBytes(255, 20) + tools.GetBytes(ord('a'), 4),
Simon Glass303f62f2019-05-17 22:00:46 -0600165 entry.data)
Simon Glass4ca8e042017-11-13 18:55:01 -0700166
167 def testDebug(self):
Simon Glass4114f972018-07-17 13:25:26 -0600168 """Check that enabling debug in the elf module produced debug output"""
Simon Glassb6dff4c2019-07-20 12:23:36 -0600169 try:
170 tout.Init(tout.DEBUG)
171 entry = FakeEntry(20)
172 section = FakeSection()
Simon Glass5d0c0262019-08-24 07:22:56 -0600173 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glassb6dff4c2019-07-20 12:23:36 -0600174 with test_util.capture_sys_output() as (stdout, stderr):
175 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
176 self.assertTrue(len(stdout.getvalue()) > 0)
177 finally:
178 tout.Init(tout.WARNING)
Simon Glass4ca8e042017-11-13 18:55:01 -0700179
Simon Glass4f379ea2019-07-08 13:18:34 -0600180 def testMakeElf(self):
181 """Test for the MakeElf function"""
182 outdir = tempfile.mkdtemp(prefix='elf.')
183 expected_text = b'1234'
184 expected_data = b'wxyz'
185 elf_fname = os.path.join(outdir, 'elf')
Simon Glassd349ada2019-08-24 07:22:45 -0600186 bin_fname = os.path.join(outdir, 'bin')
Simon Glass4f379ea2019-07-08 13:18:34 -0600187
188 # Make an Elf file and then convert it to a fkat binary file. This
189 # should produce the original data.
190 elf.MakeElf(elf_fname, expected_text, expected_data)
Alper Nebi Yasak5cd321d2020-09-06 14:46:05 +0300191 objcopy, args = tools.GetTargetCompileTool('objcopy')
192 args += ['-O', 'binary', elf_fname, bin_fname]
193 stdout = command.Output(objcopy, *args)
Simon Glass4f379ea2019-07-08 13:18:34 -0600194 with open(bin_fname, 'rb') as fd:
195 data = fd.read()
196 self.assertEqual(expected_text + expected_data, data)
197 shutil.rmtree(outdir)
198
Simon Glass567b6822019-07-08 13:18:35 -0600199 def testDecodeElf(self):
200 """Test for the MakeElf function"""
201 if not elf.ELF_TOOLS:
202 self.skipTest('Python elftools not available')
203 outdir = tempfile.mkdtemp(prefix='elf.')
204 expected_text = b'1234'
205 expected_data = b'wxyz'
206 elf_fname = os.path.join(outdir, 'elf')
207 elf.MakeElf(elf_fname, expected_text, expected_data)
208 data = tools.ReadFile(elf_fname)
209
210 load = 0xfef20000
211 entry = load + 2
212 expected = expected_text + expected_data
213 self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
214 elf.DecodeElf(data, 0))
215 self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
216 load, entry, len(expected)),
217 elf.DecodeElf(data, load + 2))
Simon Glass4affd4b2019-08-24 07:22:54 -0600218 shutil.rmtree(outdir)
Simon Glass567b6822019-07-08 13:18:35 -0600219
Simon Glass4ca8e042017-11-13 18:55:01 -0700220
Simon Glass24ad3652017-11-13 18:54:54 -0700221if __name__ == '__main__':
222 unittest.main()