1 #!/usr/perl5/bin/perl -w
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]
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);
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
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
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)
94 # Objects that are not required to have non-executable writable
98 # Objects that are not required to have a non-executable stack
101 # Objects allowed to link to 'forbidden' objects
104 # Objects to which nobody not excepted with FORBIDDEN_DEP may link
107 # Objects that should be skipped by AltObjectConfig() when building
108 # the crle script that maps objects to the proto area.
111 # Objects that are not required to use direct bindings
114 # Objects we should not check for duplicate addresses in
115 # the symbol sort sections.
118 # Objects that are no longer needed because their functionalty
119 # has migrated elsewhere. These are usually pure filters that
123 # Files and directories that should be excluded from analysis.
126 # Objects that are allowed to contain stab debugging sections
129 # Object for which relocations are allowed to the text segment
132 # Objects that are allowed undefined references
135 # "unreferenced object=" ldd(1) diagnostics.
138 # Objects that are allowed to have unused dependencies
141 # Objects that are allowed to be unused dependencies
144 # Objects with unused runpaths
147 use vars qw($EXRE_exec_data $EXRE_exec_stack $EXRE_nocrlealt);
148 use vars qw($EXRE_nodirect $EXRE_nosymsort $EXRE_forbidden_dep $EXRE_forbidden);
149 use vars qw($EXRE_olddep $EXRE_skip $EXRE_stab $EXRE_textrel $EXRE_undef_ref);
150 use vars qw($EXRE_unref_obj $EXRE_unused_deps $EXRE_unused_obj);
151 use vars qw($EXRE_unused_rpath);
158 # Reliably compare two OS revisions. Arguments are <ver1> <op> <ver2>.
159 # <op> is the string form of a normal numeric comparison operator.
161 my @ver1 = split(/\./, $_[0]);
163 my @ver2 = split(/\./, $_[2]);
165 push @ver2, ("0") x $#ver1 - $#ver2;
166 push @ver1, ("0") x $#ver2 - $#ver1;
169 while (@ver1 || @ver2) {
170 if (($diff = shift(@ver1) - shift(@ver2)) != 0) {
174 return (eval "$diff $op 0" ? 1 : 0);
177 ## ProcFile(FullPath, RelPath, File, Class, Type, Verdef)
179 # Determine whether this a ELF dynamic object and if so investigate its runtime
183 my($FullPath, $RelPath, $Class, $Type, $Verdef) = @_;
184 my(@Elf, @Ldd, $Dyn, $Sym, $ExecStack);
185 my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort);
186 my($Val, $Header, $IsX86, $RWX, $UnDep);
187 my($HasDirectBinding);
189 # Only look at executables and sharable objects
190 return if ($Type ne 'EXEC') && ($Type ne 'DYN') && ($Type ne 'PIE');
192 # Ignore symbolic links
193 return if -l $FullPath;
195 # Is this an object or directory hierarchy we don't care about?
196 return if (defined($EXRE_skip) && ($RelPath =~ $EXRE_skip));
198 # Bail if we can't stat the file. Otherwise, note if it is SUID/SGID.
199 return if !stat($FullPath);
200 my $Secure = (-u _ || -g _) ? 1 : 0;
202 # Reset output message counts for new input file
203 $$ErrTtl = $$InfoTtl = 0;
207 # Determine whether we have access to inspect the file.
208 if (!(-r $FullPath)) {
209 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
210 "unable to inspect file: permission denied");
214 # Determine whether we have a executable (static or dynamic) or a
216 @Elf = split(/\n/, `elfdump -epdcy $FullPath 2>&1`);
218 $Dyn = $ExecStack = $IsX86 = $RWX = 0;
220 foreach my $Line (@Elf) {
221 # If we have an invalid file type (which we can tell from the
222 # first line), or we're processing an archive, bail.
223 if ($Header eq 'None') {
224 if (($Line =~ /invalid file/) ||
225 ($Line =~ /\Q$FullPath\E(.*):/)) {
230 if ($Line =~ /^ELF Header/) {
235 if ($Line =~ /^Program Header/) {
241 if ($Line =~ /^Dynamic Section/) {
242 # A dynamic section indicates we're a dynamic object
243 # (this makes sure we don't check static executables).
248 if (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) {
249 # If it's a X86 object, we need to enforce RW- data.
250 $IsX86 = 1 if $Line =~ /(EM_AMD64|EM_386)/;
254 if (($Header eq 'Phdr') &&
255 ($Line =~ /\[ PF_X\s+PF_W\s+PF_R \]/)) {
261 if (($Header eq 'Phdr') &&
262 ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) {
263 # Seen an RWX PT_LOAD segment.
264 if (!defined($EXRE_exec_data) ||
265 ($RelPath !~ $EXRE_exec_data)) {
266 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
267 "application requires non-executable " .
268 "data\t<remove -Mmapfile_execdata?>");
273 if (($Header eq 'Phdr') && $RWX == 1 &&
274 ($Line =~ /\[ PT_SUNWSTACK \]/)) {
275 # This object defines an executable stack.
281 # Applications should not contain an executable stack definition.
282 if (($Type eq 'EXEC') && ($ExecStack == 1) &&
283 (!defined($EXRE_exec_stack) || ($RelPath !~ $EXRE_exec_stack))) {
284 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
285 "non-executable stack required\t<remove -Mmapfile_execstack?>");
288 # Having caught any static executables in the mcs(1) check and non-
289 # executable stack definition check, continue with dynamic objects
295 # Use ldd unless its a 64-bit object and we lack the hardware.
296 if (($Class == 32) || $Ena64) {
297 my $LDDFullPath = $FullPath;
300 # The execution of a secure application over an nfs file
301 # system mounted nosuid will result in warning messages
302 # being sent to /var/adm/messages. As this type of
303 # environment can occur with root builds, move the file
304 # being investigated to a safe place first. In addition
305 # remove its secure permission so that it can be
306 # influenced by any alternative dependency mappings.
309 $File =~ s!^.*/!!; # basename
311 my($TmpPath) = "$Tmpdir/$File";
313 system('cp', $LDDFullPath, $TmpPath);
314 chmod 0777, $TmpPath;
315 $LDDFullPath = $TmpPath;
318 # Use ldd(1) to determine the objects relocatability and use.
319 # By default look for all unreferenced dependencies. However,
320 # some objects have legitimate dependencies that they do not
327 @Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`);
337 foreach my $Line (@Ldd) {
341 # Make sure ldd(1) worked. One possible failure is that
342 # this is an old ldd(1) prior to -e addition (4390308).
343 if ($Line =~ /usage:/) {
344 $Line =~ s/$/\t<old ldd(1)?>/;
345 onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
348 } elsif ($Line =~ /execution failed/) {
349 onbld_elfmod::OutMsg($ErrFH, $ErrTtl,
354 # It's possible this binary can't be executed, ie. we've
355 # found a sparc binary while running on an intel system,
356 # or a sparcv9 binary on a sparcv7/8 system.
357 if ($Line =~ /wrong class/) {
358 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
359 "has wrong class or data encoding");
363 # Historically, ldd(1) likes executable objects to have
364 # their execute bit set.
365 if ($Line =~ /not executable/) {
366 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
367 "is not executable");
372 # Look for "file" or "versions" that aren't found. Note that
373 # these lines will occur before we find any symbol referencing
375 if (($Sym == 5) && ($Line =~ /not found\)/)) {
376 if ($Line =~ /file not found\)/) {
377 $Line =~ s/$/\t<no -zdefs?>/;
379 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
382 # Look for relocations whose symbols can't be found. Note, we
383 # only print out the first 5 relocations for any file as this
384 # output can be excessive.
385 if ($Sym && ($Line =~ /symbol not found/)) {
386 # Determine if this file is allowed undefined
388 if (($Sym == 5) && defined($EXRE_undef_ref) &&
389 ($RelPath =~ $EXRE_undef_ref)) {
394 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
395 "continued ...") if !$opt{o};
398 # Just print the symbol name.
399 $Line =~ s/$/\t<no -zdefs?>/;
400 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
403 # Look for any unused search paths.
404 if ($Line =~ /unused search path=/) {
405 next if defined($EXRE_unused_rpath) &&
406 ($Line =~ $EXRE_unused_rpath);
409 $Line =~ s!$Tmpdir/!!;
411 $Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/;
412 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
416 # Look for unreferenced dependencies. Note, if any unreferenced
417 # objects are ignored, then set $UnDep so as to suppress any
418 # associated unused-object messages.
419 if ($Line =~ /unreferenced object=/) {
420 if (defined($EXRE_unref_obj) &&
421 ($Line =~ $EXRE_unref_obj)) {
426 $Line =~ s!$Tmpdir/!!;
428 $Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
429 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
432 # Look for any unused dependencies.
433 if ($UnDep && ($Line =~ /unused/)) {
434 # Skip if object is allowed to have unused dependencies
435 next if defined($EXRE_unused_deps) &&
436 ($RelPath =~ $EXRE_unused_deps);
438 # Skip if dependency is always allowed to be unused
439 next if defined($EXRE_unused_obj) &&
440 ($Line =~ $EXRE_unused_obj);
442 $Line =~ s!$Tmpdir/!! if $Secure;
443 $Line =~ s/^[ \t]*(.*)/$1\t<remove lib or -zignore?>/;
444 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath, $Line);
449 # Reuse the elfdump(1) data to investigate additional dynamic linking
452 $Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0;
454 $HasDirectBinding = 0;
457 ELF: foreach my $Line (@Elf) {
458 # We're only interested in the section headers and the dynamic
460 if ($Line =~ /^Section Header/) {
463 if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) {
464 # This object has a combined relocation section.
467 } elsif (($Stab == 0) && ($Line =~ /\.stab/)) {
468 # This object contain .stabs sections
470 } elsif (($SymSort == 0) &&
471 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) {
472 # This object contains a symbol sort section
476 if (($Strip == 1) && ($Line =~ /\.symtab/)) {
477 # This object contains a complete symbol table.
482 } elsif ($Line =~ /^Dynamic Section/) {
485 } elsif ($Line =~ /^Syminfo Section/) {
488 } elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) {
492 # Look into the Syminfo section.
493 # Does this object have at least one Directly Bound symbol?
494 if (($Header eq 'Syminfo')) {
497 if ($HasDirectBinding == 1) {
501 @Symword = split(' ', $Line);
503 if (!defined($Symword[1])) {
506 if ($Symword[1] =~ /B/) {
507 $HasDirectBinding = 1;
512 # Does this object contain text relocations.
513 if ($Tex && ($Line =~ /TEXTREL/)) {
514 # Determine if this file is allowed text relocations.
515 if (defined($EXRE_textrel) &&
516 ($RelPath =~ $EXRE_textrel)) {
520 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
521 "TEXTREL .dynamic tag\t\t\t<no -Kpic?>");
526 # Does this file have any relocation sections (there are a few
527 # psr libraries with no relocations at all, thus a .SUNW_reloc
528 # section won't exist either).
529 if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) {
530 $Relsz = hex((split(' ', $Line))[2]);
534 # Does this file have any plt relocations. If the plt size is
535 # equivalent to the total relocation size then we don't have
536 # any relocations suitable for combining into a .SUNW_reloc
538 if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) {
539 $Pltsz = hex((split(' ', $Line))[2]);
543 # Does this object have any dependencies.
544 if ($Line =~ /NEEDED/) {
545 my($Need) = (split(' ', $Line))[3];
547 if (defined($EXRE_olddep) && ($Need =~ $EXRE_olddep)) {
548 # Catch any old (unnecessary) dependencies.
549 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
550 "NEEDED=$Need\t<dependency no " .
551 "longer necessary>");
552 } elsif ((defined($EXRE_forbidden) &&
553 ($Need =~ $EXRE_forbidden)) &&
554 (!defined($EXRE_forbidden_dep) ||
555 ($FullPath !~ $EXRE_forbidden_dep))) {
556 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
557 "NEEDED=$Need\t<forbidden dependency, " .
558 "missing -nodefaultlibs?>");
560 # Under the -i (information) option print out
561 # any useful dynamic entries.
562 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
568 # Is this object built with -B direct flag on?
569 if ($Line =~ / DIRECT /) {
570 $HasDirectBinding = 1;
573 # Does this object specify a runpath.
574 if ($opt{i} && ($Line =~ /RPATH/)) {
575 my($Rpath) = (split(' ', $Line))[3];
576 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
577 $RelPath, "RPATH=$Rpath");
582 # A shared object, that contains non-plt relocations, should have a
583 # combined relocation section indicating it was built with -z combreloc.
584 if ((($Type eq 'DYN') || ($Type eq 'PIE')) &&
585 $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) {
586 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
587 ".SUNW_reloc section missing\t\t<no -zcombreloc?>");
590 # No objects released to a customer should have any .stabs sections
591 # remaining, they should be stripped.
592 if ($opt{s} && $Stab) {
593 goto DONESTAB if defined($EXRE_stab) && ($RelPath =~ $EXRE_stab);
595 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
596 "debugging sections should be deleted\t<no strip -x?>");
599 # Identify an object that is not built with either -B direct or
602 if (defined($EXRE_nodirect) && ($RelPath =~ $EXRE_nodirect));
604 if ($Relsz && ($HasDirectBinding == 0)) {
605 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
606 "object has no direct bindings\t<no -B direct or -z direct?>");
611 # All objects should have a full symbol table to provide complete
612 # debugging stack traces.
613 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
614 "symbol table should not be stripped\t<remove -s?>") if $Strip;
616 # If there are symbol sort sections in this object, report on
617 # any that have duplicate addresses.
618 ProcSymSort($FullPath, $RelPath) if $SymSort;
620 # If -v was specified, and the object has a version definition
621 # section, generate output showing each public symbol and the
622 # version it belongs to.
623 ProcVerdef($FullPath, $RelPath)
624 if ($Verdef eq 'VERDEF') && $opt{v};
628 ## ProcSymSortOutMsg(RelPath, secname, addr, names...)
630 # Call onbld_elfmod::OutMsg for a duplicate address error in a symbol sort
633 sub ProcSymSortOutMsg {
634 my($RelPath, $secname, $addr, @names) = @_;
636 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
637 "$secname: duplicate $addr: ". join(', ', @names));
641 ## ProcSymSort(FullPath, RelPath)
643 # Examine the symbol sort sections for the given object and report
644 # on any duplicate addresses found. Ideally, mapfile directives
645 # should be used when building objects that have multiple symbols
646 # with the same address so that only one of them appears in the sort
647 # section. This saves space, reduces user confusion, and ensures that
648 # libproc and debuggers always display public names instead of symbols
649 # that are merely implementation details.
653 my($FullPath, $RelPath) = @_;
655 # If this object is exempt from checking, return quietly
656 return if defined($EXRE_nosymsort) && ($FullPath =~ $EXRE_nosymsort);
659 open(SORT, "elfdump -S $FullPath|") ||
660 die "$Prog: Unable to execute elfdump (symbol sort sections)\n";
666 while ($line = <SORT>) {
669 next if ($line eq '');
671 # If this is a header line, pick up the section name
672 if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) {
675 # Every new section is followed by a column header line
676 $line = <SORT>; # Toss header line
678 # Flush anything left from previous section
679 ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
680 if (scalar(@dups) > 1);
682 # Reset variables for new sort section
689 # Process symbol line
690 my @fields = split /\s+/, $line;
691 my $new_addr = $fields[2];
692 my $new_type = $fields[8];
693 my $new_name = $fields[9];
695 if ($new_type eq 'UNDEF') {
696 onbld_elfmod::OutMsg($ErrFH, $ErrTtl, $RelPath,
697 "$secname: unexpected UNDEF symbol " .
698 "(link-editor error): $new_name");
702 if ($new_addr eq $last_addr) {
703 push @dups, $new_name;
705 ProcSymSortOutMsg($RelPath, $secname,
706 $last_addr, @dups) if (scalar(@dups) > 1);
707 @dups = ( $new_name );
708 $last_addr = $new_addr;
712 ProcSymSortOutMsg($RelPath, $secname, $last_addr, @dups)
713 if (scalar(@dups) > 1);
719 ## ProcVerdef(FullPath, RelPath)
721 # Examine the version definition section for the given object and report
722 # each public symbol along with the version it belongs to.
726 my($FullPath, $RelPath) = @_;
729 my $tab = $opt{o} ? '' : "\t";
731 # pvs -dov provides information about the versioning hierarchy
732 # in the file. Lines are of the format:
733 # path - version[XXX];
734 # where [XXX] indicates optional information, such as flags
735 # or inherited versions.
737 # Private versions are allowed to change freely, so ignore them.
738 open(PVS, "pvs -dov $FullPath|") ||
739 die "$Prog: Unable to execute pvs (version definition section)\n";
741 while ($line = <PVS>) {
744 if ($line =~ /^[^\s]+\s+-\s+([^;]+)/) {
747 next if $ver =~ /private/i;
748 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
749 "${tab}VERDEF=$ver");
754 # pvs -dos lists the symbols assigned to each version definition.
755 # Lines are of the format:
756 # path - version: symbol;
757 # path - version: symbol (size);
758 # where the (size) is added to data items, but not for functions.
759 # We strip off the size, if present.
761 open(PVS, "pvs -dos $FullPath|") ||
762 die "$Prog: Unable to execute pvs (version definition section)\n";
763 while ($line = <PVS>) {
765 if ($line =~ /^[^\s]+\s+-\s+([^:]+):\s*([^\s;]+)/) {
769 next if $ver =~ /private/i;
772 onbld_elfmod::OutMsg($InfoFH, $InfoTtl, $RelPath,
773 "VERSION=$ver, SYMBOL=$sym");
775 if ($cur_ver ne $ver) {
776 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
777 $RelPath, "VERSION=$ver");
780 onbld_elfmod::OutMsg($InfoFH, $InfoTtl,
781 $RelPath, "SYMBOL=$sym");
790 ## OpenFindElf(file, FileHandleRef, LineNumRef)
792 # Open file in 'find_elf -r' format, and return the value of
793 # the opening PREFIX line.
796 # file - file, or find_elf child process, to open
797 # FileHandleRef - Reference to file handle to open
798 # LineNumRef - Reference to integer to increment as lines are input
801 # This routine issues a fatal error and does not return on error.
802 # Otherwise, the value of PREFIX is returned.
805 my ($file, $fh, $LineNum) = @_;
809 open($fh, $file) || die "$Prog: Unable to open: $file";
812 # This script requires relative paths as created by 'find_elf -r'.
813 # When this is done, the first non-comment line will always
814 # be PREFIX. Obtain that line, or issue a fatal error.
815 while ($line = onbld_elfmod::GetLine($fh, $LineNum)) {
816 if ($line =~ /^PREFIX\s+(.*)$/i) {
821 die "$Prog: No PREFIX line seen on line $$LineNum: $file";
830 # Open the specified file, which must be produced by "find_elf -r",
831 # and process the files it describes.
838 my $prefix = OpenFindElf($file, \*FIND_ELF, \$LineNum);
840 while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
841 next if !($line =~ /^OBJECT\s/i);
843 my ($item, $class, $type, $verdef, $obj) =
844 split(/\s+/, $line, 5);
846 ProcFile("$prefix/$obj", $obj, $class, $type, $verdef);
853 ## AltObjectConfig(file)
855 # Recurse through a directory hierarchy looking for appropriate dependencies
856 # to map from their standard system locations to the proto area via a crle
860 # file - File of ELF objects, in 'find_elf -r' format, to examine.
863 # Scripts are generated for the 32 and 64-bit cases to run crle
864 # and create runtime configuration files that will establish
865 # alternative dependency mappings for the objects identified.
867 # $Env - Set to environment variable definitions that will cause
868 # the config files generated by this routine to be used
870 # $Conf32, $Conf64 - Undefined, or set to the config files generated
871 # by this routine. If defined, the caller is responsible for
872 # unlinking the files before exiting.
874 sub AltObjectConfig {
876 my ($Crle32, $Crle64);
883 my $prefix = OpenFindElf($file, \*FIND_ELF);
886 while ($line = onbld_elfmod::GetLine(\*FIND_ELF, \$LineNum)) {
889 if ($line =~ /^OBJECT\s/i) {
890 my ($item, $class, $type, $verdef, $obj) =
891 split(/\s+/, $line, 5);
893 if ($type eq 'DYN') {
898 # Only want shared libraries
904 # We need to follow links to sharable objects so
905 # that any dependencies are expressed in all their
906 # available forms. We depend on ALIAS lines directly
907 # following the object they alias, so if we have
908 # a current object, this alias belongs to it.
909 if ($obj_active && ($line =~ /^ALIAS\s/i)) {
910 my ($item, $real_obj, $obj) =
911 split(/\s+/, $line, 3);
916 # Skip unrecognized item
920 next if !$obj_active;
922 my $full = "$prefix/$obj_path";
924 next if defined($EXRE_nocrlealt) &&
925 ($obj_path =~ $EXRE_nocrlealt);
928 $Dir =~ s/^(.*)\/.*$/$1/;
930 # Create a crle(1) script for the dependency we've found.
931 # We build separate scripts for the 32 and 64-bit cases.
932 # We create and initialize each script when we encounter
933 # the first object that needs it.
934 if ($obj_class == 32) {
936 $Crle32 = "$Tmpdir/$Prog.crle32.$$";
937 open(CRLE32, "> $Crle32") ||
938 die "$Prog: open failed: $Crle32: $!";
939 print CRLE32 "#!/bin/sh\ncrle \\\n";
941 print CRLE32 "\t-o $Dir -a /$obj_path \\\n";
944 $Crle64 = "$Tmpdir/$Prog.crle64.$$";
945 open(CRLE64, "> $Crle64") ||
946 die "$Prog: open failed: $Crle64: $!";
947 print CRLE64 "#!/bin/sh\ncrle -64\\\n";
949 print CRLE64 "\t-o $Dir -a /$obj_path \\\n";
956 # Now that the config scripts are complete, use them to generate
957 # runtime linker config files.
959 $Conf64 = "$Tmpdir/$Prog.conf64.$$";
960 print CRLE64 "\t-c $Conf64\n";
965 undef $Conf64 if system($Crle64);
967 # Done with the script
971 $Conf32 = "$Tmpdir/$Prog.conf32.$$";
972 print CRLE32 "\t-c $Conf32\n";
977 undef $Conf32 if system($Crle32);
979 # Done with the script
983 # Set $Env so that we will use the config files generated above
985 if ($Crle64 && $Conf64 && $Crle32 && $Conf32) {
986 $Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32";
987 } elsif ($Crle64 && $Conf64) {
988 $Env = "-e LD_FLAGS=config_64=$Conf64";
989 } elsif ($Crle32 && $Conf32) {
990 $Env = "-e LD_FLAGS=config_32=$Conf32";
994 # -----------------------------------------------------------------------------
996 # This script relies on ldd returning output reflecting only the binary
997 # contents. But if LD_PRELOAD* environment variables are present, libraries
998 # named by them will also appear in the output, disrupting our analysis.
999 # So, before we get too far, scrub the environment.
1001 delete($ENV{LD_PRELOAD});
1002 delete($ENV{LD_PRELOAD_32});
1003 delete($ENV{LD_PRELOAD_64});
1005 # Establish a program name for any error diagnostics.
1006 chomp($Prog = `basename $0`);
1008 # The onbld_elfmod package is maintained in the same directory as this
1009 # script, and is installed in ../lib/perl. Use the local one if present,
1010 # and the installed one otherwise.
1011 my $moddir = dirname($0);
1012 $moddir = "$moddir/../lib/perl" if ! -f "$moddir/onbld_elfmod.pm";
1013 require "$moddir/onbld_elfmod.pm";
1015 # Determine what machinery is available.
1016 my $Mach = `uname -p`;
1017 my$Isalist = `isalist`;
1018 if ($Mach =~ /sparc/) {
1019 if ($Isalist =~ /sparcv9/) {
1022 } elsif ($Mach =~ /i386/) {
1023 if ($Isalist =~ /amd64/) {
1028 # $Env is used with all calls to ldd. It is set by AltObjectConfig to
1029 # cause an alternate object mapping runtime config file to be used.
1032 # Check that we have arguments.
1033 if ((getopts('D:d:E:e:f:I:imosvw:', \%opt) == 0) ||
1034 (!$opt{f} && ($#ARGV == -1))) {
1035 print "usage: $Prog [-imosv] [-D depfile | -d depdir] [-E errfile]\n";
1036 print "\t\t[-e exfile] [-f listfile] [-I infofile] [-w outdir]\n";
1037 print "\t\t[file | dir]...\n";
1039 print "\t[-D depfile]\testablish dependencies from 'find_elf -r' file list\n";
1040 print "\t[-d depdir]\testablish dependencies from under directory\n";
1041 print "\t[-E errfile]\tdirect error output to file\n";
1042 print "\t[-e exfile]\texceptions file\n";
1043 print "\t[-f listfile]\tuse file list produced by find_elf -r\n";
1044 print "\t[-I infofile]\tdirect informational output (-i, -v) to file\n";
1045 print "\t[-i]\t\tproduce dynamic table entry information\n";
1046 print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n";
1047 print "\t[-s]\t\tprocess .stab and .symtab entries\n";
1048 print "\t[-v]\t\tprocess version definition entries\n";
1049 print "\t[-w outdir]\tinterpret all files relative to given directory\n";
1053 die "$Prog: -D and -d options are mutually exclusive\n" if ($opt{D} && $opt{d});
1055 $Tmpdir = "/tmp" if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir));
1057 # If -w, change working directory to given location
1058 !$opt{w} || chdir($opt{w}) || die "$Prog: can't cd to $opt{w}";
1060 # Locate and process the exceptions file
1061 onbld_elfmod::LoadExceptionsToEXRE('check_rtime');
1063 # Is there a proto area available, either via the -d option, or because
1064 # we are part of an activated workspace?
1067 # User specified dependency directory - make sure it exists.
1068 -d $opt{d} || die "$Prog: $opt{d} is not a directory\n";
1070 } elsif ($ENV{SRCTOP}) {
1073 # Without a user specified dependency directory see if we're
1074 # part of a codemanager workspace and if a proto area exists.
1075 $Proto = $Root if ($Root = $ENV{ROOT}) && (-d $Root);
1078 # If we are basing this analysis off the sharable objects found in
1079 # a proto area, then gather dependencies and construct an alternative
1080 # dependency mapping via a crle(1) configuration file.
1082 # To support alternative dependency mapping we'll need ldd(1)'s
1083 # -e option. This is relatively new (s81_30), so make sure
1084 # ldd(1) is capable before gathering any dependency information.
1085 if ($opt{D} || $Proto) {
1086 if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) {
1087 print "ldd: does not support -e, unable to ";
1088 print "create alternative dependency mappingings.\n";
1089 print "ldd: option added under 4390308 (s81_30).\n\n";
1091 # If -D was specified, it supplies a list of files in
1092 # 'find_elf -r' format, and can use it directly. Otherwise,
1093 # we will run find_elf as a child process to find the
1094 # sharable objects found under $Proto.
1095 AltObjectConfig($opt{D} ? $opt{D} : "find_elf -frs $Proto|");
1099 # To support unreferenced dependency detection we'll need ldd(1)'s -U
1100 # option. This is relatively new (4638070), and if not available we
1101 # can still fall back to -u. Even with this option, don't use -U with
1102 # releases prior to 5.10 as the cleanup for -U use only got integrated
1103 # into 5.10 under 4642023. Note, that nightly doesn't typically set a
1104 # RELEASE from the standard <env> files. Users who wish to disable use
1105 # of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file
1106 # if using nightly, or otherwise establish it in their environment.
1107 if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) {
1112 if (($Release = $ENV{RELEASE}) && (cmp_os_ver($Release, "<", "5.10"))) {
1119 # Set up variables used to handle output files:
1121 # Error messages go to stdout unless -E is specified. $ErrFH is a
1122 # file handle reference that points at the file handle where error messages
1123 # are sent, and $ErrTtl is a reference that points at an integer used
1124 # to count how many lines have been sent there.
1126 # Informational messages go to stdout unless -I is specified. $InfoFH is a
1127 # file handle reference that points at the file handle where info messages
1128 # are sent, and $InfoTtl is a reference that points at an integer used
1129 # to count how many lines have been sent there.
1132 open(ERROR, ">$opt{E}") || die "$Prog: open failed: $opt{E}";
1139 open(INFO, ">$opt{I}") || die "$Prog: open failed: $opt{I}";
1144 my ($err_dev, $err_ino) = stat($ErrFH);
1145 my ($info_dev, $info_ino) = stat($InfoFH);
1146 $ErrTtl = \$OutCnt1;
1147 $InfoTtl = (($err_dev == $info_dev) && ($err_ino == $info_ino)) ?
1148 \$OutCnt1 : \$OutCnt2;
1151 # If we were given a list of objects in 'find_elf -r' format, then
1153 ProcFindElf($opt{f}) if $opt{f};
1155 # Process each argument
1156 foreach my $Arg (@ARGV) {
1157 # Run find_elf to find the files given by $Arg and process them
1158 ProcFindElf("find_elf -fr $Arg|");
1161 # Cleanup output files
1162 unlink $Conf64 if $Conf64;
1163 unlink $Conf32 if $Conf32;
1164 close ERROR if $opt{E};
1165 close INFO if $opt{I};