3 # Check that use of symbols declared in a given header does not result
4 # in any symbols being brought in that are not reserved with external
5 # linkage for the given standard.
7 # Copyright (C) 2014-2015 Free Software Foundation, Inc.
8 # This file is part of the GNU C Library.
10 # The GNU C Library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 # License as published by the Free Software Foundation; either
13 # version 2.1 of the License, or (at your option) any later version.
15 # The GNU C Library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with the GNU C Library; if not, see
22 # <http://www.gnu.org/licenses/>.
27 GetOptions
('header=s' => \
$header, 'standard=s' => \
$standard,
28 'flags=s' => \
$flags, 'cc=s' => \
$CC, 'tmpdir=s' => \
$tmpdir,
29 'stdsyms=s' => \
$stdsyms_file, 'libsyms=s' => \
$libsyms_file,
30 'readelf=s' => \
$READELF);
32 # Load the list of symbols that are OK.
34 open (STDSYMS
, "<$stdsyms_file") || die ("open $stdsyms_file: $!\n");
39 close (STDSYMS
) || die ("close $stdsyms_file: $!\n");
41 # The following whitelisted symbols are also allowed for now.
43 # * Bug 15421: lgamma wrongly sets signgam for ISO C.
45 # * Bug 17576: stdin, stdout, stderr only reserved with external
46 # linkage when stdio.h included (and possibly not then), not
49 # * False positive: matherr only used conditionally.
51 @whitelist = qw(signgam stdin stdout stderr matherr);
52 foreach my $sym (@whitelist) {
56 # Return information about GLOBAL and WEAK symbols listed in readelf
60 open (SYMS
, "<$syms_file") || die ("open $syms_file: $!\n");
61 my ($file) = $syms_file;
71 my (@fields) = split (/\s+/, $_);
75 my ($bind) = $fields[4];
76 my ($ndx) = $fields[6];
77 my ($sym) = $fields[7];
78 if ($bind ne "GLOBAL" && $bind ne "WEAK") {
81 if ($sym !~ /^\w+$/) {
84 push (@ret, [$file, $sym, $bind, $ndx ne "UND"]);
86 close (SYMS
) || die ("close $syms_file: $!\n");
90 # Load information about GLOBAL and WEAK symbols defined or used in
91 # the standard libraries.
92 # Strong symbols (defined or undefined) from a given object.
94 # Strong undefined symbols from a given object.
95 %strong_undef_syms = ();
96 # Objects defining a given symbol (strongly or weakly).
98 @sym_data = list_syms
($libsyms_file);
99 foreach my $sym (@sym_data) {
100 my ($file, $name, $bind, $defined) = @
$sym;
102 if (!defined ($sym_objs{$name})) {
103 $sym_objs{$name} = [];
105 push (@
{$sym_objs{$name}}, $file);
107 if ($bind eq "GLOBAL") {
108 if (!defined ($strong_syms{$file})) {
109 $strong_syms{$file} = [];
111 push (@
{$strong_syms{$file}}, $name);
113 if (!defined ($strong_undef_syms{$file})) {
114 $strong_undef_syms{$file} = [];
116 push (@
{$strong_undef_syms{$file}}, $name);
121 # Determine what ELF-level symbols are brought in by use of C-level
122 # symbols declared in the given header.
124 # The rules followed are heuristic and so may produce false positives
125 # and false negatives.
127 # * Weak undefined symbols are ignored; however, if a code path that
128 # references one (even just to check if its address is 0) is executed,
129 # that may conflict with a definition of that symbol in the user's
132 # * Strong undefined symbols are considered of signficance, but it is
133 # possible that (a) any standard library definition is weak, so can be
134 # overridden by the user's definition, and (b) the symbol is only used
135 # conditionally and not if the program is limited to standard
136 # functionality. (matherr is an example of such a false positive.)
138 # * If a symbol reference is only brought in by the user using a data
139 # symbol rather than a function from the standard library, this will
142 # * If a symbol reference is only brought in by crt*.o or libgcc, this
143 # will not be detected.
145 # * If a symbol reference is only brought in through __builtin_foo in
146 # a standard macro being compiled to call foo, this will not be
149 # * Header inclusions should be compiled several times with different
150 # options such as -O2, -D_FORTIFY_SOURCE and -D_FILE_OFFSET_BITS=64 to
151 # find out what symbols are undefined from such a compilation; this is
152 # not yet implemented.
154 # * This script finds symbols referenced through use of macros on the
155 # basis that if a macro calls an internal function, that function must
156 # also be declared in the header. However, the header might also
157 # declare implementation-namespace functions that are not called by
158 # any standard macro in the header, resulting in false positives for
159 # any symbols brought in only through use of those
160 # implementation-namespace functions.
162 # * Namespace issues can apply for dynamic linking as well as static
163 # linking, when a call is from one shared library to another or uses a
164 # PLT entry for a call within a shared library; such issues are only
165 # detected by this script if the same namespace issue applies for
168 @c_syms = list_exported_functions
("$CC $flags", $standard, $header, $tmpdir);
169 $cincfile = "$tmpdir/undef-$$.c";
170 $cincfile_o = "$tmpdir/undef-$$.o";
171 $cincfile_sym = "$tmpdir/undef-$$.sym";
172 open (CINCFILE
, ">$cincfile") || die ("open $cincfile: $!\n");
173 print CINCFILE
"#include <$header>\n";
174 foreach my $sym (sort @c_syms) {
175 print CINCFILE
"void *__glibc_test_$sym = (void *) &$sym;\n";
177 close CINCFILE
|| die ("close $cincfile: $!\n");
178 system ("$CC $flags -D_ISOMAC $CFLAGS{$standard} -c $cincfile -o $cincfile_o")
179 && die ("compiling failed\n");
180 system ("LC_ALL=C $READELF -W -s $cincfile_o > $cincfile_sym")
181 && die ("readelf failed\n");
182 @elf_syms = list_syms
($cincfile_sym);
183 unlink ($cincfile) || die ("unlink $cincfile: $!\n");
184 unlink ($cincfile_o) || die ("unlink $cincfile_o: $!\n");
185 unlink ($cincfile_sym) || die ("unlink $cincfile_sym: $!\n");
191 foreach my $sym (@elf_syms) {
192 my ($file, $name, $bind, $defined) = @
$sym;
193 if ($bind eq "GLOBAL" && !$defined) {
194 $strong_seen{$name} = "[initial] $name";
195 $all_undef{$name} = "[initial] $name";
196 $current_undef{$name} = "[initial] $name";
200 while (%current_undef) {
202 foreach my $sym (sort keys %current_undef) {
203 foreach my $file (@
{$sym_objs{$sym}}) {
204 if (defined ($files_seen{$file})) {
207 $files_seen{$file} = 1;
208 foreach my $ssym (@
{$strong_syms{$file}}) {
209 if (!defined ($strong_seen{$ssym})) {
210 $strong_seen{$ssym} = "$current_undef{$sym} -> [$file] $ssym";
213 foreach my $usym (@
{$strong_undef_syms{$file}}) {
214 if (!defined ($all_undef{$usym})) {
215 $all_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
216 $new_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
221 %current_undef = %new_undef;
225 foreach my $sym (sort keys %strong_seen) {
229 if (defined ($stdsyms{$sym})) {
232 print "$strong_seen{$sym}\n";