This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
analyze_map.py
executable file
·176 lines (165 loc) · 7.59 KB
/
analyze_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
# Copyright (c) 2018 Ralph Versteegen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function
import sys
import os
import argparse
parser = argparse.ArgumentParser(description='Summarises the size of each object file in an ld linker map.')
parser.add_argument('map_file', help="A map file generated by passing -M/--print-map to ld during linking.")
parser.add_argument('--combine', action='store_true',
help="All object files in an .a archive or in a directory are combined")
args = parser.parse_args()
class SectionSize():
code = 0
data = 0 # Including metadata like import tables
def total(self):
return self.code + self.data
def add_section(self, section, size):
if section.startswith('.comment'):
return
if section.startswith('.debug'):
return
if section.startswith('.ARM.attributes'):
return
if section.startswith('.text'):
self.code += size
elif section != '.bss':
self.data += size
size_by_source = {}
with open(args.map_file) as f:
lines = iter(f)
for line in lines:
if line.strip() == "Linker script and memory map":
break
current_section = None
split_line = None
for line in lines:
line = line.strip('\n')
if split_line:
# Glue a line that was split in two back together
if line.startswith(' ' * 16):
line = split_line + line
else: # Shouldn't happen
print("Warning: discarding line ", split_line)
split_line = None
if line.startswith((".", " .", " *fill*")):
pieces = line.split(None, 3) # Don't split paths containing spaces
if line.startswith("."):
# Note: this line might be wrapped, with the size of the section
# on the next line, but we ignore the size anyway and will ignore that line
current_section = pieces[0]
elif len(pieces) == 1 and len(line) > 14:
# ld splits the rest of this line onto the next if the section name is too long
split_line = line
elif len(pieces) >= 3 and "=" not in pieces and "before" not in pieces:
if pieces[0] == "*fill*":
source = pieces[0]
size = int(pieces[-1], 16)
else:
source = pieces[-1]
size = int(pieces[-2], 16)
if args.combine:
if '.a(' in source:
# path/to/archive.a(object.o)
source = source[:source.index('.a(') + 2]
elif source.endswith('.o'):
where = max(source.rfind('\\'), source.rfind('/'))
if where:
source = source[:where + 1] + '*.o'
if source not in size_by_source:
size_by_source[source] = SectionSize()
size_by_source[source].add_section(current_section, size)
# Print out summary
sources = list(size_by_source.keys())
sources.sort(key = lambda x: size_by_source[x].total())
sumtotal = sumcode = sumdata = 0
sumtileset = sumevent = sumbanim = sumvoice = sumsfx = sumsong = sumtext = sumportrait = sumclasscard = summapsprite = sumchaptitle = sumbg = sumtitlescreen = 0
numtileset = numevent = numbanim = numvoice = numsfx = numsong = numportrait = numclasscard = nummapsprite = numchaptitle = numbg = numtitlescreen = 0
freespace = 16 * 1024 * 1024
for source in sources:
size = size_by_source[source]
sumcode += size.code
sumdata += size.data
sumtotal += size.total()
#print("%-40s \t%7s (code: %d data: %d)" % (os.path.normpath(source), size.total(), size.code, size.data))
if "src/res/map/tileset" in source:
sumtileset += size.data
numtileset += 1
elif "src/res/map/event/event_chap_" in source:
sumevent += size.data
numevent += 1
elif "src/res/animation" in source:
sumbanim += size.data
if "_sheets.c" not in source:
numbanim += 1
elif "res/voice/lib/libvoice.a" in source:
sumvoice += size.data
numvoice += 1
elif "res/se/lib/libse.a" in source:
sumsfx += size.data
numsfx += 1
elif "src/res/music" in source:
sumsong += size.data
numsong += 1
elif "src/text" in source:
sumtext += size.data
elif "src/res/gfx/portrait_" in source:
sumportrait += size.data
numportrait += 1
elif "src/res/gfx/classcard_" in source:
sumclasscard += size.data
numclasscard += 1
elif "src/res/gfx/standing_sprite_" in source:
summapsprite += size.data
nummapsprite += 1
elif "src/res/gfx/moving_sprite_" in source:
summapsprite += size.data
nummapsprite += 1
elif "src/res/chapter_title/chapter_title" in source:
sumchaptitle += size.data
numchaptitle += 1
elif "src/res/gfx/BG" in source:
sumbg += size.data
numbg += 1
elif "src/res/gfx/EvBg_" in source:
sumbg += size.data
numbg += 1
elif "src/res/gfx/title_screen_" in source:
sumtitlescreen += size.data
numtitlescreen += 1
print("TOTAL %d (code: %d data: %d)" % (sumtotal, sumcode, sumdata))
print("chapter map (%d): %d(%f%%)" % (numtileset, sumtileset, sumtileset / freespace * 100))
print("chapter title (%d): %d(%f%%)" % (numchaptitle, sumchaptitle, sumchaptitle / freespace * 100))
print("chapter event (%d): %d(%f%%)" % (numevent, sumevent, sumevent / freespace * 100))
print("battle animation (%d): %d(%f%%)" % (numbanim, sumbanim, sumbanim / freespace * 100))
print("character voice (%d): %d(%f%%)" % (numvoice, sumvoice, sumvoice / freespace * 100))
print("sound effect (%d): %d(%f%%)" % (numsfx, sumsfx, sumsfx / freespace * 100))
print("song (%d): %d(%f%%)" % (numsong, sumsong, sumsong / freespace * 100))
print("text: %d(%f%%)" % (sumtext, sumtext / freespace * 100))
print("background (%d): %d(%f%%)" % (numbg, sumbg, sumbg / freespace * 100))
print("portrait (%d): %d(%f%%)" % (numportrait / 2, sumportrait, sumportrait / freespace * 100))
print("class card (%d): %d(%f%%)" % (numclasscard, sumclasscard, sumclasscard / freespace * 100))
print("map sprite (%d): %d(%f%%)" % (nummapsprite, summapsprite, summapsprite / freespace * 100))
print("title screen (%d): %d(%f%%)" % (numtitlescreen, sumtitlescreen, sumtitlescreen / freespace * 100))
print("code: %d(%f%%)" % (sumcode, sumcode / freespace * 100))