[PATCH] gold: Always resolve weak undefined in PIE to 0

H.J. Lu hjl.tools@gmail.com
Sat Aug 17 13:53:20 GMT 2024


Weak undefined symbol in PIE should be resolved to 0.

	PR gold/32071
	* symtab.cc (Symbol::final_value_is_known): Always resolve weak
	undefined in PIE to 0.
	* testsuite/Makefile.am: Add the weakundef test.
	* testsuite/Makefile.in: Regenerated.
	* testsuite/weakundef.c: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 gold/symtab.cc             |  9 ++++++++-
 gold/testsuite/Makefile.am |  5 +++++
 gold/testsuite/Makefile.in | 38 ++++++++++++++++++++++++++++++++++++--
 gold/testsuite/weakundef.c | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+), 3 deletions(-)
 create mode 100644 gold/testsuite/weakundef.c

diff --git a/gold/symtab.cc b/gold/symtab.cc
index 5857dd7b098..7349216f693 100644
--- a/gold/symtab.cc
+++ b/gold/symtab.cc
@@ -450,7 +450,14 @@ Symbol::final_value_is_known() const
        || parameters->options().relocatable())
       && !(this->type() == elfcpp::STT_TLS
            && parameters->options().pie()))
-    return false;
+    {
+      // Weak undefined in PIE is always resolved to 0.
+      if (this->is_weak_undefined()
+	  && parameters->options().pie())
+	return true;
+
+      return false;
+    }
 
   // If the symbol is not from an object file, and is not undefined,
   // then it is defined, and known.
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index a40f7624395..a71be33cad3 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -4531,3 +4531,8 @@ package_metadata_test.o: package_metadata_main.c
 package_metadata_test$(EXEEXT): package_metadata_test.o gcctestdir/ld
 	$(CXXLINK) package_metadata_test.o -Wl,--package-metadata='{"foo":"bar"}'
 	$(TEST_READELF) --notes $@ | grep -q '{"foo":"bar"}'
+
+check_PROGRAMS += weakundef
+weakundef_SOURCES = weakundef.c
+weakundef_CFLAGS = -fPIE
+weakundef_LDFLAGS = -pie
diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
index 78ca73acb17..f8617811556 100644
--- a/gold/testsuite/Makefile.in
+++ b/gold/testsuite/Makefile.in
@@ -110,7 +110,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 	$(am__EXEEXT_40) $(am__EXEEXT_41) $(am__EXEEXT_42) \
 	$(am__EXEEXT_43) $(am__EXEEXT_44) $(am__EXEEXT_45) \
 	$(am__EXEEXT_46) $(am__EXEEXT_47) $(am__EXEEXT_48) \
-	package_metadata_test$(EXEEXT)
+	package_metadata_test$(EXEEXT) weakundef$(EXEEXT)
 @NATIVE_OR_CROSS_LINKER_TRUE@am__append_1 = object_unittest \
 @NATIVE_OR_CROSS_LINKER_TRUE@	binary_unittest leb128_unittest \
 @NATIVE_OR_CROSS_LINKER_TRUE@	overflow_unittest
@@ -2278,6 +2278,11 @@ weak_unresolved_symbols_test_LDADD = $(LDADD)
 weak_unresolved_symbols_test_LINK = $(CXXLD) \
 	$(weak_unresolved_symbols_test_CXXFLAGS) $(CXXFLAGS) \
 	$(weak_unresolved_symbols_test_LDFLAGS) $(LDFLAGS) -o $@
+am_weakundef_OBJECTS = weakundef-weakundef.$(OBJEXT)
+weakundef_OBJECTS = $(am_weakundef_OBJECTS)
+weakundef_LDADD = $(LDADD)
+weakundef_LINK = $(CCLD) $(weakundef_CFLAGS) $(CFLAGS) \
+	$(weakundef_LDFLAGS) $(LDFLAGS) -o $@
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
 am__v_P_0 = false
@@ -2417,7 +2422,7 @@ SOURCES = $(libgoldtest_a_SOURCES) $(aarch64_pr23870_SOURCES) \
 	$(weak_alias_test_SOURCES) weak_plt.c $(weak_test_SOURCES) \
 	$(weak_undef_nonpic_test_SOURCES) $(weak_undef_test_SOURCES) \
 	$(weak_undef_test_2_SOURCES) \
-	$(weak_unresolved_symbols_test_SOURCES)
+	$(weak_unresolved_symbols_test_SOURCES) $(weakundef_SOURCES)
 am__can_run_installinfo = \
   case $$AM_UPDATE_INFO_DIR in \
     n|no|NO) false;; \
@@ -3480,6 +3485,9 @@ DEPENDENCIES = \
 @DEFAULT_TARGET_S390_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@SPLIT_DEFSYMS = --defsym __morestack=0x100 --defsym __morestack_non_split=0x200
 @DEFAULT_TARGET_X32_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@SPLIT_DEFSYMS = --defsym __morestack=0x100 --defsym __morestack_non_split=0x200
 @DEFAULT_TARGET_X86_64_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@SPLIT_DEFSYMS = --defsym __morestack=0x100 --defsym __morestack_non_split=0x200
+weakundef_SOURCES = weakundef.c
+weakundef_CFLAGS = -fPIE
+weakundef_LDFLAGS = -pie
 all: $(BUILT_SOURCES)
 	$(MAKE) $(AM_MAKEFLAGS) all-am
 
@@ -4822,6 +4830,10 @@ weak_unresolved_symbols_test$(EXEEXT): $(weak_unresolved_symbols_test_OBJECTS) $
 	@rm -f weak_unresolved_symbols_test$(EXEEXT)
 	$(AM_V_CXXLD)$(weak_unresolved_symbols_test_LINK) $(weak_unresolved_symbols_test_OBJECTS) $(weak_unresolved_symbols_test_LDADD) $(LIBS)
 
+weakundef$(EXEEXT): $(weakundef_OBJECTS) $(weakundef_DEPENDENCIES) $(EXTRA_weakundef_DEPENDENCIES) 
+	@rm -f weakundef$(EXEEXT)
+	$(AM_V_CCLD)$(weakundef_LINK) $(weakundef_OBJECTS) $(weakundef_LDADD) $(LIBS)
+
 mostlyclean-compile:
 	-rm -f *.$(OBJEXT)
 
@@ -4992,6 +5004,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_undef_test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_undef_test_2.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_unresolved_symbols_test-weak_unresolved_symbols_test.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weakundef-weakundef.Po@am__quote@
 
 .c.o:
 @am__fastdepCC_TRUE@	$(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
@@ -5273,6 +5286,20 @@ pr20308e_test-pr20308_main.obj: pr20308_main.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(pr20308e_test_CFLAGS) $(CFLAGS) -c -o pr20308e_test-pr20308_main.obj `if test -f 'pr20308_main.c'; then $(CYGPATH_W) 'pr20308_main.c'; else $(CYGPATH_W) '$(srcdir)/pr20308_main.c'; fi`
 
+weakundef-weakundef.o: weakundef.c
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weakundef_CFLAGS) $(CFLAGS) -MT weakundef-weakundef.o -MD -MP -MF $(DEPDIR)/weakundef-weakundef.Tpo -c -o weakundef-weakundef.o `test -f 'weakundef.c' || echo '$(srcdir)/'`weakundef.c
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/weakundef-weakundef.Tpo $(DEPDIR)/weakundef-weakundef.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='weakundef.c' object='weakundef-weakundef.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weakundef_CFLAGS) $(CFLAGS) -c -o weakundef-weakundef.o `test -f 'weakundef.c' || echo '$(srcdir)/'`weakundef.c
+
+weakundef-weakundef.obj: weakundef.c
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weakundef_CFLAGS) $(CFLAGS) -MT weakundef-weakundef.obj -MD -MP -MF $(DEPDIR)/weakundef-weakundef.Tpo -c -o weakundef-weakundef.obj `if test -f 'weakundef.c'; then $(CYGPATH_W) 'weakundef.c'; else $(CYGPATH_W) '$(srcdir)/weakundef.c'; fi`
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/weakundef-weakundef.Tpo $(DEPDIR)/weakundef-weakundef.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='weakundef.c' object='weakundef-weakundef.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weakundef_CFLAGS) $(CFLAGS) -c -o weakundef-weakundef.obj `if test -f 'weakundef.c'; then $(CYGPATH_W) 'weakundef.c'; else $(CYGPATH_W) '$(srcdir)/weakundef.c'; fi`
+
 .cc.o:
 @am__fastdepCXX_TRUE@	$(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
 @am__fastdepCXX_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
@@ -7923,6 +7950,13 @@ package_metadata_test.log: package_metadata_test$(EXEEXT)
 	--log-file $$b.log --trs-file $$b.trs \
 	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
 	"$$tst" $(AM_TESTS_FD_REDIRECT)
+weakundef.log: weakundef$(EXEEXT)
+	@p='weakundef$(EXEEXT)'; \
+	b='weakundef'; \
+	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
+	--log-file $$b.log --trs-file $$b.trs \
+	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
+	"$$tst" $(AM_TESTS_FD_REDIRECT)
 .test.log:
 	@p='$<'; \
 	$(am__set_b); \
diff --git a/gold/testsuite/weakundef.c b/gold/testsuite/weakundef.c
new file mode 100644
index 00000000000..560ba0b2bd1
--- /dev/null
+++ b/gold/testsuite/weakundef.c
@@ -0,0 +1,35 @@
+/* weakundef.c -- test weak undefined symbol in PIE.
+
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   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.  */
+
+// Weak undefined symbol in PIE should be resolved to 0.
+
+#include <stdlib.h>
+
+extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
+
+int
+main (void)
+{
+  if (&undefined != NULL)
+    abort ();
+
+  return 0;
+}
-- 
2.46.0



More information about the Binutils mailing list