Blame conform/linknamespace.pl

Packit 6c4009
#!/usr/bin/perl
Packit 6c4009
Packit 6c4009
# Check that use of symbols declared in a given header does not result
Packit 6c4009
# in any symbols being brought in that are not reserved with external
Packit 6c4009
# linkage for the given standard.
Packit 6c4009
Packit 6c4009
# Copyright (C) 2014-2018 Free Software Foundation, Inc.
Packit 6c4009
# This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
# The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
# modify it under the terms of the GNU Lesser General Public
Packit 6c4009
# License as published by the Free Software Foundation; either
Packit 6c4009
# version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
# The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
# Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
# You should have received a copy of the GNU Lesser General Public
Packit 6c4009
# License along with the GNU C Library; if not, see
Packit 6c4009
# <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
use GlibcConform;
Packit 6c4009
use Getopt::Long;
Packit 6c4009
Packit 6c4009
GetOptions ('header=s' => \$header, 'standard=s' => \$standard,
Packit 6c4009
	    'flags=s' => \$flags, 'cc=s' => \$CC, 'tmpdir=s' => \$tmpdir,
Packit 6c4009
	    'stdsyms=s' => \$stdsyms_file, 'libsyms=s' => \$libsyms_file,
Packit 6c4009
	    'readelf=s' => \$READELF);
Packit 6c4009
Packit 6c4009
# Load the list of symbols that are OK.
Packit 6c4009
%stdsyms = ();
Packit 6c4009
open (STDSYMS, "<$stdsyms_file") || die ("open $stdsyms_file: $!\n");
Packit 6c4009
while (<STDSYMS>) {
Packit 6c4009
  chomp;
Packit 6c4009
  $stdsyms{$_} = 1;
Packit 6c4009
}
Packit 6c4009
close (STDSYMS) || die ("close $stdsyms_file: $!\n");
Packit 6c4009
Packit 6c4009
# The following whitelisted symbols are also allowed for now.
Packit 6c4009
#
Packit 6c4009
# * Bug 17576: stdin, stdout, stderr only reserved with external
Packit 6c4009
# linkage when stdio.h included (and possibly not then), not
Packit 6c4009
# generally.
Packit 6c4009
#
Packit 6c4009
# * Bug 18442: re_syntax_options wrongly brought in by regcomp and
Packit 6c4009
# used by re_comp.
Packit 6c4009
#
Packit 6c4009
@whitelist = qw(stdin stdout stderr re_syntax_options);
Packit 6c4009
foreach my $sym (@whitelist) {
Packit 6c4009
  $stdsyms{$sym} = 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# Return information about GLOBAL and WEAK symbols listed in readelf
Packit 6c4009
# -s output.
Packit 6c4009
sub list_syms {
Packit 6c4009
  my ($syms_file) = @_;
Packit 6c4009
  open (SYMS, "<$syms_file") || die ("open $syms_file: $!\n");
Packit 6c4009
  my ($file) = $syms_file;
Packit 6c4009
  my (@ret) = ();
Packit 6c4009
  while (<SYMS>) {
Packit 6c4009
    chomp;
Packit 6c4009
    if (/^File: (.*)/) {
Packit 6c4009
      $file = $1;
Packit 6c4009
      $file =~ s|^.*/||;
Packit 6c4009
      next;
Packit 6c4009
    }
Packit 6c4009
    s/^\s*//;
Packit 6c4009
    # Architecture-specific st_other bits appear inside [] and disrupt
Packit 6c4009
    # the format of readelf output.
Packit 6c4009
    s/\[.*?\]//;
Packit 6c4009
    my (@fields) = split (/\s+/, $_);
Packit 6c4009
    if (@fields < 8) {
Packit 6c4009
      next;
Packit 6c4009
    }
Packit 6c4009
    my ($bind) = $fields[4];
Packit 6c4009
    my ($ndx) = $fields[6];
Packit 6c4009
    my ($sym) = $fields[7];
Packit 6c4009
    if ($bind ne "GLOBAL" && $bind ne "WEAK") {
Packit 6c4009
      next;
Packit 6c4009
    }
Packit 6c4009
    if ($sym !~ /^\w+$/) {
Packit 6c4009
      next;
Packit 6c4009
    }
Packit 6c4009
    push (@ret, [$file, $sym, $bind, $ndx ne "UND"]);
Packit 6c4009
  }
Packit 6c4009
  close (SYMS) || die ("close $syms_file: $!\n");
Packit 6c4009
  return @ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# Load information about GLOBAL and WEAK symbols defined or used in
Packit 6c4009
# the standard libraries.
Packit 6c4009
# Symbols from a given object, except for weak defined symbols.
Packit 6c4009
%seen_syms = ();
Packit 6c4009
# Strong undefined symbols from a given object.
Packit 6c4009
%strong_undef_syms = ();
Packit 6c4009
# Objects defining a given symbol (strongly or weakly).
Packit 6c4009
%sym_objs = ();
Packit 6c4009
@sym_data = list_syms ($libsyms_file);
Packit 6c4009
foreach my $sym (@sym_data) {
Packit 6c4009
  my ($file, $name, $bind, $defined) = @$sym;
Packit 6c4009
  if ($defined) {
Packit 6c4009
    if (!defined ($sym_objs{$name})) {
Packit 6c4009
      $sym_objs{$name} = [];
Packit 6c4009
    }
Packit 6c4009
    push (@{$sym_objs{$name}}, $file);
Packit 6c4009
  }
Packit 6c4009
  if ($bind eq "GLOBAL" || !$defined) {
Packit 6c4009
    if (!defined ($seen_syms{$file})) {
Packit 6c4009
      $seen_syms{$file} = [];
Packit 6c4009
    }
Packit 6c4009
    push (@{$seen_syms{$file}}, $name);
Packit 6c4009
  }
Packit 6c4009
  if ($bind eq "GLOBAL" && !$defined) {
Packit 6c4009
    if (!defined ($strong_undef_syms{$file})) {
Packit 6c4009
      $strong_undef_syms{$file} = [];
Packit 6c4009
    }
Packit 6c4009
    push (@{$strong_undef_syms{$file}}, $name);
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# Determine what ELF-level symbols are brought in by use of C-level
Packit 6c4009
# symbols declared in the given header.
Packit 6c4009
#
Packit 6c4009
# The rules followed are heuristic and so may produce false positives
Packit 6c4009
# and false negatives.
Packit 6c4009
#
Packit 6c4009
# * All undefined symbols are considered of signficance, but it is
Packit 6c4009
# possible that (a) any standard library definition is weak, so can be
Packit 6c4009
# overridden by the user's definition, and (b) the symbol is only used
Packit 6c4009
# conditionally and not if the program is limited to standard
Packit 6c4009
# functionality.
Packit 6c4009
#
Packit 6c4009
# * If a symbol reference is only brought in by the user using a data
Packit 6c4009
# symbol rather than a function from the standard library, this will
Packit 6c4009
# not be detected.
Packit 6c4009
#
Packit 6c4009
# * If a symbol reference is only brought in by crt*.o or libgcc, this
Packit 6c4009
# will not be detected.
Packit 6c4009
#
Packit 6c4009
# * If a symbol reference is only brought in through __builtin_foo in
Packit 6c4009
# a standard macro being compiled to call foo, this will not be
Packit 6c4009
# detected.
Packit 6c4009
#
Packit 6c4009
# * Header inclusions should be compiled several times with different
Packit 6c4009
# options such as -O2, -D_FORTIFY_SOURCE and -D_FILE_OFFSET_BITS=64 to
Packit 6c4009
# find out what symbols are undefined from such a compilation; this is
Packit 6c4009
# not yet implemented.
Packit 6c4009
#
Packit 6c4009
# * This script finds symbols referenced through use of macros on the
Packit 6c4009
# basis that if a macro calls an internal function, that function must
Packit 6c4009
# also be declared in the header.  However, the header might also
Packit 6c4009
# declare implementation-namespace functions that are not called by
Packit 6c4009
# any standard macro in the header, resulting in false positives for
Packit 6c4009
# any symbols brought in only through use of those
Packit 6c4009
# implementation-namespace functions.
Packit 6c4009
#
Packit 6c4009
# * Namespace issues can apply for dynamic linking as well as static
Packit 6c4009
# linking, when a call is from one shared library to another or uses a
Packit 6c4009
# PLT entry for a call within a shared library; such issues are only
Packit 6c4009
# detected by this script if the same namespace issue applies for
Packit 6c4009
# static linking.
Packit 6c4009
Packit 6c4009
@c_syms = list_exported_functions ("$CC $flags", $standard, $header, $tmpdir);
Packit 6c4009
$cincfile = "$tmpdir/undef-$$.c";
Packit 6c4009
$cincfile_o = "$tmpdir/undef-$$.o";
Packit 6c4009
$cincfile_sym = "$tmpdir/undef-$$.sym";
Packit 6c4009
open (CINCFILE, ">$cincfile") || die ("open $cincfile: $!\n");
Packit 6c4009
print CINCFILE "#include <$header>\n";
Packit 6c4009
foreach my $sym (sort @c_syms) {
Packit 6c4009
  print CINCFILE "void *__glibc_test_$sym = (void *) &$sym;\n";
Packit 6c4009
}
Packit 6c4009
close CINCFILE || die ("close $cincfile: $!\n");
Packit 6c4009
system ("$CC $flags -D_ISOMAC $CFLAGS{$standard} -c $cincfile -o $cincfile_o")
Packit 6c4009
  && die ("compiling failed\n");
Packit 6c4009
system ("LC_ALL=C $READELF -W -s $cincfile_o > $cincfile_sym")
Packit 6c4009
  && die ("readelf failed\n");
Packit 6c4009
@elf_syms = list_syms ($cincfile_sym);
Packit 6c4009
unlink ($cincfile) || die ("unlink $cincfile: $!\n");
Packit 6c4009
unlink ($cincfile_o) || die ("unlink $cincfile_o: $!\n");
Packit 6c4009
unlink ($cincfile_sym) || die ("unlink $cincfile_sym: $!\n");
Packit 6c4009
Packit 6c4009
%seen_where = ();
Packit 6c4009
%files_seen = ();
Packit 6c4009
%all_undef = ();
Packit 6c4009
%current_undef = ();
Packit 6c4009
foreach my $sym (@elf_syms) {
Packit 6c4009
  my ($file, $name, $bind, $defined) = @$sym;
Packit 6c4009
  if ($bind eq "GLOBAL" && !$defined) {
Packit 6c4009
    $seen_where{$name} = "[initial] $name";
Packit 6c4009
    $all_undef{$name} = "[initial] $name";
Packit 6c4009
    $current_undef{$name} = "[initial] $name";
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
while (%current_undef) {
Packit 6c4009
  %new_undef = ();
Packit 6c4009
  foreach my $sym (sort keys %current_undef) {
Packit 6c4009
    foreach my $file (@{$sym_objs{$sym}}) {
Packit 6c4009
      if (defined ($files_seen{$file})) {
Packit 6c4009
	next;
Packit 6c4009
      }
Packit 6c4009
      $files_seen{$file} = 1;
Packit 6c4009
      foreach my $ssym (@{$seen_syms{$file}}) {
Packit 6c4009
	if (!defined ($seen_where{$ssym})) {
Packit 6c4009
	  $seen_where{$ssym} = "$current_undef{$sym} -> [$file] $ssym";
Packit 6c4009
	}
Packit 6c4009
      }
Packit 6c4009
      foreach my $usym (@{$strong_undef_syms{$file}}) {
Packit 6c4009
	if (!defined ($all_undef{$usym})) {
Packit 6c4009
	  $all_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
Packit 6c4009
	  $new_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
Packit 6c4009
	}
Packit 6c4009
      }
Packit 6c4009
    }
Packit 6c4009
  }
Packit 6c4009
  %current_undef = %new_undef;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
$ret = 0;
Packit 6c4009
foreach my $sym (sort keys %seen_where) {
Packit 6c4009
  if ($sym =~ /^_/) {
Packit 6c4009
    next;
Packit 6c4009
  }
Packit 6c4009
  if (defined ($stdsyms{$sym})) {
Packit 6c4009
    next;
Packit 6c4009
  }
Packit 6c4009
  print "$seen_where{$sym}\n";
Packit 6c4009
  $ret = 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
exit $ret;