test_output_commands.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2021 Espressif Systems (Shanghai) CO LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import sys
  18. import unittest
  19. try:
  20. from output_commands import AlignAtAddress, InputSectionDesc, SymbolAtAddress
  21. except ImportError:
  22. sys.path.append('../')
  23. from output_commands import InputSectionDesc, SymbolAtAddress, AlignAtAddress
  24. from entity import Entity
  25. SECTIONS = ['.text', '.text.*', '.literal', '.literal.*']
  26. FREERTOS = Entity('libfreertos.a')
  27. CROUTINE = Entity('libfreertos.a', 'croutine')
  28. class InputSectionDescTest(unittest.TestCase):
  29. def test_catch_all_placement(self):
  30. # Test default (catch-all) command
  31. expected = '*(.literal .literal.* .text .text.*)'
  32. desc = InputSectionDesc(Entity(), SECTIONS)
  33. self.assertEqual(expected, str(desc))
  34. desc = InputSectionDesc(Entity(Entity.ALL), SECTIONS)
  35. self.assertEqual(expected, str(desc))
  36. def test_lib_placement(self):
  37. # Test library placement command
  38. expected = '*libfreertos.a:(.literal .literal.* .text .text.*)'
  39. desc = InputSectionDesc(Entity('libfreertos.a'), SECTIONS)
  40. self.assertEqual(expected, str(desc))
  41. desc = InputSectionDesc(Entity('libfreertos.a', Entity.ALL), SECTIONS)
  42. self.assertEqual(expected, str(desc))
  43. desc = InputSectionDesc(Entity('libfreertos.a', None, Entity.ALL), SECTIONS)
  44. self.assertEqual(expected, str(desc))
  45. desc = InputSectionDesc(Entity('libfreertos.a', Entity.ALL, Entity.ALL), SECTIONS)
  46. self.assertEqual(expected, str(desc))
  47. def test_obj_placement(self):
  48. # Test object placement command
  49. expected = '*libfreertos.a:croutine.*(.literal .literal.* .text .text.*)'
  50. desc = InputSectionDesc(Entity('libfreertos.a', 'croutine'), SECTIONS)
  51. self.assertEqual(expected, str(desc))
  52. desc = InputSectionDesc(Entity('libfreertos.a', 'croutine'), SECTIONS)
  53. self.assertEqual(expected, str(desc))
  54. desc = InputSectionDesc(Entity('libfreertos.a', 'croutine', Entity.ALL), SECTIONS)
  55. self.assertEqual(expected, str(desc))
  56. # Disambugated placement
  57. expected = '*libfreertos.a:croutine.c.*(.literal .literal.* .text .text.*)'
  58. desc = InputSectionDesc(Entity('libfreertos.a', 'croutine.c'), SECTIONS)
  59. self.assertEqual(expected, str(desc))
  60. def test_invalid_entity(self):
  61. # Invalid entity specification
  62. with self.assertRaises(AssertionError):
  63. InputSectionDesc(Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList'), SECTIONS)
  64. with self.assertRaises(AssertionError):
  65. InputSectionDesc(Entity('libfreertos.a', 'croutine'), SECTIONS, [Entity()])
  66. with self.assertRaises(AssertionError):
  67. InputSectionDesc(Entity('libfreertos.a', 'croutine'), SECTIONS, [Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList')])
  68. def test_exclusions(self):
  69. # Test exclusions
  70. # Library
  71. expected = ('*libfreertos.a:croutine.*'
  72. '(EXCLUDE_FILE(*libfreertos.a) '
  73. '.literal EXCLUDE_FILE(*libfreertos.a) '
  74. '.literal.* EXCLUDE_FILE(*libfreertos.a) '
  75. '.text EXCLUDE_FILE(*libfreertos.a) .text.*)')
  76. desc = InputSectionDesc(CROUTINE, SECTIONS, [FREERTOS])
  77. self.assertEqual(expected, str(desc))
  78. # Object
  79. expected = ('*libfreertos.a:croutine.*'
  80. '(EXCLUDE_FILE(*libfreertos.a:croutine.*) '
  81. '.literal EXCLUDE_FILE(*libfreertos.a:croutine.*) '
  82. '.literal.* EXCLUDE_FILE(*libfreertos.a:croutine.*) '
  83. '.text EXCLUDE_FILE(*libfreertos.a:croutine.*) .text.*)')
  84. desc = InputSectionDesc(CROUTINE, SECTIONS, [CROUTINE])
  85. self.assertEqual(expected, str(desc))
  86. # Multiple exclusions
  87. expected = ('*libfreertos.a:croutine.*'
  88. '(EXCLUDE_FILE(*libfreertos.a *libfreertos.a:croutine.*) '
  89. '.literal EXCLUDE_FILE(*libfreertos.a *libfreertos.a:croutine.*) '
  90. '.literal.* EXCLUDE_FILE(*libfreertos.a *libfreertos.a:croutine.*) '
  91. '.text EXCLUDE_FILE(*libfreertos.a *libfreertos.a:croutine.*) .text.*)')
  92. desc = InputSectionDesc(CROUTINE, SECTIONS, [FREERTOS, CROUTINE])
  93. self.assertEqual(expected, str(desc))
  94. # Disambugated exclusion
  95. expected = ('*libfreertos.a:croutine.*'
  96. '(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) '
  97. '.literal EXCLUDE_FILE(*libfreertos.a:croutine.c.*) '
  98. '.literal.* EXCLUDE_FILE(*libfreertos.a:croutine.c.*) '
  99. '.text EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*)')
  100. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')])
  101. self.assertEqual(expected, str(desc))
  102. def test_empty_sections(self):
  103. # Test empty sections
  104. expected = '*libfreertos.a:croutine.*( )'
  105. desc = InputSectionDesc(Entity('libfreertos.a', 'croutine'), [])
  106. self.assertEqual(expected, str(desc))
  107. def test_keep(self):
  108. # Test KEEP
  109. expected = 'KEEP(*libfreertos.a:croutine.*( ))'
  110. desc = InputSectionDesc(Entity('libfreertos.a', 'croutine'), [], keep=True)
  111. self.assertEqual(expected, str(desc))
  112. def test_sort(self):
  113. # Test sort
  114. expected = ('*libfreertos.a:croutine.*('
  115. 'SORT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal) '
  116. 'SORT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*) '
  117. 'SORT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text) '
  118. 'SORT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*)'
  119. ')')
  120. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=(None, None))
  121. self.assertEqual(expected, str(desc))
  122. expected = ('*libfreertos.a:croutine.*('
  123. 'SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal) '
  124. 'SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*) '
  125. 'SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text) '
  126. 'SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*)'
  127. ')')
  128. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('name', None))
  129. self.assertEqual(expected, str(desc))
  130. expected = ('*libfreertos.a:croutine.*('
  131. 'SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal) '
  132. 'SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*) '
  133. 'SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text) '
  134. 'SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*)'
  135. ')')
  136. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('alignment', None))
  137. self.assertEqual(expected, str(desc))
  138. expected = ('*libfreertos.a:croutine.*('
  139. 'SORT_BY_INIT_PRIORITY(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal) '
  140. 'SORT_BY_INIT_PRIORITY(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*) '
  141. 'SORT_BY_INIT_PRIORITY(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text) '
  142. 'SORT_BY_INIT_PRIORITY(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*)'
  143. ')')
  144. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('init_priority', None))
  145. self.assertEqual(expected, str(desc))
  146. expected = ('*libfreertos.a:croutine.*('
  147. 'SORT_BY_NAME(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal)) '
  148. 'SORT_BY_NAME(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*)) '
  149. 'SORT_BY_NAME(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text)) '
  150. 'SORT_BY_NAME(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*))'
  151. ')')
  152. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('name', 'alignment'))
  153. self.assertEqual(expected, str(desc))
  154. expected = ('*libfreertos.a:croutine.*('
  155. 'SORT_BY_NAME(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal)) '
  156. 'SORT_BY_NAME(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*)) '
  157. 'SORT_BY_NAME(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text)) '
  158. 'SORT_BY_NAME(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*))'
  159. ')')
  160. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('name', 'name'))
  161. self.assertEqual(expected, str(desc))
  162. expected = ('*libfreertos.a:croutine.*('
  163. 'SORT_BY_ALIGNMENT(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal)) '
  164. 'SORT_BY_ALIGNMENT(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*)) '
  165. 'SORT_BY_ALIGNMENT(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text)) '
  166. 'SORT_BY_ALIGNMENT(SORT_BY_NAME(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*))'
  167. ')')
  168. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('alignment', 'name'))
  169. self.assertEqual(expected, str(desc))
  170. expected = ('*libfreertos.a:croutine.*('
  171. 'SORT_BY_ALIGNMENT(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal)) '
  172. 'SORT_BY_ALIGNMENT(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .literal.*)) '
  173. 'SORT_BY_ALIGNMENT(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text)) '
  174. 'SORT_BY_ALIGNMENT(SORT_BY_ALIGNMENT(EXCLUDE_FILE(*libfreertos.a:croutine.c.*) .text.*))'
  175. ')')
  176. desc = InputSectionDesc(CROUTINE, SECTIONS, [Entity('libfreertos.a', 'croutine.c')], keep=False, sort=('alignment', 'alignment'))
  177. self.assertEqual(expected, str(desc))
  178. class SymbolAtAddressTest(unittest.TestCase):
  179. def test_symbol(self):
  180. symbol = 'test_symbol'
  181. expected = '%s = ABSOLUTE(.);' % symbol
  182. desc = SymbolAtAddress(symbol)
  183. self.assertEqual(expected, str(desc))
  184. class AlignAtAddressTest(unittest.TestCase):
  185. def test_align(self):
  186. align = 8
  187. expected = '. = ALIGN(%d);' % 8
  188. desc = AlignAtAddress(align)
  189. self.assertEqual(expected, str(desc))
  190. if __name__ == '__main__':
  191. unittest.main()