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-2017 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 17576: stdin, stdout, stderr only reserved with external
44 # linkage when stdio.h included (and possibly not then), not
47 # * Bug 18442: re_syntax_options wrongly brought in by regcomp and
50 @whitelist = qw(stdin stdout stderr re_syntax_options);
51 foreach my $sym (@whitelist) {
55 # Return information about GLOBAL and WEAK symbols listed in readelf
59 open (SYMS
, "<$syms_file") || die ("open $syms_file: $!\n");
60 my ($file) = $syms_file;
70 # Architecture-specific st_other bits appear inside [] and disrupt
71 # the format of readelf output.
73 my (@fields) = split (/\s+/, $_);
77 my ($bind) = $fields[4];
78 my ($ndx) = $fields[6];
79 my ($sym) = $fields[7];
80 if ($bind ne "GLOBAL" && $bind ne "WEAK") {
83 if ($sym !~ /^\w+$/) {
86 push (@ret, [$file, $sym, $bind, $ndx ne "UND"]);
88 close (SYMS
) || die ("close $syms_file: $!\n");
92 # Load information about GLOBAL and WEAK symbols defined or used in
93 # the standard libraries.
94 # Symbols from a given object, except for weak defined symbols.
96 # Strong undefined symbols from a given object.
97 %strong_undef_syms = ();
98 # Objects defining a given symbol (strongly or weakly).
100 @sym_data = list_syms
($libsyms_file);
101 foreach my $sym (@sym_data) {
102 my ($file, $name, $bind, $defined) = @
$sym;
104 if (!defined ($sym_objs{$name})) {
105 $sym_objs{$name} = [];
107 push (@
{$sym_objs{$name}}, $file);
109 if ($bind eq "GLOBAL" || !$defined) {
110 if (!defined ($seen_syms{$file})) {
111 $seen_syms{$file} = [];
113 push (@
{$seen_syms{$file}}, $name);
115 if ($bind eq "GLOBAL" && !$defined) {
116 if (!defined ($strong_undef_syms{$file})) {
117 $strong_undef_syms{$file} = [];
119 push (@
{$strong_undef_syms{$file}}, $name);
123 # Determine what ELF-level symbols are brought in by use of C-level
124 # symbols declared in the given header.
126 # The rules followed are heuristic and so may produce false positives
127 # and false negatives.
129 # * All undefined symbols are considered of signficance, but it is
130 # possible that (a) any standard library definition is weak, so can be
131 # overridden by the user's definition, and (b) the symbol is only used
132 # conditionally and not if the program is limited to standard
135 # * If a symbol reference is only brought in by the user using a data
136 # symbol rather than a function from the standard library, this will
139 # * If a symbol reference is only brought in by crt*.o or libgcc, this
140 # will not be detected.
142 # * If a symbol reference is only brought in through __builtin_foo in
143 # a standard macro being compiled to call foo, this will not be
146 # * Header inclusions should be compiled several times with different
147 # options such as -O2, -D_FORTIFY_SOURCE and -D_FILE_OFFSET_BITS=64 to
148 # find out what symbols are undefined from such a compilation; this is
149 # not yet implemented.
151 # * This script finds symbols referenced through use of macros on the
152 # basis that if a macro calls an internal function, that function must
153 # also be declared in the header. However, the header might also
154 # declare implementation-namespace functions that are not called by
155 # any standard macro in the header, resulting in false positives for
156 # any symbols brought in only through use of those
157 # implementation-namespace functions.
159 # * Namespace issues can apply for dynamic linking as well as static
160 # linking, when a call is from one shared library to another or uses a
161 # PLT entry for a call within a shared library; such issues are only
162 # detected by this script if the same namespace issue applies for
165 @c_syms = list_exported_functions
("$CC $flags", $standard, $header, $tmpdir);
166 $cincfile = "$tmpdir/undef-$$.c";
167 $cincfile_o = "$tmpdir/undef-$$.o";
168 $cincfile_sym = "$tmpdir/undef-$$.sym";
169 open (CINCFILE
, ">$cincfile") || die ("open $cincfile: $!\n");
170 print CINCFILE
"#include <$header>\n";
171 foreach my $sym (sort @c_syms) {
172 print CINCFILE
"void *__glibc_test_$sym = (void *) &$sym;\n";
174 close CINCFILE
|| die ("close $cincfile: $!\n");
175 system ("$CC $flags -D_ISOMAC $CFLAGS{$standard} -c $cincfile -o $cincfile_o")
176 && die ("compiling failed\n");
177 system ("LC_ALL=C $READELF -W -s $cincfile_o > $cincfile_sym")
178 && die ("readelf failed\n");
179 @elf_syms = list_syms
($cincfile_sym);
180 unlink ($cincfile) || die ("unlink $cincfile: $!\n");
181 unlink ($cincfile_o) || die ("unlink $cincfile_o: $!\n");
182 unlink ($cincfile_sym) || die ("unlink $cincfile_sym: $!\n");
188 foreach my $sym (@elf_syms) {
189 my ($file, $name, $bind, $defined) = @
$sym;
190 if ($bind eq "GLOBAL" && !$defined) {
191 $seen_where{$name} = "[initial] $name";
192 $all_undef{$name} = "[initial] $name";
193 $current_undef{$name} = "[initial] $name";
197 while (%current_undef) {
199 foreach my $sym (sort keys %current_undef) {
200 foreach my $file (@
{$sym_objs{$sym}}) {
201 if (defined ($files_seen{$file})) {
204 $files_seen{$file} = 1;
205 foreach my $ssym (@
{$seen_syms{$file}}) {
206 if (!defined ($seen_where{$ssym})) {
207 $seen_where{$ssym} = "$current_undef{$sym} -> [$file] $ssym";
210 foreach my $usym (@
{$strong_undef_syms{$file}}) {
211 if (!defined ($all_undef{$usym})) {
212 $all_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
213 $new_undef{$usym} = "$current_undef{$sym} -> [$file] $usym";
218 %current_undef = %new_undef;
222 foreach my $sym (sort keys %seen_where) {
226 if (defined ($stdsyms{$sym})) {
229 print "$seen_where{$sym}\n";