xref: /titanic_41/usr/src/tools/scripts/check_rtime.pl (revision 7eea693d6b672899726e75993fddc4e95b52647f)
1#!/usr/perl5/bin/perl -w
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27#
28
29#
30# Check ELF information.
31#
32# This script descends a directory hierarchy inspecting ELF dynamic executables
33# and shared objects.  The general theme is to verify that common Makefile rules
34# have been used to build these objects.  Typical failures occur when Makefile
35# rules are re-invented rather than being inherited from "cmd/lib" Makefiles.
36#
37# As always, a number of components don't follow the rules, and these are
38# excluded to reduce this scripts output.  Pathnames used for this exclusion
39# assume this script is being run over a "proto" area.  The -a (all) option
40# skips any exclusions.
41#
42# By default any file that has conditions that should be reported is first
43# listed and then each condition follows.  The -o (one-line) option produces a
44# more terse output which is better for sorting/diffing with "nightly".
45#
46# NOTE: missing dependencies, symbols or versions are reported by running the
47# file through ldd(1).  As objects within a proto area are built to exist in a
48# base system, standard use of ldd(1) will bind any objects to dependencies
49# that exist in the base system.  It is frequently the case that newer objects
50# exist in the proto area that are required to satisfy other objects
51# dependencies, and without using these newer objects an ldd(1) will produce
52# misleading error messages.  To compensate for this, the -d option (or the
53# existence of the CODEMSG_WS/ROOT environment variables) cause the creation of
54# alternative dependency mappings via crle(1) configuration files that establish
55# any proto shared objects as alternatives to their base system location.  Thus
56# ldd(1) can be executed against these configuration files so that objects in a
57# proto area bind to their dependencies in the same proto area.
58
59
60# Define all global variables (required for strict)
61use vars  qw($SkipDirs $SkipFiles $SkipTextrelFiles $SkipDirectBindFiles);
62use vars  qw($SkipUndefFiles $SkipUnusedDirs);
63use vars  qw($SkipStabFiles $SkipNoExStkFiles $SkipCrleConf);
64use vars  qw($SkipUnusedSearchPath $SkipUnrefObject);
65use vars  qw($Prog $Mach $Isalist $Env $Ena64 $Tmpdir $Error $Gnuc);
66use vars  qw($UnusedPaths $LddNoU $Crle32 $Crle64 $Conf32 $Conf64);
67use vars  qw($SkipDirectBindDirs $SkipInterps $SkipSymSort $OldDeps %opt);
68
69use strict;
70
71
72# Define any directories we should skip completely.
73$SkipDirs = qr{
74	usr/lib/devfsadm |		# 4382889
75	usr/lib/libc |			# optimized libc
76	usr/lib/rcm |			# 4426119
77	usr/perl5 |			# alan's taking care of these :-)
78	usr/src				# no need to look at shipped source
79}x;
80
81# Define any files we should skip completely.
82$SkipFiles = qr{ ^(?:
83	lddstub |			# lddstub has no dependencies
84	geniconvtbl\.so |		# 4384329
85	libssagent\.so\.1 |		# 4328854
86	libpsvcplugin_psr\.so\.1 |	# 4385799
87	libpsvcpolicy_psr\.so\.1 |	#  "  "
88	libpsvcpolicy\.so\.1 |		#  "  "
89	picl_slm\.so |			#  "  "
90	mod_ipp\.so |			# Apache loadable module
91	fptest |	# USIII specific extns. cause ldd noise on USII bld. m/c
92	grub
93	)$
94}x;
95
96# Define any files that are allowed text relocations.
97$SkipTextrelFiles = qr{ ^(?:
98	unix |				# kernel models are non-pic
99	mdb				# relocations against __RTC (dbx)
100	)$
101}x;
102
103# Define any directories or files that are allowed to have no direct bound
104# symbols
105$SkipDirectBindDirs = qr{
106	usr/ucb
107}x;
108
109$SkipDirectBindFiles = qr{ ^(?:
110	unix |
111	sbcp |
112	libproc.so.1 |
113	libnisdb.so.2
114	)$
115}x;
116
117# Define any files that are allowed undefined references.
118
119$SkipUndefFiles = qr{ ^(?:
120	libsvm\.so\.1 |			# libspmicommon.so.1 lacking
121	libnisdb\.so\.2			# C++
122	)$
123}x;
124
125# Define any files that have unused dependencies.
126$SkipUnusedDirs = qr{
127	lib/picl/plugins/ |		# require devtree dependencies
128	/lib/libp			# profile libc makes libm an unused
129}x;					#	dependency of standard libc
130
131# Define any files that should contain debugging information.
132$SkipStabFiles = qr{ ^(?:
133	unix
134	)$
135}x;
136
137# Define any files that don't require a non-executable stack definition.
138$SkipNoExStkFiles = qr{ ^(?:
139	forth |
140	unix |
141	multiboot
142	)$
143}x;
144
145# Identify any files that should be skipped when building a crle(1)
146# configuration file.  As the hwcap libraries can be loop-back mounted onto
147# libc, these can confuse crle(1) because of their identical dev/inode.
148$SkipCrleConf = qr{
149	lib/libc/libc_hwcap
150}x;
151
152# Skip "unused search path=" ldd(1) diagnostics.
153$SkipUnusedSearchPath = qr{
154	/usr/lib/fs/autofs.*\ from\ .automountd |		# dlopen()
155	/etc/ppp/plugins.*\ from\ .*pppd |			# dlopen()
156	/usr/lib/inet/ppp.*\ from\ .*pppd |			# dlopen()
157	/usr/sfw/lib.*\ from\ .*libipsecutil.so.1 |		# dlopen()
158	/usr/platform/.*rsmlib.*\ from\ .*librsm.so.2 |		# dlopen()
159	\$ORIGIN.*\ from\ .*fcode.so |				# dlopen()
160	/usr/platform/.*\ from\ .*/usr/platform |		# picl
161	/usr/lib/picl/.*\ from\ .*/usr/platform |		# picl
162	/usr/platform/.*\ from\ .*/usr/lib/picl |		# picl
163	/usr/lib/smbsrv.*\ from\ .*libsmb\.so\.1 |		# future needs
164	/usr/lib/mps/secv1.*\ from\ .*libnss3\.so |		# non-OSNet
165	/usr/lib/mps.*\ from\ .*libnss3\.so |			# non-OSNet
166	/usr/sfw/lib.*\ from\ .*libdbus-1\.so\.3 |		# non-OSNet
167	/usr/sfw/lib.*\ from\ .*libdbus-glib-1\.so\.2 |		# non-OSNet
168	/usr/sfw/lib.*\ from\ .*libglib-2\.0\.so\.0 |		# non-OSNet
169	/usr/X11/lib.*\ from\ .*libglib-2\.0\.so\.0 |		# non-OSNet
170	/usr/sfw/lib.*\ from\ .*libgobject-2\.0\.so\.0 |	# non-OSNet
171	/usr/X11/lib.*\ from\ .*libgobject-2\.0\.so\.0 |	# non-OSNet
172	/usr/sfw/lib.*\ from\ .*libcrypto\.so\.0\.9\.8 |	# non-OSNet
173	/usr/sfw/lib.*\ from\ .*libnetsnmp\.so\.5 |		# non-OSNet
174	/usr/sfw/lib.*\ from\ .*libgcc_s\.so\.1 |		# non-OSNet
175	/usr.*\ from\ .*tst\.gcc\.exe				# gcc built
176}x;
177
178# Skip "unreferenced object=" ldd(1) diagnostics.
179$SkipUnrefObject = qr{
180	/libmapmalloc\.so\.1;\ unused\ dependency\ of |		# interposer
181	/libstdc\+\+\.so\.6;\ unused\ dependency\ of |		# gcc build
182	/libm\.so\.2.*\ of\ .*libstdc\+\+\.so\.6 |		# gcc build
183	/lib.*\ of\ .*/lib/picl/plugins/ |			# picl
184	/lib.*\ of\ .*libcimapi\.so |				# non-OSNET
185	/lib.*\ of\ .*libjvm\.so |				# non-OSNET
186	/lib.*\ of\ .*libnetsnmp\.so\.5 |			# non-OSNET
187	/lib.*\ of\ .*libnetsnmpagent\.so\.5 |			# non-OSNET
188	/lib.*\ of\ .*libnetsnmpmibs\.so\.5 |			# non-OSNET
189	/lib.*\ of\ .*libnetsnmphelpers\.so\.5 |		# non-OSNET
190	/lib.*\ of\ .*libnspr4\.so |				# non-OSNET
191	/lib.*\ of\ .*libsoftokn3\.so |				# non-OSNET
192	/lib.*\ of\ .*libspmicommon\.so\.1 |			# non-OSNET
193	/lib.*\ of\ .*libspmocommon\.so\.1 |			# non-OSNET
194	/lib.*\ of\ .*libssl3\.so |				# non-OSNET
195	/lib.*\ of\ .*libxml2\.so\.2 |				# non-OSNET
196	/lib.*\ of\ .*libxslt\.so\.1 |				# non-OSNET
197	/lib.*\ of\ .*libpq\.so\.4				# non-OSNET
198}x;
199
200# Define any files that should only have unused (ldd -u) processing.
201$UnusedPaths = qr{
202	ucb/shutdown			# libucb interposes on libc and makes
203					# dependencies on libc seem unnecessary
204}x;
205
206# Define interpreters we should ignore.
207$SkipInterps = qr{
208	misc/krtld |
209	misc/amd64/krtld |
210	misc/sparcv9/krtld
211}x;
212
213# Catch libintl and libw, although ld(1) will bind to these and thus determine
214# they're needed, their content was moved into libc as of on297 build 7.
215# libthread and libpthread were completely moved into libc as of on10 build 53.
216# libdl was moved into libc as of on10 build 49.  librt and libaio were moved
217# into libc as of Nevada build 44.
218$OldDeps = qr{ ^(?:
219	libintl\.so\.1 |
220	libw\.so\.1 |
221	libthread\.so\.1 |
222	libpthread\.so\.1 |
223	libdl\.so\.1 |
224	librt\.so\.1 |
225	libaio\.so\.1
226	)$
227}x;
228
229# Files for which we skip checking of duplicate addresses in the
230# symbol sort sections. Such exceptions should be rare --- most code will
231# not have duplicate addresses, since it takes assember or a "#pragma weak"
232# to do such aliasing in C. C++ is different: The compiler generates aliases
233# for implementation reasons, and the mangled names used to encode argument
234# and return value types are difficult to handle well in mapfiles.
235# Furthermore, the Sun compiler and gcc use different and incompatible
236# name mangling conventions. Since ON must be buildable by either, we
237# would have to maintain two sets of mapfiles for each such object.
238# C++ use is rare in ON, so this is not worth pursuing.
239#
240$SkipSymSort = qr{ ^.*(?:
241	opt/SUNWdtrt/tst/common/pid/tst.weak2.exe |	# DTrace test
242	lib/amd64/libnsl\.so\.1 |			# C++
243	lib/sparcv9/libnsl\.so\.1 |			# C++
244	lib/sparcv9/libfru\.so\.1 |			# C++
245	usr/lib/sgml/nsgmls |				# C++
246	ld\.so\.1					# libc_pic.a user
247	)$
248}x;
249
250use Getopt::Std;
251
252# -----------------------------------------------------------------------------
253
254# Reliably compare two OS revisions.  Arguments are <ver1> <op> <ver2>.
255# <op> is the string form of a normal numeric comparison operator.
256sub cmp_os_ver {
257	my @ver1 = split(/\./, $_[0]);
258	my $op = $_[1];
259	my @ver2 = split(/\./, $_[2]);
260
261	push @ver2, ("0") x $#ver1 - $#ver2;
262	push @ver1, ("0") x $#ver2 - $#ver1;
263
264	my $diff = 0;
265	while (@ver1 || @ver2) {
266		if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
267			last;
268		}
269	}
270	return (eval "$diff $op 0" ? 1 : 0);
271}
272
273# This script relies on ldd returning output reflecting only the binary
274# contents.  But if LD_PRELOAD* environment variables are present, libraries
275# named by them will also appear in the output, disrupting our analysis.
276# So, before we get too far, scrub the environment.
277
278delete($ENV{LD_PRELOAD});
279delete($ENV{LD_PRELOAD_32});
280delete($ENV{LD_PRELOAD_64});
281
282# Establish a program name for any error diagnostics.
283chomp($Prog = `basename $0`);
284
285# Determine what machinery is available.
286$Mach = `uname -p`;
287$Isalist = `isalist`;
288$Env = "";
289if ($Mach =~ /sparc/) {
290	if ($Isalist =~ /sparcv9/) {
291		$Ena64 = "ok";
292	}
293} elsif ($Mach =~ /i386/) {
294	if ($Isalist =~ /amd64/) {
295		$Ena64 = "ok";
296	}
297}
298
299# Check that we have arguments.
300if ((getopts('ad:imos', \%opt) == 0) || ($#ARGV == -1)) {
301	print "usage: $Prog [-a] [-d depdir] [-m] [-o] [-s] file | dir, ...\n";
302	print "\t[-a]\t\tprocess all files (ignore any exception lists)\n";
303	print "\t[-d dir]\testablish dependencies from under directory\n";
304	print "\t[-i]\t\tproduce dynamic table entry information\n";
305	print "\t[-m]\t\tprocess mcs(1) comments\n";
306	print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
307	print "\t[-s]\t\tprocess .stab and .symtab entries\n";
308	exit 1;
309} else {
310	my($Proto);
311
312	if ($opt{d}) {
313		# User specified dependency directory - make sure it exists.
314		if (! -d $opt{d}) {
315			print "$Prog: $opt{d} is not a directory\n";
316			exit 1;
317		}
318		$Proto = $opt{d};
319
320	} elsif ($ENV{CODEMGR_WS}) {
321		my($Root);
322
323		# Without a user specified dependency directory see if we're
324		# part of a codemanager workspace and if a proto area exists.
325		if (($Root = $ENV{ROOT}) && (-d $Root)) {
326			$Proto = $Root;
327		}
328	}
329
330	if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir)) {
331		$Tmpdir = "/tmp";
332	}
333
334	# Determine whether this is a __GNUC build.  If so, unused search path
335	# processing is disabled.
336	if (defined $ENV{__GNUC}) {
337		$Gnuc = 1;
338	} else {
339		$Gnuc = 0;
340	}
341
342	# Look for dependencies under $Proto.
343	if ($Proto) {
344		# To support alternative dependency mapping we'll need ldd(1)'s
345		# -e option.  This is relatively new (s81_30), so make sure
346		# ldd(1) is capable before gathering any dependency information.
347		if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
348			print "ldd: does not support -e, unable to ";
349			print "create alternative dependency mappingings.\n";
350			print "ldd: option added under 4390308 (s81_30).\n\n";
351		} else {
352			# Gather dependencies and construct a alternative
353			# dependency mapping via a crle(1) configuration file.
354			GetDeps($Proto, "/");
355			GenConf();
356		}
357	}
358
359	# To support unreferenced dependency detection we'll need ldd(1)'s -U
360	# option.  This is relatively new (4638070), and if not available we
361	# can still fall back to -u.  Even with this option, don't use -U with
362	# releases prior to 5.10 as the cleanup for -U use only got integrated
363	# into 5.10 under 4642023.  Note, that nightly doesn't typically set a
364	# RELEASE from the standard <env> files.  Users who wish to disable use
365	# of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
366	# if using nightly, or otherwise establish it in their environment.
367	if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
368		$LddNoU = 1;
369	} else {
370		my($Release);
371
372		if (($Release = $ENV{RELEASE}) &&
373		    (cmp_os_ver($Release, "<", "5.10"))) {
374			$LddNoU = 1;
375		} else {
376			$LddNoU = 0;
377		}
378	}
379
380	# For each argument determine if we're dealing with a file or directory.
381	foreach my $Arg (@ARGV) {
382		# Ignore symbolic links.
383		if (-l $Arg) {
384			next;
385		}
386
387		if (!stat($Arg)) {
388			next;
389		}
390
391		# Process simple files.
392		if (-f _) {
393			my($RelPath) = $Arg;
394			my($File) = $Arg;
395			my($Secure) = 0;
396
397			$RelPath =~ s!^.*/!./!;
398			$File =~ s!^.*/!!;
399
400			if (-u _ || -g _) {
401				$Secure = 1;
402			}
403
404			ProcFile($Arg, $RelPath, $File, $Secure);
405			next;
406		}
407		# Process directories.
408		if (-d _) {
409			ProcDir($Arg, ".");
410			next;
411		}
412
413		print "$Arg is not a file or directory\n";
414		$Error = 1;
415	}
416
417	# Cleanup
418	CleanUp();
419}
420
421$Error = 0;
422
423# Clean up any temporary files.
424sub CleanUp {
425	if ($Crle64) {
426		unlink $Crle64;
427	}
428	if ($Conf64) {
429		unlink $Conf64;
430	}
431	if ($Crle32) {
432		unlink $Crle32;
433	}
434	if ($Conf32) {
435		unlink $Conf32;
436	}
437}
438
439# Create an output message, either a one-liner (under -o) or preceded by the
440# files relative pathname as a title.
441sub OutMsg {
442	my($Ttl, $Path, $Msg) = @_;
443
444	if ($opt{o}) {
445		$Msg =~ s/^[ \t]*//;
446		print "$Path: $Msg\n";
447	} else {
448		if ($Ttl eq 0) {
449			print "==== $Path ====\n";
450		}
451		print "$Msg\n";
452	}
453}
454
455# Determine whether this a ELF dynamic object and if so investigate its runtime
456# attributes.
457sub ProcFile {
458	my($FullPath, $RelPath, $File, $Secure) = @_;
459	my(@Elf, @Ldd, $Dyn, $Intp, $Dll, $Ttl, $Sym, $Interp, $Stack);
460	my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort);
461	my($Val, $Header, $SkipLdd, $IsX86, $RWX, $UnDep);
462	my($HasDirectBinding);
463
464	# Ignore symbolic links.
465	if (-l $FullPath) {
466		return;
467	}
468
469	$Ttl = 0;
470	@Ldd = 0;
471
472	# Determine whether we have access to inspect the file.
473	if (!(-r $FullPath)) {
474		OutMsg($Ttl++, $RelPath,
475		    "\tunable to inspect file: permission denied");
476		return;
477	}
478
479	# Determine if this is a file we don't care about.
480	if (!$opt{a}) {
481		if ($File =~ $SkipFiles) {
482			return;
483		}
484	}
485
486	# Determine whether we have a executable (static or dynamic) or a
487	# shared object.
488	@Elf = split(/\n/, `elfdump -epdicy $FullPath 2>&1`);
489
490	$Dyn = $Intp = $Dll = $Stack = $IsX86 = $RWX = 0;
491	$Interp = 1;
492	$Header = 'None';
493	foreach my $Line (@Elf) {
494		# If we have an invalid file type (which we can tell from the
495		# first line), or we're processing an archive, bail.
496		if ($Header eq 'None') {
497			if (($Line =~ /invalid file/) ||
498			    ($Line =~ /$FullPath(.*):/)) {
499				return;
500			}
501		}
502
503		if ($Line =~ /^ELF Header/) {
504			$Header = 'Ehdr';
505
506		} elsif ($Line =~ /^Program Header/) {
507			$Header = 'Phdr';
508			$RWX = 0;
509
510		} elsif ($Line =~ /^Interpreter/) {
511			$Header = 'Intp';
512
513		} elsif ($Line =~ /^Dynamic Section/) {
514			# A dynamic section indicates we're a dynamic object
515			# (this makes sure we don't check static executables).
516			$Dyn = 1;
517
518		} elsif (($Header eq 'Ehdr') && ($Line =~ /e_type:/)) {
519			# The e_type field indicates whether this file is a
520			# shared object (ET_DYN) or an executable (ET_EXEC).
521			if ($Line =~ /ET_DYN/) {
522				$Dll = 1;
523			} elsif ($Line !~ /ET_EXEC/) {
524				return;
525			}
526		} elsif (($Header eq 'Ehdr') && ($Line =~ /ei_class:/)) {
527			# If we encounter a 64-bit object, but we're not running
528			# on a 64-bit system, suppress calling ldd(1).
529			if (($Line =~ /ELFCLASS64/) && !$Ena64) {
530				$SkipLdd = 1;
531			}
532		} elsif (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
533			# If it's a X86 object, we need to enforce RW- data.
534			if (($Line =~ /(EM_AMD64|EM_386)/)) {
535				$IsX86 = 1;
536			}
537		} elsif (($Header eq 'Phdr') &&
538		    ($Line =~ /\[ PF_X  PF_W  PF_R \]/)) {
539			# RWX segment seen.
540			$RWX = 1;
541
542		} elsif (($Header eq 'Phdr') &&
543		    ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
544			# Seen an RWX PT_LOAD segment.
545			if ($File !~ $SkipNoExStkFiles) {
546				OutMsg($Ttl++, $RelPath,
547				    "\tapplication requires non-executable " .
548				    "data\t<no -Mmapfile_noexdata?>");
549			}
550
551		} elsif (($Header eq 'Phdr') &&
552		    ($Line =~ /\[ PT_SUNWSTACK \]/)) {
553			# This object defines a non-executable stack.
554			$Stack = 1;
555
556		} elsif (($Header eq 'Intp') && !$opt{a} &&
557		    ($Line =~ $SkipInterps)) {
558			# This object defines an interpretor we should skip.
559			$Interp = 0;
560		}
561	}
562
563	# Determine whether this ELF executable or shared object has a
564	# conforming mcs(1) comment section.  If the correct $(POST_PROCESS)
565	# macros are used, only a 3 or 4 line .comment section should exist
566	# containing one or two "@(#)SunOS" identifying comments (one comment
567	# for a non-debug build, and two for a debug build). The results of
568	# the following split should be three or four lines, the last empty
569	# line being discarded by the split.
570	if ($opt{m}) {
571		my(@Mcs, $Con, $Dev);
572
573		@Mcs = split(/\n/, `mcs -p $FullPath 2>&1`);
574
575		$Con = $Dev = $Val = 0;
576		foreach my $Line (@Mcs) {
577			$Val++;
578
579			if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) {
580				$Con = 1;
581				last;
582			}
583			if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) {
584				$Dev = 1;
585				next;
586			}
587			if (($Dev == 0) && ($Val == 4)) {
588				$Con = 1;
589				last;
590			}
591			if (($Dev == 1) && ($Val == 5)) {
592				$Con = 1;
593				last;
594			}
595		}
596		if ($opt{m} && ($Con == 1)) {
597			OutMsg($Ttl++, $RelPath,
598			    "\tnon-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>");
599		}
600	}
601
602	# Applications should contain a non-executable stack definition.
603	if (($Dll == 0) && ($Stack == 0)) {
604		if (!$opt{a}) {
605			if ($File =~ $SkipNoExStkFiles) {
606				goto DYN;
607			}
608		}
609		OutMsg($Ttl++, $RelPath,
610		    "\tapplication requires non-executable stack\t<no -Mmapfile_noexstk?>");
611	}
612
613DYN:
614	# Having caught any static executables in the mcs(1) check and non-
615	# executable stack definition check, continue with dynamic objects
616	# from now on.
617	if ($Dyn eq 0) {
618		return;
619	}
620
621	# Only use ldd unless we've encountered an interpreter that should
622	# be skipped.
623	if (!$SkipLdd && $Interp) {
624		my $LDDFullPath = $FullPath;
625
626		if ($Secure) {
627			# The execution of a secure application over an nfs file
628			# system mounted nosuid will result in warning messages
629			# being sent to /var/adm/messages.  As this type of
630			# environment can occur with root builds, move the file
631			# being investigated to a safe place first.  In addition
632			# remove its secure permission so that it can be
633			# influenced by any alternative dependency mappings.
634
635			my($TmpPath) = "$Tmpdir/$File";
636
637			system('cp', $LDDFullPath, $TmpPath);
638			chmod 0777, $TmpPath;
639			$LDDFullPath = $TmpPath;
640		}
641
642		# Use ldd(1) to determine the objects relocatability and use.
643		# By default look for all unreferenced dependencies.  However,
644		# some objects have legitimate dependencies that they do not
645		# reference.
646		if ($LddNoU || ($RelPath =~ $UnusedPaths)) {
647			$Lddopt = "-ru";
648		} else {
649			$Lddopt = "-rU";
650		}
651		@Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
652		if ($Secure) {
653			unlink $LDDFullPath;
654		}
655	}
656
657	$Val = 0;
658	$Sym = 5;
659	$UnDep = 1;
660
661	foreach my $Line (@Ldd) {
662
663		if ($Val == 0) {
664			$Val = 1;
665			# Make sure ldd(1) worked.  One possible failure is that
666			# this is an old ldd(1) prior to -e addition (4390308).
667			if ($Line =~ /usage:/) {
668				$Line =~ s/$/\t<old ldd(1)?>/;
669				OutMsg($Ttl++, $RelPath, $Line);
670				last;
671			} elsif ($Line =~ /execution failed/) {
672				OutMsg($Ttl++, $RelPath, $Line);
673				last;
674			}
675
676			# It's possible this binary can't be executed, ie. we've
677			# found a sparc binary while running on an intel system,
678			# or a sparcv9 binary on a sparcv7/8 system.
679			if ($Line =~ /wrong class/) {
680				OutMsg($Ttl++, $RelPath,
681				    "\thas wrong class or data encoding");
682				next;
683			}
684
685			# Historically, ldd(1) likes executable objects to have
686			# their execute bit set.  Note that this test isn't
687			# applied unless the -a option is in effect, as any
688			# non-executable files are skipped by default to reduce
689			# the cost of running this script.
690			if ($Line =~ /not executable/) {
691				OutMsg($Ttl++, $RelPath,
692				    "\tis not executable");
693				next;
694			}
695		}
696
697		# Look for "file" or "versions" that aren't found.  Note that
698		# these lines will occur before we find any symbol referencing
699		# errors.
700		if (($Sym == 5) && ($Line =~ /not found\)/)) {
701			if ($Line =~ /file not found\)/) {
702				$Line =~ s/$/\t<no -zdefs?>/;
703			}
704			OutMsg($Ttl++, $RelPath, $Line);
705			next;
706		}
707		# Look for relocations whose symbols can't be found.  Note, we
708		# only print out the first 5 relocations for any file as this
709		# output can be excessive.
710		if ($Sym && ($Line =~ /symbol not found/)) {
711			# Determine if this file is allowed undefined
712			# references.
713			if ($Sym == 5) {
714				if (!$opt{a}) {
715					if ($File =~ $SkipUndefFiles) {
716						$Sym = 0;
717						next;
718					}
719				}
720			}
721			if ($Sym-- == 1) {
722				if (!$opt{o}) {
723					OutMsg($Ttl++, $RelPath,
724					    "\tcontinued ...");
725				}
726				next;
727			}
728			# Just print the symbol name.
729			$Line =~ s/$/\t<no -zdefs?>/;
730			OutMsg($Ttl++, $RelPath, $Line);
731			next;
732		}
733		# Look for any unused search paths.
734		if ($Line =~ /unused search path=/) {
735			# Note, skip this comparison for __GNUC builds, as the
736			# gnu compilers insert numerous unused search paths.
737			if ($Gnuc == 1) {
738				next;
739			}
740			if (!$opt{a}) {
741				if ($Line =~ $SkipUnusedSearchPath) {
742					next;
743				}
744			}
745			if ($Secure) {
746				$Line =~ s!$Tmpdir/!!;
747			}
748			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
749			OutMsg($Ttl++, $RelPath, $Line);
750			next;
751		}
752		# Look for unreferenced dependencies.  Note, if any unreferenced
753		# objects are ignored, then set $UnDep so as to suppress any
754		# associated unused-object messages.
755		if ($Line =~ /unreferenced object=/) {
756			if (!$opt{a}) {
757				if ($Line =~ $SkipUnrefObject) {
758					$UnDep = 0;
759					next;
760				}
761			}
762			if ($Secure) {
763				$Line =~ s!$Tmpdir/!!;
764			}
765			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove lib or -zignore?>/;
766			OutMsg($Ttl++, $RelPath, $Line);
767			next;
768		}
769		# Look for any unused dependencies.
770		if ($UnDep && ($Line =~ /unused/)) {
771			if (!$opt{a}) {
772				if ($RelPath =~ $SkipUnusedDirs) {
773					$UnDep = 0;
774					next;
775				}
776			}
777			if ($Secure) {
778				$Line =~ s!$Tmpdir/!!;
779			}
780			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove lib or -zignore?>/;
781			OutMsg($Ttl++, $RelPath, $Line);
782			next;
783		}
784	}
785
786	# Reuse the elfdump(1) data to investigate additional dynamic linking
787	# information.
788
789	$Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
790	$Tex = $Strip = 1;
791	$HasDirectBinding = 0;
792
793	$Header = 'None';
794ELF:	foreach my $Line (@Elf) {
795		# We're only interested in the section headers and the dynamic
796		# section.
797		if ($Line =~ /^Section Header/) {
798			$Header = 'Shdr';
799
800			if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
801				# This object has a combined relocation section.
802				$Sun = 1;
803
804			} elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
805				# This object contain .stabs sections
806				$Stab = 1;
807			} elsif (($SymSort == 0) &&
808				 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) {
809				# This object contains a symbol sort section
810				$SymSort = 1;
811			}
812
813			if (($Strip == 1) && ($Line =~ /\.symtab/)) {
814				# This object contains a complete symbol table.
815				$Strip = 0;
816			}
817			next;
818
819		} elsif ($Line =~ /^Dynamic Section/) {
820			$Header = 'Dyn';
821			next;
822		} elsif ($Line =~ /^Syminfo Section/) {
823			$Header = 'Syminfo';
824			next;
825		} elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) {
826			next;
827		}
828
829		# Look into the Syminfo section.
830		# Does this object have at least one Directly Bound symbol?
831		if (($Header eq 'Syminfo')) {
832			my(@Symword);
833
834			if ($HasDirectBinding == 1) {
835				next;
836			}
837
838			@Symword = split(' ', $Line);
839
840			if (!defined($Symword[1])) {
841				next;
842			}
843			if ($Symword[1] =~ /B/) {
844				$HasDirectBinding = 1;
845			}
846			next;
847		}
848
849		# Does this object contain text relocations.
850		if ($Tex && ($Line =~ /TEXTREL/)) {
851			# Determine if this file is allowed text relocations.
852			if (!$opt{a}) {
853				if ($File =~ $SkipTextrelFiles) {
854					$Tex = 0;
855					next ELF;
856				}
857			}
858			OutMsg($Ttl++, $RelPath,
859			    "\tTEXTREL .dynamic tag\t\t\t<no -Kpic?>");
860			$Tex = 0;
861			next;
862		}
863
864		# Does this file have any relocation sections (there are a few
865		# psr libraries with no relocations at all, thus a .SUNW_reloc
866		# section won't exist either).
867		if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
868			$Relsz = hex((split(' ', $Line))[2]);
869			next;
870		}
871
872		# Does this file have any plt relocations.  If the plt size is
873		# equivalent to the total relocation size then we don't have
874		# any relocations suitable for combining into a .SUNW_reloc
875		# section.
876		if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
877			$Pltsz = hex((split(' ', $Line))[2]);
878			next;
879		}
880
881		# Does this object have any dependencies.
882		if ($Line =~ /NEEDED/) {
883			my($Need) = (split(' ', $Line))[3];
884
885			if ($Need =~ $OldDeps) {
886				# Catch any old (unnecessary) dependencies.
887				OutMsg($Ttl++, $RelPath,
888				    "\tNEEDED=$Need\t<dependency no longer necessary>");
889			} elsif ($opt{i}) {
890				# Under the -i (information) option print out
891				# any useful dynamic entries.
892				OutMsg($Ttl++, $RelPath, "\tNEEDED=$Need");
893			}
894			next;
895		}
896
897		# Is this object built with -B direct flag on?
898		if ($Line =~ / DIRECT /) {
899			$HasDirectBinding = 1;
900		}
901
902		# Does this object specify a runpath.
903		if ($opt{i} && ($Line =~ /RPATH/)) {
904			my($Rpath) = (split(' ', $Line))[3];
905			OutMsg($Ttl++, $RelPath, "\tRPATH=$Rpath");
906			next;
907		}
908	}
909
910	# A shared object, that contains non-plt relocations, should have a
911	# combined relocation section indicating it was built with -z combreloc.
912	if ($Dll && $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
913		OutMsg($Ttl++, $RelPath,
914		    "\tSUNW_reloc section missing\t\t<no -zcombreloc?>");
915	}
916
917	# No objects released to a customer should have any .stabs sections
918	# remaining, they should be stripped.
919	if ($opt{s} && $Stab) {
920		if (!$opt{a}) {
921			if ($File =~ $SkipStabFiles) {
922				goto DONESTAB;
923			}
924		}
925		OutMsg($Ttl++, $RelPath,
926		    "\tdebugging sections should be deleted\t<no strip -x?>");
927	}
928
929	# Identify an object that is not built with either -B direct or
930	# -z direct.
931	if (($RelPath =~ $SkipDirectBindDirs) ||
932	    ($File =~ $SkipDirectBindFiles)) {
933		goto DONESTAB;
934	}
935	if ($Relsz && ($HasDirectBinding == 0)) {
936		OutMsg($Ttl++, $RelPath,
937		    "\tobject has no direct bindings\t<no -B direct or -z direct?>");
938	}
939
940DONESTAB:
941
942	# All objects should have a full symbol table to provide complete
943	# debugging stack traces.
944	if ($Strip) {
945		OutMsg($Ttl++, $RelPath,
946		    "\tsymbol table should not be stripped\t<remove -s?>");
947	}
948
949	# If there are symbol sort sections in this object, report on
950	# any that have duplicate addresses.
951	ProcSymSort($FullPath, $RelPath, \$Ttl) if $SymSort;
952}
953
954
955## ProcSymSortOutMsg(RefTtl, RelPath, secname, addr, names...)
956#
957# Call OutMsg for a duplicate address error in a symbol sort
958# section
959#
960sub ProcSymSortOutMsg {
961	my($RefTtl, $RelPath, $secname, $addr, @names) = @_;
962
963	OutMsg($$RefTtl++, $RelPath,
964	    "$secname: duplicate $addr: ". join(', ', @names));
965}
966
967
968## ProcSymSort(FullPath, RelPath)
969#
970# Examine the symbol sort sections for the given object and report
971# on any duplicate addresses found.  Ideally, mapfile directives
972# should be used when building objects that have multiple symbols
973# with the same address so that only one of them appears in the sort
974# section. This saves space, reduces user confusion, and ensures that
975# libproc and debuggers always display public names instead of symbols
976# that are merely implementation details.
977#
978sub ProcSymSort {
979
980	my($FullPath, $RelPath, $RefTtl) = @_;
981
982	# If this object is exempt from checking, return quietly
983	return if ($FullPath =~ $SkipSymSort);
984
985
986	open(SORT, "elfdump -S $FullPath|") ||
987	    die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
988
989	my $line;
990	my $last_addr;
991	my @dups = ();
992	my $secname;
993	while ($line = <SORT>) {
994		chomp $line;
995
996		next if ($line eq '');
997
998		# If this is a header line, pick up the section name
999		if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
1000			$secname = $1;
1001
1002			# Every new section is followed by a column header line
1003			$line = <SORT>;		# Toss header line
1004
1005			# Flush anything left from previous section
1006			ProcSymSortOutMsg($RefTtl, $RelPath, $secname,
1007			    $last_addr, @dups) if (scalar(@dups) > 1);
1008
1009			# Reset variables for new sort section
1010			$last_addr = '';
1011			@dups = ();
1012
1013			next;
1014		}
1015
1016		# Process symbol line
1017		my @fields = split /\s+/, $line;
1018		my $new_addr = $fields[2];
1019		my $new_type = $fields[8];
1020		my $new_name = $fields[9];
1021
1022		if ($new_type eq 'UNDEF') {
1023		    OutMsg($RefTtl++, $RelPath,
1024		        "$secname: unexpected UNDEF symbol " .
1025			"(link-editor error): $new_name");
1026		    next;
1027		}
1028
1029		if ($new_addr eq $last_addr) {
1030			push @dups, $new_name;
1031		} else {
1032			ProcSymSortOutMsg($RefTtl, $RelPath, $secname,
1033			    $last_addr, @dups) if (scalar(@dups) > 1);
1034			@dups = ( $new_name );
1035			$last_addr = $new_addr;
1036		}
1037	}
1038
1039	ProcSymSortOutMsg($RefTtl, $RelPath, $secname, $last_addr, @dups)
1040		if (scalar(@dups) > 1);
1041
1042	close SORT;
1043}
1044
1045
1046sub ProcDir {
1047	my($FullDir, $RelDir) = @_;
1048	my($NewFull, $NewRel);
1049
1050	# Determine if this is a directory we don't care about.
1051	if (!$opt{a}) {
1052		if ($RelDir =~ $SkipDirs) {
1053			return;
1054		}
1055	}
1056
1057	# Open the directory and read each entry, omit files starting with "."
1058	if (opendir(DIR, $FullDir)) {
1059		foreach my $Entry (readdir(DIR)) {
1060			if ($Entry =~ /^\./) {
1061				next;
1062			}
1063			$NewFull = "$FullDir/$Entry";
1064
1065			# Ignore symlinks.
1066			if (-l $NewFull) {
1067				next;
1068			}
1069			if (!stat($NewFull)) {
1070				next;
1071			}
1072			$NewRel = "$RelDir/$Entry";
1073
1074			# Descend into and process any directories.
1075			if (-d _) {
1076				ProcDir($NewFull, $NewRel);
1077				next;
1078			}
1079
1080			# Typically dynamic objects are executable, so we can
1081			# reduce the overall cost of this script (a lot!) by
1082			# screening out non-executables here, rather than pass
1083			# them to file(1) later.  However, it has been known
1084			# for shared objects to be mistakenly left non-
1085			# executable, so with -a let all files through so that
1086			# this requirement can be verified (see ProcFile()).
1087			if (!$opt{a}) {
1088				if (! -x _) {
1089					next;
1090				}
1091			}
1092
1093			# Process any standard files.
1094			if (-f _) {
1095				my($Secure) = 0;
1096
1097				if (-u _ || -g _) {
1098					$Secure = 1;
1099				}
1100
1101				ProcFile($NewFull, $NewRel, $Entry, $Secure);
1102				next;
1103			}
1104
1105		}
1106		closedir(DIR);
1107	}
1108}
1109
1110# Create a crle(1) script for any 64-bit dependencies we locate.  A runtime
1111# configuration file will be generated to establish alternative dependency
1112# mappings for all these dependencies.
1113
1114sub Entercrle64 {
1115	my($FullDir, $RelDir, $Entry) = @_;
1116
1117	if (!$Crle64) {
1118		# Create and initialize the script if is doesn't already exit.
1119
1120		$Crle64 = "$Tmpdir/$Prog.crle64.$$";
1121		open(CRLE64, "> $Crle64") ||
1122			die "$Prog: open failed: $Crle64: $!";
1123
1124		print CRLE64 "#!/bin/sh\ncrle -64\\\n";
1125	}
1126	print CRLE64 "\t-o $FullDir -a $RelDir/$Entry \\\n";
1127}
1128
1129# Create a crle(1) script for any 32-bit dependencies we locate.  A runtime
1130# configuration file will be generated to establish alternative dependency
1131# mappings for all these dependencies.
1132
1133sub Entercrle32 {
1134	my($FullDir, $RelDir, $Entry) = @_;
1135
1136	if (!$Crle32) {
1137		# Create and initialize the script if is doesn't already exit.
1138
1139		$Crle32 = "$Tmpdir/$Prog.crle32.$$";
1140		open(CRLE32, "> $Crle32") ||
1141			die "$Prog: open failed: $Crle32: $!";
1142
1143		print CRLE32 "#!/bin/sh\ncrle \\\n";
1144	}
1145	print CRLE32 "\t-o $FullDir -a $RelDir/$Entry \\\n";
1146}
1147
1148# Having finished gathering dependencies, complete any crle(1) scripts and
1149# execute them to generate the associated runtime configuration files.  In
1150# addition establish the environment variable required to pass the configuration
1151# files to ldd(1).
1152
1153sub GenConf {
1154	if ($Crle64) {
1155		$Conf64 = "$Tmpdir/$Prog.conf64.$$";
1156		print CRLE64 "\t-c $Conf64\n";
1157
1158		chmod 0755, $Crle64;
1159		close CRLE64;
1160
1161		if (system($Crle64)) {
1162			undef $Conf64;
1163		}
1164	}
1165	if ($Crle32) {
1166		$Conf32 = "$Tmpdir/$Prog.conf32.$$";
1167		print CRLE32 "\t-c $Conf32\n";
1168
1169		chmod 0755, $Crle32;
1170		close CRLE32;
1171
1172		if (system($Crle32)) {
1173			undef $Conf32;
1174		}
1175	}
1176
1177	if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
1178		$Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
1179	} elsif ($Crle64 && $Conf64) {
1180		$Env = "-e LD_FLAGS=config_64=$Conf64";
1181	} elsif ($Crle32 && $Conf32) {
1182		$Env = "-e LD_FLAGS=config_32=$Conf32";
1183	}
1184}
1185
1186# Recurse through a directory hierarchy looking for appropriate dependencies.
1187
1188sub GetDeps {
1189	my($FullDir, $RelDir) = @_;
1190	my($NewFull);
1191
1192	# Open the directory and read each entry, omit files starting with "."
1193	if (opendir(DIR, $FullDir)) {
1194		 foreach my $Entry (readdir(DIR)) {
1195			if ($Entry =~ /^\./) {
1196				next;
1197			}
1198			$NewFull = "$FullDir/$Entry";
1199
1200			# We need to follow links so that any dependencies
1201			# are expressed in all their available forms.
1202			# Bail on symlinks like 32 -> .
1203			if (-l $NewFull) {
1204				if (readlink($NewFull) =~ /^\.$/) {
1205					next;
1206				}
1207			}
1208			if (!stat($NewFull)) {
1209				next;
1210			}
1211
1212			if (!$opt{a}) {
1213				if ($NewFull =~ $SkipCrleConf) {
1214					next;
1215				}
1216			}
1217
1218			# If this is a directory descend into it.
1219			if (-d _) {
1220				my($NewRel);
1221
1222				if ($RelDir =~ /^\/$/) {
1223					$NewRel = "$RelDir$Entry";
1224				} else {
1225					$NewRel = "$RelDir/$Entry";
1226				}
1227
1228				GetDeps($NewFull, $NewRel);
1229				next;
1230			}
1231
1232			# If this is a regular file determine if its a
1233			# valid ELF dependency.
1234			if (-f _) {
1235				my($File);
1236
1237				# Typically shared object dependencies end with
1238				# ".so" or ".so.?", hence we can reduce the cost
1239				# of this script (a lot!) by screening out files
1240				# that don't follow this pattern.
1241				if (!$opt{a}) {
1242					if ($Entry !~ /\.so(?:\.\d+)*$/) {
1243						next;
1244					}
1245				}
1246
1247				$File = `file $NewFull`;
1248				if ($File !~ /dynamic lib/) {
1249					next;
1250				}
1251
1252				if ($File =~ /32-bit/) {
1253					Entercrle32($FullDir, $RelDir, $Entry);
1254				} elsif ($Ena64) {
1255					Entercrle64($FullDir, $RelDir, $Entry);
1256				}
1257				next;
1258			}
1259		}
1260		closedir(DIR);
1261	}
1262}
1263exit $Error
1264