elf_test.py 4.7 KB

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