Repository navigation
Expand file tree
/
Copy pathcosy.py
More file actions
executable file
·313 lines (251 loc) · 9.12 KB
/
Copy pathcosy.py
File metadata and controls
executable file
·313 lines (251 loc) · 9.12 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Hauke Petersen <dev@haukepetersen.de>
#
# 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 <http://www.gnu.org/licenses/>.
import sys
from os import path
import argparse
import re
import subprocess
import copy
def add_sym( target, sym ):
if sym and (sym['addr'] != 0 or sym['sym'] == 'vectors'):
target.append(copy.deepcopy(sym))
def size_init():
return {'t': 0, 'd': 0, 'b': 0, 'sum': 0}
def size_add( obj, sym ):
obj[sym['type']] += sym['size']
obj['sum'] += sym['size']
def print_shead():
print("%-60s %7s %7s %7s %7s %7s" % ("module", "text", "data", "bss", "dec", "hex"))
print("------------------------------------------------------------------------------------------")
def print_mod( name, size ):
print("%-60s %7i %7i %7i %7i %7x" % (name, size['t'], size['d'], size['b'], size['sum'], size['sum']))
def print_sum( obj ):
print("------------------------------------------------------------------------------------------")
print_mod("SUM", obj)
print("------------------------------------------------------------------------------------------")
print ""
def print_size( obj ):
print(" text data bss dec hex")
print("%7i %7i %7i %7i %7x" % (obj['t'], obj['d'], obj['b'], obj['sum'], obj['sum']))
def print_tree( depth, tree ):
print_shead()
print_subtree(depth, tree, 0)
print_sum(tree['size'])
def print_subtree( depth, tree, cur ):
if depth:
for t in tree:
if t != 'size':
ind = ""
for i in range(0, cur):
ind += ".... "
print_mod( ind + t, tree[t]['size'])
print_subtree( depth - 1, tree[t], cur + 1)
if __name__ == "__main__":
# Define some command line args
p = argparse.ArgumentParser()
p.add_argument("file", default="test", nargs="?", help="base file name, .map and .elf will be added")
p.add_argument("-p", default="", help="Toolchain prefix, e.g. arm-none-eabi-")
args = p.parse_args()
elffile = args.file + ".elf"
mapfile = args.file + ".map"
# Test if file exisists
if not path.isfile(elffile):
sys.exit("Error: ELF file '" + elffile + "' does not exist")
if not path.isfile(mapfile):
sys.exit("Error: MAP file '" + mapfile + "' does not exist")
nm_out = []
dump = subprocess.check_output(['nm', '--line-numbers', elffile])
for line in dump.splitlines():
m = re.match("([0-9a-f]+) ([tbdTDB]) ([_a-zA-Z0-9]+)[ \t]+.+/RIOT/(.+)/([-_a-zA-Z0-9]+\.[ch]):(\d+)$", line)
if m:
nm_out.append({
'sym': m.group(3),
'path': m.group(4).split('/'),
'file': m.group(5),
'line': int(m.group(6)),
'addr': int(m.group(1), 16),
'type': m.group(2).lower(),
'arcv': '',
'obj': '',
'size': -1,
'alias': []
})
map_out = []
cur_type = ''
cur_sym = {}
f = open(mapfile, 'r')
for line in f:
if re.match("^\.text", line):
cur_type = 't'
continue
if re.match("^\.(bss|stack)", line):
cur_type = 'b'
continue
if re.match("^\.(relocate|data)", line):
cur_type = 'd'
continue
if re.match("^OUTPUT.+", line):
add_sym(map_out, cur_sym)
break;
if cur_type:
# fill bytes?
m = re.match("^ *\*fill\* +0x([0-9a-f]+) +0x([0-9a-f])+", line)
if m:
add_sym(map_out, cur_sym)
cur_sym = {
'sym': 'fill',
'path': '',
'file': '',
'line': -1,
'addr': int(m.group(1), 16),
'type': cur_type,
'size': int(m.group(2), 16),
'arcv': '',
'obj': '',
'alias': []
}
# map_out.append(copy.deepcopy(cur_sym))
continue;
# start of a new symbol
m = re.match(" \.([a-z]+\.)?([-_\.A-Za-z0-9]+)", line)
if m:
# save last symbol
add_sym(map_out, cur_sym)
# reset current symbol
cur_sym = {
'sym': m.group(2),
'path': '',
'file': '',
'line': -1,
'addr': 0,
'type': cur_type,
'size': -1,
'arcv': '',
'obj': '',
'alias': []
}
# get size, addr and path of current symbol
m = re.match(".+0x([0-9a-f]+) +0x([0-9a-f]+) (/.+)$", line)
if m:
cur_sym['addr'] = int(m.group(1), 16)
cur_sym['size'] = int(m.group(2), 16)
# get object and archive files
me = re.match(".+/([-_a-zA-Z0-9]+\.a)\(([-_a-zA-Z0-9]+\.o)\)$", m.group(3))
if me:
cur_sym['arcv'] = me.group(1)
cur_sym['obj'] = me.group(2)
me = re.match(".+/([-_a-zA-Z0-9]+\.o)$", m.group(3))
if me:
cur_sym['arcv'] = ''
cur_sym['obj'] = me.group(1)
continue
m = re.match(" +0x[0-9a-f]+ +([-_a-zA-Z0-9]+)$", line)
if m:
cur_sym['alias'].append(m.group(1))
f.close()
wp = []
for sym in map_out:
if not sym['path']:
wp.append(sym)
print("Without path: %i" % len(wp))
# get paths from nm-dump output
for nm in nm_out:
for m in map_out:
if (nm['addr'] & 0xfffffffe) == m['addr']:
m['path'] = nm['path']
# fill in some known paths
for sym in map_out:
if sym['arcv'] == 'libc_s.a':
sym['path'] = ['newlib', 'libc']
elif sym['arcv'] == 'libgcc.a':
sym['path'] = ['newlib', 'libgcc']
elif sym['sym'] == 'fill':
sym['path'] = ['fill']
# try to map .a and .o files to known paths
otp = {}
for sym in map_out:
if sym['arcv'] and sym['path'] and sym['arcv'] not in otp:
otp[sym['arcv']] = sym['path']
for sym in map_out:
if not sym['path'] and sym['arcv'] and sym['arcv'] in otp:
sym['path'] = otp[sym['arcv']]
wp = []
for sym in map_out:
if not sym['path']:
wp.append(sym)
# print("%30s: a:%s, o:%s" % (sym['sym'], sym['arcv'], sym['obj']))
print("Without path: %i" % len(wp))
# print sizes on archive file base
sa = dict()
sm = size_init()
for sym in map_out:
if sym['arcv']:
k = sym['arcv']
elif sym['obj']:
k = sym['obj']
else:
k = sym['sym']
if k not in sa:
sa[k] = size_init()
size_add(sa[k], sym)
size_add(sm, sym)
print_shead()
for a in sorted(sa):
print_mod(a, sa[a])
print_sum(sm)
# print sizes on module (riot folder) base
sa = {'size': size_init()}
for sym in map_out:
size_add(sa['size'], sym)
tmp = sa
for d in sym['path']:
if d not in tmp:
tmp[d] = {'size': size_init()}
size_add(tmp[d]['size'], sym)
tmp = tmp[d]
# add symbol as leaf
if sym['sym'] not in tmp:
tmp[sym['sym']] = {'size': size_init()}
size_add(tmp[sym['sym']]['size'], sym)
print_tree(10, sa)
# print_shead()
# for a in sorted(sa):
# print_mod(a, sa[a]['size'])
# for b in sorted(sa[a]):
# if b != 'size':
# print_mod(a + '/' + b, sa[a][b]['size'])
# print ""
# print_sum(sm)
files = {
't': open('root/mem_t.csv', 'w'),
'd': open('root/mem_d.csv', 'w'),
'b': open('root/mem_b.csv', 'w'),
'sum': open('root/mem_sum.csv', 'w')
}
for sym in map_out:
line = ";".join(sym['path']) + ";" + sym['sym'] + "," + str(sym['size']) + '\n'
files[sym['type']].write(line)
files['sum'].write(line)
for t in files:
files[t].close()
res = {'t': 0, 'd': 0, 'b': 0, 'sum': 0}
for sym in map_out:
res[sym['type']] += sym['size']
res['sum'] += sym['size']
print_size(res)
# DEGBUG: output size results
print subprocess.check_output((args.p + 'size', elffile)),