xref: /onnv-gate/usr/src/cmd/abi/appcert/scripts/symcheck.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/perl5/bin/perl -w
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#
24*0Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
25*0Sstevel@tonic-gate#
26*0Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
27*0Sstevel@tonic-gate# Use is subject to license terms.
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate#
31*0Sstevel@tonic-gate# This utility program loads the unstable behavior databases, then reads
32*0Sstevel@tonic-gate# the symprof output for each binary, and record any detected unstable
33*0Sstevel@tonic-gate# behavior in the binary's output directory.
34*0Sstevel@tonic-gate#
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gaterequire 5.005;
37*0Sstevel@tonic-gateuse strict;
38*0Sstevel@tonic-gateuse locale;
39*0Sstevel@tonic-gateuse POSIX qw(locale_h);
40*0Sstevel@tonic-gateuse Sun::Solaris::Utils qw(textdomain gettext);
41*0Sstevel@tonic-gateuse File::Basename;
42*0Sstevel@tonic-gateuse File::Path;
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gateuse lib qw(/usr/lib/abi/appcert);
45*0Sstevel@tonic-gateuse AppcertUtil;
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gatesetlocale(LC_ALL, "");
48*0Sstevel@tonic-gatetextdomain(TEXT_DOMAIN);
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gateuse vars qw(
51*0Sstevel@tonic-gate	$LIBC
52*0Sstevel@tonic-gate	$tmp_check_dir
53*0Sstevel@tonic-gate	$binaries_checked_count
54*0Sstevel@tonic-gate	$misc_check_databases_loaded_ok
55*0Sstevel@tonic-gate	%load_model_default
56*0Sstevel@tonic-gate	%load_error
57*0Sstevel@tonic-gate	%model_loaded
58*0Sstevel@tonic-gate	%model
59*0Sstevel@tonic-gate	%is_system_library_cache
60*0Sstevel@tonic-gate);
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gateset_clean_up_exit_routine(\&clean_up_exit);
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gateinitialize_variables();
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gateimport_vars_from_environment();
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gatesignals('on', \&interrupted);
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gateset_working_dir();
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gateload_model_index();
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gatecheck_objects();
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gateclean_up();
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gateexit 0;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate#
81*0Sstevel@tonic-gate# Set up any variables.
82*0Sstevel@tonic-gate#
83*0Sstevel@tonic-gatesub initialize_variables
84*0Sstevel@tonic-gate{
85*0Sstevel@tonic-gate	# Here is what we call libc:
86*0Sstevel@tonic-gate	$LIBC = '/usr/lib/libc.so.1';
87*0Sstevel@tonic-gate}
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate#
90*0Sstevel@tonic-gate# working_dir has been imported by import_vars_from_environment()
91*0Sstevel@tonic-gate# A sanity check is performed here to make sure it exists.
92*0Sstevel@tonic-gate#
93*0Sstevel@tonic-gatesub set_working_dir
94*0Sstevel@tonic-gate{
95*0Sstevel@tonic-gate	if (! defined($working_dir) || ! -d $working_dir) {
96*0Sstevel@tonic-gate		exiter("$command_name: " . sprintf(gettext(
97*0Sstevel@tonic-gate		    "cannot locate working directory: %s\n"), $working_dir));
98*0Sstevel@tonic-gate	}
99*0Sstevel@tonic-gate}
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate#
102*0Sstevel@tonic-gate# Called when interrupted by a signal.
103*0Sstevel@tonic-gate#
104*0Sstevel@tonic-gatesub interrupted
105*0Sstevel@tonic-gate{
106*0Sstevel@tonic-gate	$SIG{$_[0]} = 'DEFAULT';
107*0Sstevel@tonic-gate	signals('off');
108*0Sstevel@tonic-gate	clean_up_exit(1);
109*0Sstevel@tonic-gate}
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate#
112*0Sstevel@tonic-gate# Does the cleanup then exit with return code $rc.  Note: The utility
113*0Sstevel@tonic-gate# routine exiter() will call this routine.
114*0Sstevel@tonic-gate#
115*0Sstevel@tonic-gatesub clean_up_exit
116*0Sstevel@tonic-gate{
117*0Sstevel@tonic-gate	my ($rc) = @_;
118*0Sstevel@tonic-gate	$rc = 0 unless ($rc);
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate	clean_up();
121*0Sstevel@tonic-gate	exit $rc;
122*0Sstevel@tonic-gate}
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate#
125*0Sstevel@tonic-gate# General cleanup activities are placed here.  There may not be an
126*0Sstevel@tonic-gate# immediate exit after this cleanup.
127*0Sstevel@tonic-gate#
128*0Sstevel@tonic-gatesub clean_up
129*0Sstevel@tonic-gate{
130*0Sstevel@tonic-gate	if (defined($tmp_check_dir) && -d $tmp_check_dir) {
131*0Sstevel@tonic-gate		rmtree($tmp_check_dir);
132*0Sstevel@tonic-gate	}
133*0Sstevel@tonic-gate}
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate#
136*0Sstevel@tonic-gate# Top level routine to initialize databases and then loop over the
137*0Sstevel@tonic-gate# objects and call the checking routines on each one.
138*0Sstevel@tonic-gate#
139*0Sstevel@tonic-gatesub check_objects
140*0Sstevel@tonic-gate{
141*0Sstevel@tonic-gate	# Make a tmp dir for the checking work.
142*0Sstevel@tonic-gate	$tmp_check_dir = create_tmp_dir($tmp_dir);
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate	if (! -d $tmp_check_dir) {
145*0Sstevel@tonic-gate		exiter(nocreatedir($tmp_check_dir, $!));
146*0Sstevel@tonic-gate	}
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate	emsg("\n" . gettext(
149*0Sstevel@tonic-gate	    "checking binary objects for unstable practices") . " ...\n\n");
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate	my ($dir, $path_to_object);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate	#
154*0Sstevel@tonic-gate	# Loop over each object item in the working_dir.
155*0Sstevel@tonic-gate	#  - $dir will be each one of these object directories.
156*0Sstevel@tonic-gate	#  - $path_to_object will be the corresponding actual path
157*0Sstevel@tonic-gate	#    to the the binary to be checked.
158*0Sstevel@tonic-gate	# Output will be placed down in $dir, e.g. "$dir/check.foo"
159*0Sstevel@tonic-gate	#
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate	$binaries_checked_count = 0;
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate	#
164*0Sstevel@tonic-gate	# We need to load the Misc Databases to get any modifications to
165*0Sstevel@tonic-gate	# the symbol database. E.g. gethostname should be public.
166*0Sstevel@tonic-gate	#
167*0Sstevel@tonic-gate	$misc_check_databases_loaded_ok = load_misc_check_databases();
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate	while (defined($dir = next_dir_name())) {
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate		# Map object output dir to actual path of the object:
172*0Sstevel@tonic-gate		$path_to_object = dir_name_to_path($dir);
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate		if (! -f $path_to_object) {
175*0Sstevel@tonic-gate			exiter(nopathexist($path_to_object, $!));
176*0Sstevel@tonic-gate		}
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate		# Check it:
179*0Sstevel@tonic-gate		emsg(gettext("checking: %s\n"), $path_to_object);
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate		static_check_object($path_to_object, $dir);
182*0Sstevel@tonic-gate		dynamic_check_object($path_to_object, $dir);
183*0Sstevel@tonic-gate	}
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate	if ($binaries_checked_count == 0) {
186*0Sstevel@tonic-gate		exiter("$command_name: " . gettext(
187*0Sstevel@tonic-gate		    "no binary objects where checked."));
188*0Sstevel@tonic-gate	}
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate	# Do additional heuristic checks of unstable behavior:
191*0Sstevel@tonic-gate	perform_misc_checks();
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate	clean_up();	# Remove any tmp dirs and files.
194*0Sstevel@tonic-gate}
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate#
197*0Sstevel@tonic-gate# Reads in the static profile (i.e. the symbols exported by bin in
198*0Sstevel@tonic-gate# .text section) and calls the static archive checking routine.
199*0Sstevel@tonic-gate#
200*0Sstevel@tonic-gatesub static_check_object
201*0Sstevel@tonic-gate{
202*0Sstevel@tonic-gate	my ($path_to_object, $dir) = @_;
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate	# The profile output file created by static_profile() in symprof.
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate	my $profile_file = "$dir/profile.static";
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate	my $err_fmt = gettext(
209*0Sstevel@tonic-gate	    "binary object %s has no static profile: %s: %s\n");
210*0Sstevel@tonic-gate	if (! -f $profile_file) {
211*0Sstevel@tonic-gate		emsg("$command_name: " . $err_fmt, $path_to_object,
212*0Sstevel@tonic-gate		    $profile_file, $!);
213*0Sstevel@tonic-gate		return 0;
214*0Sstevel@tonic-gate	}
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate	my $profile_fh = do { local *FH; *FH };
217*0Sstevel@tonic-gate	if (! open($profile_fh, "<$profile_file")) {
218*0Sstevel@tonic-gate		exiter(nofile($profile_file, $!));
219*0Sstevel@tonic-gate	}
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate	my ($profile, $lib, $lib2, $base, %libs_needed);
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate	my $completely_statically_linked = 0;
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate	while (<$profile_fh>) {
226*0Sstevel@tonic-gate		$profile .= $_;
227*0Sstevel@tonic-gate		if (/^\s*#dtneeded:\s*(.*)$/) {
228*0Sstevel@tonic-gate			#
229*0Sstevel@tonic-gate			# record the bare name, e.g. "libc" of the
230*0Sstevel@tonic-gate			# (direct) dtneededs.
231*0Sstevel@tonic-gate			#
232*0Sstevel@tonic-gate			foreach $lib (split(/\s+/, $1)) {
233*0Sstevel@tonic-gate				next if ($lib eq '');
234*0Sstevel@tonic-gate				$base = $lib;
235*0Sstevel@tonic-gate				# record it as libc.so.1 -> libc
236*0Sstevel@tonic-gate				$base =~ s/\.so\..*$//;
237*0Sstevel@tonic-gate				$base =~ s/\.so$//;
238*0Sstevel@tonic-gate				$libs_needed{$base} = $lib;
239*0Sstevel@tonic-gate				$libs_needed{basename($base)} = $lib;
240*0Sstevel@tonic-gate			}
241*0Sstevel@tonic-gate		} elsif (/^\s*#SKIPPED_TEST:\s+STATICALLY_LINKED/) {
242*0Sstevel@tonic-gate			#
243*0Sstevel@tonic-gate			# Record statical linking if it takes place
244*0Sstevel@tonic-gate			# since it indicates to skip the test.
245*0Sstevel@tonic-gate			#
246*0Sstevel@tonic-gate			$completely_statically_linked = 1;
247*0Sstevel@tonic-gate		}
248*0Sstevel@tonic-gate	}
249*0Sstevel@tonic-gate	close($profile_fh);
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate	my $problems = "$dir/check.problems";
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate	my $problems_fh = do { local *FH; *FH };
254*0Sstevel@tonic-gate	if (! open($problems_fh, ">>$problems")) {
255*0Sstevel@tonic-gate		exiter(nofile($problems, $!));
256*0Sstevel@tonic-gate	}
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate	if ($completely_statically_linked) {
259*0Sstevel@tonic-gate		print $problems_fh "STATIC: COMPLETELY_STATIC"  . "\n";
260*0Sstevel@tonic-gate	}
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate	my (%saw_lib);
263*0Sstevel@tonic-gate	if (! defined($profile)) {
264*0Sstevel@tonic-gate		close($problems_fh);
265*0Sstevel@tonic-gate		return;
266*0Sstevel@tonic-gate	}
267*0Sstevel@tonic-gate	foreach $lib (lib_static_check($profile)) {
268*0Sstevel@tonic-gate		#
269*0Sstevel@tonic-gate		# lib_static_check returns a list of statically linked
270*0Sstevel@tonic-gate		# libraries, however to be on the safe side we will skip
271*0Sstevel@tonic-gate		# false positives our dtneeded's show they are really
272*0Sstevel@tonic-gate		# dynamically linked in.
273*0Sstevel@tonic-gate		#
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate		next if ($libs_needed{$lib});
276*0Sstevel@tonic-gate		next if ($libs_needed{basename($lib)});
277*0Sstevel@tonic-gate		next if ($saw_lib{basename($lib)}++);
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate		$lib2 = $lib;
280*0Sstevel@tonic-gate		$lib2 =~ s/\.a$//;
281*0Sstevel@tonic-gate		next if ($libs_needed{$lib2});
282*0Sstevel@tonic-gate		next if ($libs_needed{basename($lib2)});
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate		# Otherwise, record in the problems file:
285*0Sstevel@tonic-gate		print $problems_fh "STATIC: LINKED_ARCHIVE $lib" . "\n";
286*0Sstevel@tonic-gate	}
287*0Sstevel@tonic-gate	close($problems_fh);
288*0Sstevel@tonic-gate}
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate#
291*0Sstevel@tonic-gate# Takes as input the static profile (e.g. the .text symbols) and returns
292*0Sstevel@tonic-gate# a list of suspected statically linked Solaris archive libraries.
293*0Sstevel@tonic-gate#
294*0Sstevel@tonic-gatesub lib_static_check
295*0Sstevel@tonic-gate{
296*0Sstevel@tonic-gate	my ($profile) = @_;
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate	my ($line, $area, $extent, $type, $sym, $obj);
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate	my (%symbols);
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate	#
303*0Sstevel@tonic-gate	# Working on lines like:
304*0Sstevel@tonic-gate	# /bin/ftp|TEXT|GLOB|FUNC|glob
305*0Sstevel@tonic-gate	# /bin/ftp|TEXT|GLOB|FUNC|help
306*0Sstevel@tonic-gate	#
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate	# First record all the symbols in the TEXT area:
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate	foreach $line (split(/\n/, $profile)) {
311*0Sstevel@tonic-gate		next unless ($line =~ /\bTEXT\b/);
312*0Sstevel@tonic-gate		($obj, $area, $extent, $type, $sym) = split(/\|/, $line);
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate		$symbols{$sym} = 1;
315*0Sstevel@tonic-gate	}
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate	my (@static_libs);
318*0Sstevel@tonic-gate
319*0Sstevel@tonic-gate	# Next, check against the library heuristics for static linking:
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate	# libc.a:
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gate	if (exists($symbols{'_exit'})) {
324*0Sstevel@tonic-gate		push(@static_libs, "/usr/lib/libc.a");
325*0Sstevel@tonic-gate	}
326*0Sstevel@tonic-gate
327*0Sstevel@tonic-gate	# libsocket.a:
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate	if (exists($symbols{'socket'}) && exists($symbols{'_socket'}) &&
330*0Sstevel@tonic-gate	    exists($symbols{'bind'}) && exists($symbols{'_bind'}) &&
331*0Sstevel@tonic-gate	    exists($symbols{'connect'}) && exists($symbols{'_connect'})) {
332*0Sstevel@tonic-gate			push(@static_libs, "/usr/lib/libsocket.a");
333*0Sstevel@tonic-gate	}
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate	# libnsl.a:
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate	if (exists($symbols{'_xti_bind'}) && exists($symbols{'_xti_connect'}) &&
338*0Sstevel@tonic-gate	    exists($symbols{'_tx_bind'}) && exists($symbols{'_tx_connect'})) {
339*0Sstevel@tonic-gate			push(@static_libs, "/usr/lib/libnsl.a");
340*0Sstevel@tonic-gate	}
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate	return @static_libs;
343*0Sstevel@tonic-gate}
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate#
346*0Sstevel@tonic-gate# Reads in the dynamic profile from the object's output directory.
347*0Sstevel@tonic-gate# Loads any needed public/private Solaris library symbol models.
348*0Sstevel@tonic-gate# Records unstable use of any private Solaris symbols in the object's
349*0Sstevel@tonic-gate# output directory.
350*0Sstevel@tonic-gate#
351*0Sstevel@tonic-gatesub dynamic_check_object
352*0Sstevel@tonic-gate{
353*0Sstevel@tonic-gate	my ($path_to_object, $dir) = @_;
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate	# Location of the dynamic profile output:
356*0Sstevel@tonic-gate	my $profile_file = "$dir/profile.dynamic";
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gate	my $err_fmt = gettext(
359*0Sstevel@tonic-gate	    "binary object %s has no dynamic profile: %s: %s\n");
360*0Sstevel@tonic-gate	if (! -f $profile_file) {
361*0Sstevel@tonic-gate		emsg("$command_name: " . $err_fmt, $path_to_object,
362*0Sstevel@tonic-gate		    $profile_file, $!);
363*0Sstevel@tonic-gate		return 0;
364*0Sstevel@tonic-gate	}
365*0Sstevel@tonic-gate
366*0Sstevel@tonic-gate	my $profile_fh = do { local *FH; *FH };
367*0Sstevel@tonic-gate	if (! open($profile_fh, "<$profile_file")) {
368*0Sstevel@tonic-gate		exiter(nofile($profile_file, $!));
369*0Sstevel@tonic-gate	}
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate	$binaries_checked_count++;
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gate	#
374*0Sstevel@tonic-gate	# Variables to hold temporary items:
375*0Sstevel@tonic-gate	#
376*0Sstevel@tonic-gate	# %library_list will be a hash of "to" libraries we need to load.
377*0Sstevel@tonic-gate	# @symbol_list will be an array of the "lib|sym" pairs.
378*0Sstevel@tonic-gate	#
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate	my (%library_list, @symbol_list, @unbound_list);
381*0Sstevel@tonic-gate	my ($to, $sym, $from, $binary, $line);
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate	my ($to_is_sys_lib, $from_is_sys_lib);
384*0Sstevel@tonic-gate
385*0Sstevel@tonic-gate	#
386*0Sstevel@tonic-gate	# profile lines look like:
387*0Sstevel@tonic-gate	# /bin/ftp|*DIRECT*|libsocket.so.1|socket
388*0Sstevel@tonic-gate	# /bin/ftp|libnsl.so.1|libc.so.1|mutex_lock
389*0Sstevel@tonic-gate	#
390*0Sstevel@tonic-gate	# or:
391*0Sstevel@tonic-gate	#
392*0Sstevel@tonic-gate	# /bin/ftp|*DIRECT*|/usr/lib/libsocket.so.1|socket
393*0Sstevel@tonic-gate	# /bin/ftp|/usr/lib/libnsl.so.1|/usr/lib/libc.so.1|mutex_lock
394*0Sstevel@tonic-gate	#
395*0Sstevel@tonic-gate
396*0Sstevel@tonic-gate	my ($abi, $type, $wordsize, $endian, $e_machine) =
397*0Sstevel@tonic-gate	    bin_type($path_to_object);
398*0Sstevel@tonic-gate
399*0Sstevel@tonic-gate	#
400*0Sstevel@tonic-gate	# Setting abi to 'any' will allow continuation when
401*0Sstevel@tonic-gate	# we encounter an abi we do not recognize.
402*0Sstevel@tonic-gate	#
403*0Sstevel@tonic-gate	if (! defined($abi) || $abi eq 'unknown') {
404*0Sstevel@tonic-gate		$abi = 'any';
405*0Sstevel@tonic-gate	} else {
406*0Sstevel@tonic-gate		#
407*0Sstevel@tonic-gate		# Always try to load libc.  This will be used for symbol
408*0Sstevel@tonic-gate		# migration to libc checks.
409*0Sstevel@tonic-gate		#
410*0Sstevel@tonic-gate		if (! exists($load_model_default{$abi})) {
411*0Sstevel@tonic-gate			load_model($LIBC, $abi);
412*0Sstevel@tonic-gate		}
413*0Sstevel@tonic-gate		$load_model_default{$abi} = 1;
414*0Sstevel@tonic-gate	}
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate	my $dynamic_bindings_count = 0;
417*0Sstevel@tonic-gate	my $no_bindings_msg;
418*0Sstevel@tonic-gate
419*0Sstevel@tonic-gate	while (<$profile_fh>) {
420*0Sstevel@tonic-gate		chomp;
421*0Sstevel@tonic-gate
422*0Sstevel@tonic-gate		if (/^\s*#/) {
423*0Sstevel@tonic-gate		    if (/NO_BINDINGS_FOUND\s*(.*)$/) {
424*0Sstevel@tonic-gate			my $msg = $1;
425*0Sstevel@tonic-gate			if ($msg =~ /^\s*$/) {
426*0Sstevel@tonic-gate				$no_bindings_msg = 'NO_SYMBOL_BINDINGS_FOUND';
427*0Sstevel@tonic-gate			} else {
428*0Sstevel@tonic-gate				$no_bindings_msg = $msg;
429*0Sstevel@tonic-gate			}
430*0Sstevel@tonic-gate		    }
431*0Sstevel@tonic-gate		    next;
432*0Sstevel@tonic-gate		}
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate		($binary, $from, $to, $sym) = split(/\|/, $_, 4);
435*0Sstevel@tonic-gate
436*0Sstevel@tonic-gate		$dynamic_bindings_count++;
437*0Sstevel@tonic-gate
438*0Sstevel@tonic-gate		# Skip the checking of reverse calls:
439*0Sstevel@tonic-gate		next if ($from eq "*REVERSE*");
440*0Sstevel@tonic-gate
441*0Sstevel@tonic-gate		# Skip the checking of special symbols:
442*0Sstevel@tonic-gate		next if (exists($skip_symbols{$sym}));
443*0Sstevel@tonic-gate
444*0Sstevel@tonic-gate		# Accumulate unbounds, but otherwise skip them:
445*0Sstevel@tonic-gate		if ($to eq "*UNBOUND*") {
446*0Sstevel@tonic-gate			push(@unbound_list, "$from|$sym");
447*0Sstevel@tonic-gate			next;
448*0Sstevel@tonic-gate		}
449*0Sstevel@tonic-gate
450*0Sstevel@tonic-gate		# Record if the "to" object is a system library:
451*0Sstevel@tonic-gate		$to_is_sys_lib = is_system_library($to, $abi);
452*0Sstevel@tonic-gate
453*0Sstevel@tonic-gate		if ($from eq "*DIRECT*") {
454*0Sstevel@tonic-gate			$from_is_sys_lib = 0;
455*0Sstevel@tonic-gate		} else {
456*0Sstevel@tonic-gate			#
457*0Sstevel@tonic-gate			# Otherwise we may check its calls. See if it is
458*0Sstevel@tonic-gate			# a system lib:
459*0Sstevel@tonic-gate			#
460*0Sstevel@tonic-gate			$from_is_sys_lib = is_system_library($from, $abi);
461*0Sstevel@tonic-gate		}
462*0Sstevel@tonic-gate
463*0Sstevel@tonic-gate		#
464*0Sstevel@tonic-gate		# We will pass judgement on *DIRECT* calls and indirect
465*0Sstevel@tonic-gate		# calls from a library we do not recognize.
466*0Sstevel@tonic-gate		#
467*0Sstevel@tonic-gate		if ($from_is_sys_lib) {
468*0Sstevel@tonic-gate			next;
469*0Sstevel@tonic-gate		}
470*0Sstevel@tonic-gate		if (! $to_is_sys_lib) {
471*0Sstevel@tonic-gate			# Call to a middleware or supporting library.
472*0Sstevel@tonic-gate			next;
473*0Sstevel@tonic-gate		}
474*0Sstevel@tonic-gate
475*0Sstevel@tonic-gate		$library_list{$to} = 1;
476*0Sstevel@tonic-gate		push(@symbol_list, "$from|$to|$abi|$sym");
477*0Sstevel@tonic-gate	}
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate	close($profile_fh);
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate	my $file;
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gate	my $problems_fh = do { local *FH; *FH };
484*0Sstevel@tonic-gate	if ($dynamic_bindings_count == 0 && defined($no_bindings_msg)) {
485*0Sstevel@tonic-gate		$file = "$dir/check.problems";
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate		if (! open($problems_fh, ">>$file")) {
488*0Sstevel@tonic-gate			exiter(nofile($file, $!));
489*0Sstevel@tonic-gate		}
490*0Sstevel@tonic-gate
491*0Sstevel@tonic-gate		print $problems_fh "DYNAMIC: NO_DYNAMIC_BINDINGS_FOUND" .
492*0Sstevel@tonic-gate		    " $no_bindings_msg\n";
493*0Sstevel@tonic-gate		close($problems_fh);
494*0Sstevel@tonic-gate		return;
495*0Sstevel@tonic-gate	}
496*0Sstevel@tonic-gate
497*0Sstevel@tonic-gate	my ($lib, $str);
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate	$file = "$dir/check.dynamic.abi_models";
500*0Sstevel@tonic-gate	my $model_info_fh = do { local *FH; *FH };
501*0Sstevel@tonic-gate	if (! open($model_info_fh, ">$file")) {
502*0Sstevel@tonic-gate		exiter(nofile($file, $!));
503*0Sstevel@tonic-gate	}
504*0Sstevel@tonic-gate
505*0Sstevel@tonic-gate	# Load all the needed library models:
506*0Sstevel@tonic-gate	my ($s1, $s2);
507*0Sstevel@tonic-gate	$s1 = ",NO_PUBLIC/PRIVATE_MODEL_FOUND-SKIPPING_CHECK_FOR_THIS_LIBRARY";
508*0Sstevel@tonic-gate	$s2 = "ERROR_LOADING_PUBLIC/PRIVATE_MODEL";
509*0Sstevel@tonic-gate
510*0Sstevel@tonic-gate	foreach $lib (keys %library_list) {
511*0Sstevel@tonic-gate		if (! load_model($lib, $abi) && ! $load_error{"$lib|$abi"}) {
512*0Sstevel@tonic-gate			$load_error{"$lib|$abi"} = 1;
513*0Sstevel@tonic-gate		}
514*0Sstevel@tonic-gate		$str = $model_loaded{"$lib|$abi"};
515*0Sstevel@tonic-gate		if ($str eq '__FAILED__') {
516*0Sstevel@tonic-gate			$str .= $s1;
517*0Sstevel@tonic-gate		} elsif (! $str) {
518*0Sstevel@tonic-gate			$str = $s2;
519*0Sstevel@tonic-gate		}
520*0Sstevel@tonic-gate		print $model_info_fh "$lib:$str\n";
521*0Sstevel@tonic-gate	}
522*0Sstevel@tonic-gate	close($model_info_fh);
523*0Sstevel@tonic-gate
524*0Sstevel@tonic-gate	my ($lib_abi_sym, $class, %result_list);
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gate	my ($l, $a, $s);
527*0Sstevel@tonic-gate	foreach $lib_abi_sym (@symbol_list) {
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gate		($from, $lib_abi_sym) = split(/\|/, $lib_abi_sym, 2);
530*0Sstevel@tonic-gate
531*0Sstevel@tonic-gate		($l, $a, $s) = split(/\|/, $lib_abi_sym);
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gate		if (! exists($model{$lib_abi_sym})) {
534*0Sstevel@tonic-gate			#
535*0Sstevel@tonic-gate			# Check the library.  If it is not in
536*0Sstevel@tonic-gate			# model_loaded, then we claim it is not a
537*0Sstevel@tonic-gate			# library we are interested in.
538*0Sstevel@tonic-gate			#
539*0Sstevel@tonic-gate			next if (! exists($model_loaded{"$l|$a"}));
540*0Sstevel@tonic-gate			next if ($model_loaded{"$l|$a"} eq '__FAILED__');
541*0Sstevel@tonic-gate
542*0Sstevel@tonic-gate			# it is an unrecognized symbol:
543*0Sstevel@tonic-gate			$result_list{'unrecognized'} .=
544*0Sstevel@tonic-gate			    "$from|$lib_abi_sym" . "\n";
545*0Sstevel@tonic-gate			next;
546*0Sstevel@tonic-gate		}
547*0Sstevel@tonic-gate
548*0Sstevel@tonic-gate		# N.B. $lib_abi_sym and $l may have been altered above.
549*0Sstevel@tonic-gate		$class = $model{$lib_abi_sym};
550*0Sstevel@tonic-gate		$line = "$path_to_object|$a|$from|$l|$class|$s" . "\n";
551*0Sstevel@tonic-gate
552*0Sstevel@tonic-gate		if ($class !~ /^(public|private|unclassified)$/) {
553*0Sstevel@tonic-gate			exiter("$command_name" . sprintf(gettext(
554*0Sstevel@tonic-gate			    "unrecognized symbol class: %s"), $class));
555*0Sstevel@tonic-gate		}
556*0Sstevel@tonic-gate
557*0Sstevel@tonic-gate		$result_list{$class} .= $line;
558*0Sstevel@tonic-gate	}
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate	if (@unbound_list) {
561*0Sstevel@tonic-gate		my $ldd_file = "$dir/profile.dynamic.ldd";
562*0Sstevel@tonic-gate		my $tmp;
563*0Sstevel@tonic-gate		if (-f $ldd_file) {
564*0Sstevel@tonic-gate			my $ldd_info_fh = do { local *FH; *FH };
565*0Sstevel@tonic-gate			if (! open($ldd_info_fh, "<$ldd_file")) {
566*0Sstevel@tonic-gate				exiter(nofile($ldd_file, $!));
567*0Sstevel@tonic-gate			}
568*0Sstevel@tonic-gate			while (<$ldd_info_fh>) {
569*0Sstevel@tonic-gate				$tmp .= '# ' . $_ if (/not\s+found/);
570*0Sstevel@tonic-gate			}
571*0Sstevel@tonic-gate			close($ldd_info_fh);
572*0Sstevel@tonic-gate		}
573*0Sstevel@tonic-gate		if (defined($tmp)) {
574*0Sstevel@tonic-gate			$result_list{'unbound'} = $tmp;
575*0Sstevel@tonic-gate		}
576*0Sstevel@tonic-gate		$result_list{'unbound'} .= join("\n", @unbound_list) . "\n";
577*0Sstevel@tonic-gate	}
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate	my $count;
580*0Sstevel@tonic-gate
581*0Sstevel@tonic-gate	my @classes = qw(private public unbound unclassified unrecognized);
582*0Sstevel@tonic-gate
583*0Sstevel@tonic-gate	foreach $class (@classes) {
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate		next if (! exists($result_list{$class}));
586*0Sstevel@tonic-gate
587*0Sstevel@tonic-gate		$file = "$dir/check.dynamic.$class";
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate		my $outfile_fh = do { local *FH; *FH };
590*0Sstevel@tonic-gate		if (! open($outfile_fh, ">$file")) {
591*0Sstevel@tonic-gate			exiter(nofile($file, $!));
592*0Sstevel@tonic-gate		}
593*0Sstevel@tonic-gate		if ($class eq 'private') {
594*0Sstevel@tonic-gate			print $outfile_fh
595*0Sstevel@tonic-gate			    $text{'Message_Private_Symbols_Check_Outfile'};
596*0Sstevel@tonic-gate		} elsif ($class eq 'public') {
597*0Sstevel@tonic-gate			print $outfile_fh
598*0Sstevel@tonic-gate			    $text{'Message_Public_Symbols_Check_Outfile'};
599*0Sstevel@tonic-gate		}
600*0Sstevel@tonic-gate		print $outfile_fh $result_list{$class};
601*0Sstevel@tonic-gate		close($outfile_fh);
602*0Sstevel@tonic-gate	}
603*0Sstevel@tonic-gate
604*0Sstevel@tonic-gate	$file = "$dir/check.problems";
605*0Sstevel@tonic-gate
606*0Sstevel@tonic-gate	if (! open($problems_fh, ">>$file")) {
607*0Sstevel@tonic-gate		exiter(nofile($file, $!));
608*0Sstevel@tonic-gate	}
609*0Sstevel@tonic-gate
610*0Sstevel@tonic-gate	if (exists($result_list{'private'})) {
611*0Sstevel@tonic-gate		$count = scalar(my @a = split(/\n/, $result_list{'private'}));
612*0Sstevel@tonic-gate		print $problems_fh "DYNAMIC: PRIVATE_SYMBOL_USE $count\n";
613*0Sstevel@tonic-gate	}
614*0Sstevel@tonic-gate	if (exists($result_list{'unbound'})) {
615*0Sstevel@tonic-gate		$count = scalar(@unbound_list);
616*0Sstevel@tonic-gate		print $problems_fh "DYNAMIC: UNBOUND_SYMBOL_USE $count\n";
617*0Sstevel@tonic-gate	}
618*0Sstevel@tonic-gate	if (exists($result_list{'unrecognized'})) {
619*0Sstevel@tonic-gate		$count =
620*0Sstevel@tonic-gate		    scalar(my @a = split(/\n/, $result_list{'unrecognized'}));
621*0Sstevel@tonic-gate		print $problems_fh "DYNAMIC: UNRECOGNIZED_SYMBOL_USE $count\n";
622*0Sstevel@tonic-gate	}
623*0Sstevel@tonic-gate
624*0Sstevel@tonic-gate	close($problems_fh);
625*0Sstevel@tonic-gate}
626*0Sstevel@tonic-gate
627*0Sstevel@tonic-gate#
628*0Sstevel@tonic-gate# Loads a system model for a library on demand.
629*0Sstevel@tonic-gate#
630*0Sstevel@tonic-gate# Input is a library to load and the architecture ABI.
631*0Sstevel@tonic-gate#
632*0Sstevel@tonic-gate# On successful completion, 1 is returned and the associative array:
633*0Sstevel@tonic-gate#
634*0Sstevel@tonic-gate#	%model{"$library|$abi|$symbol"} = {public,private}
635*0Sstevel@tonic-gate#
636*0Sstevel@tonic-gate#	is set.
637*0Sstevel@tonic-gate#
638*0Sstevel@tonic-gatesub load_model
639*0Sstevel@tonic-gate{
640*0Sstevel@tonic-gate	#
641*0Sstevel@tonic-gate	# Returns 1 if the model was successfully loaded, or returns 0
642*0Sstevel@tonic-gate	# if it was not.
643*0Sstevel@tonic-gate	#
644*0Sstevel@tonic-gate
645*0Sstevel@tonic-gate	my ($library, $abi) = @_;
646*0Sstevel@tonic-gate
647*0Sstevel@tonic-gate	#
648*0Sstevel@tonic-gate	# This %model_loaded hash records the following states:
649*0Sstevel@tonic-gate	#    <string>	Method by which successfully loaded.
650*0Sstevel@tonic-gate	#    __FAILED__	Failed to loaded successfully.
651*0Sstevel@tonic-gate	#    undef      Have not tried to load yet.
652*0Sstevel@tonic-gate	#
653*0Sstevel@tonic-gate	if (exists($model_loaded{"$library|$abi"})) {
654*0Sstevel@tonic-gate		if ($model_loaded{"$library|$abi"} eq '__FAILED__') {
655*0Sstevel@tonic-gate			return 0;
656*0Sstevel@tonic-gate		} elsif ($model_loaded{"$library|$abi"}) {
657*0Sstevel@tonic-gate			return 1;
658*0Sstevel@tonic-gate		}
659*0Sstevel@tonic-gate	}
660*0Sstevel@tonic-gate
661*0Sstevel@tonic-gate	my ($loaded, $ok);
662*0Sstevel@tonic-gate
663*0Sstevel@tonic-gate	$loaded = 1 if (load_model_versioned_lib($library, $abi));
664*0Sstevel@tonic-gate
665*0Sstevel@tonic-gate	# Record the result so we do not have to repeat the above:
666*0Sstevel@tonic-gate
667*0Sstevel@tonic-gate	if ($loaded) {
668*0Sstevel@tonic-gate		$ok = "OK";
669*0Sstevel@tonic-gate		my $tweaks = load_tweaks($library, $abi);
670*0Sstevel@tonic-gate		$ok .= ",Model_Tweaks\[$tweaks\]" if ($tweaks);
671*0Sstevel@tonic-gate		$model_loaded{"$library|$abi"} = $ok;
672*0Sstevel@tonic-gate	} else {
673*0Sstevel@tonic-gate		$model_loaded{"$library|$abi"} = '__FAILED__';
674*0Sstevel@tonic-gate	}
675*0Sstevel@tonic-gate
676*0Sstevel@tonic-gate	return $loaded;
677*0Sstevel@tonic-gate}
678*0Sstevel@tonic-gate
679*0Sstevel@tonic-gate#
680*0Sstevel@tonic-gate# Routine to load into %model any special modifications to the Solaris
681*0Sstevel@tonic-gate# symbol Models kept in the etc.* file.
682*0Sstevel@tonic-gate#
683*0Sstevel@tonic-gatesub load_tweaks
684*0Sstevel@tonic-gate{
685*0Sstevel@tonic-gate	my ($lib, $abi) = @_;
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gate	my $key;
688*0Sstevel@tonic-gate	if (exists($model_tweak{$lib}) && $model_tweak{$lib}) {
689*0Sstevel@tonic-gate		$key = $lib;
690*0Sstevel@tonic-gate	} else {
691*0Sstevel@tonic-gate		#
692*0Sstevel@tonic-gate		# check device/inode record so as to not get tricked by
693*0Sstevel@tonic-gate		# symlinks.
694*0Sstevel@tonic-gate		#
695*0Sstevel@tonic-gate		my ($device, $inode) = (stat($lib))[0,1];
696*0Sstevel@tonic-gate		if (! defined($device) || ! defined($inode)) {
697*0Sstevel@tonic-gate			return 0;
698*0Sstevel@tonic-gate		}
699*0Sstevel@tonic-gate		$key = "$device/$inode";
700*0Sstevel@tonic-gate		if (! exists($model_tweak{$key}) || ! $model_tweak{$key}) {
701*0Sstevel@tonic-gate			return 0;
702*0Sstevel@tonic-gate		}
703*0Sstevel@tonic-gate		#
704*0Sstevel@tonic-gate		# device/inode $key is recorded, so continue along
705*0Sstevel@tonic-gate		# using it below in the model_tweak lookup.
706*0Sstevel@tonic-gate		#
707*0Sstevel@tonic-gate	}
708*0Sstevel@tonic-gate
709*0Sstevel@tonic-gate	#
710*0Sstevel@tonic-gate	# etc line looks like:
711*0Sstevel@tonic-gate	# MODEL_TWEAK|/usr/lib/libnsl.so.1|sparc,i386|gethostname|public
712*0Sstevel@tonic-gate	# value looks like:
713*0Sstevel@tonic-gate	# gethostname|sparc,i386|public
714*0Sstevel@tonic-gate	#
715*0Sstevel@tonic-gate
716*0Sstevel@tonic-gate	my ($case, $abis, $sym, $class, $count);
717*0Sstevel@tonic-gate
718*0Sstevel@tonic-gate	$count = 0;
719*0Sstevel@tonic-gate	foreach $case (split(/,/, $model_tweak{$key})) {
720*0Sstevel@tonic-gate		($sym, $abis, $class) = split(/\|/, $case);
721*0Sstevel@tonic-gate		if ($abis eq '*' || ($abis =~ /\b${abi}\b/)) {
722*0Sstevel@tonic-gate			$model{"$lib|$abi|$sym"} = $class;
723*0Sstevel@tonic-gate			$count++;
724*0Sstevel@tonic-gate		}
725*0Sstevel@tonic-gate	}
726*0Sstevel@tonic-gate
727*0Sstevel@tonic-gate	return $count;
728*0Sstevel@tonic-gate}
729*0Sstevel@tonic-gate
730*0Sstevel@tonic-gate#
731*0Sstevel@tonic-gate# Determine the public/private symbol model for a versioned Solaris
732*0Sstevel@tonic-gate# library. Returns 0 if no model could be extracted, otherwise returns a
733*0Sstevel@tonic-gate# string detailing the model loading.
734*0Sstevel@tonic-gate#
735*0Sstevel@tonic-gatesub load_model_versioned_lib
736*0Sstevel@tonic-gate{
737*0Sstevel@tonic-gate	my ($library, $abi) = @_;
738*0Sstevel@tonic-gate
739*0Sstevel@tonic-gate	#
740*0Sstevel@tonic-gate	# This subroutine runs pvs -dos directly on the Solaris shared
741*0Sstevel@tonic-gate	# object, and parses data that looks like this:
742*0Sstevel@tonic-gate	#
743*0Sstevel@tonic-gate	# % pvs -dos /usr/lib/libsocket.so.1
744*0Sstevel@tonic-gate	# ...
745*0Sstevel@tonic-gate	# /usr/lib/libsocket.so.1 -       SUNW_1.1: __xnet_sendmsg;
746*0Sstevel@tonic-gate	# ...
747*0Sstevel@tonic-gate	# /usr/lib/libsocket.so.1 -       SISCD_2.3: connect;
748*0Sstevel@tonic-gate	# ...
749*0Sstevel@tonic-gate	# /usr/lib/libsocket.so.1 -       SUNWprivate_1.2: getnetmaskbyaddr;
750*0Sstevel@tonic-gate	#
751*0Sstevel@tonic-gate	# Note that data types look like:
752*0Sstevel@tonic-gate	# /usr/lib/libc.so.1 -    SUNWprivate_1.1: __environ_lock (24);
753*0Sstevel@tonic-gate	#
754*0Sstevel@tonic-gate	# we discard the size.
755*0Sstevel@tonic-gate	#
756*0Sstevel@tonic-gate	# On successful completion 1, is returned and the hash:
757*0Sstevel@tonic-gate	#
758*0Sstevel@tonic-gate	#	%model{"$library|$abi|$symbol"} = {public,private}
759*0Sstevel@tonic-gate	#
760*0Sstevel@tonic-gate	# is set.
761*0Sstevel@tonic-gate	#
762*0Sstevel@tonic-gate
763*0Sstevel@tonic-gate	# library must be a full path and exist:
764*0Sstevel@tonic-gate	if (! -f $library) {
765*0Sstevel@tonic-gate		return 0;
766*0Sstevel@tonic-gate	}
767*0Sstevel@tonic-gate
768*0Sstevel@tonic-gate	my ($rc, $output, $output_syslib);
769*0Sstevel@tonic-gate
770*0Sstevel@tonic-gate	#
771*0Sstevel@tonic-gate	# quote character should never happen in normal use, but if it
772*0Sstevel@tonic-gate	# did it will foul up the pvs commands below, so we return
773*0Sstevel@tonic-gate	# early.
774*0Sstevel@tonic-gate	#
775*0Sstevel@tonic-gate	if ($library =~ /'/) {
776*0Sstevel@tonic-gate		return 0;
777*0Sstevel@tonic-gate	}
778*0Sstevel@tonic-gate
779*0Sstevel@tonic-gate	#
780*0Sstevel@tonic-gate	# Get the entire list of symbols:
781*0Sstevel@tonic-gate	# note that $library does not contain a single-quote.
782*0Sstevel@tonic-gate	#
783*0Sstevel@tonic-gate	c_locale(1);
784*0Sstevel@tonic-gate	$output = `$cmd_pvs -dos '$library' 2>/dev/null`;
785*0Sstevel@tonic-gate	$rc = $?;
786*0Sstevel@tonic-gate	c_locale(0);
787*0Sstevel@tonic-gate
788*0Sstevel@tonic-gate	if ($rc != 0 || ($output =~ /^[\s\n]*$/)) {
789*0Sstevel@tonic-gate		# It is not versioned, so get out.
790*0Sstevel@tonic-gate		return 0;
791*0Sstevel@tonic-gate	}
792*0Sstevel@tonic-gate
793*0Sstevel@tonic-gate	# Library is versioned with something from this point on.
794*0Sstevel@tonic-gate
795*0Sstevel@tonic-gate	my ($line, $libtmp, $j1, $j2, $version, $symbol, $class);
796*0Sstevel@tonic-gate	my ($count, $public_count, $private_count);
797*0Sstevel@tonic-gate	my (%versions);
798*0Sstevel@tonic-gate	my $libbase = basename($library);
799*0Sstevel@tonic-gate
800*0Sstevel@tonic-gate	$count = 0;
801*0Sstevel@tonic-gate	$public_count = 0;
802*0Sstevel@tonic-gate	$private_count = 0;
803*0Sstevel@tonic-gate
804*0Sstevel@tonic-gate	my $is_system_lib = is_system_library($library, $abi);
805*0Sstevel@tonic-gate
806*0Sstevel@tonic-gate	my (@defs, $def);
807*0Sstevel@tonic-gate	if (defined($is_system_lib)) {
808*0Sstevel@tonic-gate		foreach $def (split(/:/, $is_system_lib)) {
809*0Sstevel@tonic-gate			next if ($def =~ /^FILE=/);
810*0Sstevel@tonic-gate			next if ($def =~ /^-$/);
811*0Sstevel@tonic-gate			push(@defs, $def);
812*0Sstevel@tonic-gate		}
813*0Sstevel@tonic-gate		if (@defs == 1) {
814*0Sstevel@tonic-gate			$is_system_lib = $defs[0];
815*0Sstevel@tonic-gate		}
816*0Sstevel@tonic-gate	}
817*0Sstevel@tonic-gate
818*0Sstevel@tonic-gate	my (@version_heads, $vers, $default_class);
819*0Sstevel@tonic-gate	if (defined($is_system_lib) && $is_system_lib ne 'NO_PUBLIC_SYMS') {
820*0Sstevel@tonic-gate		#
821*0Sstevel@tonic-gate		# It is a versioned system library.  Extract the public
822*0Sstevel@tonic-gate		# symbols version head end(s)
823*0Sstevel@tonic-gate		#
824*0Sstevel@tonic-gate		if ($is_system_lib =~ /^PUBLIC=(.*)$/) {
825*0Sstevel@tonic-gate			@version_heads = split(/,/, $1);
826*0Sstevel@tonic-gate		} else {
827*0Sstevel@tonic-gate			push(@version_heads, $is_system_lib);
828*0Sstevel@tonic-gate		}
829*0Sstevel@tonic-gate
830*0Sstevel@tonic-gate		#
831*0Sstevel@tonic-gate		# Rerun pvs again to extract the symbols associated with
832*0Sstevel@tonic-gate		# the *public* inheritance chain(s).
833*0Sstevel@tonic-gate		#
834*0Sstevel@tonic-gate		c_locale(1);
835*0Sstevel@tonic-gate		foreach $vers (@version_heads) {
836*0Sstevel@tonic-gate			# something is wrong if $vers has a quote
837*0Sstevel@tonic-gate			$vers =~ s/'//g;
838*0Sstevel@tonic-gate
839*0Sstevel@tonic-gate			# $library has been screened for single quotes earlier.
840*0Sstevel@tonic-gate			$output_syslib .=
841*0Sstevel@tonic-gate			    `$cmd_pvs -dos -N '$vers' '$library' 2>/dev/null`;
842*0Sstevel@tonic-gate		}
843*0Sstevel@tonic-gate		c_locale(0);
844*0Sstevel@tonic-gate	}
845*0Sstevel@tonic-gate
846*0Sstevel@tonic-gate
847*0Sstevel@tonic-gate	if (defined($output_syslib) && ($output_syslib !~ /^[\s\n]*$/)) {
848*0Sstevel@tonic-gate		#
849*0Sstevel@tonic-gate		# If non-empty there are some public symbols sets.
850*0Sstevel@tonic-gate		# First, mark everything private:
851*0Sstevel@tonic-gate		#
852*0Sstevel@tonic-gate		$output = "DEFAULT_CLASS=private\n" . $output;
853*0Sstevel@tonic-gate		# then overwrite the public ones:
854*0Sstevel@tonic-gate		$output .= "DEFAULT_CLASS=public\n"  . $output_syslib;
855*0Sstevel@tonic-gate	} elsif (defined($is_system_lib) &&
856*0Sstevel@tonic-gate	    $is_system_lib eq 'NO_PUBLIC_SYMS') {
857*0Sstevel@tonic-gate		# Everything is private:
858*0Sstevel@tonic-gate		$output = "DEFAULT_CLASS=private\n" . $output;
859*0Sstevel@tonic-gate	} else {
860*0Sstevel@tonic-gate		#
861*0Sstevel@tonic-gate		# assume public, the override will occur when version
862*0Sstevel@tonic-gate		# string matches /private/i. This is for 3rd party
863*0Sstevel@tonic-gate		# libraries.
864*0Sstevel@tonic-gate		#
865*0Sstevel@tonic-gate		$output = "DEFAULT_CLASS=public\n" . $output;
866*0Sstevel@tonic-gate	}
867*0Sstevel@tonic-gate
868*0Sstevel@tonic-gate	foreach $line (split(/\n/, $output)) {
869*0Sstevel@tonic-gate		$line = trim($line);
870*0Sstevel@tonic-gate		if ($line =~ /^DEFAULT_CLASS=(.*)$/) {
871*0Sstevel@tonic-gate			$default_class = $1;
872*0Sstevel@tonic-gate			next;
873*0Sstevel@tonic-gate		}
874*0Sstevel@tonic-gate		($libtmp, $j1, $version, $symbol, $j2) = split(/\s+/, $line);
875*0Sstevel@tonic-gate
876*0Sstevel@tonic-gate		$symbol  =~ s/;*$//;
877*0Sstevel@tonic-gate		$version =~ s/:*$//;
878*0Sstevel@tonic-gate
879*0Sstevel@tonic-gate		next if ($symbol =~ /^\s*$/);
880*0Sstevel@tonic-gate		next if ($version eq $libbase);	# see example output above
881*0Sstevel@tonic-gate
882*0Sstevel@tonic-gate		$versions{$version}++;
883*0Sstevel@tonic-gate
884*0Sstevel@tonic-gate		$class = $default_class;
885*0Sstevel@tonic-gate
886*0Sstevel@tonic-gate		if (! $output_syslib && ($version =~ /private/i)) {
887*0Sstevel@tonic-gate			$class = 'private';
888*0Sstevel@tonic-gate		}
889*0Sstevel@tonic-gate
890*0Sstevel@tonic-gate		if ($class eq 'private') {
891*0Sstevel@tonic-gate			$private_count++;
892*0Sstevel@tonic-gate		} else {
893*0Sstevel@tonic-gate			if ($output_syslib) {
894*0Sstevel@tonic-gate				# remove the double counting of this version:
895*0Sstevel@tonic-gate				$versions{$version}--;
896*0Sstevel@tonic-gate				$private_count--;
897*0Sstevel@tonic-gate				$count--;
898*0Sstevel@tonic-gate			}
899*0Sstevel@tonic-gate			$public_count++;
900*0Sstevel@tonic-gate		}
901*0Sstevel@tonic-gate
902*0Sstevel@tonic-gate		$model{"$library|$abi|$symbol"} = $class;
903*0Sstevel@tonic-gate		$count++;
904*0Sstevel@tonic-gate	}
905*0Sstevel@tonic-gate
906*0Sstevel@tonic-gate	if (! $count) {
907*0Sstevel@tonic-gate		return 0;
908*0Sstevel@tonic-gate	}
909*0Sstevel@tonic-gate
910*0Sstevel@tonic-gate	# Construct the info string:
911*0Sstevel@tonic-gate	$libtmp = "load_model_versioned_lib,$library,$abi:";
912*0Sstevel@tonic-gate	foreach $version (sort(keys(%versions))) {
913*0Sstevel@tonic-gate		$libtmp .= "$version\[$versions{$version}\],";
914*0Sstevel@tonic-gate	}
915*0Sstevel@tonic-gate	$libtmp .=
916*0Sstevel@tonic-gate	    "\[${count}symbols=${public_count}public+${private_count}private\]";
917*0Sstevel@tonic-gate	return $libtmp;
918*0Sstevel@tonic-gate}
919*0Sstevel@tonic-gate
920*0Sstevel@tonic-gate#
921*0Sstevel@tonic-gate# Returns a non-empty string if the $path_to_library is recognized as a
922*0Sstevel@tonic-gate# System (i.e. Solaris) ABI library for given abi.  Returns undef
923*0Sstevel@tonic-gate# otherwise.  The returned string will either be the public symbol version
924*0Sstevel@tonic-gate# name(s), "NO_PUBLIC_SYMS" if all symbols are private, or "-" if there
925*0Sstevel@tonic-gate# is no versioning at all.
926*0Sstevel@tonic-gate#
927*0Sstevel@tonic-gatesub is_system_library
928*0Sstevel@tonic-gate{
929*0Sstevel@tonic-gate	my ($path_to_library, $abi) = @_;
930*0Sstevel@tonic-gate
931*0Sstevel@tonic-gate	if (exists($is_system_library_cache{"$path_to_library|$abi"})) {
932*0Sstevel@tonic-gate		return $is_system_library_cache{"$path_to_library|$abi"};
933*0Sstevel@tonic-gate	}
934*0Sstevel@tonic-gate
935*0Sstevel@tonic-gate	my ($dir, $def, $key);
936*0Sstevel@tonic-gate
937*0Sstevel@tonic-gate	my ($device, $inode) = (stat($path_to_library))[0,1];
938*0Sstevel@tonic-gate	foreach $dir (@lib_index_loaded) {
939*0Sstevel@tonic-gate
940*0Sstevel@tonic-gate		$key = "$dir|$path_to_library|$abi";
941*0Sstevel@tonic-gate		$def = $lib_index_definition{$key};
942*0Sstevel@tonic-gate		if (defined($device) && defined($inode) && ! defined($def)) {
943*0Sstevel@tonic-gate			# try inode lookup (chases down unexpected symlinks)
944*0Sstevel@tonic-gate			$key = "$dir|$device/$inode|$abi";
945*0Sstevel@tonic-gate			$def = $lib_index_definition{$key};
946*0Sstevel@tonic-gate		}
947*0Sstevel@tonic-gate		last if (defined($def));
948*0Sstevel@tonic-gate	}
949*0Sstevel@tonic-gate	if (!defined($def) && $path_to_library !~ /'/) {
950*0Sstevel@tonic-gate		#
951*0Sstevel@tonic-gate		# we skip the case $path_to_library containing
952*0Sstevel@tonic-gate		# a single quote, so the cmd argument is protected.
953*0Sstevel@tonic-gate		#
954*0Sstevel@tonic-gate		my $tmp = `$cmd_pvs -dn '$path_to_library' 2>/dev/null`;
955*0Sstevel@tonic-gate		if ($tmp =~ /\b(SUNW[^;]*);/) {
956*0Sstevel@tonic-gate			$def = $1;
957*0Sstevel@tonic-gate		}
958*0Sstevel@tonic-gate	}
959*0Sstevel@tonic-gate
960*0Sstevel@tonic-gate	$is_system_library_cache{"$path_to_library|$abi"} = $def;
961*0Sstevel@tonic-gate
962*0Sstevel@tonic-gate	return $def;
963*0Sstevel@tonic-gate}
964*0Sstevel@tonic-gate
965*0Sstevel@tonic-gate#
966*0Sstevel@tonic-gate# Loop over each object item in the working_dir.
967*0Sstevel@tonic-gate#  - $dir will be each one of these object directories.
968*0Sstevel@tonic-gate#  - $path_to_object will be the corresponding actual path
969*0Sstevel@tonic-gate#    to the the binary to be checked.
970*0Sstevel@tonic-gate# Output will usually be placed down in $dir, e.g. "$dir/check.foo"
971*0Sstevel@tonic-gate#
972*0Sstevel@tonic-gatesub perform_misc_checks
973*0Sstevel@tonic-gate{
974*0Sstevel@tonic-gate	my ($dir, $path_to_object);
975*0Sstevel@tonic-gate
976*0Sstevel@tonic-gate	if (! $misc_check_databases_loaded_ok) {
977*0Sstevel@tonic-gate		#
978*0Sstevel@tonic-gate		# The load was attempted in check_objects() There is no
979*0Sstevel@tonic-gate		# point in continuing if that loading failed.
980*0Sstevel@tonic-gate		#
981*0Sstevel@tonic-gate		return;
982*0Sstevel@tonic-gate	}
983*0Sstevel@tonic-gate
984*0Sstevel@tonic-gate	emsg("\n" . gettext(
985*0Sstevel@tonic-gate	    "performing miscellaneous checks") . " ...\n\n");
986*0Sstevel@tonic-gate
987*0Sstevel@tonic-gate	while (defined($dir = next_dir_name())) {
988*0Sstevel@tonic-gate
989*0Sstevel@tonic-gate		# Map object output dir to actual path of the object:
990*0Sstevel@tonic-gate		$path_to_object = dir_name_to_path($dir);
991*0Sstevel@tonic-gate
992*0Sstevel@tonic-gate		if (! -f $path_to_object) {
993*0Sstevel@tonic-gate			exiter(nopathexist($path_to_object, $!));
994*0Sstevel@tonic-gate		}
995*0Sstevel@tonic-gate
996*0Sstevel@tonic-gate		# Check it:
997*0Sstevel@tonic-gate		misc_check($path_to_object, $dir);
998*0Sstevel@tonic-gate	}
999*0Sstevel@tonic-gate}
1000*0Sstevel@tonic-gate
1001*0Sstevel@tonic-gate#
1002*0Sstevel@tonic-gate# Routine to perform the misc. checks on a given binary object.  Records
1003*0Sstevel@tonic-gate# the findings in object's output directory.
1004*0Sstevel@tonic-gate#
1005*0Sstevel@tonic-gatesub misc_check
1006*0Sstevel@tonic-gate{
1007*0Sstevel@tonic-gate	my ($path_to_object, $dir) = @_;
1008*0Sstevel@tonic-gate
1009*0Sstevel@tonic-gate	# Load the entire dynamic profile for this object:
1010*0Sstevel@tonic-gate
1011*0Sstevel@tonic-gate	my (@profile, @profile_short, %direct_syms, $file);
1012*0Sstevel@tonic-gate	my $tmp;
1013*0Sstevel@tonic-gate
1014*0Sstevel@tonic-gate	$file = "$dir/profile.dynamic";
1015*0Sstevel@tonic-gate
1016*0Sstevel@tonic-gate	my ($app, $caller, $lib, $base, $sym);
1017*0Sstevel@tonic-gate	my ($libsymcaller, $cnt, %sawlib);
1018*0Sstevel@tonic-gate	if (-f $file) {
1019*0Sstevel@tonic-gate		my $prof_fh = do { local *FH; *FH };
1020*0Sstevel@tonic-gate		if (! open($prof_fh, "<$file")) {
1021*0Sstevel@tonic-gate			exiter(nofile($file, $!));
1022*0Sstevel@tonic-gate		}
1023*0Sstevel@tonic-gate		$cnt = 0;
1024*0Sstevel@tonic-gate		while (<$prof_fh>) {
1025*0Sstevel@tonic-gate			next if (/^\s*#/);
1026*0Sstevel@tonic-gate			next if (/^\s*$/);
1027*0Sstevel@tonic-gate			chomp;
1028*0Sstevel@tonic-gate			($app, $caller, $lib, $sym) = split(/\|/, $_, 4);
1029*0Sstevel@tonic-gate
1030*0Sstevel@tonic-gate			# Skip the checking of special symbols:
1031*0Sstevel@tonic-gate			next if (exists($skip_symbols{$sym}));
1032*0Sstevel@tonic-gate
1033*0Sstevel@tonic-gate			push(@profile, "$lib|$sym|$caller");
1034*0Sstevel@tonic-gate
1035*0Sstevel@tonic-gate			#
1036*0Sstevel@tonic-gate			# We collect in @profile_short up to 10
1037*0Sstevel@tonic-gate			# lib-sym-caller triples where all the libs are
1038*0Sstevel@tonic-gate			# distinct.  This is used to speed up some
1039*0Sstevel@tonic-gate			# loops over the profile below: when we catch
1040*0Sstevel@tonic-gate			# the lib-matching checks early in the loop we
1041*0Sstevel@tonic-gate			# can exclude those checks from the remainder
1042*0Sstevel@tonic-gate			# of the loop.  Since a profile may involve
1043*0Sstevel@tonic-gate			# 1000's of symbols this can be a savings.
1044*0Sstevel@tonic-gate			#
1045*0Sstevel@tonic-gate			if ($cnt < 10 && ! exists($sawlib{$lib})) {
1046*0Sstevel@tonic-gate				push(@profile_short, "$lib|$sym|$caller");
1047*0Sstevel@tonic-gate				$sawlib{$lib} = 1;
1048*0Sstevel@tonic-gate				$cnt++;
1049*0Sstevel@tonic-gate			}
1050*0Sstevel@tonic-gate		}
1051*0Sstevel@tonic-gate		close($prof_fh);
1052*0Sstevel@tonic-gate	}
1053*0Sstevel@tonic-gate	#
1054*0Sstevel@tonic-gate	# Misc Check #1:
1055*0Sstevel@tonic-gate	# Go through dynamic profile looking for scoped local symbols:
1056*0Sstevel@tonic-gate	#
1057*0Sstevel@tonic-gate
1058*0Sstevel@tonic-gate	my (%all_neededs, %lib_not_found);
1059*0Sstevel@tonic-gate	my ($scoped_list, $scoped_msg);
1060*0Sstevel@tonic-gate	my ($sc_rel, $sc_lib, $sc_sym, $sc_val);
1061*0Sstevel@tonic-gate
1062*0Sstevel@tonic-gate	%all_neededs = all_ldd_neededs($path_to_object);
1063*0Sstevel@tonic-gate	my ($key, $key_trim);
1064*0Sstevel@tonic-gate	foreach $key (keys(%all_neededs)) {
1065*0Sstevel@tonic-gate		$key_trim = basename($key);
1066*0Sstevel@tonic-gate		$all_neededs{$key_trim} = $all_neededs{$key};
1067*0Sstevel@tonic-gate		if ($all_neededs{$key} =~ /file not found/) {
1068*0Sstevel@tonic-gate			# %lib_not_found will be used below in check #2
1069*0Sstevel@tonic-gate			$lib_not_found{$key}++;
1070*0Sstevel@tonic-gate			$lib_not_found{$key_trim}++;
1071*0Sstevel@tonic-gate		}
1072*0Sstevel@tonic-gate	}
1073*0Sstevel@tonic-gate
1074*0Sstevel@tonic-gate	# We will need the abi of the object:
1075*0Sstevel@tonic-gate	my $abi;
1076*0Sstevel@tonic-gate	($abi) = bin_type($path_to_object);
1077*0Sstevel@tonic-gate	if ($abi eq '' || ($abi =~ /^(unknown|any)$/)) {
1078*0Sstevel@tonic-gate		if ($uname_p =~ /sparc/i) {
1079*0Sstevel@tonic-gate			$abi = 'sparc';
1080*0Sstevel@tonic-gate		} else {
1081*0Sstevel@tonic-gate			$abi = 'i386';
1082*0Sstevel@tonic-gate		}
1083*0Sstevel@tonic-gate	}
1084*0Sstevel@tonic-gate
1085*0Sstevel@tonic-gate	foreach $libsymcaller (@profile) {
1086*0Sstevel@tonic-gate		next unless ($libsymcaller =~ /\*DIRECT\*$/);
1087*0Sstevel@tonic-gate
1088*0Sstevel@tonic-gate		($lib, $sym, $caller) = split(/\|/, $libsymcaller, 3);
1089*0Sstevel@tonic-gate
1090*0Sstevel@tonic-gate		#
1091*0Sstevel@tonic-gate		# Record direct symbols to improve our %wskip list used
1092*0Sstevel@tonic-gate		# to speed up loops over symbols.
1093*0Sstevel@tonic-gate		#
1094*0Sstevel@tonic-gate		$direct_syms{$sym} = 1;
1095*0Sstevel@tonic-gate		next unless (exists($scoped_symbol_all{$sym}));
1096*0Sstevel@tonic-gate
1097*0Sstevel@tonic-gate		$base = basename($lib);
1098*0Sstevel@tonic-gate
1099*0Sstevel@tonic-gate		#
1100*0Sstevel@tonic-gate		# We only worry if a scoped call is a direct one.  This
1101*0Sstevel@tonic-gate		# assumes the user's support shared objects are also
1102*0Sstevel@tonic-gate		# checked by appcert.
1103*0Sstevel@tonic-gate		#
1104*0Sstevel@tonic-gate
1105*0Sstevel@tonic-gate		if (exists($scoped_symbol{"$lib|$sym"}) ||
1106*0Sstevel@tonic-gate		    exists($scoped_symbol{"$base|$sym"})) {
1107*0Sstevel@tonic-gate			#
1108*0Sstevel@tonic-gate			# This one is for checking on releases BEFORE
1109*0Sstevel@tonic-gate			# the scoping actually occurred.
1110*0Sstevel@tonic-gate			#
1111*0Sstevel@tonic-gate			$scoped_msg  .= "$base:$sym ";
1112*0Sstevel@tonic-gate			$scoped_list .= "$path_to_object|$caller|$lib|$sym\n";
1113*0Sstevel@tonic-gate
1114*0Sstevel@tonic-gate		} elsif ($lib eq '*UNBOUND*' &&
1115*0Sstevel@tonic-gate		    exists($scoped_symbol_all{$sym})) {
1116*0Sstevel@tonic-gate			#
1117*0Sstevel@tonic-gate			# This one is for checking on releases AFTER the
1118*0Sstevel@tonic-gate			# scoping.
1119*0Sstevel@tonic-gate			#
1120*0Sstevel@tonic-gate			# Assume these type of unbounds are deprecated
1121*0Sstevel@tonic-gate			# if found in scoped_symbol_all. Double check it
1122*0Sstevel@tonic-gate			# is in the all-needed-libs though:
1123*0Sstevel@tonic-gate			#
1124*0Sstevel@tonic-gate
1125*0Sstevel@tonic-gate			if (defined($sc_sym) &&
1126*0Sstevel@tonic-gate			    exists($model{"$LIBC|$abi|$sc_sym"})) {
1127*0Sstevel@tonic-gate				next;
1128*0Sstevel@tonic-gate			}
1129*0Sstevel@tonic-gate
1130*0Sstevel@tonic-gate			foreach $sc_val (split(/,/, $scoped_symbol_all{$sym})) {
1131*0Sstevel@tonic-gate				($sc_rel, $sc_lib, $sc_sym) =
1132*0Sstevel@tonic-gate				    split(/\|/, $sc_val);
1133*0Sstevel@tonic-gate
1134*0Sstevel@tonic-gate				#
1135*0Sstevel@tonic-gate				# The general scoping that occurred for
1136*0Sstevel@tonic-gate				# ld.so.1 makes the current heuristic
1137*0Sstevel@tonic-gate				# somewhat less accurate. Unboundedness
1138*0Sstevel@tonic-gate				# from other means has too good a chance
1139*0Sstevel@tonic-gate				# of overlapping with the ld.so.1
1140*0Sstevel@tonic-gate				# scoping. Note that the app likely does
1141*0Sstevel@tonic-gate				# NOT have ld.so.1 in its neededs, but
1142*0Sstevel@tonic-gate				# we still skip this case.
1143*0Sstevel@tonic-gate				#
1144*0Sstevel@tonic-gate
1145*0Sstevel@tonic-gate				next if ($sc_lib eq 'ld.so.1');
1146*0Sstevel@tonic-gate
1147*0Sstevel@tonic-gate				if ($all_neededs{$sc_lib}) {
1148*0Sstevel@tonic-gate					# note that $lib is '*UNBOUND*'
1149*0Sstevel@tonic-gate					$scoped_msg  .= "<unbound>:$sym ";
1150*0Sstevel@tonic-gate					$scoped_list .=
1151*0Sstevel@tonic-gate					"$path_to_object|$caller|$lib|$sym\n";
1152*0Sstevel@tonic-gate				}
1153*0Sstevel@tonic-gate			}
1154*0Sstevel@tonic-gate		}
1155*0Sstevel@tonic-gate	}
1156*0Sstevel@tonic-gate
1157*0Sstevel@tonic-gate	if (defined($scoped_msg)) {
1158*0Sstevel@tonic-gate		my $problems = "$dir/check.problems";
1159*0Sstevel@tonic-gate
1160*0Sstevel@tonic-gate		# problems will be appended to the file:
1161*0Sstevel@tonic-gate		my $problems_fh = do { local *FH; *FH };
1162*0Sstevel@tonic-gate		if (! open($problems_fh, ">>$problems")) {
1163*0Sstevel@tonic-gate			exiter(nofile($problems, $!));
1164*0Sstevel@tonic-gate		}
1165*0Sstevel@tonic-gate		print $problems_fh
1166*0Sstevel@tonic-gate		    "MISC: REMOVED_SCOPED_SYMBOLS: $scoped_msg\n";
1167*0Sstevel@tonic-gate		close($problems_fh);
1168*0Sstevel@tonic-gate
1169*0Sstevel@tonic-gate		$problems = "$dir/check.demoted_symbols";
1170*0Sstevel@tonic-gate
1171*0Sstevel@tonic-gate		# problems will be appended to the file:
1172*0Sstevel@tonic-gate		my $check_fh = do { local *FH; *FH };
1173*0Sstevel@tonic-gate		if (! open($check_fh, ">>$problems")) {
1174*0Sstevel@tonic-gate			exiter(nofile($problems, $!));
1175*0Sstevel@tonic-gate		}
1176*0Sstevel@tonic-gate		print $check_fh $scoped_list;
1177*0Sstevel@tonic-gate		close($check_fh);
1178*0Sstevel@tonic-gate	}
1179*0Sstevel@tonic-gate
1180*0Sstevel@tonic-gate	#
1181*0Sstevel@tonic-gate	# Misc Check #2
1182*0Sstevel@tonic-gate	# Go through dynamic profile looking for special warnings.
1183*0Sstevel@tonic-gate	#
1184*0Sstevel@tonic-gate
1185*0Sstevel@tonic-gate	my (%warnings, %wskip);
1186*0Sstevel@tonic-gate	my (%lib_star, %sym_star, %caller_star);
1187*0Sstevel@tonic-gate	my ($tag, $tag0, $sub, $res);
1188*0Sstevel@tonic-gate
1189*0Sstevel@tonic-gate	while (($tag, $sub) = each(%warnings_match)) {
1190*0Sstevel@tonic-gate		next if (! $sub);
1191*0Sstevel@tonic-gate
1192*0Sstevel@tonic-gate		$res = &{$sub}($path_to_object);
1193*0Sstevel@tonic-gate		$warnings{$tag} = 1 if ($res);
1194*0Sstevel@tonic-gate	}
1195*0Sstevel@tonic-gate
1196*0Sstevel@tonic-gate	my $warnings_bind_has_non_direct = 0;
1197*0Sstevel@tonic-gate
1198*0Sstevel@tonic-gate	while (($tag0, $tmp) =  each(%warnings_bind)) {
1199*0Sstevel@tonic-gate		($lib, $sym, $caller) = split(/\|/, $tmp, 3);
1200*0Sstevel@tonic-gate		$lib_star{$tag0}	= 1 if ($lib eq '*');
1201*0Sstevel@tonic-gate		$sym_star{$tag0}	= 1 if ($sym eq '*');
1202*0Sstevel@tonic-gate		$caller_star{$tag0}	= 1 if ($caller eq '*');
1203*0Sstevel@tonic-gate		if ($lib ne '*' && $lib !~ m,/, && ! $all_neededs{$lib}) {
1204*0Sstevel@tonic-gate			# it can never match:
1205*0Sstevel@tonic-gate			$wskip{$tag0} = 1;
1206*0Sstevel@tonic-gate		}
1207*0Sstevel@tonic-gate		if ($caller ne '*DIRECT*') {
1208*0Sstevel@tonic-gate			# this will be used to speed up the *DIRECT* only case:
1209*0Sstevel@tonic-gate			$warnings_bind_has_non_direct = 1;
1210*0Sstevel@tonic-gate		} elsif ($sym ne '*' && ! $direct_syms{$sym}) {
1211*0Sstevel@tonic-gate			# it can never match:
1212*0Sstevel@tonic-gate			$wskip{$tag0} = 1;
1213*0Sstevel@tonic-gate		}
1214*0Sstevel@tonic-gate	}
1215*0Sstevel@tonic-gate
1216*0Sstevel@tonic-gate	foreach $lib (keys(%lib_not_found)) {
1217*0Sstevel@tonic-gate		#
1218*0Sstevel@tonic-gate		# add a placeholder symbol in %profile to indicate
1219*0Sstevel@tonic-gate		# $lib is on dtneeded, but wasn't found. This will
1220*0Sstevel@tonic-gate		# match a $sym = '*' warnings_bind misc check:
1221*0Sstevel@tonic-gate		#
1222*0Sstevel@tonic-gate		push(@profile,
1223*0Sstevel@tonic-gate		    "$lib|__ldd_indicated_file_not_found__|*DIRECT*");
1224*0Sstevel@tonic-gate	}
1225*0Sstevel@tonic-gate
1226*0Sstevel@tonic-gate	my ($l_t, $s_t, $c_t, $match_t);
1227*0Sstevel@tonic-gate
1228*0Sstevel@tonic-gate	my (@tag_list, @tag_list2, $new_tags);
1229*0Sstevel@tonic-gate	#
1230*0Sstevel@tonic-gate	# create a list of tags excluding the ones we know will be
1231*0Sstevel@tonic-gate	# skipped in the $libsymcaller loop below.
1232*0Sstevel@tonic-gate	#
1233*0Sstevel@tonic-gate	foreach $tag0 (keys(%warnings_bind)) {
1234*0Sstevel@tonic-gate		next if ($wskip{$tag0});
1235*0Sstevel@tonic-gate		push(@tag_list, $tag0);
1236*0Sstevel@tonic-gate	}
1237*0Sstevel@tonic-gate
1238*0Sstevel@tonic-gate	#
1239*0Sstevel@tonic-gate	# we loop over @profile_short first, these will give us up to
1240*0Sstevel@tonic-gate	# 10 different libraries early to help us shrink @tag_list
1241*0Sstevel@tonic-gate	# as we go through the profile.
1242*0Sstevel@tonic-gate	#
1243*0Sstevel@tonic-gate	foreach $libsymcaller (@profile_short, @profile) {
1244*0Sstevel@tonic-gate		@tag_list = @tag_list2 if ($new_tags);
1245*0Sstevel@tonic-gate		last if (! @tag_list);
1246*0Sstevel@tonic-gate
1247*0Sstevel@tonic-gate		($lib, $sym, $caller) = split(/\|/, $libsymcaller, 3);
1248*0Sstevel@tonic-gate
1249*0Sstevel@tonic-gate		if (! $warnings_bind_has_non_direct && $caller ne '*DIRECT*') {
1250*0Sstevel@tonic-gate			next;
1251*0Sstevel@tonic-gate		}
1252*0Sstevel@tonic-gate
1253*0Sstevel@tonic-gate		$base = basename($lib);
1254*0Sstevel@tonic-gate		$new_tags = 0;
1255*0Sstevel@tonic-gate
1256*0Sstevel@tonic-gate		foreach $tag0 (@tag_list) {
1257*0Sstevel@tonic-gate
1258*0Sstevel@tonic-gate			# try to get out early:
1259*0Sstevel@tonic-gate			next if ($wskip{$tag0});
1260*0Sstevel@tonic-gate
1261*0Sstevel@tonic-gate			($tag, $tmp) = split(/\|/, $tag0, 2);
1262*0Sstevel@tonic-gate			# try to get out early:
1263*0Sstevel@tonic-gate			next if ($warnings{$tag});
1264*0Sstevel@tonic-gate
1265*0Sstevel@tonic-gate			$match_t = $warnings_bind{$tag0};
1266*0Sstevel@tonic-gate
1267*0Sstevel@tonic-gate			$l_t = $lib;
1268*0Sstevel@tonic-gate			$s_t = $sym;
1269*0Sstevel@tonic-gate			$c_t = $caller;
1270*0Sstevel@tonic-gate
1271*0Sstevel@tonic-gate			$l_t = '*' if ($lib_star{$tag0});
1272*0Sstevel@tonic-gate			$s_t = '*' if ($sym_star{$tag0});
1273*0Sstevel@tonic-gate			$c_t = '*' if ($caller_star{$tag0});
1274*0Sstevel@tonic-gate
1275*0Sstevel@tonic-gate			if ("$l_t|$s_t|$c_t" eq $match_t ||
1276*0Sstevel@tonic-gate			    "$base|$s_t|$c_t" eq $match_t) {
1277*0Sstevel@tonic-gate				$warnings{$tag} = 1;
1278*0Sstevel@tonic-gate				$wskip{$tag0} = 1;
1279*0Sstevel@tonic-gate
1280*0Sstevel@tonic-gate				# shorten tag list:
1281*0Sstevel@tonic-gate				my (@t, $tg, $tg2, $tp);
1282*0Sstevel@tonic-gate				foreach $tg (@tag_list) {
1283*0Sstevel@tonic-gate					next if ($tg eq $tag0);
1284*0Sstevel@tonic-gate					($tg2, $tp) = split(/\|/, $tg, 2);
1285*0Sstevel@tonic-gate					next if ($tg2 eq $tag);
1286*0Sstevel@tonic-gate					push(@t, $tg);
1287*0Sstevel@tonic-gate				}
1288*0Sstevel@tonic-gate				@tag_list2 = @t;
1289*0Sstevel@tonic-gate				$new_tags = 1;
1290*0Sstevel@tonic-gate			}
1291*0Sstevel@tonic-gate		}
1292*0Sstevel@tonic-gate	}
1293*0Sstevel@tonic-gate
1294*0Sstevel@tonic-gate	if (%warnings) {
1295*0Sstevel@tonic-gate		my $problems = "$dir/check.problems";
1296*0Sstevel@tonic-gate
1297*0Sstevel@tonic-gate		# append problems to the file:
1298*0Sstevel@tonic-gate		my $problems_fh = do { local *FH; *FH };
1299*0Sstevel@tonic-gate		if (! open($problems_fh, ">>$problems")) {
1300*0Sstevel@tonic-gate			exiter(nofile($problems, $!));
1301*0Sstevel@tonic-gate		}
1302*0Sstevel@tonic-gate
1303*0Sstevel@tonic-gate		my $tag;
1304*0Sstevel@tonic-gate		foreach $tag (keys(%warnings)) {
1305*0Sstevel@tonic-gate			print $problems_fh "MISC: WARNING: $tag\n";
1306*0Sstevel@tonic-gate		}
1307*0Sstevel@tonic-gate		close($problems_fh);
1308*0Sstevel@tonic-gate	}
1309*0Sstevel@tonic-gate}
1310