xref: /titanic_50/usr/src/tools/scripts/check_rtime.pl (revision 75ce41a57ff334bd8fe2cb9ed51eea835892f944)
17c478bd9Sstevel@tonic-gate#!/usr/perl5/bin/perl -w
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6749f21d3Swesolows# Common Development and Distribution License (the "License").
7749f21d3Swesolows# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
22749f21d3Swesolows
237c478bd9Sstevel@tonic-gate#
24567efe8fSDavid Major# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate# Use is subject to license terms.
267c478bd9Sstevel@tonic-gate#
277c478bd9Sstevel@tonic-gate#
28749f21d3Swesolows
29749f21d3Swesolows#
307c478bd9Sstevel@tonic-gate# Check ELF information.
317c478bd9Sstevel@tonic-gate#
327c478bd9Sstevel@tonic-gate# This script descends a directory hierarchy inspecting ELF dynamic executables
337c478bd9Sstevel@tonic-gate# and shared objects.  The general theme is to verify that common Makefile rules
347c478bd9Sstevel@tonic-gate# have been used to build these objects.  Typical failures occur when Makefile
357c478bd9Sstevel@tonic-gate# rules are re-invented rather than being inherited from "cmd/lib" Makefiles.
367c478bd9Sstevel@tonic-gate#
377c478bd9Sstevel@tonic-gate# As always, a number of components don't follow the rules, and these are
38*75ce41a5SAli Bahrami# excluded to reduce this scripts output.
397c478bd9Sstevel@tonic-gate#
407c478bd9Sstevel@tonic-gate# By default any file that has conditions that should be reported is first
417c478bd9Sstevel@tonic-gate# listed and then each condition follows.  The -o (one-line) option produces a
427c478bd9Sstevel@tonic-gate# more terse output which is better for sorting/diffing with "nightly".
437c478bd9Sstevel@tonic-gate#
447c478bd9Sstevel@tonic-gate# NOTE: missing dependencies, symbols or versions are reported by running the
457c478bd9Sstevel@tonic-gate# file through ldd(1).  As objects within a proto area are built to exist in a
467c478bd9Sstevel@tonic-gate# base system, standard use of ldd(1) will bind any objects to dependencies
477c478bd9Sstevel@tonic-gate# that exist in the base system.  It is frequently the case that newer objects
487c478bd9Sstevel@tonic-gate# exist in the proto area that are required to satisfy other objects
497c478bd9Sstevel@tonic-gate# dependencies, and without using these newer objects an ldd(1) will produce
50*75ce41a5SAli Bahrami# misleading error messages.  To compensate for this, the -D/-d options, or the
51*75ce41a5SAli Bahrami# existence of the CODEMSG_WS/ROOT environment variables, cause the creation of
527c478bd9Sstevel@tonic-gate# alternative dependency mappings via crle(1) configuration files that establish
537c478bd9Sstevel@tonic-gate# any proto shared objects as alternatives to their base system location.  Thus
547c478bd9Sstevel@tonic-gate# ldd(1) can be executed against these configuration files so that objects in a
557c478bd9Sstevel@tonic-gate# proto area bind to their dependencies in the same proto area.
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate# Define all global variables (required for strict)
59*75ce41a5SAli Bahramiuse vars  qw($Prog $Env $Ena64 $Tmpdir $Gnuc);
60*75ce41a5SAli Bahramiuse vars  qw($LddNoU $Conf32 $Conf64);
61*75ce41a5SAli Bahramiuse vars  qw(%opt);
62*75ce41a5SAli Bahramiuse vars  qw($ErrFH $ErrTtl $InfoFH $InfoTtl $OutCnt1 $OutCnt2);
63*75ce41a5SAli Bahrami
64*75ce41a5SAli Bahrami# An exception file is used to specify regular expressions to match
65*75ce41a5SAli Bahrami# objects. These directives specify special attributes of the object.
66*75ce41a5SAli Bahrami# The regular expressions are read from the file and compiled into the
67*75ce41a5SAli Bahrami# regular expression variables.
68*75ce41a5SAli Bahrami#
69*75ce41a5SAli Bahrami# The name of each regular expression variable is of the form
70*75ce41a5SAli Bahrami#
71*75ce41a5SAli Bahrami#	$EXRE_xxx
72*75ce41a5SAli Bahrami#
73*75ce41a5SAli Bahrami# where xxx is the name of the exception in lower case. For example,
74*75ce41a5SAli Bahrami# the regular expression variable for EXEC_STACK is $EXRE_exec_stack.
75*75ce41a5SAli Bahrami#
76*75ce41a5SAli Bahrami# onbld_elfmod::LoadExceptionsToEXRE() depends on this naming convention
77*75ce41a5SAli Bahrami# to initialize the regular expression variables, and to detect invalid
78*75ce41a5SAli Bahrami# exception names.
79*75ce41a5SAli Bahrami#
80*75ce41a5SAli Bahrami# If a given exception is not used in the exception file, its regular
81*75ce41a5SAli Bahrami# expression variable will be undefined. Users of these variables must
82*75ce41a5SAli Bahrami# test the variable with defined() prior to use:
83*75ce41a5SAli Bahrami#
84*75ce41a5SAli Bahrami#	defined($EXRE_exec_stack) && ($foo =~ $EXRE_exec_stack)
85*75ce41a5SAli Bahrami#
86*75ce41a5SAli Bahrami# ----
87*75ce41a5SAli Bahrami#
88*75ce41a5SAli Bahrami# The exceptions are:
89*75ce41a5SAli Bahrami#
90*75ce41a5SAli Bahrami#   EXEC_STACK
91*75ce41a5SAli Bahrami#	Objects that are not required to have a non-executable stack
92*75ce41a5SAli Bahrami#
93*75ce41a5SAli Bahrami#   NOCRLEALT
94*75ce41a5SAli Bahrami#	Objects that should be skipped by AltObjectConfig() when building
95*75ce41a5SAli Bahrami#	the crle script that maps objects to the proto area.
96*75ce41a5SAli Bahrami#
97*75ce41a5SAli Bahrami#    NODIRECT
98*75ce41a5SAli Bahrami#	Objects that are not required to use direct bindings
99*75ce41a5SAli Bahrami#
100*75ce41a5SAli Bahrami#    NOSYMSORT
101*75ce41a5SAli Bahrami#	Objects we should not check for duplicate addresses in
102*75ce41a5SAli Bahrami#	the symbol sort sections.
103*75ce41a5SAli Bahrami#
104*75ce41a5SAli Bahrami#    OLDDEP
105*75ce41a5SAli Bahrami#	Objects that are no longer needed because their functionalty
106*75ce41a5SAli Bahrami#	has migrated elsewhere. These are usually pure filters that
107*75ce41a5SAli Bahrami#	point at libc.
108*75ce41a5SAli Bahrami#
109*75ce41a5SAli Bahrami#    SKIP
110*75ce41a5SAli Bahrami#	Files and directories that should be excluded from analysis.
111*75ce41a5SAli Bahrami#
112*75ce41a5SAli Bahrami#    STAB
113*75ce41a5SAli Bahrami#	Objects that are allowed to contain stab debugging sections
114*75ce41a5SAli Bahrami#
115*75ce41a5SAli Bahrami#    TEXTREL
116*75ce41a5SAli Bahrami#	Object for which relocations are allowed to the text segment
117*75ce41a5SAli Bahrami#
118*75ce41a5SAli Bahrami#    UNDEF_REF
119*75ce41a5SAli Bahrami#	Objects that are allowed undefined references
120*75ce41a5SAli Bahrami#
121*75ce41a5SAli Bahrami#    UNREF_OBJ
122*75ce41a5SAli Bahrami#	"unreferenced object=" ldd(1) diagnostics.
123*75ce41a5SAli Bahrami#
124*75ce41a5SAli Bahrami#    UNUSED_DEPS
125*75ce41a5SAli Bahrami#	Objects that are allowed to have unused dependencies
126*75ce41a5SAli Bahrami#
127*75ce41a5SAli Bahrami#    UNUSED_OBJ
128*75ce41a5SAli Bahrami#	Objects that are allowed to be unused dependencies
129*75ce41a5SAli Bahrami#
130*75ce41a5SAli Bahrami#    UNUSED_RPATH
131*75ce41a5SAli Bahrami#	Objects with unused runpaths
132*75ce41a5SAli Bahrami#
133*75ce41a5SAli Bahrami
134*75ce41a5SAli Bahramiuse vars  qw($EXRE_exec_stack $EXRE_nocrlealt $EXRE_nodirect $EXRE_nosymsort);
135*75ce41a5SAli Bahramiuse vars  qw($EXRE_olddep $EXRE_skip $EXRE_stab $EXRE_textrel $EXRE_undef_ref);
136*75ce41a5SAli Bahramiuse vars  qw($EXRE_unref_obj $EXRE_unused_deps $EXRE_unused_obj);
137*75ce41a5SAli Bahramiuse vars  qw($EXRE_unused_rpath);
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gateuse strict;
1407c478bd9Sstevel@tonic-gateuse Getopt::Std;
141*75ce41a5SAli Bahramiuse File::Basename;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate# Reliably compare two OS revisions.  Arguments are <ver1> <op> <ver2>.
1457c478bd9Sstevel@tonic-gate# <op> is the string form of a normal numeric comparison operator.
1467c478bd9Sstevel@tonic-gatesub cmp_os_ver {
1477c478bd9Sstevel@tonic-gate	my @ver1 = split(/\./, $_[0]);
1487c478bd9Sstevel@tonic-gate	my $op = $_[1];
1497c478bd9Sstevel@tonic-gate	my @ver2 = split(/\./, $_[2]);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate	push @ver2, ("0") x $#ver1 - $#ver2;
1527c478bd9Sstevel@tonic-gate	push @ver1, ("0") x $#ver2 - $#ver1;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate	my $diff = 0;
1557c478bd9Sstevel@tonic-gate	while (@ver1 || @ver2) {
1567c478bd9Sstevel@tonic-gate		if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
1577c478bd9Sstevel@tonic-gate			last;
1587c478bd9Sstevel@tonic-gate		}
1597c478bd9Sstevel@tonic-gate	}
1607c478bd9Sstevel@tonic-gate	return (eval "$diff $op 0" ? 1 : 0);
1617c478bd9Sstevel@tonic-gate}
1627c478bd9Sstevel@tonic-gate
163*75ce41a5SAli Bahrami## ProcFile(FullPath, RelPath, File, Class, Type, Verdef)
164*75ce41a5SAli Bahrami#
1657c478bd9Sstevel@tonic-gate# Determine whether this a ELF dynamic object and if so investigate its runtime
1667c478bd9Sstevel@tonic-gate# attributes.
167*75ce41a5SAli Bahrami#
1687c478bd9Sstevel@tonic-gatesub ProcFile {
169*75ce41a5SAli Bahrami	my($FullPath, $RelPath, $Class, $Type, $Verdef) = @_;
170*75ce41a5SAli Bahrami	my(@Elf, @Ldd, $Dyn, $Sym, $Stack);
17167e3a03eSrie	my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort);
172*75ce41a5SAli Bahrami	my($Val, $Header, $IsX86, $RWX, $UnDep);
173*75ce41a5SAli Bahrami	my($HasDirectBinding);
1747c478bd9Sstevel@tonic-gate
175*75ce41a5SAli Bahrami	# Only look at executables and sharable objects
176*75ce41a5SAli Bahrami	return if ($Type ne 'EXEC') && ($Type ne 'DYN');
1777c478bd9Sstevel@tonic-gate
178*75ce41a5SAli Bahrami	# Ignore symbolic links
179*75ce41a5SAli Bahrami	return if -l $FullPath;
180*75ce41a5SAli Bahrami
181*75ce41a5SAli Bahrami	# Is this an object or directory hierarchy we don't care about?
182*75ce41a5SAli Bahrami	return if (defined($EXRE_skip) && ($RelPath =~ $EXRE_skip));
183*75ce41a5SAli Bahrami
184*75ce41a5SAli Bahrami	# Bail if we can't stat the file. Otherwise, note if it is SUID/SGID.
185*75ce41a5SAli Bahrami	return if !stat($FullPath);
186*75ce41a5SAli Bahrami	my $Secure = (-u _ || -g _) ? 1 : 0;
187*75ce41a5SAli Bahrami
188*75ce41a5SAli Bahrami	# Reset output message counts for new input file
189*75ce41a5SAli Bahrami	$$ErrTtl = $$InfoTtl = 0;
190*75ce41a5SAli Bahrami
1917c478bd9Sstevel@tonic-gate	@Ldd = 0;
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate	# Determine whether we have access to inspect the file.
1947c478bd9Sstevel@tonic-gate	if (!(-r $FullPath)) {
195*75ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
196*75ce41a5SAli Bahrami		    "unable to inspect file: permission denied");
1977c478bd9Sstevel@tonic-gate		return;
1987c478bd9Sstevel@tonic-gate	}
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate	# Determine whether we have a executable (static or dynamic) or a
2017c478bd9Sstevel@tonic-gate	# shared object.
202*75ce41a5SAli Bahrami	@Elf = split(/\n/, `elfdump -epdcy $FullPath 2>&1`);
2037c478bd9Sstevel@tonic-gate
204*75ce41a5SAli Bahrami	$Dyn = $Stack = $IsX86 = $RWX = 0;
2057c478bd9Sstevel@tonic-gate	$Header = 'None';
2067c478bd9Sstevel@tonic-gate	foreach my $Line (@Elf) {
2077c478bd9Sstevel@tonic-gate		# If we have an invalid file type (which we can tell from the
2087c478bd9Sstevel@tonic-gate		# first line), or we're processing an archive, bail.
2097c478bd9Sstevel@tonic-gate		if ($Header eq 'None') {
2107c478bd9Sstevel@tonic-gate			if (($Line =~ /invalid file/) ||
2117c478bd9Sstevel@tonic-gate			    ($Line =~ /$FullPath(.*):/)) {
2127c478bd9Sstevel@tonic-gate				return;
2137c478bd9Sstevel@tonic-gate			}
2147c478bd9Sstevel@tonic-gate		}
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate		if ($Line =~ /^ELF Header/) {
2177c478bd9Sstevel@tonic-gate			$Header = 'Ehdr';
218*75ce41a5SAli Bahrami			next;
219*75ce41a5SAli Bahrami		}
2207c478bd9Sstevel@tonic-gate
221*75ce41a5SAli Bahrami		if ($Line =~ /^Program Header/) {
2227c478bd9Sstevel@tonic-gate			$Header = 'Phdr';
2237c478bd9Sstevel@tonic-gate			$RWX = 0;
224*75ce41a5SAli Bahrami			next;
225*75ce41a5SAli Bahrami		}
2267c478bd9Sstevel@tonic-gate
227*75ce41a5SAli Bahrami		if ($Line =~ /^Dynamic Section/) {
2287c478bd9Sstevel@tonic-gate			# A dynamic section indicates we're a dynamic object
2297c478bd9Sstevel@tonic-gate			# (this makes sure we don't check static executables).
2307c478bd9Sstevel@tonic-gate			$Dyn = 1;
231*75ce41a5SAli Bahrami			next;
232*75ce41a5SAli Bahrami		}
2337c478bd9Sstevel@tonic-gate
234*75ce41a5SAli Bahrami		if (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
2357c478bd9Sstevel@tonic-gate			# If it's a X86 object, we need to enforce RW- data.
236*75ce41a5SAli Bahrami			$IsX86 = 1 if $Line =~ /(EM_AMD64|EM_386)/;
237*75ce41a5SAli Bahrami			next;
2387c478bd9Sstevel@tonic-gate		}
239*75ce41a5SAli Bahrami
240*75ce41a5SAli Bahrami		if (($Header eq 'Phdr') &&
2417c478bd9Sstevel@tonic-gate		    ($Line =~ /\[ PF_X  PF_W  PF_R \]/)) {
2427c478bd9Sstevel@tonic-gate			# RWX segment seen.
2437c478bd9Sstevel@tonic-gate			$RWX = 1;
244*75ce41a5SAli Bahrami			next;
2457c478bd9Sstevel@tonic-gate		}
2467c478bd9Sstevel@tonic-gate
247*75ce41a5SAli Bahrami		if (($Header eq 'Phdr') &&
248*75ce41a5SAli Bahrami		    ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
249*75ce41a5SAli Bahrami			# Seen an RWX PT_LOAD segment.
250*75ce41a5SAli Bahrami			if (defined($EXRE_exec_stack) &&
251*75ce41a5SAli Bahrami			    ($RelPath !~ $EXRE_exec_stack)) {
252*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
253*75ce41a5SAli Bahrami				    "application requires non-executable " .
254*75ce41a5SAli Bahrami				    "data\t<no -Mmapfile_noexdata?>");
255*75ce41a5SAli Bahrami			}
256*75ce41a5SAli Bahrami			next;
257*75ce41a5SAli Bahrami		}
258*75ce41a5SAli Bahrami
259*75ce41a5SAli Bahrami		if (($Header eq 'Phdr') && ($Line =~ /\[ PT_SUNWSTACK \]/)) {
2607c478bd9Sstevel@tonic-gate			# This object defines a non-executable stack.
2617c478bd9Sstevel@tonic-gate			$Stack = 1;
262*75ce41a5SAli Bahrami			next;
2637c478bd9Sstevel@tonic-gate		}
2647c478bd9Sstevel@tonic-gate	}
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate	# Determine whether this ELF executable or shared object has a
2677c478bd9Sstevel@tonic-gate	# conforming mcs(1) comment section.  If the correct $(POST_PROCESS)
2687c478bd9Sstevel@tonic-gate	# macros are used, only a 3 or 4 line .comment section should exist
2697c478bd9Sstevel@tonic-gate	# containing one or two "@(#)SunOS" identifying comments (one comment
2707c478bd9Sstevel@tonic-gate	# for a non-debug build, and two for a debug build). The results of
2717c478bd9Sstevel@tonic-gate	# the following split should be three or four lines, the last empty
2727c478bd9Sstevel@tonic-gate	# line being discarded by the split.
2737c478bd9Sstevel@tonic-gate	if ($opt{m}) {
2747c478bd9Sstevel@tonic-gate		my(@Mcs, $Con, $Dev);
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate		@Mcs = split(/\n/, `mcs -p $FullPath 2>&1`);
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate		$Con = $Dev = $Val = 0;
2797c478bd9Sstevel@tonic-gate		foreach my $Line (@Mcs) {
2807c478bd9Sstevel@tonic-gate			$Val++;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate			if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) {
2837c478bd9Sstevel@tonic-gate				$Con = 1;
2847c478bd9Sstevel@tonic-gate				last;
2857c478bd9Sstevel@tonic-gate			}
2867c478bd9Sstevel@tonic-gate			if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) {
2877c478bd9Sstevel@tonic-gate				$Dev = 1;
2887c478bd9Sstevel@tonic-gate				next;
2897c478bd9Sstevel@tonic-gate			}
2907c478bd9Sstevel@tonic-gate			if (($Dev == 0) && ($Val == 4)) {
2917c478bd9Sstevel@tonic-gate				$Con = 1;
2927c478bd9Sstevel@tonic-gate				last;
2937c478bd9Sstevel@tonic-gate			}
2947c478bd9Sstevel@tonic-gate			if (($Dev == 1) && ($Val == 5)) {
2957c478bd9Sstevel@tonic-gate				$Con = 1;
2967c478bd9Sstevel@tonic-gate				last;
2977c478bd9Sstevel@tonic-gate			}
2987c478bd9Sstevel@tonic-gate		}
2997c478bd9Sstevel@tonic-gate		if ($opt{m} && ($Con == 1)) {
300*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
301*75ce41a5SAli Bahrami		    "non-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>");
3027c478bd9Sstevel@tonic-gate		}
3037c478bd9Sstevel@tonic-gate	}
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate	# Applications should contain a non-executable stack definition.
306*75ce41a5SAli Bahrami	if (($Type eq 'EXEC') && ($Stack == 0) &&
307*75ce41a5SAli Bahrami	    (!defined($EXRE_exec_stack) || ($RelPath !~ $EXRE_exec_stack))) {
308*75ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
309*75ce41a5SAli Bahrami		    "non-executable stack required\t<no -Mmapfile_noexstk?>");
3107c478bd9Sstevel@tonic-gate	}
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate	# Having caught any static executables in the mcs(1) check and non-
3137c478bd9Sstevel@tonic-gate	# executable stack definition check, continue with dynamic objects
3147c478bd9Sstevel@tonic-gate	# from now on.
3157c478bd9Sstevel@tonic-gate	if ($Dyn eq 0) {
3167c478bd9Sstevel@tonic-gate		return;
3177c478bd9Sstevel@tonic-gate	}
3187c478bd9Sstevel@tonic-gate
319*75ce41a5SAli Bahrami	# Use ldd unless its a 64-bit object and we lack the hardware.
320*75ce41a5SAli Bahrami	if (($Class == 32) || $Ena64) {
321976d55c9Sab196087		my $LDDFullPath = $FullPath;
322976d55c9Sab196087
3237c478bd9Sstevel@tonic-gate		if ($Secure) {
3247c478bd9Sstevel@tonic-gate			# The execution of a secure application over an nfs file
3257c478bd9Sstevel@tonic-gate			# system mounted nosuid will result in warning messages
3267c478bd9Sstevel@tonic-gate			# being sent to /var/adm/messages.  As this type of
3277c478bd9Sstevel@tonic-gate			# environment can occur with root builds, move the file
3287c478bd9Sstevel@tonic-gate			# being investigated to a safe place first.  In addition
3297c478bd9Sstevel@tonic-gate			# remove its secure permission so that it can be
3307c478bd9Sstevel@tonic-gate			# influenced by any alternative dependency mappings.
3317c478bd9Sstevel@tonic-gate
332*75ce41a5SAli Bahrami			my $File = $RelPath;
333*75ce41a5SAli Bahrami			$File =~ s!^.*/!!;      # basename
334*75ce41a5SAli Bahrami
3357c478bd9Sstevel@tonic-gate			my($TmpPath) = "$Tmpdir/$File";
3367c478bd9Sstevel@tonic-gate
337976d55c9Sab196087			system('cp', $LDDFullPath, $TmpPath);
3387c478bd9Sstevel@tonic-gate			chmod 0777, $TmpPath;
339976d55c9Sab196087			$LDDFullPath = $TmpPath;
3407c478bd9Sstevel@tonic-gate		}
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate		# Use ldd(1) to determine the objects relocatability and use.
3437c478bd9Sstevel@tonic-gate		# By default look for all unreferenced dependencies.  However,
3447c478bd9Sstevel@tonic-gate		# some objects have legitimate dependencies that they do not
3457c478bd9Sstevel@tonic-gate		# reference.
346*75ce41a5SAli Bahrami		if ($LddNoU) {
3477c478bd9Sstevel@tonic-gate			$Lddopt = "-ru";
3487c478bd9Sstevel@tonic-gate		} else {
3497c478bd9Sstevel@tonic-gate			$Lddopt = "-rU";
3507c478bd9Sstevel@tonic-gate		}
351976d55c9Sab196087		@Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
3527c478bd9Sstevel@tonic-gate		if ($Secure) {
353976d55c9Sab196087			unlink $LDDFullPath;
3547c478bd9Sstevel@tonic-gate		}
3557c478bd9Sstevel@tonic-gate	}
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate	$Val = 0;
3587c478bd9Sstevel@tonic-gate	$Sym = 5;
35967e3a03eSrie	$UnDep = 1;
3607c478bd9Sstevel@tonic-gate
36167e3a03eSrie	foreach my $Line (@Ldd) {
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate		if ($Val == 0) {
3647c478bd9Sstevel@tonic-gate			$Val = 1;
3657c478bd9Sstevel@tonic-gate			# Make sure ldd(1) worked.  One possible failure is that
3667c478bd9Sstevel@tonic-gate			# this is an old ldd(1) prior to -e addition (4390308).
3677c478bd9Sstevel@tonic-gate			if ($Line =~ /usage:/) {
3687c478bd9Sstevel@tonic-gate				$Line =~ s/$/\t<old ldd(1)?>/;
369*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
370*75ce41a5SAli Bahrami				    $RelPath, $Line);
3717c478bd9Sstevel@tonic-gate				last;
3727c478bd9Sstevel@tonic-gate			} elsif ($Line =~ /execution failed/) {
373*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
374*75ce41a5SAli Bahrami				    $RelPath, $Line);
3757c478bd9Sstevel@tonic-gate				last;
3767c478bd9Sstevel@tonic-gate			}
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate			# It's possible this binary can't be executed, ie. we've
3797c478bd9Sstevel@tonic-gate			# found a sparc binary while running on an intel system,
3807c478bd9Sstevel@tonic-gate			# or a sparcv9 binary on a sparcv7/8 system.
3817c478bd9Sstevel@tonic-gate			if ($Line =~ /wrong class/) {
382*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
383*75ce41a5SAli Bahrami				    "has wrong class or data encoding");
3847c478bd9Sstevel@tonic-gate				next;
3857c478bd9Sstevel@tonic-gate			}
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate			# Historically, ldd(1) likes executable objects to have
388*75ce41a5SAli Bahrami			# their execute bit set.
3897c478bd9Sstevel@tonic-gate			if ($Line =~ /not executable/) {
390*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
391*75ce41a5SAli Bahrami				    "is not executable");
3927c478bd9Sstevel@tonic-gate				next;
3937c478bd9Sstevel@tonic-gate			}
3947c478bd9Sstevel@tonic-gate		}
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate		# Look for "file" or "versions" that aren't found.  Note that
3977c478bd9Sstevel@tonic-gate		# these lines will occur before we find any symbol referencing
3987c478bd9Sstevel@tonic-gate		# errors.
3997c478bd9Sstevel@tonic-gate		if (($Sym == 5) && ($Line =~ /not found\)/)) {
4007c478bd9Sstevel@tonic-gate			if ($Line =~ /file not found\)/) {
4017c478bd9Sstevel@tonic-gate				$Line =~ s/$/\t<no -zdefs?>/;
4027c478bd9Sstevel@tonic-gate			}
403*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
4047c478bd9Sstevel@tonic-gate			next;
4057c478bd9Sstevel@tonic-gate		}
4067c478bd9Sstevel@tonic-gate		# Look for relocations whose symbols can't be found.  Note, we
4077c478bd9Sstevel@tonic-gate		# only print out the first 5 relocations for any file as this
4087c478bd9Sstevel@tonic-gate		# output can be excessive.
4097c478bd9Sstevel@tonic-gate		if ($Sym && ($Line =~ /symbol not found/)) {
4107c478bd9Sstevel@tonic-gate			# Determine if this file is allowed undefined
4117c478bd9Sstevel@tonic-gate			# references.
412*75ce41a5SAli Bahrami			if (($Sym == 5) && defined($EXRE_undef_ref) &&
413*75ce41a5SAli Bahrami			    ($RelPath =~ $EXRE_undef_ref)) {
4147c478bd9Sstevel@tonic-gate				$Sym = 0;
41567e3a03eSrie				next;
4167c478bd9Sstevel@tonic-gate			}
4177c478bd9Sstevel@tonic-gate			if ($Sym-- == 1) {
418*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
419*75ce41a5SAli Bahrami				    "continued ...") if !$opt{o};
4207c478bd9Sstevel@tonic-gate				next;
4217c478bd9Sstevel@tonic-gate			}
4227c478bd9Sstevel@tonic-gate			# Just print the symbol name.
4237c478bd9Sstevel@tonic-gate			$Line =~ s/$/\t<no -zdefs?>/;
424*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
4257c478bd9Sstevel@tonic-gate			next;
4267c478bd9Sstevel@tonic-gate		}
42767e3a03eSrie		# Look for any unused search paths.
42867e3a03eSrie		if ($Line =~ /unused search path=/) {
429d89524d0Srie			# Note, skip this comparison for __GNUC builds, as the
430d89524d0Srie			# gnu compilers insert numerous unused search paths.
431d89524d0Srie			if ($Gnuc == 1) {
432d89524d0Srie				next;
433d89524d0Srie			}
434*75ce41a5SAli Bahrami			next if defined($EXRE_unused_rpath) &&
435*75ce41a5SAli Bahrami			    ($Line =~ $EXRE_unused_rpath);
436*75ce41a5SAli Bahrami
43767e3a03eSrie			if ($Secure) {
43867e3a03eSrie				$Line =~ s!$Tmpdir/!!;
43967e3a03eSrie			}
44067e3a03eSrie			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
441*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
44267e3a03eSrie			next;
44367e3a03eSrie		}
44467e3a03eSrie		# Look for unreferenced dependencies.  Note, if any unreferenced
44567e3a03eSrie		# objects are ignored, then set $UnDep so as to suppress any
44667e3a03eSrie		# associated unused-object messages.
44767e3a03eSrie		if ($Line =~ /unreferenced object=/) {
448*75ce41a5SAli Bahrami			if (defined($EXRE_unref_obj) &&
449*75ce41a5SAli Bahrami			    ($Line =~ $EXRE_unref_obj)) {
45067e3a03eSrie				$UnDep = 0;
45167e3a03eSrie				next;
45267e3a03eSrie			}
45367e3a03eSrie			if ($Secure) {
45467e3a03eSrie				$Line =~ s!$Tmpdir/!!;
45567e3a03eSrie			}
456*75ce41a5SAli Bahrami			$Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
457*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
45867e3a03eSrie			next;
45967e3a03eSrie		}
4607c478bd9Sstevel@tonic-gate		# Look for any unused dependencies.
46167e3a03eSrie		if ($UnDep && ($Line =~ /unused/)) {
462*75ce41a5SAli Bahrami			# Skip if object is allowed to have unused dependencies
463*75ce41a5SAli Bahrami			next if defined($EXRE_unused_deps) &&
464*75ce41a5SAli Bahrami			    ($RelPath =~ $EXRE_unused_deps);
465*75ce41a5SAli Bahrami
466*75ce41a5SAli Bahrami			# Skip if dependency is always allowed to be unused
467*75ce41a5SAli Bahrami			next if defined($EXRE_unused_obj) &&
468*75ce41a5SAli Bahrami			    ($Line =~ $EXRE_unused_obj);
469*75ce41a5SAli Bahrami
470*75ce41a5SAli Bahrami			$Line =~ s!$Tmpdir/!! if $Secure;
471*75ce41a5SAli Bahrami			$Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
472*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
4737c478bd9Sstevel@tonic-gate			next;
4747c478bd9Sstevel@tonic-gate		}
4757c478bd9Sstevel@tonic-gate	}
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate	# Reuse the elfdump(1) data to investigate additional dynamic linking
4787c478bd9Sstevel@tonic-gate	# information.
4797c478bd9Sstevel@tonic-gate
480dfb96a4fSab196087	$Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
4817c478bd9Sstevel@tonic-gate	$Tex = $Strip = 1;
482f6acbf7cSrie	$HasDirectBinding = 0;
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate	$Header = 'None';
4857c478bd9Sstevel@tonic-gateELF:	foreach my $Line (@Elf) {
4867c478bd9Sstevel@tonic-gate		# We're only interested in the section headers and the dynamic
4877c478bd9Sstevel@tonic-gate		# section.
4887c478bd9Sstevel@tonic-gate		if ($Line =~ /^Section Header/) {
4897c478bd9Sstevel@tonic-gate			$Header = 'Shdr';
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate			if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
4927c478bd9Sstevel@tonic-gate				# This object has a combined relocation section.
4937c478bd9Sstevel@tonic-gate				$Sun = 1;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate			} elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
4967c478bd9Sstevel@tonic-gate				# This object contain .stabs sections
4977c478bd9Sstevel@tonic-gate				$Stab = 1;
498dfb96a4fSab196087			} elsif (($SymSort == 0) &&
499dfb96a4fSab196087				 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) {
500dfb96a4fSab196087				# This object contains a symbol sort section
501dfb96a4fSab196087				$SymSort = 1;
5027c478bd9Sstevel@tonic-gate			}
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate			if (($Strip == 1) && ($Line =~ /\.symtab/)) {
5057c478bd9Sstevel@tonic-gate				# This object contains a complete symbol table.
5067c478bd9Sstevel@tonic-gate				$Strip = 0;
5077c478bd9Sstevel@tonic-gate			}
5087c478bd9Sstevel@tonic-gate			next;
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate		} elsif ($Line =~ /^Dynamic Section/) {
5117c478bd9Sstevel@tonic-gate			$Header = 'Dyn';
5127c478bd9Sstevel@tonic-gate			next;
513f6acbf7cSrie		} elsif ($Line =~ /^Syminfo Section/) {
514f6acbf7cSrie			$Header = 'Syminfo';
515f6acbf7cSrie			next;
516f6acbf7cSrie		} elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) {
517f6acbf7cSrie			next;
518f6acbf7cSrie		}
519f6acbf7cSrie
520f6acbf7cSrie		# Look into the Syminfo section.
521f6acbf7cSrie		# Does this object have at least one Directly Bound symbol?
522f6acbf7cSrie		if (($Header eq 'Syminfo')) {
523f6acbf7cSrie			my(@Symword);
524f6acbf7cSrie
525f6acbf7cSrie			if ($HasDirectBinding == 1) {
526f6acbf7cSrie				next;
527f6acbf7cSrie			}
528f6acbf7cSrie
529f6acbf7cSrie			@Symword = split(' ', $Line);
530f6acbf7cSrie
531f6acbf7cSrie			if (!defined($Symword[1])) {
532f6acbf7cSrie				next;
533f6acbf7cSrie			}
534f6acbf7cSrie			if ($Symword[1] =~ /B/) {
535f6acbf7cSrie				$HasDirectBinding = 1;
536f6acbf7cSrie			}
5377c478bd9Sstevel@tonic-gate			next;
5387c478bd9Sstevel@tonic-gate		}
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate		# Does this object contain text relocations.
5417c478bd9Sstevel@tonic-gate		if ($Tex && ($Line =~ /TEXTREL/)) {
5427c478bd9Sstevel@tonic-gate			# Determine if this file is allowed text relocations.
543*75ce41a5SAli Bahrami			if (defined($EXRE_textrel) &&
544*75ce41a5SAli Bahrami			    ($RelPath =~ $EXRE_textrel)) {
5457c478bd9Sstevel@tonic-gate				$Tex = 0;
5467c478bd9Sstevel@tonic-gate				next ELF;
5477c478bd9Sstevel@tonic-gate			}
548*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
549*75ce41a5SAli Bahrami			    "TEXTREL .dynamic tag\t\t\t<no -Kpic?>");
5507c478bd9Sstevel@tonic-gate			$Tex = 0;
5517c478bd9Sstevel@tonic-gate			next;
5527c478bd9Sstevel@tonic-gate		}
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate		# Does this file have any relocation sections (there are a few
5557c478bd9Sstevel@tonic-gate		# psr libraries with no relocations at all, thus a .SUNW_reloc
5567c478bd9Sstevel@tonic-gate		# section won't exist either).
5577c478bd9Sstevel@tonic-gate		if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
5587c478bd9Sstevel@tonic-gate			$Relsz = hex((split(' ', $Line))[2]);
5597c478bd9Sstevel@tonic-gate			next;
5607c478bd9Sstevel@tonic-gate		}
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate		# Does this file have any plt relocations.  If the plt size is
5637c478bd9Sstevel@tonic-gate		# equivalent to the total relocation size then we don't have
5647c478bd9Sstevel@tonic-gate		# any relocations suitable for combining into a .SUNW_reloc
5657c478bd9Sstevel@tonic-gate		# section.
5667c478bd9Sstevel@tonic-gate		if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
5677c478bd9Sstevel@tonic-gate			$Pltsz = hex((split(' ', $Line))[2]);
5687c478bd9Sstevel@tonic-gate			next;
5697c478bd9Sstevel@tonic-gate		}
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate		# Does this object have any dependencies.
57267e3a03eSrie		if ($Line =~ /NEEDED/) {
5737c478bd9Sstevel@tonic-gate			my($Need) = (split(' ', $Line))[3];
5747c478bd9Sstevel@tonic-gate
575*75ce41a5SAli Bahrami			if (defined($EXRE_olddep) && ($Need =~ $EXRE_olddep)) {
57667e3a03eSrie				# Catch any old (unnecessary) dependencies.
577*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
578*75ce41a5SAli Bahrami			"NEEDED=$Need\t<dependency no longer necessary>");
57967e3a03eSrie			} elsif ($opt{i}) {
58067e3a03eSrie				# Under the -i (information) option print out
58167e3a03eSrie				# any useful dynamic entries.
582*75ce41a5SAli Bahrami				onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
583*75ce41a5SAli Bahrami				    "NEEDED=$Need");
5847c478bd9Sstevel@tonic-gate			}
5857c478bd9Sstevel@tonic-gate			next;
5867c478bd9Sstevel@tonic-gate		}
5877c478bd9Sstevel@tonic-gate
588f6acbf7cSrie		# Is this object built with -B direct flag on?
589f6acbf7cSrie		if ($Line =~ / DIRECT /) {
590f6acbf7cSrie			$HasDirectBinding = 1;
591f6acbf7cSrie		}
592f6acbf7cSrie
5937c478bd9Sstevel@tonic-gate		# Does this object specify a runpath.
5947c478bd9Sstevel@tonic-gate		if ($opt{i} && ($Line =~ /RPATH/)) {
5957c478bd9Sstevel@tonic-gate			my($Rpath) = (split(' ', $Line))[3];
596*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
597*75ce41a5SAli Bahrami			    $RelPath, "RPATH=$Rpath");
5987c478bd9Sstevel@tonic-gate			next;
5997c478bd9Sstevel@tonic-gate		}
6007c478bd9Sstevel@tonic-gate	}
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate	# A shared object, that contains non-plt relocations, should have a
6037c478bd9Sstevel@tonic-gate	# combined relocation section indicating it was built with -z combreloc.
604*75ce41a5SAli Bahrami	if (($Type eq 'DYN') && $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
605*75ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
606*75ce41a5SAli Bahrami		    "SUNW_reloc section missing\t\t<no -zcombreloc?>");
6077c478bd9Sstevel@tonic-gate	}
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate	# No objects released to a customer should have any .stabs sections
6107c478bd9Sstevel@tonic-gate	# remaining, they should be stripped.
6117c478bd9Sstevel@tonic-gate	if ($opt{s} && $Stab) {
612*75ce41a5SAli Bahrami		goto DONESTAB if defined($EXRE_stab) && ($RelPath =~ $EXRE_stab);
613*75ce41a5SAli Bahrami
614*75ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
615*75ce41a5SAli Bahrami		    "debugging sections should be deleted\t<no strip -x?>");
6167c478bd9Sstevel@tonic-gate	}
6177c478bd9Sstevel@tonic-gate
618f6acbf7cSrie	# Identify an object that is not built with either -B direct or
619f6acbf7cSrie	# -z direct.
620*75ce41a5SAli Bahrami	goto DONESTAB
621*75ce41a5SAli Bahrami	    if (defined($EXRE_nodirect) && ($RelPath =~ $EXRE_nodirect));
622*75ce41a5SAli Bahrami
623f6acbf7cSrie	if ($Relsz && ($HasDirectBinding == 0)) {
624*75ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
625*75ce41a5SAli Bahrami		 "object has no direct bindings\t<no -B direct or -z direct?>");
626f6acbf7cSrie	}
627f6acbf7cSrie
6287c478bd9Sstevel@tonic-gateDONESTAB:
6297c478bd9Sstevel@tonic-gate
6308ad60789Srie	# All objects should have a full symbol table to provide complete
6317c478bd9Sstevel@tonic-gate	# debugging stack traces.
632*75ce41a5SAli Bahrami	onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
633*75ce41a5SAli Bahrami	    "symbol table should not be stripped\t<remove -s?>") if $Strip;
634dfb96a4fSab196087
635dfb96a4fSab196087	# If there are symbol sort sections in this object, report on
636dfb96a4fSab196087	# any that have duplicate addresses.
637*75ce41a5SAli Bahrami	ProcSymSort($FullPath, $RelPath) if $SymSort;
638bfed486aSAli Bahrami
639bfed486aSAli Bahrami	# If -v was specified, and the object has a version definition
640bfed486aSAli Bahrami	# section, generate output showing each public symbol and the
641bfed486aSAli Bahrami	# version it belongs to.
642*75ce41a5SAli Bahrami	ProcVerdef($FullPath, $RelPath)
643*75ce41a5SAli Bahrami	    if ($Verdef eq 'VERDEF') && $opt{v};
644dfb96a4fSab196087}
645dfb96a4fSab196087
646dfb96a4fSab196087
647*75ce41a5SAli Bahrami## ProcSymSortOutMsg(RelPath, secname, addr, names...)
648dfb96a4fSab196087#
649*75ce41a5SAli Bahrami# Call onbld_elfmod::OutMsg for a duplicate address error in a symbol sort
650dfb96a4fSab196087# section
651dfb96a4fSab196087#
652dfb96a4fSab196087sub ProcSymSortOutMsg {
653*75ce41a5SAli Bahrami	my($RelPath, $secname, $addr, @names) = @_;
654dfb96a4fSab196087
655*75ce41a5SAli Bahrami	onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
656dfb96a4fSab196087	    "$secname: duplicate $addr: ". join(', ', @names));
657dfb96a4fSab196087}
658dfb96a4fSab196087
659dfb96a4fSab196087
660dfb96a4fSab196087## ProcSymSort(FullPath, RelPath)
661dfb96a4fSab196087#
662dfb96a4fSab196087# Examine the symbol sort sections for the given object and report
663dfb96a4fSab196087# on any duplicate addresses found.  Ideally, mapfile directives
664dfb96a4fSab196087# should be used when building objects that have multiple symbols
665dfb96a4fSab196087# with the same address so that only one of them appears in the sort
666dfb96a4fSab196087# section. This saves space, reduces user confusion, and ensures that
667dfb96a4fSab196087# libproc and debuggers always display public names instead of symbols
668dfb96a4fSab196087# that are merely implementation details.
669dfb96a4fSab196087#
670dfb96a4fSab196087sub ProcSymSort {
671dfb96a4fSab196087
672*75ce41a5SAli Bahrami	my($FullPath, $RelPath) = @_;
673dfb96a4fSab196087
674dfb96a4fSab196087	# If this object is exempt from checking, return quietly
675*75ce41a5SAli Bahrami	return if defined($EXRE_nosymsort) && ($FullPath =~ $EXRE_nosymsort);
676dfb96a4fSab196087
677dfb96a4fSab196087
678dfb96a4fSab196087	open(SORT, "elfdump -S $FullPath|") ||
679dfb96a4fSab196087	    die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
680dfb96a4fSab196087
681dfb96a4fSab196087	my $line;
682dfb96a4fSab196087	my $last_addr;
683dfb96a4fSab196087	my @dups = ();
684dfb96a4fSab196087	my $secname;
685dfb96a4fSab196087	while ($line = <SORT>) {
686dfb96a4fSab196087		chomp $line;
687dfb96a4fSab196087
688dfb96a4fSab196087		next if ($line eq '');
689dfb96a4fSab196087
690dfb96a4fSab196087		# If this is a header line, pick up the section name
691dfb96a4fSab196087		if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
692dfb96a4fSab196087			$secname = $1;
693dfb96a4fSab196087
694dfb96a4fSab196087			# Every new section is followed by a column header line
695dfb96a4fSab196087			$line = <SORT>;		# Toss header line
696dfb96a4fSab196087
697dfb96a4fSab196087			# Flush anything left from previous section
698*75ce41a5SAli Bahrami			ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
699*75ce41a5SAli Bahrami			    if (scalar(@dups) > 1);
700dfb96a4fSab196087
701dfb96a4fSab196087			# Reset variables for new sort section
702dfb96a4fSab196087			$last_addr = '';
703dfb96a4fSab196087			@dups = ();
704dfb96a4fSab196087
705dfb96a4fSab196087			next;
706dfb96a4fSab196087		}
707dfb96a4fSab196087
708dfb96a4fSab196087		# Process symbol line
709dfb96a4fSab196087		my @fields = split /\s+/, $line;
710dfb96a4fSab196087		my $new_addr = $fields[2];
711169e20d9SAli Bahrami		my $new_type = $fields[8];
712dfb96a4fSab196087		my $new_name = $fields[9];
713607c61fdSab196087
714169e20d9SAli Bahrami		if ($new_type eq 'UNDEF') {
715*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
716169e20d9SAli Bahrami			    "$secname: unexpected UNDEF symbol " .
717169e20d9SAli Bahrami			    "(link-editor error): $new_name");
718169e20d9SAli Bahrami			next;
719169e20d9SAli Bahrami		}
720169e20d9SAli Bahrami
721a854c1c1Sab196087		if ($new_addr eq $last_addr) {
722dfb96a4fSab196087			push @dups, $new_name;
723dfb96a4fSab196087		} else {
724*75ce41a5SAli Bahrami			ProcSymSortOutMsg($RelPath, $secname,
725dfb96a4fSab196087			    $last_addr, @dups) if (scalar(@dups) > 1);
726dfb96a4fSab196087			@dups = ( $new_name );
727dfb96a4fSab196087			$last_addr = $new_addr;
728dfb96a4fSab196087		}
729dfb96a4fSab196087	}
730dfb96a4fSab196087
731*75ce41a5SAli Bahrami	ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
732dfb96a4fSab196087		if (scalar(@dups) > 1);
733dfb96a4fSab196087
734dfb96a4fSab196087	close SORT;
7357c478bd9Sstevel@tonic-gate}
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate
738bfed486aSAli Bahrami## ProcVerdef(FullPath, RelPath)
739bfed486aSAli Bahrami#
740bfed486aSAli Bahrami# Examine the version definition section for the given object and report
741bfed486aSAli Bahrami# each public symbol along with the version it belongs to.
742bfed486aSAli Bahrami#
743bfed486aSAli Bahramisub ProcVerdef {
744bfed486aSAli Bahrami
745*75ce41a5SAli Bahrami	my($FullPath, $RelPath) = @_;
746bfed486aSAli Bahrami	my $line;
747bfed486aSAli Bahrami	my $cur_ver = '';
748bfed486aSAli Bahrami	my $tab = $opt{o} ? '' : "\t";
749bfed486aSAli Bahrami
750bfed486aSAli Bahrami	# pvs -dov provides information about the versioning hierarchy
751bfed486aSAli Bahrami	# in the file. Lines are of the format:
752bfed486aSAli Bahrami	#	path - version[XXX];
753bfed486aSAli Bahrami	# where [XXX] indicates optional information, such as flags
754bfed486aSAli Bahrami	# or inherited versions.
755bfed486aSAli Bahrami	#
756bfed486aSAli Bahrami	# Private versions are allowed to change freely, so ignore them.
757bfed486aSAli Bahrami	open(PVS, "pvs -dov $FullPath|") ||
758bfed486aSAli Bahrami	    die "$Prog: Unable to execute pvs (version definition section)\n";
759bfed486aSAli Bahrami
760bfed486aSAli Bahrami	while ($line = <PVS>) {
761bfed486aSAli Bahrami		chomp $line;
762bfed486aSAli Bahrami
763bfed486aSAli Bahrami		if ($line =~ /^[^\s]+\s+-\s+([^;]+)/) {
764bfed486aSAli Bahrami			my $ver = $1;
765bfed486aSAli Bahrami
766bfed486aSAli Bahrami			next if $ver =~ /private/i;
767*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
768*75ce41a5SAli Bahrami			    "${tab}VERDEF=$ver");
769bfed486aSAli Bahrami		}
770bfed486aSAli Bahrami	}
771bfed486aSAli Bahrami	close PVS;
772bfed486aSAli Bahrami
773bfed486aSAli Bahrami	# pvs -dos lists the symbols assigned to each version definition.
774bfed486aSAli Bahrami	# Lines are of the format:
775bfed486aSAli Bahrami	#	path - version: symbol;
776bfed486aSAli Bahrami	#	path - version: symbol (size);
777bfed486aSAli Bahrami	# where the (size) is added to data items, but not for functions.
778bfed486aSAli Bahrami	# We strip off the size, if present.
779bfed486aSAli Bahrami
780bfed486aSAli Bahrami	open(PVS, "pvs -dos $FullPath|") ||
781bfed486aSAli Bahrami	    die "$Prog: Unable to execute pvs (version definition section)\n";
782bfed486aSAli Bahrami	while ($line = <PVS>) {
783bfed486aSAli Bahrami		chomp $line;
784bfed486aSAli Bahrami		if ($line =~ /^[^\s]+\s+-\s+([^:]+):\s*([^\s;]+)/) {
785bfed486aSAli Bahrami		    my $ver = $1;
786bfed486aSAli Bahrami		    my $sym = $2;
787bfed486aSAli Bahrami
788bfed486aSAli Bahrami		    next if $ver =~ /private/i;
789bfed486aSAli Bahrami
790bfed486aSAli Bahrami		    if ($opt{o}) {
791*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
792bfed486aSAli Bahrami			    "VERSION=$ver, SYMBOL=$sym");
793bfed486aSAli Bahrami		    } else {
794bfed486aSAli Bahrami			if ($cur_ver ne $ver) {
795*75ce41a5SAli Bahrami			    onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
796*75ce41a5SAli Bahrami			        $RelPath, "VERSION=$ver");
797bfed486aSAli Bahrami			    $cur_ver = $ver;
798bfed486aSAli Bahrami			}
799*75ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
800*75ce41a5SAli Bahrami			    $RelPath, "SYMBOL=$sym");
801bfed486aSAli Bahrami		    }
802bfed486aSAli Bahrami		}
803bfed486aSAli Bahrami	}
804bfed486aSAli Bahrami
805bfed486aSAli Bahrami	close PVS;
806bfed486aSAli Bahrami}
807bfed486aSAli Bahrami
808bfed486aSAli Bahrami
809*75ce41a5SAli Bahrami## OpenFindElf(file, FileHandleRef, LineNumRef)
810*75ce41a5SAli Bahrami#
811*75ce41a5SAli Bahrami# Open file in 'find_elf -r' format, and return the value of
812*75ce41a5SAli Bahrami# the opening PREFIX line.
813*75ce41a5SAli Bahrami#
814*75ce41a5SAli Bahrami# entry:
815*75ce41a5SAli Bahrami#	file - file, or find_elf child process, to open
816*75ce41a5SAli Bahrami#	FileHandleRef - Reference to file handle to open
817*75ce41a5SAli Bahrami#	LineNumRef - Reference to integer to increment as lines are input
818*75ce41a5SAli Bahrami#
819*75ce41a5SAli Bahrami# exit:
820*75ce41a5SAli Bahrami#	This routine issues a fatal error and does not return on error.
821*75ce41a5SAli Bahrami#	Otherwise, the value of PREFIX is returned.
822*75ce41a5SAli Bahrami#
823*75ce41a5SAli Bahramisub OpenFindElf {
824*75ce41a5SAli Bahrami	my ($file, $fh, $LineNum) = @_;
825*75ce41a5SAli Bahrami	my $line;
826*75ce41a5SAli Bahrami	my $prefix;
8277c478bd9Sstevel@tonic-gate
828*75ce41a5SAli Bahrami	open($fh, $file) || die "$Prog: Unable to open: $file";
829*75ce41a5SAli Bahrami	$$LineNum = 0;
830*75ce41a5SAli Bahrami
831*75ce41a5SAli Bahrami	# This script requires relative paths as created by 'find_elf -r'.
832*75ce41a5SAli Bahrami	# When this is done, the first non-comment line will always
833*75ce41a5SAli Bahrami	# be PREFIX. Obtain that line, or issue a fatal error.
834*75ce41a5SAli Bahrami	while ($line = onbld_elfmod::GetLine($fh, $LineNum)) {
835*75ce41a5SAli Bahrami		if ($line =~ /^PREFIX\s+(.*)$/i) {
836*75ce41a5SAli Bahrami			$prefix = $1;
837*75ce41a5SAli Bahrami			last;
8387c478bd9Sstevel@tonic-gate		}
8397c478bd9Sstevel@tonic-gate
840*75ce41a5SAli Bahrami		die "$Prog: No PREFIX line seen on line $$LineNum: $file";
8417c478bd9Sstevel@tonic-gate	}
8427c478bd9Sstevel@tonic-gate
843*75ce41a5SAli Bahrami	$prefix;
8447c478bd9Sstevel@tonic-gate}
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate
847*75ce41a5SAli Bahrami## ProcFindElf(file)
848*75ce41a5SAli Bahrami#
849*75ce41a5SAli Bahrami# Open the specified file, which must be produced by "find_elf -r",
850*75ce41a5SAli Bahrami# and process the files it describes.
851*75ce41a5SAli Bahrami#
852*75ce41a5SAli Bahramisub ProcFindElf {
853*75ce41a5SAli Bahrami	my $file = $_[0];
854*75ce41a5SAli Bahrami	my $line;
855*75ce41a5SAli Bahrami	my $LineNum;
856*75ce41a5SAli Bahrami
857*75ce41a5SAli Bahrami	my $prefix = OpenFindElf($file, \*FIND_ELF, \$LineNum);
858*75ce41a5SAli Bahrami
859*75ce41a5SAli Bahrami	while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
860*75ce41a5SAli Bahrami		next if !($line =~ /^OBJECT\s/i);
861*75ce41a5SAli Bahrami
862*75ce41a5SAli Bahrami		my ($item, $class, $type, $verdef, $obj) =
863*75ce41a5SAli Bahrami		    split(/\s+/, $line, 5);
864*75ce41a5SAli Bahrami
865*75ce41a5SAli Bahrami		ProcFile("$prefix/$obj", $obj, $class, $type, $verdef);
8667c478bd9Sstevel@tonic-gate	}
8677c478bd9Sstevel@tonic-gate
868*75ce41a5SAli Bahrami	close FIND_ELF;
8697c478bd9Sstevel@tonic-gate}
8707c478bd9Sstevel@tonic-gate
871*75ce41a5SAli Bahrami
872*75ce41a5SAli Bahrami## AltObjectConfig(file)
873*75ce41a5SAli Bahrami#
874*75ce41a5SAli Bahrami# Recurse through a directory hierarchy looking for appropriate dependencies
875*75ce41a5SAli Bahrami# to map from their standard system locations to the proto area via a crle
876*75ce41a5SAli Bahrami# config file.
877*75ce41a5SAli Bahrami#
878*75ce41a5SAli Bahrami# entry:
879*75ce41a5SAli Bahrami#	file - File of ELF objects, in 'find_elf -r' format, to examine.
880*75ce41a5SAli Bahrami#
881*75ce41a5SAli Bahrami# exit:
882*75ce41a5SAli Bahrami#	Scripts are generated for the 32 and 64-bit cases to run crle
883*75ce41a5SAli Bahrami#	and create runtime configuration files that will establish
884*75ce41a5SAli Bahrami#	alternative dependency mappings for the objects identified.
885*75ce41a5SAli Bahrami#
886*75ce41a5SAli Bahrami#	$Env - Set to environment variable definitions that will cause
887*75ce41a5SAli Bahrami#		the config files generated by this routine to be used
888*75ce41a5SAli Bahrami#		by ldd.
889*75ce41a5SAli Bahrami#	$Conf32, $Conf64 - Undefined, or set to the config files generated
890*75ce41a5SAli Bahrami#		by this routine. If defined, the caller is responsible for
891*75ce41a5SAli Bahrami#		unlinking the files before exiting.
892*75ce41a5SAli Bahrami#
893*75ce41a5SAli Bahramisub AltObjectConfig {
894*75ce41a5SAli Bahrami	my $file = $_[0];
895*75ce41a5SAli Bahrami	my ($Crle32, $Crle64);
896*75ce41a5SAli Bahrami	my $line;
897*75ce41a5SAli Bahrami	my $LineNum;
898*75ce41a5SAli Bahrami	my $obj_path;
899*75ce41a5SAli Bahrami	my $obj_active = 0;
900*75ce41a5SAli Bahrami	my $obj_class;
901*75ce41a5SAli Bahrami
902*75ce41a5SAli Bahrami	my $prefix = OpenFindElf($file, \*FIND_ELF);
903*75ce41a5SAli Bahrami
904*75ce41a5SAli BahramiLINE:
905*75ce41a5SAli Bahrami	while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
906*75ce41a5SAli Bahrami	      ITEM: {
907*75ce41a5SAli Bahrami
908*75ce41a5SAli Bahrami			if ($line =~ /^OBJECT\s/i) {
909*75ce41a5SAli Bahrami				my ($item, $class, $type, $verdef, $obj) =
910*75ce41a5SAli Bahrami				    split(/\s+/, $line, 5);
911*75ce41a5SAli Bahrami
912*75ce41a5SAli Bahrami				if ($type eq 'DYN') {
913*75ce41a5SAli Bahrami					$obj_active = 1;
914*75ce41a5SAli Bahrami					$obj_path = $obj;
915*75ce41a5SAli Bahrami					$obj_class = $class;
916*75ce41a5SAli Bahrami				} else {
917*75ce41a5SAli Bahrami					# Only want sharable objects
918*75ce41a5SAli Bahrami					$obj_active = 0;
9197c478bd9Sstevel@tonic-gate				}
920*75ce41a5SAli Bahrami				last ITEM;
9217c478bd9Sstevel@tonic-gate			}
9227c478bd9Sstevel@tonic-gate
923*75ce41a5SAli Bahrami			# We need to follow links to sharable objects so
924*75ce41a5SAli Bahrami			# that any dependencies are expressed in all their
925*75ce41a5SAli Bahrami			# available forms. We depend on ALIAS lines directly
926*75ce41a5SAli Bahrami			# following the object they alias, so if we have
927*75ce41a5SAli Bahrami			# a current object, this alias belongs to it.
928*75ce41a5SAli Bahrami			if ($obj_active && ($line =~ /^ALIAS\s/i)) {
929*75ce41a5SAli Bahrami				my ($item, $real_obj, $obj) =
930*75ce41a5SAli Bahrami				    split(/\s+/, $line, 3);
931*75ce41a5SAli Bahrami				$obj_path = $obj;
932*75ce41a5SAli Bahrami				last ITEM;
9337c478bd9Sstevel@tonic-gate			}
9347c478bd9Sstevel@tonic-gate
935*75ce41a5SAli Bahrami			# Skip unrecognized item
936*75ce41a5SAli Bahrami			next LINE;
937*75ce41a5SAli Bahrami		}
9387c478bd9Sstevel@tonic-gate
939*75ce41a5SAli Bahrami		next if !$obj_active;
9407c478bd9Sstevel@tonic-gate
941*75ce41a5SAli Bahrami		my $full = "$prefix/$obj_path";
942*75ce41a5SAli Bahrami
943*75ce41a5SAli Bahrami		next if defined($EXRE_nocrlealt) &&
944*75ce41a5SAli Bahrami		    ($obj_path =~ $EXRE_nocrlealt);
945*75ce41a5SAli Bahrami
946*75ce41a5SAli Bahrami		my $Dir = $full;
947*75ce41a5SAli Bahrami		$Dir =~ s/^(.*)\/.*$/$1/;
948*75ce41a5SAli Bahrami
949*75ce41a5SAli Bahrami		# Create a crle(1) script for the dependency we've found.
950*75ce41a5SAli Bahrami		# We build separate scripts for the 32 and 64-bit cases.
951*75ce41a5SAli Bahrami		# We create and initialize each script when we encounter
952*75ce41a5SAli Bahrami		# the first object that needs it.
953*75ce41a5SAli Bahrami		if ($obj_class == 32) {
9547c478bd9Sstevel@tonic-gate			if (!$Crle32) {
9557c478bd9Sstevel@tonic-gate				$Crle32 = "$Tmpdir/$Prog.crle32.$$";
9567c478bd9Sstevel@tonic-gate				open(CRLE32, "> $Crle32") ||
9577c478bd9Sstevel@tonic-gate				    die "$Prog: open failed: $Crle32: $!";
9587c478bd9Sstevel@tonic-gate				print CRLE32 "#!/bin/sh\ncrle \\\n";
9597c478bd9Sstevel@tonic-gate			}
960*75ce41a5SAli Bahrami			print CRLE32 "\t-o $Dir -a /$obj_path \\\n";
961*75ce41a5SAli Bahrami		} elsif ($Ena64) {
962*75ce41a5SAli Bahrami			if (!$Crle64) {
963*75ce41a5SAli Bahrami				$Crle64 = "$Tmpdir/$Prog.crle64.$$";
964*75ce41a5SAli Bahrami				open(CRLE64, "> $Crle64") ||
965*75ce41a5SAli Bahrami				    die "$Prog: open failed: $Crle64: $!";
966*75ce41a5SAli Bahrami				print CRLE64 "#!/bin/sh\ncrle -64\\\n";
967*75ce41a5SAli Bahrami			}
968*75ce41a5SAli Bahrami			print CRLE64 "\t-o $Dir -a /$obj_path \\\n";
969*75ce41a5SAli Bahrami		}
9707c478bd9Sstevel@tonic-gate	}
9717c478bd9Sstevel@tonic-gate
972*75ce41a5SAli Bahrami	close FIND_ELF;
9737c478bd9Sstevel@tonic-gate
974*75ce41a5SAli Bahrami
975*75ce41a5SAli Bahrami	# Now that the config scripts are complete, use them to generate
976*75ce41a5SAli Bahrami	# runtime linker config files.
9777c478bd9Sstevel@tonic-gate	if ($Crle64) {
9787c478bd9Sstevel@tonic-gate		$Conf64 = "$Tmpdir/$Prog.conf64.$$";
9797c478bd9Sstevel@tonic-gate		print CRLE64 "\t-c $Conf64\n";
9807c478bd9Sstevel@tonic-gate
9817c478bd9Sstevel@tonic-gate		chmod 0755, $Crle64;
9827c478bd9Sstevel@tonic-gate		close CRLE64;
9837c478bd9Sstevel@tonic-gate
984*75ce41a5SAli Bahrami		undef $Conf64 if system($Crle64);
985*75ce41a5SAli Bahrami
986*75ce41a5SAli Bahrami		# Done with the script
987*75ce41a5SAli Bahrami		unlink $Crle64;
9887c478bd9Sstevel@tonic-gate	}
9897c478bd9Sstevel@tonic-gate	if ($Crle32) {
9907c478bd9Sstevel@tonic-gate		$Conf32 = "$Tmpdir/$Prog.conf32.$$";
9917c478bd9Sstevel@tonic-gate		print CRLE32 "\t-c $Conf32\n";
9927c478bd9Sstevel@tonic-gate
9937c478bd9Sstevel@tonic-gate		chmod 0755, $Crle32;
9947c478bd9Sstevel@tonic-gate		close CRLE32;
9957c478bd9Sstevel@tonic-gate
996*75ce41a5SAli Bahrami		undef $Conf32 if system($Crle32);
997*75ce41a5SAli Bahrami
998*75ce41a5SAli Bahrami		# Done with the script
999*75ce41a5SAli Bahrami		unlink $Crle32;
10007c478bd9Sstevel@tonic-gate	}
10017c478bd9Sstevel@tonic-gate
1002*75ce41a5SAli Bahrami	# Set $Env so that we will use the config files generated above
1003*75ce41a5SAli Bahrami	# when we run ldd.
10047c478bd9Sstevel@tonic-gate	if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
10057c478bd9Sstevel@tonic-gate		$Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
10067c478bd9Sstevel@tonic-gate	} elsif ($Crle64 && $Conf64) {
10077c478bd9Sstevel@tonic-gate		$Env = "-e LD_FLAGS=config_64=$Conf64";
10087c478bd9Sstevel@tonic-gate	} elsif ($Crle32 && $Conf32) {
10097c478bd9Sstevel@tonic-gate		$Env = "-e LD_FLAGS=config_32=$Conf32";
10107c478bd9Sstevel@tonic-gate	}
10117c478bd9Sstevel@tonic-gate}
10127c478bd9Sstevel@tonic-gate
1013*75ce41a5SAli Bahrami# -----------------------------------------------------------------------------
10147c478bd9Sstevel@tonic-gate
1015*75ce41a5SAli Bahrami# This script relies on ldd returning output reflecting only the binary
1016*75ce41a5SAli Bahrami# contents.  But if LD_PRELOAD* environment variables are present, libraries
1017*75ce41a5SAli Bahrami# named by them will also appear in the output, disrupting our analysis.
1018*75ce41a5SAli Bahrami# So, before we get too far, scrub the environment.
10197c478bd9Sstevel@tonic-gate
1020*75ce41a5SAli Bahramidelete($ENV{LD_PRELOAD});
1021*75ce41a5SAli Bahramidelete($ENV{LD_PRELOAD_32});
1022*75ce41a5SAli Bahramidelete($ENV{LD_PRELOAD_64});
10237c478bd9Sstevel@tonic-gate
1024*75ce41a5SAli Bahrami# Establish a program name for any error diagnostics.
1025*75ce41a5SAli Bahramichomp($Prog = `basename $0`);
10267c478bd9Sstevel@tonic-gate
1027*75ce41a5SAli Bahrami# The onbld_elfmod package is maintained in the same directory as this
1028*75ce41a5SAli Bahrami# script, and is installed in ../lib/perl. Use the local one if present,
1029*75ce41a5SAli Bahrami# and the installed one otherwise.
1030*75ce41a5SAli Bahramimy $moddir = dirname($0);
1031*75ce41a5SAli Bahrami$moddir = "$moddir/../lib/perl" if ! -f "$moddir/onbld_elfmod.pm";
1032*75ce41a5SAli Bahramirequire "$moddir/onbld_elfmod.pm";
1033*75ce41a5SAli Bahrami
1034*75ce41a5SAli Bahrami# Determine what machinery is available.
1035*75ce41a5SAli Bahramimy $Mach = `uname -p`;
1036*75ce41a5SAli Bahramimy$Isalist = `isalist`;
1037*75ce41a5SAli Bahramiif ($Mach =~ /sparc/) {
1038*75ce41a5SAli Bahrami	if ($Isalist =~ /sparcv9/) {
1039*75ce41a5SAli Bahrami		$Ena64 = "ok";
1040*75ce41a5SAli Bahrami	}
1041*75ce41a5SAli Bahrami} elsif ($Mach =~ /i386/) {
1042*75ce41a5SAli Bahrami	if ($Isalist =~ /amd64/) {
1043*75ce41a5SAli Bahrami		$Ena64 = "ok";
10444840e086Srie	}
10454840e086Srie}
10464840e086Srie
1047*75ce41a5SAli Bahrami# $Env is used with all calls to ldd. It is set by AltObjectConfig to
1048*75ce41a5SAli Bahrami# cause an alternate object mapping runtime config file to be used.
1049*75ce41a5SAli Bahrami$Env = '';
10507c478bd9Sstevel@tonic-gate
1051*75ce41a5SAli Bahrami# Check that we have arguments.
1052*75ce41a5SAli Bahramiif ((getopts('D:d:E:e:f:I:imosvw:', \%opt) == 0) ||
1053*75ce41a5SAli Bahrami    (!$opt{f} && ($#ARGV == -1))) {
1054*75ce41a5SAli Bahrami	print "usage: $Prog [-imosv] [-D depfile | -d depdir] [-E errfile]\n";
1055*75ce41a5SAli Bahrami	print "\t\t[-e exfile] [-f listfile] [-I infofile] [-w outdir]\n";
1056*75ce41a5SAli Bahrami	print "\t\t[file | dir]...\n";
1057*75ce41a5SAli Bahrami	print "\n";
1058*75ce41a5SAli Bahrami	print "\t[-D depfile]\testablish dependencies from 'find_elf -r' file list\n";
1059*75ce41a5SAli Bahrami	print "\t[-d depdir]\testablish dependencies from under directory\n";
1060*75ce41a5SAli Bahrami	print "\t[-E errfile]\tdirect error output to file\n";
1061*75ce41a5SAli Bahrami	print "\t[-e exfile]\texceptions file\n";
1062*75ce41a5SAli Bahrami	print "\t[-f listfile]\tuse file list produced by find_elf -r\n";
1063*75ce41a5SAli Bahrami	print "\t[-I infofile]\tdirect informational output (-i, -v) to file\n";
1064*75ce41a5SAli Bahrami	print "\t[-i]\t\tproduce dynamic table entry information\n";
1065*75ce41a5SAli Bahrami	print "\t[-m]\t\tprocess mcs(1) comments\n";
1066*75ce41a5SAli Bahrami	print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
1067*75ce41a5SAli Bahrami	print "\t[-s]\t\tprocess .stab and .symtab entries\n";
1068*75ce41a5SAli Bahrami	print "\t[-v]\t\tprocess version definition entries\n";
1069*75ce41a5SAli Bahrami	print "\t[-w outdir]\tinterpret all files relative to given directory\n";
1070*75ce41a5SAli Bahrami	exit 1;
1071*75ce41a5SAli Bahrami}
1072*75ce41a5SAli Bahrami
1073*75ce41a5SAli Bahramidie "$Prog: -D and -d options are mutually exclusive\n" if ($opt{D} && $opt{d});
1074*75ce41a5SAli Bahrami
1075*75ce41a5SAli Bahrami$Tmpdir = "/tmp" if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir));
1076*75ce41a5SAli Bahrami
1077*75ce41a5SAli Bahrami# Determine whether this is a __GNUC build.  If so, unused search path
1078*75ce41a5SAli Bahrami# processing is disabled.
1079*75ce41a5SAli Bahrami$Gnuc = defined $ENV{__GNUC} ? 1 : 0;
1080*75ce41a5SAli Bahrami
1081*75ce41a5SAli Bahrami# If -w, change working directory to given location
1082*75ce41a5SAli Bahrami!$opt{w} || chdir($opt{w}) || die "$Prog: can't cd to $opt{w}";
1083*75ce41a5SAli Bahrami
1084*75ce41a5SAli Bahrami# Locate and process the exceptions file
1085*75ce41a5SAli Bahramionbld_elfmod::LoadExceptionsToEXRE('check_rtime');
1086*75ce41a5SAli Bahrami
1087*75ce41a5SAli Bahrami# Is there a proto area available, either via the -d option, or because
1088*75ce41a5SAli Bahrami# we are part of an activated workspace?
1089*75ce41a5SAli Bahramimy $Proto;
1090*75ce41a5SAli Bahramiif ($opt{d}) {
1091*75ce41a5SAli Bahrami	# User specified dependency directory - make sure it exists.
1092*75ce41a5SAli Bahrami	-d $opt{d} || die "$Prog: $opt{d} is not a directory\n";
1093*75ce41a5SAli Bahrami	$Proto = $opt{d};
1094*75ce41a5SAli Bahrami} elsif ($ENV{CODEMGR_WS}) {
1095*75ce41a5SAli Bahrami	my $Root;
1096*75ce41a5SAli Bahrami
1097*75ce41a5SAli Bahrami	# Without a user specified dependency directory see if we're
1098*75ce41a5SAli Bahrami	# part of a codemanager workspace and if a proto area exists.
1099*75ce41a5SAli Bahrami	$Proto = $Root if ($Root = $ENV{ROOT}) && (-d $Root);
1100*75ce41a5SAli Bahrami}
1101*75ce41a5SAli Bahrami
1102*75ce41a5SAli Bahrami# If we are basing this analysis off the sharable objects found in
1103*75ce41a5SAli Bahrami# a proto area, then gather dependencies and construct an alternative
1104*75ce41a5SAli Bahrami# dependency mapping via a crle(1) configuration file.
1105*75ce41a5SAli Bahrami#
1106*75ce41a5SAli Bahrami# To support alternative dependency mapping we'll need ldd(1)'s
1107*75ce41a5SAli Bahrami# -e option.  This is relatively new (s81_30), so make sure
1108*75ce41a5SAli Bahrami# ldd(1) is capable before gathering any dependency information.
1109*75ce41a5SAli Bahramiif ($opt{D} || $Proto) {
1110*75ce41a5SAli Bahrami	if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
1111*75ce41a5SAli Bahrami		print "ldd: does not support -e, unable to ";
1112*75ce41a5SAli Bahrami		print "create alternative dependency mappingings.\n";
1113*75ce41a5SAli Bahrami		print "ldd: option added under 4390308 (s81_30).\n\n";
11147c478bd9Sstevel@tonic-gate	} else {
1115*75ce41a5SAli Bahrami		# If -D was specified, it supplies a list of files in
1116*75ce41a5SAli Bahrami		# 'find_elf -r' format, and can use it directly. Otherwise,
1117*75ce41a5SAli Bahrami		# we will run find_elf as a child process to find the
1118*75ce41a5SAli Bahrami		# sharable objects found under $Proto.
1119*75ce41a5SAli Bahrami		AltObjectConfig($opt{D} ? $opt{D} : "find_elf -frs $Proto|");
11207c478bd9Sstevel@tonic-gate	}
11217c478bd9Sstevel@tonic-gate}
11227c478bd9Sstevel@tonic-gate
1123*75ce41a5SAli Bahrami# To support unreferenced dependency detection we'll need ldd(1)'s -U
1124*75ce41a5SAli Bahrami# option.  This is relatively new (4638070), and if not available we
1125*75ce41a5SAli Bahrami# can still fall back to -u.  Even with this option, don't use -U with
1126*75ce41a5SAli Bahrami# releases prior to 5.10 as the cleanup for -U use only got integrated
1127*75ce41a5SAli Bahrami# into 5.10 under 4642023.  Note, that nightly doesn't typically set a
1128*75ce41a5SAli Bahrami# RELEASE from the standard <env> files.  Users who wish to disable use
1129*75ce41a5SAli Bahrami# of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
1130*75ce41a5SAli Bahrami# if using nightly, or otherwise establish it in their environment.
1131*75ce41a5SAli Bahramiif (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
1132*75ce41a5SAli Bahrami	$LddNoU = 1;
1133*75ce41a5SAli Bahrami} else {
1134*75ce41a5SAli Bahrami	my($Release);
1135*75ce41a5SAli Bahrami
1136*75ce41a5SAli Bahrami	if (($Release = $ENV{RELEASE}) && (cmp_os_ver($Release, "<", "5.10"))) {
1137*75ce41a5SAli Bahrami		$LddNoU = 1;
1138*75ce41a5SAli Bahrami	} else {
1139*75ce41a5SAli Bahrami		$LddNoU = 0;
1140*75ce41a5SAli Bahrami	}
11417c478bd9Sstevel@tonic-gate}
11427c478bd9Sstevel@tonic-gate
1143*75ce41a5SAli Bahrami# Set up variables used to handle output files:
1144*75ce41a5SAli Bahrami#
1145*75ce41a5SAli Bahrami# Error messages go to stdout unless -E is specified. $ErrFH is a
1146*75ce41a5SAli Bahrami# file handle reference that points at the file handle where error messages
1147*75ce41a5SAli Bahrami# are sent, and $ErrTtl is a reference that points at an integer used
1148*75ce41a5SAli Bahrami# to count how many lines have been sent there.
1149*75ce41a5SAli Bahrami#
1150*75ce41a5SAli Bahrami# Informational messages go to stdout unless -I is specified. $InfoFH is a
1151*75ce41a5SAli Bahrami# file handle reference that points at the file handle where info messages
1152*75ce41a5SAli Bahrami# are sent, and $InfoTtl is a reference that points at an integer used
1153*75ce41a5SAli Bahrami# to count how many lines have been sent there.
1154*75ce41a5SAli Bahrami#
1155*75ce41a5SAli Bahramiif ($opt{E}) {
1156*75ce41a5SAli Bahrami	open(ERROR, ">$opt{E}") || die "$Prog: open failed: $opt{E}";
1157*75ce41a5SAli Bahrami	$ErrFH = \*ERROR;
1158*75ce41a5SAli Bahrami} else {
1159*75ce41a5SAli Bahrami	$ErrFH = \*STDOUT;
11607c478bd9Sstevel@tonic-gate}
1161*75ce41a5SAli Bahrami
1162*75ce41a5SAli Bahramiif ($opt{I}) {
1163*75ce41a5SAli Bahrami	open(INFO, ">$opt{I}") || die "$Prog: open failed: $opt{I}";
1164*75ce41a5SAli Bahrami	$InfoFH = \*INFO;
1165*75ce41a5SAli Bahrami} else {
1166*75ce41a5SAli Bahrami	$InfoFH = \*STDOUT;
11677c478bd9Sstevel@tonic-gate}
1168*75ce41a5SAli Bahramimy ($err_dev, $err_ino) = stat($ErrFH);
1169*75ce41a5SAli Bahramimy ($info_dev, $info_ino) = stat($InfoFH);
1170*75ce41a5SAli Bahrami$ErrTtl = \$OutCnt1;
1171*75ce41a5SAli Bahrami$InfoTtl = (($err_dev == $info_dev) && ($err_ino == $info_ino)) ?
1172*75ce41a5SAli Bahrami    \$OutCnt1 : \$OutCnt2;
1173*75ce41a5SAli Bahrami
1174*75ce41a5SAli Bahrami
1175*75ce41a5SAli Bahrami# If we were given a list of objects in 'find_elf -r' format, then
1176*75ce41a5SAli Bahrami# process it.
1177*75ce41a5SAli BahramiProcFindElf($opt{f}) if $opt{f};
1178*75ce41a5SAli Bahrami
1179*75ce41a5SAli Bahrami# Process each argument
1180*75ce41a5SAli Bahramiforeach my $Arg (@ARGV) {
1181*75ce41a5SAli Bahrami	# Run find_elf to find the files given by $Arg and process them
1182*75ce41a5SAli Bahrami	ProcFindElf("find_elf -fr $Arg|");
11837c478bd9Sstevel@tonic-gate}
1184*75ce41a5SAli Bahrami
1185*75ce41a5SAli Bahrami# Cleanup output files
1186*75ce41a5SAli Bahramiunlink $Conf64 if $Conf64;
1187*75ce41a5SAli Bahramiunlink $Conf32 if $Conf32;
1188*75ce41a5SAli Bahramiclose ERROR if $opt{E};
1189*75ce41a5SAli Bahramiclose INFO if $opt{I};
1190*75ce41a5SAli Bahrami
1191*75ce41a5SAli Bahramiexit 0;
1192