esp32ulp_mapgen.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. import sys
  10. BASE_ADDR = 0x50000000
  11. def gen_ld_h_from_sym(f_sym, f_ld, f_h):
  12. f_ld.write("/* Variable definitions for ESP32ULP linker\n")
  13. f_ld.write(" * This file is generated automatically by esp32ulp_mapgen.py utility.\n")
  14. f_ld.write(" */\n\n")
  15. f_h.write("// Variable definitions for ESP32ULP\n")
  16. f_h.write("// This file is generated automatically by esp32ulp_mapgen.py utility\n\n")
  17. f_h.write("#pragma once\n\n")
  18. for line in f_sym:
  19. name, _, addr_str = line.split(" ", 2)
  20. addr = int(addr_str, 16) + BASE_ADDR
  21. f_h.write("extern uint32_t ulp_{0};\n".format(name))
  22. f_ld.write("PROVIDE ( ulp_{0} = 0x{1:08x} );\n".format(name, addr))
  23. def gen_ld_h_from_sym_riscv(f_sym, f_ld, f_h):
  24. f_ld.write("/* Variable definitions for ESP32ULP linker\n")
  25. f_ld.write(" * This file is generated automatically by esp32ulp_mapgen.py utility.\n")
  26. f_ld.write(" */\n\n")
  27. f_h.write("// Variable definitions for ESP32ULP\n")
  28. f_h.write("// This file is generated automatically by esp32ulp_mapgen.py utility\n\n")
  29. f_h.write("#pragma once\n\n")
  30. for line in f_sym:
  31. addr_str, _, name = line.split()
  32. addr = int(addr_str, 16) + BASE_ADDR
  33. f_h.write("extern uint32_t ulp_{0};\n".format(name))
  34. f_ld.write("PROVIDE ( ulp_{0} = 0x{1:08x} );\n".format(name, addr))
  35. def main():
  36. if sys.version_info[0] < 3:
  37. print("WARNING: Support for Python 2 is deprecated and will be removed in future versions.", file=sys.stderr)
  38. elif sys.version_info[0] == 3 and sys.version_info[1] < 6:
  39. print("WARNING: Python 3 versions older than 3.6 are not supported.", file=sys.stderr)
  40. description = ("This application generates .h and .ld files for symbols defined in input file. "
  41. "The input symbols file can be generated using nm utility like this: "
  42. "esp32-ulp-nm -g -f posix <elf_file> > <symbols_file>")
  43. parser = OptionParser(description=description)
  44. parser.add_option("-s", "--symfile", dest="symfile",
  45. help="symbols file name", metavar="SYMFILE")
  46. parser.add_option("-o", "--outputfile", dest="outputfile",
  47. help="destination .h and .ld files name prefix", metavar="OUTFILE")
  48. parser.add_option("--riscv", action="store_true", help="use format for ulp riscv .sym file")
  49. (options, args) = parser.parse_args()
  50. if options.symfile is None:
  51. parser.print_help()
  52. return 1
  53. if options.outputfile is None:
  54. parser.print_help()
  55. return 1
  56. if options.riscv:
  57. with open(options.outputfile + ".h", 'w') as f_h, open(options.outputfile + ".ld", 'w') as f_ld, open(options.symfile) as f_sym:
  58. gen_ld_h_from_sym_riscv(f_sym, f_ld, f_h)
  59. return 0
  60. with open(options.outputfile + ".h", 'w') as f_h, open(options.outputfile + ".ld", 'w') as f_ld, open(options.symfile) as f_sym:
  61. gen_ld_h_from_sym(f_sym, f_ld, f_h)
  62. return 0
  63. if __name__ == "__main__":
  64. exit(main())