summaryrefslogtreecommitdiff
path: root/usr/src/cmd/abi/appcert/scripts/abi_index.pl
blob: 61a4e19cc88673bd2aa629b0e7186c3e4f06f589 (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
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
#!/usr/perl5/bin/perl -w
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# This utility program reads the contents file to extract Solaris ELF
# libraries, and then runs pvs(1) on them to find the library versioning
# information (if any).  This info is printed to stdout in an index file
# format.
#

require 5.005;
use strict;
use locale;
use POSIX qw(locale_h);
use Sun::Solaris::Utils qw(textdomain gettext);
use File::Basename;

use vars qw(
	@liblist
	%symlink
	%inode_hash
	%fileoutput
	%didlib
);

setlocale(LC_ALL, "");
textdomain(TEXT_DOMAIN);

# parameters for what types of libraries to list out:
my $must_be_versioned = 0;
my $must_be_public = 0;

# paths to skip outright.
my @skip_list = qw(
	/etc
	/usr/perl5
);
my $path_skip = join('|', @skip_list);
$path_skip = qr/^($path_skip)/;

# find library names:
#
# We have to use pkgchk -l output (even though it is much slower than
# parsing /var/sadm/install/contents ourselves) because the contents
# file will go away or change incompatibly at some point.
#
my $old = $ENV{'LC_ALL'};
$ENV{'LC_ALL'} = 'C';
my $contents_fh = do { local *FH; *FH };
open($contents_fh, "/usr/sbin/pkgchk -l|") || die "$!\n";
if (defined($old)) {
	$ENV{'LC_ALL'} = $old;
} else {
	delete($ENV{'LC_ALL'});
}

my $pathname = '';
my $type = '';
my $link = '';
my $pkgs = '';
my $status = '';
my $inpkgs = 0;
while (<$contents_fh>) {
	next if (/^Ex/);
	chomp;
	if (/^Pathname:\s*/i) {
		$pathname = $';
		$type = '';
		$link = '';
		$status = '';
		$pkgs = '';
		$inpkgs = 0;
		next;
	} elsif (/^Type:\s*/i) {
		$type = $';
		next;
	} elsif (/^Source of link:\s*/i) {
		$link = $';
		next;
	} elsif (/^Referenced by/i) {
		$inpkgs = 1;
	} elsif (/^Current status:\s*/i) {
		$status = $';
		$inpkgs = 0;
		next;
	} elsif (/^\s*$/) {
		next unless ($pathname =~ m,\.so,);
		next unless ($pathname =~ m,/lib,);
		next unless ($pathname =~ m,/lib[^/]*\.so\b,);
		next unless ($type =~ /regular file|symbolic link/i);
		next unless ($status =~ /^\s*installed\s*$/);
		$pathname = trim($pathname);
		$link = trim($link);
		filter($pathname, $link, $pkgs);
	}
	if ($inpkgs) {
		$pkgs .= $_ . ' ';
	}
}
close($contents_fh);

# run pvs(1) on the libraries found:
my $batch = 30;	# batch size to use (running in batches is faster).

my @list = ();
for (my $i = 1; $i <= scalar(@liblist); $i++) {
	push(@list, $liblist[$i-1]);
	if ($i % $batch == 0) {
		do_pvs(@list) if (@list);
		@list = ();
	}
}
do_pvs(@list) if (@list);	# finish any remainder.

exit 0;

#
# Take a pkgchk -l entry and decide if it corresponds to a Solaris
# library. If so, save it in the list @liblist, and record info in
# %symlink & %inode_hash associative arrays as appropriate.
#
sub filter
{
	my ($path, $link, $pkgs) = @_;


	# consider only SUNW packages:
	return unless ($pkgs =~ /\bSUNW\S+/);

	my $basename;

	$basename = basename($path);

	if ($link ne '') {
		# include developer build-time symlinks:
		return unless ($basename =~ /^lib.*\.so[\.\d]*$/);
	} else {
		return unless ($basename =~ /^lib.*\.so\.[\.\d]+$/);
	}
	return if ($path =~ /$path_skip/);

	return unless (-f $path);

	# inode is used to identify what file a symlink point to:
	my $inode;
	$inode = (stat($path))[1];
	return unless (defined($inode));

	if ($link ne '') {
		# record info about symlinks:
		if (exists($symlink{$inode})) {
			$symlink{$inode} .= ":" . $path;
		} else {
			$symlink{$inode} = ":" . $path;
		}
	} else {
		# ordinary file case:
		$inode_hash{$path} = $inode;
		push(@liblist, $path);
	}
}

#
# Run pvs(1) on a list of libraries. More than one is done at a time to
# speed things up.
#
# Extracts the version information and passes it to the output() routine
# for final processing.
#
sub do_pvs
{
	my (@list) = @_;

	my (%list, $paths, $path, $cnt);

	#
	# record info about the library paths and construct the list of
	# files for the pvs command line.
	#
	$cnt = 0;
	$paths = '';
	foreach $path (@list) {
		$list{$path} = 1;
		$paths .= ' ' if ($paths ne '');
		#
		# $path should never have single quote in it in
		# all normal usage. Make sure this is so:
		#
		next if ($path =~ /'/);
		#
		# quote the filename in case it has meta-characters
		# (which should never happen in all normal usage)
		#
		$paths .= "'$path'";
		$cnt++;
	}

	return if ($cnt == 0);

	# set locale to C for running command, since we interpret the output:
	my $old = $ENV{'LC_ALL'};
	$ENV{'LC_ALL'} = 'C';

	# get the file(1) output for each item:
	my $file_fh = do { local *FH; *FH };
	open($file_fh, "/usr/bin/file $paths 2>&1 |") || die "$!\n";
	my ($file, $out);
	while (<$file_fh>) {
		($file, $out) = split(/:/, $_, 2);
		if ($list{$file} && $out =~ /\bELF\b/) {
			$fileoutput{$file} = $out;
		}
	}
	close($file_fh);

	#
	# in the case of only 1 item, we place it on the command line
	# twice to induce pvs(1) to indicate which file it is reporting
	# on.
	#
	if ($cnt == 1) {
		$paths .= " $paths";
	}

	#
	# $paths are entries from /var/sadm/install/contents and
	# so should not contain spaces or meta characters:
	#
	my $pvs_fh = do { local *FH; *FH };
	open($pvs_fh, "/usr/bin/pvs -dn $paths 2>&1 |") || die "$!\n";

	# reset LC_ALL, if there was any:
	if (defined($old)) {
		$ENV{'LC_ALL'} = $old;
	} else {
		delete($ENV{'LC_ALL'});
	}

	my ($pub, $pri, $obs, $evo, $vers, $new_path);

	undef($path);

	# initialize strings used below for appending info to:
	$pub = '';
	$pri = '';
	$obs = '';
	$evo = '';

	while (<$pvs_fh>) {
		$_ =~ s/\s*$//;
		if (m,^([^:]+):$,) {
		    # a new pvs file header, e.g. "/usr/lib/libc.so.1:"
		    if ($list{$1}) {
			$new_path = $1;

			# output the previous one and reset accumulators:
			if (defined($path)) {
				output($path, $pub, $pri, $obs, $evo);

				$pub = '';
				$pri = '';
				$obs = '';
				$evo = '';
			}
			$path = $new_path;
			next;	# done with pvs header case
		    }
		}

		# extract SUNW version head end:

		$vers = trim($_);
		$vers =~ s/;//g;

		# handle the various non-standard cases in Solaris libraries:
		if ($vers =~ /^(SUNW.*private|SUNW_XIL_GPI)/i) {
			$pri .= $vers . ":";
		} elsif ($vers =~ /^(SUNW_\d|SYSVABI|SISCD)/) {
			$pub .= $vers . ":";
		} elsif ($vers =~ /^(SUNW\.\d|SUNW_XIL)/) {
			$pub .= $vers . ":";
		} elsif ($vers =~ /^SUNWobsolete/) {
			$obs .= $vers . ":";
		} elsif ($vers =~ /^SUNWevolving/) {
			$evo .= $vers . ":";
		} else {
			next;
		}
	}
	close($pvs_fh);

	# output the last one (if any):
	if (defined($path)) {
		output($path, $pub, $pri, $obs, $evo);
	}
}

#
# Take the raw library versioning information and process it into index
# file format and then print it out.
#
sub output
{
	my ($path, $pub, $pri, $obs, $evo) = @_;

	return if ($didlib{$path});	# skip repeating a library

	# trim off any trailing separators:
	$pub =~ s/:$//;
	$pri =~ s/:$//;
	$obs =~ s/:$//;
	$evo =~ s/:$//;

	# work out the type of library:
	my $type;
	my $defn;
	my $n;
	if ($pri && ! $pub && ! $obs && ! $evo) {
		$type = 'INTERNAL';
		$defn = 'NO_PUBLIC_SYMS';
	} elsif ($obs) {
		$type = 'OBSOLETE';
		$defn = $obs;
	} elsif ($pub) {
		$type = 'PUBLIC';
		$defn = $pub;
		if ($defn =~ /:/) {
			$defn =~ s/:/,/g;
			$defn = "PUBLIC=$defn";
		}
	} elsif ($evo) {
		$type = 'EVOLVING';
		$defn = $evo;
	} elsif (! $pri && ! $pub && ! $obs && ! $evo) {
		$type = 'UNVERSIONED';
		$defn = '-';
	} else {
		return;
	}

	# return if instructed to skip either of these cases:
	if ($must_be_versioned && $type eq 'UNVERSIONED') {
		return;
	}
	if ($must_be_public && $type eq 'INTERNAL') {
		return;
	}


	# prepare the output line, including any symlink information:
	my $inode = $inode_hash{$path};
	my $links;
	if ($inode && exists($symlink{$inode})) {
		$links = "${path}$symlink{$inode}";
	} else {
		$links = "$path";
	}

	# count the total number of references:
	my (@n) = split(/:/, $links);
	$n = scalar(@n);

	# determine the abi to which the library file belongs:
	my ($fout, $abi);
	$abi = 'unknown';
	$fout = $fileoutput{$path};
	if ($fout =~ /\bSPARCV9\b/) {
		$abi = 'sparcv9';
	} elsif ($fout =~ /\bSPARC/) {
		$abi = 'sparc';
	} elsif ($fout =~ /\bAMD64\b/ || $fout =~ /\bELF\s+64-bit\s+LSB\b/) {
		$abi = 'amd64';
	} elsif ($fout =~ /\b80386\b/) {
		$abi = 'i386';
	}
	print STDOUT "$abi|$path|$defn|$n|$links\n";

	# record that we did this library so we do not process it a second time.
	$didlib{$path} = 1;
}

#
# Remove leading and trailing spaces.
#
sub trim
{
	my ($x) = @_;
	$x =~ s/^\s*//;
	$x =~ s/\s*$//;

	return $x;
}