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