4505 check_rtime should always check search paths
[unleashed.git] / usr / src / tools / scripts / check_rtime.pl
blob10691e75afbb99a81bcce8ecb9b700f3938b370f
1 #!/usr/perl5/bin/perl -w
3 # CDDL HEADER START
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.
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.
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]
20 # CDDL HEADER END
24 # Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28 # Check ELF information.
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.
35 # As always, a number of components don't follow the rules, and these are
36 # excluded to reduce this scripts output.
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".
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.
56 # Define all global variables (required for strict)
57 use vars qw($Prog $Env $Ena64 $Tmpdir);
58 use vars qw($LddNoU $Conf32 $Conf64);
59 use vars qw(%opt);
60 use vars qw($ErrFH $ErrTtl $InfoFH $InfoTtl $OutCnt1 $OutCnt2);
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.
67 # The name of each regular expression variable is of the form
69 # $EXRE_xxx
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.
74 # onbld_elfmod::LoadExceptionsToEXRE() depends on this naming convention
75 # to initialize the regular expression variables, and to detect invalid
76 # exception names.
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:
82 # defined($EXRE_exec_stack) && ($foo =~ $EXRE_exec_stack)
84 # or if the test is to make sure the item is not specified:
86 # !defined($EXRE_exec_stack) || ($foo !~ $EXRE_exec_stack)
88 # ----
90 # The exceptions are:
92 # EXEC_DATA
93 # Objects that are not required to have non-executable writable
94 # data segments.
96 # EXEC_STACK
97 # Objects that are not required to have a non-executable stack
99 # NOCRLEALT
100 # Objects that should be skipped by AltObjectConfig() when building
101 # the crle script that maps objects to the proto area.
103 # NODIRECT
104 # Objects that are not required to use direct bindings
106 # NOSYMSORT
107 # Objects we should not check for duplicate addresses in
108 # the symbol sort sections.
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.
115 # SKIP
116 # Files and directories that should be excluded from analysis.
118 # STAB
119 # Objects that are allowed to contain stab debugging sections
121 # TEXTREL
122 # Object for which relocations are allowed to the text segment
124 # UNDEF_REF
125 # Objects that are allowed undefined references
127 # UNREF_OBJ
128 # "unreferenced object=" ldd(1) diagnostics.
130 # UNUSED_DEPS
131 # Objects that are allowed to have unused dependencies
133 # UNUSED_OBJ
134 # Objects that are allowed to be unused dependencies
136 # UNUSED_RPATH
137 # Objects with unused runpaths
140 use vars qw($EXRE_exec_data $EXRE_exec_stack $EXRE_nocrlealt);
141 use vars qw($EXRE_nodirect $EXRE_nosymsort);
142 use vars qw($EXRE_olddep $EXRE_skip $EXRE_stab $EXRE_textrel $EXRE_undef_ref);
143 use vars qw($EXRE_unref_obj $EXRE_unused_deps $EXRE_unused_obj);
144 use vars qw($EXRE_unused_rpath);
146 use strict;
147 use Getopt::Std;
148 use File::Basename;
151 # Reliably compare two OS revisions. Arguments are <ver1> <op> <ver2>.
152 # <op> is the string form of a normal numeric comparison operator.
153 sub cmp_os_ver {
154 my @ver1 = split(/\./, $_[0]);
155 my $op = $_[1];
156 my @ver2 = split(/\./, $_[2]);
158 push @ver2, ("0") x $#ver1 - $#ver2;
159 push @ver1, ("0") x $#ver2 - $#ver1;
161 my $diff = 0;
162 while (@ver1 || @ver2) {
163 if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
164 last;
167 return (eval "$diff $op 0" ? 1 : 0);
170 ## ProcFile(FullPath, RelPath, File, Class, Type, Verdef)
172 # Determine whether this a ELF dynamic object and if so investigate its runtime
173 # attributes.
175 sub 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);
182 # Only look at executables and sharable objects
183 return if ($Type ne 'EXEC') && ($Type ne 'DYN');
185 # Ignore symbolic links
186 return if -l $FullPath;
188 # Is this an object or directory hierarchy we don't care about?
189 return if (defined($EXRE_skip) && ($RelPath =~ $EXRE_skip));
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;
195 # Reset output message counts for new input file
196 $$ErrTtl = $$InfoTtl = 0;
198 @Ldd = 0;
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;
207 # Determine whether we have a executable (static or dynamic) or a
208 # shared object.
209 @Elf = split(/\n/, `elfdump -epdcy $FullPath 2>&1`);
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;
223 if ($Line =~ /^ELF Header/) {
224 $Header = 'Ehdr';
225 next;
228 if ($Line =~ /^Program Header/) {
229 $Header = 'Phdr';
230 $RWX = 0;
231 next;
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;
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;
247 if (($Header eq 'Phdr') &&
248 ($Line =~ /\[ PF_X\s+PF_W\s+PF_R \]/)) {
249 # RWX segment seen.
250 $RWX = 1;
251 next;
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?>");
263 next;
266 if (($Header eq 'Phdr') && ($Line =~ /\[ PT_SUNWSTACK \]/)) {
267 # This object defines a non-executable stack.
268 $Stack = 1;
269 next;
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);
283 @Mcs = split(/\n/, `mcs -p $FullPath 2>&1`);
285 $Con = $Dev = $Val = 0;
286 foreach my $Line (@Mcs) {
287 $Val++;
289 if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) {
290 $Con = 1;
291 last;
293 if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) {
294 $Dev = 1;
295 next;
297 if (($Dev == 0) && ($Val == 4)) {
298 $Con = 1;
299 last;
301 if (($Dev == 1) && ($Val == 5)) {
302 $Con = 1;
303 last;
306 if ($opt{m} && ($Con == 1)) {
307 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
308 "non-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>");
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?>");
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;
326 # Use ldd unless its a 64-bit object and we lack the hardware.
327 if (($Class == 32) || $Ena64) {
328 my $LDDFullPath = $FullPath;
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.
339 my $File = $RelPath;
340 $File =~ s!^.*/!!; # basename
342 my($TmpPath) = "$Tmpdir/$File";
344 system('cp', $LDDFullPath, $TmpPath);
345 chmod 0777, $TmpPath;
346 $LDDFullPath = $TmpPath;
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";
358 @Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
359 if ($Secure) {
360 unlink $LDDFullPath;
364 $Val = 0;
365 $Sym = 5;
366 $UnDep = 1;
368 foreach my $Line (@Ldd) {
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;
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;
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;
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?>/;
410 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
411 next;
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;
424 if ($Sym-- == 1) {
425 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
426 "continued ...") if !$opt{o};
427 next;
429 # Just print the symbol name.
430 $Line =~ s/$/\t<no -zdefs?>/;
431 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
432 next;
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);
439 if ($Secure) {
440 $Line =~ s!$Tmpdir/!!;
442 $Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
443 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
444 next;
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;
455 if ($Secure) {
456 $Line =~ s!$Tmpdir/!!;
458 $Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
459 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
460 next;
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);
468 # Skip if dependency is always allowed to be unused
469 next if defined($EXRE_unused_obj) &&
470 ($Line =~ $EXRE_unused_obj);
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;
479 # Reuse the elfdump(1) data to investigate additional dynamic linking
480 # information.
482 $Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
483 $Tex = $Strip = 1;
484 $HasDirectBinding = 0;
486 $Header = 'None';
487 ELF: 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';
493 if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
494 # This object has a combined relocation section.
495 $Sun = 1;
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;
506 if (($Strip == 1) && ($Line =~ /\.symtab/)) {
507 # This object contains a complete symbol table.
508 $Strip = 0;
510 next;
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;
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);
527 if ($HasDirectBinding == 1) {
528 next;
531 @Symword = split(' ', $Line);
533 if (!defined($Symword[1])) {
534 next;
536 if ($Symword[1] =~ /B/) {
537 $HasDirectBinding = 1;
539 next;
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;
550 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
551 "TEXTREL .dynamic tag\t\t\t<no -Kpic?>");
552 $Tex = 0;
553 next;
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;
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;
573 # Does this object have any dependencies.
574 if ($Line =~ /NEEDED/) {
575 my($Need) = (split(' ', $Line))[3];
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");
587 next;
590 # Is this object built with -B direct flag on?
591 if ($Line =~ / DIRECT /) {
592 $HasDirectBinding = 1;
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;
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?>");
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);
616 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
617 "debugging sections should be deleted\t<no strip -x?>");
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));
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?>");
630 DONESTAB:
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;
637 # If there are symbol sort sections in this object, report on
638 # any that have duplicate addresses.
639 ProcSymSort($FullPath, $RelPath) if $SymSort;
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};
649 ## ProcSymSortOutMsg(RelPath, secname, addr, names...)
651 # Call onbld_elfmod::OutMsg for a duplicate address error in a symbol sort
652 # section
654 sub ProcSymSortOutMsg {
655 my($RelPath, $secname, $addr, @names) = @_;
657 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
658 "$secname: duplicate $addr: ". join(', ', @names));
662 ## ProcSymSort(FullPath, RelPath)
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.
672 sub ProcSymSort {
674 my($FullPath, $RelPath) = @_;
676 # If this object is exempt from checking, return quietly
677 return if defined($EXRE_nosymsort) && ($FullPath =~ $EXRE_nosymsort);
680 open(SORT, "elfdump -S $FullPath|") ||
681 die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
683 my $line;
684 my $last_addr;
685 my @dups = ();
686 my $secname;
687 while ($line = <SORT>) {
688 chomp $line;
690 next if ($line eq '');
692 # If this is a header line, pick up the section name
693 if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
694 $secname = $1;
696 # Every new section is followed by a column header line
697 $line = <SORT>; # Toss header line
699 # Flush anything left from previous section
700 ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
701 if (scalar(@dups) > 1);
703 # Reset variables for new sort section
704 $last_addr = '';
705 @dups = ();
707 next;
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];
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;
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;
733 ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
734 if (scalar(@dups) > 1);
736 close SORT;
740 ## ProcVerdef(FullPath, RelPath)
742 # Examine the version definition section for the given object and report
743 # each public symbol along with the version it belongs to.
745 sub ProcVerdef {
747 my($FullPath, $RelPath) = @_;
748 my $line;
749 my $cur_ver = '';
750 my $tab = $opt{o} ? '' : "\t";
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.
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";
762 while ($line = <PVS>) {
763 chomp $line;
765 if ($line =~ /^[^\s]+\s+-\s+([^;]+)/) {
766 my $ver = $1;
768 next if $ver =~ /private/i;
769 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
770 "${tab}VERDEF=$ver");
773 close PVS;
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.
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;
790 next if $ver =~ /private/i;
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;
801 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
802 $RelPath, "SYMBOL=$sym");
807 close PVS;
811 ## OpenFindElf(file, FileHandleRef, LineNumRef)
813 # Open file in 'find_elf -r' format, and return the value of
814 # the opening PREFIX line.
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
821 # exit:
822 # This routine issues a fatal error and does not return on error.
823 # Otherwise, the value of PREFIX is returned.
825 sub OpenFindElf {
826 my ($file, $fh, $LineNum) = @_;
827 my $line;
828 my $prefix;
830 open($fh, $file) || die "$Prog: Unable to open: $file";
831 $$LineNum = 0;
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;
842 die "$Prog: No PREFIX line seen on line $$LineNum: $file";
845 $prefix;
849 ## ProcFindElf(file)
851 # Open the specified file, which must be produced by "find_elf -r",
852 # and process the files it describes.
854 sub ProcFindElf {
855 my $file = $_[0];
856 my $line;
857 my $LineNum;
859 my $prefix = OpenFindElf($file, \*FIND_ELF, \$LineNum);
861 while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
862 next if !($line =~ /^OBJECT\s/i);
864 my ($item, $class, $type, $verdef, $obj) =
865 split(/\s+/, $line, 5);
867 ProcFile("$prefix/$obj", $obj, $class, $type, $verdef);
870 close FIND_ELF;
874 ## AltObjectConfig(file)
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.
880 # entry:
881 # file - File of ELF objects, in 'find_elf -r' format, to examine.
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.
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.
895 sub 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;
904 my $prefix = OpenFindElf($file, \*FIND_ELF);
906 LINE:
907 while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
908 ITEM: {
910 if ($line =~ /^OBJECT\s/i) {
911 my ($item, $class, $type, $verdef, $obj) =
912 split(/\s+/, $line, 5);
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;
922 last ITEM;
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;
937 # Skip unrecognized item
938 next LINE;
941 next if !$obj_active;
943 my $full = "$prefix/$obj_path";
945 next if defined($EXRE_nocrlealt) &&
946 ($obj_path =~ $EXRE_nocrlealt);
948 my $Dir = $full;
949 $Dir =~ s/^(.*)\/.*$/$1/;
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";
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";
970 print CRLE64 "\t-o $Dir -a /$obj_path \\\n";
974 close FIND_ELF;
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";
983 chmod 0755, $Crle64;
984 close CRLE64;
986 undef $Conf64 if system($Crle64);
988 # Done with the script
989 unlink $Crle64;
991 if ($Crle32) {
992 $Conf32 = "$Tmpdir/$Prog.conf32.$$";
993 print CRLE32 "\t-c $Conf32\n";
995 chmod 0755, $Crle32;
996 close CRLE32;
998 undef $Conf32 if system($Crle32);
1000 # Done with the script
1001 unlink $Crle32;
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";
1015 # -----------------------------------------------------------------------------
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.
1022 delete($ENV{LD_PRELOAD});
1023 delete($ENV{LD_PRELOAD_32});
1024 delete($ENV{LD_PRELOAD_64});
1026 # Establish a program name for any error diagnostics.
1027 chomp($Prog = `basename $0`);
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.
1032 my $moddir = dirname($0);
1033 $moddir = "$moddir/../lib/perl" if ! -f "$moddir/onbld_elfmod.pm";
1034 require "$moddir/onbld_elfmod.pm";
1036 # Determine what machinery is available.
1037 my $Mach = `uname -p`;
1038 my$Isalist = `isalist`;
1039 if ($Mach =~ /sparc/) {
1040 if ($Isalist =~ /sparcv9/) {
1041 $Ena64 = "ok";
1043 } elsif ($Mach =~ /i386/) {
1044 if ($Isalist =~ /amd64/) {
1045 $Ena64 = "ok";
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 = '';
1053 # Check that we have arguments.
1054 if ((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;
1075 die "$Prog: -D and -d options are mutually exclusive\n" if ($opt{D} && $opt{d});
1077 $Tmpdir = "/tmp" if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir));
1079 # If -w, change working directory to given location
1080 !$opt{w} || chdir($opt{w}) || die "$Prog: can't cd to $opt{w}";
1082 # Locate and process the exceptions file
1083 onbld_elfmod::LoadExceptionsToEXRE('check_rtime');
1085 # Is there a proto area available, either via the -d option, or because
1086 # we are part of an activated workspace?
1087 my $Proto;
1088 if ($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;
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);
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.
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.
1107 if ($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|");
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.
1129 if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
1130 $LddNoU = 1;
1131 } else {
1132 my($Release);
1134 if (($Release = $ENV{RELEASE}) && (cmp_os_ver($Release, "<", "5.10"))) {
1135 $LddNoU = 1;
1136 } else {
1137 $LddNoU = 0;
1141 # Set up variables used to handle output files:
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.
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.
1153 if ($opt{E}) {
1154 open(ERROR, ">$opt{E}") || die "$Prog: open failed: $opt{E}";
1155 $ErrFH = \*ERROR;
1156 } else {
1157 $ErrFH = \*STDOUT;
1160 if ($opt{I}) {
1161 open(INFO, ">$opt{I}") || die "$Prog: open failed: $opt{I}";
1162 $InfoFH = \*INFO;
1163 } else {
1164 $InfoFH = \*STDOUT;
1166 my ($err_dev, $err_ino) = stat($ErrFH);
1167 my ($info_dev, $info_ino) = stat($InfoFH);
1168 $ErrTtl = \$OutCnt1;
1169 $InfoTtl = (($err_dev == $info_dev) && ($err_ino == $info_ino)) ?
1170 \$OutCnt1 : \$OutCnt2;
1173 # If we were given a list of objects in 'find_elf -r' format, then
1174 # process it.
1175 ProcFindElf($opt{f}) if $opt{f};
1177 # Process each argument
1178 foreach my $Arg (@ARGV) {
1179 # Run find_elf to find the files given by $Arg and process them
1180 ProcFindElf("find_elf -fr $Arg|");
1183 # Cleanup output files
1184 unlink $Conf64 if $Conf64;
1185 unlink $Conf32 if $Conf32;
1186 close ERROR if $opt{E};
1187 close INFO if $opt{I};
1189 exit 0;