blob: 49f5643dae3223f4208c6b6aafd0de4478ef53e8 [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.huangffae1712022-11-01 18:04:01 +080030 def rom(self):
31 return self.text + self.data
32 def ram(self):
33 return self.data + self.bss
34 def total(self):
35 return self.text + self.data + self.bss
xiaohu.huange6c31672022-12-05 10:10:42 +080036 def add_gcc_section(self, section, size):
xiaohu.huangffae1712022-11-01 18:04:01 +080037 if section.startswith('.comment'):
38 return
39 if section.startswith('.debug'):
40 return
41 if section.startswith('.ARM.attributes'):
42 return
43 if section.startswith('.text'):
44 self.text += size
45 elif section.startswith('.rela.dyn'):
46 self.rela_text += size
47 elif section.startswith('.bss') or section.startswith('.common') or section.startswith('.sbss'):
48 self.bss += size
49 elif section.startswith('.data') or section.startswith('.rodata'):
50 self.data += size
51 elif section.startswith('.heap'):
xiaohu.huang1f337742023-04-20 17:06:31 +080052 SectionSize.system_heap += size
xiaohu.huangffae1712022-11-01 18:04:01 +080053 elif section.startswith('.stack'):
xiaohu.huang1f337742023-04-20 17:06:31 +080054 SectionSize.system_stack += size
xiaohu.huangffae1712022-11-01 18:04:01 +080055 else:
56 if (size > 0):
57 print("customer section:%s, size:%d" % (section, size))
58 self.customize += size
xiaohu.huange6c31672022-12-05 10:10:42 +080059 def add_xcc_section(self, section, size):
60 if section.startswith('.comment'):
61 return
62 if section.startswith('.debug'):
63 return
64 if section.startswith('.xt.prop')or section.startswith('.xt.lit'):
65 return
66 if section.startswith('.text') or section.endswith('.text') \
67 or section.startswith('.literal') or section.endswith('.literal') \
68 or section.startswith('.rodata') or section.endswith('.rodata'):
69 self.text += size
70 elif section.startswith('.rela.dyn'):
71 self.rela_text += size
72 elif section.startswith('.bss') or section.startswith('.common') or section.startswith('.sbss'):
73 self.bss += size
74 elif section.startswith('.data') or section.endswith('.data'):
75 self.data += size
76 elif section.startswith('.heap'):
77 self.heap += size
78 self.bss += size
79 elif section.startswith('.stack'):
80 self.stack += size
81 self.bss += size
82 else:
83 if (size > 0):
84 print("customer section:%s, size:%d" % (section, size))
85 self.customize += size
xiaohu.huangffae1712022-11-01 18:04:01 +080086
87size_by_source = {}
xiaohu.huangffae1712022-11-01 18:04:01 +080088with open(args.map_file) as f:
xiaohu.huang29fd84c2022-12-19 15:27:00 +080089 if os.getenv('COMPILER') == "xcc":
xiaohu.huange6c31672022-12-05 10:10:42 +080090 arch_toolchain = "XCC"
91 toolchain_keyword = "xtensa-elf"
92 is_xtensa = 1
93 else:
94 arch_toolchain = "GCC"
95 toolchain_keyword = "toolchains"
96 is_xtensa = 0
97 print("%s toolchain map analyzer" % arch_toolchain)
98
xiaohu.huangffae1712022-11-01 18:04:01 +080099 lines = iter(f)
100 for line in lines:
101 if line.strip() == "Linker script and memory map":
102 break
103
104 current_section = None
105 split_line = None
106 last_addr = 0
107 last_size = 0
108 for line in lines:
109 line = line.strip('\n')
110 if split_line:
111 # Glue a line that was split in two back together
112 if line.startswith(' ' * 16):
113 line = split_line + line
114 else: # Shouldn't happen
115 print("Warning: discarding line ", split_line)
116 split_line = None
117
118 if ('size before relaxing' in line):
119 continue
120 if line.startswith((".", " .", " *fill*")):
121 pieces = line.split(None, 3) # Don't split paths containing spaces
xiaohu.huang1f337742023-04-20 17:06:31 +0800122 pieces_num = len(pieces)
xiaohu.huangffae1712022-11-01 18:04:01 +0800123
124 if line.startswith("."):
125 # Note: this line might be wrapped, with the size of the section
126 # on the next line, but we ignore the size anyway and will ignore that line
127 current_section = pieces[0]
xiaohu.huange6c31672022-12-05 10:10:42 +0800128 # XCC text section format
xiaohu.huang1f337742023-04-20 17:06:31 +0800129 if (pieces_num == 4):
xiaohu.huange6c31672022-12-05 10:10:42 +0800130 source = pieces[-1]
xiaohu.huang1f337742023-04-20 17:06:31 +0800131 elif pieces_num == 1 and len(line) > 14:
xiaohu.huangffae1712022-11-01 18:04:01 +0800132 # ld splits the rest of this line onto the next if the section name is too long
133 split_line = line
xiaohu.huang1f337742023-04-20 17:06:31 +0800134 elif pieces_num >= 3 and "=" not in pieces and "before" not in pieces:
xiaohu.huangffae1712022-11-01 18:04:01 +0800135 if pieces[0] == "*fill*":
136 # fill use the last source to store the fill align data
137 #source = pieces[0]
138 size = int(pieces[-1], 16)
139 if (pieces[-2] == last_addr):
140 # sub the merged size from the same address for fill data
141 size = size - last_size
142 else:
143 last_size = size
144 last_addr = pieces[-2]
145 else:
146 source = pieces[-1]
147 size = int(pieces[-2], 16)
148 if (pieces[-3] == last_addr):
149 # sub the merged size from the same address for text data
150 size = size - last_size
151 else:
152 last_size = size
153 last_addr = pieces[-3]
154
155 if args.combine:
156 if '.a(' in source:
157 # path/to/archive.a(object.o)
158 source = source[:source.index('.a(') + 2]
159 elif '.dir' in source:
160 source = source[:source.find(".dir")]
161 elif source.endswith('.o'):
xiaohu.huange6c31672022-12-05 10:10:42 +0800162 if toolchain_keyword in source:
xiaohu.huangffae1712022-11-01 18:04:01 +0800163 source = 'toolchain_obj'
164 else:
165 source = os.path.basename(source)
166
167 if source not in size_by_source:
168 size_by_source[source] = SectionSize()
xiaohu.huange6c31672022-12-05 10:10:42 +0800169 if is_xtensa == 1:
170 size_by_source[source].add_xcc_section(current_section, size)
171 else:
172 size_by_source[source].add_gcc_section(current_section, size)
xiaohu.huangffae1712022-11-01 18:04:01 +0800173
174# Print out summary
175sources = list(size_by_source.keys())
176sources.sort(key = lambda x: size_by_source[x].total())
xiaohu.huang1f337742023-04-20 17:06:31 +0800177sumrom = sumram = sumcode = sumdata = sumbss = sumcustomize = 0
xiaohu.huangffae1712022-11-01 18:04:01 +0800178
179print('---------------------------------------------------------------------------------------------------')
180col_format = "%-20s\t%-12s\t%-12s\t%-7s\t%-12s\t%-12s\t%-7s"
181print(col_format % ("module file", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", ".bss", "customize"))
182for source in sources:
183 size = size_by_source[source]
184 sumcode += size.text
185 sumdata += size.data
186 sumbss += size.bss
187 sumrom += size.rom()
188 sumram += size.ram()
xiaohu.huangffae1712022-11-01 18:04:01 +0800189 sumcustomize += size.customize
190 print(col_format % (os.path.basename(source), size.rom(), size.ram(), size.text, size.data, size.bss, size.customize))
191
192print('---------------------------------------------------------------------------------------------------')
193col_format = "%-5s\t%-12s\t%-12s\t%-7s\t%-12s\t%-12s\t%-7s\t%-7s\t%-7s"
xiaohu.huang1f337742023-04-20 17:06:31 +0800194sys_mem_usage = SectionSize.system_stack + SectionSize.system_heap
xiaohu.huangffae1712022-11-01 18:04:01 +0800195print(col_format % (" ", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", ".bss", "cust", "stack", "heap" ))
xiaohu.huang1f337742023-04-20 17:06:31 +0800196print(col_format % ("total", sumrom, sumram + sys_mem_usage, sumcode, sumdata, sumbss + sys_mem_usage, sumcustomize, SectionSize.system_stack, SectionSize.system_heap))
xiaohu.huangffae1712022-11-01 18:04:01 +0800197print('---------------------------------------------------------------------------------------------------')