1#!/usr/perl5/bin/perl -w 2# 3# CDDL HEADER START 4# 5# The contents of this file are subject to the terms of the 6# Common Development and Distribution License (the "License"). 7# You may not use this file except in compliance with the License. 8# 9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10# or http://www.opensolaris.org/os/licensing. 11# See the License for the specific language governing permissions 12# and limitations under the License. 13# 14# When distributing Covered Code, include this CDDL HEADER in each 15# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16# If applicable, add the following below this CDDL HEADER, with the 17# fields enclosed by brackets "[]" replaced with your own identifying 18# information: Portions Copyright [yyyy] [name of copyright owner] 19# 20# CDDL HEADER END 21# 22 23# 24# Copyright 2008 Sun Microsystems, Inc. All rights reserved. 25# Use is subject to license terms. 26# 27# ident "%Z%%M% %I% %E% SMI" 28# 29 30# 31# Check ELF information. 32# 33# This script descends a directory hierarchy inspecting ELF dynamic executables 34# and shared objects. The general theme is to verify that common Makefile rules 35# have been used to build these objects. Typical failures occur when Makefile 36# rules are re-invented rather than being inherited from "cmd/lib" Makefiles. 37# 38# As always, a number of components don't follow the rules, and these are 39# excluded to reduce this scripts output. Pathnames used for this exclusion 40# assume this script is being run over a "proto" area. The -a (all) option 41# skips any exclusions. 42# 43# By default any file that has conditions that should be reported is first 44# listed and then each condition follows. The -o (one-line) option produces a 45# more terse output which is better for sorting/diffing with "nightly". 46# 47# NOTE: missing dependencies, symbols or versions are reported by running the 48# file through ldd(1). As objects within a proto area are built to exist in a 49# base system, standard use of ldd(1) will bind any objects to dependencies 50# that exist in the base system. It is frequently the case that newer objects 51# exist in the proto area that are required to satisfy other objects 52# dependencies, and without using these newer objects an ldd(1) will produce 53# misleading error messages. To compensate for this, the -d option (or the 54# existence of the CODEMSG_WS/ROOT environment variables) cause the creation of 55# alternative dependency mappings via crle(1) configuration files that establish 56# any proto shared objects as alternatives to their base system location. Thus 57# ldd(1) can be executed against these configuration files so that objects in a 58# proto area bind to their dependencies in the same proto area. 59 60 61# Define all global variables (required for strict) 62use vars qw($SkipDirs $SkipFiles $SkipTextrelFiles $SkipDirectBindFiles); 63use vars qw($SkipUndefFiles $SkipUnusedDirs); 64use vars qw($SkipStabFiles $SkipNoExStkFiles $SkipCrleConf); 65use vars qw($SkipUnusedSearchPath $SkipUnrefObject); 66use vars qw($Prog $Mach $Isalist $Env $Ena64 $Tmpdir $Error $Gnuc); 67use vars qw($UnusedPaths $LddNoU $Crle32 $Crle64 $Conf32 $Conf64); 68use vars qw($SkipDirectBindDirs $SkipInterps $SkipSymSort $OldDeps %opt); 69 70use strict; 71 72 73# Define any directories we should skip completely. 74$SkipDirs = qr{ 75 usr/lib/devfsadm | # 4382889 76 usr/lib/libc | # optimized libc 77 usr/lib/rcm | # 4426119 78 usr/perl5 | # alan's taking care of these :-) 79 usr/src # no need to look at shipped source 80}x; 81 82# Define any files we should skip completely. 83$SkipFiles = qr{ ^(?: 84 lddstub | # lddstub has no dependencies 85 geniconvtbl\.so | # 4384329 86 libssagent\.so\.1 | # 4328854 87 libpsvcplugin_psr\.so\.1 | # 4385799 88 libpsvcpolicy_psr\.so\.1 | # " " 89 libpsvcpolicy\.so\.1 | # " " 90 picl_slm\.so | # " " 91 mod_ipp\.so | # Apache loadable module 92 fptest | # USIII specific extns. cause ldd noise on USII bld. m/c 93 grub 94 )$ 95}x; 96 97# Define any files that are allowed text relocations. 98$SkipTextrelFiles = qr{ ^(?: 99 unix | # kernel models are non-pic 100 mdb # relocations against __RTC (dbx) 101 )$ 102}x; 103 104# Define any directories or files that are allowed to have no direct bound 105# symbols 106$SkipDirectBindDirs = qr{ 107 usr/ucb 108}x; 109 110$SkipDirectBindFiles = qr{ ^(?: 111 unix | 112 sbcp | 113 libproc.so.1 | 114 libnisdb.so.2 115 )$ 116}x; 117 118# Define any files that are allowed undefined references. 119 120$SkipUndefFiles = qr{ ^(?: 121 libsvm\.so\.1 | # libspmicommon.so.1 lacking 122 libnisdb\.so\.2 # C++ 123 )$ 124}x; 125 126# Define any files that have unused dependencies. 127$SkipUnusedDirs = qr{ 128 lib/picl/plugins/ | # require devtree dependencies 129 /lib/libp # profile libc makes libm an unused 130}x; # dependency of standard libc 131 132# Define any files that should contain debugging information. 133$SkipStabFiles = qr{ ^(?: 134 unix 135 )$ 136}x; 137 138# Define any files that don't require a non-executable stack definition. 139$SkipNoExStkFiles = qr{ ^(?: 140 forth | 141 unix | 142 multiboot 143 )$ 144}x; 145 146# Identify any files that should be skipped when building a crle(1) 147# configuration file. As the hwcap libraries can be loop-back mounted onto 148# libc, these can confuse crle(1) because of their identical dev/inode. 149$SkipCrleConf = qr{ 150 lib/libc/libc_hwcap 151}x; 152 153# Skip "unused search path=" ldd(1) diagnostics. 154$SkipUnusedSearchPath = qr{ 155 /usr/lib/fs/autofs.*\ from\ .automountd | # dlopen() 156 /etc/ppp/plugins.*\ from\ .*pppd | # dlopen() 157 /usr/lib/inet/ppp.*\ from\ .*pppd | # dlopen() 158 /usr/sfw/lib.*\ from\ .*libipsecutil.so.1 | # dlopen() 159 /usr/platform/.*rsmlib.*\ from\ .*librsm.so.2 | # dlopen() 160 \$ORIGIN.*\ from\ .*fcode.so | # dlopen() 161 /usr/platform/.*\ from\ .*/usr/platform | # picl 162 /usr/lib/picl/.*\ from\ .*/usr/platform | # picl 163 /usr/platform/.*\ from\ .*/usr/lib/picl | # picl 164 /usr/lib/smbsrv.*\ from\ .*libsmb\.so\.1 | # future needs 165 /usr/lib/mps/secv1.*\ from\ .*libnss3\.so | # non-OSNet 166 /usr/lib/mps.*\ from\ .*libnss3\.so | # non-OSNet 167 /usr/sfw/lib.*\ from\ .*libdbus-1\.so\.3 | # non-OSNet 168 /usr/sfw/lib.*\ from\ .*libdbus-glib-1\.so\.2 | # non-OSNet 169 /usr/sfw/lib.*\ from\ .*libglib-2\.0\.so\.0 | # non-OSNet 170 /usr/X11/lib.*\ from\ .*libglib-2\.0\.so\.0 | # non-OSNet 171 /usr/sfw/lib.*\ from\ .*libgobject-2\.0\.so\.0 | # non-OSNet 172 /usr/X11/lib.*\ from\ .*libgobject-2\.0\.so\.0 | # non-OSNet 173 /usr/sfw/lib.*\ from\ .*libcrypto\.so\.0\.9\.8 | # non-OSNet 174 /usr/sfw/lib.*\ from\ .*libnetsnmp\.so\.5 | # non-OSNet 175 /usr/sfw/lib.*\ from\ .*libgcc_s\.so\.1 | # non-OSNet 176 /usr.*\ from\ .*tst\.gcc\.exe # gcc built 177}x; 178 179# Skip "unreferenced object=" ldd(1) diagnostics. 180$SkipUnrefObject = qr{ 181 /libmapmalloc\.so\.1;\ unused\ dependency\ of | # interposer 182 /libstdc\+\+\.so\.6;\ unused\ dependency\ of | # gcc build 183 /libm\.so\.2.*\ of\ .*libstdc\+\+\.so\.6 | # gcc build 184 /lib.*\ of\ .*/lib/picl/plugins/ | # picl 185 /lib.*\ of\ .*libcimapi\.so | # non-OSNET 186 /lib.*\ of\ .*libjvm\.so | # non-OSNET 187 /lib.*\ of\ .*libnetsnmp\.so\.5 | # non-OSNET 188 /lib.*\ of\ .*libnetsnmpagent\.so\.5 | # non-OSNET 189 /lib.*\ of\ .*libnetsnmpmibs\.so\.5 | # non-OSNET 190 /lib.*\ of\ .*libnetsnmphelpers\.so\.5 | # non-OSNET 191 /lib.*\ of\ .*libnspr4\.so | # non-OSNET 192 /lib.*\ of\ .*libsoftokn3\.so | # non-OSNET 193 /lib.*\ of\ .*libspmicommon\.so\.1 | # non-OSNET 194 /lib.*\ of\ .*libspmocommon\.so\.1 | # non-OSNET 195 /lib.*\ of\ .*libssl3\.so | # non-OSNET 196 /lib.*\ of\ .*libxml2\.so\.2 | # non-OSNET 197 /lib.*\ of\ .*libxslt\.so\.1 # non-OSNET 198}x; 199 200# Define any files that should only have unused (ldd -u) processing. 201$UnusedPaths = qr{ 202 ucb/shutdown # libucb interposes on libc and makes 203 # dependencies on libc seem unnecessary 204}x; 205 206# Define interpreters we should ignore. 207$SkipInterps = qr{ 208 misc/krtld | 209 misc/amd64/krtld | 210 misc/sparcv9/krtld 211}x; 212 213# Catch libintl and libw, although ld(1) will bind to these and thus determine 214# they're needed, their content was moved into libc as of on297 build 7. 215# libthread and libpthread were completely moved into libc as of on10 build 53. 216# libdl was moved into libc as of on10 build 49. librt and libaio were moved 217# into libc as of Nevada build 44. 218$OldDeps = qr{ ^(?: 219 libintl\.so\.1 | 220 libw\.so\.1 | 221 libthread\.so\.1 | 222 libpthread\.so\.1 | 223 libdl\.so\.1 | 224 librt\.so\.1 | 225 libaio\.so\.1 226 )$ 227}x; 228 229# Files for which we skip checking of duplicate addresses in the 230# symbol sort sections. Such exceptions should be rare --- most code will 231# not have duplicate addresses, since it takes assember or a "#pragma weak" 232# to do such aliasing in C. C++ is different: The compiler generates aliases 233# for implementation reasons, and the mangled names used to encode argument 234# and return value types are difficult to handle well in mapfiles. 235# Furthermore, the Sun compiler and gcc use different and incompatible 236# name mangling conventions. Since ON must be buildable by either, we 237# would have to maintain two sets of mapfiles for each such object. 238# C++ use is rare in ON, so this is not worth pursuing. 239# 240$SkipSymSort = qr{ ^.*(?: 241 opt/SUNWdtrt/tst/common/pid/tst.weak2.exe | # DTrace test 242 lib/amd64/libnsl\.so\.1 | # C++ 243 lib/sparcv9/libnsl\.so\.1 | # C++ 244 lib/sparcv9/libfru\.so\.1 | # C++ 245 usr/lib/sgml/nsgmls | # C++ 246 ld\.so\.1 # libc_pic.a user 247 )$ 248}x; 249 250use Getopt::Std; 251 252# ----------------------------------------------------------------------------- 253 254# Reliably compare two OS revisions. Arguments are <ver1> <op> <ver2>. 255# <op> is the string form of a normal numeric comparison operator. 256sub cmp_os_ver { 257 my @ver1 = split(/\./, $_[0]); 258 my $op = $_[1]; 259 my @ver2 = split(/\./, $_[2]); 260 261 push @ver2, ("0") x $#ver1 - $#ver2; 262 push @ver1, ("0") x $#ver2 - $#ver1; 263 264 my $diff = 0; 265 while (@ver1 || @ver2) { 266 if (($diff = shift(@ver1) - shift(@ver2)) != 0) { 267 last; 268 } 269 } 270 return (eval "$diff $op 0" ? 1 : 0); 271} 272 273# This script relies on ldd returning output reflecting only the binary 274# contents. But if LD_PRELOAD* environment variables are present, libraries 275# named by them will also appear in the output, disrupting our analysis. 276# So, before we get too far, scrub the environment. 277 278delete($ENV{LD_PRELOAD}); 279delete($ENV{LD_PRELOAD_32}); 280delete($ENV{LD_PRELOAD_64}); 281 282# Establish a program name for any error diagnostics. 283chomp($Prog = `basename $0`); 284 285# Determine what machinery is available. 286$Mach = `uname -p`; 287$Isalist = `isalist`; 288$Env = ""; 289if ($Mach =~ /sparc/) { 290 if ($Isalist =~ /sparcv9/) { 291 $Ena64 = "ok"; 292 } 293} elsif ($Mach =~ /i386/) { 294 if ($Isalist =~ /amd64/) { 295 $Ena64 = "ok"; 296 } 297} 298 299# Check that we have arguments. 300if ((getopts('ad:imos', \%opt) == 0) || ($#ARGV == -1)) { 301 print "usage: $Prog [-a] [-d depdir] [-m] [-o] [-s] file | dir, ...\n"; 302 print "\t[-a]\t\tprocess all files (ignore any exception lists)\n"; 303 print "\t[-d dir]\testablish dependencies from under directory\n"; 304 print "\t[-i]\t\tproduce dynamic table entry information\n"; 305 print "\t[-m]\t\tprocess mcs(1) comments\n"; 306 print "\t[-o]\t\tproduce one-liner output (prefixed with pathname)\n"; 307 print "\t[-s]\t\tprocess .stab and .symtab entries\n"; 308 exit 1; 309} else { 310 my($Proto); 311 312 if ($opt{d}) { 313 # User specified dependency directory - make sure it exists. 314 if (! -d $opt{d}) { 315 print "$Prog: $opt{d} is not a directory\n"; 316 exit 1; 317 } 318 $Proto = $opt{d}; 319 320 } elsif ($ENV{CODEMGR_WS}) { 321 my($Root); 322 323 # Without a user specified dependency directory see if we're 324 # part of a codemanager workspace and if a proto area exists. 325 if (($Root = $ENV{ROOT}) && (-d $Root)) { 326 $Proto = $Root; 327 } 328 } 329 330 if (!($Tmpdir = $ENV{TMPDIR}) || (! -d $Tmpdir)) { 331 $Tmpdir = "/tmp"; 332 } 333 334 # Determine whether this is a __GNUC build. If so, unused search path 335 # processing is disabled. 336 if (defined $ENV{__GNUC}) { 337 $Gnuc = 1; 338 } else { 339 $Gnuc = 0; 340 } 341 342 # Look for dependencies under $Proto. 343 if ($Proto) { 344 # To support alternative dependency mapping we'll need ldd(1)'s 345 # -e option. This is relatively new (s81_30), so make sure 346 # ldd(1) is capable before gathering any dependency information. 347 if (system('ldd -e /usr/lib/lddstub 2> /dev/null')) { 348 print "ldd: does not support -e, unable to "; 349 print "create alternative dependency mappingings.\n"; 350 print "ldd: option added under 4390308 (s81_30).\n\n"; 351 } else { 352 # Gather dependencies and construct a alternative 353 # dependency mapping via a crle(1) configuration file. 354 GetDeps($Proto, "/"); 355 GenConf(); 356 } 357 } 358 359 # To support unreferenced dependency detection we'll need ldd(1)'s -U 360 # option. This is relatively new (4638070), and if not available we 361 # can still fall back to -u. Even with this option, don't use -U with 362 # releases prior to 5.10 as the cleanup for -U use only got integrated 363 # into 5.10 under 4642023. Note, that nightly doesn't typically set a 364 # RELEASE from the standard <env> files. Users who wish to disable use 365 # of ldd(1)'s -U should set (or uncomment) RELEASE in their <env> file 366 # if using nightly, or otherwise establish it in their environment. 367 if (system('ldd -U /usr/lib/lddstub 2> /dev/null')) { 368 $LddNoU = 1; 369 } else { 370 my($Release); 371 372 if (($Release = $ENV{RELEASE}) && 373 (cmp_os_ver($Release, "<", "5.10"))) { 374 $LddNoU = 1; 375 } else { 376 $LddNoU = 0; 377 } 378 } 379 380 # For each argument determine if we're dealing with a file or directory. 381 foreach my $Arg (@ARGV) { 382 # Ignore symbolic links. 383 if (-l $Arg) { 384 next; 385 } 386 387 if (!stat($Arg)) { 388 next; 389 } 390 391 # Process simple files. 392 if (-f _) { 393 my($RelPath) = $Arg; 394 my($File) = $Arg; 395 my($Secure) = 0; 396 397 $RelPath =~ s!^.*/!./!; 398 $File =~ s!^.*/!!; 399 400 if (-u _ || -g _) { 401 $Secure = 1; 402 } 403 404 ProcFile($Arg, $RelPath, $File, $Secure); 405 next; 406 } 407 # Process directories. 408 if (-d _) { 409 ProcDir($Arg, "."); 410 next; 411 } 412 413 print "$Arg is not a file or directory\n"; 414 $Error = 1; 415 } 416 417 # Cleanup 418 CleanUp(); 419} 420 421$Error = 0; 422 423# Clean up any temporary files. 424sub CleanUp { 425 if ($Crle64) { 426 unlink $Crle64; 427 } 428 if ($Conf64) { 429 unlink $Conf64; 430 } 431 if ($Crle32) { 432 unlink $Crle32; 433 } 434 if ($Conf32) { 435 unlink $Conf32; 436 } 437} 438 439# Create an output message, either a one-liner (under -o) or preceded by the 440# files relative pathname as a title. 441sub OutMsg { 442 my($Ttl, $Path, $Msg) = @_; 443 444 if ($opt{o}) { 445 $Msg =~ s/^[ \t]*//; 446 print "$Path: $Msg\n"; 447 } else { 448 if ($Ttl eq 0) { 449 print "==== $Path ====\n"; 450 } 451 print "$Msg\n"; 452 } 453} 454 455# Determine whether this a ELF dynamic object and if so investigate its runtime 456# attributes. 457sub ProcFile { 458 my($FullPath, $RelPath, $File, $Secure) = @_; 459 my(@Elf, @Ldd, $Dyn, $Intp, $Dll, $Ttl, $Sym, $Interp, $Stack); 460 my($Sun, $Relsz, $Pltsz, $Tex, $Stab, $Strip, $Lddopt, $SymSort); 461 my($Val, $Header, $SkipLdd, $IsX86, $RWX, $UnDep); 462 my($HasDirectBinding); 463 464 # Ignore symbolic links. 465 if (-l $FullPath) { 466 return; 467 } 468 469 $Ttl = 0; 470 @Ldd = 0; 471 472 # Determine whether we have access to inspect the file. 473 if (!(-r $FullPath)) { 474 OutMsg($Ttl++, $RelPath, 475 "\tunable to inspect file: permission denied"); 476 return; 477 } 478 479 # Determine if this is a file we don't care about. 480 if (!$opt{a}) { 481 if ($File =~ $SkipFiles) { 482 return; 483 } 484 } 485 486 # Determine whether we have a executable (static or dynamic) or a 487 # shared object. 488 @Elf = split(/\n/, `elfdump -epdicy $FullPath 2>&1`); 489 490 $Dyn = $Intp = $Dll = $Stack = $IsX86 = $RWX = 0; 491 $Interp = 1; 492 $Header = 'None'; 493 foreach my $Line (@Elf) { 494 # If we have an invalid file type (which we can tell from the 495 # first line), or we're processing an archive, bail. 496 if ($Header eq 'None') { 497 if (($Line =~ /invalid file/) || 498 ($Line =~ /$FullPath(.*):/)) { 499 return; 500 } 501 } 502 503 if ($Line =~ /^ELF Header/) { 504 $Header = 'Ehdr'; 505 506 } elsif ($Line =~ /^Program Header/) { 507 $Header = 'Phdr'; 508 $RWX = 0; 509 510 } elsif ($Line =~ /^Interpreter/) { 511 $Header = 'Intp'; 512 513 } elsif ($Line =~ /^Dynamic Section/) { 514 # A dynamic section indicates we're a dynamic object 515 # (this makes sure we don't check static executables). 516 $Dyn = 1; 517 518 } elsif (($Header eq 'Ehdr') && ($Line =~ /e_type:/)) { 519 # The e_type field indicates whether this file is a 520 # shared object (ET_DYN) or an executable (ET_EXEC). 521 if ($Line =~ /ET_DYN/) { 522 $Dll = 1; 523 } elsif ($Line !~ /ET_EXEC/) { 524 return; 525 } 526 } elsif (($Header eq 'Ehdr') && ($Line =~ /ei_class:/)) { 527 # If we encounter a 64-bit object, but we're not running 528 # on a 64-bit system, suppress calling ldd(1). 529 if (($Line =~ /ELFCLASS64/) && !$Ena64) { 530 $SkipLdd = 1; 531 } 532 } elsif (($Header eq 'Ehdr') && ($Line =~ /e_machine:/)) { 533 # If it's a X86 object, we need to enforce RW- data. 534 if (($Line =~ /(EM_AMD64|EM_386)/)) { 535 $IsX86 = 1; 536 } 537 } elsif (($Header eq 'Phdr') && 538 ($Line =~ /\[ PF_X PF_W PF_R \]/)) { 539 # RWX segment seen. 540 $RWX = 1; 541 542 } elsif (($Header eq 'Phdr') && 543 ($Line =~ /\[ PT_LOAD \]/ && $RWX && $IsX86)) { 544 # Seen an RWX PT_LOAD segment. 545 if ($File !~ $SkipNoExStkFiles) { 546 OutMsg($Ttl++, $RelPath, 547 "\tapplication requires non-executable " . 548 "data\t<no -Mmapfile_noexdata?>"); 549 } 550 551 } elsif (($Header eq 'Phdr') && 552 ($Line =~ /\[ PT_SUNWSTACK \]/)) { 553 # This object defines a non-executable stack. 554 $Stack = 1; 555 556 } elsif (($Header eq 'Intp') && !$opt{a} && 557 ($Line =~ $SkipInterps)) { 558 # This object defines an interpretor we should skip. 559 $Interp = 0; 560 } 561 } 562 563 # Determine whether this ELF executable or shared object has a 564 # conforming mcs(1) comment section. If the correct $(POST_PROCESS) 565 # macros are used, only a 3 or 4 line .comment section should exist 566 # containing one or two "@(#)SunOS" identifying comments (one comment 567 # for a non-debug build, and two for a debug build). The results of 568 # the following split should be three or four lines, the last empty 569 # line being discarded by the split. 570 if ($opt{m}) { 571 my(@Mcs, $Con, $Dev); 572 573 @Mcs = split(/\n/, `mcs -p $FullPath 2>&1`); 574 575 $Con = $Dev = $Val = 0; 576 foreach my $Line (@Mcs) { 577 $Val++; 578 579 if (($Val == 3) && ($Line !~ /^@\(#\)SunOS/)) { 580 $Con = 1; 581 last; 582 } 583 if (($Val == 4) && ($Line =~ /^@\(#\)SunOS/)) { 584 $Dev = 1; 585 next; 586 } 587 if (($Dev == 0) && ($Val == 4)) { 588 $Con = 1; 589 last; 590 } 591 if (($Dev == 1) && ($Val == 5)) { 592 $Con = 1; 593 last; 594 } 595 } 596 if ($opt{m} && ($Con == 1)) { 597 OutMsg($Ttl++, $RelPath, 598 "\tnon-conforming mcs(1) comment\t<no \$(POST_PROCESS)?>"); 599 } 600 } 601 602 # Applications should contain a non-executable stack definition. 603 if (($Dll == 0) && ($Stack == 0)) { 604 if (!$opt{a}) { 605 if ($File =~ $SkipNoExStkFiles) { 606 goto DYN; 607 } 608 } 609 OutMsg($Ttl++, $RelPath, 610 "\tapplication requires non-executable stack\t<no -Mmapfile_noexstk?>"); 611 } 612 613DYN: 614 # Having caught any static executables in the mcs(1) check and non- 615 # executable stack definition check, continue with dynamic objects 616 # from now on. 617 if ($Dyn eq 0) { 618 return; 619 } 620 621 # Only use ldd unless we've encountered an interpreter that should 622 # be skipped. 623 if (!$SkipLdd && $Interp) { 624 my $LDDFullPath = $FullPath; 625 626 if ($Secure) { 627 # The execution of a secure application over an nfs file 628 # system mounted nosuid will result in warning messages 629 # being sent to /var/adm/messages. As this type of 630 # environment can occur with root builds, move the file 631 # being investigated to a safe place first. In addition 632 # remove its secure permission so that it can be 633 # influenced by any alternative dependency mappings. 634 635 my($TmpPath) = "$Tmpdir/$File"; 636 637 system('cp', $LDDFullPath, $TmpPath); 638 chmod 0777, $TmpPath; 639 $LDDFullPath = $TmpPath; 640 } 641 642 # Use ldd(1) to determine the objects relocatability and use. 643 # By default look for all unreferenced dependencies. However, 644 # some objects have legitimate dependencies that they do not 645 # reference. 646 if ($LddNoU || ($RelPath =~ $UnusedPaths)) { 647 $Lddopt = "-ru"; 648 } else { 649 $Lddopt = "-rU"; 650 } 651 @Ldd = split(/\n/, `ldd $Lddopt $Env $LDDFullPath 2>&1`); 652 if ($Secure) { 653 unlink $LDDFullPath; 654 } 655 } 656 657 $Val = 0; 658 $Sym = 5; 659 $UnDep = 1; 660 661 foreach my $Line (@Ldd) { 662 663 if ($Val == 0) { 664 $Val = 1; 665 # Make sure ldd(1) worked. One possible failure is that 666 # this is an old ldd(1) prior to -e addition (4390308). 667 if ($Line =~ /usage:/) { 668 $Line =~ s/$/\t<old ldd(1)?>/; 669 OutMsg($Ttl++, $RelPath, $Line); 670 last; 671 } elsif ($Line =~ /execution failed/) { 672 OutMsg($Ttl++, $RelPath, $Line); 673 last; 674 } 675 676 # It's possible this binary can't be executed, ie. we've 677 # found a sparc binary while running on an intel system, 678 # or a sparcv9 binary on a sparcv7/8 system. 679 if ($Line =~ /wrong class/) { 680 OutMsg($Ttl++, $RelPath, 681 "\thas wrong class or data encoding"); 682 next; 683 } 684 685 # Historically, ldd(1) likes executable objects to have 686 # their execute bit set. Note that this test isn't 687 # applied unless the -a option is in effect, as any 688 # non-executable files are skipped by default to reduce 689 # the cost of running this script. 690 if ($Line =~ /not executable/) { 691 OutMsg($Ttl++, $RelPath, 692 "\tis not executable"); 693 next; 694 } 695 } 696 697 # Look for "file" or "versions" that aren't found. Note that 698 # these lines will occur before we find any symbol referencing 699 # errors. 700 if (($Sym == 5) && ($Line =~ /not found\)/)) { 701 if ($Line =~ /file not found\)/) { 702 $Line =~ s/$/\t<no -zdefs?>/; 703 } 704 OutMsg($Ttl++, $RelPath, $Line); 705 next; 706 } 707 # Look for relocations whose symbols can't be found. Note, we 708 # only print out the first 5 relocations for any file as this 709 # output can be excessive. 710 if ($Sym && ($Line =~ /symbol not found/)) { 711 # Determine if this file is allowed undefined 712 # references. 713 if ($Sym == 5) { 714 if (!$opt{a}) { 715 if ($File =~ $SkipUndefFiles) { 716 $Sym = 0; 717 next; 718 } 719 } 720 } 721 if ($Sym-- == 1) { 722 if (!$opt{o}) { 723 OutMsg($Ttl++, $RelPath, 724 "\tcontinued ..."); 725 } 726 next; 727 } 728 # Just print the symbol name. 729 $Line =~ s/$/\t<no -zdefs?>/; 730 OutMsg($Ttl++, $RelPath, $Line); 731 next; 732 } 733 # Look for any unused search paths. 734 if ($Line =~ /unused search path=/) { 735 # Note, skip this comparison for __GNUC builds, as the 736 # gnu compilers insert numerous unused search paths. 737 if ($Gnuc == 1) { 738 next; 739 } 740 if (!$opt{a}) { 741 if ($Line =~ $SkipUnusedSearchPath) { 742 next; 743 } 744 } 745 if ($Secure) { 746 $Line =~ s!$Tmpdir/!!; 747 } 748 $Line =~ s/^[ \t]*(.*)/\t$1\t<remove search path?>/; 749 OutMsg($Ttl++, $RelPath, $Line); 750 next; 751 } 752 # Look for unreferenced dependencies. Note, if any unreferenced 753 # objects are ignored, then set $UnDep so as to suppress any 754 # associated unused-object messages. 755 if ($Line =~ /unreferenced object=/) { 756 if (!$opt{a}) { 757 if ($Line =~ $SkipUnrefObject) { 758 $UnDep = 0; 759 next; 760 } 761 } 762 if ($Secure) { 763 $Line =~ s!$Tmpdir/!!; 764 } 765 $Line =~ s/^[ \t]*(.*)/\t$1\t<remove lib or -zignore?>/; 766 OutMsg($Ttl++, $RelPath, $Line); 767 next; 768 } 769 # Look for any unused dependencies. 770 if ($UnDep && ($Line =~ /unused/)) { 771 if (!$opt{a}) { 772 if ($RelPath =~ $SkipUnusedDirs) { 773 $UnDep = 0; 774 next; 775 } 776 } 777 if ($Secure) { 778 $Line =~ s!$Tmpdir/!!; 779 } 780 $Line =~ s/^[ \t]*(.*)/\t$1\t<remove lib or -zignore?>/; 781 OutMsg($Ttl++, $RelPath, $Line); 782 next; 783 } 784 } 785 786 # Reuse the elfdump(1) data to investigate additional dynamic linking 787 # information. 788 789 $Sun = $Relsz = $Pltsz = $Dyn = $Stab = $SymSort = 0; 790 $Tex = $Strip = 1; 791 $HasDirectBinding = 0; 792 793 $Header = 'None'; 794ELF: foreach my $Line (@Elf) { 795 # We're only interested in the section headers and the dynamic 796 # section. 797 if ($Line =~ /^Section Header/) { 798 $Header = 'Shdr'; 799 800 if (($Sun == 0) && ($Line =~ /\.SUNW_reloc/)) { 801 # This object has a combined relocation section. 802 $Sun = 1; 803 804 } elsif (($Stab == 0) && ($Line =~ /\.stab/)) { 805 # This object contain .stabs sections 806 $Stab = 1; 807 } elsif (($SymSort == 0) && 808 ($Line =~ /\.SUNW_dyn(sym)|(tls)sort/)) { 809 # This object contains a symbol sort section 810 $SymSort = 1; 811 } 812 813 if (($Strip == 1) && ($Line =~ /\.symtab/)) { 814 # This object contains a complete symbol table. 815 $Strip = 0; 816 } 817 next; 818 819 } elsif ($Line =~ /^Dynamic Section/) { 820 $Header = 'Dyn'; 821 next; 822 } elsif ($Line =~ /^Syminfo Section/) { 823 $Header = 'Syminfo'; 824 next; 825 } elsif (($Header ne 'Dyn') && ($Header ne 'Syminfo')) { 826 next; 827 } 828 829 # Look into the Syminfo section. 830 # Does this object have at least one Directly Bound symbol? 831 if (($Header eq 'Syminfo')) { 832 my(@Symword); 833 834 if ($HasDirectBinding == 1) { 835 next; 836 } 837 838 @Symword = split(' ', $Line); 839 840 if (!defined($Symword[1])) { 841 next; 842 } 843 if ($Symword[1] =~ /B/) { 844 $HasDirectBinding = 1; 845 } 846 next; 847 } 848 849 # Does this object contain text relocations. 850 if ($Tex && ($Line =~ /TEXTREL/)) { 851 # Determine if this file is allowed text relocations. 852 if (!$opt{a}) { 853 if ($File =~ $SkipTextrelFiles) { 854 $Tex = 0; 855 next ELF; 856 } 857 } 858 OutMsg($Ttl++, $RelPath, 859 "\tTEXTREL .dynamic tag\t\t\t<no -Kpic?>"); 860 $Tex = 0; 861 next; 862 } 863 864 # Does this file have any relocation sections (there are a few 865 # psr libraries with no relocations at all, thus a .SUNW_reloc 866 # section won't exist either). 867 if (($Relsz == 0) && ($Line =~ / RELA?SZ/)) { 868 $Relsz = hex((split(' ', $Line))[2]); 869 next; 870 } 871 872 # Does this file have any plt relocations. If the plt size is 873 # equivalent to the total relocation size then we don't have 874 # any relocations suitable for combining into a .SUNW_reloc 875 # section. 876 if (($Pltsz == 0) && ($Line =~ / PLTRELSZ/)) { 877 $Pltsz = hex((split(' ', $Line))[2]); 878 next; 879 } 880 881 # Does this object have any dependencies. 882 if ($Line =~ /NEEDED/) { 883 my($Need) = (split(' ', $Line))[3]; 884 885 if ($Need =~ $OldDeps) { 886 # Catch any old (unnecessary) dependencies. 887 OutMsg($Ttl++, $RelPath, 888 "\tNEEDED=$Need\t<dependency no longer necessary>"); 889 } elsif ($opt{i}) { 890 # Under the -i (information) option print out 891 # any useful dynamic entries. 892 OutMsg($Ttl++, $RelPath, "\tNEEDED=$Need"); 893 } 894 next; 895 } 896 897 # Is this object built with -B direct flag on? 898 if ($Line =~ / DIRECT /) { 899 $HasDirectBinding = 1; 900 } 901 902 # Does this object specify a runpath. 903 if ($opt{i} && ($Line =~ /RPATH/)) { 904 my($Rpath) = (split(' ', $Line))[3]; 905 OutMsg($Ttl++, $RelPath, "\tRPATH=$Rpath"); 906 next; 907 } 908 } 909 910 # A shared object, that contains non-plt relocations, should have a 911 # combined relocation section indicating it was built with -z combreloc. 912 if ($Dll && $Relsz && ($Relsz != $Pltsz) && ($Sun == 0)) { 913 OutMsg($Ttl++, $RelPath, 914 "\tSUNW_reloc section missing\t\t<no -zcombreloc?>"); 915 } 916 917 # No objects released to a customer should have any .stabs sections 918 # remaining, they should be stripped. 919 if ($opt{s} && $Stab) { 920 if (!$opt{a}) { 921 if ($File =~ $SkipStabFiles) { 922 goto DONESTAB; 923 } 924 } 925 OutMsg($Ttl++, $RelPath, 926 "\tdebugging sections should be deleted\t<no strip -x?>"); 927 } 928 929 # Identify an object that is not built with either -B direct or 930 # -z direct. 931 if (($RelPath =~ $SkipDirectBindDirs) || 932 ($File =~ $SkipDirectBindFiles)) { 933 goto DONESTAB; 934 } 935 if ($Relsz && ($HasDirectBinding == 0)) { 936 OutMsg($Ttl++, $RelPath, 937 "\tobject has no direct bindings\t<no -B direct or -z direct?>"); 938 } 939 940DONESTAB: 941 942 # All objects should have a full symbol table to provide complete 943 # debugging stack traces. 944 if ($Strip) { 945 OutMsg($Ttl++, $RelPath, 946 "\tsymbol table should not be stripped\t<remove -s?>"); 947 } 948 949 # If there are symbol sort sections in this object, report on 950 # any that have duplicate addresses. 951 ProcSymSort($FullPath, $RelPath, \$Ttl) if $SymSort; 952} 953 954 955## ProcSymSortOutMsg(RefTtl, RelPath, secname, addr, names...) 956# 957# Call OutMsg for a duplicate address error in a symbol sort 958# section 959# 960sub ProcSymSortOutMsg { 961 my($RefTtl, $RelPath, $secname, $addr, @names) = @_; 962 963 OutMsg($$RefTtl++, $RelPath, 964 "$secname: duplicate $addr: ". join(', ', @names)); 965} 966 967 968## ProcSymSort(FullPath, RelPath) 969# 970# Examine the symbol sort sections for the given object and report 971# on any duplicate addresses found. Ideally, mapfile directives 972# should be used when building objects that have multiple symbols 973# with the same address so that only one of them appears in the sort 974# section. This saves space, reduces user confusion, and ensures that 975# libproc and debuggers always display public names instead of symbols 976# that are merely implementation details. 977# 978sub ProcSymSort { 979 980 my($FullPath, $RelPath, $RefTtl) = @_; 981 982 # If this object is exempt from checking, return quietly 983 return if ($FullPath =~ $SkipSymSort); 984 985 986 open(SORT, "elfdump -S $FullPath|") || 987 die "$Prog: Unable to execute elfdump (symbol sort sections)\n"; 988 989 my $line; 990 my $last_addr; 991 my @dups = (); 992 my $secname; 993 while ($line = <SORT>) { 994 chomp $line; 995 996 next if ($line eq ''); 997 998 # If this is a header line, pick up the section name 999 if ($line =~ /^Symbol Sort Section:\s+([^\s]+)\s+/) { 1000 $secname = $1; 1001 1002 # Every new section is followed by a column header line 1003 $line = <SORT>; # Toss header line 1004 1005 # Flush anything left from previous section 1006 ProcSymSortOutMsg($RefTtl, $RelPath, $secname, 1007 $last_addr, @dups) if (scalar(@dups) > 1); 1008 1009 # Reset variables for new sort section 1010 $last_addr = ''; 1011 @dups = (); 1012 1013 next; 1014 } 1015 1016 # Process symbol line 1017 my @fields = split /\s+/, $line; 1018 my $new_addr = $fields[2]; 1019 my $new_name = $fields[9]; 1020 1021 if ($new_addr eq $last_addr) { 1022 push @dups, $new_name; 1023 } else { 1024 ProcSymSortOutMsg($RefTtl, $RelPath, $secname, 1025 $last_addr, @dups) if (scalar(@dups) > 1); 1026 @dups = ( $new_name ); 1027 $last_addr = $new_addr; 1028 } 1029 } 1030 1031 ProcSymSortOutMsg($RefTtl, $RelPath, $secname, $last_addr, @dups) 1032 if (scalar(@dups) > 1); 1033 1034 close SORT; 1035} 1036 1037 1038sub ProcDir { 1039 my($FullDir, $RelDir) = @_; 1040 my($NewFull, $NewRel); 1041 1042 # Determine if this is a directory we don't care about. 1043 if (!$opt{a}) { 1044 if ($RelDir =~ $SkipDirs) { 1045 return; 1046 } 1047 } 1048 1049 # Open the directory and read each entry, omit files starting with "." 1050 if (opendir(DIR, $FullDir)) { 1051 foreach my $Entry (readdir(DIR)) { 1052 if ($Entry =~ /^\./) { 1053 next; 1054 } 1055 $NewFull = "$FullDir/$Entry"; 1056 1057 # Ignore symlinks. 1058 if (-l $NewFull) { 1059 next; 1060 } 1061 if (!stat($NewFull)) { 1062 next; 1063 } 1064 $NewRel = "$RelDir/$Entry"; 1065 1066 # Descend into and process any directories. 1067 if (-d _) { 1068 ProcDir($NewFull, $NewRel); 1069 next; 1070 } 1071 1072 # Typically dynamic objects are executable, so we can 1073 # reduce the overall cost of this script (a lot!) by 1074 # screening out non-executables here, rather than pass 1075 # them to file(1) later. However, it has been known 1076 # for shared objects to be mistakenly left non- 1077 # executable, so with -a let all files through so that 1078 # this requirement can be verified (see ProcFile()). 1079 if (!$opt{a}) { 1080 if (! -x _) { 1081 next; 1082 } 1083 } 1084 1085 # Process any standard files. 1086 if (-f _) { 1087 my($Secure) = 0; 1088 1089 if (-u _ || -g _) { 1090 $Secure = 1; 1091 } 1092 1093 ProcFile($NewFull, $NewRel, $Entry, $Secure); 1094 next; 1095 } 1096 1097 } 1098 closedir(DIR); 1099 } 1100} 1101 1102# Create a crle(1) script for any 64-bit dependencies we locate. A runtime 1103# configuration file will be generated to establish alternative dependency 1104# mappings for all these dependencies. 1105 1106sub Entercrle64 { 1107 my($FullDir, $RelDir, $Entry) = @_; 1108 1109 if (!$Crle64) { 1110 # Create and initialize the script if is doesn't already exit. 1111 1112 $Crle64 = "$Tmpdir/$Prog.crle64.$$"; 1113 open(CRLE64, "> $Crle64") || 1114 die "$Prog: open failed: $Crle64: $!"; 1115 1116 print CRLE64 "#!/bin/sh\ncrle -64\\\n"; 1117 } 1118 print CRLE64 "\t-o $FullDir -a $RelDir/$Entry \\\n"; 1119} 1120 1121# Create a crle(1) script for any 32-bit dependencies we locate. A runtime 1122# configuration file will be generated to establish alternative dependency 1123# mappings for all these dependencies. 1124 1125sub Entercrle32 { 1126 my($FullDir, $RelDir, $Entry) = @_; 1127 1128 if (!$Crle32) { 1129 # Create and initialize the script if is doesn't already exit. 1130 1131 $Crle32 = "$Tmpdir/$Prog.crle32.$$"; 1132 open(CRLE32, "> $Crle32") || 1133 die "$Prog: open failed: $Crle32: $!"; 1134 1135 print CRLE32 "#!/bin/sh\ncrle \\\n"; 1136 } 1137 print CRLE32 "\t-o $FullDir -a $RelDir/$Entry \\\n"; 1138} 1139 1140# Having finished gathering dependencies, complete any crle(1) scripts and 1141# execute them to generate the associated runtime configuration files. In 1142# addition establish the environment variable required to pass the configuration 1143# files to ldd(1). 1144 1145sub GenConf { 1146 if ($Crle64) { 1147 $Conf64 = "$Tmpdir/$Prog.conf64.$$"; 1148 print CRLE64 "\t-c $Conf64\n"; 1149 1150 chmod 0755, $Crle64; 1151 close CRLE64; 1152 1153 if (system($Crle64)) { 1154 undef $Conf64; 1155 } 1156 } 1157 if ($Crle32) { 1158 $Conf32 = "$Tmpdir/$Prog.conf32.$$"; 1159 print CRLE32 "\t-c $Conf32\n"; 1160 1161 chmod 0755, $Crle32; 1162 close CRLE32; 1163 1164 if (system($Crle32)) { 1165 undef $Conf32; 1166 } 1167 } 1168 1169 if ($Crle64 && $Conf64 && $Crle32 && $Conf32) { 1170 $Env = "-e LD_FLAGS=config_64=$Conf64,config_32=$Conf32"; 1171 } elsif ($Crle64 && $Conf64) { 1172 $Env = "-e LD_FLAGS=config_64=$Conf64"; 1173 } elsif ($Crle32 && $Conf32) { 1174 $Env = "-e LD_FLAGS=config_32=$Conf32"; 1175 } 1176} 1177 1178# Recurse through a directory hierarchy looking for appropriate dependencies. 1179 1180sub GetDeps { 1181 my($FullDir, $RelDir) = @_; 1182 my($NewFull); 1183 1184 # Open the directory and read each entry, omit files starting with "." 1185 if (opendir(DIR, $FullDir)) { 1186 foreach my $Entry (readdir(DIR)) { 1187 if ($Entry =~ /^\./) { 1188 next; 1189 } 1190 $NewFull = "$FullDir/$Entry"; 1191 1192 # We need to follow links so that any dependencies 1193 # are expressed in all their available forms. 1194 # Bail on symlinks like 32 -> . 1195 if (-l $NewFull) { 1196 if (readlink($NewFull) =~ /^\.$/) { 1197 next; 1198 } 1199 } 1200 if (!stat($NewFull)) { 1201 next; 1202 } 1203 1204 if (!$opt{a}) { 1205 if ($NewFull =~ $SkipCrleConf) { 1206 next; 1207 } 1208 } 1209 1210 # If this is a directory descend into it. 1211 if (-d _) { 1212 my($NewRel); 1213 1214 if ($RelDir =~ /^\/$/) { 1215 $NewRel = "$RelDir$Entry"; 1216 } else { 1217 $NewRel = "$RelDir/$Entry"; 1218 } 1219 1220 GetDeps($NewFull, $NewRel); 1221 next; 1222 } 1223 1224 # If this is a regular file determine if its a 1225 # valid ELF dependency. 1226 if (-f _) { 1227 my($File); 1228 1229 # Typically shared object dependencies end with 1230 # ".so" or ".so.?", hence we can reduce the cost 1231 # of this script (a lot!) by screening out files 1232 # that don't follow this pattern. 1233 if (!$opt{a}) { 1234 if ($Entry !~ /\.so(?:\.\d+)*$/) { 1235 next; 1236 } 1237 } 1238 1239 $File = `file $NewFull`; 1240 if ($File !~ /dynamic lib/) { 1241 next; 1242 } 1243 1244 if ($File =~ /32-bit/) { 1245 Entercrle32($FullDir, $RelDir, $Entry); 1246 } elsif ($Ena64) { 1247 Entercrle64($FullDir, $RelDir, $Entry); 1248 } 1249 next; 1250 } 1251 } 1252 closedir(DIR); 1253 } 1254} 1255exit $Error 1256