forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizes.py
executable file
·188 lines (155 loc) · 5.04 KB
/
sizes.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
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
# Display the segment sizes used by an ELF
#
# Copyright (C) 2019 - Earle F. Philhower, III
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import os
import subprocess
import sys
import locale
sys.stdout = sys.stderr
# retrieve *system* encoding, not the one used by python internally
if sys.version_info >= (3, 11):
def get_encoding():
return locale.getencoding()
else:
def get_encoding():
return locale.getdefaultlocale()[1]
def get_segment_sizes(elf, path, mmu):
iram_size = 0
iheap_size = 0
icache_size = 32168
for line in mmu.split():
words = line.split("=")
if line.startswith("-DMMU_IRAM_SIZE"):
iram_size = int(words[1], 16)
elif line.startswith("-DMMU_ICACHE_SIZE"):
icache_size = int(words[1], 16)
elif line.startswith("-DMMU_SEC_HEAP_SIZE"):
iheap_size = int(words[1], 16)
sizes = [
[
"Variables and constants in RAM (global, static)",
[
{
"DATA": 0,
"RODATA": 0,
"BSS": 0,
},
80192,
],
],
[
"Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR)",
[
{
"ICACHE": icache_size,
"IHEAP": iheap_size,
"IRAM": 0,
},
65536,
],
],
["Code in flash (default, ICACHE_FLASH_ATTR)", [{"IROM": 0}, 1048576]],
]
section_mapping = (
(".irom0.text", "IROM"),
(".text", "IRAM"),
(".data", "DATA"),
(".rodata", "RODATA"),
(".bss", "BSS"),
)
cmd = [os.path.join(path, "xtensa-lx106-elf-size"), "-A", elf]
with subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True, encoding=get_encoding()) as proc:
lines = proc.stdout.readlines()
for line in lines:
words = line.split()
for section, target in section_mapping:
if not line.startswith(section):
continue
for group, (segments, total) in sizes:
if target in segments:
segments[target] += int(words[1])
assert segments[target] <= total
return sizes
def percentage(lhs, rhs):
return "{}%".format(int(100.0 * float(lhs) / float(rhs)))
HINTS = {
"ICACHE": "reserved space for flash instruction cache",
"IRAM": "code in IRAM",
"IHEAP": "secondary heap space",
"IROM": "code in flash",
"DATA": "initialized variables",
"RODATA": "constants",
"BSS": "zeroed variables",
}
def safe_prefix(n, length):
if n == length:
return "`--"
return "|--"
def prefix(n, length):
if n == length:
return "╚══"
return "╠══"
def filter_segments(segments):
used = 0
number = 0
available = []
for (segment, size) in segments.items():
if not size:
continue
used += size
number += 1
available.append((number, segment, size))
return (number, used, available)
def main():
parser = argparse.ArgumentParser(
description="Report the different segment sizes of a compiled ELF file"
)
parser.add_argument(
"-e",
"--elf",
action="store",
required=True,
help="Path to the Arduino sketch ELF",
)
parser.add_argument(
"-p",
"--path",
action="store",
required=True,
help="Path to Xtensa toolchain binaries",
)
parser.add_argument(
"-i", "--mmu", action="store", required=False, help="MMU build options"
)
args = parser.parse_args()
sizes = get_segment_sizes(args.elf, args.path, args.mmu)
for group, (segments, total) in sizes:
number, used, segments = filter_segments(segments)
print(f". {group:<8}, used {used} / {total} bytes ({percentage(used, total)})")
try:
print("║ SEGMENT BYTES DESCRIPTION")
except UnicodeEncodeError:
print("| SEGMENT BYTES DESCRIPTION")
for n, segment, size in segments:
try:
print(f"{prefix(n, number)} ", end="")
except UnicodeEncodeError:
print(f"{safe_prefix(n, number)} ", end="")
print(f"{segment:<8} {size:<8} {HINTS[segment]:<16}")
if __name__ == "__main__":
main()