xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | #coding:utf-8 |
| 3 | # |
| 4 | # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 5 | # |
| 6 | # SPDX-License-Identifier: MIT |
| 7 | # |
| 8 | |
| 9 | |
| 10 | from __future__ import print_function |
| 11 | |
| 12 | import sys |
| 13 | import os |
| 14 | import argparse |
| 15 | |
| 16 | parser = argparse.ArgumentParser(description='total size of each object file in an ld linker map.') |
| 17 | parser.add_argument('map_file', help="A map file generated by passing -M/--print-map to ld during linking.") |
| 18 | parser.add_argument('--combine', action='store_true', |
| 19 | help="All object files in an .a archive or in a directory are combined") |
| 20 | args = parser.parse_args() |
| 21 | |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 22 | class SectionSize: |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 23 | text = 0 |
| 24 | rela_text = 0 |
| 25 | data = 0 # Including metadata like import tables |
| 26 | bss = 0 |
| 27 | customize = 0 |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 28 | system_heap = 0 |
| 29 | system_stack = 0 |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 30 | rodata = 0 |
| 31 | rom_usage = 0 |
| 32 | ram_usage = 0 |
| 33 | |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 34 | def total(self): |
| 35 | return self.text + self.data + self.bss |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 36 | |
| 37 | def add_text_with_rodata(self, section, size): |
| 38 | if section.startswith('.text') or section.startswith('.rodata'): |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 39 | self.text += size |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 40 | #add the .rodata to data in order to match the result of size command with gcc's elf file |
| 41 | #the elf file generate by riscv then toolchain add the .rodata to .text |
| 42 | if section.startswith('.rodata'): |
| 43 | self.rodata += size |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 44 | elif section.startswith('.rela.dyn'): |
| 45 | self.rela_text += size |
| 46 | elif section.startswith('.bss') or section.startswith('.common') or section.startswith('.sbss'): |
| 47 | self.bss += size |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 48 | elif section.startswith('.data'): |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 49 | self.data += size |
| 50 | elif section.startswith('.heap'): |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 51 | SectionSize.system_heap += size |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 52 | elif section.startswith('.stack'): |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 53 | SectionSize.system_stack += size |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 54 | else: |
| 55 | if (size > 0): |
| 56 | print("customer section:%s, size:%d" % (section, size)) |
| 57 | self.customize += size |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 58 | self.rom_usage = (self.text + self.data) |
| 59 | self.ram_usage = (self.data + self.bss) |
| 60 | |
| 61 | def add_data_with_rodata(self, section, size): |
| 62 | #arch is arm64 and arm32 |
| 63 | if section.startswith('.text'): |
| 64 | self.text += size |
| 65 | elif section.startswith('.rela.dyn'): |
| 66 | self.rela_text += size |
| 67 | elif section.startswith('.bss') or section.startswith('.common') or section.startswith('.sbss'): |
| 68 | self.bss += size |
| 69 | elif section.startswith('.data') or section.startswith('.rodata'): |
| 70 | #add the .rodata to data in order to match the result of size command with gcc's elf file |
| 71 | #the elf file generate by arm toolchain then it add the .rodata to .data |
| 72 | self.data += size |
| 73 | if section.startswith('.rodata'): |
| 74 | self.rodata += size |
| 75 | elif section.startswith('.heap'): |
| 76 | SectionSize.system_heap += size |
| 77 | elif section.startswith('.stack'): |
| 78 | SectionSize.system_stack += size |
| 79 | else: |
| 80 | if (size > 0): |
| 81 | print("customer section:%s, size:%d" % (section, size)) |
| 82 | self.customize += size |
| 83 | self.rom_usage = (self.text + self.data) |
| 84 | self.ram_usage = (self.data + self.bss - self.rodata) |
| 85 | |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 86 | def add_xcc_section(self, section, size): |
xiaohu.huang | fc13997 | 2024-01-23 10:36:50 +0800 | [diff] [blame] | 87 | if current_section is None: |
| 88 | return; |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 89 | if section.startswith('.comment'): |
| 90 | return |
| 91 | if section.startswith('.debug'): |
| 92 | return |
| 93 | if section.startswith('.xt.prop')or section.startswith('.xt.lit'): |
| 94 | return |
| 95 | if section.startswith('.text') or section.endswith('.text') \ |
| 96 | or section.startswith('.literal') or section.endswith('.literal') \ |
| 97 | or section.startswith('.rodata') or section.endswith('.rodata'): |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 98 | #add the .rodata to text in order to match the result of size command with xcc's elf file |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 99 | self.text += size |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 100 | if section.startswith('.rodata') or section.endswith('.rodata'): |
| 101 | self.rodata += size |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 102 | elif section.startswith('.rela.dyn'): |
| 103 | self.rela_text += size |
| 104 | elif section.startswith('.bss') or section.startswith('.common') or section.startswith('.sbss'): |
| 105 | self.bss += size |
| 106 | elif section.startswith('.data') or section.endswith('.data'): |
| 107 | self.data += size |
| 108 | elif section.startswith('.heap'): |
| 109 | self.heap += size |
| 110 | self.bss += size |
| 111 | elif section.startswith('.stack'): |
| 112 | self.stack += size |
| 113 | self.bss += size |
| 114 | else: |
| 115 | if (size > 0): |
| 116 | print("customer section:%s, size:%d" % (section, size)) |
| 117 | self.customize += size |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 118 | self.rom_usage = (self.text + self.data) |
| 119 | self.ram_usage = (self.data + self.bss) |
| 120 | |
| 121 | def print_codesize_module_text_with_rodata(): |
| 122 | print('---------------------------------------------------------------------------------------------------') |
| 123 | col_format = "%-20s\t%-12s\t%-12s\t%-10s%-10s\t%-12s\t%-12s\t%-7s" |
| 124 | print(col_format % ("module file", "ROM(text+data)", "RAM(data+bss)", ".text", "(.rodata)", ".data", ".bss", "customize")) |
| 125 | col_format = "%-20s\t%-12s\t%-12s\t%-10s(%-10s)\t%-12s\t%-12s\t%-7s" |
| 126 | for source in sources: |
| 127 | size = size_by_source[source] |
| 128 | print(col_format % (os.path.basename(source)[:20], size.rom_usage, size.ram_usage, size.text, size.rodata, size.data, size.bss, size.customize)) |
| 129 | print('---------------------------------------------------------------------------------------------------') |
| 130 | |
| 131 | def print_codesize_module_data_with_rodata(): |
| 132 | print('---------------------------------------------------------------------------------------------------') |
| 133 | col_format = "%-20s\t%-12s\t%-12s\t%-7s\t%-10s%-10s\t%-12s\t%-7s" |
| 134 | print(col_format % ("module file", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", "(.rodata)", ".bss", "customize")) |
| 135 | col_format = "%-20s\t%-12s\t%-12s\t%-7s\t%-10s(%-10s)\t%-12s\t%-7s" |
| 136 | for source in sources: |
| 137 | size = size_by_source[source] |
| 138 | print(col_format % (os.path.basename(source)[:20], size.rom_usage, size.ram_usage, size.text, size.data, size.rodata, size.bss, size.customize)) |
| 139 | print('---------------------------------------------------------------------------------------------------') |
| 140 | |
| 141 | def print_codesize_summary_text_with_rodata(): |
| 142 | global sumrom |
| 143 | global sumram |
| 144 | global sumcode |
| 145 | global sumdata |
| 146 | global sumbss |
| 147 | global sumcustomize |
| 148 | global sumrodata |
| 149 | global sys_mem_usage |
| 150 | |
| 151 | for source in sources: |
| 152 | size = size_by_source[source] |
| 153 | sumcode += size.text |
| 154 | sumdata += size.data |
| 155 | sumbss += size.bss |
| 156 | sumrodata += size.rodata |
| 157 | sumrom += size.rom_usage |
| 158 | sumram += size.ram_usage |
| 159 | sumcustomize += size.customize |
| 160 | |
| 161 | print('---------------------------------------------------------------------------------------------------') |
| 162 | col_format = "%-5s\t%-12s\t%-12s\t%-12s%-12s\t%-12s\t%-12s\t%-7s\t%-7s\t%-7s" |
| 163 | print(col_format % (" ", "ROM(text+data)", "RAM(data+bss)", ".text", "(.rodata)", ".data", ".bss", "cust", "stack", "heap" )) |
| 164 | col_format = "%-5s\t%-12s\t%-12s\t%-12s(%-12s)\t%-12s\t%-12s\t%-7s\t%-7s\t%-7s" |
| 165 | print(col_format % ("total", sumrom, sumram + sys_mem_usage, sumcode, sumrodata, sumdata, sumbss + sys_mem_usage, sumcustomize, SectionSize.system_stack, SectionSize.system_heap)) |
| 166 | print('---------------------------------------------------------------------------------------------------') |
| 167 | |
| 168 | def print_codesize_summary_data_with_rodata(): |
| 169 | global sumrom |
| 170 | global sumram |
| 171 | global sumcode |
| 172 | global sumdata |
| 173 | global sumbss |
| 174 | global sumcustomize |
| 175 | global sumrodata |
| 176 | global sys_mem_usage |
| 177 | |
| 178 | for source in sources: |
| 179 | size = size_by_source[source] |
| 180 | sumcode += size.text |
| 181 | sumdata += size.data |
| 182 | sumbss += size.bss |
| 183 | sumrodata += size.rodata |
| 184 | sumrom += size.rom_usage |
| 185 | sumram += size.ram_usage |
| 186 | sumcustomize += size.customize |
| 187 | |
| 188 | print('---------------------------------------------------------------------------------------------------') |
| 189 | col_format = "%-5s\t%-12s\t%-12s\t%-7s\t%-12s%-12s\t%-12s\t%-7s\t%-7s\t%-7s" |
| 190 | print(col_format % (" ", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", "(.rodata)", ".bss", "cust", "stack", "heap" )) |
| 191 | col_format = "%-5s\t%-12s\t%-12s\t%-7s\t%-12s(%-12s)\t%-12s\t%-7s\t%-7s\t%-7s" |
| 192 | print(col_format % ("total", sumrom, sumram + sys_mem_usage, sumcode, sumdata, sumrodata, sumbss + sys_mem_usage, sumcustomize, SectionSize.system_stack, SectionSize.system_heap)) |
| 193 | print('---------------------------------------------------------------------------------------------------') |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 194 | |
| 195 | size_by_source = {} |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 196 | with open(args.map_file) as f: |
xiaohu.huang | 5728549 | 2024-06-26 15:47:07 +0800 | [diff] [blame] | 197 | arch_toolchain = "XCC" |
| 198 | toolchain_keyword = "xtensa-elf" |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 199 | print("%s toolchain map analyzer" % arch_toolchain) |
| 200 | |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 201 | lines = iter(f) |
| 202 | for line in lines: |
| 203 | if line.strip() == "Linker script and memory map": |
| 204 | break |
| 205 | |
| 206 | current_section = None |
| 207 | split_line = None |
| 208 | last_addr = 0 |
| 209 | last_size = 0 |
| 210 | for line in lines: |
| 211 | line = line.strip('\n') |
| 212 | if split_line: |
| 213 | # Glue a line that was split in two back together |
| 214 | if line.startswith(' ' * 16): |
| 215 | line = split_line + line |
| 216 | else: # Shouldn't happen |
| 217 | print("Warning: discarding line ", split_line) |
| 218 | split_line = None |
| 219 | |
| 220 | if ('size before relaxing' in line): |
| 221 | continue |
| 222 | if line.startswith((".", " .", " *fill*")): |
| 223 | pieces = line.split(None, 3) # Don't split paths containing spaces |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 224 | pieces_num = len(pieces) |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 225 | |
| 226 | if line.startswith("."): |
| 227 | # Note: this line might be wrapped, with the size of the section |
| 228 | # on the next line, but we ignore the size anyway and will ignore that line |
| 229 | current_section = pieces[0] |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 230 | # XCC text section format |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 231 | if (pieces_num == 4): |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 232 | source = pieces[-1] |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 233 | elif pieces_num == 1 and len(line) > 14: |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 234 | # ld splits the rest of this line onto the next if the section name is too long |
| 235 | split_line = line |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 236 | elif pieces_num >= 3 and "=" not in pieces and "before" not in pieces: |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 237 | if pieces[0] == "*fill*": |
| 238 | # fill use the last source to store the fill align data |
| 239 | #source = pieces[0] |
| 240 | size = int(pieces[-1], 16) |
| 241 | if (pieces[-2] == last_addr): |
| 242 | # sub the merged size from the same address for fill data |
| 243 | size = size - last_size |
| 244 | else: |
| 245 | last_size = size |
| 246 | last_addr = pieces[-2] |
| 247 | else: |
| 248 | source = pieces[-1] |
| 249 | size = int(pieces[-2], 16) |
| 250 | if (pieces[-3] == last_addr): |
| 251 | # sub the merged size from the same address for text data |
| 252 | size = size - last_size |
| 253 | else: |
| 254 | last_size = size |
| 255 | last_addr = pieces[-3] |
| 256 | |
| 257 | if args.combine: |
| 258 | if '.a(' in source: |
| 259 | # path/to/archive.a(object.o) |
| 260 | source = source[:source.index('.a(') + 2] |
| 261 | elif '.dir' in source: |
| 262 | source = source[:source.find(".dir")] |
| 263 | elif source.endswith('.o'): |
xiaohu.huang | e6c3167 | 2022-12-05 10:10:42 +0800 | [diff] [blame] | 264 | if toolchain_keyword in source: |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 265 | source = 'toolchain_obj' |
| 266 | else: |
| 267 | source = os.path.basename(source) |
| 268 | |
| 269 | if source not in size_by_source: |
| 270 | size_by_source[source] = SectionSize() |
xiaohu.huang | 5728549 | 2024-06-26 15:47:07 +0800 | [diff] [blame] | 271 | size_by_source[source].add_xcc_section(current_section, size) |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 272 | |
xiaohu.huang | ffae171 | 2022-11-01 18:04:01 +0800 | [diff] [blame] | 273 | sources = list(size_by_source.keys()) |
| 274 | sources.sort(key = lambda x: size_by_source[x].total()) |
xiaohu.huang | 0fb34ef | 2024-04-19 17:05:08 +0800 | [diff] [blame] | 275 | sumrom = sumram = sumcode = sumdata = sumbss = sumcustomize = sumrodata = 0 |
xiaohu.huang | 1f33774 | 2023-04-20 17:06:31 +0800 | [diff] [blame] | 276 | sys_mem_usage = SectionSize.system_stack + SectionSize.system_heap |
xiaohu.huang | 5728549 | 2024-06-26 15:47:07 +0800 | [diff] [blame] | 277 | print_codesize_module_text_with_rodata() |
| 278 | print_codesize_summary_text_with_rodata() |