xref: /titanic_51/usr/src/tools/scripts/check_rtime.pl (revision 13bb140908ba7c972416022c1d5fa537ac84f99f)
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#
245253169eSAli Bahrami# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate#
26749f21d3Swesolows
27749f21d3Swesolows#
287c478bd9Sstevel@tonic-gate# Check ELF information.
297c478bd9Sstevel@tonic-gate#
307c478bd9Sstevel@tonic-gate# This script descends a directory hierarchy inspecting ELF dynamic executables
317c478bd9Sstevel@tonic-gate# and shared objects.  The general theme is to verify that common Makefile rules
327c478bd9Sstevel@tonic-gate# have been used to build these objects.  Typical failures occur when Makefile
337c478bd9Sstevel@tonic-gate# rules are re-invented rather than being inherited from "cmd/lib" Makefiles.
347c478bd9Sstevel@tonic-gate#
357c478bd9Sstevel@tonic-gate# As always, a number of components don't follow the rules, and these are
3675ce41a5SAli Bahrami# excluded to reduce this scripts output.
377c478bd9Sstevel@tonic-gate#
387c478bd9Sstevel@tonic-gate# By default any file that has conditions that should be reported is first
397c478bd9Sstevel@tonic-gate# listed and then each condition follows.  The -o (one-line) option produces a
407c478bd9Sstevel@tonic-gate# more terse output which is better for sorting/diffing with "nightly".
417c478bd9Sstevel@tonic-gate#
427c478bd9Sstevel@tonic-gate# NOTE: missing dependencies, symbols or versions are reported by running the
437c478bd9Sstevel@tonic-gate# file through ldd(1).  As objects within a proto area are built to exist in a
447c478bd9Sstevel@tonic-gate# base system, standard use of ldd(1) will bind any objects to dependencies
457c478bd9Sstevel@tonic-gate# that exist in the base system.  It is frequently the case that newer objects
467c478bd9Sstevel@tonic-gate# exist in the proto area that are required to satisfy other objects
477c478bd9Sstevel@tonic-gate# dependencies, and without using these newer objects an ldd(1) will produce
4875ce41a5SAli Bahrami# misleading error messages.  To compensate for this, the -D/-d options, or the
4975ce41a5SAli Bahrami# existence of the CODEMSG_WS/ROOT environment variables, cause the creation of
507c478bd9Sstevel@tonic-gate# alternative dependency mappings via crle(1) configuration files that establish
517c478bd9Sstevel@tonic-gate# any proto shared objects as alternatives to their base system location.  Thus
527c478bd9Sstevel@tonic-gate# ldd(1) can be executed against these configuration files so that objects in a
537c478bd9Sstevel@tonic-gate# proto area bind to their dependencies in the same proto area.
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate# Define all global variables (required for strict)
57*13bb1409SRichard Loweuse vars  qw($Prog $Env $Ena64 $Tmpdir);
5875ce41a5SAli Bahramiuse vars  qw($LddNoU $Conf32 $Conf64);
5975ce41a5SAli Bahramiuse vars  qw(%opt);
6075ce41a5SAli Bahramiuse vars  qw($ErrFH $ErrTtl $InfoFH $InfoTtl $OutCnt1 $OutCnt2);
6175ce41a5SAli Bahrami
6275ce41a5SAli Bahrami# An exception file is used to specify regular expressions to match
6375ce41a5SAli Bahrami# objects. These directives specify special attributes of the object.
6475ce41a5SAli Bahrami# The regular expressions are read from the file and compiled into the
6575ce41a5SAli Bahrami# regular expression variables.
6675ce41a5SAli Bahrami#
6775ce41a5SAli Bahrami# The name of each regular expression variable is of the form
6875ce41a5SAli Bahrami#
6975ce41a5SAli Bahrami#	$EXRE_xxx
7075ce41a5SAli Bahrami#
7175ce41a5SAli Bahrami# where xxx is the name of the exception in lower case. For example,
7275ce41a5SAli Bahrami# the regular expression variable for EXEC_STACK is $EXRE_exec_stack.
7375ce41a5SAli Bahrami#
7475ce41a5SAli Bahrami# onbld_elfmod::LoadExceptionsToEXRE() depends on this naming convention
7575ce41a5SAli Bahrami# to initialize the regular expression variables, and to detect invalid
7675ce41a5SAli Bahrami# exception names.
7775ce41a5SAli Bahrami#
7875ce41a5SAli Bahrami# If a given exception is not used in the exception file, its regular
7975ce41a5SAli Bahrami# expression variable will be undefined. Users of these variables must
8075ce41a5SAli Bahrami# test the variable with defined() prior to use:
8175ce41a5SAli Bahrami#
8275ce41a5SAli Bahrami#	defined($EXRE_exec_stack) && ($foo =~ $EXRE_exec_stack)
8375ce41a5SAli Bahrami#
845253169eSAli Bahrami# or if the test is to make sure the item is not specified:
855253169eSAli Bahrami#
865253169eSAli Bahrami#	!defined($EXRE_exec_stack) || ($foo !~ $EXRE_exec_stack)
875253169eSAli Bahrami#
8875ce41a5SAli Bahrami# ----
8975ce41a5SAli Bahrami#
9075ce41a5SAli Bahrami# The exceptions are:
9175ce41a5SAli Bahrami#
925253169eSAli Bahrami#   EXEC_DATA
935253169eSAli Bahrami#	Objects that are not required to have non-executable writable
945253169eSAli Bahrami#	data segments.
955253169eSAli Bahrami#
9675ce41a5SAli Bahrami#   EXEC_STACK
9775ce41a5SAli Bahrami#	Objects that are not required to have a non-executable stack
9875ce41a5SAli Bahrami#
9975ce41a5SAli Bahrami#   NOCRLEALT
10075ce41a5SAli Bahrami#	Objects that should be skipped by AltObjectConfig() when building
10175ce41a5SAli Bahrami#	the crle script that maps objects to the proto area.
10275ce41a5SAli Bahrami#
10375ce41a5SAli Bahrami#    NODIRECT
10475ce41a5SAli Bahrami#	Objects that are not required to use direct bindings
10575ce41a5SAli Bahrami#
10675ce41a5SAli Bahrami#    NOSYMSORT
10775ce41a5SAli Bahrami#	Objects we should not check for duplicate addresses in
10875ce41a5SAli Bahrami#	the symbol sort sections.
10975ce41a5SAli Bahrami#
11075ce41a5SAli Bahrami#    OLDDEP
11175ce41a5SAli Bahrami#	Objects that are no longer needed because their functionalty
11275ce41a5SAli Bahrami#	has migrated elsewhere. These are usually pure filters that
11375ce41a5SAli Bahrami#	point at libc.
11475ce41a5SAli Bahrami#
11575ce41a5SAli Bahrami#    SKIP
11675ce41a5SAli Bahrami#	Files and directories that should be excluded from analysis.
11775ce41a5SAli Bahrami#
11875ce41a5SAli Bahrami#    STAB
11975ce41a5SAli Bahrami#	Objects that are allowed to contain stab debugging sections
12075ce41a5SAli Bahrami#
12175ce41a5SAli Bahrami#    TEXTREL
12275ce41a5SAli Bahrami#	Object for which relocations are allowed to the text segment
12375ce41a5SAli Bahrami#
12475ce41a5SAli Bahrami#    UNDEF_REF
12575ce41a5SAli Bahrami#	Objects that are allowed undefined references
12675ce41a5SAli Bahrami#
12775ce41a5SAli Bahrami#    UNREF_OBJ
12875ce41a5SAli Bahrami#	"unreferenced object=" ldd(1) diagnostics.
12975ce41a5SAli Bahrami#
13075ce41a5SAli Bahrami#    UNUSED_DEPS
13175ce41a5SAli Bahrami#	Objects that are allowed to have unused dependencies
13275ce41a5SAli Bahrami#
13375ce41a5SAli Bahrami#    UNUSED_OBJ
13475ce41a5SAli Bahrami#	Objects that are allowed to be unused dependencies
13575ce41a5SAli Bahrami#
13675ce41a5SAli Bahrami#    UNUSED_RPATH
13775ce41a5SAli Bahrami#	Objects with unused runpaths
13875ce41a5SAli Bahrami#
13975ce41a5SAli Bahrami
1405253169eSAli Bahramiuse vars  qw($EXRE_exec_data $EXRE_exec_stack $EXRE_nocrlealt);
1415253169eSAli Bahramiuse vars  qw($EXRE_nodirect $EXRE_nosymsort);
14275ce41a5SAli Bahramiuse vars  qw($EXRE_olddep $EXRE_skip $EXRE_stab $EXRE_textrel $EXRE_undef_ref);
14375ce41a5SAli Bahramiuse vars  qw($EXRE_unref_obj $EXRE_unused_deps $EXRE_unused_obj);
14475ce41a5SAli Bahramiuse vars  qw($EXRE_unused_rpath);
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gateuse strict;
1477c478bd9Sstevel@tonic-gateuse Getopt::Std;
14875ce41a5SAli Bahramiuse File::Basename;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate# Reliably compare two OS revisions.  Arguments are <ver1> <op> <ver2>.
1527c478bd9Sstevel@tonic-gate# <op> is the string form of a normal numeric comparison operator.
1537c478bd9Sstevel@tonic-gatesub cmp_os_ver {
1547c478bd9Sstevel@tonic-gate	my @ver1 = split(/\./, $_[0]);
1557c478bd9Sstevel@tonic-gate	my $op = $_[1];
1567c478bd9Sstevel@tonic-gate	my @ver2 = split(/\./, $_[2]);
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate	push @ver2, ("0") x $#ver1 - $#ver2;
1597c478bd9Sstevel@tonic-gate	push @ver1, ("0") x $#ver2 - $#ver1;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate	my $diff = 0;
1627c478bd9Sstevel@tonic-gate	while (@ver1 || @ver2) {
1637c478bd9Sstevel@tonic-gate		if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
1647c478bd9Sstevel@tonic-gate			last;
1657c478bd9Sstevel@tonic-gate		}
1667c478bd9Sstevel@tonic-gate	}
1677c478bd9Sstevel@tonic-gate	return (eval "$diff $op 0" ? 1 : 0);
1687c478bd9Sstevel@tonic-gate}
1697c478bd9Sstevel@tonic-gate
17075ce41a5SAli Bahrami## ProcFile(FullPath, RelPath, File, Class, Type, Verdef)
17175ce41a5SAli Bahrami#
1727c478bd9Sstevel@tonic-gate# Determine whether this a ELF dynamic object and if so investigate its runtime
1737c478bd9Sstevel@tonic-gate# attributes.
17475ce41a5SAli Bahrami#
1757c478bd9Sstevel@tonic-gatesub ProcFile {
17675ce41a5SAli Bahrami	my($FullPath, $RelPath, $Class, $Type, $Verdef) = @_;
17775ce41a5SAli Bahrami	my(@Elf, @Ldd, $Dyn, $Sym, $Stack);
17867e3a03eSrie	my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort);
17975ce41a5SAli Bahrami	my($Val, $Header, $IsX86, $RWX, $UnDep);
18075ce41a5SAli Bahrami	my($HasDirectBinding);
1817c478bd9Sstevel@tonic-gate
18275ce41a5SAli Bahrami	# Only look at executables and sharable objects
18375ce41a5SAli Bahrami	return if ($Type ne 'EXEC') && ($Type ne 'DYN');
1847c478bd9Sstevel@tonic-gate
18575ce41a5SAli Bahrami	# Ignore symbolic links
18675ce41a5SAli Bahrami	return if -l $FullPath;
18775ce41a5SAli Bahrami
18875ce41a5SAli Bahrami	# Is this an object or directory hierarchy we don't care about?
18975ce41a5SAli Bahrami	return if (defined($EXRE_skip) && ($RelPath =~ $EXRE_skip));
19075ce41a5SAli Bahrami
19175ce41a5SAli Bahrami	# Bail if we can't stat the file. Otherwise, note if it is SUID/SGID.
19275ce41a5SAli Bahrami	return if !stat($FullPath);
19375ce41a5SAli Bahrami	my $Secure = (-u _ || -g _) ? 1 : 0;
19475ce41a5SAli Bahrami
19575ce41a5SAli Bahrami	# Reset output message counts for new input file
19675ce41a5SAli Bahrami	$$ErrTtl = $$InfoTtl = 0;
19775ce41a5SAli Bahrami
1987c478bd9Sstevel@tonic-gate	@Ldd = 0;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate	# Determine whether we have access to inspect the file.
2017c478bd9Sstevel@tonic-gate	if (!(-r $FullPath)) {
20275ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
20375ce41a5SAli Bahrami		    "unable to inspect file: permission denied");
2047c478bd9Sstevel@tonic-gate		return;
2057c478bd9Sstevel@tonic-gate	}
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate	# Determine whether we have a executable (static or dynamic) or a
2087c478bd9Sstevel@tonic-gate	# shared object.
20975ce41a5SAli Bahrami	@Elf = split(/\n/, `elfdump -epdcy $FullPath 2>&1`);
2107c478bd9Sstevel@tonic-gate
21175ce41a5SAli Bahrami	$Dyn = $Stack = $IsX86 = $RWX = 0;
2127c478bd9Sstevel@tonic-gate	$Header = 'None';
2137c478bd9Sstevel@tonic-gate	foreach my $Line (@Elf) {
2147c478bd9Sstevel@tonic-gate		# If we have an invalid file type (which we can tell from the
2157c478bd9Sstevel@tonic-gate		# first line), or we're processing an archive, bail.
2167c478bd9Sstevel@tonic-gate		if ($Header eq 'None') {
2177c478bd9Sstevel@tonic-gate			if (($Line =~ /invalid file/) ||
2185253169eSAli Bahrami			    ($Line =~ /\Q$FullPath\E(.*):/)) {
2197c478bd9Sstevel@tonic-gate				return;
2207c478bd9Sstevel@tonic-gate			}
2217c478bd9Sstevel@tonic-gate		}
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate		if ($Line =~ /^ELF Header/) {
2247c478bd9Sstevel@tonic-gate			$Header = 'Ehdr';
22575ce41a5SAli Bahrami			next;
22675ce41a5SAli Bahrami		}
2277c478bd9Sstevel@tonic-gate
22875ce41a5SAli Bahrami		if ($Line =~ /^Program Header/) {
2297c478bd9Sstevel@tonic-gate			$Header = 'Phdr';
2307c478bd9Sstevel@tonic-gate			$RWX = 0;
23175ce41a5SAli Bahrami			next;
23275ce41a5SAli Bahrami		}
2337c478bd9Sstevel@tonic-gate
23475ce41a5SAli Bahrami		if ($Line =~ /^Dynamic Section/) {
2357c478bd9Sstevel@tonic-gate			# A dynamic section indicates we're a dynamic object
2367c478bd9Sstevel@tonic-gate			# (this makes sure we don't check static executables).
2377c478bd9Sstevel@tonic-gate			$Dyn = 1;
23875ce41a5SAli Bahrami			next;
23975ce41a5SAli Bahrami		}
2407c478bd9Sstevel@tonic-gate
24175ce41a5SAli Bahrami		if (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
2427c478bd9Sstevel@tonic-gate			# If it's a X86 object, we need to enforce RW- data.
24375ce41a5SAli Bahrami			$IsX86 = 1 if $Line =~ /(EM_AMD64|EM_386)/;
24475ce41a5SAli Bahrami			next;
2457c478bd9Sstevel@tonic-gate		}
24675ce41a5SAli Bahrami
24775ce41a5SAli Bahrami		if (($Header eq 'Phdr') &&
2485253169eSAli Bahrami		    ($Line =~ /\[ PF_X\s+PF_W\s+PF_R \]/)) {
2497c478bd9Sstevel@tonic-gate			# RWX segment seen.
2507c478bd9Sstevel@tonic-gate			$RWX = 1;
25175ce41a5SAli Bahrami			next;
2527c478bd9Sstevel@tonic-gate		}
2537c478bd9Sstevel@tonic-gate
25475ce41a5SAli Bahrami		if (($Header eq 'Phdr') &&
25575ce41a5SAli Bahrami		    ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
25675ce41a5SAli Bahrami			# Seen an RWX PT_LOAD segment.
2575253169eSAli Bahrami			if (!defined($EXRE_exec_data) ||
2585253169eSAli Bahrami			    ($RelPath !~ $EXRE_exec_data)) {
25975ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
26075ce41a5SAli Bahrami				    "application requires non-executable " .
26175ce41a5SAli Bahrami				    "data\t<no -Mmapfile_noexdata?>");
26275ce41a5SAli Bahrami			}
26375ce41a5SAli Bahrami			next;
26475ce41a5SAli Bahrami		}
26575ce41a5SAli Bahrami
26675ce41a5SAli Bahrami		if (($Header eq 'Phdr') && ($Line =~ /\[ PT_SUNWSTACK \]/)) {
2677c478bd9Sstevel@tonic-gate			# This object defines a non-executable stack.
2687c478bd9Sstevel@tonic-gate			$Stack = 1;
26975ce41a5SAli Bahrami			next;
2707c478bd9Sstevel@tonic-gate		}
2717c478bd9Sstevel@tonic-gate	}
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate	# Determine whether this ELF executable or shared object has a
2747c478bd9Sstevel@tonic-gate	# conforming mcs(1) comment section.  If the correct $(POST_PROCESS)
2757c478bd9Sstevel@tonic-gate	# macros are used, only a 3 or 4 line .comment section should exist
2767c478bd9Sstevel@tonic-gate	# containing one or two "@(#)SunOS" identifying comments (one comment
2777c478bd9Sstevel@tonic-gate	# for a non-debug build, and two for a debug build). The results of
2787c478bd9Sstevel@tonic-gate	# the following split should be three or four lines, the last empty
2797c478bd9Sstevel@tonic-gate	# line being discarded by the split.
2807c478bd9Sstevel@tonic-gate	if ($opt{m}) {
2817c478bd9Sstevel@tonic-gate		my(@Mcs, $Con, $Dev);
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate		@Mcs = split(/\n/, `mcs -p $FullPath 2>&1`);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate		$Con = $Dev = $Val = 0;
2867c478bd9Sstevel@tonic-gate		foreach my $Line (@Mcs) {
2877c478bd9Sstevel@tonic-gate			$Val++;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate			if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) {
2907c478bd9Sstevel@tonic-gate				$Con = 1;
2917c478bd9Sstevel@tonic-gate				last;
2927c478bd9Sstevel@tonic-gate			}
2937c478bd9Sstevel@tonic-gate			if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) {
2947c478bd9Sstevel@tonic-gate				$Dev = 1;
2957c478bd9Sstevel@tonic-gate				next;
2967c478bd9Sstevel@tonic-gate			}
2977c478bd9Sstevel@tonic-gate			if (($Dev == 0) && ($Val == 4)) {
2987c478bd9Sstevel@tonic-gate				$Con = 1;
2997c478bd9Sstevel@tonic-gate				last;
3007c478bd9Sstevel@tonic-gate			}
3017c478bd9Sstevel@tonic-gate			if (($Dev == 1) && ($Val == 5)) {
3027c478bd9Sstevel@tonic-gate				$Con = 1;
3037c478bd9Sstevel@tonic-gate				last;
3047c478bd9Sstevel@tonic-gate			}
3057c478bd9Sstevel@tonic-gate		}
3067c478bd9Sstevel@tonic-gate		if ($opt{m} && ($Con == 1)) {
30775ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
30875ce41a5SAli Bahrami		    "non-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>");
3097c478bd9Sstevel@tonic-gate		}
3107c478bd9Sstevel@tonic-gate	}
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate	# Applications should contain a non-executable stack definition.
31375ce41a5SAli Bahrami	if (($Type eq 'EXEC') && ($Stack == 0) &&
31475ce41a5SAli Bahrami	    (!defined($EXRE_exec_stack) || ($RelPath !~ $EXRE_exec_stack))) {
31575ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
31675ce41a5SAli Bahrami		    "non-executable stack required\t<no -Mmapfile_noexstk?>");
3177c478bd9Sstevel@tonic-gate	}
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate	# Having caught any static executables in the mcs(1) check and non-
3207c478bd9Sstevel@tonic-gate	# executable stack definition check, continue with dynamic objects
3217c478bd9Sstevel@tonic-gate	# from now on.
3227c478bd9Sstevel@tonic-gate	if ($Dyn eq 0) {
3237c478bd9Sstevel@tonic-gate		return;
3247c478bd9Sstevel@tonic-gate	}
3257c478bd9Sstevel@tonic-gate
32675ce41a5SAli Bahrami	# Use ldd unless its a 64-bit object and we lack the hardware.
32775ce41a5SAli Bahrami	if (($Class == 32) || $Ena64) {
328976d55c9Sab196087		my $LDDFullPath = $FullPath;
329976d55c9Sab196087
3307c478bd9Sstevel@tonic-gate		if ($Secure) {
3317c478bd9Sstevel@tonic-gate			# The execution of a secure application over an nfs file
3327c478bd9Sstevel@tonic-gate			# system mounted nosuid will result in warning messages
3337c478bd9Sstevel@tonic-gate			# being sent to /var/adm/messages.  As this type of
3347c478bd9Sstevel@tonic-gate			# environment can occur with root builds, move the file
3357c478bd9Sstevel@tonic-gate			# being investigated to a safe place first.  In addition
3367c478bd9Sstevel@tonic-gate			# remove its secure permission so that it can be
3377c478bd9Sstevel@tonic-gate			# influenced by any alternative dependency mappings.
3387c478bd9Sstevel@tonic-gate
33975ce41a5SAli Bahrami			my $File = $RelPath;
34075ce41a5SAli Bahrami			$File =~ s!^.*/!!;      # basename
34175ce41a5SAli Bahrami
3427c478bd9Sstevel@tonic-gate			my($TmpPath) = "$Tmpdir/$File";
3437c478bd9Sstevel@tonic-gate
344976d55c9Sab196087			system('cp', $LDDFullPath, $TmpPath);
3457c478bd9Sstevel@tonic-gate			chmod 0777, $TmpPath;
346976d55c9Sab196087			$LDDFullPath = $TmpPath;
3477c478bd9Sstevel@tonic-gate		}
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate		# Use ldd(1) to determine the objects relocatability and use.
3507c478bd9Sstevel@tonic-gate		# By default look for all unreferenced dependencies.  However,
3517c478bd9Sstevel@tonic-gate		# some objects have legitimate dependencies that they do not
3527c478bd9Sstevel@tonic-gate		# reference.
35375ce41a5SAli Bahrami		if ($LddNoU) {
3547c478bd9Sstevel@tonic-gate			$Lddopt = "-ru";
3557c478bd9Sstevel@tonic-gate		} else {
3567c478bd9Sstevel@tonic-gate			$Lddopt = "-rU";
3577c478bd9Sstevel@tonic-gate		}
358976d55c9Sab196087		@Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
3597c478bd9Sstevel@tonic-gate		if ($Secure) {
360976d55c9Sab196087			unlink $LDDFullPath;
3617c478bd9Sstevel@tonic-gate		}
3627c478bd9Sstevel@tonic-gate	}
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate	$Val = 0;
3657c478bd9Sstevel@tonic-gate	$Sym = 5;
36667e3a03eSrie	$UnDep = 1;
3677c478bd9Sstevel@tonic-gate
36867e3a03eSrie	foreach my $Line (@Ldd) {
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate		if ($Val == 0) {
3717c478bd9Sstevel@tonic-gate			$Val = 1;
3727c478bd9Sstevel@tonic-gate			# Make sure ldd(1) worked.  One possible failure is that
3737c478bd9Sstevel@tonic-gate			# this is an old ldd(1) prior to -e addition (4390308).
3747c478bd9Sstevel@tonic-gate			if ($Line =~ /usage:/) {
3757c478bd9Sstevel@tonic-gate				$Line =~ s/$/\t<old ldd(1)?>/;
37675ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
37775ce41a5SAli Bahrami				    $RelPath, $Line);
3787c478bd9Sstevel@tonic-gate				last;
3797c478bd9Sstevel@tonic-gate			} elsif ($Line =~ /execution failed/) {
38075ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
38175ce41a5SAli Bahrami				    $RelPath, $Line);
3827c478bd9Sstevel@tonic-gate				last;
3837c478bd9Sstevel@tonic-gate			}
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate			# It's possible this binary can't be executed, ie. we've
3867c478bd9Sstevel@tonic-gate			# found a sparc binary while running on an intel system,
3877c478bd9Sstevel@tonic-gate			# or a sparcv9 binary on a sparcv7/8 system.
3887c478bd9Sstevel@tonic-gate			if ($Line =~ /wrong class/) {
38975ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
39075ce41a5SAli Bahrami				    "has wrong class or data encoding");
3917c478bd9Sstevel@tonic-gate				next;
3927c478bd9Sstevel@tonic-gate			}
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate			# Historically, ldd(1) likes executable objects to have
39575ce41a5SAli Bahrami			# their execute bit set.
3967c478bd9Sstevel@tonic-gate			if ($Line =~ /not executable/) {
39775ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
39875ce41a5SAli Bahrami				    "is not executable");
3997c478bd9Sstevel@tonic-gate				next;
4007c478bd9Sstevel@tonic-gate			}
4017c478bd9Sstevel@tonic-gate		}
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate		# Look for "file" or "versions" that aren't found.  Note that
4047c478bd9Sstevel@tonic-gate		# these lines will occur before we find any symbol referencing
4057c478bd9Sstevel@tonic-gate		# errors.
4067c478bd9Sstevel@tonic-gate		if (($Sym == 5) && ($Line =~ /not found\)/)) {
4077c478bd9Sstevel@tonic-gate			if ($Line =~ /file not found\)/) {
4087c478bd9Sstevel@tonic-gate				$Line =~ s/$/\t<no -zdefs?>/;
4097c478bd9Sstevel@tonic-gate			}
41075ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
4117c478bd9Sstevel@tonic-gate			next;
4127c478bd9Sstevel@tonic-gate		}
4137c478bd9Sstevel@tonic-gate		# Look for relocations whose symbols can't be found.  Note, we
4147c478bd9Sstevel@tonic-gate		# only print out the first 5 relocations for any file as this
4157c478bd9Sstevel@tonic-gate		# output can be excessive.
4167c478bd9Sstevel@tonic-gate		if ($Sym && ($Line =~ /symbol not found/)) {
4177c478bd9Sstevel@tonic-gate			# Determine if this file is allowed undefined
4187c478bd9Sstevel@tonic-gate			# references.
41975ce41a5SAli Bahrami			if (($Sym == 5) && defined($EXRE_undef_ref) &&
42075ce41a5SAli Bahrami			    ($RelPath =~ $EXRE_undef_ref)) {
4217c478bd9Sstevel@tonic-gate				$Sym = 0;
42267e3a03eSrie				next;
4237c478bd9Sstevel@tonic-gate			}
4247c478bd9Sstevel@tonic-gate			if ($Sym-- == 1) {
42575ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
42675ce41a5SAli Bahrami				    "continued ...") if !$opt{o};
4277c478bd9Sstevel@tonic-gate				next;
4287c478bd9Sstevel@tonic-gate			}
4297c478bd9Sstevel@tonic-gate			# Just print the symbol name.
4307c478bd9Sstevel@tonic-gate			$Line =~ s/$/\t<no -zdefs?>/;
43175ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
4327c478bd9Sstevel@tonic-gate			next;
4337c478bd9Sstevel@tonic-gate		}
43467e3a03eSrie		# Look for any unused search paths.
43567e3a03eSrie		if ($Line =~ /unused search path=/) {
43675ce41a5SAli Bahrami			next if defined($EXRE_unused_rpath) &&
43775ce41a5SAli Bahrami			    ($Line =~ $EXRE_unused_rpath);
43875ce41a5SAli Bahrami
43967e3a03eSrie			if ($Secure) {
44067e3a03eSrie				$Line =~ s!$Tmpdir/!!;
44167e3a03eSrie			}
44267e3a03eSrie			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
44375ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
44467e3a03eSrie			next;
44567e3a03eSrie		}
44667e3a03eSrie		# Look for unreferenced dependencies.  Note, if any unreferenced
44767e3a03eSrie		# objects are ignored, then set $UnDep so as to suppress any
44867e3a03eSrie		# associated unused-object messages.
44967e3a03eSrie		if ($Line =~ /unreferenced object=/) {
45075ce41a5SAli Bahrami			if (defined($EXRE_unref_obj) &&
45175ce41a5SAli Bahrami			    ($Line =~ $EXRE_unref_obj)) {
45267e3a03eSrie				$UnDep = 0;
45367e3a03eSrie				next;
45467e3a03eSrie			}
45567e3a03eSrie			if ($Secure) {
45667e3a03eSrie				$Line =~ s!$Tmpdir/!!;
45767e3a03eSrie			}
45875ce41a5SAli Bahrami			$Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
45975ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
46067e3a03eSrie			next;
46167e3a03eSrie		}
4627c478bd9Sstevel@tonic-gate		# Look for any unused dependencies.
46367e3a03eSrie		if ($UnDep && ($Line =~ /unused/)) {
46475ce41a5SAli Bahrami			# Skip if object is allowed to have unused dependencies
46575ce41a5SAli Bahrami			next if defined($EXRE_unused_deps) &&
46675ce41a5SAli Bahrami			    ($RelPath =~ $EXRE_unused_deps);
46775ce41a5SAli Bahrami
46875ce41a5SAli Bahrami			# Skip if dependency is always allowed to be unused
46975ce41a5SAli Bahrami			next if defined($EXRE_unused_obj) &&
47075ce41a5SAli Bahrami			    ($Line =~ $EXRE_unused_obj);
47175ce41a5SAli Bahrami
47275ce41a5SAli Bahrami			$Line =~ s!$Tmpdir/!! if $Secure;
47375ce41a5SAli Bahrami			$Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
47475ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
4757c478bd9Sstevel@tonic-gate			next;
4767c478bd9Sstevel@tonic-gate		}
4777c478bd9Sstevel@tonic-gate	}
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate	# Reuse the elfdump(1) data to investigate additional dynamic linking
4807c478bd9Sstevel@tonic-gate	# information.
4817c478bd9Sstevel@tonic-gate
482dfb96a4fSab196087	$Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
4837c478bd9Sstevel@tonic-gate	$Tex = $Strip = 1;
484f6acbf7cSrie	$HasDirectBinding = 0;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate	$Header = 'None';
4877c478bd9Sstevel@tonic-gateELF:	foreach my $Line (@Elf) {
4887c478bd9Sstevel@tonic-gate		# We're only interested in the section headers and the dynamic
4897c478bd9Sstevel@tonic-gate		# section.
4907c478bd9Sstevel@tonic-gate		if ($Line =~ /^Section Header/) {
4917c478bd9Sstevel@tonic-gate			$Header = 'Shdr';
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate			if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
4947c478bd9Sstevel@tonic-gate				# This object has a combined relocation section.
4957c478bd9Sstevel@tonic-gate				$Sun = 1;
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate			} elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
4987c478bd9Sstevel@tonic-gate				# This object contain .stabs sections
4997c478bd9Sstevel@tonic-gate				$Stab = 1;
500dfb96a4fSab196087			} elsif (($SymSort == 0) &&
501dfb96a4fSab196087				 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) {
502dfb96a4fSab196087				# This object contains a symbol sort section
503dfb96a4fSab196087				$SymSort = 1;
5047c478bd9Sstevel@tonic-gate			}
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate			if (($Strip == 1) && ($Line =~ /\.symtab/)) {
5077c478bd9Sstevel@tonic-gate				# This object contains a complete symbol table.
5087c478bd9Sstevel@tonic-gate				$Strip = 0;
5097c478bd9Sstevel@tonic-gate			}
5107c478bd9Sstevel@tonic-gate			next;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate		} elsif ($Line =~ /^Dynamic Section/) {
5137c478bd9Sstevel@tonic-gate			$Header = 'Dyn';
5147c478bd9Sstevel@tonic-gate			next;
515f6acbf7cSrie		} elsif ($Line =~ /^Syminfo Section/) {
516f6acbf7cSrie			$Header = 'Syminfo';
517f6acbf7cSrie			next;
518f6acbf7cSrie		} elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) {
519f6acbf7cSrie			next;
520f6acbf7cSrie		}
521f6acbf7cSrie
522f6acbf7cSrie		# Look into the Syminfo section.
523f6acbf7cSrie		# Does this object have at least one Directly Bound symbol?
524f6acbf7cSrie		if (($Header eq 'Syminfo')) {
525f6acbf7cSrie			my(@Symword);
526f6acbf7cSrie
527f6acbf7cSrie			if ($HasDirectBinding == 1) {
528f6acbf7cSrie				next;
529f6acbf7cSrie			}
530f6acbf7cSrie
531f6acbf7cSrie			@Symword = split(' ', $Line);
532f6acbf7cSrie
533f6acbf7cSrie			if (!defined($Symword[1])) {
534f6acbf7cSrie				next;
535f6acbf7cSrie			}
536f6acbf7cSrie			if ($Symword[1] =~ /B/) {
537f6acbf7cSrie				$HasDirectBinding = 1;
538f6acbf7cSrie			}
5397c478bd9Sstevel@tonic-gate			next;
5407c478bd9Sstevel@tonic-gate		}
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate		# Does this object contain text relocations.
5437c478bd9Sstevel@tonic-gate		if ($Tex && ($Line =~ /TEXTREL/)) {
5447c478bd9Sstevel@tonic-gate			# Determine if this file is allowed text relocations.
54575ce41a5SAli Bahrami			if (defined($EXRE_textrel) &&
54675ce41a5SAli Bahrami			    ($RelPath =~ $EXRE_textrel)) {
5477c478bd9Sstevel@tonic-gate				$Tex = 0;
5487c478bd9Sstevel@tonic-gate				next ELF;
5497c478bd9Sstevel@tonic-gate			}
55075ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
55175ce41a5SAli Bahrami			    "TEXTREL .dynamic tag\t\t\t<no -Kpic?>");
5527c478bd9Sstevel@tonic-gate			$Tex = 0;
5537c478bd9Sstevel@tonic-gate			next;
5547c478bd9Sstevel@tonic-gate		}
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate		# Does this file have any relocation sections (there are a few
5577c478bd9Sstevel@tonic-gate		# psr libraries with no relocations at all, thus a .SUNW_reloc
5587c478bd9Sstevel@tonic-gate		# section won't exist either).
5597c478bd9Sstevel@tonic-gate		if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
5607c478bd9Sstevel@tonic-gate			$Relsz = hex((split(' ', $Line))[2]);
5617c478bd9Sstevel@tonic-gate			next;
5627c478bd9Sstevel@tonic-gate		}
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate		# Does this file have any plt relocations.  If the plt size is
5657c478bd9Sstevel@tonic-gate		# equivalent to the total relocation size then we don't have
5667c478bd9Sstevel@tonic-gate		# any relocations suitable for combining into a .SUNW_reloc
5677c478bd9Sstevel@tonic-gate		# section.
5687c478bd9Sstevel@tonic-gate		if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
5697c478bd9Sstevel@tonic-gate			$Pltsz = hex((split(' ', $Line))[2]);
5707c478bd9Sstevel@tonic-gate			next;
5717c478bd9Sstevel@tonic-gate		}
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate		# Does this object have any dependencies.
57467e3a03eSrie		if ($Line =~ /NEEDED/) {
5757c478bd9Sstevel@tonic-gate			my($Need) = (split(' ', $Line))[3];
5767c478bd9Sstevel@tonic-gate
57775ce41a5SAli Bahrami			if (defined($EXRE_olddep) && ($Need =~ $EXRE_olddep)) {
57867e3a03eSrie				# Catch any old (unnecessary) dependencies.
57975ce41a5SAli Bahrami				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
58075ce41a5SAli Bahrami			"NEEDED=$Need\t<dependency no longer necessary>");
58167e3a03eSrie			} elsif ($opt{i}) {
58267e3a03eSrie				# Under the -i (information) option print out
58367e3a03eSrie				# any useful dynamic entries.
58475ce41a5SAli Bahrami				onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
58575ce41a5SAli Bahrami				    "NEEDED=$Need");
5867c478bd9Sstevel@tonic-gate			}
5877c478bd9Sstevel@tonic-gate			next;
5887c478bd9Sstevel@tonic-gate		}
5897c478bd9Sstevel@tonic-gate
590f6acbf7cSrie		# Is this object built with -B direct flag on?
591f6acbf7cSrie		if ($Line =~ / DIRECT /) {
592f6acbf7cSrie			$HasDirectBinding = 1;
593f6acbf7cSrie		}
594f6acbf7cSrie
5957c478bd9Sstevel@tonic-gate		# Does this object specify a runpath.
5967c478bd9Sstevel@tonic-gate		if ($opt{i} && ($Line =~ /RPATH/)) {
5977c478bd9Sstevel@tonic-gate			my($Rpath) = (split(' ', $Line))[3];
59875ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
59975ce41a5SAli Bahrami			    $RelPath, "RPATH=$Rpath");
6007c478bd9Sstevel@tonic-gate			next;
6017c478bd9Sstevel@tonic-gate		}
6027c478bd9Sstevel@tonic-gate	}
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate	# A shared object, that contains non-plt relocations, should have a
6057c478bd9Sstevel@tonic-gate	# combined relocation section indicating it was built with -z combreloc.
60675ce41a5SAli Bahrami	if (($Type eq 'DYN') && $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
60775ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
6085253169eSAli Bahrami		    ".SUNW_reloc section missing\t\t<no -zcombreloc?>");
6097c478bd9Sstevel@tonic-gate	}
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate	# No objects released to a customer should have any .stabs sections
6127c478bd9Sstevel@tonic-gate	# remaining, they should be stripped.
6137c478bd9Sstevel@tonic-gate	if ($opt{s} && $Stab) {
61475ce41a5SAli Bahrami		goto DONESTAB if defined($EXRE_stab) && ($RelPath =~ $EXRE_stab);
61575ce41a5SAli Bahrami
61675ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
61775ce41a5SAli Bahrami		    "debugging sections should be deleted\t<no strip -x?>");
6187c478bd9Sstevel@tonic-gate	}
6197c478bd9Sstevel@tonic-gate
620f6acbf7cSrie	# Identify an object that is not built with either -B direct or
621f6acbf7cSrie	# -z direct.
62275ce41a5SAli Bahrami	goto DONESTAB
62375ce41a5SAli Bahrami	    if (defined($EXRE_nodirect) && ($RelPath =~ $EXRE_nodirect));
62475ce41a5SAli Bahrami
625f6acbf7cSrie	if ($Relsz && ($HasDirectBinding == 0)) {
62675ce41a5SAli Bahrami		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
62775ce41a5SAli Bahrami		 "object has no direct bindings\t<no -B direct or -z direct?>");
628f6acbf7cSrie	}
629f6acbf7cSrie
6307c478bd9Sstevel@tonic-gateDONESTAB:
6317c478bd9Sstevel@tonic-gate
6328ad60789Srie	# All objects should have a full symbol table to provide complete
6337c478bd9Sstevel@tonic-gate	# debugging stack traces.
63475ce41a5SAli Bahrami	onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
63575ce41a5SAli Bahrami	    "symbol table should not be stripped\t<remove -s?>") if $Strip;
636dfb96a4fSab196087
637dfb96a4fSab196087	# If there are symbol sort sections in this object, report on
638dfb96a4fSab196087	# any that have duplicate addresses.
63975ce41a5SAli Bahrami	ProcSymSort($FullPath, $RelPath) if $SymSort;
640bfed486aSAli Bahrami
641bfed486aSAli Bahrami	# If -v was specified, and the object has a version definition
642bfed486aSAli Bahrami	# section, generate output showing each public symbol and the
643bfed486aSAli Bahrami	# version it belongs to.
64475ce41a5SAli Bahrami	ProcVerdef($FullPath, $RelPath)
64575ce41a5SAli Bahrami	    if ($Verdef eq 'VERDEF') && $opt{v};
646dfb96a4fSab196087}
647dfb96a4fSab196087
648dfb96a4fSab196087
64975ce41a5SAli Bahrami## ProcSymSortOutMsg(RelPath, secname, addr, names...)
650dfb96a4fSab196087#
65175ce41a5SAli Bahrami# Call onbld_elfmod::OutMsg for a duplicate address error in a symbol sort
652dfb96a4fSab196087# section
653dfb96a4fSab196087#
654dfb96a4fSab196087sub ProcSymSortOutMsg {
65575ce41a5SAli Bahrami	my($RelPath, $secname, $addr, @names) = @_;
656dfb96a4fSab196087
65775ce41a5SAli Bahrami	onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
658dfb96a4fSab196087	    "$secname: duplicate $addr: ". join(', ', @names));
659dfb96a4fSab196087}
660dfb96a4fSab196087
661dfb96a4fSab196087
662dfb96a4fSab196087## ProcSymSort(FullPath, RelPath)
663dfb96a4fSab196087#
664dfb96a4fSab196087# Examine the symbol sort sections for the given object and report
665dfb96a4fSab196087# on any duplicate addresses found.  Ideally, mapfile directives
666dfb96a4fSab196087# should be used when building objects that have multiple symbols
667dfb96a4fSab196087# with the same address so that only one of them appears in the sort
668dfb96a4fSab196087# section. This saves space, reduces user confusion, and ensures that
669dfb96a4fSab196087# libproc and debuggers always display public names instead of symbols
670dfb96a4fSab196087# that are merely implementation details.
671dfb96a4fSab196087#
672dfb96a4fSab196087sub ProcSymSort {
673dfb96a4fSab196087
67475ce41a5SAli Bahrami	my($FullPath, $RelPath) = @_;
675dfb96a4fSab196087
676dfb96a4fSab196087	# If this object is exempt from checking, return quietly
67775ce41a5SAli Bahrami	return if defined($EXRE_nosymsort) && ($FullPath =~ $EXRE_nosymsort);
678dfb96a4fSab196087
679dfb96a4fSab196087
680dfb96a4fSab196087	open(SORT, "elfdump -S $FullPath|") ||
681dfb96a4fSab196087	    die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
682dfb96a4fSab196087
683dfb96a4fSab196087	my $line;
684dfb96a4fSab196087	my $last_addr;
685dfb96a4fSab196087	my @dups = ();
686dfb96a4fSab196087	my $secname;
687dfb96a4fSab196087	while ($line = <SORT>) {
688dfb96a4fSab196087		chomp $line;
689dfb96a4fSab196087
690dfb96a4fSab196087		next if ($line eq '');
691dfb96a4fSab196087
692dfb96a4fSab196087		# If this is a header line, pick up the section name
693dfb96a4fSab196087		if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
694dfb96a4fSab196087			$secname = $1;
695dfb96a4fSab196087
696dfb96a4fSab196087			# Every new section is followed by a column header line
697dfb96a4fSab196087			$line = <SORT>;		# Toss header line
698dfb96a4fSab196087
699dfb96a4fSab196087			# Flush anything left from previous section
70075ce41a5SAli Bahrami			ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
70175ce41a5SAli Bahrami			    if (scalar(@dups) > 1);
702dfb96a4fSab196087
703dfb96a4fSab196087			# Reset variables for new sort section
704dfb96a4fSab196087			$last_addr = '';
705dfb96a4fSab196087			@dups = ();
706dfb96a4fSab196087
707dfb96a4fSab196087			next;
708dfb96a4fSab196087		}
709dfb96a4fSab196087
710dfb96a4fSab196087		# Process symbol line
711dfb96a4fSab196087		my @fields = split /\s+/, $line;
712dfb96a4fSab196087		my $new_addr = $fields[2];
713169e20d9SAli Bahrami		my $new_type = $fields[8];
714dfb96a4fSab196087		my $new_name = $fields[9];
715607c61fdSab196087
716169e20d9SAli Bahrami		if ($new_type eq 'UNDEF') {
71775ce41a5SAli Bahrami			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
718169e20d9SAli Bahrami			    "$secname: unexpected UNDEF symbol " .
719169e20d9SAli Bahrami			    "(link-editor error): $new_name");
720169e20d9SAli Bahrami			next;
721169e20d9SAli Bahrami		}
722169e20d9SAli Bahrami
723a854c1c1Sab196087		if ($new_addr eq $last_addr) {
724dfb96a4fSab196087			push @dups, $new_name;
725dfb96a4fSab196087		} else {
72675ce41a5SAli Bahrami			ProcSymSortOutMsg($RelPath, $secname,
727dfb96a4fSab196087			    $last_addr, @dups) if (scalar(@dups) > 1);
728dfb96a4fSab196087			@dups = ( $new_name );
729dfb96a4fSab196087			$last_addr = $new_addr;
730dfb96a4fSab196087		}
731dfb96a4fSab196087	}
732dfb96a4fSab196087
73375ce41a5SAli Bahrami	ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
734dfb96a4fSab196087		if (scalar(@dups) > 1);
735dfb96a4fSab196087
736dfb96a4fSab196087	close SORT;
7377c478bd9Sstevel@tonic-gate}
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate
740bfed486aSAli Bahrami## ProcVerdef(FullPath, RelPath)
741bfed486aSAli Bahrami#
742bfed486aSAli Bahrami# Examine the version definition section for the given object and report
743bfed486aSAli Bahrami# each public symbol along with the version it belongs to.
744bfed486aSAli Bahrami#
745bfed486aSAli Bahramisub ProcVerdef {
746bfed486aSAli Bahrami
74775ce41a5SAli Bahrami	my($FullPath, $RelPath) = @_;
748bfed486aSAli Bahrami	my $line;
749bfed486aSAli Bahrami	my $cur_ver = '';
750bfed486aSAli Bahrami	my $tab = $opt{o} ? '' : "\t";
751bfed486aSAli Bahrami
752bfed486aSAli Bahrami	# pvs -dov provides information about the versioning hierarchy
753bfed486aSAli Bahrami	# in the file. Lines are of the format:
754bfed486aSAli Bahrami	#	path - version[XXX];
755bfed486aSAli Bahrami	# where [XXX] indicates optional information, such as flags
756bfed486aSAli Bahrami	# or inherited versions.
757bfed486aSAli Bahrami	#
758bfed486aSAli Bahrami	# Private versions are allowed to change freely, so ignore them.
759bfed486aSAli Bahrami	open(PVS, "pvs -dov $FullPath|") ||
760bfed486aSAli Bahrami	    die "$Prog: Unable to execute pvs (version definition section)\n";
761bfed486aSAli Bahrami
762bfed486aSAli Bahrami	while ($line = <PVS>) {
763bfed486aSAli Bahrami		chomp $line;
764bfed486aSAli Bahrami
765bfed486aSAli Bahrami		if ($line =~ /^[^\s]+\s+-\s+([^;]+)/) {
766bfed486aSAli Bahrami			my $ver = $1;
767bfed486aSAli Bahrami
768bfed486aSAli Bahrami			next if $ver =~ /private/i;
76975ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
77075ce41a5SAli Bahrami			    "${tab}VERDEF=$ver");
771bfed486aSAli Bahrami		}
772bfed486aSAli Bahrami	}
773bfed486aSAli Bahrami	close PVS;
774bfed486aSAli Bahrami
775bfed486aSAli Bahrami	# pvs -dos lists the symbols assigned to each version definition.
776bfed486aSAli Bahrami	# Lines are of the format:
777bfed486aSAli Bahrami	#	path - version: symbol;
778bfed486aSAli Bahrami	#	path - version: symbol (size);
779bfed486aSAli Bahrami	# where the (size) is added to data items, but not for functions.
780bfed486aSAli Bahrami	# We strip off the size, if present.
781bfed486aSAli Bahrami
782bfed486aSAli Bahrami	open(PVS, "pvs -dos $FullPath|") ||
783bfed486aSAli Bahrami	    die "$Prog: Unable to execute pvs (version definition section)\n";
784bfed486aSAli Bahrami	while ($line = <PVS>) {
785bfed486aSAli Bahrami		chomp $line;
786bfed486aSAli Bahrami		if ($line =~ /^[^\s]+\s+-\s+([^:]+):\s*([^\s;]+)/) {
787bfed486aSAli Bahrami		    my $ver = $1;
788bfed486aSAli Bahrami		    my $sym = $2;
789bfed486aSAli Bahrami
790bfed486aSAli Bahrami		    next if $ver =~ /private/i;
791bfed486aSAli Bahrami
792bfed486aSAli Bahrami		    if ($opt{o}) {
79375ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
794bfed486aSAli Bahrami			    "VERSION=$ver, SYMBOL=$sym");
795bfed486aSAli Bahrami		    } else {
796bfed486aSAli Bahrami			if ($cur_ver ne $ver) {
79775ce41a5SAli Bahrami			    onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
79875ce41a5SAli Bahrami			        $RelPath, "VERSION=$ver");
799bfed486aSAli Bahrami			    $cur_ver = $ver;
800bfed486aSAli Bahrami			}
80175ce41a5SAli Bahrami			onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
80275ce41a5SAli Bahrami			    $RelPath, "SYMBOL=$sym");
803bfed486aSAli Bahrami		    }
804bfed486aSAli Bahrami		}
805bfed486aSAli Bahrami	}
806bfed486aSAli Bahrami
807bfed486aSAli Bahrami	close PVS;
808bfed486aSAli Bahrami}
809bfed486aSAli Bahrami
810bfed486aSAli Bahrami
81175ce41a5SAli Bahrami## OpenFindElf(file, FileHandleRef, LineNumRef)
81275ce41a5SAli Bahrami#
81375ce41a5SAli Bahrami# Open file in 'find_elf -r' format, and return the value of
81475ce41a5SAli Bahrami# the opening PREFIX line.
81575ce41a5SAli Bahrami#
81675ce41a5SAli Bahrami# entry:
81775ce41a5SAli Bahrami#	file - file, or find_elf child process, to open
81875ce41a5SAli Bahrami#	FileHandleRef - Reference to file handle to open
81975ce41a5SAli Bahrami#	LineNumRef - Reference to integer to increment as lines are input
82075ce41a5SAli Bahrami#
82175ce41a5SAli Bahrami# exit:
82275ce41a5SAli Bahrami#	This routine issues a fatal error and does not return on error.
82375ce41a5SAli Bahrami#	Otherwise, the value of PREFIX is returned.
82475ce41a5SAli Bahrami#
82575ce41a5SAli Bahramisub OpenFindElf {
82675ce41a5SAli Bahrami	my ($file, $fh, $LineNum) = @_;
82775ce41a5SAli Bahrami	my $line;
82875ce41a5SAli Bahrami	my $prefix;
8297c478bd9Sstevel@tonic-gate
83075ce41a5SAli Bahrami	open($fh, $file) || die "$Prog: Unable to open: $file";
83175ce41a5SAli Bahrami	$$LineNum = 0;
83275ce41a5SAli Bahrami
83375ce41a5SAli Bahrami	# This script requires relative paths as created by 'find_elf -r'.
83475ce41a5SAli Bahrami	# When this is done, the first non-comment line will always
83575ce41a5SAli Bahrami	# be PREFIX. Obtain that line, or issue a fatal error.
83675ce41a5SAli Bahrami	while ($line = onbld_elfmod::GetLine($fh, $LineNum)) {
83775ce41a5SAli Bahrami		if ($line =~ /^PREFIX\s+(.*)$/i) {
83875ce41a5SAli Bahrami			$prefix = $1;
83975ce41a5SAli Bahrami			last;
8407c478bd9Sstevel@tonic-gate		}
8417c478bd9Sstevel@tonic-gate
84275ce41a5SAli Bahrami		die "$Prog: No PREFIX line seen on line $$LineNum: $file";
8437c478bd9Sstevel@tonic-gate	}
8447c478bd9Sstevel@tonic-gate
84575ce41a5SAli Bahrami	$prefix;
8467c478bd9Sstevel@tonic-gate}
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate
84975ce41a5SAli Bahrami## ProcFindElf(file)
85075ce41a5SAli Bahrami#
85175ce41a5SAli Bahrami# Open the specified file, which must be produced by "find_elf -r",
85275ce41a5SAli Bahrami# and process the files it describes.
85375ce41a5SAli Bahrami#
85475ce41a5SAli Bahramisub ProcFindElf {
85575ce41a5SAli Bahrami	my $file = $_[0];
85675ce41a5SAli Bahrami	my $line;
85775ce41a5SAli Bahrami	my $LineNum;
85875ce41a5SAli Bahrami
85975ce41a5SAli Bahrami	my $prefix = OpenFindElf($file, \*FIND_ELF, \$LineNum);
86075ce41a5SAli Bahrami
86175ce41a5SAli Bahrami	while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
86275ce41a5SAli Bahrami		next if !($line =~ /^OBJECT\s/i);
86375ce41a5SAli Bahrami
86475ce41a5SAli Bahrami		my ($item, $class, $type, $verdef, $obj) =
86575ce41a5SAli Bahrami		    split(/\s+/, $line, 5);
86675ce41a5SAli Bahrami
86775ce41a5SAli Bahrami		ProcFile("$prefix/$obj", $obj, $class, $type, $verdef);
8687c478bd9Sstevel@tonic-gate	}
8697c478bd9Sstevel@tonic-gate
87075ce41a5SAli Bahrami	close FIND_ELF;
8717c478bd9Sstevel@tonic-gate}
8727c478bd9Sstevel@tonic-gate
87375ce41a5SAli Bahrami
87475ce41a5SAli Bahrami## AltObjectConfig(file)
87575ce41a5SAli Bahrami#
87675ce41a5SAli Bahrami# Recurse through a directory hierarchy looking for appropriate dependencies
87775ce41a5SAli Bahrami# to map from their standard system locations to the proto area via a crle
87875ce41a5SAli Bahrami# config file.
87975ce41a5SAli Bahrami#
88075ce41a5SAli Bahrami# entry:
88175ce41a5SAli Bahrami#	file - File of ELF objects, in 'find_elf -r' format, to examine.
88275ce41a5SAli Bahrami#
88375ce41a5SAli Bahrami# exit:
88475ce41a5SAli Bahrami#	Scripts are generated for the 32 and 64-bit cases to run crle
88575ce41a5SAli Bahrami#	and create runtime configuration files that will establish
88675ce41a5SAli Bahrami#	alternative dependency mappings for the objects identified.
88775ce41a5SAli Bahrami#
88875ce41a5SAli Bahrami#	$Env - Set to environment variable definitions that will cause
88975ce41a5SAli Bahrami#		the config files generated by this routine to be used
89075ce41a5SAli Bahrami#		by ldd.
89175ce41a5SAli Bahrami#	$Conf32, $Conf64 - Undefined, or set to the config files generated
89275ce41a5SAli Bahrami#		by this routine. If defined, the caller is responsible for
89375ce41a5SAli Bahrami#		unlinking the files before exiting.
89475ce41a5SAli Bahrami#
89575ce41a5SAli Bahramisub AltObjectConfig {
89675ce41a5SAli Bahrami	my $file = $_[0];
89775ce41a5SAli Bahrami	my ($Crle32, $Crle64);
89875ce41a5SAli Bahrami	my $line;
89975ce41a5SAli Bahrami	my $LineNum;
90075ce41a5SAli Bahrami	my $obj_path;
90175ce41a5SAli Bahrami	my $obj_active = 0;
90275ce41a5SAli Bahrami	my $obj_class;
90375ce41a5SAli Bahrami
90475ce41a5SAli Bahrami	my $prefix = OpenFindElf($file, \*FIND_ELF);
90575ce41a5SAli Bahrami
90675ce41a5SAli BahramiLINE:
90775ce41a5SAli Bahrami	while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
90875ce41a5SAli Bahrami	      ITEM: {
90975ce41a5SAli Bahrami
91075ce41a5SAli Bahrami			if ($line =~ /^OBJECT\s/i) {
91175ce41a5SAli Bahrami				my ($item, $class, $type, $verdef, $obj) =
91275ce41a5SAli Bahrami				    split(/\s+/, $line, 5);
91375ce41a5SAli Bahrami
91475ce41a5SAli Bahrami				if ($type eq 'DYN') {
91575ce41a5SAli Bahrami					$obj_active = 1;
91675ce41a5SAli Bahrami					$obj_path = $obj;
91775ce41a5SAli Bahrami					$obj_class = $class;
91875ce41a5SAli Bahrami				} else {
91975ce41a5SAli Bahrami					# Only want sharable objects
92075ce41a5SAli Bahrami					$obj_active = 0;
9217c478bd9Sstevel@tonic-gate				}
92275ce41a5SAli Bahrami				last ITEM;
9237c478bd9Sstevel@tonic-gate			}
9247c478bd9Sstevel@tonic-gate
92575ce41a5SAli Bahrami			# We need to follow links to sharable objects so
92675ce41a5SAli Bahrami			# that any dependencies are expressed in all their
92775ce41a5SAli Bahrami			# available forms. We depend on ALIAS lines directly
92875ce41a5SAli Bahrami			# following the object they alias, so if we have
92975ce41a5SAli Bahrami			# a current object, this alias belongs to it.
93075ce41a5SAli Bahrami			if ($obj_active && ($line =~ /^ALIAS\s/i)) {
93175ce41a5SAli Bahrami				my ($item, $real_obj, $obj) =
93275ce41a5SAli Bahrami				    split(/\s+/, $line, 3);
93375ce41a5SAli Bahrami				$obj_path = $obj;
93475ce41a5SAli Bahrami				last ITEM;
9357c478bd9Sstevel@tonic-gate			}
9367c478bd9Sstevel@tonic-gate
93775ce41a5SAli Bahrami			# Skip unrecognized item
93875ce41a5SAli Bahrami			next LINE;
93975ce41a5SAli Bahrami		}
9407c478bd9Sstevel@tonic-gate
94175ce41a5SAli Bahrami		next if !$obj_active;
9427c478bd9Sstevel@tonic-gate
94375ce41a5SAli Bahrami		my $full = "$prefix/$obj_path";
94475ce41a5SAli Bahrami
94575ce41a5SAli Bahrami		next if defined($EXRE_nocrlealt) &&
94675ce41a5SAli Bahrami		    ($obj_path =~ $EXRE_nocrlealt);
94775ce41a5SAli Bahrami
94875ce41a5SAli Bahrami		my $Dir = $full;
94975ce41a5SAli Bahrami		$Dir =~ s/^(.*)\/.*$/$1/;
95075ce41a5SAli Bahrami
95175ce41a5SAli Bahrami		# Create a crle(1) script for the dependency we've found.
95275ce41a5SAli Bahrami		# We build separate scripts for the 32 and 64-bit cases.
95375ce41a5SAli Bahrami		# We create and initialize each script when we encounter
95475ce41a5SAli Bahrami		# the first object that needs it.
95575ce41a5SAli Bahrami		if ($obj_class == 32) {
9567c478bd9Sstevel@tonic-gate			if (!$Crle32) {
9577c478bd9Sstevel@tonic-gate				$Crle32 = "$Tmpdir/$Prog.crle32.$$";
9587c478bd9Sstevel@tonic-gate				open(CRLE32, "> $Crle32") ||
9597c478bd9Sstevel@tonic-gate				    die "$Prog: open failed: $Crle32: $!";
9607c478bd9Sstevel@tonic-gate				print CRLE32 "#!/bin/sh\ncrle \\\n";
9617c478bd9Sstevel@tonic-gate			}
96275ce41a5SAli Bahrami			print CRLE32 "\t-o $Dir -a /$obj_path \\\n";
96375ce41a5SAli Bahrami		} elsif ($Ena64) {
96475ce41a5SAli Bahrami			if (!$Crle64) {
96575ce41a5SAli Bahrami				$Crle64 = "$Tmpdir/$Prog.crle64.$$";
96675ce41a5SAli Bahrami				open(CRLE64, "> $Crle64") ||
96775ce41a5SAli Bahrami				    die "$Prog: open failed: $Crle64: $!";
96875ce41a5SAli Bahrami				print CRLE64 "#!/bin/sh\ncrle -64\\\n";
96975ce41a5SAli Bahrami			}
97075ce41a5SAli Bahrami			print CRLE64 "\t-o $Dir -a /$obj_path \\\n";
97175ce41a5SAli Bahrami		}
9727c478bd9Sstevel@tonic-gate	}
9737c478bd9Sstevel@tonic-gate
97475ce41a5SAli Bahrami	close FIND_ELF;
9757c478bd9Sstevel@tonic-gate
97675ce41a5SAli Bahrami
97775ce41a5SAli Bahrami	# Now that the config scripts are complete, use them to generate
97875ce41a5SAli Bahrami	# runtime linker config files.
9797c478bd9Sstevel@tonic-gate	if ($Crle64) {
9807c478bd9Sstevel@tonic-gate		$Conf64 = "$Tmpdir/$Prog.conf64.$$";
9817c478bd9Sstevel@tonic-gate		print CRLE64 "\t-c $Conf64\n";
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate		chmod 0755, $Crle64;
9847c478bd9Sstevel@tonic-gate		close CRLE64;
9857c478bd9Sstevel@tonic-gate
98675ce41a5SAli Bahrami		undef $Conf64 if system($Crle64);
98775ce41a5SAli Bahrami
98875ce41a5SAli Bahrami		# Done with the script
98975ce41a5SAli Bahrami		unlink $Crle64;
9907c478bd9Sstevel@tonic-gate	}
9917c478bd9Sstevel@tonic-gate	if ($Crle32) {
9927c478bd9Sstevel@tonic-gate		$Conf32 = "$Tmpdir/$Prog.conf32.$$";
9937c478bd9Sstevel@tonic-gate		print CRLE32 "\t-c $Conf32\n";
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate		chmod 0755, $Crle32;
9967c478bd9Sstevel@tonic-gate		close CRLE32;
9977c478bd9Sstevel@tonic-gate
99875ce41a5SAli Bahrami		undef $Conf32 if system($Crle32);
99975ce41a5SAli Bahrami
100075ce41a5SAli Bahrami		# Done with the script
100175ce41a5SAli Bahrami		unlink $Crle32;
10027c478bd9Sstevel@tonic-gate	}
10037c478bd9Sstevel@tonic-gate
100475ce41a5SAli Bahrami	# Set $Env so that we will use the config files generated above
100575ce41a5SAli Bahrami	# when we run ldd.
10067c478bd9Sstevel@tonic-gate	if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
10077c478bd9Sstevel@tonic-gate		$Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
10087c478bd9Sstevel@tonic-gate	} elsif ($Crle64 && $Conf64) {
10097c478bd9Sstevel@tonic-gate		$Env = "-e LD_FLAGS=config_64=$Conf64";
10107c478bd9Sstevel@tonic-gate	} elsif ($Crle32 && $Conf32) {
10117c478bd9Sstevel@tonic-gate		$Env = "-e LD_FLAGS=config_32=$Conf32";
10127c478bd9Sstevel@tonic-gate	}
10137c478bd9Sstevel@tonic-gate}
10147c478bd9Sstevel@tonic-gate
101575ce41a5SAli Bahrami# -----------------------------------------------------------------------------
10167c478bd9Sstevel@tonic-gate
101775ce41a5SAli Bahrami# This script relies on ldd returning output reflecting only the binary
101875ce41a5SAli Bahrami# contents.  But if LD_PRELOAD* environment variables are present, libraries
101975ce41a5SAli Bahrami# named by them will also appear in the output, disrupting our analysis.
102075ce41a5SAli Bahrami# So, before we get too far, scrub the environment.
10217c478bd9Sstevel@tonic-gate
102275ce41a5SAli Bahramidelete($ENV{LD_PRELOAD});
102375ce41a5SAli Bahramidelete($ENV{LD_PRELOAD_32});
102475ce41a5SAli Bahramidelete($ENV{LD_PRELOAD_64});
10257c478bd9Sstevel@tonic-gate
102675ce41a5SAli Bahrami# Establish a program name for any error diagnostics.
102775ce41a5SAli Bahramichomp($Prog = `basename $0`);
10287c478bd9Sstevel@tonic-gate
102975ce41a5SAli Bahrami# The onbld_elfmod package is maintained in the same directory as this
103075ce41a5SAli Bahrami# script, and is installed in ../lib/perl. Use the local one if present,
103175ce41a5SAli Bahrami# and the installed one otherwise.
103275ce41a5SAli Bahramimy $moddir = dirname($0);
103375ce41a5SAli Bahrami$moddir = "$moddir/../lib/perl" if ! -f "$moddir/onbld_elfmod.pm";
103475ce41a5SAli Bahramirequire "$moddir/onbld_elfmod.pm";
103575ce41a5SAli Bahrami
103675ce41a5SAli Bahrami# Determine what machinery is available.
103775ce41a5SAli Bahramimy $Mach = `uname -p`;
103875ce41a5SAli Bahramimy$Isalist = `isalist`;
103975ce41a5SAli Bahramiif ($Mach =~ /sparc/) {
104075ce41a5SAli Bahrami	if ($Isalist =~ /sparcv9/) {
104175ce41a5SAli Bahrami		$Ena64 = "ok";
104275ce41a5SAli Bahrami	}
104375ce41a5SAli Bahrami} elsif ($Mach =~ /i386/) {
104475ce41a5SAli Bahrami	if ($Isalist =~ /amd64/) {
104575ce41a5SAli Bahrami		$Ena64 = "ok";
10464840e086Srie	}
10474840e086Srie}
10484840e086Srie
104975ce41a5SAli Bahrami# $Env is used with all calls to ldd. It is set by AltObjectConfig to
105075ce41a5SAli Bahrami# cause an alternate object mapping runtime config file to be used.
105175ce41a5SAli Bahrami$Env = '';
10527c478bd9Sstevel@tonic-gate
105375ce41a5SAli Bahrami# Check that we have arguments.
105475ce41a5SAli Bahramiif ((getopts('D:d:E:e:f:I:imosvw:', \%opt) == 0) ||
105575ce41a5SAli Bahrami    (!$opt{f} && ($#ARGV == -1))) {
105675ce41a5SAli Bahrami	print "usage: $Prog [-imosv] [-D depfile | -d depdir] [-E errfile]\n";
105775ce41a5SAli Bahrami	print "\t\t[-e exfile] [-f listfile] [-I infofile] [-w outdir]\n";
105875ce41a5SAli Bahrami	print "\t\t[file | dir]...\n";
105975ce41a5SAli Bahrami	print "\n";
106075ce41a5SAli Bahrami	print "\t[-D depfile]\testablish dependencies from 'find_elf -r' file list\n";
106175ce41a5SAli Bahrami	print "\t[-d depdir]\testablish dependencies from under directory\n";
106275ce41a5SAli Bahrami	print "\t[-E errfile]\tdirect error output to file\n";
106375ce41a5SAli Bahrami	print "\t[-e exfile]\texceptions file\n";
106475ce41a5SAli Bahrami	print "\t[-f listfile]\tuse file list produced by find_elf -r\n";
106575ce41a5SAli Bahrami	print "\t[-I infofile]\tdirect informational output (-i, -v) to file\n";
106675ce41a5SAli Bahrami	print "\t[-i]\t\tproduce dynamic table entry information\n";
106775ce41a5SAli Bahrami	print "\t[-m]\t\tprocess mcs(1) comments\n";
106875ce41a5SAli Bahrami	print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
106975ce41a5SAli Bahrami	print "\t[-s]\t\tprocess .stab and .symtab entries\n";
107075ce41a5SAli Bahrami	print "\t[-v]\t\tprocess version definition entries\n";
107175ce41a5SAli Bahrami	print "\t[-w outdir]\tinterpret all files relative to given directory\n";
107275ce41a5SAli Bahrami	exit 1;
107375ce41a5SAli Bahrami}
107475ce41a5SAli Bahrami
107575ce41a5SAli Bahramidie "$Prog: -D and -d options are mutually exclusive\n" if ($opt{D} && $opt{d});
107675ce41a5SAli Bahrami
107775ce41a5SAli Bahrami$Tmpdir = "/tmp" if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir));
107875ce41a5SAli Bahrami
107975ce41a5SAli Bahrami# If -w, change working directory to given location
108075ce41a5SAli Bahrami!$opt{w} || chdir($opt{w}) || die "$Prog: can't cd to $opt{w}";
108175ce41a5SAli Bahrami
108275ce41a5SAli Bahrami# Locate and process the exceptions file
108375ce41a5SAli Bahramionbld_elfmod::LoadExceptionsToEXRE('check_rtime');
108475ce41a5SAli Bahrami
108575ce41a5SAli Bahrami# Is there a proto area available, either via the -d option, or because
108675ce41a5SAli Bahrami# we are part of an activated workspace?
108775ce41a5SAli Bahramimy $Proto;
108875ce41a5SAli Bahramiif ($opt{d}) {
108975ce41a5SAli Bahrami	# User specified dependency directory - make sure it exists.
109075ce41a5SAli Bahrami	-d $opt{d} || die "$Prog: $opt{d} is not a directory\n";
109175ce41a5SAli Bahrami	$Proto = $opt{d};
109275ce41a5SAli Bahrami} elsif ($ENV{CODEMGR_WS}) {
109375ce41a5SAli Bahrami	my $Root;
109475ce41a5SAli Bahrami
109575ce41a5SAli Bahrami	# Without a user specified dependency directory see if we're
109675ce41a5SAli Bahrami	# part of a codemanager workspace and if a proto area exists.
109775ce41a5SAli Bahrami	$Proto = $Root if ($Root = $ENV{ROOT}) && (-d $Root);
109875ce41a5SAli Bahrami}
109975ce41a5SAli Bahrami
110075ce41a5SAli Bahrami# If we are basing this analysis off the sharable objects found in
110175ce41a5SAli Bahrami# a proto area, then gather dependencies and construct an alternative
110275ce41a5SAli Bahrami# dependency mapping via a crle(1) configuration file.
110375ce41a5SAli Bahrami#
110475ce41a5SAli Bahrami# To support alternative dependency mapping we'll need ldd(1)'s
110575ce41a5SAli Bahrami# -e option.  This is relatively new (s81_30), so make sure
110675ce41a5SAli Bahrami# ldd(1) is capable before gathering any dependency information.
110775ce41a5SAli Bahramiif ($opt{D} || $Proto) {
110875ce41a5SAli Bahrami	if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
110975ce41a5SAli Bahrami		print "ldd: does not support -e, unable to ";
111075ce41a5SAli Bahrami		print "create alternative dependency mappingings.\n";
111175ce41a5SAli Bahrami		print "ldd: option added under 4390308 (s81_30).\n\n";
11127c478bd9Sstevel@tonic-gate	} else {
111375ce41a5SAli Bahrami		# If -D was specified, it supplies a list of files in
111475ce41a5SAli Bahrami		# 'find_elf -r' format, and can use it directly. Otherwise,
111575ce41a5SAli Bahrami		# we will run find_elf as a child process to find the
111675ce41a5SAli Bahrami		# sharable objects found under $Proto.
111775ce41a5SAli Bahrami		AltObjectConfig($opt{D} ? $opt{D} : "find_elf -frs $Proto|");
11187c478bd9Sstevel@tonic-gate	}
11197c478bd9Sstevel@tonic-gate}
11207c478bd9Sstevel@tonic-gate
112175ce41a5SAli Bahrami# To support unreferenced dependency detection we'll need ldd(1)'s -U
112275ce41a5SAli Bahrami# option.  This is relatively new (4638070), and if not available we
112375ce41a5SAli Bahrami# can still fall back to -u.  Even with this option, don't use -U with
112475ce41a5SAli Bahrami# releases prior to 5.10 as the cleanup for -U use only got integrated
112575ce41a5SAli Bahrami# into 5.10 under 4642023.  Note, that nightly doesn't typically set a
112675ce41a5SAli Bahrami# RELEASE from the standard <env> files.  Users who wish to disable use
112775ce41a5SAli Bahrami# of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
112875ce41a5SAli Bahrami# if using nightly, or otherwise establish it in their environment.
112975ce41a5SAli Bahramiif (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
113075ce41a5SAli Bahrami	$LddNoU = 1;
113175ce41a5SAli Bahrami} else {
113275ce41a5SAli Bahrami	my($Release);
113375ce41a5SAli Bahrami
113475ce41a5SAli Bahrami	if (($Release = $ENV{RELEASE}) && (cmp_os_ver($Release, "<", "5.10"))) {
113575ce41a5SAli Bahrami		$LddNoU = 1;
113675ce41a5SAli Bahrami	} else {
113775ce41a5SAli Bahrami		$LddNoU = 0;
113875ce41a5SAli Bahrami	}
11397c478bd9Sstevel@tonic-gate}
11407c478bd9Sstevel@tonic-gate
114175ce41a5SAli Bahrami# Set up variables used to handle output files:
114275ce41a5SAli Bahrami#
114375ce41a5SAli Bahrami# Error messages go to stdout unless -E is specified. $ErrFH is a
114475ce41a5SAli Bahrami# file handle reference that points at the file handle where error messages
114575ce41a5SAli Bahrami# are sent, and $ErrTtl is a reference that points at an integer used
114675ce41a5SAli Bahrami# to count how many lines have been sent there.
114775ce41a5SAli Bahrami#
114875ce41a5SAli Bahrami# Informational messages go to stdout unless -I is specified. $InfoFH is a
114975ce41a5SAli Bahrami# file handle reference that points at the file handle where info messages
115075ce41a5SAli Bahrami# are sent, and $InfoTtl is a reference that points at an integer used
115175ce41a5SAli Bahrami# to count how many lines have been sent there.
115275ce41a5SAli Bahrami#
115375ce41a5SAli Bahramiif ($opt{E}) {
115475ce41a5SAli Bahrami	open(ERROR, ">$opt{E}") || die "$Prog: open failed: $opt{E}";
115575ce41a5SAli Bahrami	$ErrFH = \*ERROR;
115675ce41a5SAli Bahrami} else {
115775ce41a5SAli Bahrami	$ErrFH = \*STDOUT;
11587c478bd9Sstevel@tonic-gate}
115975ce41a5SAli Bahrami
116075ce41a5SAli Bahramiif ($opt{I}) {
116175ce41a5SAli Bahrami	open(INFO, ">$opt{I}") || die "$Prog: open failed: $opt{I}";
116275ce41a5SAli Bahrami	$InfoFH = \*INFO;
116375ce41a5SAli Bahrami} else {
116475ce41a5SAli Bahrami	$InfoFH = \*STDOUT;
11657c478bd9Sstevel@tonic-gate}
116675ce41a5SAli Bahramimy ($err_dev, $err_ino) = stat($ErrFH);
116775ce41a5SAli Bahramimy ($info_dev, $info_ino) = stat($InfoFH);
116875ce41a5SAli Bahrami$ErrTtl = \$OutCnt1;
116975ce41a5SAli Bahrami$InfoTtl = (($err_dev == $info_dev) && ($err_ino == $info_ino)) ?
117075ce41a5SAli Bahrami    \$OutCnt1 : \$OutCnt2;
117175ce41a5SAli Bahrami
117275ce41a5SAli Bahrami
117375ce41a5SAli Bahrami# If we were given a list of objects in 'find_elf -r' format, then
117475ce41a5SAli Bahrami# process it.
117575ce41a5SAli BahramiProcFindElf($opt{f}) if $opt{f};
117675ce41a5SAli Bahrami
117775ce41a5SAli Bahrami# Process each argument
117875ce41a5SAli Bahramiforeach my $Arg (@ARGV) {
117975ce41a5SAli Bahrami	# Run find_elf to find the files given by $Arg and process them
118075ce41a5SAli Bahrami	ProcFindElf("find_elf -fr $Arg|");
11817c478bd9Sstevel@tonic-gate}
118275ce41a5SAli Bahrami
118375ce41a5SAli Bahrami# Cleanup output files
118475ce41a5SAli Bahramiunlink $Conf64 if $Conf64;
118575ce41a5SAli Bahramiunlink $Conf32 if $Conf32;
118675ce41a5SAli Bahramiclose ERROR if $opt{E};
118775ce41a5SAli Bahramiclose INFO if $opt{I};
118875ce41a5SAli Bahrami
118975ce41a5SAli Bahramiexit 0;
1190