blob: b8b407c5a296424d7b6cfee5fdcd607babd80e15 [file] [log] [blame]
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +05301#!/usr/bin/env python3
Ioannis Glaropoulos560357b2018-11-23 09:39:53 +01002#
3# Copyright (c) 2018 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +05307import sys
8import argparse
9import os
10import re
11import string
12from elf_helper import ElfHelper
13from elftools.elf.elffile import ELFFile
14
15
16# This script will create linker comands for power of two aligned MPU
17# when APP_SHARED_MEM is enabled.
18print_template = """
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053019 /* Auto generated code do not modify */
Ioannis Glaropoulos62c58942018-11-22 14:55:41 +010020 MPU_ALIGN(data_smem_{0}b_end - data_smem_{0});
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053021 data_smem_{0} = .;
22 KEEP(*(SORT(data_smem_{0}*)))
Ioannis Glaropoulos62c58942018-11-22 14:55:41 +010023 MPU_ALIGN(data_smem_{0}b_end - data_smem_{0});
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053024 data_smem_{0}b_end = .;
25"""
26linker_start_seq = """
27 SECTION_PROLOGUE(_APP_SMEM_SECTION_NAME, (OPTIONAL),)
28 {
Adithya Baglody10c6a0c2018-09-21 10:17:58 +053029 APP_SHARED_ALIGN;
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053030 _app_smem_start = .;
31"""
32
33linker_end_seq = """
34 _app_smem_end = .;
Adithya Baglody10c6a0c2018-09-21 10:17:58 +053035 APP_SHARED_ALIGN;
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053036 } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
37"""
38
39size_cal_string = """
40 data_smem_{0}_size = data_smem_{0}b_end - data_smem_{0};
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +053041"""
42
43
44def find_partitions(filename, full_list_of_partitions, partitions_source_file):
45 with open(filename, 'rb') as f:
46 full_lib = ELFFile( f)
47 if (not full_lib):
48 print("Error parsing file: ",filename)
49 os.exit(1)
50
51 sections = [ x for x in full_lib.iter_sections()]
52 for section in sections:
53 if ("smem" in section.name and not ".rel" in section.name):
54 partition_name = section.name.split("data_smem_")[1]
55 if partition_name not in full_list_of_partitions:
56 full_list_of_partitions.append(partition_name)
57 if args.verbose:
58 partitions_source_file.update({partition_name: filename})
59
60 return( full_list_of_partitions, partitions_source_file)
61
62def cleanup_remove_bss_regions(full_list_of_partitions):
63 for partition in full_list_of_partitions:
64 if (partition+"b" in full_list_of_partitions):
65 full_list_of_partitions.remove(partition+"b")
66 return full_list_of_partitions
67
68def generate_final_linker(linker_file, full_list_of_partitions):
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053069 string = linker_start_seq
70 size_string = ''
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +053071 for partition in full_list_of_partitions:
72 string += print_template.format(partition)
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053073 size_string += size_cal_string.format(partition)
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +053074
Adithya Baglodyae92f2b2018-09-19 11:20:09 +053075 string += linker_end_seq
76 string += size_string
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +053077 with open(linker_file, "w") as fw:
78 fw.write(string)
79
80
81def parse_args():
82 global args
83 parser = argparse.ArgumentParser(
84 description=__doc__,
85 formatter_class=argparse.RawDescriptionHelpFormatter)
86 parser.add_argument("-d", "--directory", required=True,
87 help="Root build directory")
88 parser.add_argument("-o", "--output", required=False,
89 help="Output ld file")
90 parser.add_argument("-v", "--verbose", action="count", default =0,
91 help="Verbose Output")
92 args = parser.parse_args()
93
94
95def main():
96 parse_args()
97 root_directory = args.directory
98 linker_file = args.output
99 full_list_of_partitions = []
100 partitions_source_file= {}
101
102 for dirpath, dirs, files in os.walk(root_directory):
103 for filename in files:
104 if re.match(".*\.obj$",filename):
105 fullname = os.path.join(dirpath, filename)
106 full_list_of_partitions, partitions_source_file = find_partitions(fullname, full_list_of_partitions, partitions_source_file)
107
108 full_list_of_partitions = cleanup_remove_bss_regions(full_list_of_partitions)
109 generate_final_linker(linker_file, full_list_of_partitions)
110 if args.verbose:
111 print("Partitions retrieved: PARTITION, FILENAME")
112 print([key + " "+ partitions_source_file[key] for key in full_list_of_partitions])
113
114if __name__ == '__main__':
115 main()