elf_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 sys
  8. import unittest
  9. import elf
  10. import test_util
  11. import tools
  12. binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  13. class FakeEntry:
  14. """A fake Entry object, usedfor testing
  15. This supports an entry with a given size.
  16. """
  17. def __init__(self, contents_size):
  18. self.contents_size = contents_size
  19. self.data = 'a' * contents_size
  20. def GetPath(self):
  21. return 'entry_path'
  22. class FakeSection:
  23. """A fake Section object, used for testing
  24. This has the minimum feature set needed to support testing elf functions.
  25. A LookupSymbol() function is provided which returns a fake value for amu
  26. symbol requested.
  27. """
  28. def __init__(self, sym_value=1):
  29. self.sym_value = sym_value
  30. def GetPath(self):
  31. return 'section_path'
  32. def LookupSymbol(self, name, weak, msg):
  33. """Fake implementation which returns the same value for all symbols"""
  34. return self.sym_value
  35. class TestElf(unittest.TestCase):
  36. @classmethod
  37. def setUpClass(self):
  38. tools.SetInputDirs(['.'])
  39. def testAllSymbols(self):
  40. """Test that we can obtain a symbol from the ELF file"""
  41. fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
  42. syms = elf.GetSymbols(fname, [])
  43. self.assertIn('.ucode', syms)
  44. def testRegexSymbols(self):
  45. """Test that we can obtain from the ELF file by regular expression"""
  46. fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
  47. syms = elf.GetSymbols(fname, ['ucode'])
  48. self.assertIn('.ucode', syms)
  49. syms = elf.GetSymbols(fname, ['missing'])
  50. self.assertNotIn('.ucode', syms)
  51. syms = elf.GetSymbols(fname, ['missing', 'ucode'])
  52. self.assertIn('.ucode', syms)
  53. def testMissingFile(self):
  54. """Test that a missing file is detected"""
  55. entry = FakeEntry(10)
  56. section = FakeSection()
  57. with self.assertRaises(ValueError) as e:
  58. syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
  59. self.assertIn("Filename 'missing-file' not found in input path",
  60. str(e.exception))
  61. def testOutsideFile(self):
  62. """Test a symbol which extends outside the entry area is detected"""
  63. entry = FakeEntry(10)
  64. section = FakeSection()
  65. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
  66. with self.assertRaises(ValueError) as e:
  67. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  68. self.assertIn('entry_path has offset 4 (size 8) but the contents size '
  69. 'is a', str(e.exception))
  70. def testMissingImageStart(self):
  71. """Test that we detect a missing __image_copy_start symbol
  72. This is needed to mark the start of the image. Without it we cannot
  73. locate the offset of a binman symbol within the image.
  74. """
  75. entry = FakeEntry(10)
  76. section = FakeSection()
  77. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
  78. self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
  79. None)
  80. def testBadSymbolSize(self):
  81. """Test that an attempt to use an 8-bit symbol are detected
  82. Only 32 and 64 bits are supported, since we need to store an offset
  83. into the image.
  84. """
  85. entry = FakeEntry(10)
  86. section = FakeSection()
  87. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
  88. with self.assertRaises(ValueError) as e:
  89. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  90. self.assertIn('has size 1: only 4 and 8 are supported',
  91. str(e.exception))
  92. def testNoValue(self):
  93. """Test the case where we have no value for the symbol
  94. This should produce -1 values for all thress symbols, taking up the
  95. first 16 bytes of the image.
  96. """
  97. entry = FakeEntry(20)
  98. section = FakeSection(sym_value=None)
  99. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
  100. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  101. self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
  102. def testDebug(self):
  103. """Check that enabling debug in the elf module produced debug output"""
  104. elf.debug = True
  105. entry = FakeEntry(20)
  106. section = FakeSection()
  107. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
  108. with test_util.capture_sys_output() as (stdout, stderr):
  109. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  110. elf.debug = False
  111. self.assertTrue(len(stdout.getvalue()) > 0)
  112. if __name__ == '__main__':
  113. unittest.main()