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