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 | |
Andrew Boie | c78c5e6 | 2019-03-11 14:45:43 -0700 | [diff] [blame] | 7 | """ |
| 8 | Script to generate a linker script organizing application memory partitions |
| 9 | |
| 10 | Applications may declare build-time memory domain partitions with |
| 11 | K_APPMEM_PARTITION_DEFINE, and assign globals to them using K_APP_DMEM |
| 12 | or K_APP_BMEM macros. For each of these partitions, we need to |
| 13 | route all their data into appropriately-sized memory areas which meet the |
| 14 | size/alignment constraints of the memory protection hardware. |
| 15 | |
| 16 | This linker script is created very early in the build process, before |
| 17 | the build attempts to link the kernel binary, as the linker script this |
| 18 | tool generates is a necessary pre-condition for kernel linking. We extract |
| 19 | the set of memory partitions to generate by looking for variables which |
| 20 | have been assigned to input sections that follow a defined naming convention. |
| 21 | We also allow entire libraries to be pulled in to assign their globals |
| 22 | to a particular memory partition via command line directives. |
| 23 | |
| 24 | This script takes as inputs: |
| 25 | |
| 26 | - The base directory to look for compiled objects |
| 27 | - key/value pairs mapping static library files to what partitions their globals |
| 28 | should end up in. |
| 29 | |
| 30 | The output is a linker script fragment containing the definition of the |
| 31 | app shared memory section, which is further divided, for each partition |
| 32 | found, into data and BSS for each partition. |
| 33 | """ |
| 34 | |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 35 | import sys |
| 36 | import argparse |
| 37 | import os |
| 38 | import re |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 39 | from collections import OrderedDict |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 40 | from elftools.elf.elffile import ELFFile |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 41 | from elftools.elf.sections import SymbolTableSection |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 42 | |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 43 | SZ = 'size' |
| 44 | SRC = 'sources' |
| 45 | LIB = 'libraries' |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 46 | |
Andrew Boie | 7adff46 | 2019-01-30 13:44:54 -0800 | [diff] [blame] | 47 | # This script will create sections and linker variables to place the |
| 48 | # application shared memory partitions. |
| 49 | # these are later read by the macros defined in app_memdomain.h for |
Andrew Boie | 4ce652e | 2019-02-22 16:08:44 -0800 | [diff] [blame] | 50 | # initialization purpose when USERSPACE is enabled. |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 51 | data_template = """ |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 52 | /* Auto generated code do not modify */ |
Andrew Boie | f084c38 | 2019-03-04 17:57:06 -0800 | [diff] [blame] | 53 | SMEM_PARTITION_ALIGN(z_data_smem_{0}_bss_end - z_data_smem_{0}_part_start); |
| 54 | z_data_smem_{0}_part_start = .; |
Eugeniy Paltsev | 0a7b65e | 2020-09-02 16:52:09 +0300 | [diff] [blame] | 55 | KEEP(*(data_smem_{0}_data*)) |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 56 | """ |
| 57 | |
| 58 | library_data_template = """ |
Jim Shu | 46eb3e5 | 2021-09-28 10:03:21 +0800 | [diff] [blame] | 59 | *{0}:*(.data .data.* .sdata .sdata.*) |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 60 | """ |
| 61 | |
| 62 | bss_template = """ |
Andrew Boie | f084c38 | 2019-03-04 17:57:06 -0800 | [diff] [blame] | 63 | z_data_smem_{0}_bss_start = .; |
Eugeniy Paltsev | 0a7b65e | 2020-09-02 16:52:09 +0300 | [diff] [blame] | 64 | KEEP(*(data_smem_{0}_bss*)) |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 65 | """ |
| 66 | |
| 67 | library_bss_template = """ |
Jim Shu | 46eb3e5 | 2021-09-28 10:03:21 +0800 | [diff] [blame] | 68 | *{0}:*(.bss .bss.* .sbss .sbss.* COMMON COMMON.*) |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 69 | """ |
| 70 | |
| 71 | footer_template = """ |
Andrew Boie | f084c38 | 2019-03-04 17:57:06 -0800 | [diff] [blame] | 72 | z_data_smem_{0}_bss_end = .; |
| 73 | SMEM_PARTITION_ALIGN(z_data_smem_{0}_bss_end - z_data_smem_{0}_part_start); |
| 74 | z_data_smem_{0}_part_end = .; |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 75 | """ |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 76 | |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 77 | linker_start_seq = """ |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 78 | SECTION_PROLOGUE(_APP_SMEM{1}_SECTION_NAME,,) |
| 79 | {{ |
Adithya Baglody | 10c6a0c | 2018-09-21 10:17:58 +0530 | [diff] [blame] | 80 | APP_SHARED_ALIGN; |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 81 | _app_smem{0}_start = .; |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 82 | """ |
| 83 | |
| 84 | linker_end_seq = """ |
Adithya Baglody | 10c6a0c | 2018-09-21 10:17:58 +0530 | [diff] [blame] | 85 | APP_SHARED_ALIGN; |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 86 | _app_smem{0}_end = .; |
| 87 | }} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) |
| 88 | """ |
| 89 | |
| 90 | empty_app_smem = """ |
| 91 | SECTION_PROLOGUE(_APP_SMEM{1}_SECTION_NAME,,) |
| 92 | {{ |
Jaxson Han | f079e66 | 2021-08-31 14:14:03 +0800 | [diff] [blame] | 93 | #ifdef EMPTY_APP_SHARED_ALIGN |
| 94 | EMPTY_APP_SHARED_ALIGN; |
| 95 | #endif |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 96 | _app_smem{0}_start = .; |
| 97 | _app_smem{0}_end = .; |
| 98 | }} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) |
Adithya Baglody | ae92f2b | 2018-09-19 11:20:09 +0530 | [diff] [blame] | 99 | """ |
| 100 | |
| 101 | size_cal_string = """ |
Andrew Boie | f084c38 | 2019-03-04 17:57:06 -0800 | [diff] [blame] | 102 | z_data_smem_{0}_part_size = z_data_smem_{0}_part_end - z_data_smem_{0}_part_start; |
| 103 | z_data_smem_{0}_bss_size = z_data_smem_{0}_bss_end - z_data_smem_{0}_bss_start; |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 104 | """ |
| 105 | |
Eugeniy Paltsev | 0a7b65e | 2020-09-02 16:52:09 +0300 | [diff] [blame] | 106 | section_regex = re.compile(r'data_smem_([A-Za-z0-9_]*)_(data|bss)*') |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 107 | |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 108 | elf_part_size_regex = re.compile(r'z_data_smem_(.*)_part_size') |
| 109 | |
| 110 | def find_obj_file_partitions(filename, partitions): |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 111 | with open(filename, 'rb') as f: |
Ulf Magnusson | 0d39a10 | 2019-09-06 11:13:19 +0200 | [diff] [blame] | 112 | full_lib = ELFFile(f) |
Ulf Magnusson | ba312fe | 2019-03-20 19:30:29 +0100 | [diff] [blame] | 113 | if not full_lib: |
Ulf Magnusson | e9c1d6d | 2019-03-20 22:40:22 +0100 | [diff] [blame] | 114 | sys.exit("Error parsing file: " + filename) |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 115 | |
Andrew Boie | 7adff46 | 2019-01-30 13:44:54 -0800 | [diff] [blame] | 116 | sections = [x for x in full_lib.iter_sections()] |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 117 | for section in sections: |
Andrew Boie | 7adff46 | 2019-01-30 13:44:54 -0800 | [diff] [blame] | 118 | m = section_regex.match(section.name) |
| 119 | if not m: |
| 120 | continue |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 121 | |
Andrew Boie | 7adff46 | 2019-01-30 13:44:54 -0800 | [diff] [blame] | 122 | partition_name = m.groups()[0] |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 123 | if partition_name not in partitions: |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 124 | partitions[partition_name] = {SZ: section.header.sh_size} |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 125 | |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 126 | if args.verbose: |
| 127 | partitions[partition_name][SRC] = filename |
| 128 | |
| 129 | else: |
| 130 | partitions[partition_name][SZ] += section.header.sh_size |
| 131 | |
| 132 | |
| 133 | return partitions |
| 134 | |
| 135 | |
| 136 | def parse_obj_files(partitions): |
| 137 | # Iterate over all object files to find partitions |
Ulf Magnusson | 12ba9df | 2019-03-19 19:28:24 +0100 | [diff] [blame] | 138 | for dirpath, _, files in os.walk(args.directory): |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 139 | for filename in files: |
Ulf Magnusson | 0d39a10 | 2019-09-06 11:13:19 +0200 | [diff] [blame] | 140 | if re.match(r".*\.obj$", filename): |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 141 | fullname = os.path.join(dirpath, filename) |
| 142 | find_obj_file_partitions(fullname, partitions) |
| 143 | |
| 144 | |
| 145 | def parse_elf_file(partitions): |
| 146 | with open(args.elf, 'rb') as f: |
| 147 | elffile = ELFFile(f) |
| 148 | |
| 149 | symbol_tbls = [s for s in elffile.iter_sections() |
| 150 | if isinstance(s, SymbolTableSection)] |
| 151 | |
| 152 | for section in symbol_tbls: |
Ulf Magnusson | 12ba9df | 2019-03-19 19:28:24 +0100 | [diff] [blame] | 153 | for symbol in section.iter_symbols(): |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 154 | if symbol['st_shndx'] != "SHN_ABS": |
| 155 | continue |
| 156 | |
| 157 | x = elf_part_size_regex.match(symbol.name) |
| 158 | if not x: |
| 159 | continue |
| 160 | |
| 161 | partition_name = x.groups()[0] |
| 162 | size = symbol['st_value'] |
| 163 | if partition_name not in partitions: |
| 164 | partitions[partition_name] = {SZ: size} |
| 165 | |
| 166 | if args.verbose: |
| 167 | partitions[partition_name][SRC] = args.elf |
| 168 | |
| 169 | else: |
| 170 | partitions[partition_name][SZ] += size |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 171 | |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 172 | |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 173 | def generate_final_linker(linker_file, partitions, lnkr_sect=""): |
| 174 | string = "" |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 175 | |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 176 | if len(partitions) > 0: |
| 177 | string = linker_start_seq.format(lnkr_sect, lnkr_sect.upper()) |
| 178 | size_string = '' |
| 179 | for partition, item in partitions.items(): |
| 180 | string += data_template.format(partition) |
| 181 | if LIB in item: |
| 182 | for lib in item[LIB]: |
| 183 | string += library_data_template.format(lib) |
| 184 | string += bss_template.format(partition, lnkr_sect) |
| 185 | if LIB in item: |
| 186 | for lib in item[LIB]: |
| 187 | string += library_bss_template.format(lib) |
| 188 | string += footer_template.format(partition) |
| 189 | size_string += size_cal_string.format(partition) |
| 190 | |
| 191 | string += linker_end_seq.format(lnkr_sect) |
| 192 | string += size_string |
| 193 | else: |
| 194 | string = empty_app_smem.format(lnkr_sect, lnkr_sect.upper()) |
| 195 | |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 196 | with open(linker_file, "w") as fw: |
| 197 | fw.write(string) |
| 198 | |
| 199 | |
| 200 | def parse_args(): |
| 201 | global args |
| 202 | parser = argparse.ArgumentParser( |
| 203 | description=__doc__, |
| 204 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 205 | parser.add_argument("-d", "--directory", required=False, default=None, |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 206 | help="Root build directory") |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 207 | parser.add_argument("-e", "--elf", required=False, default=None, |
| 208 | help="ELF file") |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 209 | parser.add_argument("-o", "--output", required=False, |
| 210 | help="Output ld file") |
Ulf Magnusson | 0d39a10 | 2019-09-06 11:13:19 +0200 | [diff] [blame] | 211 | parser.add_argument("-v", "--verbose", action="count", default=0, |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 212 | help="Verbose Output") |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 213 | parser.add_argument("-l", "--library", nargs=2, action="append", default=[], |
| 214 | metavar=("LIBRARY", "PARTITION"), |
| 215 | help="Include globals for a particular library or object filename into a designated partition") |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 216 | parser.add_argument("--pinoutput", required=False, |
| 217 | help="Output ld file for pinned sections") |
| 218 | parser.add_argument("--pinpartitions", action="store", required=False, default="", |
| 219 | help="Comma separated names of partitions to be pinned in physical memory") |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 220 | |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 221 | args = parser.parse_args() |
| 222 | |
| 223 | |
| 224 | def main(): |
| 225 | parse_args() |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 226 | partitions = {} |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 227 | |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 228 | if args.directory is not None: |
| 229 | parse_obj_files(partitions) |
| 230 | elif args.elf is not None: |
| 231 | parse_elf_file(partitions) |
| 232 | else: |
| 233 | return |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 234 | |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 235 | for lib, ptn in args.library: |
| 236 | if ptn not in partitions: |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 237 | partitions[ptn] = {} |
Andrew Boie | 686bd91 | 2019-02-01 11:52:20 -0800 | [diff] [blame] | 238 | |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 239 | if LIB not in partitions[ptn]: |
| 240 | partitions[ptn][LIB] = [lib] |
| 241 | else: |
| 242 | partitions[ptn][LIB].append(lib) |
| 243 | |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 244 | if args.pinoutput: |
| 245 | pin_part_names = args.pinpartitions.split(',') |
| 246 | |
| 247 | generic_partitions = {key: value for key, value in partitions.items() |
| 248 | if key not in pin_part_names} |
| 249 | pinned_partitions = {key: value for key, value in partitions.items() |
| 250 | if key in pin_part_names} |
| 251 | else: |
| 252 | generic_partitions = partitions |
Marc Herbert | eefea9d | 2019-06-27 16:09:26 -0700 | [diff] [blame] | 253 | |
| 254 | # Sample partitions.items() list before sorting: |
| 255 | # [ ('part1', {'size': 64}), ('part3', {'size': 64}, ... |
| 256 | # ('part0', {'size': 334}) ] |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 257 | decreasing_tuples = sorted(generic_partitions.items(), |
Marc Herbert | eefea9d | 2019-06-27 16:09:26 -0700 | [diff] [blame] | 258 | key=lambda x: (x[1][SZ], x[0]), reverse=True) |
| 259 | |
| 260 | partsorted = OrderedDict(decreasing_tuples) |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 261 | |
| 262 | generate_final_linker(args.output, partsorted) |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 263 | if args.verbose: |
Andrew Boie | 7adff46 | 2019-01-30 13:44:54 -0800 | [diff] [blame] | 264 | print("Partitions retrieved:") |
Daniel Leung | 212ec9a | 2019-03-10 14:20:21 -0700 | [diff] [blame] | 265 | for key in partsorted: |
| 266 | print(" {0}: size {1}: {2}".format(key, |
| 267 | partsorted[key][SZ], |
| 268 | partsorted[key][SRC])) |
| 269 | |
Daniel Leung | 2117a2a | 2021-07-12 13:33:32 -0700 | [diff] [blame] | 270 | if args.pinoutput: |
| 271 | decreasing_tuples = sorted(pinned_partitions.items(), |
| 272 | key=lambda x: (x[1][SZ], x[0]), reverse=True) |
| 273 | |
| 274 | partsorted = OrderedDict(decreasing_tuples) |
| 275 | |
| 276 | generate_final_linker(args.pinoutput, partsorted, lnkr_sect="_pinned") |
| 277 | if args.verbose: |
| 278 | print("Pinned partitions retrieved:") |
| 279 | for key in partsorted: |
| 280 | print(" {0}: size {1}: {2}".format(key, |
| 281 | partsorted[key][SZ], |
| 282 | partsorted[key][SRC])) |
| 283 | |
Adithya Baglody | c69fb0d | 2018-08-04 19:48:52 +0530 | [diff] [blame] | 284 | |
| 285 | if __name__ == '__main__': |
| 286 | main() |