88"""
99This script will relocate .text, .rodata, .data and .bss sections from required files
1010and places it in the required memory region. This memory region and file
11- are given to this python script in the form of a string.
11+ are given to this python script in the form of a file.
12+ A regular expression filter can be applied to select only the required sections from the file.
1213
13- Example of such a string would be::
14+ Example of content in such an input file would be::
1415
15- SRAM2:COPY:/home/xyz/zephyr/samples/hello_world/src/main.c,\
16- SRAM1:COPY:/home/xyz/zephyr/samples/hello_world/src/main2.c, \
17- FLASH2:NOCOPY:/home/xyz/zephyr/samples/hello_world/src/main3.c
16+ SRAM2:COPY:/home/xyz/zephyr/samples/hello_world/src/main.c,.*foo|.*bar
17+ SRAM1:COPY:/home/xyz/zephyr/samples/hello_world/src/main2.c,.*bar
18+ FLASH2:NOCOPY:/home/xyz/zephyr/samples/hello_world/src/main3.c,
1819
1920One can also specify the program header for a given memory region:
2021
21- SRAM2\\ :phdr0:COPY:/home/xyz/zephyr/samples/hello_world/src/main.c
22+ SRAM2\\ :phdr0:COPY:/home/xyz/zephyr/samples/hello_world/src/main.c,
2223
2324To invoke this script::
2425
25- python3 gen_relocate_app.py -i input_string -o generated_linker -c generated_code
26+ python3 gen_relocate_app.py -i input_file -o generated_linker -c generated_code
2627
2728Configuration that needs to be sent to the python script.
2829
4546import argparse
4647import os
4748import glob
49+ import re
4850import warnings
4951from collections import defaultdict
5052from enum import Enum
@@ -225,7 +227,7 @@ def region_is_default_ram(region_name: str) -> bool:
225227 return region_name == args .default_ram_region
226228
227229
228- def find_sections (filename : str ) -> 'dict[SectionKind, list[OutputSection]]' :
230+ def find_sections (filename : str , symbol_filter : str ) -> 'dict[SectionKind, list[OutputSection]]' :
229231 """
230232 Locate relocatable sections in the given object file.
231233
@@ -243,6 +245,9 @@ def find_sections(filename: str) -> 'dict[SectionKind, list[OutputSection]]':
243245 out = defaultdict (list )
244246
245247 for section in sections :
248+ if not re .search (symbol_filter , section .name ):
249+ # Section is filtered-out
250+ continue
246251 section_kind = SectionKind .for_section_named (section .name )
247252 if section_kind is None :
248253 continue
@@ -501,9 +506,10 @@ def get_obj_filename(searchpath, filename):
501506
502507
503508# Extracts all possible components for the input string:
504- # <mem_region>[\ :program_header]:<flag_1>[;<flag_2>...]:<file_1>[;<file_2>...]
505- # Returns a 4 -tuple with them: (mem_region, program_header, flags, files)
509+ # <mem_region>[\ :program_header]:<flag_1>[;<flag_2>...]:<file_1>[;<file_2>...][,filter]
510+ # Returns a 5 -tuple with them: (mem_region, program_header, flags, files, filter )
506511# If no `program_header` is defined, returns an empty string
512+ # If no `filter` is defined, returns an empty string
507513def parse_input_string (line ):
508514 # Be careful when splitting by : to avoid breaking absolute paths on Windows
509515 mem_region , rest = line .split (':' , 1 )
@@ -513,13 +519,19 @@ def parse_input_string(line):
513519 mem_region = mem_region .rstrip ()
514520 phdr , rest = rest .split (':' , 1 )
515521
516- # Split lists by semicolons, in part to support generator expressions
517- flag_list , file_list = (lst .split (';' ) for lst in rest .split (':' , 1 ))
522+ flag_list , rest = rest .split (':' , 1 )
523+ flag_list = flag_list .split (';' )
524+
525+
526+ # Split file list by semicolons, in part to support generator expressions
527+ file_list , symbol_filter = rest .split (',' , 1 )
528+ file_list = file_list .split (';' )
518529
519- return mem_region , phdr , flag_list , file_list
530+ return mem_region , phdr , flag_list , file_list , symbol_filter
520531
521532
522- # Create a dict with key as memory type and files as a list of values.
533+ # Create a dict with key as memory type and (files, symbol_filter) tuple
534+ # as a list of values.
523535# Also, return another dict with program headers for memory regions
524536def create_dict_wrt_mem ():
525537 # need to support wild card *
@@ -529,11 +541,12 @@ def create_dict_wrt_mem():
529541 input_rel_dict = args .input_rel_dict .read ().splitlines ()
530542 if not input_rel_dict :
531543 sys .exit ("Disable CONFIG_CODE_DATA_RELOCATION if no file needs relocation" )
544+
532545 for line in input_rel_dict :
533546 if ':' not in line :
534547 continue
535548
536- mem_region , phdr , flag_list , file_list = parse_input_string (line )
549+ mem_region , phdr , flag_list , file_list , symbol_filter = parse_input_string (line )
537550
538551 # Handle any program header
539552 if phdr != '' :
@@ -556,12 +569,15 @@ def create_dict_wrt_mem():
556569 if args .verbose :
557570 print ("Memory region " , mem_region , " Selected for files:" , file_name_list )
558571
572+ # Apply filter on files
573+ file_name_filter_list = [(f , symbol_filter ) for f in file_name_list ]
574+
559575 mem_region = "|" .join ((mem_region , * flag_list ))
560576
561577 if mem_region in rel_dict :
562- rel_dict [mem_region ].extend (file_name_list )
578+ rel_dict [mem_region ].extend (file_name_filter_list )
563579 else :
564- rel_dict [mem_region ] = file_name_list
580+ rel_dict [mem_region ] = file_name_filter_list
565581
566582 return rel_dict , phdrs
567583
@@ -585,13 +601,13 @@ def main():
585601 for memory_type , files in rel_dict .items ():
586602 full_list_of_sections : 'dict[SectionKind, list[OutputSection]]' = defaultdict (list )
587603
588- for filename in files :
604+ for filename , symbol_filter in files :
589605 obj_filename = get_obj_filename (searchpath , filename )
590606 # the obj file wasn't found. Probably not compiled.
591607 if not obj_filename :
592608 continue
593609
594- file_sections = find_sections (obj_filename )
610+ file_sections = find_sections (obj_filename , symbol_filter )
595611 # Merge sections from file into collection of sections for all files
596612 for category , sections in file_sections .items ():
597613 full_list_of_sections [category ].extend (sections )
0 commit comments