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