check_alignment.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2020 Espressif Systems (Shanghai) PTE 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 argparse
  18. import re
  19. import subprocess
  20. from typing import Tuple
  21. argparser = argparse.ArgumentParser()
  22. argparser.add_argument('readelf')
  23. argparser.add_argument('elf')
  24. args = argparser.parse_args()
  25. # Get the content of the readelf command
  26. contents = subprocess.check_output([args.readelf, '-S', args.elf]).decode()
  27. # Define a class for readelf parsing error
  28. class ParsingError(Exception):
  29. pass
  30. # Look for the start address and size of any section
  31. def find_partition_info(sectionname): # type: (str) -> Tuple[int, int, int]
  32. match = re.search(sectionname + r'\s+PROGBITS\s+([a-f0-9]+) [a-f0-9]+ ([a-f0-9]+) \d+\s+[A-Z]+ 0 0 (\d+)',
  33. contents)
  34. if not match:
  35. raise ParsingError('ELF header parsing error')
  36. # Return the address of the section, the size and the alignment
  37. address = match.group(1)
  38. size = match.group(2)
  39. alignment = match.group(3)
  40. return (int(address, 16), int(size, 16), int(alignment, 10))
  41. # Get address and size for .flash.appdesc section
  42. app_address, app_size, app_align = find_partition_info('.flash.appdesc')
  43. # Same goes for .flash.rodata section
  44. rodata_address, _, rodata_align = find_partition_info('.flash.rodata')
  45. # Assert than everything is as expected:
  46. # appdesc is aligned on 16
  47. # rodata is aligned on 64
  48. # appdesc ends where rodata starts
  49. assert app_align == 16, '.flash.appdesc section should have been aligned on 16!'
  50. assert rodata_align == 64, '.flash.rodata section should have been aligned on 64!'
  51. assert app_address + app_size == rodata_address, ".flash.appdesc's end address and .flash.rodata's begin start must have no gap in between!"