Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ioannis Glaropoulos | 560357b | 2018-11-23 09:39:53 +0100 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2018 Intel Corporation |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 7 | import sys |
| 8 | import argparse |
| 9 | import os |
| 10 | import re |
| 11 | import string |
| 12 | from elf_helper import ElfHelper |
| 13 | from 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. |
| 18 | print_template = """ |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 19 | /* Auto generated code do not modify */ |
Ioannis Glaropoulos | 62c5894 | 2018-11-22 14:55:41 +0100 | [diff] [blame] | 20 | MPU_ALIGN(data_smem_{0}b_end - data_smem_{0}); |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 21 | data_smem_{0} = .; |
| 22 | KEEP(*(SORT(data_smem_{0}*))) |
Ioannis Glaropoulos | 62c5894 | 2018-11-22 14:55:41 +0100 | [diff] [blame] | 23 | MPU_ALIGN(data_smem_{0}b_end - data_smem_{0}); |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 24 | data_smem_{0}b_end = .; |
| 25 | """ |
| 26 | linker_start_seq = """ |
| 27 | SECTION_PROLOGUE(_APP_SMEM_SECTION_NAME, (OPTIONAL),) |
| 28 | { |
Adithya Baglody | 10c6a0c | 2018-09-21 10:17:58 +0530 | [diff] [blame] | 29 | APP_SHARED_ALIGN; |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 30 | _app_smem_start = .; |
| 31 | """ |
| 32 | |
| 33 | linker_end_seq = """ |
| 34 | _app_smem_end = .; |
Adithya Baglody | 10c6a0c | 2018-09-21 10:17:58 +0530 | [diff] [blame] | 35 | APP_SHARED_ALIGN; |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 36 | } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) |
| 37 | """ |
| 38 | |
| 39 | size_cal_string = """ |
| 40 | data_smem_{0}_size = data_smem_{0}b_end - data_smem_{0}; |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 41 | """ |
| 42 | |
| 43 | |
| 44 | def 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 | |
| 62 | def 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 | |
| 68 | def generate_final_linker(linker_file, full_list_of_partitions): |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 69 | string = linker_start_seq |
| 70 | size_string = '' |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 71 | for partition in full_list_of_partitions: |
| 72 | string += print_template.format(partition) |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 73 | size_string += size_cal_string.format(partition) |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 74 | |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 75 | string += linker_end_seq |
| 76 | string += size_string |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 77 | with open(linker_file, "w") as fw: |
| 78 | fw.write(string) |
| 79 | |
| 80 | |
| 81 | def 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 | |
| 95 | def 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 | |
| 114 | if __name__ == '__main__': |
| 115 | main() |