blob: 42d94cbbbe28bcd9a1e03f4315b9e1ff876218b3 [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
8import sys
9import unittest
10
11import elf
Simon Glass3609d5e2018-07-06 10:27:34 -060012import test_util
Simon Glass752e7552018-10-01 21:12:41 -060013import tools
Simon Glass24ad3652017-11-13 18:54:54 -070014
15binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass4ca8e042017-11-13 18:55:01 -070016
Simon Glass4ca8e042017-11-13 18:55:01 -070017
18class FakeEntry:
Simon Glass4114f972018-07-17 13:25:26 -060019 """A fake Entry object, usedfor testing
20
21 This supports an entry with a given size.
22 """
Simon Glass4ca8e042017-11-13 18:55:01 -070023 def __init__(self, contents_size):
24 self.contents_size = contents_size
Simon Glass303f62f2019-05-17 22:00:46 -060025 self.data = tools.GetBytes(ord('a'), contents_size)
Simon Glass4ca8e042017-11-13 18:55:01 -070026
27 def GetPath(self):
28 return 'entry_path'
29
Simon Glass4114f972018-07-17 13:25:26 -060030
Simon Glass8a6f56e2018-06-01 09:38:13 -060031class FakeSection:
Simon Glass4114f972018-07-17 13:25:26 -060032 """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 """
Simon Glass4ca8e042017-11-13 18:55:01 -070038 def __init__(self, sym_value=1):
39 self.sym_value = sym_value
40
41 def GetPath(self):
Simon Glass8a6f56e2018-06-01 09:38:13 -060042 return 'section_path'
Simon Glass4ca8e042017-11-13 18:55:01 -070043
44 def LookupSymbol(self, name, weak, msg):
Simon Glass4114f972018-07-17 13:25:26 -060045 """Fake implementation which returns the same value for all symbols"""
Simon Glass4ca8e042017-11-13 18:55:01 -070046 return self.sym_value
Simon Glass24ad3652017-11-13 18:54:54 -070047
Simon Glass4114f972018-07-17 13:25:26 -060048
Simon Glass24ad3652017-11-13 18:54:54 -070049class TestElf(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060050 @classmethod
51 def setUpClass(self):
52 tools.SetInputDirs(['.'])
53
Simon Glass24ad3652017-11-13 18:54:54 -070054 def testAllSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -060055 """Test that we can obtain a symbol from the ELF file"""
Simon Glass4ca8e042017-11-13 18:55:01 -070056 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -070057 syms = elf.GetSymbols(fname, [])
58 self.assertIn('.ucode', syms)
59
60 def testRegexSymbols(self):
Simon Glass4114f972018-07-17 13:25:26 -060061 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass4ca8e042017-11-13 18:55:01 -070062 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glass24ad3652017-11-13 18:54:54 -070063 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
Simon Glass4ca8e042017-11-13 18:55:01 -070070 def testMissingFile(self):
Simon Glass4114f972018-07-17 13:25:26 -060071 """Test that a missing file is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -070072 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -060073 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -070074 with self.assertRaises(ValueError) as e:
Simon Glass8a6f56e2018-06-01 09:38:13 -060075 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -070076 self.assertIn("Filename 'missing-file' not found in input path",
77 str(e.exception))
78
79 def testOutsideFile(self):
Simon Glass4114f972018-07-17 13:25:26 -060080 """Test a symbol which extends outside the entry area is detected"""
Simon Glass4ca8e042017-11-13 18:55:01 -070081 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -060082 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -070083 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
84 with self.assertRaises(ValueError) as e:
Simon Glass8a6f56e2018-06-01 09:38:13 -060085 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -070086 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
87 'is a', str(e.exception))
88
89 def testMissingImageStart(self):
Simon Glass4114f972018-07-17 13:25:26 -060090 """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 """
Simon Glass4ca8e042017-11-13 18:55:01 -070095 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -060096 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -070097 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
Simon Glass8a6f56e2018-06-01 09:38:13 -060098 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass4ca8e042017-11-13 18:55:01 -070099 None)
100
101 def testBadSymbolSize(self):
Simon Glass4114f972018-07-17 13:25:26 -0600102 """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 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700107 entry = FakeEntry(10)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600108 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -0700109 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
110 with self.assertRaises(ValueError) as e:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600111 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700112 self.assertIn('has size 1: only 4 and 8 are supported',
113 str(e.exception))
114
115 def testNoValue(self):
Simon Glass4114f972018-07-17 13:25:26 -0600116 """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 """
Simon Glass4ca8e042017-11-13 18:55:01 -0700121 entry = FakeEntry(20)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600122 section = FakeSection(sym_value=None)
Simon Glass4ca8e042017-11-13 18:55:01 -0700123 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glass8a6f56e2018-06-01 09:38:13 -0600124 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass303f62f2019-05-17 22:00:46 -0600125 self.assertEqual(tools.GetBytes(255, 16) + tools.GetBytes(ord('a'), 4),
126 entry.data)
Simon Glass4ca8e042017-11-13 18:55:01 -0700127
128 def testDebug(self):
Simon Glass4114f972018-07-17 13:25:26 -0600129 """Check that enabling debug in the elf module produced debug output"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700130 elf.debug = True
131 entry = FakeEntry(20)
Simon Glass8a6f56e2018-06-01 09:38:13 -0600132 section = FakeSection()
Simon Glass4ca8e042017-11-13 18:55:01 -0700133 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glass3609d5e2018-07-06 10:27:34 -0600134 with test_util.capture_sys_output() as (stdout, stderr):
Simon Glass8a6f56e2018-06-01 09:38:13 -0600135 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass4ca8e042017-11-13 18:55:01 -0700136 elf.debug = False
137 self.assertTrue(len(stdout.getvalue()) > 0)
138
139
Simon Glass24ad3652017-11-13 18:54:54 -0700140if __name__ == '__main__':
141 unittest.main()