blob: 9c8f1feca8596d4f901fd762c09f544acbf0aeda [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:
18 def __init__(self, contents_size):
19 self.contents_size = contents_size
20 self.data = 'a' * contents_size
21
22 def GetPath(self):
23 return 'entry_path'
24
Simon Glassf55382b2018-06-01 09:38:13 -060025class FakeSection:
Simon Glass19790632017-11-13 18:55:01 -070026 def __init__(self, sym_value=1):
27 self.sym_value = sym_value
28
29 def GetPath(self):
Simon Glassf55382b2018-06-01 09:38:13 -060030 return 'section_path'
Simon Glass19790632017-11-13 18:55:01 -070031
32 def LookupSymbol(self, name, weak, msg):
33 return self.sym_value
Simon Glassb50e5612017-11-13 18:54:54 -070034
35class TestElf(unittest.TestCase):
36 def testAllSymbols(self):
Simon Glass19790632017-11-13 18:55:01 -070037 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070038 syms = elf.GetSymbols(fname, [])
39 self.assertIn('.ucode', syms)
40
41 def testRegexSymbols(self):
Simon Glass19790632017-11-13 18:55:01 -070042 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -070043 syms = elf.GetSymbols(fname, ['ucode'])
44 self.assertIn('.ucode', syms)
45 syms = elf.GetSymbols(fname, ['missing'])
46 self.assertNotIn('.ucode', syms)
47 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
48 self.assertIn('.ucode', syms)
49
Simon Glass19790632017-11-13 18:55:01 -070050 def testMissingFile(self):
51 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060052 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070053 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060054 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
Simon Glass19790632017-11-13 18:55:01 -070055 self.assertIn("Filename 'missing-file' not found in input path",
56 str(e.exception))
57
58 def testOutsideFile(self):
59 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060060 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070061 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
62 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060063 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -070064 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
65 'is a', str(e.exception))
66
67 def testMissingImageStart(self):
68 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060069 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070070 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
Simon Glassf55382b2018-06-01 09:38:13 -060071 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
Simon Glass19790632017-11-13 18:55:01 -070072 None)
73
74 def testBadSymbolSize(self):
75 entry = FakeEntry(10)
Simon Glassf55382b2018-06-01 09:38:13 -060076 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070077 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
78 with self.assertRaises(ValueError) as e:
Simon Glassf55382b2018-06-01 09:38:13 -060079 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -070080 self.assertIn('has size 1: only 4 and 8 are supported',
81 str(e.exception))
82
83 def testNoValue(self):
84 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -060085 section = FakeSection(sym_value=None)
Simon Glass19790632017-11-13 18:55:01 -070086 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassf55382b2018-06-01 09:38:13 -060087 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -070088 self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
89
90 def testDebug(self):
91 elf.debug = True
92 entry = FakeEntry(20)
Simon Glassf55382b2018-06-01 09:38:13 -060093 section = FakeSection()
Simon Glass19790632017-11-13 18:55:01 -070094 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
Simon Glassc3f94542018-07-06 10:27:34 -060095 with test_util.capture_sys_output() as (stdout, stderr):
Simon Glassf55382b2018-06-01 09:38:13 -060096 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
Simon Glass19790632017-11-13 18:55:01 -070097 elf.debug = False
98 self.assertTrue(len(stdout.getvalue()) > 0)
99
100
Simon Glassb50e5612017-11-13 18:54:54 -0700101if __name__ == '__main__':
102 unittest.main()