blob: c19391beeb94b838cf7a3edef18ce2a280f2b8eb [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
22class SectionSize():
23 text = 0
24 rela_text = 0
25 data = 0 # Including metadata like import tables
26 bss = 0
27 customize = 0
28 heap = 0
29 stack = 0
30 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'):
52 self.heap += size
53 self.bss += size
54 elif section.startswith('.stack'):
55 self.stack += size
56 self.bss += size
57 else:
58 if (size > 0):
59 print("customer section:%s, size:%d" % (section, size))
60 self.customize += size
xiaohu.huange6c31672022-12-05 10:10:42 +080061 def add_xcc_section(self, section, size):
62 if section.startswith('.comment'):
63 return
64 if section.startswith('.debug'):
65 return
66 if section.startswith('.xt.prop')or section.startswith('.xt.lit'):
67 return
68 if section.startswith('.text') or section.endswith('.text') \
69 or section.startswith('.literal') or section.endswith('.literal') \
70 or section.startswith('.rodata') or section.endswith('.rodata'):
71 self.text += size
72 elif section.startswith('.rela.dyn'):
73 self.rela_text += size
74 elif section.startswith('.bss') or section.startswith('.common') or section.startswith('.sbss'):
75 self.bss += size
76 elif section.startswith('.data') or section.endswith('.data'):
77 self.data += size
78 elif section.startswith('.heap'):
79 self.heap += size
80 self.bss += size
81 elif section.startswith('.stack'):
82 self.stack += size
83 self.bss += size
84 else:
85 if (size > 0):
86 print("customer section:%s, size:%d" % (section, size))
87 self.customize += size
xiaohu.huangffae1712022-11-01 18:04:01 +080088
89size_by_source = {}
xiaohu.huangffae1712022-11-01 18:04:01 +080090with open(args.map_file) as f:
xiaohu.huang29fd84c2022-12-19 15:27:00 +080091 if os.getenv('COMPILER') == "xcc":
xiaohu.huange6c31672022-12-05 10:10:42 +080092 arch_toolchain = "XCC"
93 toolchain_keyword = "xtensa-elf"
94 is_xtensa = 1
95 else:
96 arch_toolchain = "GCC"
97 toolchain_keyword = "toolchains"
98 is_xtensa = 0
99 print("%s toolchain map analyzer" % arch_toolchain)
100
xiaohu.huangffae1712022-11-01 18:04:01 +0800101 lines = iter(f)
102 for line in lines:
103 if line.strip() == "Linker script and memory map":
104 break
105
106 current_section = None
107 split_line = None
108 last_addr = 0
109 last_size = 0
110 for line in lines:
111 line = line.strip('\n')
112 if split_line:
113 # Glue a line that was split in two back together
114 if line.startswith(' ' * 16):
115 line = split_line + line
116 else: # Shouldn't happen
117 print("Warning: discarding line ", split_line)
118 split_line = None
119
120 if ('size before relaxing' in line):
121 continue
122 if line.startswith((".", " .", " *fill*")):
123 pieces = line.split(None, 3) # Don't split paths containing spaces
124
125 if line.startswith("."):
126 # Note: this line might be wrapped, with the size of the section
127 # on the next line, but we ignore the size anyway and will ignore that line
128 current_section = pieces[0]
xiaohu.huange6c31672022-12-05 10:10:42 +0800129 # XCC text section format
130 if (pieces == 4):
131 source = pieces[-1]
xiaohu.huangffae1712022-11-01 18:04:01 +0800132 elif len(pieces) == 1 and len(line) > 14:
133 # ld splits the rest of this line onto the next if the section name is too long
134 split_line = line
135 elif len(pieces) >= 3 and "=" not in pieces and "before" not in pieces:
136 if pieces[0] == "*fill*":
137 # fill use the last source to store the fill align data
138 #source = pieces[0]
139 size = int(pieces[-1], 16)
140 if (pieces[-2] == last_addr):
141 # sub the merged size from the same address for fill data
142 size = size - last_size
143 else:
144 last_size = size
145 last_addr = pieces[-2]
146 else:
147 source = pieces[-1]
148 size = int(pieces[-2], 16)
149 if (pieces[-3] == last_addr):
150 # sub the merged size from the same address for text data
151 size = size - last_size
152 else:
153 last_size = size
154 last_addr = pieces[-3]
155
156 if args.combine:
157 if '.a(' in source:
158 # path/to/archive.a(object.o)
159 source = source[:source.index('.a(') + 2]
160 elif '.dir' in source:
161 source = source[:source.find(".dir")]
162 elif source.endswith('.o'):
xiaohu.huange6c31672022-12-05 10:10:42 +0800163 if toolchain_keyword in source:
xiaohu.huangffae1712022-11-01 18:04:01 +0800164 source = 'toolchain_obj'
165 else:
166 source = os.path.basename(source)
167
168 if source not in size_by_source:
169 size_by_source[source] = SectionSize()
xiaohu.huange6c31672022-12-05 10:10:42 +0800170 if is_xtensa == 1:
171 size_by_source[source].add_xcc_section(current_section, size)
172 else:
173 size_by_source[source].add_gcc_section(current_section, size)
xiaohu.huangffae1712022-11-01 18:04:01 +0800174
175# Print out summary
176sources = list(size_by_source.keys())
177sources.sort(key = lambda x: size_by_source[x].total())
178sumrom = sumram = sumcode = sumdata = sumbss = sumstack = sumheap = sumcustomize = 0
179
180print('---------------------------------------------------------------------------------------------------')
181col_format = "%-20s\t%-12s\t%-12s\t%-7s\t%-12s\t%-12s\t%-7s"
182print(col_format % ("module file", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", ".bss", "customize"))
183for source in sources:
184 size = size_by_source[source]
185 sumcode += size.text
186 sumdata += size.data
187 sumbss += size.bss
188 sumrom += size.rom()
189 sumram += size.ram()
190 sumstack += size.stack
191 sumheap += size.heap
192 sumcustomize += size.customize
193 print(col_format % (os.path.basename(source), size.rom(), size.ram(), size.text, size.data, size.bss, size.customize))
194
195print('---------------------------------------------------------------------------------------------------')
196col_format = "%-5s\t%-12s\t%-12s\t%-7s\t%-12s\t%-12s\t%-7s\t%-7s\t%-7s"
197print(col_format % (" ", "ROM(text+data)", "RAM(data+bss)", ".text", ".data", ".bss", "cust", "stack", "heap" ))
198print(col_format % ("total", sumrom, sumram, sumcode, sumdata, sumbss, sumcustomize, sumstack, sumheap))
199print('---------------------------------------------------------------------------------------------------')