dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / tools / scripts / check_rtime.pl
blobac9a172ebde3784bbf86a4f9794ca914e032cb3d
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.
25 # Copyright 2014 Garrett D'Amore <garrett@damore.org>
29 # Check ELF information.
31 # This script descends a directory hierarchy inspecting ELF dynamic executables
32 # and shared objects. The general theme is to verify that common Makefile rules
33 # have been used to build these objects. Typical failures occur when Makefile
34 # rules are re-invented rather than being inherited from "cmd/lib" Makefiles.
36 # As always, a number of components don't follow the rules, and these are
37 # excluded to reduce this scripts output.
39 # By default any file that has conditions that should be reported is first
40 # listed and then each condition follows. The -o (one-line) option produces a
41 # more terse output which is better for sorting/diffing with "nightly".
43 # NOTE: missing dependencies, symbols or versions are reported by running the
44 # file through ldd(1). As objects within a proto area are built to exist in a
45 # base system, standard use of ldd(1) will bind any objects to dependencies
46 # that exist in the base system. It is frequently the case that newer objects
47 # exist in the proto area that are required to satisfy other objects
48 # dependencies, and without using these newer objects an ldd(1) will produce
49 # misleading error messages. To compensate for this, the -D/-d options, or the
50 # existence of the CODEMSG_WS/ROOT environment variables, cause the creation of
51 # alternative dependency mappings via crle(1) configuration files that establish
52 # any proto shared objects as alternatives to their base system location. Thus
53 # ldd(1) can be executed against these configuration files so that objects in a
54 # proto area bind to their dependencies in the same proto area.
57 # Define all global variables (required for strict)
58 use vars qw($Prog $Env $Ena64 $Tmpdir);
59 use vars qw($LddNoU $Conf32 $Conf64);
60 use vars qw(%opt);
61 use vars qw($ErrFH $ErrTtl $InfoFH $InfoTtl $OutCnt1 $OutCnt2);
63 # An exception file is used to specify regular expressions to match
64 # objects. These directives specify special attributes of the object.
65 # The regular expressions are read from the file and compiled into the
66 # regular expression variables.
68 # The name of each regular expression variable is of the form
70 # $EXRE_xxx
72 # where xxx is the name of the exception in lower case. For example,
73 # the regular expression variable for EXEC_STACK is $EXRE_exec_stack.
75 # onbld_elfmod::LoadExceptionsToEXRE() depends on this naming convention
76 # to initialize the regular expression variables, and to detect invalid
77 # exception names.
79 # If a given exception is not used in the exception file, its regular
80 # expression variable will be undefined. Users of these variables must
81 # test the variable with defined() prior to use:
83 # defined($EXRE_exec_stack) && ($foo =~ $EXRE_exec_stack)
85 # or if the test is to make sure the item is not specified:
87 # !defined($EXRE_exec_stack) || ($foo !~ $EXRE_exec_stack)
89 # ----
91 # The exceptions are:
93 # EXEC_DATA
94 # Objects that are not required to have non-executable writable
95 # data segments.
97 # EXEC_STACK
98 # Objects that are not required to have a non-executable stack
100 # NOCRLEALT
101 # Objects that should be skipped by AltObjectConfig() when building
102 # the crle script that maps objects to the proto area.
104 # NODIRECT
105 # Objects that are not required to use direct bindings
107 # NOSYMSORT
108 # Objects we should not check for duplicate addresses in
109 # the symbol sort sections.
111 # OLDDEP
112 # Objects that are no longer needed because their functionalty
113 # has migrated elsewhere. These are usually pure filters that
114 # point at libc.
116 # SKIP
117 # Files and directories that should be excluded from analysis.
119 # STAB
120 # Objects that are allowed to contain stab debugging sections
122 # TEXTREL
123 # Object for which relocations are allowed to the text segment
125 # UNDEF_REF
126 # Objects that are allowed undefined references
128 # UNREF_OBJ
129 # "unreferenced object=" ldd(1) diagnostics.
131 # UNUSED_DEPS
132 # Objects that are allowed to have unused dependencies
134 # UNUSED_OBJ
135 # Objects that are allowed to be unused dependencies
137 # UNUSED_RPATH
138 # Objects with unused runpaths
141 use vars qw($EXRE_exec_data $EXRE_exec_stack $EXRE_nocrlealt);
142 use vars qw($EXRE_nodirect $EXRE_nosymsort);
143 use vars qw($EXRE_olddep $EXRE_skip $EXRE_stab $EXRE_textrel $EXRE_undef_ref);
144 use vars qw($EXRE_unref_obj $EXRE_unused_deps $EXRE_unused_obj);
145 use vars qw($EXRE_unused_rpath);
147 use strict;
148 use Getopt::Std;
149 use File::Basename;
152 # Reliably compare two OS revisions. Arguments are <ver1> <op> <ver2>.
153 # <op> is the string form of a normal numeric comparison operator.
154 sub cmp_os_ver {
155 my @ver1 = split(/\./, $_[0]);
156 my $op = $_[1];
157 my @ver2 = split(/\./, $_[2]);
159 push @ver2, ("0") x $#ver1 - $#ver2;
160 push @ver1, ("0") x $#ver2 - $#ver1;
162 my $diff = 0;
163 while (@ver1 || @ver2) {
164 if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
165 last;
168 return (eval "$diff $op 0" ? 1 : 0);
171 ## ProcFile(FullPath, RelPath, File, Class, Type, Verdef)
173 # Determine whether this a ELF dynamic object and if so investigate its runtime
174 # attributes.
176 sub ProcFile {
177 my($FullPath, $RelPath, $Class, $Type, $Verdef) = @_;
178 my(@Elf, @Ldd, $Dyn, $Sym, $ExecStack);
179 my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort);
180 my($Val, $Header, $IsX86, $RWX, $UnDep);
181 my($HasDirectBinding);
183 # Only look at executables and sharable objects
184 return if ($Type ne 'EXEC') && ($Type ne 'DYN') && ($Type ne 'PIE');
186 # Ignore symbolic links
187 return if -l $FullPath;
189 # Is this an object or directory hierarchy we don't care about?
190 return if (defined($EXRE_skip) && ($RelPath =~ $EXRE_skip));
192 # Bail if we can't stat the file. Otherwise, note if it is SUID/SGID.
193 return if !stat($FullPath);
194 my $Secure = (-u _ || -g _) ? 1 : 0;
196 # Reset output message counts for new input file
197 $$ErrTtl = $$InfoTtl = 0;
199 @Ldd = 0;
201 # Determine whether we have access to inspect the file.
202 if (!(-r $FullPath)) {
203 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
204 "unable to inspect file: permission denied");
205 return;
208 # Determine whether we have a executable (static or dynamic) or a
209 # shared object.
210 @Elf = split(/\n/, `elfdump -epdcy $FullPath 2>&1`);
212 $Dyn = $ExecStack = $IsX86 = $RWX = 0;
213 $Header = 'None';
214 foreach my $Line (@Elf) {
215 # If we have an invalid file type (which we can tell from the
216 # first line), or we're processing an archive, bail.
217 if ($Header eq 'None') {
218 if (($Line =~ /invalid file/) ||
219 ($Line =~ /\Q$FullPath\E(.*):/)) {
220 return;
224 if ($Line =~ /^ELF Header/) {
225 $Header = 'Ehdr';
226 next;
229 if ($Line =~ /^Program Header/) {
230 $Header = 'Phdr';
231 $RWX = 0;
232 next;
235 if ($Line =~ /^Dynamic Section/) {
236 # A dynamic section indicates we're a dynamic object
237 # (this makes sure we don't check static executables).
238 $Dyn = 1;
239 next;
242 if (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
243 # If it's a X86 object, we need to enforce RW- data.
244 $IsX86 = 1 if $Line =~ /(EM_AMD64|EM_386)/;
245 next;
248 if (($Header eq 'Phdr') &&
249 ($Line =~ /\[ PF_X\s+PF_W\s+PF_R \]/)) {
250 # RWX segment seen.
251 $RWX = 1;
252 next;
255 if (($Header eq 'Phdr') &&
256 ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
257 # Seen an RWX PT_LOAD segment.
258 if (!defined($EXRE_exec_data) ||
259 ($RelPath !~ $EXRE_exec_data)) {
260 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
261 "application requires non-executable " .
262 "data\t<remove -Mmapfile_execdata?>");
264 next;
267 if (($Header eq 'Phdr') && $RWX == 1 &&
268 ($Line =~ /\[ PT_SUNWSTACK \]/)) {
269 # This object defines an executable stack.
270 $ExecStack = 1;
271 next;
275 # Applications should not contain an executable stack definition.
276 if (($Type eq 'EXEC') && ($ExecStack == 1) &&
277 (!defined($EXRE_exec_stack) || ($RelPath !~ $EXRE_exec_stack))) {
278 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
279 "non-executable stack required\t<remove -Mmapfile_execstack?>");
282 # Having caught any static executables in the mcs(1) check and non-
283 # executable stack definition check, continue with dynamic objects
284 # from now on.
285 if ($Dyn eq 0) {
286 return;
289 # Use ldd unless its a 64-bit object and we lack the hardware.
290 if (($Class == 32) || $Ena64) {
291 my $LDDFullPath = $FullPath;
293 if ($Secure) {
294 # The execution of a secure application over an nfs file
295 # system mounted nosuid will result in warning messages
296 # being sent to /var/adm/messages. As this type of
297 # environment can occur with root builds, move the file
298 # being investigated to a safe place first. In addition
299 # remove its secure permission so that it can be
300 # influenced by any alternative dependency mappings.
302 my $File = $RelPath;
303 $File =~ s!^.*/!!; # basename
305 my($TmpPath) = "$Tmpdir/$File";
307 system('cp', $LDDFullPath, $TmpPath);
308 chmod 0777, $TmpPath;
309 $LDDFullPath = $TmpPath;
312 # Use ldd(1) to determine the objects relocatability and use.
313 # By default look for all unreferenced dependencies. However,
314 # some objects have legitimate dependencies that they do not
315 # reference.
316 if ($LddNoU) {
317 $Lddopt = "-ru";
318 } else {
319 $Lddopt = "-rU";
321 @Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
322 if ($Secure) {
323 unlink $LDDFullPath;
327 $Val = 0;
328 $Sym = 5;
329 $UnDep = 1;
331 foreach my $Line (@Ldd) {
333 if ($Val == 0) {
334 $Val = 1;
335 # Make sure ldd(1) worked. One possible failure is that
336 # this is an old ldd(1) prior to -e addition (4390308).
337 if ($Line =~ /usage:/) {
338 $Line =~ s/$/\t<old ldd(1)?>/;
339 onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
340 $RelPath, $Line);
341 last;
342 } elsif ($Line =~ /execution failed/) {
343 onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
344 $RelPath, $Line);
345 last;
348 # It's possible this binary can't be executed, ie. we've
349 # found a sparc binary while running on an intel system,
350 # or a sparcv9 binary on a sparcv7/8 system.
351 if ($Line =~ /wrong class/) {
352 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
353 "has wrong class or data encoding");
354 next;
357 # Historically, ldd(1) likes executable objects to have
358 # their execute bit set.
359 if ($Line =~ /not executable/) {
360 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
361 "is not executable");
362 next;
366 # Look for "file" or "versions" that aren't found. Note that
367 # these lines will occur before we find any symbol referencing
368 # errors.
369 if (($Sym == 5) && ($Line =~ /not found\)/)) {
370 if ($Line =~ /file not found\)/) {
371 $Line =~ s/$/\t<no -zdefs?>/;
373 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
374 next;
376 # Look for relocations whose symbols can't be found. Note, we
377 # only print out the first 5 relocations for any file as this
378 # output can be excessive.
379 if ($Sym && ($Line =~ /symbol not found/)) {
380 # Determine if this file is allowed undefined
381 # references.
382 if (($Sym == 5) && defined($EXRE_undef_ref) &&
383 ($RelPath =~ $EXRE_undef_ref)) {
384 $Sym = 0;
385 next;
387 if ($Sym-- == 1) {
388 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
389 "continued ...") if !$opt{o};
390 next;
392 # Just print the symbol name.
393 $Line =~ s/$/\t<no -zdefs?>/;
394 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
395 next;
397 # Look for any unused search paths.
398 if ($Line =~ /unused search path=/) {
399 next if defined($EXRE_unused_rpath) &&
400 ($Line =~ $EXRE_unused_rpath);
402 if ($Secure) {
403 $Line =~ s!$Tmpdir/!!;
405 $Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
406 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
407 next;
409 # Look for unreferenced dependencies. Note, if any unreferenced
410 # objects are ignored, then set $UnDep so as to suppress any
411 # associated unused-object messages.
412 if ($Line =~ /unreferenced object=/) {
413 if (defined($EXRE_unref_obj) &&
414 ($Line =~ $EXRE_unref_obj)) {
415 $UnDep = 0;
416 next;
418 if ($Secure) {
419 $Line =~ s!$Tmpdir/!!;
421 $Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
422 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
423 next;
425 # Look for any unused dependencies.
426 if ($UnDep && ($Line =~ /unused/)) {
427 # Skip if object is allowed to have unused dependencies
428 next if defined($EXRE_unused_deps) &&
429 ($RelPath =~ $EXRE_unused_deps);
431 # Skip if dependency is always allowed to be unused
432 next if defined($EXRE_unused_obj) &&
433 ($Line =~ $EXRE_unused_obj);
435 $Line =~ s!$Tmpdir/!! if $Secure;
436 $Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
437 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
438 next;
442 # Reuse the elfdump(1) data to investigate additional dynamic linking
443 # information.
445 $Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
446 $Tex = $Strip = 1;
447 $HasDirectBinding = 0;
449 $Header = 'None';
450 ELF: foreach my $Line (@Elf) {
451 # We're only interested in the section headers and the dynamic
452 # section.
453 if ($Line =~ /^Section Header/) {
454 $Header = 'Shdr';
456 if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
457 # This object has a combined relocation section.
458 $Sun = 1;
460 } elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
461 # This object contain .stabs sections
462 $Stab = 1;
463 } elsif (($SymSort == 0) &&
464 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) {
465 # This object contains a symbol sort section
466 $SymSort = 1;
469 if (($Strip == 1) && ($Line =~ /\.symtab/)) {
470 # This object contains a complete symbol table.
471 $Strip = 0;
473 next;
475 } elsif ($Line =~ /^Dynamic Section/) {
476 $Header = 'Dyn';
477 next;
478 } elsif ($Line =~ /^Syminfo Section/) {
479 $Header = 'Syminfo';
480 next;
481 } elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) {
482 next;
485 # Look into the Syminfo section.
486 # Does this object have at least one Directly Bound symbol?
487 if (($Header eq 'Syminfo')) {
488 my(@Symword);
490 if ($HasDirectBinding == 1) {
491 next;
494 @Symword = split(' ', $Line);
496 if (!defined($Symword[1])) {
497 next;
499 if ($Symword[1] =~ /B/) {
500 $HasDirectBinding = 1;
502 next;
505 # Does this object contain text relocations.
506 if ($Tex && ($Line =~ /TEXTREL/)) {
507 # Determine if this file is allowed text relocations.
508 if (defined($EXRE_textrel) &&
509 ($RelPath =~ $EXRE_textrel)) {
510 $Tex = 0;
511 next ELF;
513 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
514 "TEXTREL .dynamic tag\t\t\t<no -Kpic?>");
515 $Tex = 0;
516 next;
519 # Does this file have any relocation sections (there are a few
520 # psr libraries with no relocations at all, thus a .SUNW_reloc
521 # section won't exist either).
522 if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
523 $Relsz = hex((split(' ', $Line))[2]);
524 next;
527 # Does this file have any plt relocations. If the plt size is
528 # equivalent to the total relocation size then we don't have
529 # any relocations suitable for combining into a .SUNW_reloc
530 # section.
531 if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
532 $Pltsz = hex((split(' ', $Line))[2]);
533 next;
536 # Does this object have any dependencies.
537 if ($Line =~ /NEEDED/) {
538 my($Need) = (split(' ', $Line))[3];
540 if (defined($EXRE_olddep) && ($Need =~ $EXRE_olddep)) {
541 # Catch any old (unnecessary) dependencies.
542 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
543 "NEEDED=$Need\t<dependency no longer necessary>");
544 } elsif ($opt{i}) {
545 # Under the -i (information) option print out
546 # any useful dynamic entries.
547 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
548 "NEEDED=$Need");
550 next;
553 # Is this object built with -B direct flag on?
554 if ($Line =~ / DIRECT /) {
555 $HasDirectBinding = 1;
558 # Does this object specify a runpath.
559 if ($opt{i} && ($Line =~ /RPATH/)) {
560 my($Rpath) = (split(' ', $Line))[3];
561 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
562 $RelPath, "RPATH=$Rpath");
563 next;
567 # A shared object, that contains non-plt relocations, should have a
568 # combined relocation section indicating it was built with -z combreloc.
569 if ((($Type eq 'DYN') || ($Type eq 'PIE')) &&
570 $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
571 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
572 ".SUNW_reloc section missing\t\t<no -zcombreloc?>");
575 # No objects released to a customer should have any .stabs sections
576 # remaining, they should be stripped.
577 if ($opt{s} && $Stab) {
578 goto DONESTAB if defined($EXRE_stab) && ($RelPath =~ $EXRE_stab);
580 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
581 "debugging sections should be deleted\t<no strip -x?>");
584 # Identify an object that is not built with either -B direct or
585 # -z direct.
586 goto DONESTAB
587 if (defined($EXRE_nodirect) && ($RelPath =~ $EXRE_nodirect));
589 if ($Relsz && ($HasDirectBinding == 0)) {
590 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
591 "object has no direct bindings\t<no -B direct or -z direct?>");
594 DONESTAB:
596 # All objects should have a full symbol table to provide complete
597 # debugging stack traces.
598 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
599 "symbol table should not be stripped\t<remove -s?>") if $Strip;
601 # If there are symbol sort sections in this object, report on
602 # any that have duplicate addresses.
603 ProcSymSort($FullPath, $RelPath) if $SymSort;
605 # If -v was specified, and the object has a version definition
606 # section, generate output showing each public symbol and the
607 # version it belongs to.
608 ProcVerdef($FullPath, $RelPath)
609 if ($Verdef eq 'VERDEF') && $opt{v};
613 ## ProcSymSortOutMsg(RelPath, secname, addr, names...)
615 # Call onbld_elfmod::OutMsg for a duplicate address error in a symbol sort
616 # section
618 sub ProcSymSortOutMsg {
619 my($RelPath, $secname, $addr, @names) = @_;
621 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
622 "$secname: duplicate $addr: ". join(', ', @names));
626 ## ProcSymSort(FullPath, RelPath)
628 # Examine the symbol sort sections for the given object and report
629 # on any duplicate addresses found. Ideally, mapfile directives
630 # should be used when building objects that have multiple symbols
631 # with the same address so that only one of them appears in the sort
632 # section. This saves space, reduces user confusion, and ensures that
633 # libproc and debuggers always display public names instead of symbols
634 # that are merely implementation details.
636 sub ProcSymSort {
638 my($FullPath, $RelPath) = @_;
640 # If this object is exempt from checking, return quietly
641 return if defined($EXRE_nosymsort) && ($FullPath =~ $EXRE_nosymsort);
644 open(SORT, "elfdump -S $FullPath|") ||
645 die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
647 my $line;
648 my $last_addr;
649 my @dups = ();
650 my $secname;
651 while ($line = <SORT>) {
652 chomp $line;
654 next if ($line eq '');
656 # If this is a header line, pick up the section name
657 if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
658 $secname = $1;
660 # Every new section is followed by a column header line
661 $line = <SORT>; # Toss header line
663 # Flush anything left from previous section
664 ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
665 if (scalar(@dups) > 1);
667 # Reset variables for new sort section
668 $last_addr = '';
669 @dups = ();
671 next;
674 # Process symbol line
675 my @fields = split /\s+/, $line;
676 my $new_addr = $fields[2];
677 my $new_type = $fields[8];
678 my $new_name = $fields[9];
680 if ($new_type eq 'UNDEF') {
681 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
682 "$secname: unexpected UNDEF symbol " .
683 "(link-editor error): $new_name");
684 next;
687 if ($new_addr eq $last_addr) {
688 push @dups, $new_name;
689 } else {
690 ProcSymSortOutMsg($RelPath, $secname,
691 $last_addr, @dups) if (scalar(@dups) > 1);
692 @dups = ( $new_name );
693 $last_addr = $new_addr;
697 ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
698 if (scalar(@dups) > 1);
700 close SORT;
704 ## ProcVerdef(FullPath, RelPath)
706 # Examine the version definition section for the given object and report
707 # each public symbol along with the version it belongs to.
709 sub ProcVerdef {
711 my($FullPath, $RelPath) = @_;
712 my $line;
713 my $cur_ver = '';
714 my $tab = $opt{o} ? '' : "\t";
716 # pvs -dov provides information about the versioning hierarchy
717 # in the file. Lines are of the format:
718 # path - version[XXX];
719 # where [XXX] indicates optional information, such as flags
720 # or inherited versions.
722 # Private versions are allowed to change freely, so ignore them.
723 open(PVS, "pvs -dov $FullPath|") ||
724 die "$Prog: Unable to execute pvs (version definition section)\n";
726 while ($line = <PVS>) {
727 chomp $line;
729 if ($line =~ /^[^\s]+\s+-\s+([^;]+)/) {
730 my $ver = $1;
732 next if $ver =~ /private/i;
733 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
734 "${tab}VERDEF=$ver");
737 close PVS;
739 # pvs -dos lists the symbols assigned to each version definition.
740 # Lines are of the format:
741 # path - version: symbol;
742 # path - version: symbol (size);
743 # where the (size) is added to data items, but not for functions.
744 # We strip off the size, if present.
746 open(PVS, "pvs -dos $FullPath|") ||
747 die "$Prog: Unable to execute pvs (version definition section)\n";
748 while ($line = <PVS>) {
749 chomp $line;
750 if ($line =~ /^[^\s]+\s+-\s+([^:]+):\s*([^\s;]+)/) {
751 my $ver = $1;
752 my $sym = $2;
754 next if $ver =~ /private/i;
756 if ($opt{o}) {
757 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
758 "VERSION=$ver, SYMBOL=$sym");
759 } else {
760 if ($cur_ver ne $ver) {
761 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
762 $RelPath, "VERSION=$ver");
763 $cur_ver = $ver;
765 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
766 $RelPath, "SYMBOL=$sym");
771 close PVS;
775 ## OpenFindElf(file, FileHandleRef, LineNumRef)
777 # Open file in 'find_elf -r' format, and return the value of
778 # the opening PREFIX line.
780 # entry:
781 # file - file, or find_elf child process, to open
782 # FileHandleRef - Reference to file handle to open
783 # LineNumRef - Reference to integer to increment as lines are input
785 # exit:
786 # This routine issues a fatal error and does not return on error.
787 # Otherwise, the value of PREFIX is returned.
789 sub OpenFindElf {
790 my ($file, $fh, $LineNum) = @_;
791 my $line;
792 my $prefix;
794 open($fh, $file) || die "$Prog: Unable to open: $file";
795 $$LineNum = 0;
797 # This script requires relative paths as created by 'find_elf -r'.
798 # When this is done, the first non-comment line will always
799 # be PREFIX. Obtain that line, or issue a fatal error.
800 while ($line = onbld_elfmod::GetLine($fh, $LineNum)) {
801 if ($line =~ /^PREFIX\s+(.*)$/i) {
802 $prefix = $1;
803 last;
806 die "$Prog: No PREFIX line seen on line $$LineNum: $file";
809 $prefix;
813 ## ProcFindElf(file)
815 # Open the specified file, which must be produced by "find_elf -r",
816 # and process the files it describes.
818 sub ProcFindElf {
819 my $file = $_[0];
820 my $line;
821 my $LineNum;
823 my $prefix = OpenFindElf($file, \*FIND_ELF, \$LineNum);
825 while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
826 next if !($line =~ /^OBJECT\s/i);
828 my ($item, $class, $type, $verdef, $obj) =
829 split(/\s+/, $line, 5);
831 ProcFile("$prefix/$obj", $obj, $class, $type, $verdef);
834 close FIND_ELF;
838 ## AltObjectConfig(file)
840 # Recurse through a directory hierarchy looking for appropriate dependencies
841 # to map from their standard system locations to the proto area via a crle
842 # config file.
844 # entry:
845 # file - File of ELF objects, in 'find_elf -r' format, to examine.
847 # exit:
848 # Scripts are generated for the 32 and 64-bit cases to run crle
849 # and create runtime configuration files that will establish
850 # alternative dependency mappings for the objects identified.
852 # $Env - Set to environment variable definitions that will cause
853 # the config files generated by this routine to be used
854 # by ldd.
855 # $Conf32, $Conf64 - Undefined, or set to the config files generated
856 # by this routine. If defined, the caller is responsible for
857 # unlinking the files before exiting.
859 sub AltObjectConfig {
860 my $file = $_[0];
861 my ($Crle32, $Crle64);
862 my $line;
863 my $LineNum;
864 my $obj_path;
865 my $obj_active = 0;
866 my $obj_class;
868 my $prefix = OpenFindElf($file, \*FIND_ELF);
870 LINE:
871 while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
872 ITEM: {
874 if ($line =~ /^OBJECT\s/i) {
875 my ($item, $class, $type, $verdef, $obj) =
876 split(/\s+/, $line, 5);
878 if ($type eq 'DYN') {
879 $obj_active = 1;
880 $obj_path = $obj;
881 $obj_class = $class;
882 } else {
883 # Only want shared libraries
884 $obj_active = 0;
886 last ITEM;
889 # We need to follow links to sharable objects so
890 # that any dependencies are expressed in all their
891 # available forms. We depend on ALIAS lines directly
892 # following the object they alias, so if we have
893 # a current object, this alias belongs to it.
894 if ($obj_active && ($line =~ /^ALIAS\s/i)) {
895 my ($item, $real_obj, $obj) =
896 split(/\s+/, $line, 3);
897 $obj_path = $obj;
898 last ITEM;
901 # Skip unrecognized item
902 next LINE;
905 next if !$obj_active;
907 my $full = "$prefix/$obj_path";
909 next if defined($EXRE_nocrlealt) &&
910 ($obj_path =~ $EXRE_nocrlealt);
912 my $Dir = $full;
913 $Dir =~ s/^(.*)\/.*$/$1/;
915 # Create a crle(1) script for the dependency we've found.
916 # We build separate scripts for the 32 and 64-bit cases.
917 # We create and initialize each script when we encounter
918 # the first object that needs it.
919 if ($obj_class == 32) {
920 if (!$Crle32) {
921 $Crle32 = "$Tmpdir/$Prog.crle32.$$";
922 open(CRLE32, "> $Crle32") ||
923 die "$Prog: open failed: $Crle32: $!";
924 print CRLE32 "#!/bin/sh\ncrle \\\n";
926 print CRLE32 "\t-o $Dir -a /$obj_path \\\n";
927 } elsif ($Ena64) {
928 if (!$Crle64) {
929 $Crle64 = "$Tmpdir/$Prog.crle64.$$";
930 open(CRLE64, "> $Crle64") ||
931 die "$Prog: open failed: $Crle64: $!";
932 print CRLE64 "#!/bin/sh\ncrle -64\\\n";
934 print CRLE64 "\t-o $Dir -a /$obj_path \\\n";
938 close FIND_ELF;
941 # Now that the config scripts are complete, use them to generate
942 # runtime linker config files.
943 if ($Crle64) {
944 $Conf64 = "$Tmpdir/$Prog.conf64.$$";
945 print CRLE64 "\t-c $Conf64\n";
947 chmod 0755, $Crle64;
948 close CRLE64;
950 undef $Conf64 if system($Crle64);
952 # Done with the script
953 unlink $Crle64;
955 if ($Crle32) {
956 $Conf32 = "$Tmpdir/$Prog.conf32.$$";
957 print CRLE32 "\t-c $Conf32\n";
959 chmod 0755, $Crle32;
960 close CRLE32;
962 undef $Conf32 if system($Crle32);
964 # Done with the script
965 unlink $Crle32;
968 # Set $Env so that we will use the config files generated above
969 # when we run ldd.
970 if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
971 $Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
972 } elsif ($Crle64 && $Conf64) {
973 $Env = "-e LD_FLAGS=config_64=$Conf64";
974 } elsif ($Crle32 && $Conf32) {
975 $Env = "-e LD_FLAGS=config_32=$Conf32";
979 # -----------------------------------------------------------------------------
981 # This script relies on ldd returning output reflecting only the binary
982 # contents. But if LD_PRELOAD* environment variables are present, libraries
983 # named by them will also appear in the output, disrupting our analysis.
984 # So, before we get too far, scrub the environment.
986 delete($ENV{LD_PRELOAD});
987 delete($ENV{LD_PRELOAD_32});
988 delete($ENV{LD_PRELOAD_64});
990 # Establish a program name for any error diagnostics.
991 chomp($Prog = `basename $0`);
993 # The onbld_elfmod package is maintained in the same directory as this
994 # script, and is installed in ../lib/perl. Use the local one if present,
995 # and the installed one otherwise.
996 my $moddir = dirname($0);
997 $moddir = "$moddir/../lib/perl" if ! -f "$moddir/onbld_elfmod.pm";
998 require "$moddir/onbld_elfmod.pm";
1000 # Determine what machinery is available.
1001 my $Mach = `uname -p`;
1002 my$Isalist = `isalist`;
1003 if ($Mach =~ /sparc/) {
1004 if ($Isalist =~ /sparcv9/) {
1005 $Ena64 = "ok";
1007 } elsif ($Mach =~ /i386/) {
1008 if ($Isalist =~ /amd64/) {
1009 $Ena64 = "ok";
1013 # $Env is used with all calls to ldd. It is set by AltObjectConfig to
1014 # cause an alternate object mapping runtime config file to be used.
1015 $Env = '';
1017 # Check that we have arguments.
1018 if ((getopts('D:d:E:e:f:I:imosvw:', \%opt) == 0) ||
1019 (!$opt{f} && ($#ARGV == -1))) {
1020 print "usage: $Prog [-imosv] [-D depfile | -d depdir] [-E errfile]\n";
1021 print "\t\t[-e exfile] [-f listfile] [-I infofile] [-w outdir]\n";
1022 print "\t\t[file | dir]...\n";
1023 print "\n";
1024 print "\t[-D depfile]\testablish dependencies from 'find_elf -r' file list\n";
1025 print "\t[-d depdir]\testablish dependencies from under directory\n";
1026 print "\t[-E errfile]\tdirect error output to file\n";
1027 print "\t[-e exfile]\texceptions file\n";
1028 print "\t[-f listfile]\tuse file list produced by find_elf -r\n";
1029 print "\t[-I infofile]\tdirect informational output (-i, -v) to file\n";
1030 print "\t[-i]\t\tproduce dynamic table entry information\n";
1031 print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
1032 print "\t[-s]\t\tprocess .stab and .symtab entries\n";
1033 print "\t[-v]\t\tprocess version definition entries\n";
1034 print "\t[-w outdir]\tinterpret all files relative to given directory\n";
1035 exit 1;
1038 die "$Prog: -D and -d options are mutually exclusive\n" if ($opt{D} && $opt{d});
1040 $Tmpdir = "/tmp" if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir));
1042 # If -w, change working directory to given location
1043 !$opt{w} || chdir($opt{w}) || die "$Prog: can't cd to $opt{w}";
1045 # Locate and process the exceptions file
1046 onbld_elfmod::LoadExceptionsToEXRE('check_rtime');
1048 # Is there a proto area available, either via the -d option, or because
1049 # we are part of an activated workspace?
1050 my $Proto;
1051 if ($opt{d}) {
1052 # User specified dependency directory - make sure it exists.
1053 -d $opt{d} || die "$Prog: $opt{d} is not a directory\n";
1054 $Proto = $opt{d};
1055 } elsif ($ENV{SRCTOP}) {
1056 my $Root;
1058 # Without a user specified dependency directory see if we're
1059 # part of a codemanager workspace and if a proto area exists.
1060 $Proto = $Root if ($Root = $ENV{ROOT}) && (-d $Root);
1063 # If we are basing this analysis off the sharable objects found in
1064 # a proto area, then gather dependencies and construct an alternative
1065 # dependency mapping via a crle(1) configuration file.
1067 # To support alternative dependency mapping we'll need ldd(1)'s
1068 # -e option. This is relatively new (s81_30), so make sure
1069 # ldd(1) is capable before gathering any dependency information.
1070 if ($opt{D} || $Proto) {
1071 if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
1072 print "ldd: does not support -e, unable to ";
1073 print "create alternative dependency mappingings.\n";
1074 print "ldd: option added under 4390308 (s81_30).\n\n";
1075 } else {
1076 # If -D was specified, it supplies a list of files in
1077 # 'find_elf -r' format, and can use it directly. Otherwise,
1078 # we will run find_elf as a child process to find the
1079 # sharable objects found under $Proto.
1080 AltObjectConfig($opt{D} ? $opt{D} : "find_elf -frs $Proto|");
1084 # To support unreferenced dependency detection we'll need ldd(1)'s -U
1085 # option. This is relatively new (4638070), and if not available we
1086 # can still fall back to -u. Even with this option, don't use -U with
1087 # releases prior to 5.10 as the cleanup for -U use only got integrated
1088 # into 5.10 under 4642023. Note, that nightly doesn't typically set a
1089 # RELEASE from the standard <env> files. Users who wish to disable use
1090 # of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
1091 # if using nightly, or otherwise establish it in their environment.
1092 if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
1093 $LddNoU = 1;
1094 } else {
1095 my($Release);
1097 if (($Release = $ENV{RELEASE}) && (cmp_os_ver($Release, "<", "5.10"))) {
1098 $LddNoU = 1;
1099 } else {
1100 $LddNoU = 0;
1104 # Set up variables used to handle output files:
1106 # Error messages go to stdout unless -E is specified. $ErrFH is a
1107 # file handle reference that points at the file handle where error messages
1108 # are sent, and $ErrTtl is a reference that points at an integer used
1109 # to count how many lines have been sent there.
1111 # Informational messages go to stdout unless -I is specified. $InfoFH is a
1112 # file handle reference that points at the file handle where info messages
1113 # are sent, and $InfoTtl is a reference that points at an integer used
1114 # to count how many lines have been sent there.
1116 if ($opt{E}) {
1117 open(ERROR, ">$opt{E}") || die "$Prog: open failed: $opt{E}";
1118 $ErrFH = \*ERROR;
1119 } else {
1120 $ErrFH = \*STDOUT;
1123 if ($opt{I}) {
1124 open(INFO, ">$opt{I}") || die "$Prog: open failed: $opt{I}";
1125 $InfoFH = \*INFO;
1126 } else {
1127 $InfoFH = \*STDOUT;
1129 my ($err_dev, $err_ino) = stat($ErrFH);
1130 my ($info_dev, $info_ino) = stat($InfoFH);
1131 $ErrTtl = \$OutCnt1;
1132 $InfoTtl = (($err_dev == $info_dev) && ($err_ino == $info_ino)) ?
1133 \$OutCnt1 : \$OutCnt2;
1136 # If we were given a list of objects in 'find_elf -r' format, then
1137 # process it.
1138 ProcFindElf($opt{f}) if $opt{f};
1140 # Process each argument
1141 foreach my $Arg (@ARGV) {
1142 # Run find_elf to find the files given by $Arg and process them
1143 ProcFindElf("find_elf -fr $Arg|");
1146 # Cleanup output files
1147 unlink $Conf64 if $Conf64;
1148 unlink $Conf32 if $Conf32;
1149 close ERROR if $opt{E};
1150 close INFO if $opt{I};
1152 exit 0;