check_alignment.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. import argparse
  7. import re
  8. import subprocess
  9. from typing import Tuple
  10. argparser = argparse.ArgumentParser()
  11. argparser.add_argument('readelf')
  12. argparser.add_argument('elf')
  13. args = argparser.parse_args()
  14. # Get the content of the readelf command
  15. contents = subprocess.check_output([args.readelf, '-S', args.elf]).decode()
  16. # Define a class for readelf parsing error
  17. class ParsingError(Exception):
  18. pass
  19. # Look for the start address and size of any section
  20. def find_partition_info(sectionname): # type: (str) -> Tuple[int, int, int]
  21. match = re.search(sectionname + r'\s+PROGBITS\s+([a-f0-9]+) [a-f0-9]+ ([a-f0-9]+) \d+\s+[A-Z]+ 0 0 (\d+)',
  22. contents)
  23. if not match:
  24. raise ParsingError('ELF header parsing error')
  25. # Return the address of the section, the size and the alignment
  26. address = match.group(1)
  27. size = match.group(2)
  28. alignment = match.group(3)
  29. return (int(address, 16), int(size, 16), int(alignment, 10))
  30. # Get address and size for .flash.appdesc section
  31. app_address, app_size, app_align = find_partition_info('.flash.appdesc')
  32. # Same goes for .flash.rodata section
  33. rodata_address, _, rodata_align = find_partition_info('.flash.rodata')
  34. # Assert than everything is as expected:
  35. # appdesc is aligned on 16
  36. # rodata is aligned on 64
  37. # appdesc ends where rodata starts
  38. assert app_align == 16, '.flash.appdesc section should have been aligned on 16!'
  39. assert rodata_align == 64, '.flash.rodata section should have been aligned on 64!'
  40. assert app_address + app_size == rodata_address, ".flash.appdesc's end address and .flash.rodata's begin start must have no gap in between!"