blob: cc6e9c5128738cd5d9bd72570a5dad681e9b8b98 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassb50e5612017-11-13 18:54:54 -07002# Copyright (c) 2017 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glassb50e5612017-11-13 18:54:54 -07005# Test for the elf module
6
7import os
Simon Glassf58558a2019-07-08 13:18:34 -06008import shutil
Simon Glassb50e5612017-11-13 18:54:54 -07009import sys
Simon Glassf58558a2019-07-08 13:18:34 -060010import tempfile
Simon Glassb50e5612017-11-13 18:54:54 -070011import unittest
12
Simon Glassf58558a2019-07-08 13:18:34 -060013import command
Simon Glassb50e5612017-11-13 18:54:54 -070014import elf
Simon Glassc3f94542018-07-06 10:27:34 -060015import test_util
Simon Glasse0e62752018-10-01 21:12:41 -060016import tools
Simon Glass9f297b02019-07-20 12:23:36 -060017import tout
Simon Glassb50e5612017-11-13 18:54:54 -070018
19binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass19790632017-11-13 18:55:01 -070020
Simon Glass19790632017-11-13 18:55:01 -070021
22class FakeEntry:
Simon Glassb2b0df82018-07-17 13:25:26 -060023 """A fake Entry object, usedfor testing
24
25 This supports an entry with a given size.
26 """
Simon Glass19790632017-11-13 18:55:01 -070027 def __init__(self, contents_size):
28 self.contents_size = contents_size
Simon Glassc6c10e72019-05-17 22:00:46 -060029 self.data = tools.GetBytes(ord('a'), contents_size)
Simon Glass19790632017-11-13 18:55:01 -070030
31 def GetPath(self):
32 return 'entry_path'
33
Simon Glassb2b0df82018-07-17 13:25:26 -060034
Simon Glassf55382b2018-06-01 09:38:13 -060035class FakeSection:
Simon Glassb2b0df82018-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 Glass19790632017-11-13 18:55:01 -070042 def __init__(self, sym_value=1):
43 self.sym_value = sym_value
44
45 def GetPath(self):
Simon Glassf55382b2018-06-01 09:38:13 -060046 return 'section_path'
Simon Glass19790632017-11-13 18:55:01 -070047
48 def LookupSymbol(self, name, weak, msg):
Simon Glassb2b0df82018-07-17 13:25:26 -060049 """Fake implementation which returns the same value for all symbols"""
Simon Glass19790632017-11-13 18:55:01 -070050 return self.sym_value
Simon Glassb50e5612017-11-13 18:54:54 -070051
Simon Glassb2b0df82018-07-17 13:25:26 -060052
Simon Glassb50e5612017-11-13 18:54:54 -070053class TestElf(unittest.TestCase):
Simon Glasse0e62752018-10-01 21:12:41 -060054 @classmethod
55 def setUpClass(self):
56 tools.SetInputDirs(['.'])
57
Simon Glassb50e5612017-11-13 18:54:54 -070058 def testAllSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060059 """Test that we can obtain a symbol from the ELF file"""
Simon Glass19790632017-11-13 18:55:01 -070060 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070061 syms = elf.GetSymbols(fname, [])
62 self.assertIn('.ucode', syms)
63
64 def testRegexSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060065 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass19790632017-11-13 18:55:01 -070066 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070067 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
Simon Glass19790632017-11-13 18:55:01 -070074 def testMissingFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060075 """Test that a missing file is detected"""
Simon Glass19790632017-11-13 18:55:01 -070076 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060077 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070078 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060079 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass19790632017-11-13 18:55:01 -070080 self.assertIn("Filename 'missing-file' not found in input path",
81 str(e.exception))
82
83 def testOutsideFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060084 """Test a symbol which extends outside the entry area is detected"""
Simon Glass19790632017-11-13 18:55:01 -070085 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060086 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070087 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
88 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060089 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -070090 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
91 'is a', str(e.exception))
92
93 def testMissingImageStart(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060094 """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 """
Simon Glass19790632017-11-13 18:55:01 -070099 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600100 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700101 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
Simon Glassf55382b2018-06-01 09:38:13 -0600102 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass19790632017-11-13 18:55:01 -0700103 None)
104
105 def testBadSymbolSize(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600106 """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 """
Simon Glass19790632017-11-13 18:55:01 -0700111 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600112 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700113 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
114 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -0600115 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700116 self.assertIn('has size 1: only 4 and 8 are supported',
117 str(e.exception))
118
119 def testNoValue(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600120 """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 """
Simon Glass19790632017-11-13 18:55:01 -0700125 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -0600126 section = FakeSection(sym_value=None)
Simon Glass19790632017-11-13 18:55:01 -0700127 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassf55382b2018-06-01 09:38:13 -0600128 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glassc6c10e72019-05-17 22:00:46 -0600129 self.assertEqual(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4),
130 entry.data)
Simon Glass19790632017-11-13 18:55:01 -0700131
132 def testDebug(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600133 """Check that enabling debug in the elf module produced debug output"""
Simon Glass9f297b02019-07-20 12:23:36 -0600134 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)
Simon Glass19790632017-11-13 18:55:01 -0700144
Simon Glassf58558a2019-07-08 13:18:34 -0600145 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')
Simon Glass9d44a7e2019-08-24 07:22:45 -0600151 bin_fname = os.path.join(outdir, 'bin')
Simon Glassf58558a2019-07-08 13:18:34 -0600152
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
Simon Glassd8d40742019-07-08 13:18:35 -0600162 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
Simon Glass19790632017-11-13 18:55:01 -0700183
Simon Glassb50e5612017-11-13 18:54:54 -0700184if __name__ == '__main__':
185 unittest.main()