Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2018 Intel Corporation. |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | # |
| 7 | |
David B. Kinder | 17299f0 | 2019-05-31 15:39:39 -0700 | [diff] [blame] | 8 | """ |
| 9 | This script will relocate .text, .rodata, .data and .bss sections from required files |
| 10 | and 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. |
| 12 | |
| 13 | Example of such a string would be:: |
| 14 | |
| 15 | SRAM2:/home/xyz/zephyr/samples/hello_world/src/main.c,\ |
| 16 | SRAM1:/home/xyz/zephyr/samples/hello_world/src/main2.c |
| 17 | |
| 18 | To invoke this script:: |
| 19 | |
| 20 | python3 gen_relocate_app.py -i input_string -o generated_linker -c generated_code |
| 21 | |
| 22 | Configuration that needs to be sent to the python script. |
| 23 | |
| 24 | - If the memory is like SRAM1/SRAM2/CCD/AON then place full object in |
| 25 | the sections |
| 26 | - If the memory type is appended with _DATA / _TEXT/ _RODATA/ _BSS only the |
| 27 | selected memory is placed in the required memory region. Others are |
| 28 | ignored. |
| 29 | |
| 30 | Multiple regions can be appended together like SRAM2_DATA_BSS |
| 31 | this will place data and bss inside SRAM2. |
| 32 | """ |
| 33 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 34 | |
| 35 | import sys |
| 36 | import argparse |
| 37 | import os |
| 38 | import glob |
| 39 | import warnings |
| 40 | from elftools.elf.elffile import ELFFile |
| 41 | |
| 42 | # This script will create linker comands for text,rodata data, bss section relocation |
| 43 | |
| 44 | PRINT_TEMPLATE = """ |
| 45 | KEEP(*({0})) |
| 46 | """ |
| 47 | |
| 48 | SECTION_LOAD_MEMORY_SEQ = """ |
| 49 | __{0}_{1}_rom_start = LOADADDR(_{2}_{3}_SECTION_NAME); |
| 50 | """ |
| 51 | |
Wentong Wu | c8f43b9 | 2019-05-14 01:04:06 +0800 | [diff] [blame] | 52 | LOAD_ADDRESS_LOCATION_FLASH = """ |
| 53 | #ifdef CONFIG_XIP |
| 54 | GROUP_DATA_LINK_IN({0}, FLASH) |
| 55 | #else |
| 56 | GROUP_DATA_LINK_IN({0}, {0}) |
| 57 | #endif |
| 58 | """ |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 59 | LOAD_ADDRESS_LOCATION_BSS = "GROUP_LINK_IN({0})" |
| 60 | |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 61 | MPU_RO_REGION_START = """ |
| 62 | |
Jordan Yates | c6cc927 | 2021-04-16 20:38:44 +1000 | [diff] [blame] | 63 | _{0}_mpu_ro_region_start = ORIGIN({1}); |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 64 | |
| 65 | """ |
| 66 | |
| 67 | MPU_RO_REGION_END = """ |
| 68 | |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 69 | _{0}_mpu_ro_region_end = .; |
| 70 | |
| 71 | """ |
| 72 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 73 | # generic section creation format |
| 74 | LINKER_SECTION_SEQ = """ |
| 75 | |
| 76 | /* Linker section for memory region {2} for {3} section */ |
| 77 | |
Kumar Gala | 4da0f8b | 2019-03-14 18:39:36 -0500 | [diff] [blame] | 78 | SECTION_PROLOGUE(_{2}_{3}_SECTION_NAME,,) |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 79 | {{ |
| 80 | . = ALIGN(4); |
| 81 | {4} |
| 82 | . = ALIGN(4); |
| 83 | }} {5} |
| 84 | __{0}_{1}_end = .; |
| 85 | __{0}_{1}_start = ADDR(_{2}_{3}_SECTION_NAME); |
| 86 | __{0}_{1}_size = SIZEOF(_{2}_{3}_SECTION_NAME); |
| 87 | """ |
| 88 | |
Wentong Wu | fa7ce3f | 2019-07-02 01:18:40 +0800 | [diff] [blame] | 89 | LINKER_SECTION_SEQ_MPU = """ |
| 90 | |
| 91 | /* Linker section for memory region {2} for {3} section */ |
| 92 | |
| 93 | SECTION_PROLOGUE(_{2}_{3}_SECTION_NAME,,) |
| 94 | {{ |
| 95 | __{0}_{1}_start = .; |
| 96 | {4} |
Wentong Wu | 09af98a | 2019-09-19 21:40:29 +0800 | [diff] [blame] | 97 | #if {6} |
| 98 | . = ALIGN({6}); |
| 99 | #else |
Wentong Wu | fa7ce3f | 2019-07-02 01:18:40 +0800 | [diff] [blame] | 100 | MPU_ALIGN(__{0}_{1}_size); |
Wentong Wu | 09af98a | 2019-09-19 21:40:29 +0800 | [diff] [blame] | 101 | #endif |
Wentong Wu | fa7ce3f | 2019-07-02 01:18:40 +0800 | [diff] [blame] | 102 | __{0}_{1}_end = .; |
| 103 | }} {5} |
| 104 | __{0}_{1}_size = __{0}_{1}_end - __{0}_{1}_start; |
| 105 | """ |
| 106 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 107 | SOURCE_CODE_INCLUDES = """ |
| 108 | /* Auto generated code. Do not modify.*/ |
| 109 | #include <zephyr.h> |
| 110 | #include <linker/linker-defs.h> |
| 111 | #include <kernel_structs.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 112 | #include <string.h> |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 113 | """ |
| 114 | |
| 115 | EXTERN_LINKER_VAR_DECLARATION = """ |
| 116 | extern char __{0}_{1}_start[]; |
| 117 | extern char __{0}_{1}_rom_start[]; |
Adithya Baglody | 65f79cd | 2018-12-17 14:21:34 +0530 | [diff] [blame] | 118 | extern char __{0}_{1}_size[]; |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 119 | """ |
| 120 | |
| 121 | |
| 122 | DATA_COPY_FUNCTION = """ |
| 123 | void data_copy_xip_relocation(void) |
| 124 | {{ |
| 125 | {0} |
| 126 | }} |
| 127 | """ |
| 128 | |
| 129 | BSS_ZEROING_FUNCTION = """ |
| 130 | void bss_zeroing_relocation(void) |
| 131 | {{ |
| 132 | {0} |
| 133 | }} |
| 134 | """ |
| 135 | |
| 136 | MEMCPY_TEMPLATE = """ |
| 137 | (void)memcpy(&__{0}_{1}_start, &__{0}_{1}_rom_start, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 138 | (uint32_t) &__{0}_{1}_size); |
Adithya Baglody | 65f79cd | 2018-12-17 14:21:34 +0530 | [diff] [blame] | 139 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 140 | """ |
| 141 | |
| 142 | MEMSET_TEMPLATE = """ |
| 143 | (void)memset(&__{0}_bss_start, 0, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 144 | (uint32_t) &__{0}_bss_size); |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 145 | """ |
| 146 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 147 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 148 | def find_sections(filename, full_list_of_sections): |
| 149 | with open(filename, 'rb') as obj_file_desc: |
| 150 | full_lib = ELFFile(obj_file_desc) |
| 151 | if not full_lib: |
Ulf Magnusson | 50b9b12 | 2019-09-07 14:41:01 +0200 | [diff] [blame] | 152 | sys.exit("Error parsing file: " + filename) |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 153 | |
| 154 | sections = [x for x in full_lib.iter_sections()] |
| 155 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 156 | for section in sections: |
| 157 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 158 | if ".text." in section.name: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 159 | full_list_of_sections["text"].append(section.name) |
| 160 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 161 | if ".rodata." in section.name: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 162 | full_list_of_sections["rodata"].append(section.name) |
| 163 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 164 | if ".data." in section.name: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 165 | full_list_of_sections["data"].append(section.name) |
| 166 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 167 | if ".bss." in section.name: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 168 | full_list_of_sections["bss"].append(section.name) |
| 169 | |
| 170 | # Common variables will be placed in the .bss section |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 171 | # only after linking in the final executable. This "if" finds |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 172 | # common symbols and warns the user of the problem. |
| 173 | # The solution to which is simply assigning a 0 to |
| 174 | # bss variable and it will go to the required place. |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 175 | if ".symtab" in section.name: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 176 | symbols = [x for x in section.iter_symbols()] |
| 177 | for symbol in symbols: |
| 178 | if symbol.entry["st_shndx"] == 'SHN_COMMON': |
| 179 | warnings.warn("Common variable found. Move "+ |
| 180 | symbol.name + " to bss by assigning it to 0/NULL") |
| 181 | |
| 182 | return full_list_of_sections |
| 183 | |
| 184 | |
| 185 | def assign_to_correct_mem_region(memory_type, |
| 186 | full_list_of_sections, complete_list_of_sections): |
| 187 | all_regions = False |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 188 | iteration_sections = {"text": False, "rodata": False, "data": False, "bss": False} |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 189 | if "_TEXT" in memory_type: |
| 190 | iteration_sections["text"] = True |
| 191 | memory_type = memory_type.replace("_TEXT", "") |
| 192 | if "_RODATA" in memory_type: |
| 193 | iteration_sections["rodata"] = True |
| 194 | memory_type = memory_type.replace("_RODATA", "") |
| 195 | if "_DATA" in memory_type: |
| 196 | iteration_sections["data"] = True |
| 197 | memory_type = memory_type.replace("_DATA", "") |
| 198 | if "_BSS" in memory_type: |
| 199 | iteration_sections["bss"] = True |
| 200 | memory_type = memory_type.replace("_BSS", "") |
| 201 | if not (iteration_sections["data"] or iteration_sections["bss"] or |
| 202 | iteration_sections["text"] or iteration_sections["rodata"]): |
| 203 | all_regions = True |
| 204 | |
Wentong Wu | 09af98a | 2019-09-19 21:40:29 +0800 | [diff] [blame] | 205 | pos = memory_type.find('_') |
| 206 | if pos in range(len(memory_type)): |
| 207 | align_size = int(memory_type[pos+1:]) |
| 208 | memory_type = memory_type[:pos] |
| 209 | mpu_align[memory_type] = align_size |
| 210 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 211 | if memory_type in complete_list_of_sections: |
| 212 | for iter_sec in ["text", "rodata", "data", "bss"]: |
| 213 | if ((iteration_sections[iter_sec] or all_regions) and |
| 214 | full_list_of_sections[iter_sec] != []): |
| 215 | complete_list_of_sections[memory_type][iter_sec] += ( |
| 216 | full_list_of_sections[iter_sec]) |
| 217 | else: |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 218 | # new memory type was found. in which case just assign the |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 219 | # full_list_of_sections to the memorytype dict |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 220 | tmp_list = {"text": [], "rodata": [], "data": [], "bss": []} |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 221 | for iter_sec in ["text", "rodata", "data", "bss"]: |
| 222 | if ((iteration_sections[iter_sec] or all_regions) and |
| 223 | full_list_of_sections[iter_sec] != []): |
| 224 | tmp_list[iter_sec] = full_list_of_sections[iter_sec] |
| 225 | |
| 226 | complete_list_of_sections[memory_type] = tmp_list |
| 227 | |
| 228 | return complete_list_of_sections |
| 229 | |
| 230 | |
| 231 | def print_linker_sections(list_sections): |
| 232 | print_string = '' |
Marc Herbert | 6ccd026 | 2019-06-24 12:20:18 -0700 | [diff] [blame] | 233 | for section in sorted(list_sections): |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 234 | print_string += PRINT_TEMPLATE.format(section) |
| 235 | return print_string |
| 236 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 237 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 238 | def string_create_helper(region, memory_type, |
| 239 | full_list_of_sections, load_address_in_flash): |
| 240 | linker_string = '' |
| 241 | if load_address_in_flash: |
| 242 | load_address_string = LOAD_ADDRESS_LOCATION_FLASH.format(memory_type) |
| 243 | else: |
| 244 | load_address_string = LOAD_ADDRESS_LOCATION_BSS.format(memory_type) |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 245 | if full_list_of_sections[region]: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 246 | # Create a complete list of funcs/ variables that goes in for this |
| 247 | # memory type |
| 248 | tmp = print_linker_sections(full_list_of_sections[region]) |
Ulf Magnusson | 11d98ae | 2019-09-03 14:29:31 +0200 | [diff] [blame] | 249 | if memory_type == 'SRAM' and region in {'data', 'bss'}: |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 250 | linker_string += tmp |
| 251 | else: |
Wentong Wu | fa7ce3f | 2019-07-02 01:18:40 +0800 | [diff] [blame] | 252 | if memory_type != 'SRAM' and region == 'rodata': |
Wentong Wu | 09af98a | 2019-09-19 21:40:29 +0800 | [diff] [blame] | 253 | align_size = 0 |
| 254 | if memory_type in mpu_align.keys(): |
| 255 | align_size = mpu_align[memory_type] |
| 256 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 257 | linker_string += LINKER_SECTION_SEQ_MPU.format(memory_type.lower(), region, memory_type.upper(), |
Wentong Wu | 09af98a | 2019-09-19 21:40:29 +0800 | [diff] [blame] | 258 | region.upper(), tmp, load_address_string, align_size) |
Wentong Wu | fa7ce3f | 2019-07-02 01:18:40 +0800 | [diff] [blame] | 259 | else: |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 260 | linker_string += LINKER_SECTION_SEQ.format(memory_type.lower(), region, memory_type.upper(), |
| 261 | region.upper(), tmp, load_address_string) |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 262 | |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 263 | if load_address_in_flash: |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 264 | linker_string += SECTION_LOAD_MEMORY_SEQ.format(memory_type.lower(), region, memory_type.upper(), |
| 265 | region.upper()) |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 266 | return linker_string |
| 267 | |
| 268 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 269 | def generate_linker_script(linker_file, sram_data_linker_file, sram_bss_linker_file, complete_list_of_sections): |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 270 | gen_string = '' |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 271 | gen_string_sram_data = '' |
| 272 | gen_string_sram_bss = '' |
Marc Herbert | 6ccd026 | 2019-06-24 12:20:18 -0700 | [diff] [blame] | 273 | |
| 274 | for memory_type, full_list_of_sections in \ |
| 275 | sorted(complete_list_of_sections.items()): |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 276 | |
| 277 | if memory_type != "SRAM": |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 278 | gen_string += MPU_RO_REGION_START.format(memory_type.lower(), memory_type.upper()) |
| 279 | gen_string += string_create_helper("text", memory_type, full_list_of_sections, 1) |
| 280 | gen_string += string_create_helper("rodata", memory_type, full_list_of_sections, 1) |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 281 | if memory_type != "SRAM": |
| 282 | gen_string += MPU_RO_REGION_END.format(memory_type.lower()) |
| 283 | |
| 284 | if memory_type == 'SRAM': |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 285 | gen_string_sram_data += string_create_helper("data", memory_type, full_list_of_sections, 1) |
| 286 | gen_string_sram_bss += string_create_helper("bss", memory_type, full_list_of_sections, 0) |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 287 | else: |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 288 | gen_string += string_create_helper("data", memory_type, full_list_of_sections, 1) |
| 289 | gen_string += string_create_helper("bss", memory_type, full_list_of_sections, 0) |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 290 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 291 | # finally writing to the linker file |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 292 | with open(linker_file, "a+") as file_desc: |
| 293 | file_desc.write(gen_string) |
| 294 | |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 295 | with open(sram_data_linker_file, "a+") as file_desc: |
| 296 | file_desc.write(gen_string_sram_data) |
| 297 | |
| 298 | with open(sram_bss_linker_file, "a+") as file_desc: |
| 299 | file_desc.write(gen_string_sram_bss) |
| 300 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 301 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 302 | def generate_memcpy_code(memory_type, full_list_of_sections, code_generation): |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 303 | all_sections = True |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 304 | generate_section = {"text": False, "rodata": False, "data": False, "bss": False} |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 305 | for section_name in ["_TEXT", "_RODATA", "_DATA", "_BSS"]: |
| 306 | if section_name in memory_type: |
| 307 | generate_section[section_name.lower()[1:]] = True |
| 308 | memory_type = memory_type.replace(section_name, "") |
| 309 | all_sections = False |
| 310 | |
| 311 | if all_sections: |
| 312 | generate_section["text"] = True |
| 313 | generate_section["rodata"] = True |
| 314 | generate_section["data"] = True |
| 315 | generate_section["bss"] = True |
| 316 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 317 | # add all the regions that needs to be copied on boot up |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 318 | for mtype in ["text", "rodata", "data"]: |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 319 | if memory_type == "SRAM" and mtype == "data": |
| 320 | continue |
| 321 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 322 | if full_list_of_sections[mtype] and generate_section[mtype]: |
| 323 | code_generation["copy_code"] += MEMCPY_TEMPLATE.format(memory_type.lower(), mtype) |
| 324 | code_generation["extern"] += EXTERN_LINKER_VAR_DECLARATION.format( |
| 325 | memory_type.lower(), mtype) |
| 326 | |
| 327 | # add for all the bss data that needs to be zeored on boot up |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 328 | if full_list_of_sections["bss"] and generate_section["bss"] and memory_type != "SRAM": |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 329 | code_generation["zero_code"] += MEMSET_TEMPLATE.format(memory_type.lower()) |
| 330 | code_generation["extern"] += EXTERN_LINKER_VAR_DECLARATION.format( |
| 331 | memory_type.lower(), "bss") |
| 332 | |
| 333 | return code_generation |
| 334 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 335 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 336 | def dump_header_file(header_file, code_generation): |
| 337 | code_string = '' |
| 338 | # create a dummy void function if there is no code to generate for |
| 339 | # bss/data/text regions |
| 340 | |
| 341 | code_string += code_generation["extern"] |
| 342 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 343 | if code_generation["copy_code"]: |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 344 | code_string += DATA_COPY_FUNCTION.format(code_generation["copy_code"]) |
| 345 | else: |
| 346 | code_string += DATA_COPY_FUNCTION.format("void;") |
| 347 | if code_generation["zero_code"]: |
| 348 | code_string += BSS_ZEROING_FUNCTION.format(code_generation["zero_code"]) |
| 349 | else: |
| 350 | code_string += BSS_ZEROING_FUNCTION.format("return;") |
| 351 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 352 | with open(header_file, "w") as header_file_desc: |
| 353 | header_file_desc.write(SOURCE_CODE_INCLUDES) |
| 354 | header_file_desc.write(code_string) |
| 355 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 356 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 357 | def parse_args(): |
| 358 | global args |
| 359 | parser = argparse.ArgumentParser( |
| 360 | description=__doc__, |
| 361 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 362 | parser.add_argument("-d", "--directory", required=True, |
| 363 | help="obj file's directory") |
| 364 | parser.add_argument("-i", "--input_rel_dict", required=True, |
| 365 | help="input src:memory type(sram2 or ccm or aon etc) string") |
| 366 | parser.add_argument("-o", "--output", required=False, help="Output ld file") |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 367 | parser.add_argument("-s", "--output_sram_data", required=False, |
| 368 | help="Output sram data ld file") |
| 369 | parser.add_argument("-b", "--output_sram_bss", required=False, |
| 370 | help="Output sram bss ld file") |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 371 | parser.add_argument("-c", "--output_code", required=False, |
| 372 | help="Output relocation code header file") |
| 373 | parser.add_argument("-v", "--verbose", action="count", default=0, |
| 374 | help="Verbose Output") |
| 375 | args = parser.parse_args() |
| 376 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 377 | |
| 378 | # return the absolute path for the object file. |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 379 | def get_obj_filename(searchpath, filename): |
| 380 | # get the object file name which is almost always pended with .obj |
| 381 | obj_filename = filename.split("/")[-1] + ".obj" |
| 382 | |
Ulf Magnusson | 12ba9df | 2019-03-19 19:28:24 +0100 | [diff] [blame] | 383 | for dirpath, _, files in os.walk(searchpath): |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 384 | for filename1 in files: |
| 385 | if filename1 == obj_filename: |
| 386 | if filename.split("/")[-2] in dirpath.split("/")[-1]: |
| 387 | fullname = os.path.join(dirpath, filename1) |
| 388 | return fullname |
| 389 | |
| 390 | |
| 391 | # Create a dict with key as memory type and files as a list of values. |
| 392 | def create_dict_wrt_mem(): |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 393 | # need to support wild card * |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 394 | rel_dict = dict() |
| 395 | if args.input_rel_dict == '': |
Ulf Magnusson | 50b9b12 | 2019-09-07 14:41:01 +0200 | [diff] [blame] | 396 | sys.exit("Disable CONFIG_CODE_DATA_RELOCATION if no file needs relocation") |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 397 | for line in args.input_rel_dict.split(';'): |
Torsten Rasmussen | 1bd0b29 | 2020-12-08 14:52:10 +0100 | [diff] [blame] | 398 | mem_region, file_name = line.split(':', 1) |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 399 | |
| 400 | file_name_list = glob.glob(file_name) |
| 401 | if not file_name_list: |
| 402 | warnings.warn("File: "+file_name+" Not found") |
| 403 | continue |
| 404 | if mem_region == '': |
| 405 | continue |
| 406 | if args.verbose: |
| 407 | print("Memory region ", mem_region, " Selected for file:", file_name_list) |
| 408 | if mem_region in rel_dict: |
| 409 | rel_dict[mem_region].extend(file_name_list) |
| 410 | else: |
| 411 | rel_dict[mem_region] = file_name_list |
| 412 | |
| 413 | return rel_dict |
| 414 | |
| 415 | |
| 416 | def main(): |
Wentong Wu | 09af98a | 2019-09-19 21:40:29 +0800 | [diff] [blame] | 417 | global mpu_align |
| 418 | mpu_align = {} |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 419 | parse_args() |
| 420 | searchpath = args.directory |
| 421 | linker_file = args.output |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 422 | sram_data_linker_file = args.output_sram_data |
| 423 | sram_bss_linker_file = args.output_sram_bss |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 424 | rel_dict = create_dict_wrt_mem() |
| 425 | complete_list_of_sections = {} |
| 426 | |
| 427 | # Create/or trucate file contents if it already exists |
| 428 | # raw = open(linker_file, "w") |
| 429 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 430 | # for each memory_type, create text/rodata/data/bss sections for all obj files |
| 431 | for memory_type, files in rel_dict.items(): |
| 432 | full_list_of_sections = {"text": [], "rodata": [], "data": [], "bss": []} |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 433 | |
| 434 | for filename in files: |
| 435 | obj_filename = get_obj_filename(searchpath, filename) |
| 436 | # the obj file wasn't found. Probably not compiled. |
| 437 | if not obj_filename: |
| 438 | continue |
| 439 | |
| 440 | full_list_of_sections = find_sections(obj_filename, full_list_of_sections) |
| 441 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 442 | # cleanup and attach the sections to the memory type after cleanup. |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 443 | complete_list_of_sections = assign_to_correct_mem_region(memory_type, |
| 444 | full_list_of_sections, |
| 445 | complete_list_of_sections) |
| 446 | |
Wentong Wu | 743a184 | 2019-05-14 00:30:52 +0800 | [diff] [blame] | 447 | generate_linker_script(linker_file, sram_data_linker_file, |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 448 | sram_bss_linker_file, complete_list_of_sections) |
Marc Herbert | 6ccd026 | 2019-06-24 12:20:18 -0700 | [diff] [blame] | 449 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 450 | code_generation = {"copy_code": '', "zero_code": '', "extern": ''} |
| 451 | for mem_type, list_of_sections in sorted(complete_list_of_sections.items()): |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 452 | code_generation = generate_memcpy_code(mem_type, |
| 453 | list_of_sections, code_generation) |
| 454 | |
| 455 | dump_header_file(args.output_code, code_generation) |
| 456 | |
Håkon Øye Amundsen | 4e13501 | 2019-08-15 08:36:15 +0000 | [diff] [blame] | 457 | |
Adithya Baglody | ff63101 | 2018-11-26 21:52:42 +0530 | [diff] [blame] | 458 | if __name__ == '__main__': |
| 459 | main() |