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