|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import re |
| 6 | +import string |
| 7 | +from elf_helper import ElfHelper |
| 8 | +from elftools.elf.elffile import ELFFile |
| 9 | + |
| 10 | + |
| 11 | +# This script will create linker comands for power of two aligned MPU |
| 12 | +# when APP_SHARED_MEM is enabled. |
| 13 | +print_template = """ |
| 14 | +/* Auto generated code do not modify */ |
| 15 | +. = ALIGN( 1 << LOG2CEIL(data_smem_{0}b_end - data_smem_{0})); |
| 16 | +data_smem_{0} = .; |
| 17 | +KEEP(*(SORT(data_smem_{0}*))) |
| 18 | +. = ALIGN(_app_data_align); |
| 19 | +data_smem_{0}b_end = .; |
| 20 | +. = ALIGN( 1 << LOG2CEIL(data_smem_{0}b_end - data_smem_{0})); |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +def find_partitions(filename, full_list_of_partitions, partitions_source_file): |
| 25 | + with open(filename, 'rb') as f: |
| 26 | + full_lib = ELFFile( f) |
| 27 | + if (not full_lib): |
| 28 | + print("Error parsing file: ",filename) |
| 29 | + os.exit(1) |
| 30 | + |
| 31 | + sections = [ x for x in full_lib.iter_sections()] |
| 32 | + for section in sections: |
| 33 | + if ("smem" in section.name and not ".rel" in section.name): |
| 34 | + partition_name = section.name.split("data_smem_")[1] |
| 35 | + if partition_name not in full_list_of_partitions: |
| 36 | + full_list_of_partitions.append(partition_name) |
| 37 | + if args.verbose: |
| 38 | + partitions_source_file.update({partition_name: filename}) |
| 39 | + |
| 40 | + return( full_list_of_partitions, partitions_source_file) |
| 41 | + |
| 42 | +def cleanup_remove_bss_regions(full_list_of_partitions): |
| 43 | + for partition in full_list_of_partitions: |
| 44 | + if (partition+"b" in full_list_of_partitions): |
| 45 | + full_list_of_partitions.remove(partition+"b") |
| 46 | + return full_list_of_partitions |
| 47 | + |
| 48 | +def generate_final_linker(linker_file, full_list_of_partitions): |
| 49 | + string= '' |
| 50 | + for partition in full_list_of_partitions: |
| 51 | + string += print_template.format(partition) |
| 52 | + |
| 53 | + with open(linker_file, "w") as fw: |
| 54 | + fw.write(string) |
| 55 | + |
| 56 | + |
| 57 | +def parse_args(): |
| 58 | + global args |
| 59 | + parser = argparse.ArgumentParser( |
| 60 | + description=__doc__, |
| 61 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 62 | + parser.add_argument("-d", "--directory", required=True, |
| 63 | + help="Root build directory") |
| 64 | + parser.add_argument("-o", "--output", required=False, |
| 65 | + help="Output ld file") |
| 66 | + parser.add_argument("-v", "--verbose", action="count", default =0, |
| 67 | + help="Verbose Output") |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + |
| 71 | +def main(): |
| 72 | + parse_args() |
| 73 | + root_directory = args.directory |
| 74 | + linker_file = args.output |
| 75 | + full_list_of_partitions = [] |
| 76 | + partitions_source_file= {} |
| 77 | + |
| 78 | + for dirpath, dirs, files in os.walk(root_directory): |
| 79 | + for filename in files: |
| 80 | + if re.match(".*\.obj$",filename): |
| 81 | + fullname = os.path.join(dirpath, filename) |
| 82 | + full_list_of_partitions, partitions_source_file = find_partitions(fullname, full_list_of_partitions, partitions_source_file) |
| 83 | + |
| 84 | + full_list_of_partitions = cleanup_remove_bss_regions(full_list_of_partitions) |
| 85 | + generate_final_linker(linker_file, full_list_of_partitions) |
| 86 | + if args.verbose: |
| 87 | + print("Partitions retrieved: PARTITION, FILENAME") |
| 88 | + print([key + " "+ partitions_source_file[key] for key in full_list_of_partitions]) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
0 commit comments