blob: 88f53d63aa398e3ad0f2b9f128d2368fb3e857f7 [file] [log] [blame]
xiaohu.huangffae1712022-11-01 18:04:01 +08001#!/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
10from __future__ import print_function
11
12import sys
13import os
14import argparse
15
16parser = argparse.ArgumentParser(description='total size of each object file in an ld linker map.')
17parser.add_argument('map_file', help="A map file generated by passing -M/--print-map to ld during linking.")
18parser.add_argument('--combine', action='store_true',
19 help="All object files in an .a archive or in a directory are combined")
20args = parser.parse_args()
21
xiaohu.huang1f337742023-04-20 17:06:31 +080022class SectionSize:
xiaohu.huangffae1712022-11-01 18:04:01 +080023 text = 0
24 rela_text = 0
25 data = 0 # Including metadata like import tables
26 bss = 0
27 customize = 0
xiaohu.huang1f337742023-04-20 17:06:31 +080028 system_heap = 0
29 system_stack = 0
xiaohu.huang0fb34ef2024-04-19 17:05:08 +080030 rodata = 0
31 rom_usage = 0
32 ram_usage = 0
33
xiaohu.huangffae1712022-11-01 18:04:01 +080034 def total(self):
35 return self.text + self.data + self.bss
xiaohu.huang0fb34ef2024-04-19 17:05:08 +080036
37 def add_text_with_rodata(self, section, size):
38 if section.startswith('.text') or section.startswith('.rodata'):
xiaohu.huangffae1712022-11-01 18:04:01 +080039 self.text += size
xiaohu.huang0fb34ef2024-04-19 17:05:08 +080040 #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.huangffae1712022-11-01 18:04:01 +080044 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.huang0fb34ef2024-04-19 17:05:08 +080048 elif section.startswith('.data'):
xiaohu.huangffae1712022-11-01 18:04:01 +080049 self.data += size
50 elif section.startswith('.heap'):
xiaohu.huang1f337742023-04-20 17:06:31 +080051 SectionSize.system_heap += size
xiaohu.huangffae1712022-11-01 18:04:01 +080052 elif section.startswith('.stack'):
xiaohu.huang1f337742023-04-20 17:06:31 +080053 SectionSize.system_stack += size
xiaohu.huangffae1712022-11-01 18:04:01 +080054 else:
55 if (size > 0):
56 print("customer section:%s, size:%d" % (section, size))
57 self.customize += size
xiaohu.huang0fb34ef2024-04-19 17:05:08 +080058 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
86 def add_gcc_section(self, section, size):
87 if current_section is None:
88 return;
89 if section.startswith('.comment'):
90 return
91 if section.startswith('.debug'):
92 return
93 if section.startswith('.ARM.attributes'):
94 return
95
96 if os.getenv('ARCH') == "riscv":
97 self.add_text_with_rodata(section, size)
98 else:
99 self.add_data_with_rodata(section, size)
100
xiaohu.huang0fb34ef2024-04-19 17:05:08 +0800101
102def print_codesize_module_text_with_rodata():
103 print('---------------------------------------------------------------------------------------------------')
104 col_format = "%-20s\t%-12s\t%-12s\t%-10s%-10s\t%-12s\t%-12s\t%-7s"
105 print(col_format % ("module file", "ROM(text+data)", "RAM(data+bss)", ".text", "(.rodata)", ".data", ".bss", "customize"))
106 col_format = "%-20s\t%-12s\t%-12s\t%-10s(%-10s)\t%-12s\t%-12s\t%-7s"
107 for source in sources:
108 size = size_by_source[source]
109 print(col_format % (os.path.basename(source)[:20], size.rom_usage, size.ram_usage, size.text, size.rodata, size.data, size.bss, size.customize))
110 print('---------------------------------------------------------------------------------------------------')
111
112def print_codesize_module_data_with_rodata():
113 print('---------------------------------------------------------------------------------------------------')
114 col_format = "%-20s\t%-12s\t%-12s\t%-7s\t%-10s%-10s\t%-12s\t%-7s"
115 print(col_format % ("module file", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", "(.rodata)", ".bss", "customize"))
116 col_format = "%-20s\t%-12s\t%-12s\t%-7s\t%-10s(%-10s)\t%-12s\t%-7s"
117 for source in sources:
118 size = size_by_source[source]
119 print(col_format % (os.path.basename(source)[:20], size.rom_usage, size.ram_usage, size.text, size.data, size.rodata, size.bss, size.customize))
120 print('---------------------------------------------------------------------------------------------------')
121
122def print_codesize_summary_text_with_rodata():
123 global sumrom
124 global sumram
125 global sumcode
126 global sumdata
127 global sumbss
128 global sumcustomize
129 global sumrodata
130 global sys_mem_usage
131
132 for source in sources:
133 size = size_by_source[source]
134 sumcode += size.text
135 sumdata += size.data
136 sumbss += size.bss
137 sumrodata += size.rodata
138 sumrom += size.rom_usage
139 sumram += size.ram_usage
140 sumcustomize += size.customize
141
142 print('---------------------------------------------------------------------------------------------------')
143 col_format = "%-5s\t%-12s\t%-12s\t%-12s%-12s\t%-12s\t%-12s\t%-7s\t%-7s\t%-7s"
144 print(col_format % (" ", "ROM(text+data)", "RAM(data+bss)", ".text", "(.rodata)", ".data", ".bss", "cust", "stack", "heap" ))
145 col_format = "%-5s\t%-12s\t%-12s\t%-12s(%-12s)\t%-12s\t%-12s\t%-7s\t%-7s\t%-7s"
146 print(col_format % ("total", sumrom, sumram + sys_mem_usage, sumcode, sumrodata, sumdata, sumbss + sys_mem_usage, sumcustomize, SectionSize.system_stack, SectionSize.system_heap))
147 print('---------------------------------------------------------------------------------------------------')
148
149def print_codesize_summary_data_with_rodata():
150 global sumrom
151 global sumram
152 global sumcode
153 global sumdata
154 global sumbss
155 global sumcustomize
156 global sumrodata
157 global sys_mem_usage
158
159 for source in sources:
160 size = size_by_source[source]
161 sumcode += size.text
162 sumdata += size.data
163 sumbss += size.bss
164 sumrodata += size.rodata
165 sumrom += size.rom_usage
166 sumram += size.ram_usage
167 sumcustomize += size.customize
168
169 print('---------------------------------------------------------------------------------------------------')
170 col_format = "%-5s\t%-12s\t%-12s\t%-7s\t%-12s%-12s\t%-12s\t%-7s\t%-7s\t%-7s"
171 print(col_format % (" ", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", "(.rodata)", ".bss", "cust", "stack", "heap" ))
172 col_format = "%-5s\t%-12s\t%-12s\t%-7s\t%-12s(%-12s)\t%-12s\t%-7s\t%-7s\t%-7s"
173 print(col_format % ("total", sumrom, sumram + sys_mem_usage, sumcode, sumdata, sumrodata, sumbss + sys_mem_usage, sumcustomize, SectionSize.system_stack, SectionSize.system_heap))
174 print('---------------------------------------------------------------------------------------------------')
xiaohu.huangffae1712022-11-01 18:04:01 +0800175
176size_by_source = {}
xiaohu.huangffae1712022-11-01 18:04:01 +0800177with open(args.map_file) as f:
xiaohu.huang57285492024-06-26 15:47:07 +0800178 arch_toolchain = "GCC"
179 toolchain_keyword = "toolchains"
xiaohu.huange6c31672022-12-05 10:10:42 +0800180 print("%s toolchain map analyzer" % arch_toolchain)
181
xiaohu.huangffae1712022-11-01 18:04:01 +0800182 lines = iter(f)
183 for line in lines:
184 if line.strip() == "Linker script and memory map":
185 break
186
187 current_section = None
188 split_line = None
189 last_addr = 0
190 last_size = 0
191 for line in lines:
192 line = line.strip('\n')
193 if split_line:
194 # Glue a line that was split in two back together
195 if line.startswith(' ' * 16):
196 line = split_line + line
197 else: # Shouldn't happen
198 print("Warning: discarding line ", split_line)
199 split_line = None
200
201 if ('size before relaxing' in line):
202 continue
203 if line.startswith((".", " .", " *fill*")):
204 pieces = line.split(None, 3) # Don't split paths containing spaces
xiaohu.huang1f337742023-04-20 17:06:31 +0800205 pieces_num = len(pieces)
xiaohu.huangffae1712022-11-01 18:04:01 +0800206
207 if line.startswith("."):
208 # Note: this line might be wrapped, with the size of the section
209 # on the next line, but we ignore the size anyway and will ignore that line
210 current_section = pieces[0]
xiaohu.huange6c31672022-12-05 10:10:42 +0800211 # XCC text section format
xiaohu.huang1f337742023-04-20 17:06:31 +0800212 if (pieces_num == 4):
xiaohu.huange6c31672022-12-05 10:10:42 +0800213 source = pieces[-1]
xiaohu.huang1f337742023-04-20 17:06:31 +0800214 elif pieces_num == 1 and len(line) > 14:
xiaohu.huangffae1712022-11-01 18:04:01 +0800215 # ld splits the rest of this line onto the next if the section name is too long
216 split_line = line
xiaohu.huang1f337742023-04-20 17:06:31 +0800217 elif pieces_num >= 3 and "=" not in pieces and "before" not in pieces:
xiaohu.huangffae1712022-11-01 18:04:01 +0800218 if pieces[0] == "*fill*":
219 # fill use the last source to store the fill align data
220 #source = pieces[0]
221 size = int(pieces[-1], 16)
222 if (pieces[-2] == last_addr):
223 # sub the merged size from the same address for fill data
224 size = size - last_size
225 else:
226 last_size = size
227 last_addr = pieces[-2]
228 else:
229 source = pieces[-1]
230 size = int(pieces[-2], 16)
231 if (pieces[-3] == last_addr):
232 # sub the merged size from the same address for text data
233 size = size - last_size
234 else:
235 last_size = size
236 last_addr = pieces[-3]
237
238 if args.combine:
239 if '.a(' in source:
240 # path/to/archive.a(object.o)
241 source = source[:source.index('.a(') + 2]
242 elif '.dir' in source:
243 source = source[:source.find(".dir")]
244 elif source.endswith('.o'):
xiaohu.huange6c31672022-12-05 10:10:42 +0800245 if toolchain_keyword in source:
xiaohu.huangffae1712022-11-01 18:04:01 +0800246 source = 'toolchain_obj'
247 else:
248 source = os.path.basename(source)
249
250 if source not in size_by_source:
251 size_by_source[source] = SectionSize()
xiaohu.huang57285492024-06-26 15:47:07 +0800252 size_by_source[source].add_gcc_section(current_section, size)
xiaohu.huangffae1712022-11-01 18:04:01 +0800253
xiaohu.huangffae1712022-11-01 18:04:01 +0800254sources = list(size_by_source.keys())
255sources.sort(key = lambda x: size_by_source[x].total())
xiaohu.huang0fb34ef2024-04-19 17:05:08 +0800256sumrom = sumram = sumcode = sumdata = sumbss = sumcustomize = sumrodata = 0
xiaohu.huang1f337742023-04-20 17:06:31 +0800257sys_mem_usage = SectionSize.system_stack + SectionSize.system_heap
xiaohu.huang57285492024-06-26 15:47:07 +0800258if os.getenv('ARCH') == "riscv":
xiaohu.huang0fb34ef2024-04-19 17:05:08 +0800259 print_codesize_module_text_with_rodata()
260 print_codesize_summary_text_with_rodata()
261else:
xiaohu.huang57285492024-06-26 15:47:07 +0800262 print_codesize_module_data_with_rodata()
263 print_codesize_summary_data_with_rodata()