esp32ulp_mapgen.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # esp32ulp_mapgen utility converts a symbol list provided by nm into an export script
  3. # for the linker and a header file.
  4. #
  5. # Copyright (c) 2016-2017 Espressif Systems (Shanghai) PTE LTD.
  6. # Distributed under the terms of Apache License v2.0 found in the top-level LICENSE file.
  7. from __future__ import print_function
  8. from optparse import OptionParser
  9. BASE_ADDR = 0x50000000
  10. def gen_ld_h_from_sym(f_sym, f_ld, f_h):
  11. f_ld.write('/* Variable definitions for ESP32ULP linker\n')
  12. f_ld.write(' * This file is generated automatically by esp32ulp_mapgen.py utility.\n')
  13. f_ld.write(' */\n\n')
  14. f_h.write('// Variable definitions for ESP32ULP\n')
  15. f_h.write('// This file is generated automatically by esp32ulp_mapgen.py utility\n\n')
  16. f_h.write('#pragma once\n\n')
  17. for line in f_sym:
  18. name, _, addr_str = line.split(' ', 2)
  19. addr = int(addr_str, 16) + BASE_ADDR
  20. f_h.write('extern uint32_t ulp_{0};\n'.format(name))
  21. f_ld.write('PROVIDE ( ulp_{0} = 0x{1:08x} );\n'.format(name, addr))
  22. def gen_ld_h_from_sym_riscv(f_sym, f_ld, f_h):
  23. f_ld.write('/* Variable definitions for ESP32ULP linker\n')
  24. f_ld.write(' * This file is generated automatically by esp32ulp_mapgen.py utility.\n')
  25. f_ld.write(' */\n\n')
  26. f_h.write('// Variable definitions for ESP32ULP\n')
  27. f_h.write('// This file is generated automatically by esp32ulp_mapgen.py utility\n\n')
  28. f_h.write('#pragma once\n\n')
  29. for line in f_sym:
  30. addr_str, _, name = line.split()
  31. addr = int(addr_str, 16) + BASE_ADDR
  32. f_h.write('extern uint32_t ulp_{0};\n'.format(name))
  33. f_ld.write('PROVIDE ( ulp_{0} = 0x{1:08x} );\n'.format(name, addr))
  34. def main():
  35. description = ('This application generates .h and .ld files for symbols defined in input file. '
  36. 'The input symbols file can be generated using nm utility like this: '
  37. 'esp32-ulp-nm -g -f posix <elf_file> > <symbols_file>')
  38. parser = OptionParser(description=description)
  39. parser.add_option('-s', '--symfile', dest='symfile',
  40. help='symbols file name', metavar='SYMFILE')
  41. parser.add_option('-o', '--outputfile', dest='outputfile',
  42. help='destination .h and .ld files name prefix', metavar='OUTFILE')
  43. parser.add_option('--riscv', action='store_true', help='use format for ulp riscv .sym file')
  44. (options, args) = parser.parse_args()
  45. if options.symfile is None:
  46. parser.print_help()
  47. return 1
  48. if options.outputfile is None:
  49. parser.print_help()
  50. return 1
  51. if options.riscv:
  52. with open(options.outputfile + '.h', 'w') as f_h, open(options.outputfile + '.ld', 'w') as f_ld, open(options.symfile) as f_sym:
  53. gen_ld_h_from_sym_riscv(f_sym, f_ld, f_h)
  54. return 0
  55. with open(options.outputfile + '.h', 'w') as f_h, open(options.outputfile + '.ld', 'w') as f_ld, open(options.symfile) as f_sym:
  56. gen_ld_h_from_sym(f_sym, f_ld, f_h)
  57. return 0
  58. if __name__ == '__main__':
  59. exit(main())