blob: c16f71401d1c92be88e0d53979e5b2ba16b1fffc [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 Glassb50e5612017-11-13 18:54:54 -070013
14binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass19790632017-11-13 18:55:01 -070015
Simon Glass19790632017-11-13 18:55:01 -070016
17class FakeEntry:
Simon Glassb2b0df82018-07-17 13:25:26 -060018 """A fake Entry object, usedfor testing
19
20 This supports an entry with a given size.
21 """
Simon Glass19790632017-11-13 18:55:01 -070022 def __init__(self, contents_size):
23 self.contents_size = contents_size
24 self.data = 'a' * contents_size
25
26 def GetPath(self):
27 return 'entry_path'
28
Simon Glassb2b0df82018-07-17 13:25:26 -060029
Simon Glassf55382b2018-06-01 09:38:13 -060030class FakeSection:
Simon Glassb2b0df82018-07-17 13:25:26 -060031 """A fake Section object, used for testing
32
33 This has the minimum feature set needed to support testing elf functions.
34 A LookupSymbol() function is provided which returns a fake value for amu
35 symbol requested.
36 """
Simon Glass19790632017-11-13 18:55:01 -070037 def __init__(self, sym_value=1):
38 self.sym_value = sym_value
39
40 def GetPath(self):
Simon Glassf55382b2018-06-01 09:38:13 -060041 return 'section_path'
Simon Glass19790632017-11-13 18:55:01 -070042
43 def LookupSymbol(self, name, weak, msg):
Simon Glassb2b0df82018-07-17 13:25:26 -060044 """Fake implementation which returns the same value for all symbols"""
Simon Glass19790632017-11-13 18:55:01 -070045 return self.sym_value
Simon Glassb50e5612017-11-13 18:54:54 -070046
Simon Glassb2b0df82018-07-17 13:25:26 -060047
Simon Glassb50e5612017-11-13 18:54:54 -070048class TestElf(unittest.TestCase):
49 def testAllSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060050 """Test that we can obtain a symbol from the ELF file"""
Simon Glass19790632017-11-13 18:55:01 -070051 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070052 syms = elf.GetSymbols(fname, [])
53 self.assertIn('.ucode', syms)
54
55 def testRegexSymbols(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060056 """Test that we can obtain from the ELF file by regular expression"""
Simon Glass19790632017-11-13 18:55:01 -070057 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070058 syms = elf.GetSymbols(fname, ['ucode'])
59 self.assertIn('.ucode', syms)
60 syms = elf.GetSymbols(fname, ['missing'])
61 self.assertNotIn('.ucode', syms)
62 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
63 self.assertIn('.ucode', syms)
64
Simon Glass19790632017-11-13 18:55:01 -070065 def testMissingFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060066 """Test that a missing file is detected"""
Simon Glass19790632017-11-13 18:55:01 -070067 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060068 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070069 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060070 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass19790632017-11-13 18:55:01 -070071 self.assertIn("Filename 'missing-file' not found in input path",
72 str(e.exception))
73
74 def testOutsideFile(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060075 """Test a symbol which extends outside the entry area 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 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
79 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060080 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -070081 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
82 'is a', str(e.exception))
83
84 def testMissingImageStart(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060085 """Test that we detect a missing __image_copy_start symbol
86
87 This is needed to mark the start of the image. Without it we cannot
88 locate the offset of a binman symbol within the image.
89 """
Simon Glass19790632017-11-13 18:55:01 -070090 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060091 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070092 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
Simon Glassf55382b2018-06-01 09:38:13 -060093 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass19790632017-11-13 18:55:01 -070094 None)
95
96 def testBadSymbolSize(self):
Simon Glassb2b0df82018-07-17 13:25:26 -060097 """Test that an attempt to use an 8-bit symbol are detected
98
99 Only 32 and 64 bits are supported, since we need to store an offset
100 into the image.
101 """
Simon Glass19790632017-11-13 18:55:01 -0700102 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -0600103 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700104 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
105 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -0600106 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700107 self.assertIn('has size 1: only 4 and 8 are supported',
108 str(e.exception))
109
110 def testNoValue(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600111 """Test the case where we have no value for the symbol
112
113 This should produce -1 values for all thress symbols, taking up the
114 first 16 bytes of the image.
115 """
Simon Glass19790632017-11-13 18:55:01 -0700116 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -0600117 section = FakeSection(sym_value=None)
Simon Glass19790632017-11-13 18:55:01 -0700118 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassf55382b2018-06-01 09:38:13 -0600119 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700120 self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
121
122 def testDebug(self):
Simon Glassb2b0df82018-07-17 13:25:26 -0600123 """Check that enabling debug in the elf module produced debug output"""
Simon Glass19790632017-11-13 18:55:01 -0700124 elf.debug = True
125 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -0600126 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -0700127 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassc3f94542018-07-06 10:27:34 -0600128 with test_util.capture_sys_output() as (stdout, stderr):
Simon Glassf55382b2018-06-01 09:38:13 -0600129 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -0700130 elf.debug = False
131 self.assertTrue(len(stdout.getvalue()) > 0)
132
133
Simon Glassb50e5612017-11-13 18:54:54 -0700134if __name__ == '__main__':
135 unittest.main()