blob: b68530c19ba6e6c724bed6d970a5dfcc203049d3 [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
8import sys
9import unittest
10
11import elf
Simon Glassc3f94542018-07-06 10:27:34 -060012import test_util
Simon Glasse0e62752018-10-01 21:12:41 -060013import tools
Simon Glassb50e5612017-11-13 18:54:54 -070014
15binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass19790632017-11-13 18:55:01 -070016
Simon Glass19790632017-11-13 18:55:01 -070017
18class FakeEntry:
Simon Glassb2b0df82018-07-17 13:25:26 -060019 """A fake Entry object, usedfor testing
20
21 This supports an entry with a given size.
22 """
Simon Glass19790632017-11-13 18:55:01 -070023 def __init__(self, contents_size):
24 self.contents_size = contents_size
25 self.data = 'a' * contents_size
26
27 def GetPath(self):
28 return 'entry_path'
29
Simon Glassb2b0df82018-07-17 13:25:26 -060030
Simon Glassf55382b2018-06-01 09:38:13 -060031class FakeSection:
Simon Glassb2b0df82018-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 Glass19790632017-11-13 18:55:01 -070038 def __init__(self, sym_value=1):
39 self.sym_value = sym_value
40
41 def GetPath(self):
Simon Glassf55382b2018-06-01 09:38:13 -060042 return 'section_path'
Simon Glass19790632017-11-13 18:55:01 -070043
44 def LookupSymbol(self, name, weak, msg):
Simon Glassb2b0df82018-07-17 13:25:26 -060045 """Fake implementation which returns the same value for all symbols"""
Simon Glass19790632017-11-13 18:55:01 -070046 return self.sym_value
Simon Glassb50e5612017-11-13 18:54:54 -070047
Simon Glassb2b0df82018-07-17 13:25:26 -060048
Simon Glassb50e5612017-11-13 18:54:54 -070049class TestElf(unittest.TestCase):
Simon Glasse0e62752018-10-01 21:12:41 -060050 @classmethod
51 def setUpClass(self):
52 tools.SetInputDirs(['.'])
53
Simon Glassb50e5612017-11-13 18:54:54 -070054 def testAllSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060055 """Test that we can obtain a symbol from the ELF file"""
Simon Glass19790632017-11-13 18:55:01 -070056 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070057 syms = elf.GetSymbols(fname, [])
58 self.assertIn('.ucode', syms)
59
60 def testRegexSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060061 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass19790632017-11-13 18:55:01 -070062 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-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 Glass19790632017-11-13 18:55:01 -070070 def testMissingFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060071 """Test that a missing file is detected"""
Simon Glass19790632017-11-13 18:55:01 -070072 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060073 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070074 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060075 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass19790632017-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 Glassb2b0df82018-07-17 13:25:26 -060080 """Test a symbol which extends outside the entry area is detected"""
Simon Glass19790632017-11-13 18:55:01 -070081 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060082 section = FakeSection()
Simon Glass19790632017-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 Glassf55382b2018-06-01 09:38:13 -060085 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-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 Glassb2b0df82018-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 Glass19790632017-11-13 18:55:01 -070095 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060096 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070097 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
Simon Glassf55382b2018-06-01 09:38:13 -060098 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass19790632017-11-13 18:55:01 -070099 None)
100
101 def testBadSymbolSize(self):
Simon Glassb2b0df82018-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 Glass19790632017-11-13 18:55:01 -0700107 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600108 section = FakeSection()
Simon Glass19790632017-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 Glassf55382b2018-06-01 09:38:13 -0600111 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-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 Glassb2b0df82018-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 Glass19790632017-11-13 18:55:01 -0700121 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -0600122 section = FakeSection(sym_value=None)
Simon Glass19790632017-11-13 18:55:01 -0700123 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassf55382b2018-06-01 09:38:13 -0600124 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700125 self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
126
127 def testDebug(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600128 """Check that enabling debug in the elf module produced debug output"""
Simon Glass19790632017-11-13 18:55:01 -0700129 elf.debug = True
130 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -0600131 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700132 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassc3f94542018-07-06 10:27:34 -0600133 with test_util.capture_sys_output() as (stdout, stderr):
Simon Glassf55382b2018-06-01 09:38:13 -0600134 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700135 elf.debug = False
136 self.assertTrue(len(stdout.getvalue()) > 0)
137
138
Simon Glassb50e5612017-11-13 18:54:54 -0700139if __name__ == '__main__':
140 unittest.main()