-
Notifications
You must be signed in to change notification settings - Fork 57
/
rtems-kern-symbols
executable file
·505 lines (449 loc) · 15.7 KB
/
rtems-kern-symbols
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#! /usr/bin/env python
# SPDX-License-Identifier: BSD-2-Clause
"""RTEMS LibBBSD Kernel Symbols
Generate the symbols for the kernel headers and merge in any new ones
"""
#
# Copyright (C) 2021 Chris Johns <chrisj@rtems.org>, All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
from __future__ import print_function
import argparse
import os
import re
import sys
version = "1.0"
kern_objects = [
( 'freebsd/sys', '.*\.o' ),
( 'rtemsbsd/rtems', 'rtems-kernel-.*\.o' )
] # yapf: disable
kern_excludes = [
'^rtems_',
'^accept$',
'^arc4random$',
'^bind$',
'^blackhole$',
'^bootverbose$',
'^bpf_filter$',
'^bpf_jitter$',
'^bpf_jitter_enable$',
'^bpf_validate$',
'^cache_enter$',
'^connect$',
'^drop_redirect$',
'^drop_synfin$',
'^free$',
'^getentropy$',
'^getpeername$',
'^getsockname$',
'^getsockopt$',
'^global_epoch$',
'^global_epoch_preempt$',
'^ifqmaxlen$',
'^in6addr_any$',
'^in6addr_linklocal_allnodes$',
'^in6addr_loopback$',
'^in6addr_nodelocal_allnodes$',
'^in_epoch$',
'^kevent$',
'^kqueue$',
'^listen$',
'^malloc$',
'^max_datalen$',
'^max_hdr$',
'^max_linkhdr$',
'^max_protohdr$',
'^maxsockets$',
'^nd6_debug$',
'^nd6_delay$',
'^nd6_gctimer$',
'^nd6_maxnudhint$',
'^nd6_mmaxtries$',
'^nd6_onlink_ns_rfc4861$',
'^nd6_prune$',
'^nd6_umaxtries$',
'^nd6_useloopback$',
'^net_epoch$',
'^net_epoch_preempt$',
'^nmbclusters$',
'^nmbjumbo16$',
'^nmbjumbo9$',
'^nmbjumbop$',
'^nmbufs$',
'^nolocaltimewait$',
'^path_mtu_discovery$',
'^pause$',
'^pf_osfp_entry_pl$',
'^pf_osfp_pl$',
'^pipe$',
'^poll$',
'^pselect$',
'^random$',
'^realloc$',
'^reallocf$',
'^recvfrom$',
'^recvmsg$',
'^rtems',
'^select$',
'^sendmsg$',
'^sendto$',
'^setfib$',
'^setsockopt$',
'^shutdown$',
'^socket$',
'^socketpair$',
'^soreceive_stream$',
'^srandom$',
'^strdup$',
'^sysctlbyname$',
'^sysctl$',
'^sysctlnametomib$',
'sys_init',
'^taskqueue_',
'^tcp_offload_listen_start$',
'^tcp_offload_listen_stop$',
'^ticks$',
'^useloopback$',
'^_Watchdog_Ticks_since_boot$'
] # yapf: disable
kern_header = 'rtemsbsd/include/machine/rtems-bsd-kernel-namespace.h'
class exit(Exception):
"""Base class for exceptions."""
def __init__(self, code):
self.code = code
class error(Exception):
"""Base class for exceptions."""
def set_output(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class general_error(error):
"""Raise for a general error."""
def __init__(self, what):
self.set_output('error: ' + str(what))
class command:
def __init__(self, cmd, cwd='.'):
self.exit_code = 0
self.output = None
self.cmd = cmd
self.cwd = cwd
self.result = None
def run(self):
import subprocess
#
# Support Python 2.6
#
if "check_output" not in dir(subprocess):
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError(
'stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE,
*popenargs,
**kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
self.exit_code = 0
try:
if os.name == 'nt':
cmd = ['sh', '-c'] + self.cmd
else:
cmd = self.cmd
output = subprocess.check_output(cmd,
cwd=self.cwd).decode("utf-8")
self.output = output.split(os.linesep)
except subprocess.CalledProcessError as cpe:
self.exit_code = cpe.returncode
output = cpe.output.decode("utf-8")
self.output = output.split(os.linesep)
except OSError as ose:
cs = ' '.join(cmd)
if len(cs) > 80:
cs = cs[:80] + '...'
raise general_error('bootstrap failed: %s in %s: %s' % \
(cs, self.cwd, (str(ose))))
except KeyboardInterrupt:
pass
except:
raise
class kernel_symbols:
def __init__(self, excludes):
self.bsd_tag = '_bsd_'
self.excludes = [re.compile(exc) for exc in excludes]
self.bsps = {}
self.header = {'source': [], 'symbols': []}
self.output = {'source': [], 'symbols': []}
self.analysis = {'mapped': [], 'unmapped': [], 'new': []}
@staticmethod
def _find(base, spec):
found = []
filter = re.compile(spec[1])
for root, dirs, files in os.walk(os.path.join(base, spec[0]),
topdown=True):
for f in files:
if filter.match(f):
found += [os.path.join(root, f)]
return found
@staticmethod
def _find_bsps(build):
bsps = []
filter = re.compile('^.*-rtems[0-9].*-.*')
for name in os.listdir(build):
if os.path.isdir(os.path.join(build, name)) and \
filter.match(name) != None:
bsps += [name]
return bsps
@staticmethod
def bsp_arch(bsp):
bs = bsp.split('-')
return bs[0] + '-' + bs[1]
def _clean(self, symbols):
syms = []
for sym in symbols:
add = True
for exclude in self.excludes:
if exclude.search(sym) is not None:
add = False
break
if add:
syms += [sym]
return sorted(list(set(syms)))
def load_header(self, header):
with open(header, 'r') as h:
self.header['source'] = h.read().splitlines()
filter = re.compile('^#define\s')
for line in self.header['source']:
if filter.match(line) != None:
ls = line.split()
if len(ls) == 3:
self.header['symbols'] += [ls[1]]
self.header['symbols'] = self._clean(self.header['symbols'])
def load_symbols(self, specs, excludes, build, tools):
bsps = self._find_bsps(build)
for bsp in bsps:
self.bsps[bsp] = {'output': [], 'objects': [], 'symbols': []}
for spec in specs:
self.bsps[bsp]['objects'] += self._find(
os.path.join(build, bsp), spec)
arch = self.bsp_arch(bsp)
if tools is not None:
cmd = os.path.join(tools, 'bin', arch + '-nm')
else:
cmd = arch + '-nm'
nm = command([cmd] + self.bsps[bsp]['objects'])
nm.run()
self.bsps[bsp]['output'] = nm.output
object = '-'
syms = []
for line in nm.output:
if len(line) == 0:
continue
if line[-1] == ':':
object = os.path.basename(line[:-1])
continue
ls = line.split()
if len(ls) == 3:
ls = ls[1:]
if ls[0] in ['A', 'B', 'C', 'D', 'R', 'T', 'W']:
sym = ls[1]
if sym.startswith(self.bsd_tag):
sym = sym[len(self.bsd_tag):]
if sym in syms:
print('warning: duplicate symbol: %s:%s: %s (%s)' %
(bsp, object, sym, ls[1]))
syms += [sym]
self.bsps[bsp]['symbols'] += syms
def generate_header(self):
self.output['source'] = [
'/*', '* RTEMS Libbsd, this file is generated. Do not edit.', '*/',
'#ifndef _RTEMS_BSD_MACHINE_RTEMS_BSD_KERNEL_SPACE_H_',
'#error "the header file <machine/rtems-bsd-kernel-space.h> must be included first"',
'#endif', ''
] + ['#define\t%s %s%s' % (sym, self.bsd_tag, sym) for sym in self.output['symbols']]
def write_header(self, header):
with open(header, 'wb') as o:
o.write(
os.linesep.join(self.output['source'] + ['']).encode("utf-8"))
def write_sym_data(self):
for bsp in self.bsps:
arch = self.bsp_arch(bsp)
with open('sym-data-' + arch + '.txt', 'w') as o:
o.writelines(os.linesep.join(self.bsps[bsp]['output']))
def merge(self, symbols):
self.output['symbols'] = \
self._clean(self.output['symbols'] + symbols)
def merge_bsp(self):
for bsp in self.bsps:
self.merge(self.bsps[bsp]['symbols'])
def analyse(self):
for bsp in self.bsps:
for sym in self.bsps[bsp]['symbols']:
if sym in self.header['symbols']:
key = 'mapped'
else:
key = 'new'
self.analysis[key] += [sym]
for key in self.analysis:
self.analysis[key] = self._clean(self.analysis[key])
self.analysis['unmapped'] = [sym for sym in self.header['symbols'] if sym not in self.analysis['mapped']]
def diff(self):
import difflib
return list(
difflib.unified_diff(self.header['source'], self.output['source']))
def report(self):
out = [
'Symbols:',
' header : %d' % (len(self.header['symbols'])),
' mapped : %d' % (len(self.analysis['mapped'])),
' unmapped : %d' % (len(self.analysis['unmapped'])),
' new : %d' % (len(self.analysis['new']))
]
max_len = 0
for bsp in self.bsps:
if max_len < len(bsp):
max_len = len(bsp)
out += ['BSPs: %*s Unmapped Total' % (max_len - 4, ' ')]
for bsp in self.bsps:
unmapped = len(self._clean(self.bsps[bsp]['symbols']))
total = len(self.bsps[bsp]['symbols'])
out += [' %-*s: %-8d %d' % (max_len, bsp, unmapped, total)]
out += ['New:'] + [' ' + sym for sym in self.analysis['new']]
out += ['Unmapped:'] + [' ' + sym for sym in self.analysis['unmapped']]
return out
def run(args):
try:
argsp = argparse.ArgumentParser(
prog='rtems-kern-symbols',
description="RTEMS LibBSD Kernel Symbols")
argsp.add_argument('-t',
'--rtems-tools',
help='RTEMS Tools (default: %(default)s).',
type=str,
default=None)
argsp.add_argument(
'-w',
'--write',
action='store_true',
help=
'Write the header to the output file name (default: %(default)s).')
argsp.add_argument(
'-d',
'--diff',
action='store_true',
help='Show a diff if the header has changed (default: %(default)s).'
)
argsp.add_argument(
'-o',
'--output',
type=str,
default=kern_header,
help='output path to the write the header (default: %(default)s).')
argsp.add_argument(
'-b',
'--build',
type=str,
default='build',
help='path to the rtems libbsd build output (default: %(default)s).'
)
argsp.add_argument(
'-K',
'--kern-header',
type=str,
default=kern_header,
help=
'kernel header to load existing symbols from(default: %(default)s).'
)
argsp.add_argument(
'-S',
'--sym-data',
action="store_true",
help=
'Write the BSP symbol data that is parsed (default: %(default)s).')
argsp.add_argument(
'-r',
'--regenerate',
action="store_true",
help=
'Regenerate the header file from the symbols in it, write option ignored (default: %(default)s).'
)
argsp.add_argument('-R',
'--report',
action="store_true",
help='Generate a report (default: %(default)s).')
argopts = argsp.parse_args(args[1:])
print('RTEMS LibBSD Kernel Symbols, %s' % (version))
if not os.path.exists(argopts.build):
raise general_error('path does not exist: %s' % (argopts.build))
ks = kernel_symbols(kern_excludes)
ks.load_header(argopts.kern_header)
if argopts.regenerate:
ks.merge(ks.header['symbols'])
print('Regenerating: symbols: %d: %s' %
(len(ks.output['symbols']), argopts.output))
ks.generate_header()
diff = ks.diff()
if len(diff) == 0:
print('info: no changes; header not updated')
else:
print('info: writing: %s' % (argopts.output))
ks.write_header(argopts.output)
raise exit(0)
ks.load_symbols(kern_objects, kern_excludes, argopts.build,
argopts.rtems_tools)
if argopts.sym_data:
ks.write_sym_data()
ks.analyse()
ks.merge(ks.header['symbols'])
ks.merge_bsp()
ks.generate_header()
diff = ks.diff()
if argopts.write:
if len(diff) == 0:
print('info: no changes; header not updated')
else:
print('info: writing: %s' % (argopts.output))
ks.write_header(argopts.output)
if argopts.report:
print(os.linesep.join(ks.report()))
if argopts.diff:
print('Diff: %d' % (len(diff)))
print(os.linesep.join(diff))
except general_error as gerr:
print(gerr)
print('RTEMS Kernel Symbols FAILED', file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:
log.notice('abort: user terminated')
sys.exit(1)
except exit as ec:
sys.exit(ec.code)
sys.exit(0)
if __name__ == "__main__":
run(sys.argv)