Fix erfcf spurious underflows (bug 18217).
[glibc.git] / conform / linknamespace.pl
blob8ea437d4570ca9eb9562dfbdeb1194270042e3f6
1 #! /usr/bin/perl
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/>.
24 use GlibcConform;
25 use Getopt::Long;
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.
33 %stdsyms = ();
34 open (STDSYMS, "<$stdsyms_file") || die ("open $stdsyms_file: $!\n");
35 while (<STDSYMS>) {
36 chomp;
37 $stdsyms{$_} = 1;
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
47 # generally.
49 # * False positive: matherr only used conditionally.
51 @whitelist = qw(signgam stdin stdout stderr matherr);
52 foreach my $sym (@whitelist) {
53 $stdsyms{$sym} = 1;
56 # Return information about GLOBAL and WEAK symbols listed in readelf
57 # -s output.
58 sub list_syms {
59 my ($syms_file) = @_;
60 open (SYMS, "<$syms_file") || die ("open $syms_file: $!\n");
61 my ($file) = $syms_file;
62 my (@ret) = ();
63 while (<SYMS>) {
64 chomp;
65 if (/^File: (.*)/) {
66 $file = $1;
67 $file =~ s|^.*/||;
68 next;
70 s/^\s*//;
71 # Architecture-specific st_other bits appear inside [] and disrupt
72 # the format of readelf output.
73 s/\[.*?\]//;
74 my (@fields) = split (/\s+/, $_);
75 if (@fields < 8) {
76 next;
78 my ($bind) = $fields[4];
79 my ($ndx) = $fields[6];
80 my ($sym) = $fields[7];
81 if ($bind ne "GLOBAL" && $bind ne "WEAK") {
82 next;
84 if ($sym !~ /^\w+$/) {
85 next;
87 push (@ret, [$file, $sym, $bind, $ndx ne "UND"]);
89 close (SYMS) || die ("close $syms_file: $!\n");
90 return @ret;
93 # Load information about GLOBAL and WEAK symbols defined or used in
94 # the standard libraries.
95 # Strong symbols (defined or undefined) from a given object.
96 %strong_syms = ();
97 # Strong undefined symbols from a given object.
98 %strong_undef_syms = ();
99 # Objects defining a given symbol (strongly or weakly).
100 %sym_objs = ();
101 @sym_data = list_syms ($libsyms_file);
102 foreach my $sym (@sym_data) {
103 my ($file, $name, $bind, $defined) = @$sym;
104 if ($defined) {
105 if (!defined ($sym_objs{$name})) {
106 $sym_objs{$name} = [];
108 push (@{$sym_objs{$name}}, $file);
110 if ($bind eq "GLOBAL") {
111 if (!defined ($strong_syms{$file})) {
112 $strong_syms{$file} = [];
114 push (@{$strong_syms{$file}}, $name);
115 if (!$defined) {
116 if (!defined ($strong_undef_syms{$file})) {
117 $strong_undef_syms{$file} = [];
119 push (@{$strong_undef_syms{$file}}, $name);
124 # Determine what ELF-level symbols are brought in by use of C-level
125 # symbols declared in the given header.
127 # The rules followed are heuristic and so may produce false positives
128 # and false negatives.
130 # * Weak undefined symbols are ignored; however, if a code path that
131 # references one (even just to check if its address is 0) is executed,
132 # that may conflict with a definition of that symbol in the user's
133 # program.
135 # * Strong undefined symbols are considered of signficance, but it is
136 # possible that (a) any standard library definition is weak, so can be
137 # overridden by the user's definition, and (b) the symbol is only used
138 # conditionally and not if the program is limited to standard
139 # functionality. (matherr is an example of such a false positive.)
141 # * If a symbol reference is only brought in by the user using a data
142 # symbol rather than a function from the standard library, this will
143 # not be detected.
145 # * If a symbol reference is only brought in by crt*.o or libgcc, this
146 # will not be detected.
148 # * If a symbol reference is only brought in through __builtin_foo in
149 # a standard macro being compiled to call foo, this will not be
150 # detected.
152 # * Header inclusions should be compiled several times with different
153 # options such as -O2, -D_FORTIFY_SOURCE and -D_FILE_OFFSET_BITS=64 to
154 # find out what symbols are undefined from such a compilation; this is
155 # not yet implemented.
157 # * This script finds symbols referenced through use of macros on the
158 # basis that if a macro calls an internal function, that function must
159 # also be declared in the header. However, the header might also
160 # declare implementation-namespace functions that are not called by
161 # any standard macro in the header, resulting in false positives for
162 # any symbols brought in only through use of those
163 # implementation-namespace functions.
165 # * Namespace issues can apply for dynamic linking as well as static
166 # linking, when a call is from one shared library to another or uses a
167 # PLT entry for a call within a shared library; such issues are only
168 # detected by this script if the same namespace issue applies for
169 # static linking.
171 @c_syms = list_exported_functions ("$CC $flags", $standard, $header, $tmpdir);
172 $cincfile = "$tmpdir/undef-$$.c";
173 $cincfile_o = "$tmpdir/undef-$$.o";
174 $cincfile_sym = "$tmpdir/undef-$$.sym";
175 open (CINCFILE, ">$cincfile") || die ("open $cincfile: $!\n");
176 print CINCFILE "#include <$header>\n";
177 foreach my $sym (sort @c_syms) {
178 print CINCFILE "void *__glibc_test_$sym = (void *) &$sym;\n";
180 close CINCFILE || die ("close $cincfile: $!\n");
181 system ("$CC $flags -D_ISOMAC $CFLAGS{$standard} -c $cincfile -o $cincfile_o")
182 && die ("compiling failed\n");
183 system ("LC_ALL=C $READELF -W -s $cincfile_o > $cincfile_sym")
184 && die ("readelf failed\n");
185 @elf_syms = list_syms ($cincfile_sym);
186 unlink ($cincfile) || die ("unlink $cincfile: $!\n");
187 unlink ($cincfile_o) || die ("unlink $cincfile_o: $!\n");
188 unlink ($cincfile_sym) || die ("unlink $cincfile_sym: $!\n");
190 %strong_seen = ();
191 %files_seen = ();
192 %all_undef = ();
193 %current_undef = ();
194 foreach my $sym (@elf_syms) {
195 my ($file, $name, $bind, $defined) = @$sym;
196 if ($bind eq "GLOBAL" && !$defined) {
197 $strong_seen{$name} = "[initial] $name";
198 $all_undef{$name} = "[initial] $name";
199 $current_undef{$name} = "[initial] $name";
203 while (%current_undef) {
204 %new_undef = ();
205 foreach my $sym (sort keys %current_undef) {
206 foreach my $file (@{$sym_objs{$sym}}) {
207 if (defined ($files_seen{$file})) {
208 next;
210 $files_seen{$file} = 1;
211 foreach my $ssym (@{$strong_syms{$file}}) {
212 if (!defined ($strong_seen{$ssym})) {
213 $strong_seen{$ssym} = "$current_undef{$sym} -> [$file] $ssym";
216 foreach my $usym (@{$strong_undef_syms{$file}}) {
217 if (!defined ($all_undef{$usym})) {
218 $all_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
219 $new_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
224 %current_undef = %new_undef;
227 $ret = 0;
228 foreach my $sym (sort keys %strong_seen) {
229 if ($sym =~ /^_/) {
230 next;
232 if (defined ($stdsyms{$sym})) {
233 next;
235 print "$strong_seen{$sym}\n";
236 $ret = 1;
239 exit $ret;