blob: 1d99cfa3d991aba49af379c18f910212c4cacee4 [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
xiaohu.huange6c31672022-12-05 10:10:42 +080086 def add_xcc_section(self, section, size):
xiaohu.huangfc139972024-01-23 10:36:50 +080087 if current_section is None:
88 return;
xiaohu.huange6c31672022-12-05 10:10:42 +080089 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.huang0fb34ef2024-04-19 17:05:08 +080098 #add the .rodata to text in order to match the result of size command with xcc's elf file
xiaohu.huange6c31672022-12-05 10:10:42 +080099 self.text += size
xiaohu.huang0fb34ef2024-04-19 17:05:08 +0800100 if section.startswith('.rodata') or section.endswith('.rodata'):
101 self.rodata += size
xiaohu.huange6c31672022-12-05 10:10:42 +0800102 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.huang0fb34ef2024-04-19 17:05:08 +0800118 self.rom_usage = (self.text + self.data)
119 self.ram_usage = (self.data + self.bss)
120
121def 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
131def 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
141def 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
168def 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.huangffae1712022-11-01 18:04:01 +0800194
195size_by_source = {}
xiaohu.huangffae1712022-11-01 18:04:01 +0800196with open(args.map_file) as f:
xiaohu.huang57285492024-06-26 15:47:07 +0800197 arch_toolchain = "XCC"
198 toolchain_keyword = "xtensa-elf"
xiaohu.huange6c31672022-12-05 10:10:42 +0800199 print("%s toolchain map analyzer" % arch_toolchain)
200
xiaohu.huangffae1712022-11-01 18:04:01 +0800201 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.huang1f337742023-04-20 17:06:31 +0800224 pieces_num = len(pieces)
xiaohu.huangffae1712022-11-01 18:04:01 +0800225
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.huange6c31672022-12-05 10:10:42 +0800230 # XCC text section format
xiaohu.huang1f337742023-04-20 17:06:31 +0800231 if (pieces_num == 4):
xiaohu.huange6c31672022-12-05 10:10:42 +0800232 source = pieces[-1]
xiaohu.huang1f337742023-04-20 17:06:31 +0800233 elif pieces_num == 1 and len(line) > 14:
xiaohu.huangffae1712022-11-01 18:04:01 +0800234 # ld splits the rest of this line onto the next if the section name is too long
235 split_line = line
xiaohu.huang1f337742023-04-20 17:06:31 +0800236 elif pieces_num >= 3 and "=" not in pieces and "before" not in pieces:
xiaohu.huangffae1712022-11-01 18:04:01 +0800237 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.huange6c31672022-12-05 10:10:42 +0800264 if toolchain_keyword in source:
xiaohu.huangffae1712022-11-01 18:04:01 +0800265 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.huang57285492024-06-26 15:47:07 +0800271 size_by_source[source].add_xcc_section(current_section, size)
xiaohu.huangffae1712022-11-01 18:04:01 +0800272
xiaohu.huangffae1712022-11-01 18:04:01 +0800273sources = list(size_by_source.keys())
274sources.sort(key = lambda x: size_by_source[x].total())
xiaohu.huang0fb34ef2024-04-19 17:05:08 +0800275sumrom = sumram = sumcode = sumdata = sumbss = sumcustomize = sumrodata = 0
xiaohu.huang1f337742023-04-20 17:06:31 +0800276sys_mem_usage = SectionSize.system_stack + SectionSize.system_heap
xiaohu.huang57285492024-06-26 15:47:07 +0800277print_codesize_module_text_with_rodata()
278print_codesize_summary_text_with_rodata()