-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdlopen-notes.py
More file actions
executable file
·334 lines (287 loc) · 10.5 KB
/
dlopen-notes.py
File metadata and controls
executable file
·334 lines (287 loc) · 10.5 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0
"""\
Read .note.dlopen notes from ELF files and report the contents.
"""
import argparse
import enum
import fnmatch
import functools
import json
import sys
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import NoteSection
try:
import rich
print_json = rich.print_json
except ImportError:
print_json = print
def dictify(f):
def wrap(*args, **kwargs):
return dict(f(*args, **kwargs))
return functools.update_wrapper(wrap, f)
def listify(f):
def wrap(*args, **kwargs):
return list(f(*args, **kwargs))
return functools.update_wrapper(wrap, f)
class ELFFileReader:
def __init__(self, filename):
self.filename = filename
self.elffile = ELFFile(open(filename, 'rb'))
@functools.cache
@listify
def notes(self):
for section in self.elffile.iter_sections():
if not isinstance(section, NoteSection) or section.name != '.note.dlopen':
continue
for note in section.iter_notes():
if note['n_type'] != 0x407c0c0a or note['n_name'] != 'FDO':
continue
note_desc = note['n_desc']
try:
# On older Python versions (e.g.: Ubuntu 22.04) we get a string, on
# newer versions a bytestring
if not isinstance(note_desc, str):
text = note_desc.decode('utf-8').rstrip('\0')
else:
text = note_desc.rstrip('\0')
except UnicodeDecodeError as e:
raise ValueError(f'{self.filename}: Invalid UTF-8 in .note.dlopen n_desc') from e
try:
j = json.loads(text)
except json.JSONDecodeError as e:
raise ValueError(f'{self.filename}: Invalid JSON in .note.dlopen note_desc') from e
if not isinstance(j, list):
print(f'{self.filename}: ignoring .note.dlopen n_desc with JSON that is not a list',
file=sys.stderr)
continue
yield from j
@dictify
def group_by_soname(elffiles):
for elffile in elffiles:
for element in elffile.notes():
priority = element.get('priority', 'recommended')
for soname in element['soname']:
yield soname, priority
class Priority(enum.Enum):
suggested = 1
recommended = 2
required = 3
def __lt__(self, other):
return self.value < other.value
def rpm_name(self):
if self == self.__class__.suggested:
return 'Suggests'
if self == self.__class__.recommended:
return 'Recommends'
if self == self.__class__.required:
return 'Requires'
raise ValueError
def group_by_feature(elffiles):
features = {}
# We expect each note to be in the format:
# [
# {
# "feature": "...",
# "description": "...",
# "priority": "required"|"recommended"|"suggested",
# "soname": ["..."],
# }
# ]
for elffiles in elffiles:
for note in elffiles.notes():
prio = Priority[note.get('priority', 'recommended')]
feature_name = note['feature']
try:
feature = features[feature_name]
except KeyError:
# Create new
feature = features[feature_name] = {
'description': note.get('description', ''),
'sonames': { soname:prio for soname in note['soname'] },
}
else:
# Merge
if feature['description'] != note.get('description', ''):
print(f"{note.filename}: feature {note['feature']!r} found with different description, ignoring",
file=sys.stderr)
for soname in note['soname']:
highest = max(feature['sonames'].get(soname, Priority.suggested),
prio)
feature['sonames'][soname] = highest
return features
def filter_features(features, filter):
if filter is None:
return None
ans = { name:feature for name,feature in features.items()
if name in filter or not filter }
if missing := set(filter) - set(ans):
sys.exit('Some features not found:', ', '.join(missing))
return ans
@listify
def generate_rpm(elffiles, stanza, filter):
# Produces output like:
# Requires: libqrencode.so.4()(64bit)
# Requires: libzstd.so.1()(64bit)
for elffile in elffiles:
suffix = '()(64bit)' if elffile.elffile.elfclass == 64 else ''
for note in elffile.notes():
if note['feature'] in filter or not filter:
soname = next(iter(note['soname'])) # we take the first — most recommended — soname
yield f"{stanza}: {soname}{suffix}"
def rpm_fileattr_generator(args):
if args.rpm_features is not None:
if not any(fnmatch.fnmatch(args.subpackage, pattern[0])
for pattern in args.rpm_features):
# Current subpackage is not listed, nothing to do.
# Consume all input as required by the protocol.
sys.stdin.read()
return
for file in sys.stdin:
file = file.strip()
if not file:
continue # ignore empty lines
elffile = ELFFileReader(file)
suffix = '()(64bit)' if elffile.elffile.elfclass == 64 else ''
first = True
for note in elffile.notes():
# Feature name is optional. Allow this to be matched
# by the empty string ('') or a wildcard glob ('*').
feature = note.get('feature', '')
if args.rpm_features is not None:
for package_pattern,feature_pattern in args.rpm_features:
if (fnmatch.fnmatch(args.subpackage, package_pattern) and
fnmatch.fnmatch(feature, feature_pattern)):
break
else:
# not matched
continue
else:
# if no mapping, print all features at the suggested level
level = Priority[note.get('priority', 'recommended')].rpm_name()
if level != args.rpm_fileattr:
continue
if first:
print(f';{file}')
first = False
soname = next(iter(note['soname'])) # we take the first — most recommended — soname
print(f'{soname}{suffix}')
def make_parser():
p = argparse.ArgumentParser(
description=__doc__,
allow_abbrev=False,
add_help=False,
epilog='If no option is specifed, --raw is the default.',
)
p.add_argument(
'-r', '--raw',
action='store_true',
help='Show the original JSON extracted from input files',
)
p.add_argument(
'-s', '--sonames',
action='store_true',
help='List all sonames and their priorities, one soname per line',
)
p.add_argument(
'-f', '--features',
nargs='?',
const=[],
type=lambda s: s.split(','),
action='extend',
metavar='FEATURE1,FEATURE2',
help='Describe features, can be specified multiple times',
)
p.add_argument(
'--rpm-requires',
nargs='?',
const=[],
type=lambda s: s.split(','),
action='extend',
metavar='FEATURE1,FEATURE2',
help='Generate rpm Requires for listed features',
)
p.add_argument(
'--rpm-recommends',
nargs='?',
const=[],
type=lambda s: s.split(','),
action='extend',
metavar='FEATURE1,FEATURE2',
help='Generate rpm Recommends for listed features',
)
p.add_argument(
'--rpm-fileattr',
metavar='TYPE',
help='Run as rpm fileattr generator for TYPE dependencies',
)
p.add_argument(
'--subpackage',
metavar='NAME',
default='',
help='Current subpackage NAME',
)
p.add_argument(
'--rpm-features',
metavar='SUBPACKAGE:FEATURE,SUBPACKAGE:FEATURE',
type=lambda s: [x.split(':', maxsplit=1) for x in s.split(',')],
action='extend',
help='Specify subpackage:feature mapping',
)
p.add_argument(
'filenames',
nargs='*',
metavar='FILENAME',
help='Library file to extract notes from',
)
p.add_argument(
'-h', '--help',
action='help',
help='Show this help message and exit',
)
return p
def parse_args():
args = make_parser().parse_args()
if (not args.raw
and not args.sonames
and args.features is None
and args.rpm_requires is None
and args.rpm_recommends is None
and args.rpm_fileattr is None):
# Make --raw the default if no action is specified.
args.raw = True
if args.rpm_fileattr is not None:
if (args.filenames
or args.raw
or args.features is not None
or args.rpm_requires
or args.rpm_recommends):
raise ValueError('--rpm-generate cannot be combined with most options')
if args.rpm_fileattr is None and not args.filenames:
raise ValueError('At least one positional FILENAME parameter is required')
return args
if __name__ == '__main__':
args = parse_args()
if args.rpm_fileattr is not None:
sys.exit(rpm_fileattr_generator(args))
elffiles = [ELFFileReader(filename) for filename in args.filenames]
features = group_by_feature(elffiles)
if args.raw:
for elffile in elffiles:
print(f'# {elffile.filename}')
print_json(json.dumps(elffile.notes(), indent=2))
if features_to_print := filter_features(features, args.features):
print('# grouped by feature')
print_json(json.dumps(features_to_print,
indent=2,
default=lambda prio: prio.name))
if args.rpm_requires is not None:
lines = generate_rpm(elffiles, 'Requires', args.rpm_requires)
print('\n'.join(lines))
if args.rpm_recommends is not None:
lines = generate_rpm(elffiles, 'Recommends', args.rpm_recommends)
print('\n'.join(lines))
if args.sonames:
sonames = group_by_soname(elffiles)
for soname in sorted(sonames.keys()):
print(f"{soname} {sonames[soname]}")