aboutsummaryrefslogtreecommitdiff
blob: e43db2784c0f164be5d4af663c91fa8042979242 (plain)
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
#!/usr/bin/env python

import sys
import getopt
import os
import subprocess
import re
import pax

def get_ldd_linkings(binary):

	try:
		#TODO: when subprocess.DEVNULL makes it to python, change this: http://bugs.python.org/issue5870
		ldd_output = subprocess.check_output(['/usr/bin/ldd', binary], stderr=subprocess.PIPE)
	except:
		#TODO: we should record these binaries which are probably statically linked
		return []

	ldd_lines = ldd_output.split('\n')

	linkings = []
	mappings = {}
	for m in range(0,len(ldd_lines)):
		if not re.search('=>', ldd_lines[m]):
			continue
		ldd_lines[m] = ldd_lines[m].strip()
		mapp = re.split('=>', ldd_lines[m] )
		soname = mapp[0].strip()
		soname = os.path.basename(soname)	# This is for ./libSDL-1.2.so.0
		library = re.sub('\(.*$', '', mapp[1]).strip()
		if library == '':
			continue
		library = os.path.realpath(library)
		linkings.append(soname)
		mappings[soname] = library 

	return ( linkings, mappings )


def get_forward_linkings():
	#TODO: I'm still not sure we wan to use /var/db/pkg vs some path of binaries
	var_db_pkg = '/var/db/pkg'

	forward_linkings = {}
	so2library_mappings = {}
	for cat in os.listdir(var_db_pkg):
		catdir = '%s/%s' % (var_db_pkg, cat)
		for pkg in os.listdir(catdir):
			pkgdir = '%s/%s' % (catdir, pkg)
			need = '%s/%s' % (pkgdir, 'NEEDED')
			try:
				g = open(need, 'r')
				needs = g.readlines()
				for line in needs:
					line = line.strip()
					link = re.split('\s', line)
					binary = link[0]
					( linkings, mappings ) = get_ldd_linkings(binary)
					forward_linkings[binary] = linkings 
					so2library_mappings.update(mappings)
			except:
				continue

	return ( forward_linkings, so2library_mappings )


def invert_linkings( forward_linkings ):
	reverse_linkings = {}
	for binary in forward_linkings:
		for library in forward_linkings[binary]:
			reverse_linkings[library] = []

	for binary in forward_linkings:
		for library in forward_linkings[binary]:
			reverse_linkings[library].append(binary)

	return reverse_linkings 


def print_forward_linkings( forward_linkings, so2library_mappings, verbose ):
	missing_binaries = []
	missing_links = []
	for binary in forward_linkings:

		try:
			binary_flags = pax.getflags(binary)
			sv = "%s ( %s )" % ( binary, binary_flags )
			s = sv
		except:
			missing_binaries.append(binary)
			continue

		count = 0
		for soname in forward_linkings[binary]:
			try:
				library = so2library_mappings[soname]
				library_flags = pax.getflags(library)
				sv = "%s\n\t%s\t%s ( %s )" % ( sv, soname, library, library_flags )
				if binary_flags != library_flags:
					s = "%s\n\t%s\t%s ( %s )" % ( s, soname, library, library_flags )
					count = count + 1
			except:
				missing_links.append(soname)

		if verbose:
			print sv
			print
			if count == 0:
				print
				print '\tNo mismatches'
				print
			else:
				print
				print '\tMismatches'
				print
		else:
			if count != 0:
				print s

	print

	missing_binaries = set(missing_binaries)
	print
	print
	print '**** Missing binaries ****'
	for m in missing_binaries:
		print m

	missing_links = set(missing_links)
	print
	print
	print '**** Missing forward linkings ****'
	for m in missing_links:
		print m

	print
	print


def print_reverse_linkings( reverse_linkings, so2library_mappings, verbose ):
	missing_sonames = []
	missing_links = []

	for soname in reverse_linkings:

		try:
			library = so2library_mappings[soname]
			library_flags = pax.getflags(library)
			sv = "%s\t%s ( %s )" % ( soname, library, library_flags )
			s = sv
		except:
			missing_sonames.append(soname)
			continue

		count = 0
		for binary in reverse_linkings[soname]:
			try:
				binary_flags = pax.getflags(binary)
				sv = "%s\n\t%s ( %s )" % ( sv, binary, binary_flags )
				if library_flags != binary_flags:
					s = "%s\n\t%s ( %s )" % ( s, binary, binary_flags )
					count = count + 1
			except:
				missing_links.append(binary)

		if verbose:
			print sv
			print
			if count == 0:
				print
				print '\tNo mismatches'
				print
			else:
				print
				print '\tMismatches'
				print
		else:
			if count != 0:
				print s

	print

	missing_sonames = set(missing_sonames)
	print
	print
	print '**** Missing sonames ****'
	for m in missing_sonames:
		print m

	missing_links = set(missing_links)
	print
	print
	print '**** Missing reverse linkings ****'
	for m in missing_links:
		print m
	print
	print


def run_usage():
	print 'Package Name : elfix'
	print 'Bug Reports  : http://bugs.gentoo.org/'
	print 'Program Name : revdep-pax'
	print 'Description  : Get or set pax flags on an ELF object'
	print
	print 'Usage        : revdep-pax [-fv] | [-rv] | -v [-b BINARY] | -v [-s SONAME] | -h'
	print
	print 'Options      : -f         print out all the forward mappings for all system binaries'
	print '             : -r         print out all the reverse mappints for all system sonames'
	print '             : -b BINARY  print all the forward mappings only for BINARY'
	print '             : -s SONAME  print all the reverse mappings only for SONAME'
	print '             : -v         verbose, otherwise just print mismatched pax flags'
	print '             : -h         print out this help'
	print


def run_forward(verbose):
	( forward_linkings, so2library_mappings ) = get_forward_linkings()
	print_forward_linkings( forward_linkings, so2library_mappings, verbose)
	print


def run_reverse(verbose):
	( forward_linkings, so2library_mappings ) = get_forward_linkings()
	reverse_linkings = invert_linkings( forward_linkings )
	print_reverse_linkings( reverse_linkings, so2library_mappings, verbose )
	print


def run_binary(binary, verbose):
	( linkings, mappings ) = get_ldd_linkings(binary)

	binary_flags = pax.getflags(binary)
	print binary, '(', binary_flags, ')'
	print

	count = 0
	for soname in linkings:
		try:
			library = mappings[soname]
			library_flags = pax.getflags(library)
			if verbose:
				print '\t', soname, '\t', library, '(', library_flags, ')'
			else:
				if binary_flags != library_flags:
					print '\t', soname, '\t', library, '(', library_flags, ')'
					count = count + 1
		except:
			print "file for soname %s not found" % soname

	if count == 0:
		print
		print '\tNo mismatches'
		print
	else:
		print
		print '\tMismatches'
		print


def invert_so2library_mappings( so2library_mappings ):
	library2soname_mappings = {}
	for soname, library in so2library_mappings.iteritems():
		library2soname_mappings[library] = soname
	return library2soname_mappings


def run_soname(name, verbose, use_soname):
	( forward_linkings, so2library_mappings ) = get_forward_linkings()
	reverse_linkings = invert_linkings( forward_linkings )

	if use_soname:
		soname = name
	else:
		library2soname_mappings = invert_so2library_mappings(so2library_mappings)
		soname = library2soname_mappings[name]

	linkings = reverse_linkings[soname]
	library = so2library_mappings[soname]

	library_flags = pax.getflags(library)
	print soname, '\t', library, '(', library_flags, ')'
	print

	count = 0
	for binary in linkings:
		try:
			binary_flags = pax.getflags(binary)
			if verbose:
				print '\t', binary, '(', binary_flags, ')'
			else:
				if library_flags != binary_flags:
					print '\t', binary, '(', binary_flags, ')'
					count = count + 1
		except:
			print "cannot obtain pax flags for %s" % binary

	if count == 0:
		print
		print '\tNo mismatches'
		print
	else:
		print
		print '\tMismatches'
		print


def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'hfrb:s:l:v')
	except getopt.GetoptError, err:
		print str(err) # will print something like 'option -a not recognized'
		run_usage()
		sys.exit(1)

	if len(opts) == 0:
		run_usage()
		sys.exit(1)

	do_usage   = False
	do_forward = False
	do_reverse = False

	binary = None
	soname = None
	library = None

	verbose = False

	for o, a in opts:
		if o == '-h':
			do_usage = True
		elif o == '-f':
			do_forward = True
		elif o == '-r':
			do_reverse = True
		elif o == '-b':
			binary = a
		elif o == '-s':
			soname = a
		elif o == '-l':
			library = a
		elif o == '-v':
			verbose = True
		else:
			print 'Option included in getopt but not handled here!'
			print 'Please file a bug'
			sys.exit(1)

	#TODO: Add code to only allow one of -h, -f -r -b -s

	if do_usage:
		run_usage()

	if do_forward:
		run_forward(verbose)

	if do_reverse:
		run_reverse(verbose)

	if binary != None:
		run_binary(binary, verbose)

	if soname != None:
		run_soname(soname, verbose, True)

	if library != None:
		library = os.path.realpath(library)
		run_soname(library, verbose, False)

if __name__ == '__main__':
    main()