blob: 736b931fd547a31f8987d07dd33c64744c785df8 [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 Glass53e22bf2019-08-24 07:22:53 -060053def BuildElfTestFiles(target_dir):
54 """Build ELF files used for testing in binman
55
56 This compiles and links the test files into the specified directory. It the
57 Makefile and source files in the binman test/ directory.
58
59 Args:
60 target_dir: Directory to put the files into
61 """
62 if not os.path.exists(target_dir):
63 os.mkdir(target_dir)
64 testdir = os.path.join(binman_dir, 'test')
65
66 # If binman is involved from the main U-Boot Makefile the -r and -R
67 # flags are set in MAKEFLAGS. This prevents this Makefile from working
68 # correctly. So drop any make flags here.
69 if 'MAKEFLAGS' in os.environ:
70 del os.environ['MAKEFLAGS']
71 tools.Run('make', '-C', target_dir, '-f',
72 os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir,
73 'bss_data', 'u_boot_ucode_ptr')
74
75
Simon Glassb50e5612017-11-13 18:54:54 -070076class TestElf(unittest.TestCase):
Simon Glasse0e62752018-10-01 21:12:41 -060077 @classmethod
78 def setUpClass(self):
79 tools.SetInputDirs(['.'])
80
Simon Glassb50e5612017-11-13 18:54:54 -070081 def testAllSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060082 """Test that we can obtain a symbol from the ELF file"""
Simon Glass19790632017-11-13 18:55:01 -070083 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070084 syms = elf.GetSymbols(fname, [])
85 self.assertIn('.ucode', syms)
86
87 def testRegexSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060088 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass19790632017-11-13 18:55:01 -070089 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070090 syms = elf.GetSymbols(fname, ['ucode'])
91 self.assertIn('.ucode', syms)
92 syms = elf.GetSymbols(fname, ['missing'])
93 self.assertNotIn('.ucode', syms)
94 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
95 self.assertIn('.ucode', syms)
96
Simon Glass19790632017-11-13 18:55:01 -070097 def testMissingFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060098 """Test that a missing file is detected"""
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 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -0600102 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700103 self.assertIn("Filename 'missing-file' not found in input path",
104 str(e.exception))
105
106 def testOutsideFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600107 """Test a symbol which extends outside the entry area is detected"""
Simon Glass19790632017-11-13 18:55:01 -0700108 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600109 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700110 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
111 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -0600112 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700113 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
114 'is a', str(e.exception))
115
116 def testMissingImageStart(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600117 """Test that we detect a missing __image_copy_start symbol
118
119 This is needed to mark the start of the image. Without it we cannot
120 locate the offset of a binman symbol within the image.
121 """
Simon Glass19790632017-11-13 18:55:01 -0700122 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600123 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700124 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
Simon Glassf55382b2018-06-01 09:38:13 -0600125 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass19790632017-11-13 18:55:01 -0700126 None)
127
128 def testBadSymbolSize(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600129 """Test that an attempt to use an 8-bit symbol are detected
130
131 Only 32 and 64 bits are supported, since we need to store an offset
132 into the image.
133 """
Simon Glass19790632017-11-13 18:55:01 -0700134 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600135 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700136 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
137 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -0600138 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700139 self.assertIn('has size 1: only 4 and 8 are supported',
140 str(e.exception))
141
142 def testNoValue(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600143 """Test the case where we have no value for the symbol
144
145 This should produce -1 values for all thress symbols, taking up the
146 first 16 bytes of the image.
147 """
Simon Glass19790632017-11-13 18:55:01 -0700148 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -0600149 section = FakeSection(sym_value=None)
Simon Glass19790632017-11-13 18:55:01 -0700150 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassf55382b2018-06-01 09:38:13 -0600151 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glassc6c10e72019-05-17 22:00:46 -0600152 self.assertEqual(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4),
153 entry.data)
Simon Glass19790632017-11-13 18:55:01 -0700154
155 def testDebug(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600156 """Check that enabling debug in the elf module produced debug output"""
Simon Glass9f297b02019-07-20 12:23:36 -0600157 try:
158 tout.Init(tout.DEBUG)
159 entry = FakeEntry(20)
160 section = FakeSection()
161 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
162 with test_util.capture_sys_output() as (stdout, stderr):
163 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
164 self.assertTrue(len(stdout.getvalue()) > 0)
165 finally:
166 tout.Init(tout.WARNING)
Simon Glass19790632017-11-13 18:55:01 -0700167
Simon Glassf58558a2019-07-08 13:18:34 -0600168 def testMakeElf(self):
169 """Test for the MakeElf function"""
170 outdir = tempfile.mkdtemp(prefix='elf.')
171 expected_text = b'1234'
172 expected_data = b'wxyz'
173 elf_fname = os.path.join(outdir, 'elf')
Simon Glass9d44a7e2019-08-24 07:22:45 -0600174 bin_fname = os.path.join(outdir, 'bin')
Simon Glassf58558a2019-07-08 13:18:34 -0600175
176 # Make an Elf file and then convert it to a fkat binary file. This
177 # should produce the original data.
178 elf.MakeElf(elf_fname, expected_text, expected_data)
179 stdout = command.Output('objcopy', '-O', 'binary', elf_fname, bin_fname)
180 with open(bin_fname, 'rb') as fd:
181 data = fd.read()
182 self.assertEqual(expected_text + expected_data, data)
183 shutil.rmtree(outdir)
184
Simon Glassd8d40742019-07-08 13:18:35 -0600185 def testDecodeElf(self):
186 """Test for the MakeElf function"""
187 if not elf.ELF_TOOLS:
188 self.skipTest('Python elftools not available')
189 outdir = tempfile.mkdtemp(prefix='elf.')
190 expected_text = b'1234'
191 expected_data = b'wxyz'
192 elf_fname = os.path.join(outdir, 'elf')
193 elf.MakeElf(elf_fname, expected_text, expected_data)
194 data = tools.ReadFile(elf_fname)
195
196 load = 0xfef20000
197 entry = load + 2
198 expected = expected_text + expected_data
199 self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
200 elf.DecodeElf(data, 0))
201 self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
202 load, entry, len(expected)),
203 elf.DecodeElf(data, load + 2))
204 #shutil.rmtree(outdir)
205
Simon Glass19790632017-11-13 18:55:01 -0700206
Simon Glassb50e5612017-11-13 18:54:54 -0700207if __name__ == '__main__':
208 unittest.main()