xref: /titanic_50/usr/src/tools/scripts/check_rtime.pl (revision 1886f67eb9de99edbcda88765f2c603a30c87fc2)
1#!/usr/perl5/bin/perl -w
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27#
28# Check ELF information.
29#
30# This script descends a directory hierarchy inspecting ELF dynamic executables
31# and shared objects.  The general theme is to verify that common Makefile rules
32# have been used to build these objects.  Typical failures occur when Makefile
33# rules are re-invented rather than being inherited from "cmd/lib" Makefiles.
34#
35# As always, a number of components don't follow the rules, and these are
36# excluded to reduce this scripts output.
37#
38# By default any file that has conditions that should be reported is first
39# listed and then each condition follows.  The -o (one-line) option produces a
40# more terse output which is better for sorting/diffing with "nightly".
41#
42# NOTE: missing dependencies, symbols or versions are reported by running the
43# file through ldd(1).  As objects within a proto area are built to exist in a
44# base system, standard use of ldd(1) will bind any objects to dependencies
45# that exist in the base system.  It is frequently the case that newer objects
46# exist in the proto area that are required to satisfy other objects
47# dependencies, and without using these newer objects an ldd(1) will produce
48# misleading error messages.  To compensate for this, the -D/-d options, or the
49# existence of the CODEMSG_WS/ROOT environment variables, cause the creation of
50# alternative dependency mappings via crle(1) configuration files that establish
51# any proto shared objects as alternatives to their base system location.  Thus
52# ldd(1) can be executed against these configuration files so that objects in a
53# proto area bind to their dependencies in the same proto area.
54
55
56# Define all global variables (required for strict)
57use vars  qw($Prog $Env $Ena64 $Tmpdir);
58use vars  qw($LddNoU $Conf32 $Conf64);
59use vars  qw(%opt);
60use vars  qw($ErrFH $ErrTtl $InfoFH $InfoTtl $OutCnt1 $OutCnt2);
61
62# An exception file is used to specify regular expressions to match
63# objects. These directives specify special attributes of the object.
64# The regular expressions are read from the file and compiled into the
65# regular expression variables.
66#
67# The name of each regular expression variable is of the form
68#
69#	$EXRE_xxx
70#
71# where xxx is the name of the exception in lower case. For example,
72# the regular expression variable for EXEC_STACK is $EXRE_exec_stack.
73#
74# onbld_elfmod::LoadExceptionsToEXRE() depends on this naming convention
75# to initialize the regular expression variables, and to detect invalid
76# exception names.
77#
78# If a given exception is not used in the exception file, its regular
79# expression variable will be undefined. Users of these variables must
80# test the variable with defined() prior to use:
81#
82#	defined($EXRE_exec_stack) && ($foo =~ $EXRE_exec_stack)
83#
84# or if the test is to make sure the item is not specified:
85#
86#	!defined($EXRE_exec_stack) || ($foo !~ $EXRE_exec_stack)
87#
88# ----
89#
90# The exceptions are:
91#
92#   EXEC_DATA
93#	Objects that are not required to have non-executable writable
94#	data segments.
95#
96#   EXEC_STACK
97#	Objects that are not required to have a non-executable stack
98#
99#   NOCRLEALT
100#	Objects that should be skipped by AltObjectConfig() when building
101#	the crle script that maps objects to the proto area.
102#
103#    NODIRECT
104#	Objects that are not required to use direct bindings
105#
106#    NOSYMSORT
107#	Objects we should not check for duplicate addresses in
108#	the symbol sort sections.
109#
110#    OLDDEP
111#	Objects that are no longer needed because their functionalty
112#	has migrated elsewhere. These are usually pure filters that
113#	point at libc.
114#
115#    SKIP
116#	Files and directories that should be excluded from analysis.
117#
118#    STAB
119#	Objects that are allowed to contain stab debugging sections
120#
121#    TEXTREL
122#	Object for which relocations are allowed to the text segment
123#
124#    UNDEF_REF
125#	Objects that are allowed undefined references
126#
127#    UNREF_OBJ
128#	"unreferenced object=" ldd(1) diagnostics.
129#
130#    UNUSED_DEPS
131#	Objects that are allowed to have unused dependencies
132#
133#    UNUSED_OBJ
134#	Objects that are allowed to be unused dependencies
135#
136#    UNUSED_RPATH
137#	Objects with unused runpaths
138#
139
140use vars  qw($EXRE_exec_data $EXRE_exec_stack $EXRE_nocrlealt);
141use vars  qw($EXRE_nodirect $EXRE_nosymsort);
142use vars  qw($EXRE_olddep $EXRE_skip $EXRE_stab $EXRE_textrel $EXRE_undef_ref);
143use vars  qw($EXRE_unref_obj $EXRE_unused_deps $EXRE_unused_obj);
144use vars  qw($EXRE_unused_rpath);
145
146use strict;
147use Getopt::Std;
148use File::Basename;
149
150
151# Reliably compare two OS revisions.  Arguments are <ver1> <op> <ver2>.
152# <op> is the string form of a normal numeric comparison operator.
153sub cmp_os_ver {
154	my @ver1 = split(/\./, $_[0]);
155	my $op = $_[1];
156	my @ver2 = split(/\./, $_[2]);
157
158	push @ver2, ("0") x $#ver1 - $#ver2;
159	push @ver1, ("0") x $#ver2 - $#ver1;
160
161	my $diff = 0;
162	while (@ver1 || @ver2) {
163		if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
164			last;
165		}
166	}
167	return (eval "$diff $op 0" ? 1 : 0);
168}
169
170## ProcFile(FullPath, RelPath, File, Class, Type, Verdef)
171#
172# Determine whether this a ELF dynamic object and if so investigate its runtime
173# attributes.
174#
175sub ProcFile {
176	my($FullPath, $RelPath, $Class, $Type, $Verdef) = @_;
177	my(@Elf, @Ldd, $Dyn, $Sym, $Stack);
178	my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort);
179	my($Val, $Header, $IsX86, $RWX, $UnDep);
180	my($HasDirectBinding);
181
182	# Only look at executables and sharable objects
183	return if ($Type ne 'EXEC') && ($Type ne 'DYN');
184
185	# Ignore symbolic links
186	return if -l $FullPath;
187
188	# Is this an object or directory hierarchy we don't care about?
189	return if (defined($EXRE_skip) && ($RelPath =~ $EXRE_skip));
190
191	# Bail if we can't stat the file. Otherwise, note if it is SUID/SGID.
192	return if !stat($FullPath);
193	my $Secure = (-u _ || -g _) ? 1 : 0;
194
195	# Reset output message counts for new input file
196	$$ErrTtl = $$InfoTtl = 0;
197
198	@Ldd = 0;
199
200	# Determine whether we have access to inspect the file.
201	if (!(-r $FullPath)) {
202		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
203		    "unable to inspect file: permission denied");
204		return;
205	}
206
207	# Determine whether we have a executable (static or dynamic) or a
208	# shared object.
209	@Elf = split(/\n/, `elfdump -epdcy $FullPath 2>&1`);
210
211	$Dyn = $Stack = $IsX86 = $RWX = 0;
212	$Header = 'None';
213	foreach my $Line (@Elf) {
214		# If we have an invalid file type (which we can tell from the
215		# first line), or we're processing an archive, bail.
216		if ($Header eq 'None') {
217			if (($Line =~ /invalid file/) ||
218			    ($Line =~ /\Q$FullPath\E(.*):/)) {
219				return;
220			}
221		}
222
223		if ($Line =~ /^ELF Header/) {
224			$Header = 'Ehdr';
225			next;
226		}
227
228		if ($Line =~ /^Program Header/) {
229			$Header = 'Phdr';
230			$RWX = 0;
231			next;
232		}
233
234		if ($Line =~ /^Dynamic Section/) {
235			# A dynamic section indicates we're a dynamic object
236			# (this makes sure we don't check static executables).
237			$Dyn = 1;
238			next;
239		}
240
241		if (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
242			# If it's a X86 object, we need to enforce RW- data.
243			$IsX86 = 1 if $Line =~ /(EM_AMD64|EM_386)/;
244			next;
245		}
246
247		if (($Header eq 'Phdr') &&
248		    ($Line =~ /\[ PF_X\s+PF_W\s+PF_R \]/)) {
249			# RWX segment seen.
250			$RWX = 1;
251			next;
252		}
253
254		if (($Header eq 'Phdr') &&
255		    ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
256			# Seen an RWX PT_LOAD segment.
257			if (!defined($EXRE_exec_data) ||
258			    ($RelPath !~ $EXRE_exec_data)) {
259				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
260				    "application requires non-executable " .
261				    "data\t<no -Mmapfile_noexdata?>");
262			}
263			next;
264		}
265
266		if (($Header eq 'Phdr') && ($Line =~ /\[ PT_SUNWSTACK \]/)) {
267			# This object defines a non-executable stack.
268			$Stack = 1;
269			next;
270		}
271	}
272
273	# Determine whether this ELF executable or shared object has a
274	# conforming mcs(1) comment section.  If the correct $(POST_PROCESS)
275	# macros are used, only a 3 or 4 line .comment section should exist
276	# containing one or two "@(#)SunOS" identifying comments (one comment
277	# for a non-debug build, and two for a debug build). The results of
278	# the following split should be three or four lines, the last empty
279	# line being discarded by the split.
280	if ($opt{m}) {
281		my(@Mcs, $Con, $Dev);
282
283		@Mcs = split(/\n/, `mcs -p $FullPath 2>&1`);
284
285		$Con = $Dev = $Val = 0;
286		foreach my $Line (@Mcs) {
287			$Val++;
288
289			if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) {
290				$Con = 1;
291				last;
292			}
293			if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) {
294				$Dev = 1;
295				next;
296			}
297			if (($Dev == 0) && ($Val == 4)) {
298				$Con = 1;
299				last;
300			}
301			if (($Dev == 1) && ($Val == 5)) {
302				$Con = 1;
303				last;
304			}
305		}
306		if ($opt{m} && ($Con == 1)) {
307			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
308		    "non-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>");
309		}
310	}
311
312	# Applications should contain a non-executable stack definition.
313	if (($Type eq 'EXEC') && ($Stack == 0) &&
314	    (!defined($EXRE_exec_stack) || ($RelPath !~ $EXRE_exec_stack))) {
315		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
316		    "non-executable stack required\t<no -Mmapfile_noexstk?>");
317	}
318
319	# Having caught any static executables in the mcs(1) check and non-
320	# executable stack definition check, continue with dynamic objects
321	# from now on.
322	if ($Dyn eq 0) {
323		return;
324	}
325
326	# Use ldd unless its a 64-bit object and we lack the hardware.
327	if (($Class == 32) || $Ena64) {
328		my $LDDFullPath = $FullPath;
329
330		if ($Secure) {
331			# The execution of a secure application over an nfs file
332			# system mounted nosuid will result in warning messages
333			# being sent to /var/adm/messages.  As this type of
334			# environment can occur with root builds, move the file
335			# being investigated to a safe place first.  In addition
336			# remove its secure permission so that it can be
337			# influenced by any alternative dependency mappings.
338
339			my $File = $RelPath;
340			$File =~ s!^.*/!!;      # basename
341
342			my($TmpPath) = "$Tmpdir/$File";
343
344			system('cp', $LDDFullPath, $TmpPath);
345			chmod 0777, $TmpPath;
346			$LDDFullPath = $TmpPath;
347		}
348
349		# Use ldd(1) to determine the objects relocatability and use.
350		# By default look for all unreferenced dependencies.  However,
351		# some objects have legitimate dependencies that they do not
352		# reference.
353		if ($LddNoU) {
354			$Lddopt = "-ru";
355		} else {
356			$Lddopt = "-rU";
357		}
358		@Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
359		if ($Secure) {
360			unlink $LDDFullPath;
361		}
362	}
363
364	$Val = 0;
365	$Sym = 5;
366	$UnDep = 1;
367
368	foreach my $Line (@Ldd) {
369
370		if ($Val == 0) {
371			$Val = 1;
372			# Make sure ldd(1) worked.  One possible failure is that
373			# this is an old ldd(1) prior to -e addition (4390308).
374			if ($Line =~ /usage:/) {
375				$Line =~ s/$/\t<old ldd(1)?>/;
376				onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
377				    $RelPath, $Line);
378				last;
379			} elsif ($Line =~ /execution failed/) {
380				onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
381				    $RelPath, $Line);
382				last;
383			}
384
385			# It's possible this binary can't be executed, ie. we've
386			# found a sparc binary while running on an intel system,
387			# or a sparcv9 binary on a sparcv7/8 system.
388			if ($Line =~ /wrong class/) {
389				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
390				    "has wrong class or data encoding");
391				next;
392			}
393
394			# Historically, ldd(1) likes executable objects to have
395			# their execute bit set.
396			if ($Line =~ /not executable/) {
397				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
398				    "is not executable");
399				next;
400			}
401		}
402
403		# Look for "file" or "versions" that aren't found.  Note that
404		# these lines will occur before we find any symbol referencing
405		# errors.
406		if (($Sym == 5) && ($Line =~ /not found\)/)) {
407			if ($Line =~ /file not found\)/) {
408				$Line =~ s/$/\t<no -zdefs?>/;
409			}
410			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
411			next;
412		}
413		# Look for relocations whose symbols can't be found.  Note, we
414		# only print out the first 5 relocations for any file as this
415		# output can be excessive.
416		if ($Sym && ($Line =~ /symbol not found/)) {
417			# Determine if this file is allowed undefined
418			# references.
419			if (($Sym == 5) && defined($EXRE_undef_ref) &&
420			    ($RelPath =~ $EXRE_undef_ref)) {
421				$Sym = 0;
422				next;
423			}
424			if ($Sym-- == 1) {
425				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
426				    "continued ...") if !$opt{o};
427				next;
428			}
429			# Just print the symbol name.
430			$Line =~ s/$/\t<no -zdefs?>/;
431			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
432			next;
433		}
434		# Look for any unused search paths.
435		if ($Line =~ /unused search path=/) {
436			next if defined($EXRE_unused_rpath) &&
437			    ($Line =~ $EXRE_unused_rpath);
438
439			if ($Secure) {
440				$Line =~ s!$Tmpdir/!!;
441			}
442			$Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
443			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
444			next;
445		}
446		# Look for unreferenced dependencies.  Note, if any unreferenced
447		# objects are ignored, then set $UnDep so as to suppress any
448		# associated unused-object messages.
449		if ($Line =~ /unreferenced object=/) {
450			if (defined($EXRE_unref_obj) &&
451			    ($Line =~ $EXRE_unref_obj)) {
452				$UnDep = 0;
453				next;
454			}
455			if ($Secure) {
456				$Line =~ s!$Tmpdir/!!;
457			}
458			$Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
459			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
460			next;
461		}
462		# Look for any unused dependencies.
463		if ($UnDep && ($Line =~ /unused/)) {
464			# Skip if object is allowed to have unused dependencies
465			next if defined($EXRE_unused_deps) &&
466			    ($RelPath =~ $EXRE_unused_deps);
467
468			# Skip if dependency is always allowed to be unused
469			next if defined($EXRE_unused_obj) &&
470			    ($Line =~ $EXRE_unused_obj);
471
472			$Line =~ s!$Tmpdir/!! if $Secure;
473			$Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
474			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
475			next;
476		}
477	}
478
479	# Reuse the elfdump(1) data to investigate additional dynamic linking
480	# information.
481
482	$Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
483	$Tex = $Strip = 1;
484	$HasDirectBinding = 0;
485
486	$Header = 'None';
487ELF:	foreach my $Line (@Elf) {
488		# We're only interested in the section headers and the dynamic
489		# section.
490		if ($Line =~ /^Section Header/) {
491			$Header = 'Shdr';
492
493			if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
494				# This object has a combined relocation section.
495				$Sun = 1;
496
497			} elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
498				# This object contain .stabs sections
499				$Stab = 1;
500			} elsif (($SymSort == 0) &&
501				 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) {
502				# This object contains a symbol sort section
503				$SymSort = 1;
504			}
505
506			if (($Strip == 1) && ($Line =~ /\.symtab/)) {
507				# This object contains a complete symbol table.
508				$Strip = 0;
509			}
510			next;
511
512		} elsif ($Line =~ /^Dynamic Section/) {
513			$Header = 'Dyn';
514			next;
515		} elsif ($Line =~ /^Syminfo Section/) {
516			$Header = 'Syminfo';
517			next;
518		} elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) {
519			next;
520		}
521
522		# Look into the Syminfo section.
523		# Does this object have at least one Directly Bound symbol?
524		if (($Header eq 'Syminfo')) {
525			my(@Symword);
526
527			if ($HasDirectBinding == 1) {
528				next;
529			}
530
531			@Symword = split(' ', $Line);
532
533			if (!defined($Symword[1])) {
534				next;
535			}
536			if ($Symword[1] =~ /B/) {
537				$HasDirectBinding = 1;
538			}
539			next;
540		}
541
542		# Does this object contain text relocations.
543		if ($Tex && ($Line =~ /TEXTREL/)) {
544			# Determine if this file is allowed text relocations.
545			if (defined($EXRE_textrel) &&
546			    ($RelPath =~ $EXRE_textrel)) {
547				$Tex = 0;
548				next ELF;
549			}
550			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
551			    "TEXTREL .dynamic tag\t\t\t<no -Kpic?>");
552			$Tex = 0;
553			next;
554		}
555
556		# Does this file have any relocation sections (there are a few
557		# psr libraries with no relocations at all, thus a .SUNW_reloc
558		# section won't exist either).
559		if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
560			$Relsz = hex((split(' ', $Line))[2]);
561			next;
562		}
563
564		# Does this file have any plt relocations.  If the plt size is
565		# equivalent to the total relocation size then we don't have
566		# any relocations suitable for combining into a .SUNW_reloc
567		# section.
568		if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
569			$Pltsz = hex((split(' ', $Line))[2]);
570			next;
571		}
572
573		# Does this object have any dependencies.
574		if ($Line =~ /NEEDED/) {
575			my($Need) = (split(' ', $Line))[3];
576
577			if (defined($EXRE_olddep) && ($Need =~ $EXRE_olddep)) {
578				# Catch any old (unnecessary) dependencies.
579				onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
580			"NEEDED=$Need\t<dependency no longer necessary>");
581			} elsif ($opt{i}) {
582				# Under the -i (information) option print out
583				# any useful dynamic entries.
584				onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
585				    "NEEDED=$Need");
586			}
587			next;
588		}
589
590		# Is this object built with -B direct flag on?
591		if ($Line =~ / DIRECT /) {
592			$HasDirectBinding = 1;
593		}
594
595		# Does this object specify a runpath.
596		if ($opt{i} && ($Line =~ /RPATH/)) {
597			my($Rpath) = (split(' ', $Line))[3];
598			onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
599			    $RelPath, "RPATH=$Rpath");
600			next;
601		}
602	}
603
604	# A shared object, that contains non-plt relocations, should have a
605	# combined relocation section indicating it was built with -z combreloc.
606	if (($Type eq 'DYN') && $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
607		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
608		    ".SUNW_reloc section missing\t\t<no -zcombreloc?>");
609	}
610
611	# No objects released to a customer should have any .stabs sections
612	# remaining, they should be stripped.
613	if ($opt{s} && $Stab) {
614		goto DONESTAB if defined($EXRE_stab) && ($RelPath =~ $EXRE_stab);
615
616		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
617		    "debugging sections should be deleted\t<no strip -x?>");
618	}
619
620	# Identify an object that is not built with either -B direct or
621	# -z direct.
622	goto DONESTAB
623	    if (defined($EXRE_nodirect) && ($RelPath =~ $EXRE_nodirect));
624
625	if ($Relsz && ($HasDirectBinding == 0)) {
626		onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
627		 "object has no direct bindings\t<no -B direct or -z direct?>");
628	}
629
630DONESTAB:
631
632	# All objects should have a full symbol table to provide complete
633	# debugging stack traces.
634	onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
635	    "symbol table should not be stripped\t<remove -s?>") if $Strip;
636
637	# If there are symbol sort sections in this object, report on
638	# any that have duplicate addresses.
639	ProcSymSort($FullPath, $RelPath) if $SymSort;
640
641	# If -v was specified, and the object has a version definition
642	# section, generate output showing each public symbol and the
643	# version it belongs to.
644	ProcVerdef($FullPath, $RelPath)
645	    if ($Verdef eq 'VERDEF') && $opt{v};
646}
647
648
649## ProcSymSortOutMsg(RelPath, secname, addr, names...)
650#
651# Call onbld_elfmod::OutMsg for a duplicate address error in a symbol sort
652# section
653#
654sub ProcSymSortOutMsg {
655	my($RelPath, $secname, $addr, @names) = @_;
656
657	onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
658	    "$secname: duplicate $addr: ". join(', ', @names));
659}
660
661
662## ProcSymSort(FullPath, RelPath)
663#
664# Examine the symbol sort sections for the given object and report
665# on any duplicate addresses found.  Ideally, mapfile directives
666# should be used when building objects that have multiple symbols
667# with the same address so that only one of them appears in the sort
668# section. This saves space, reduces user confusion, and ensures that
669# libproc and debuggers always display public names instead of symbols
670# that are merely implementation details.
671#
672sub ProcSymSort {
673
674	my($FullPath, $RelPath) = @_;
675
676	# If this object is exempt from checking, return quietly
677	return if defined($EXRE_nosymsort) && ($FullPath =~ $EXRE_nosymsort);
678
679
680	open(SORT, "elfdump -S $FullPath|") ||
681	    die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
682
683	my $line;
684	my $last_addr;
685	my @dups = ();
686	my $secname;
687	while ($line = <SORT>) {
688		chomp $line;
689
690		next if ($line eq '');
691
692		# If this is a header line, pick up the section name
693		if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
694			$secname = $1;
695
696			# Every new section is followed by a column header line
697			$line = <SORT>;		# Toss header line
698
699			# Flush anything left from previous section
700			ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
701			    if (scalar(@dups) > 1);
702
703			# Reset variables for new sort section
704			$last_addr = '';
705			@dups = ();
706
707			next;
708		}
709
710		# Process symbol line
711		my @fields = split /\s+/, $line;
712		my $new_addr = $fields[2];
713		my $new_type = $fields[8];
714		my $new_name = $fields[9];
715
716		if ($new_type eq 'UNDEF') {
717			onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
718			    "$secname: unexpected UNDEF symbol " .
719			    "(link-editor error): $new_name");
720			next;
721		}
722
723		if ($new_addr eq $last_addr) {
724			push @dups, $new_name;
725		} else {
726			ProcSymSortOutMsg($RelPath, $secname,
727			    $last_addr, @dups) if (scalar(@dups) > 1);
728			@dups = ( $new_name );
729			$last_addr = $new_addr;
730		}
731	}
732
733	ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
734		if (scalar(@dups) > 1);
735
736	close SORT;
737}
738
739
740## ProcVerdef(FullPath, RelPath)
741#
742# Examine the version definition section for the given object and report
743# each public symbol along with the version it belongs to.
744#
745sub ProcVerdef {
746
747	my($FullPath, $RelPath) = @_;
748	my $line;
749	my $cur_ver = '';
750	my $tab = $opt{o} ? '' : "\t";
751
752	# pvs -dov provides information about the versioning hierarchy
753	# in the file. Lines are of the format:
754	#	path - version[XXX];
755	# where [XXX] indicates optional information, such as flags
756	# or inherited versions.
757	#
758	# Private versions are allowed to change freely, so ignore them.
759	open(PVS, "pvs -dov $FullPath|") ||
760	    die "$Prog: Unable to execute pvs (version definition section)\n";
761
762	while ($line = <PVS>) {
763		chomp $line;
764
765		if ($line =~ /^[^\s]+\s+-\s+([^;]+)/) {
766			my $ver = $1;
767
768			next if $ver =~ /private/i;
769			onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
770			    "${tab}VERDEF=$ver");
771		}
772	}
773	close PVS;
774
775	# pvs -dos lists the symbols assigned to each version definition.
776	# Lines are of the format:
777	#	path - version: symbol;
778	#	path - version: symbol (size);
779	# where the (size) is added to data items, but not for functions.
780	# We strip off the size, if present.
781
782	open(PVS, "pvs -dos $FullPath|") ||
783	    die "$Prog: Unable to execute pvs (version definition section)\n";
784	while ($line = <PVS>) {
785		chomp $line;
786		if ($line =~ /^[^\s]+\s+-\s+([^:]+):\s*([^\s;]+)/) {
787		    my $ver = $1;
788		    my $sym = $2;
789
790		    next if $ver =~ /private/i;
791
792		    if ($opt{o}) {
793			onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
794			    "VERSION=$ver, SYMBOL=$sym");
795		    } else {
796			if ($cur_ver ne $ver) {
797			    onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
798			        $RelPath, "VERSION=$ver");
799			    $cur_ver = $ver;
800			}
801			onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
802			    $RelPath, "SYMBOL=$sym");
803		    }
804		}
805	}
806
807	close PVS;
808}
809
810
811## OpenFindElf(file, FileHandleRef, LineNumRef)
812#
813# Open file in 'find_elf -r' format, and return the value of
814# the opening PREFIX line.
815#
816# entry:
817#	file - file, or find_elf child process, to open
818#	FileHandleRef - Reference to file handle to open
819#	LineNumRef - Reference to integer to increment as lines are input
820#
821# exit:
822#	This routine issues a fatal error and does not return on error.
823#	Otherwise, the value of PREFIX is returned.
824#
825sub OpenFindElf {
826	my ($file, $fh, $LineNum) = @_;
827	my $line;
828	my $prefix;
829
830	open($fh, $file) || die "$Prog: Unable to open: $file";
831	$$LineNum = 0;
832
833	# This script requires relative paths as created by 'find_elf -r'.
834	# When this is done, the first non-comment line will always
835	# be PREFIX. Obtain that line, or issue a fatal error.
836	while ($line = onbld_elfmod::GetLine($fh, $LineNum)) {
837		if ($line =~ /^PREFIX\s+(.*)$/i) {
838			$prefix = $1;
839			last;
840		}
841
842		die "$Prog: No PREFIX line seen on line $$LineNum: $file";
843	}
844
845	$prefix;
846}
847
848
849## ProcFindElf(file)
850#
851# Open the specified file, which must be produced by "find_elf -r",
852# and process the files it describes.
853#
854sub ProcFindElf {
855	my $file = $_[0];
856	my $line;
857	my $LineNum;
858
859	my $prefix = OpenFindElf($file, \*FIND_ELF, \$LineNum);
860
861	while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
862		next if !($line =~ /^OBJECT\s/i);
863
864		my ($item, $class, $type, $verdef, $obj) =
865		    split(/\s+/, $line, 5);
866
867		ProcFile("$prefix/$obj", $obj, $class, $type, $verdef);
868	}
869
870	close FIND_ELF;
871}
872
873
874## AltObjectConfig(file)
875#
876# Recurse through a directory hierarchy looking for appropriate dependencies
877# to map from their standard system locations to the proto area via a crle
878# config file.
879#
880# entry:
881#	file - File of ELF objects, in 'find_elf -r' format, to examine.
882#
883# exit:
884#	Scripts are generated for the 32 and 64-bit cases to run crle
885#	and create runtime configuration files that will establish
886#	alternative dependency mappings for the objects identified.
887#
888#	$Env - Set to environment variable definitions that will cause
889#		the config files generated by this routine to be used
890#		by ldd.
891#	$Conf32, $Conf64 - Undefined, or set to the config files generated
892#		by this routine. If defined, the caller is responsible for
893#		unlinking the files before exiting.
894#
895sub AltObjectConfig {
896	my $file = $_[0];
897	my ($Crle32, $Crle64);
898	my $line;
899	my $LineNum;
900	my $obj_path;
901	my $obj_active = 0;
902	my $obj_class;
903
904	my $prefix = OpenFindElf($file, \*FIND_ELF);
905
906LINE:
907	while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
908	      ITEM: {
909
910			if ($line =~ /^OBJECT\s/i) {
911				my ($item, $class, $type, $verdef, $obj) =
912				    split(/\s+/, $line, 5);
913
914				if ($type eq 'DYN') {
915					$obj_active = 1;
916					$obj_path = $obj;
917					$obj_class = $class;
918				} else {
919					# Only want sharable objects
920					$obj_active = 0;
921				}
922				last ITEM;
923			}
924
925			# We need to follow links to sharable objects so
926			# that any dependencies are expressed in all their
927			# available forms. We depend on ALIAS lines directly
928			# following the object they alias, so if we have
929			# a current object, this alias belongs to it.
930			if ($obj_active && ($line =~ /^ALIAS\s/i)) {
931				my ($item, $real_obj, $obj) =
932				    split(/\s+/, $line, 3);
933				$obj_path = $obj;
934				last ITEM;
935			}
936
937			# Skip unrecognized item
938			next LINE;
939		}
940
941		next if !$obj_active;
942
943		my $full = "$prefix/$obj_path";
944
945		next if defined($EXRE_nocrlealt) &&
946		    ($obj_path =~ $EXRE_nocrlealt);
947
948		my $Dir = $full;
949		$Dir =~ s/^(.*)\/.*$/$1/;
950
951		# Create a crle(1) script for the dependency we've found.
952		# We build separate scripts for the 32 and 64-bit cases.
953		# We create and initialize each script when we encounter
954		# the first object that needs it.
955		if ($obj_class == 32) {
956			if (!$Crle32) {
957				$Crle32 = "$Tmpdir/$Prog.crle32.$$";
958				open(CRLE32, "> $Crle32") ||
959				    die "$Prog: open failed: $Crle32: $!";
960				print CRLE32 "#!/bin/sh\ncrle \\\n";
961			}
962			print CRLE32 "\t-o $Dir -a /$obj_path \\\n";
963		} elsif ($Ena64) {
964			if (!$Crle64) {
965				$Crle64 = "$Tmpdir/$Prog.crle64.$$";
966				open(CRLE64, "> $Crle64") ||
967				    die "$Prog: open failed: $Crle64: $!";
968				print CRLE64 "#!/bin/sh\ncrle -64\\\n";
969			}
970			print CRLE64 "\t-o $Dir -a /$obj_path \\\n";
971		}
972	}
973
974	close FIND_ELF;
975
976
977	# Now that the config scripts are complete, use them to generate
978	# runtime linker config files.
979	if ($Crle64) {
980		$Conf64 = "$Tmpdir/$Prog.conf64.$$";
981		print CRLE64 "\t-c $Conf64\n";
982
983		chmod 0755, $Crle64;
984		close CRLE64;
985
986		undef $Conf64 if system($Crle64);
987
988		# Done with the script
989		unlink $Crle64;
990	}
991	if ($Crle32) {
992		$Conf32 = "$Tmpdir/$Prog.conf32.$$";
993		print CRLE32 "\t-c $Conf32\n";
994
995		chmod 0755, $Crle32;
996		close CRLE32;
997
998		undef $Conf32 if system($Crle32);
999
1000		# Done with the script
1001		unlink $Crle32;
1002	}
1003
1004	# Set $Env so that we will use the config files generated above
1005	# when we run ldd.
1006	if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
1007		$Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
1008	} elsif ($Crle64 && $Conf64) {
1009		$Env = "-e LD_FLAGS=config_64=$Conf64";
1010	} elsif ($Crle32 && $Conf32) {
1011		$Env = "-e LD_FLAGS=config_32=$Conf32";
1012	}
1013}
1014
1015# -----------------------------------------------------------------------------
1016
1017# This script relies on ldd returning output reflecting only the binary
1018# contents.  But if LD_PRELOAD* environment variables are present, libraries
1019# named by them will also appear in the output, disrupting our analysis.
1020# So, before we get too far, scrub the environment.
1021
1022delete($ENV{LD_PRELOAD});
1023delete($ENV{LD_PRELOAD_32});
1024delete($ENV{LD_PRELOAD_64});
1025
1026# Establish a program name for any error diagnostics.
1027chomp($Prog = `basename $0`);
1028
1029# The onbld_elfmod package is maintained in the same directory as this
1030# script, and is installed in ../lib/perl. Use the local one if present,
1031# and the installed one otherwise.
1032my $moddir = dirname($0);
1033$moddir = "$moddir/../lib/perl" if ! -f "$moddir/onbld_elfmod.pm";
1034require "$moddir/onbld_elfmod.pm";
1035
1036# Determine what machinery is available.
1037my $Mach = `uname -p`;
1038my$Isalist = `isalist`;
1039if ($Mach =~ /sparc/) {
1040	if ($Isalist =~ /sparcv9/) {
1041		$Ena64 = "ok";
1042	}
1043} elsif ($Mach =~ /i386/) {
1044	if ($Isalist =~ /amd64/) {
1045		$Ena64 = "ok";
1046	}
1047}
1048
1049# $Env is used with all calls to ldd. It is set by AltObjectConfig to
1050# cause an alternate object mapping runtime config file to be used.
1051$Env = '';
1052
1053# Check that we have arguments.
1054if ((getopts('D:d:E:e:f:I:imosvw:', \%opt) == 0) ||
1055    (!$opt{f} && ($#ARGV == -1))) {
1056	print "usage: $Prog [-imosv] [-D depfile | -d depdir] [-E errfile]\n";
1057	print "\t\t[-e exfile] [-f listfile] [-I infofile] [-w outdir]\n";
1058	print "\t\t[file | dir]...\n";
1059	print "\n";
1060	print "\t[-D depfile]\testablish dependencies from 'find_elf -r' file list\n";
1061	print "\t[-d depdir]\testablish dependencies from under directory\n";
1062	print "\t[-E errfile]\tdirect error output to file\n";
1063	print "\t[-e exfile]\texceptions file\n";
1064	print "\t[-f listfile]\tuse file list produced by find_elf -r\n";
1065	print "\t[-I infofile]\tdirect informational output (-i, -v) to file\n";
1066	print "\t[-i]\t\tproduce dynamic table entry information\n";
1067	print "\t[-m]\t\tprocess mcs(1) comments\n";
1068	print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
1069	print "\t[-s]\t\tprocess .stab and .symtab entries\n";
1070	print "\t[-v]\t\tprocess version definition entries\n";
1071	print "\t[-w outdir]\tinterpret all files relative to given directory\n";
1072	exit 1;
1073}
1074
1075die "$Prog: -D and -d options are mutually exclusive\n" if ($opt{D} && $opt{d});
1076
1077$Tmpdir = "/tmp" if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir));
1078
1079# If -w, change working directory to given location
1080!$opt{w} || chdir($opt{w}) || die "$Prog: can't cd to $opt{w}";
1081
1082# Locate and process the exceptions file
1083onbld_elfmod::LoadExceptionsToEXRE('check_rtime');
1084
1085# Is there a proto area available, either via the -d option, or because
1086# we are part of an activated workspace?
1087my $Proto;
1088if ($opt{d}) {
1089	# User specified dependency directory - make sure it exists.
1090	-d $opt{d} || die "$Prog: $opt{d} is not a directory\n";
1091	$Proto = $opt{d};
1092} elsif ($ENV{CODEMGR_WS}) {
1093	my $Root;
1094
1095	# Without a user specified dependency directory see if we're
1096	# part of a codemanager workspace and if a proto area exists.
1097	$Proto = $Root if ($Root = $ENV{ROOT}) && (-d $Root);
1098}
1099
1100# If we are basing this analysis off the sharable objects found in
1101# a proto area, then gather dependencies and construct an alternative
1102# dependency mapping via a crle(1) configuration file.
1103#
1104# To support alternative dependency mapping we'll need ldd(1)'s
1105# -e option.  This is relatively new (s81_30), so make sure
1106# ldd(1) is capable before gathering any dependency information.
1107if ($opt{D} || $Proto) {
1108	if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
1109		print "ldd: does not support -e, unable to ";
1110		print "create alternative dependency mappingings.\n";
1111		print "ldd: option added under 4390308 (s81_30).\n\n";
1112	} else {
1113		# If -D was specified, it supplies a list of files in
1114		# 'find_elf -r' format, and can use it directly. Otherwise,
1115		# we will run find_elf as a child process to find the
1116		# sharable objects found under $Proto.
1117		AltObjectConfig($opt{D} ? $opt{D} : "find_elf -frs $Proto|");
1118	}
1119}
1120
1121# To support unreferenced dependency detection we'll need ldd(1)'s -U
1122# option.  This is relatively new (4638070), and if not available we
1123# can still fall back to -u.  Even with this option, don't use -U with
1124# releases prior to 5.10 as the cleanup for -U use only got integrated
1125# into 5.10 under 4642023.  Note, that nightly doesn't typically set a
1126# RELEASE from the standard <env> files.  Users who wish to disable use
1127# of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
1128# if using nightly, or otherwise establish it in their environment.
1129if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
1130	$LddNoU = 1;
1131} else {
1132	my($Release);
1133
1134	if (($Release = $ENV{RELEASE}) && (cmp_os_ver($Release, "<", "5.10"))) {
1135		$LddNoU = 1;
1136	} else {
1137		$LddNoU = 0;
1138	}
1139}
1140
1141# Set up variables used to handle output files:
1142#
1143# Error messages go to stdout unless -E is specified. $ErrFH is a
1144# file handle reference that points at the file handle where error messages
1145# are sent, and $ErrTtl is a reference that points at an integer used
1146# to count how many lines have been sent there.
1147#
1148# Informational messages go to stdout unless -I is specified. $InfoFH is a
1149# file handle reference that points at the file handle where info messages
1150# are sent, and $InfoTtl is a reference that points at an integer used
1151# to count how many lines have been sent there.
1152#
1153if ($opt{E}) {
1154	open(ERROR, ">$opt{E}") || die "$Prog: open failed: $opt{E}";
1155	$ErrFH = \*ERROR;
1156} else {
1157	$ErrFH = \*STDOUT;
1158}
1159
1160if ($opt{I}) {
1161	open(INFO, ">$opt{I}") || die "$Prog: open failed: $opt{I}";
1162	$InfoFH = \*INFO;
1163} else {
1164	$InfoFH = \*STDOUT;
1165}
1166my ($err_dev, $err_ino) = stat($ErrFH);
1167my ($info_dev, $info_ino) = stat($InfoFH);
1168$ErrTtl = \$OutCnt1;
1169$InfoTtl = (($err_dev == $info_dev) && ($err_ino == $info_ino)) ?
1170    \$OutCnt1 : \$OutCnt2;
1171
1172
1173# If we were given a list of objects in 'find_elf -r' format, then
1174# process it.
1175ProcFindElf($opt{f}) if $opt{f};
1176
1177# Process each argument
1178foreach my $Arg (@ARGV) {
1179	# Run find_elf to find the files given by $Arg and process them
1180	ProcFindElf("find_elf -fr $Arg|");
1181}
1182
1183# Cleanup output files
1184unlink $Conf64 if $Conf64;
1185unlink $Conf32 if $Conf32;
1186close ERROR if $opt{E};
1187close INFO if $opt{I};
1188
1189exit 0;
1190