elf_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2017 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Test for the elf module
  6. import os
  7. import shutil
  8. import struct
  9. import sys
  10. import tempfile
  11. import unittest
  12. from binman import elf
  13. from u_boot_pylib import command
  14. from u_boot_pylib import test_util
  15. from u_boot_pylib import tools
  16. from u_boot_pylib import tout
  17. binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  18. class FakeEntry:
  19. """A fake Entry object, usedfor testing
  20. This supports an entry with a given size.
  21. """
  22. def __init__(self, contents_size):
  23. self.contents_size = contents_size
  24. self.data = tools.get_bytes(ord('a'), contents_size)
  25. def GetPath(self):
  26. return 'entry_path'
  27. class FakeSection:
  28. """A fake Section object, used for testing
  29. This has the minimum feature set needed to support testing elf functions.
  30. A LookupSymbol() function is provided which returns a fake value for amu
  31. symbol requested.
  32. """
  33. def __init__(self, sym_value=1):
  34. self.sym_value = sym_value
  35. def GetPath(self):
  36. return 'section_path'
  37. def LookupImageSymbol(self, name, weak, msg, base_addr):
  38. """Fake implementation which returns the same value for all symbols"""
  39. return self.sym_value
  40. def GetImage(self):
  41. return self
  42. def BuildElfTestFiles(target_dir):
  43. """Build ELF files used for testing in binman
  44. This compiles and links the test files into the specified directory. It uses
  45. the Makefile and source files in the binman test/ directory.
  46. Args:
  47. target_dir: Directory to put the files into
  48. """
  49. if not os.path.exists(target_dir):
  50. os.mkdir(target_dir)
  51. testdir = os.path.join(binman_dir, 'test')
  52. # If binman is involved from the main U-Boot Makefile the -r and -R
  53. # flags are set in MAKEFLAGS. This prevents this Makefile from working
  54. # correctly. So drop any make flags here.
  55. if 'MAKEFLAGS' in os.environ:
  56. del os.environ['MAKEFLAGS']
  57. try:
  58. tools.run('make', '-C', target_dir, '-f',
  59. os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
  60. except ValueError as e:
  61. # The test system seems to suppress this in a strange way
  62. print(e)
  63. class TestElf(unittest.TestCase):
  64. @classmethod
  65. def setUpClass(cls):
  66. cls._indir = tempfile.mkdtemp(prefix='elf.')
  67. tools.set_input_dirs(['.'])
  68. BuildElfTestFiles(cls._indir)
  69. @classmethod
  70. def tearDownClass(cls):
  71. if cls._indir:
  72. shutil.rmtree(cls._indir)
  73. @classmethod
  74. def ElfTestFile(cls, fname):
  75. return os.path.join(cls._indir, fname)
  76. def testAllSymbols(self):
  77. """Test that we can obtain a symbol from the ELF file"""
  78. fname = self.ElfTestFile('u_boot_ucode_ptr')
  79. syms = elf.GetSymbols(fname, [])
  80. self.assertIn('_dt_ucode_base_size', syms)
  81. def testRegexSymbols(self):
  82. """Test that we can obtain from the ELF file by regular expression"""
  83. fname = self.ElfTestFile('u_boot_ucode_ptr')
  84. syms = elf.GetSymbols(fname, ['ucode'])
  85. self.assertIn('_dt_ucode_base_size', syms)
  86. syms = elf.GetSymbols(fname, ['missing'])
  87. self.assertNotIn('_dt_ucode_base_size', syms)
  88. syms = elf.GetSymbols(fname, ['missing', 'ucode'])
  89. self.assertIn('_dt_ucode_base_size', syms)
  90. def testMissingFile(self):
  91. """Test that a missing file is detected"""
  92. entry = FakeEntry(10)
  93. section = FakeSection()
  94. with self.assertRaises(ValueError) as e:
  95. elf.LookupAndWriteSymbols('missing-file', entry, section)
  96. self.assertIn("Filename 'missing-file' not found in input path",
  97. str(e.exception))
  98. def testOutsideFile(self):
  99. """Test a symbol which extends outside the entry area is detected"""
  100. if not elf.ELF_TOOLS:
  101. self.skipTest('Python elftools not available')
  102. entry = FakeEntry(10)
  103. section = FakeSection()
  104. elf_fname = self.ElfTestFile('u_boot_binman_syms')
  105. with self.assertRaises(ValueError) as e:
  106. elf.LookupAndWriteSymbols(elf_fname, entry, section)
  107. self.assertIn('entry_path has offset 8 (size 8) but the contents size '
  108. 'is a', str(e.exception))
  109. def testMissingImageStart(self):
  110. """Test that we detect a missing __image_copy_start symbol
  111. This is needed to mark the start of the image. Without it we cannot
  112. locate the offset of a binman symbol within the image.
  113. """
  114. entry = FakeEntry(10)
  115. section = FakeSection()
  116. elf_fname = self.ElfTestFile('u_boot_binman_syms_bad')
  117. count = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  118. self.assertEqual(0, count)
  119. def testBadSymbolSize(self):
  120. """Test that an attempt to use an 8-bit symbol are detected
  121. Only 32 and 64 bits are supported, since we need to store an offset
  122. into the image.
  123. """
  124. if not elf.ELF_TOOLS:
  125. self.skipTest('Python elftools not available')
  126. entry = FakeEntry(10)
  127. section = FakeSection()
  128. elf_fname =self.ElfTestFile('u_boot_binman_syms_size')
  129. with self.assertRaises(ValueError) as e:
  130. elf.LookupAndWriteSymbols(elf_fname, entry, section)
  131. self.assertIn('has size 1: only 4 and 8 are supported',
  132. str(e.exception))
  133. def testNoValue(self):
  134. """Test the case where we have no value for the symbol
  135. This should produce -1 values for all three symbols, taking up the
  136. first 16 bytes of the image.
  137. """
  138. if not elf.ELF_TOOLS:
  139. self.skipTest('Python elftools not available')
  140. entry = FakeEntry(28)
  141. section = FakeSection(sym_value=None)
  142. elf_fname = self.ElfTestFile('u_boot_binman_syms')
  143. count = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  144. self.assertEqual(5, count)
  145. expected = (struct.pack('<L', elf.BINMAN_SYM_MAGIC_VALUE) +
  146. tools.get_bytes(255, 20) +
  147. tools.get_bytes(ord('a'), 4))
  148. self.assertEqual(expected, entry.data)
  149. def testDebug(self):
  150. """Check that enabling debug in the elf module produced debug output"""
  151. if not elf.ELF_TOOLS:
  152. self.skipTest('Python elftools not available')
  153. try:
  154. tout.init(tout.DEBUG)
  155. entry = FakeEntry(24)
  156. section = FakeSection()
  157. elf_fname = self.ElfTestFile('u_boot_binman_syms')
  158. with test_util.capture_sys_output() as (stdout, stderr):
  159. elf.LookupAndWriteSymbols(elf_fname, entry, section)
  160. self.assertTrue(len(stdout.getvalue()) > 0)
  161. finally:
  162. tout.init(tout.WARNING)
  163. def testMakeElf(self):
  164. """Test for the MakeElf function"""
  165. outdir = tempfile.mkdtemp(prefix='elf.')
  166. expected_text = b'1234'
  167. expected_data = b'wxyz'
  168. elf_fname = os.path.join(outdir, 'elf')
  169. bin_fname = os.path.join(outdir, 'bin')
  170. # Make an Elf file and then convert it to a fkat binary file. This
  171. # should produce the original data.
  172. elf.MakeElf(elf_fname, expected_text, expected_data)
  173. objcopy, args = tools.get_target_compile_tool('objcopy')
  174. args += ['-O', 'binary', elf_fname, bin_fname]
  175. stdout = command.output(objcopy, *args)
  176. with open(bin_fname, 'rb') as fd:
  177. data = fd.read()
  178. self.assertEqual(expected_text + expected_data, data)
  179. shutil.rmtree(outdir)
  180. def testDecodeElf(self):
  181. """Test for the MakeElf function"""
  182. if not elf.ELF_TOOLS:
  183. self.skipTest('Python elftools not available')
  184. outdir = tempfile.mkdtemp(prefix='elf.')
  185. expected_text = b'1234'
  186. expected_data = b'wxyz'
  187. elf_fname = os.path.join(outdir, 'elf')
  188. elf.MakeElf(elf_fname, expected_text, expected_data)
  189. data = tools.read_file(elf_fname)
  190. load = 0xfef20000
  191. entry = load + 2
  192. expected = expected_text + expected_data
  193. self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
  194. elf.DecodeElf(data, 0))
  195. self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
  196. load, entry, len(expected)),
  197. elf.DecodeElf(data, load + 2))
  198. shutil.rmtree(outdir)
  199. def testEmbedData(self):
  200. """Test for the GetSymbolFileOffset() function"""
  201. if not elf.ELF_TOOLS:
  202. self.skipTest('Python elftools not available')
  203. fname = self.ElfTestFile('embed_data')
  204. offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
  205. start = offset['embed_start'].offset
  206. end = offset['embed_end'].offset
  207. data = tools.read_file(fname)
  208. embed_data = data[start:end]
  209. expect = struct.pack('<IIIII', 2, 3, 0x1234, 0x5678, 0)
  210. self.assertEqual(expect, embed_data)
  211. def testEmbedFail(self):
  212. """Test calling GetSymbolFileOffset() without elftools"""
  213. try:
  214. old_val = elf.ELF_TOOLS
  215. elf.ELF_TOOLS = False
  216. fname = self.ElfTestFile('embed_data')
  217. with self.assertRaises(ValueError) as e:
  218. elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
  219. with self.assertRaises(ValueError) as e:
  220. elf.DecodeElf(tools.read_file(fname), 0xdeadbeef)
  221. with self.assertRaises(ValueError) as e:
  222. elf.GetFileOffset(fname, 0xdeadbeef)
  223. with self.assertRaises(ValueError) as e:
  224. elf.GetSymbolFromAddress(fname, 0xdeadbeef)
  225. with self.assertRaises(ValueError) as e:
  226. entry = FakeEntry(10)
  227. section = FakeSection()
  228. elf.LookupAndWriteSymbols(fname, entry, section, True)
  229. self.assertIn(
  230. "Section 'section_path': entry 'entry_path': Cannot write symbols to an ELF file without Python elftools",
  231. str(e.exception))
  232. finally:
  233. elf.ELF_TOOLS = old_val
  234. def testEmbedDataNoSym(self):
  235. """Test for GetSymbolFileOffset() getting no symbols"""
  236. if not elf.ELF_TOOLS:
  237. self.skipTest('Python elftools not available')
  238. fname = self.ElfTestFile('embed_data')
  239. offset = elf.GetSymbolFileOffset(fname, ['missing_sym'])
  240. self.assertEqual({}, offset)
  241. def test_read_loadable_segments(self):
  242. """Test for read_loadable_segments()"""
  243. if not elf.ELF_TOOLS:
  244. self.skipTest('Python elftools not available')
  245. fname = self.ElfTestFile('embed_data')
  246. segments, entry = elf.read_loadable_segments(tools.read_file(fname))
  247. def test_read_segments_fail(self):
  248. """Test for read_loadable_segments() without elftools"""
  249. try:
  250. old_val = elf.ELF_TOOLS
  251. elf.ELF_TOOLS = False
  252. fname = self.ElfTestFile('embed_data')
  253. with self.assertRaises(ValueError) as e:
  254. elf.read_loadable_segments(tools.read_file(fname))
  255. self.assertIn("Python: No module named 'elftools'",
  256. str(e.exception))
  257. finally:
  258. elf.ELF_TOOLS = old_val
  259. def test_read_segments_bad_data(self):
  260. """Test for read_loadable_segments() with an invalid ELF file"""
  261. if not elf.ELF_TOOLS:
  262. self.skipTest('Python elftools not available')
  263. fname = self.ElfTestFile('embed_data')
  264. with self.assertRaises(ValueError) as e:
  265. elf.read_loadable_segments(tools.get_bytes(100, 100))
  266. self.assertIn('Magic number does not match', str(e.exception))
  267. def test_get_file_offset(self):
  268. """Test GetFileOffset() gives the correct file offset for a symbol"""
  269. if not elf.ELF_TOOLS:
  270. self.skipTest('Python elftools not available')
  271. fname = self.ElfTestFile('embed_data')
  272. syms = elf.GetSymbols(fname, ['embed'])
  273. addr = syms['embed'].address
  274. offset = elf.GetFileOffset(fname, addr)
  275. data = tools.read_file(fname)
  276. # Just use the first 4 bytes and assume it is little endian
  277. embed_data = data[offset:offset + 4]
  278. embed_value = struct.unpack('<I', embed_data)[0]
  279. self.assertEqual(0x1234, embed_value)
  280. def test_get_file_offset_fail(self):
  281. """Test calling GetFileOffset() without elftools"""
  282. try:
  283. old_val = elf.ELF_TOOLS
  284. elf.ELF_TOOLS = False
  285. fname = self.ElfTestFile('embed_data')
  286. with self.assertRaises(ValueError) as e:
  287. elf.GetFileOffset(fname, 0)
  288. self.assertIn("Python: No module named 'elftools'",
  289. str(e.exception))
  290. finally:
  291. elf.ELF_TOOLS = old_val
  292. def test_get_symbol_from_address(self):
  293. """Test GetSymbolFromAddress()"""
  294. if not elf.ELF_TOOLS:
  295. self.skipTest('Python elftools not available')
  296. fname = self.ElfTestFile('elf_sections')
  297. sym_name = 'calculate'
  298. syms = elf.GetSymbols(fname, [sym_name])
  299. addr = syms[sym_name].address
  300. sym = elf.GetSymbolFromAddress(fname, addr)
  301. self.assertEqual(sym_name, sym)
  302. def test_get_symbol_from_address_fail(self):
  303. """Test calling GetSymbolFromAddress() without elftools"""
  304. try:
  305. old_val = elf.ELF_TOOLS
  306. elf.ELF_TOOLS = False
  307. fname = self.ElfTestFile('embed_data')
  308. with self.assertRaises(ValueError) as e:
  309. elf.GetSymbolFromAddress(fname, 0x1000)
  310. self.assertIn("Python: No module named 'elftools'",
  311. str(e.exception))
  312. finally:
  313. elf.ELF_TOOLS = old_val
  314. def test_is_valid(self):
  315. """Test is_valid()"""
  316. self.assertEqual(False, elf.is_valid(b''))
  317. self.assertEqual(False, elf.is_valid(b'1234'))
  318. fname = self.ElfTestFile('elf_sections')
  319. data = tools.read_file(fname)
  320. self.assertEqual(True, elf.is_valid(data))
  321. self.assertEqual(False, elf.is_valid(data[4:]))
  322. def test_get_symbol_offset(self):
  323. fname = self.ElfTestFile('embed_data')
  324. syms = elf.GetSymbols(fname, ['embed_start', 'embed'])
  325. expected = syms['embed'].address - syms['embed_start'].address
  326. val = elf.GetSymbolOffset(fname, 'embed', 'embed_start')
  327. self.assertEqual(expected, val)
  328. with self.assertRaises(KeyError) as e:
  329. elf.GetSymbolOffset(fname, 'embed')
  330. self.assertIn('__image_copy_start', str(e.exception))
  331. def test_get_symbol_address(self):
  332. fname = self.ElfTestFile('embed_data')
  333. addr = elf.GetSymbolAddress(fname, 'region_size')
  334. self.assertEqual(0, addr)
  335. if __name__ == '__main__':
  336. unittest.main()