xref: /titanic_52/usr/src/tools/scripts/check_rtime.pl (revision c0c79a3f09914f35651895ffc111883455b7f62d)
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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"%Z%%M%	%I%	%E% SMI"
28#
29
30#
31# Check ELF information.
32#
33# This script descends a directory hierarchy inspecting ELF dynamic executables
34# and shared objects.  The general theme is to verify that common Makefile rules
35# have been used to build these objects.  Typical failures occur when Makefile
36# rules are re-invented rather than being inherited from "cmd/lib" Makefiles.
37#
38# As always, a number of components don't follow the rules, and these are
39# excluded to reduce this scripts output.  Pathnames used for this exclusion
40# assume this script is being run over a "proto" area.  The -a (all) option
41# skips any exclusions.
42#
43# By default any file that has conditions that should be reported is first
44# listed and then each condition follows.  The -o (one-line) option produces a
45# more terse output which is better for sorting/diffing with "nightly".
46#
47# NOTE: missing dependencies, symbols or versions are reported by running the
48# file through ldd(1).  As objects within a proto area are built to exist in a
49# base system, standard use of ldd(1) will bind any objects to dependencies
50# that exist in the base system.  It is frequently the case that newer objects
51# exist in the proto area that are required to satisfy other objects
52# dependencies, and without using these newer objects an ldd(1) will produce
53# misleading error messages.  To compensate for this, the -d option (or the
54# existence of the CODEMSG_WS/ROOT environment variables) cause the creation of
55# alternative dependency mappings via crle(1) configuration files that establish
56# any proto shared objects as alternatives to their base system location.  Thus
57# ldd(1) can be executed against these configuration files so that objects in a
58# proto area bind to their dependencies in the same proto area.
59
60
61# Define all global variables (required for strict)
62use vars  qw($SkipDirs $SkipFiles $SkipTextrelFiles);
63use vars  qw($SkipUndefDirs $SkipUndefFiles $SkipUnusedDirs $SkipUnusedFiles);
64use vars  qw($SkipStabFiles $SkipNoExStkFiles $SkipCrleConf);
65use vars  qw($UnusedNoise $Prog $Mach $Isalist $Env $Ena64 $Tmpdir $Error);
66use vars  qw($UnusedFiles $UnusedPaths $LddNoU $Crle32 $Crle64 $Conf32 $Conf64);
67use vars  qw($SkipInterps $OldDeps %opt);
68
69use strict;
70
71
72# Define any directories we should skip completely.
73$SkipDirs = qr{
74	etc/lib |			# special - used for partial statics
75	usr/lib/devfsadm |		# 4382889
76	usr/lib/libc |			# optimized libc
77	usr/lib/rcm |			# 4426119
78	usr/perl5 |			# alan's taking care of these :-)
79	usr/src				# no need to look at shipped source
80}x;
81
82# Define any files we should skip completely.
83$SkipFiles = qr{ ^(?:
84	ld\.so\.1 |			# confusing but correct dependencies
85	lddstub |			# lddstub has no dependencies
86	libmakestate\.so\.1 |		# temporary; delivered by compiler group
87	libm\.so\.1 |			# temporary; delivered by compiler group
88	libm\.so\.2 |			# temporary; delivered by compiler group
89	geniconvtbl\.so |		# 4384329
90	libssagent\.so\.1 |		# 4328854
91	libpsvcplugin_psr\.so\.1 |	# 4385799
92	libpsvcpolicy_psr\.so\.1 |	#  "  "
93	libpsvcpolicy\.so\.1 |		#  "  "
94	picl_slm\.so |			#  "  "
95	libcrypto_extra\.so\.0\.9\.8 |	# OpenSSL SUNWcry filter lib
96	libssl_extra\.so\.0\.9\.8 |	# OpenSSL SUNWcry filter lib
97	fcpackage\.so |			# circular dependency on fcthread.so
98	mod_ipp\.so |			# Apache loadable module
99	grub
100	)$
101}x;
102
103# Define any files that are allowed text relocations.
104$SkipTextrelFiles = qr{ ^(?:
105	unix |				# kernel models are non-pic
106	mdb				# relocations against __RTC (dbx)
107	)$
108}x;
109
110# Define any files that are allowed undefined references.
111$SkipUndefDirs = qr{
112	usr/lib/inet/ppp/ |		# pppd plugins have callbacks
113	usr/lib/libp/ |			# libc.so.1 requires _mcount
114	/lib/mdb/ |			# mdb modules have callbacks
115	/lib/fm/fmd/plugins/ |		# fmd modules have callbacks
116	/lib/fm/fmd/schemes/ |		# fmd schemes have callbacks
117	/i86pc/lib/mtst/		# mtst modules have callbacks
118}x;
119
120$SkipUndefFiles = qr{ ^(?:
121	libthread_db\.so\.0 |		# callbacks to proc service interface
122	libthread_db\.so\.1 |		#  "	"	"	"
123	librtld_db\.so\.1 |		#  "	"	"	"
124	libc_db\.so\.1 |		#  "	"	"	"
125	libldstab\.so\.1 |		# link-edit support libraries have
126	libld\.so\.[2-4] |			# callback to the link-editors
127	liblddbg\.so\.4 |		#  "	"	"	"
128	librtld\.so\.1 |		#  "	"	"	"
129	libnisdb\.so\.2 |		# C++
130	libsvm\.so\.1 |			# libspmicommon.so.1 lacking
131	libwanboot\.so\.1 |		# libcrypto.a and libssl.a
132	libwrap\.so\.1\.0 |		# uses symbols provided by application
133	fcthread\.so |			# uses symbols provided by application
134	fn\.so\.2 |			# callback to automount
135	preen_md\.so\.1 |		# callback to driver
136	libike\.so\.1 |			# callbacks to in.iked for IKE policy
137	devfsadmd_mod\.so |		# sysevent module callback to syseventd
138	sysevent_conf_mod\.so |		# sysevent module callback to syseventd
139	sysevent_reg_mod\.so		# sysevent module callback to syseventd
140	)$
141}x;
142
143# Define any files that have unused dependencies.
144$SkipUnusedDirs = qr{
145	lib/picl/plugins/ |		# require devtree dependencies
146	/lib/libp			# profile libc makes libm an unused
147}x;					#	dependency of standard libc
148
149$SkipUnusedFiles = qr{ ^(?:
150	devfsadm |			# 4382889
151	disks |				#  "  "
152	tapes |				#  "  "
153	ports |				#  "  "
154	audlinks |			#  "  "
155	devlinks |			#  "  "
156	drvconfig |			#  "  "
157	ntptrace |			# on intel doesn't need libmd5
158	ocfserv |			# libsched unreference by libjvm,
159	poold |				#	see 4952319.
160	libc\.so\.1\.9 |		# 4lib/libc versions have private
161	libc\.so\.2\.9			#	copies of stuff from libc.
162	)$
163}x;
164
165# Define any files that should contain debugging information.
166$SkipStabFiles = qr{ ^(?:
167	abi_.* |
168	interceptors\.so\.1 |
169	unix
170	)$
171}x;
172
173# Define any files that don't require a non-executable stack definition.
174$SkipNoExStkFiles = qr{ ^(?:
175	forth |
176	unix |
177	multiboot
178	)$
179}x;
180
181# Identify any files that should be skipped when building a crle(1)
182# configuration file.  As the hwcap libraries can be loop-back mounted onto
183# libc, these can confuse crle(1) because of their identical dev/inode.
184$SkipCrleConf = qr{
185	lib/libc/libc_hwcap
186}x;
187
188# Define any files that should only have unused (ldd -u) processing.
189$UnusedPaths = qr{
190	ucb/shutdown			# libucb interposes on libc and makes
191					# dependencies on libc seem unnecessary
192}x;
193
194$UnusedFiles = qr{ ^(?:
195	rpc\.nisd			# CCNEEDED makes pthread unreferenced
196	)$
197}x;
198
199# Define unused dependencies we should ignore.
200# libCrun has a unnecessary dependency on libw, and libmapmalloc is often
201# defined to interpose on libc but isn't used by the application itself.
202# Threads dependencies look unused if libc is bound first.
203$UnusedNoise = qr{
204	libw\.so\.1;\ unused |
205	unused\ object=.*libw\.so\.1 |
206	libthread\.so\.1;\ unused |
207	libpthread\.so\.1;\ unused |
208	unused\ object=.*libpthread\.so\.1 |
209	libnsl\.so\.1;\ unused\ dependency\ of\ .*libxslt\.so\.1 |
210	libdl\.so\.1;\ unused\ dependency\ of\ .*libspmicommon\.so\.1 |
211	libdl\.so\.1;\ unused\ dependency\ of\ .*libCrun\.so\.1 |
212	libfru\.so\.1;\ unused\ object=.*libdl\.so\.1 |
213	libfrupicl\.so\.1;\ unused\ object=.*libdl\.so\.1 |
214	libmapmalloc\.so\.1;\ unused |
215	unused\ dependency\ of\ .*libstdc\+\+\.so\.6 |
216	unreferenced\ object=.*libstdc\+\+\.so\.6 |
217	unused\ dependency\ of\ .*libnetsnmphelpers\.so\.5 |
218	unused\ dependency\ of\ .*libnetsnmpmibs\.so\.5 |
219	unused\ dependency\ of\ .*libnetsnmpagent\.so\.5
220}x;
221
222# Define interpreters we should ignore.
223$SkipInterps = qr{
224	misc/krtld |
225	misc/amd64/krtld |
226	misc/sparcv9/krtld
227}x;
228
229# Catch libintl and libw, although ld(1) will bind to these and thus determine
230# they're needed, their content was moved into libc as of on297 build 7.
231# libthread and libpthread were completely moved into libc as of on10 build 53.
232# Also, catch libdl, whose content was moved into libc as of on10 build 49.
233$OldDeps = qr{ ^(?:
234	libintl\.so\.1 |
235	libw\.so\.1 |
236	libthread\.so\.1 |
237	libpthread\.so\.1 |
238	libdl\.so\.1
239	)$
240}x;
241
242use Getopt::Std;
243
244# -----------------------------------------------------------------------------
245
246# Reliably compare two OS revisions.  Arguments are <ver1> <op> <ver2>.
247# <op> is the string form of a normal numeric comparison operator.
248sub cmp_os_ver {
249	my @ver1 = split(/\./, $_[0]);
250	my $op = $_[1];
251	my @ver2 = split(/\./, $_[2]);
252
253	push @ver2, ("0") x $#ver1 - $#ver2;
254	push @ver1, ("0") x $#ver2 - $#ver1;
255
256	my $diff = 0;
257	while (@ver1 || @ver2) {
258		if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
259			last;
260		}
261	}
262	return (eval "$diff $op 0" ? 1 : 0);
263}
264
265# This script relies on ldd returning output reflecting only the binary
266# contents.  But if LD_PRELOAD* environment variables are present, libraries
267# named by them will also appear in the output, disrupting our analysis.
268# So, before we get too far, scrub the environment.
269
270delete($ENV{LD_PRELOAD});
271delete($ENV{LD_PRELOAD_32});
272delete($ENV{LD_PRELOAD_64});
273
274# Establish a program name for any error diagnostics.
275chomp($Prog = `basename $0`);
276
277# Determine what machinery is available.
278$Mach = `uname -p`;
279$Isalist = `isalist`;
280$Env = "";
281if ($Mach =~ /sparc/) {
282	if ($Isalist =~ /sparcv9/) {
283		$Ena64 = "ok";
284	}
285} elsif ($Mach =~ /i386/) {
286	if ($Isalist =~ /amd64/) {
287		$Ena64 = "ok";
288	}
289}
290
291# Check that we have arguments.
292if ((getopts('ad:imos', \%opt) == 0) || ($#ARGV == -1)) {
293	print "usage: $Prog [-a] [-d depdir] [-m] [-o] [-s] file | dir, ...\n";
294	print "\t[-a]\t\tprocess all files (ignore any exception lists)\n";
295	print "\t[-d dir]\testablish dependencies from under directory\n";
296	print "\t[-i]\t\tproduce dynamic table entry information\n";
297	print "\t[-m]\t\tprocess mcs(1) comments\n";
298	print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
299	print "\t[-s]\t\tprocess .stab and .symtab entries\n";
300	exit 1;
301} else {
302	my($Proto);
303
304	if ($opt{d}) {
305		# User specified dependency directory - make sure it exists.
306		if (! -d $opt{d}) {
307			print "$Prog: $opt{d} is not a directory\n";
308			exit 1;
309		}
310		$Proto = $opt{d};
311
312	} elsif ($ENV{CODEMGR_WS}) {
313		my($Root);
314
315		# Without a user specified dependency directory see if we're
316		# part of a codemanager workspace and if a proto area exists.
317		if (($Root = $ENV{ROOT}) && (-d $Root)) {
318			$Proto = $Root;
319		}
320	}
321
322	if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir)) {
323		$Tmpdir = "/tmp";
324	}
325
326	# Look for dependencies under $Proto.
327	if ($Proto) {
328		# To support alternative dependency mapping we'll need ldd(1)'s
329		# -e option.  This is relatively new (s81_30), so make sure
330		# ldd(1) is capable before gathering any dependency information.
331		if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
332			print "ldd: does not support -e, unable to ";
333			print "create alternative dependency mappingings.\n";
334			print "ldd: option added under 4390308 (s81_30).\n\n";
335		} else {
336			# Gather dependencies and construct a alternative
337			# dependency mapping via a crle(1) configuration file.
338			GetDeps($Proto, "/");
339			GenConf();
340		}
341	}
342
343	# To support unreferenced dependency detection we'll need ldd(1)'s -U
344	# option.  This is relatively new (4638070), and if not available we
345	# can still fall back to -u.  Even with this option, don't use -U with
346	# releases prior to 5.10 as the cleanup for -U use only got integrated
347	# into 5.10 under 4642023.  Note, that nightly doesn't typically set a
348	# RELEASE from the standard <env> files.  Users who wish to disable use
349	# of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
350	# if using nightly, or otherwise establish it in their environment.
351	if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
352		$LddNoU = 1;
353	} else {
354		my($Release);
355
356		if (($Release = $ENV{RELEASE}) &&
357		    (cmp_os_ver($Release, "<", "5.10"))) {
358			$LddNoU = 1;
359		} else {
360			$LddNoU = 0;
361		}
362	}
363
364	# For each argument determine if we're dealing with a file or directory.
365	foreach my $Arg (@ARGV) {
366		# Ignore symbolic links.
367		if (-l $Arg) {
368			next;
369		}
370
371		if (!stat($Arg)) {
372			next;
373		}
374
375		# Process simple files.
376		if (-f _) {
377			my($RelPath) = $Arg;
378			my($File) = $Arg;
379			my($Secure) = 0;
380
381			$RelPath =~ s!^.*/!./!;
382			$File =~ s!^.*/!!;
383
384			if (-u _ || -g _) {
385				$Secure = 1;
386			}
387
388			ProcFile($Arg, $RelPath, $File, $Secure);
389			next;
390		}
391		# Process directories.
392		if (-d _) {
393			ProcDir($Arg, ".");
394			next;
395		}
396
397		print "$Arg is not a file or directory\n";
398		$Error = 1;
399	}
400
401	# Cleanup
402	CleanUp();
403}
404
405$Error = 0;
406
407# Clean up any temporary files.
408sub CleanUp {
409	if ($Crle64) {
410		unlink $Crle64;
411	}
412	if ($Conf64) {
413		unlink $Conf64;
414	}
415	if ($Crle32) {
416		unlink $Crle32;
417	}
418	if ($Conf32) {
419		unlink $Conf32;
420	}
421}
422
423# Create an output message, either a one-liner (under -o) or preceded by the
424# files relative pathname as a title.
425sub OutMsg {
426	my($Ttl, $Path, $Msg) = @_;
427
428	if ($opt{o}) {
429		$Msg =~ s/^[ \t]*//;
430		print "$Path: $Msg\n";
431	} else {
432		if ($Ttl eq 0) {
433			print "==== $Path ====\n";
434		}
435		print "$Msg\n";
436	}
437}
438
439# Determine whether this a ELF dynamic object and if so investigate its runtime
440# attributes.
441sub ProcFile {
442	my($FullPath, $RelPath, $File, $Secure) = @_;
443	my(@Elf, @Ldd, $Dyn, $Intp, $Dll, $Ttl, $Sym, $Interp, $Stack);
444	my($Sun, $Relsz, $Pltsz, $Uns, $Tex, $Stab, $Strip, $Lddopt);
445	my($Val, $Header, $SkipLdd, $IsX86, $RWX);
446
447	# Ignore symbolic links.
448	if (-l $FullPath) {
449		return;
450	}
451
452	$Ttl = 0;
453	@Ldd = 0;
454
455	# Determine whether we have access to inspect the file.
456	if (!(-r $FullPath)) {
457		OutMsg($Ttl++, $RelPath,
458		    "\tunable to inspect file: permission denied");
459		return;
460	}
461
462	# Determine if this is a file we don't care about.
463	if (!$opt{a}) {
464		if ($File =~ $SkipFiles) {
465			return;
466		}
467	}
468
469	# Determine whether we have a executable (static or dynamic) or a
470	# shared object.
471	@Elf = split(/\n/, `elfdump -epdic $FullPath 2>&1`);
472
473	$Dyn = $Intp = $Dll = $Stack = $IsX86 = $RWX = 0;
474	$Interp = 1;
475	$Header = 'None';
476	foreach my $Line (@Elf) {
477		# If we have an invalid file type (which we can tell from the
478		# first line), or we're processing an archive, bail.
479		if ($Header eq 'None') {
480			if (($Line =~ /invalid file/) ||
481			    ($Line =~ /$FullPath(.*):/)) {
482				return;
483			}
484		}
485
486		if ($Line =~ /^ELF Header/) {
487			$Header = 'Ehdr';
488
489		} elsif ($Line =~ /^Program Header/) {
490			$Header = 'Phdr';
491			$RWX = 0;
492
493		} elsif ($Line =~ /^Interpreter/) {
494			$Header = 'Intp';
495
496		} elsif ($Line =~ /^Dynamic Section/) {
497			# A dynamic section indicates we're a dynamic object
498			# (this makes sure we don't check static executables).
499			$Dyn = 1;
500
501		} elsif (($Header eq 'Ehdr') && ($Line =~ /e_type:/)) {
502			# The e_type field indicates whether this file is a
503			# shared object (ET_DYN) or an executable (ET_EXEC).
504			if ($Line =~ /ET_DYN/) {
505				$Dll = 1;
506			} elsif ($Line !~ /ET_EXEC/) {
507				return;
508			}
509		} elsif (($Header eq 'Ehdr') && ($Line =~ /ei_class:/)) {
510			# If we encounter a 64-bit object, but we're not running
511			# on a 64-bit system, suppress calling ldd(1).
512			if (($Line =~ /ELFCLASS64/) && !$Ena64) {
513				$SkipLdd = 1;
514			}
515		} elsif (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
516			# If it's a X86 object, we need to enforce RW- data.
517			if (($Line =~ /(EM_AMD64|EM_386)/)) {
518				$IsX86 = 1;
519			}
520		} elsif (($Header eq 'Phdr') &&
521		    ($Line =~ /\[ PF_X  PF_W  PF_R \]/)) {
522			# RWX segment seen.
523			$RWX = 1;
524
525		} elsif (($Header eq 'Phdr') &&
526		    ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
527			# Seen an RWX PT_LOAD segment.
528			if ($File !~ $SkipNoExStkFiles) {
529				OutMsg($Ttl++, $RelPath,
530				    "\tapplication requires non-executable " .
531				    "data\t<no -Mmapfile_noexdata?>");
532			}
533
534		} elsif (($Header eq 'Phdr') &&
535		    ($Line =~ /\[ PT_SUNWSTACK \]/)) {
536			# This object defines a non-executable stack.
537			$Stack = 1;
538
539		} elsif (($Header eq 'Intp') && !$opt{a} &&
540		    ($Line =~ $SkipInterps)) {
541			# This object defines an interpretor we should skip.
542			$Interp = 0;
543		}
544	}
545
546	# Determine whether this ELF executable or shared object has a
547	# conforming mcs(1) comment section.  If the correct $(POST_PROCESS)
548	# macros are used, only a 3 or 4 line .comment section should exist
549	# containing one or two "@(#)SunOS" identifying comments (one comment
550	# for a non-debug build, and two for a debug build). The results of
551	# the following split should be three or four lines, the last empty
552	# line being discarded by the split.
553	if ($opt{m}) {
554		my(@Mcs, $Con, $Dev);
555
556		@Mcs = split(/\n/, `mcs -p $FullPath 2>&1`);
557
558		$Con = $Dev = $Val = 0;
559		foreach my $Line (@Mcs) {
560			$Val++;
561
562			if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) {
563				$Con = 1;
564				last;
565			}
566			if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) {
567				$Dev = 1;
568				next;
569			}
570			if (($Dev == 0) && ($Val == 4)) {
571				$Con = 1;
572				last;
573			}
574			if (($Dev == 1) && ($Val == 5)) {
575				$Con = 1;
576				last;
577			}
578		}
579		if ($opt{m} && ($Con == 1)) {
580			OutMsg($Ttl++, $RelPath,
581			    "\tnon-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>");
582		}
583	}
584
585	# Applications should contain a non-executable stack definition.
586	if (($Dll == 0) && ($Stack == 0)) {
587		if (!$opt{a}) {
588			if ($File =~ $SkipNoExStkFiles) {
589				goto DYN;
590			}
591		}
592		OutMsg($Ttl++, $RelPath,
593		    "\tapplication requires non-executable stack\t<no -Mmapfile_noexstk?>");
594	}
595
596DYN:
597	# Having caught any static executables in the mcs(1) check and non-
598	# executable stack definition check, continue with dynamic objects
599	# from now on.
600	if ($Dyn eq 0) {
601		return;
602	}
603
604	# Only use ldd unless we've encountered an interpreter that should
605	# be skipped.
606	if (!$SkipLdd && $Interp) {
607		if ($Secure) {
608			# The execution of a secure application over an nfs file
609			# system mounted nosuid will result in warning messages
610			# being sent to /var/adm/messages.  As this type of
611			# environment can occur with root builds, move the file
612			# being investigated to a safe place first.  In addition
613			# remove its secure permission so that it can be
614			# influenced by any alternative dependency mappings.
615
616			my($TmpPath) = "$Tmpdir/$File";
617
618			system('cp', $FullPath, $TmpPath);
619			chmod 0777, $TmpPath;
620			$FullPath = $TmpPath;
621		}
622
623		# Use ldd(1) to determine the objects relocatability and use.
624		# By default look for all unreferenced dependencies.  However,
625		# some objects have legitimate dependencies that they do not
626		# reference.
627		if ($LddNoU || ($File =~ $UnusedFiles) ||
628		    ($RelPath =~ $UnusedPaths)) {
629			$Lddopt = "-ru";
630		} else {
631			$Lddopt = "-rU";
632		}
633		@Ldd = split(/\n/, `ldd $Lddopt $Env $FullPath 2>&1`);
634		if ($Secure) {
635			unlink $FullPath;
636		}
637	}
638
639	$Val = 0;
640	$Sym = 5;
641	$Uns = 1;
642
643LDD:	foreach my $Line (@Ldd) {
644
645		if ($Val == 0) {
646			$Val = 1;
647			# Make sure ldd(1) worked.  One possible failure is that
648			# this is an old ldd(1) prior to -e addition (4390308).
649			if ($Line =~ /usage:/) {
650				$Line =~ s/$/\t<old ldd(1)?>/;
651				OutMsg($Ttl++, $RelPath, $Line);
652				last;
653			} elsif ($Line =~ /execution failed/) {
654				OutMsg($Ttl++, $RelPath, $Line);
655				last;
656			}
657
658			# It's possible this binary can't be executed, ie. we've
659			# found a sparc binary while running on an intel system,
660			# or a sparcv9 binary on a sparcv7/8 system.
661			if ($Line =~ /wrong class/) {
662				OutMsg($Ttl++, $RelPath,
663				    "\thas wrong class or data encoding");
664				next;
665			}
666
667			# Historically, ldd(1) likes executable objects to have
668			# their execute bit set.  Note that this test isn't
669			# applied unless the -a option is in effect, as any
670			# non-executable files are skipped by default to reduce
671			# the cost of running this script.
672			if ($Line =~ /not executable/) {
673				OutMsg($Ttl++, $RelPath,
674				    "\tis not executable");
675				next;
676			}
677		}
678
679		# Look for "file" or "versions" that aren't found.  Note that
680		# these lines will occur before we find any symbol referencing
681		# errors.
682		if (($Sym == 5) && ($Line =~ /not found\)/)) {
683			if ($Line =~ /file not found\)/) {
684				$Line =~ s/$/\t<no -zdefs?>/;
685			}
686			OutMsg($Ttl++, $RelPath, $Line);
687			next;
688		}
689		# Look for relocations whose symbols can't be found.  Note, we
690		# only print out the first 5 relocations for any file as this
691		# output can be excessive.
692		if ($Sym && ($Line =~ /symbol not found/)) {
693			# Determine if this file is allowed undefined
694			# references.
695			if ($Sym == 5) {
696				if (!$opt{a}) {
697					if ($RelPath =~ $SkipUndefDirs) {
698						$Sym = 0;
699						next LDD;
700					}
701					if ($File =~ $SkipUndefFiles) {
702						$Sym = 0;
703						next LDD;
704					}
705				}
706			}
707			if ($Sym-- == 1) {
708				if (!$opt{o}) {
709					OutMsg($Ttl++, $RelPath,
710					    "\tcontinued ...");
711				}
712				next;
713			}
714			# Just print the symbol name.
715			$Line =~ s/$/\t<no -zdefs?>/;
716			OutMsg($Ttl++, $RelPath, $Line);
717			next;
718		}
719		# Look for any unused dependencies.
720		if ($Uns && ($Line =~ /unused/)) {
721			if (!$opt{a}) {
722				if ($RelPath =~ $SkipUnusedDirs) {
723					$Uns = 0;
724					next LDD;
725				}
726				if ($File =~ $SkipUnusedFiles) {
727					$Uns = 0;
728					next LDD;
729				}
730
731				# Remove any noise.
732				if ($Line =~ $UnusedNoise) {
733					$Uns = 0;
734					next LDD;
735				}
736			}
737			if ($Secure) {
738				$Line =~ s!$Tmpdir/!!;
739			}
740			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove lib or -zignore?>/;
741			OutMsg($Ttl++, $RelPath, $Line);
742			next;
743		}
744	}
745
746	# Reuse the elfdump(1) data to investigate additional dynamic linking
747	# information.
748
749	$Sun = $Relsz = $Pltsz = $Dyn = $Stab = 0;
750	$Tex = $Strip = 1;
751
752	$Header = 'None';
753ELF:	foreach my $Line (@Elf) {
754		# We're only interested in the section headers and the dynamic
755		# section.
756		if ($Line =~ /^Section Header/) {
757			$Header = 'Shdr';
758
759			if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
760				# This object has a combined relocation section.
761				$Sun = 1;
762
763			} elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
764				# This object contain .stabs sections
765				$Stab = 1;
766			}
767
768			if (($Strip == 1) && ($Line =~ /\.symtab/)) {
769				# This object contains a complete symbol table.
770				$Strip = 0;
771			}
772			next;
773
774		} elsif ($Line =~ /^Dynamic Section/) {
775			$Header = 'Dyn';
776			next;
777		} elsif ($Header ne 'Dyn') {
778			next;
779		}
780
781		# Does this object contain text relocations.
782		if ($Tex && ($Line =~ /TEXTREL/)) {
783			# Determine if this file is allowed text relocations.
784			if (!$opt{a}) {
785				if ($File =~ $SkipTextrelFiles) {
786					$Tex = 0;
787					next ELF;
788				}
789			}
790			OutMsg($Ttl++, $RelPath,
791			    "\tTEXTREL .dynamic tag\t\t\t<no -Kpic?>");
792			$Tex = 0;
793			next;
794		}
795
796		# Does this file have any relocation sections (there are a few
797		# psr libraries with no relocations at all, thus a .SUNW_reloc
798		# section won't exist either).
799		if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
800			$Relsz = hex((split(' ', $Line))[2]);
801			next;
802		}
803
804		# Does this file have any plt relocations.  If the plt size is
805		# equivalent to the total relocation size then we don't have
806		# any relocations suitable for combining into a .SUNW_reloc
807		# section.
808		if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
809			$Pltsz = hex((split(' ', $Line))[2]);
810			next;
811		}
812
813		# Under the -i (information) option print out any useful dynamic
814		# entries.
815		# Does this object have any dependencies.
816		if ($opt{i} && ($Line =~ /NEEDED/)) {
817			my($Need) = (split(' ', $Line))[3];
818
819			# Catch any old (unnecessary) dependencies.
820			if ($Need =~ $OldDeps) {
821				OutMsg($Ttl++, $RelPath,
822				    "\tNEEDED=$Need\t<dependency no longer necessary>");
823			} else {
824				OutMsg($Ttl++, $RelPath, "\tNEEDED=$Need");
825			}
826			next;
827		}
828
829		# Does this object specify a runpath.
830		if ($opt{i} && ($Line =~ /RPATH/)) {
831			my($Rpath) = (split(' ', $Line))[3];
832			OutMsg($Ttl++, $RelPath, "\tRPATH=$Rpath");
833			next;
834		}
835	}
836
837	# A shared object, that contains non-plt relocations, should have a
838	# combined relocation section indicating it was built with -z combreloc.
839	if ($Dll && $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
840		OutMsg($Ttl++, $RelPath,
841		    "\tSUNW_reloc section missing\t\t<no -zcombreloc?>");
842	}
843
844	# No objects released to a customer should have any .stabs sections
845	# remaining, they should be stripped.
846	if ($opt{s} && $Stab) {
847		if (!$opt{a}) {
848			if ($File =~ $SkipStabFiles) {
849				goto DONESTAB;
850			}
851		}
852		OutMsg($Ttl++, $RelPath,
853		    "\tdebugging sections should be deleted\t<no strip -x?>");
854	}
855
856DONESTAB:
857
858	# All objects should have a full symbol table to provide complete
859	# debugging stack traces.
860	if ($Strip) {
861		OutMsg($Ttl++, $RelPath,
862		    "\tsymbol table should not be stripped\t<remove -s?>");
863	}
864}
865
866
867sub ProcDir {
868	my($FullDir, $RelDir) = @_;
869	my($NewFull, $NewRel);
870
871	# Determine if this is a directory we don't care about.
872	if (!$opt{a}) {
873		if ($RelDir =~ $SkipDirs) {
874			return;
875		}
876	}
877
878	# Open the directory and read each entry, omit files starting with "."
879	if (opendir(DIR, $FullDir)) {
880		foreach my $Entry (readdir(DIR)) {
881			if ($Entry =~ /^\./) {
882				next;
883			}
884			$NewFull = "$FullDir/$Entry";
885
886			# Ignore symlinks.
887			if (-l $NewFull) {
888				next;
889			}
890			if (!stat($NewFull)) {
891				next;
892			}
893			$NewRel = "$RelDir/$Entry";
894
895			# Descend into and process any directories.
896			if (-d _) {
897				ProcDir($NewFull, $NewRel);
898				next;
899			}
900
901			# Typically dynamic objects are executable, so we can
902			# reduce the overall cost of this script (a lot!) by
903			# screening out non-executables here, rather than pass
904			# them to file(1) later.  However, it has been known
905			# for shared objects to be mistakenly left non-
906			# executable, so with -a let all files through so that
907			# this requirement can be verified (see ProcFile()).
908			if (!$opt{a}) {
909				if (! -x _) {
910					next;
911				}
912			}
913
914			# Process any standard files.
915			if (-f _) {
916				my($Secure) = 0;
917
918				if (-u _ || -g _) {
919					$Secure = 1;
920				}
921
922				ProcFile($NewFull, $NewRel, $Entry, $Secure);
923				next;
924			}
925
926		}
927		closedir(DIR);
928	}
929}
930
931# Create a crle(1) script for any 64-bit dependencies we locate.  A runtime
932# configuration file will be generated to establish alternative dependency
933# mappings for all these dependencies.
934
935sub Entercrle64 {
936	my($FullDir, $RelDir, $Entry) = @_;
937
938	if (!$Crle64) {
939		# Create and initialize the script if is doesn't already exit.
940
941		$Crle64 = "$Tmpdir/$Prog.crle64.$$";
942		open(CRLE64, "> $Crle64") ||
943			die "$Prog: open failed: $Crle64: $!";
944
945		print CRLE64 "#!/bin/sh\ncrle -64\\\n";
946	}
947	print CRLE64 "\t-o $FullDir -a $RelDir/$Entry \\\n";
948}
949
950# Create a crle(1) script for any 32-bit dependencies we locate.  A runtime
951# configuration file will be generated to establish alternative dependency
952# mappings for all these dependencies.
953
954sub Entercrle32 {
955	my($FullDir, $RelDir, $Entry) = @_;
956
957	if (!$Crle32) {
958		# Create and initialize the script if is doesn't already exit.
959
960		$Crle32 = "$Tmpdir/$Prog.crle32.$$";
961		open(CRLE32, "> $Crle32") ||
962			die "$Prog: open failed: $Crle32: $!";
963
964		print CRLE32 "#!/bin/sh\ncrle \\\n";
965	}
966	print CRLE32 "\t-o $FullDir -a $RelDir/$Entry \\\n";
967}
968
969# Having finished gathering dependencies, complete any crle(1) scripts and
970# execute them to generate the associated runtime configuration files.  In
971# addition establish the environment variable required to pass the configuration
972# files to ldd(1).
973
974sub GenConf {
975	if ($Crle64) {
976		$Conf64 = "$Tmpdir/$Prog.conf64.$$";
977		print CRLE64 "\t-c $Conf64\n";
978
979		chmod 0755, $Crle64;
980		close CRLE64;
981
982		if (system($Crle64)) {
983			undef $Conf64;
984		}
985	}
986	if ($Crle32) {
987		$Conf32 = "$Tmpdir/$Prog.conf32.$$";
988		print CRLE32 "\t-c $Conf32\n";
989
990		chmod 0755, $Crle32;
991		close CRLE32;
992
993		if (system($Crle32)) {
994			undef $Conf32;
995		}
996	}
997
998	if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
999		$Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
1000	} elsif ($Crle64 && $Conf64) {
1001		$Env = "-e LD_FLAGS=config_64=$Conf64";
1002	} elsif ($Crle32 && $Conf32) {
1003		$Env = "-e LD_FLAGS=config_32=$Conf32";
1004	}
1005}
1006
1007# Recurse through a directory hierarchy looking for appropriate dependencies.
1008
1009sub GetDeps {
1010	my($FullDir, $RelDir) = @_;
1011	my($NewFull);
1012
1013	# Open the directory and read each entry, omit files starting with "."
1014	if (opendir(DIR, $FullDir)) {
1015		 foreach my $Entry (readdir(DIR)) {
1016			if ($Entry =~ /^\./) {
1017				next;
1018			}
1019			$NewFull = "$FullDir/$Entry";
1020
1021			# We need to follow links so that any dependencies
1022			# are expressed in all their available forms.
1023			# Bail on symlinks like 32 -> .
1024			if (-l $NewFull) {
1025				if (readlink($NewFull) =~ /^\.$/) {
1026					next;
1027				}
1028			}
1029			if (!stat($NewFull)) {
1030				next;
1031			}
1032
1033			if (!$opt{a}) {
1034				if ($NewFull =~ $SkipCrleConf) {
1035					next;
1036				}
1037			}
1038
1039			# If this is a directory descend into it.
1040			if (-d _) {
1041				my($NewRel);
1042
1043				if ($RelDir =~ /^\/$/) {
1044					$NewRel = "$RelDir$Entry";
1045				} else {
1046					$NewRel = "$RelDir/$Entry";
1047				}
1048
1049				GetDeps($NewFull, $NewRel);
1050				next;
1051			}
1052
1053			# If this is a regular file determine if its a
1054			# valid ELF dependency.
1055			if (-f _) {
1056				my($File);
1057
1058				# Typically shared object dependencies end with
1059				# ".so" or ".so.?", hence we can reduce the cost
1060				# of this script (a lot!) by screening out files
1061				# that don't follow this pattern.
1062				if (!$opt{a}) {
1063					if ($Entry !~ /\.so(?:\.\d+)*$/) {
1064						next;
1065					}
1066				}
1067
1068				$File = `file $NewFull`;
1069				if ($File !~ /dynamic lib/) {
1070					next;
1071				}
1072
1073				if ($File =~ /32-bit/) {
1074					Entercrle32($FullDir, $RelDir, $Entry);
1075				} elsif ($Ena64) {
1076					Entercrle64($FullDir, $RelDir, $Entry);
1077				}
1078				next;
1079			}
1080		}
1081		closedir(DIR);
1082	}
1083}
1084exit $Error
1085