tst-tzset: raise timeout to 5 seconds
[glibc.git] / conform / linknamespace.pl
blob847d2dd33a15be95d8e05efc3b61aebe55fce747
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 # * Bug 18442: re_syntax_options wrongly brought in by regcomp and
50 # used by re_comp.
52 # * False positive: matherr only used conditionally.
54 @whitelist = qw(signgam stdin stdout stderr re_syntax_options matherr);
55 foreach my $sym (@whitelist) {
56 $stdsyms{$sym} = 1;
59 # Return information about GLOBAL and WEAK symbols listed in readelf
60 # -s output.
61 sub list_syms {
62 my ($syms_file) = @_;
63 open (SYMS, "<$syms_file") || die ("open $syms_file: $!\n");
64 my ($file) = $syms_file;
65 my (@ret) = ();
66 while (<SYMS>) {
67 chomp;
68 if (/^File: (.*)/) {
69 $file = $1;
70 $file =~ s|^.*/||;
71 next;
73 s/^\s*//;
74 # Architecture-specific st_other bits appear inside [] and disrupt
75 # the format of readelf output.
76 s/\[.*?\]//;
77 my (@fields) = split (/\s+/, $_);
78 if (@fields < 8) {
79 next;
81 my ($bind) = $fields[4];
82 my ($ndx) = $fields[6];
83 my ($sym) = $fields[7];
84 if ($bind ne "GLOBAL" && $bind ne "WEAK") {
85 next;
87 if ($sym !~ /^\w+$/) {
88 next;
90 push (@ret, [$file, $sym, $bind, $ndx ne "UND"]);
92 close (SYMS) || die ("close $syms_file: $!\n");
93 return @ret;
96 # Load information about GLOBAL and WEAK symbols defined or used in
97 # the standard libraries.
98 # Strong symbols (defined or undefined) from a given object.
99 %strong_syms = ();
100 # Strong undefined symbols from a given object.
101 %strong_undef_syms = ();
102 # Objects defining a given symbol (strongly or weakly).
103 %sym_objs = ();
104 @sym_data = list_syms ($libsyms_file);
105 foreach my $sym (@sym_data) {
106 my ($file, $name, $bind, $defined) = @$sym;
107 if ($defined) {
108 if (!defined ($sym_objs{$name})) {
109 $sym_objs{$name} = [];
111 push (@{$sym_objs{$name}}, $file);
113 if ($bind eq "GLOBAL") {
114 if (!defined ($strong_syms{$file})) {
115 $strong_syms{$file} = [];
117 push (@{$strong_syms{$file}}, $name);
118 if (!$defined) {
119 if (!defined ($strong_undef_syms{$file})) {
120 $strong_undef_syms{$file} = [];
122 push (@{$strong_undef_syms{$file}}, $name);
127 # Determine what ELF-level symbols are brought in by use of C-level
128 # symbols declared in the given header.
130 # The rules followed are heuristic and so may produce false positives
131 # and false negatives.
133 # * Weak undefined symbols are ignored; however, if a code path that
134 # references one (even just to check if its address is 0) is executed,
135 # that may conflict with a definition of that symbol in the user's
136 # program.
138 # * Strong undefined symbols are considered of signficance, but it is
139 # possible that (a) any standard library definition is weak, so can be
140 # overridden by the user's definition, and (b) the symbol is only used
141 # conditionally and not if the program is limited to standard
142 # functionality. (matherr is an example of such a false positive.)
144 # * If a symbol reference is only brought in by the user using a data
145 # symbol rather than a function from the standard library, this will
146 # not be detected.
148 # * If a symbol reference is only brought in by crt*.o or libgcc, this
149 # will not be detected.
151 # * If a symbol reference is only brought in through __builtin_foo in
152 # a standard macro being compiled to call foo, this will not be
153 # detected.
155 # * Header inclusions should be compiled several times with different
156 # options such as -O2, -D_FORTIFY_SOURCE and -D_FILE_OFFSET_BITS=64 to
157 # find out what symbols are undefined from such a compilation; this is
158 # not yet implemented.
160 # * This script finds symbols referenced through use of macros on the
161 # basis that if a macro calls an internal function, that function must
162 # also be declared in the header. However, the header might also
163 # declare implementation-namespace functions that are not called by
164 # any standard macro in the header, resulting in false positives for
165 # any symbols brought in only through use of those
166 # implementation-namespace functions.
168 # * Namespace issues can apply for dynamic linking as well as static
169 # linking, when a call is from one shared library to another or uses a
170 # PLT entry for a call within a shared library; such issues are only
171 # detected by this script if the same namespace issue applies for
172 # static linking.
174 @c_syms = list_exported_functions ("$CC $flags", $standard, $header, $tmpdir);
175 $cincfile = "$tmpdir/undef-$$.c";
176 $cincfile_o = "$tmpdir/undef-$$.o";
177 $cincfile_sym = "$tmpdir/undef-$$.sym";
178 open (CINCFILE, ">$cincfile") || die ("open $cincfile: $!\n");
179 print CINCFILE "#include <$header>\n";
180 foreach my $sym (sort @c_syms) {
181 print CINCFILE "void *__glibc_test_$sym = (void *) &$sym;\n";
183 close CINCFILE || die ("close $cincfile: $!\n");
184 system ("$CC $flags -D_ISOMAC $CFLAGS{$standard} -c $cincfile -o $cincfile_o")
185 && die ("compiling failed\n");
186 system ("LC_ALL=C $READELF -W -s $cincfile_o > $cincfile_sym")
187 && die ("readelf failed\n");
188 @elf_syms = list_syms ($cincfile_sym);
189 unlink ($cincfile) || die ("unlink $cincfile: $!\n");
190 unlink ($cincfile_o) || die ("unlink $cincfile_o: $!\n");
191 unlink ($cincfile_sym) || die ("unlink $cincfile_sym: $!\n");
193 %strong_seen = ();
194 %files_seen = ();
195 %all_undef = ();
196 %current_undef = ();
197 foreach my $sym (@elf_syms) {
198 my ($file, $name, $bind, $defined) = @$sym;
199 if ($bind eq "GLOBAL" && !$defined) {
200 $strong_seen{$name} = "[initial] $name";
201 $all_undef{$name} = "[initial] $name";
202 $current_undef{$name} = "[initial] $name";
206 while (%current_undef) {
207 %new_undef = ();
208 foreach my $sym (sort keys %current_undef) {
209 foreach my $file (@{$sym_objs{$sym}}) {
210 if (defined ($files_seen{$file})) {
211 next;
213 $files_seen{$file} = 1;
214 foreach my $ssym (@{$strong_syms{$file}}) {
215 if (!defined ($strong_seen{$ssym})) {
216 $strong_seen{$ssym} = "$current_undef{$sym} -> [$file] $ssym";
219 foreach my $usym (@{$strong_undef_syms{$file}}) {
220 if (!defined ($all_undef{$usym})) {
221 $all_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
222 $new_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
227 %current_undef = %new_undef;
230 $ret = 0;
231 foreach my $sym (sort keys %strong_seen) {
232 if ($sym =~ /^_/) {
233 next;
235 if (defined ($stdsyms{$sym})) {
236 next;
238 print "$strong_seen{$sym}\n";
239 $ret = 1;
242 exit $ret;