This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
gold patch committed: Warn about undefined protected symbols
- From: Ian Lance Taylor <iant at google dot com>
- To: binutils at sourceware dot org
- Date: Tue, 12 Jan 2010 11:13:36 -0800
- Subject: gold patch committed: Warn about undefined protected symbols
It's an error to refer to a protected/hidden/internal symbol which is
not defined locally. The dynamic linker will not be able to resolve
such a reference. I committed this patch to gold to make it gives
error about such cases.
Ian
2010-01-12 Ian Lance Taylor <iant@google.com>
* target-reloc.h (visibility_error): New inline function.
(relocate_section): Call visibility_error.
* testsuite/Makefile.am (check_DATA): Add protected_3.err.
(MOSTLYCLEANFILES): Likewise.
(protected_4_pic.o, protected_3.err): New targets.
* testsuite/protected_4.cc: New file.
Index: target-reloc.h
===================================================================
RCS file: /cvs/src/src/gold/target-reloc.h,v
retrieving revision 1.38
diff -p -u -r1.38 target-reloc.h
--- target-reloc.h 9 Jan 2010 00:13:48 -0000 1.38
+++ target-reloc.h 12 Jan 2010 19:11:48 -0000
@@ -144,6 +144,31 @@ get_comdat_behavior(const char* name)
return CB_WARNING;
}
+// Give an error for a symbol with non-default visibility which is not
+// defined locally.
+
+inline void
+visibility_error(const Symbol* sym)
+{
+ const char* v;
+ switch (sym->visibility())
+ {
+ case elfcpp::STV_INTERNAL:
+ v = _("internal");
+ break;
+ case elfcpp::STV_HIDDEN:
+ v = _("hidden");
+ break;
+ case elfcpp::STV_PROTECTED:
+ v = _("protected");
+ break;
+ default:
+ gold_unreachable();
+ }
+ gold_error(_("%s symbol '%s' is not defined locally"),
+ v, sym->name());
+}
+
// This function implements the generic part of relocation processing.
// The template parameter Relocate must be a class type which provides
// a single function, relocate(), which implements the machine
@@ -323,6 +348,10 @@ relocate_section(
&& (!parameters->options().shared() // -shared
|| parameters->options().defs())) // -z defs
gold_undefined_symbol_at_location(sym, relinfo, i, offset);
+ else if (sym != NULL
+ && sym->visibility() != elfcpp::STV_DEFAULT
+ && (sym->is_undefined() || sym->is_from_dynobj()))
+ visibility_error(sym);
if (sym != NULL && sym->has_warning())
relinfo->symtab->issue_warning(sym, relinfo, i, offset);
Index: testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.117
diff -p -u -r1.117 Makefile.am
--- testsuite/Makefile.am 11 Jan 2010 18:34:48 -0000 1.117
+++ testsuite/Makefile.am 12 Jan 2010 19:11:48 -0000
@@ -973,6 +973,18 @@ protected_2_DEPENDENCIES = gcctestdir/ld
protected_2_LDFLAGS = -Bgcctestdir/ -Wl,-R,.
protected_2_LDADD = protected_1.so
+check_DATA += protected_3.err
+MOSTLYCLEANFILES += protected_3.err
+protected_4_pic.o: protected_4.cc
+ $(CXXCOMPILE) -c -fpic -o $@ $<
+protected_3.err: protected_4_pic.o gcctestdir/ld
+ @echo $(CXXLINK) -Bgcctestdir/ -shared -o protected_4.so protected_4_pic.o "2>$@"
+ @if $(CXXLINK) -Bgcctestdir/ -shared -o protected_4.so protected_4_pic.o 2>$@; then \
+ echo 1>&2 "Link of protected_4.so should have failed"; \
+ rm -f $@; \
+ exit 1; \
+ fi
+
check_PROGRAMS += relro_test
relro_test_SOURCES = relro_test_main.cc
relro_test_DEPENDENCIES = gcctestdir/ld relro_test.so
Index: testsuite/protected_4.cc
===================================================================
RCS file: testsuite/protected_4.cc
diff -N testsuite/protected_4.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/protected_4.cc 12 Jan 2010 19:11:49 -0000
@@ -0,0 +1,32 @@
+// protected_4.cc -- a test case for gold
+
+// Copyright 2010 Free Software Foundation, Inc.
+// Written by Ian Lance Taylor <iant@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+// The function f1 is protected but not defined.
+
+int
+f1() __attribute__ ((__visibility__ ("protected")));
+
+int
+f2()
+{
+ return f1();
+}