[v3] gold: Always resolve non-default weak undefined to 0

Message ID 20240821150641.113102-1-hjl.tools@gmail.com
State Committed
Headers
Series [v3] gold: Always resolve non-default weak undefined to 0 |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed

Commit Message

H.J. Lu Aug. 21, 2024, 3:06 p.m. UTC
  Non-default weak undefined symbols in executable and shared library are
always resolved to 0 at runtime and don't need dynamic relocation.

Tested on i686, x86-64, powerpc64le and aarch64_be.

	PR gold/32071
	* symtab.cc (Symbol::final_value_is_known): Always resolve
	non-default weak undefined symbol in executable and shared library
	to 0 at runtime.
	* symtab.h (Symbol::needs_dynamic_reloc): Return false for
	non-default weak undefined symbol in executable and shared library.
	* testsuite/Makefile.am: Add weak_undef_test_3 and
	weak_undef_test_4 tests.
	* testsuite/Makefile.in: Regenerated.
	* testsuite/weak_undef_lib_4.c: New file.
	* testsuite/weak_undef_test_3.c: Likewise.
	* testsuite/weak_undef_test_4.c: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 gold/symtab.cc                     | 11 ++++-
 gold/symtab.h                      |  7 +++
 gold/testsuite/Makefile.am         | 17 +++++++
 gold/testsuite/Makefile.in         | 75 ++++++++++++++++++++++++++++--
 gold/testsuite/weak_undef_lib_4.c  | 40 ++++++++++++++++
 gold/testsuite/weak_undef_test_3.c | 40 ++++++++++++++++
 gold/testsuite/weak_undef_test_4.c | 29 ++++++++++++
 7 files changed, 214 insertions(+), 5 deletions(-)
 create mode 100644 gold/testsuite/weak_undef_lib_4.c
 create mode 100644 gold/testsuite/weak_undef_test_3.c
 create mode 100644 gold/testsuite/weak_undef_test_4.c
  

Comments

H.J. Lu Aug. 25, 2024, 2:16 p.m. UTC | #1
On Wed, Aug 21, 2024 at 8:06 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Non-default weak undefined symbols in executable and shared library are
> always resolved to 0 at runtime and don't need dynamic relocation.
>
> Tested on i686, x86-64, powerpc64le and aarch64_be.

I tested it on aarch64, not aarch64_be.  Any comments or objections?

Thanks.

>
>         PR gold/32071
>         * symtab.cc (Symbol::final_value_is_known): Always resolve
>         non-default weak undefined symbol in executable and shared library
>         to 0 at runtime.
>         * symtab.h (Symbol::needs_dynamic_reloc): Return false for
>         non-default weak undefined symbol in executable and shared library.
>         * testsuite/Makefile.am: Add weak_undef_test_3 and
>         weak_undef_test_4 tests.
>         * testsuite/Makefile.in: Regenerated.
>         * testsuite/weak_undef_lib_4.c: New file.
>         * testsuite/weak_undef_test_3.c: Likewise.
>         * testsuite/weak_undef_test_4.c: Likewise.
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  gold/symtab.cc                     | 11 ++++-
>  gold/symtab.h                      |  7 +++
>  gold/testsuite/Makefile.am         | 17 +++++++
>  gold/testsuite/Makefile.in         | 75 ++++++++++++++++++++++++++++--
>  gold/testsuite/weak_undef_lib_4.c  | 40 ++++++++++++++++
>  gold/testsuite/weak_undef_test_3.c | 40 ++++++++++++++++
>  gold/testsuite/weak_undef_test_4.c | 29 ++++++++++++
>  7 files changed, 214 insertions(+), 5 deletions(-)
>  create mode 100644 gold/testsuite/weak_undef_lib_4.c
>  create mode 100644 gold/testsuite/weak_undef_test_3.c
>  create mode 100644 gold/testsuite/weak_undef_test_4.c
>
> diff --git a/gold/symtab.cc b/gold/symtab.cc
> index 5857dd7b098..91b551cae1d 100644
> --- a/gold/symtab.cc
> +++ b/gold/symtab.cc
> @@ -450,7 +450,16 @@ Symbol::final_value_is_known() const
>         || parameters->options().relocatable())
>        && !(this->type() == elfcpp::STT_TLS
>             && parameters->options().pie()))
> -    return false;
> +    {
> +      // Non-default weak undefined symbols in executable and shared
> +      // library are always resolved to 0 at runtime.
> +      if (this->visibility() != elfcpp::STV_DEFAULT
> +         && this->is_weak_undefined()
> +         && !parameters->options().relocatable())
> +       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/symtab.h b/gold/symtab.h
> index 0a1f6a63a76..9c255599d69 100644
> --- a/gold/symtab.h
> +++ b/gold/symtab.h
> @@ -709,6 +709,13 @@ class Symbol
>      if (this->is_absolute())
>        return false;
>
> +    // Non-default weak undefined symbols in executable and shared
> +    // library are always resolved to 0 at runtime.
> +    if (this->visibility() != elfcpp::STV_DEFAULT
> +       && this->is_weak_undefined()
> +       && !parameters->options().relocatable())
> +      return false;
> +
>      // An absolute reference within a position-independent output file
>      // will need a dynamic relocation.
>      if ((flags & ABSOLUTE_REF)
> diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
> index a40f7624395..c1e7e07f431 100644
> --- a/gold/testsuite/Makefile.am
> +++ b/gold/testsuite/Makefile.am
> @@ -865,6 +865,23 @@ weak_undef_file3.o: weak_undef_file3.cc
>  weak_undef_file4.o: weak_undef_file4.cc
>         $(CXXCOMPILE) -c -o $@ $<
>
> +check_PROGRAMS += weak_undef_test_3
> +weak_undef_test_3_SOURCES = weak_undef_test_3.c
> +weak_undef_test_3_DEPENDENCIES = gcctestdir/ld
> +weak_undef_test_3_CFLAGS = -fPIE
> +weak_undef_test_3_LDFLAGS = -pie
> +
> +check_PROGRAMS += weak_undef_test_4
> +weak_undef_test_4_SOURCES = weak_undef_test_4.c
> +weak_undef_test_4_DEPENDENCIES = gcctestdir/ld weak_undef_lib_4.so
> +weak_undef_test_4_LDADD = weak_undef_lib_4.so
> +weak_undef_test_4_LDFLAGS =  -Wl,-R,.
> +MOSTLYCLEANFILES += weak_undef_lib_4.o weak_undef_lib_4.so
> +weak_undef_lib_4.o:  weak_undef_lib_4.c
> +       $(COMPILE) -fPIC -c -o $@ $<
> +weak_undef_lib_4.so: gcctestdir/ld weak_undef_lib_4.o
> +       $(LINK) -shared -o $@ weak_undef_lib_4.o
> +
>  if FN_PTRS_IN_SO_WITHOUT_PIC
>  check_PROGRAMS += weak_undef_nonpic_test
>  MOSTLYCLEANFILES += alt/weak_undef_lib_nonpic.so
> diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
> index 78ca73acb17..6df050f4a01 100644
> --- a/gold/testsuite/Makefile.in
> +++ b/gold/testsuite/Makefile.in
> @@ -211,7 +211,9 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ eh_test_2.sects \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ two_file_shared.dbg \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ alt/weak_undef_lib.so \
> -@GCC_TRUE@@NATIVE_LINKER_TRUE@ libweak_undef_2.a
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ libweak_undef_2.a \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_lib_4.o \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_lib_4.so
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_5 = icf_virtual_function_folding_test \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ large_symbol_alignment \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ basic_test basic_pic_test \
> @@ -269,7 +271,9 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
>  @GCC_TRUE@@HAVE_STATIC_TRUE@@NATIVE_LINKER_TRUE@am__append_16 = exception_static_test
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_17 = weak_test \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test \
> -@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2 \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_3 \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_4
>  @GCC_FALSE@weak_test_DEPENDENCIES =
>  @NATIVE_LINKER_FALSE@weak_test_DEPENDENCIES =
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_18 = weak_undef_nonpic_test
> @@ -1255,7 +1259,9 @@ libgoldtest_a_OBJECTS = $(am_libgoldtest_a_OBJECTS)
>  @GCC_TRUE@@HAVE_STATIC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_13 = exception_static_test$(EXEEXT)
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_14 = weak_test$(EXEEXT) \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test$(EXEEXT) \
> -@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2$(EXEEXT)
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2$(EXEEXT) \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_3$(EXEEXT) \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_4$(EXEEXT)
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_15 = weak_undef_nonpic_test$(EXEEXT)
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_16 =  \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_alias_test$(EXEEXT) \
> @@ -2271,6 +2277,16 @@ weak_undef_test_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
>  weak_undef_test_2_OBJECTS = $(am_weak_undef_test_2_OBJECTS)
>  weak_undef_test_2_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
>         $(weak_undef_test_2_LDFLAGS) $(LDFLAGS) -o $@
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_undef_test_3_OBJECTS = weak_undef_test_3-weak_undef_test_3.$(OBJEXT)
> +weak_undef_test_3_OBJECTS = $(am_weak_undef_test_3_OBJECTS)
> +weak_undef_test_3_LDADD = $(LDADD)
> +weak_undef_test_3_LINK = $(CCLD) $(weak_undef_test_3_CFLAGS) $(CFLAGS) \
> +       $(weak_undef_test_3_LDFLAGS) $(LDFLAGS) -o $@
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_undef_test_4_OBJECTS =  \
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_4.$(OBJEXT)
> +weak_undef_test_4_OBJECTS = $(am_weak_undef_test_4_OBJECTS)
> +weak_undef_test_4_LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
> +       $(weak_undef_test_4_LDFLAGS) $(LDFLAGS) -o $@
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_unresolved_symbols_test_OBJECTS = weak_unresolved_symbols_test-weak_unresolved_symbols_test.$(OBJEXT)
>  weak_unresolved_symbols_test_OBJECTS =  \
>         $(am_weak_unresolved_symbols_test_OBJECTS)
> @@ -2416,7 +2432,8 @@ SOURCES = $(libgoldtest_a_SOURCES) $(aarch64_pr23870_SOURCES) \
>         $(ver_test_8_SOURCES) $(ver_test_9_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_undef_test_2_SOURCES) $(weak_undef_test_3_SOURCES) \
> +       $(weak_undef_test_4_SOURCES) \
>         $(weak_unresolved_symbols_test_SOURCES)
>  am__can_run_installinfo = \
>    case $$AM_UPDATE_INFO_DIR in \
> @@ -3143,6 +3160,14 @@ DEPENDENCIES = \
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_DEPENDENCIES = gcctestdir/ld libweak_undef_2.a
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_LDFLAGS = -u weak_undef_2
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_LDADD = -L . -lweak_undef_2
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_SOURCES = weak_undef_test_3.c
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_DEPENDENCIES = gcctestdir/ld
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_CFLAGS = -fPIE
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_LDFLAGS = -pie
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_SOURCES = weak_undef_test_4.c
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_DEPENDENCIES = gcctestdir/ld weak_undef_lib_4.so
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_LDADD = weak_undef_lib_4.so
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_LDFLAGS = -Wl,-R,.
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_SOURCES = weak_undef_test.cc
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_DEPENDENCIES = gcctestdir/ld weak_undef_lib_nonpic.so alt/weak_undef_lib_nonpic.so
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_LDFLAGS = -Wl,-R,alt
> @@ -4818,6 +4843,14 @@ weak_undef_test_2$(EXEEXT): $(weak_undef_test_2_OBJECTS) $(weak_undef_test_2_DEP
>         @rm -f weak_undef_test_2$(EXEEXT)
>         $(AM_V_CXXLD)$(weak_undef_test_2_LINK) $(weak_undef_test_2_OBJECTS) $(weak_undef_test_2_LDADD) $(LIBS)
>
> +weak_undef_test_3$(EXEEXT): $(weak_undef_test_3_OBJECTS) $(weak_undef_test_3_DEPENDENCIES) $(EXTRA_weak_undef_test_3_DEPENDENCIES)
> +       @rm -f weak_undef_test_3$(EXEEXT)
> +       $(AM_V_CCLD)$(weak_undef_test_3_LINK) $(weak_undef_test_3_OBJECTS) $(weak_undef_test_3_LDADD) $(LIBS)
> +
> +weak_undef_test_4$(EXEEXT): $(weak_undef_test_4_OBJECTS) $(weak_undef_test_4_DEPENDENCIES) $(EXTRA_weak_undef_test_4_DEPENDENCIES)
> +       @rm -f weak_undef_test_4$(EXEEXT)
> +       $(AM_V_CCLD)$(weak_undef_test_4_LINK) $(weak_undef_test_4_OBJECTS) $(weak_undef_test_4_LDADD) $(LIBS)
> +
>  weak_unresolved_symbols_test$(EXEEXT): $(weak_unresolved_symbols_test_OBJECTS) $(weak_unresolved_symbols_test_DEPENDENCIES) $(EXTRA_weak_unresolved_symbols_test_DEPENDENCIES)
>         @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)
> @@ -4991,6 +5024,8 @@ distclean-compile:
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_test.Po@am__quote@
>  @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_undef_test_3-weak_undef_test_3.Po@am__quote@
> +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_undef_test_4.Po@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_unresolved_symbols_test-weak_unresolved_symbols_test.Po@am__quote@
>
>  .c.o:
> @@ -5273,6 +5308,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`
>
> +weak_undef_test_3-weak_undef_test_3.o: weak_undef_test_3.c
> +@am__fastdepCC_TRUE@   $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -MT weak_undef_test_3-weak_undef_test_3.o -MD -MP -MF $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo -c -o weak_undef_test_3-weak_undef_test_3.o `test -f 'weak_undef_test_3.c' || echo '$(srcdir)/'`weak_undef_test_3.c
> +@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Po
> +@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='weak_undef_test_3.c' object='weak_undef_test_3-weak_undef_test_3.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) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -c -o weak_undef_test_3-weak_undef_test_3.o `test -f 'weak_undef_test_3.c' || echo '$(srcdir)/'`weak_undef_test_3.c
> +
> +weak_undef_test_3-weak_undef_test_3.obj: weak_undef_test_3.c
> +@am__fastdepCC_TRUE@   $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -MT weak_undef_test_3-weak_undef_test_3.obj -MD -MP -MF $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo -c -o weak_undef_test_3-weak_undef_test_3.obj `if test -f 'weak_undef_test_3.c'; then $(CYGPATH_W) 'weak_undef_test_3.c'; else $(CYGPATH_W) '$(srcdir)/weak_undef_test_3.c'; fi`
> +@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Po
> +@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='weak_undef_test_3.c' object='weak_undef_test_3-weak_undef_test_3.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) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -c -o weak_undef_test_3-weak_undef_test_3.obj `if test -f 'weak_undef_test_3.c'; then $(CYGPATH_W) 'weak_undef_test_3.c'; else $(CYGPATH_W) '$(srcdir)/weak_undef_test_3.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
> @@ -6950,6 +6999,20 @@ weak_undef_test_2.log: weak_undef_test_2$(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)
> +weak_undef_test_3.log: weak_undef_test_3$(EXEEXT)
> +       @p='weak_undef_test_3$(EXEEXT)'; \
> +       b='weak_undef_test_3'; \
> +       $(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)
> +weak_undef_test_4.log: weak_undef_test_4$(EXEEXT)
> +       @p='weak_undef_test_4$(EXEEXT)'; \
> +       b='weak_undef_test_4'; \
> +       $(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)
>  weak_undef_nonpic_test.log: weak_undef_nonpic_test$(EXEEXT)
>         @p='weak_undef_nonpic_test$(EXEEXT)'; \
>         b='weak_undef_nonpic_test'; \
> @@ -8389,6 +8452,10 @@ uninstall-am:
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -o $@ $<
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file4.o: weak_undef_file4.cc
>  @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -o $@ $<
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_lib_4.o:  weak_undef_lib_4.c
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(COMPILE) -fPIC -c -o $@ $<
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_lib_4.so: gcctestdir/ld weak_undef_lib_4.o
> +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -shared -o $@ weak_undef_lib_4.o
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file1_nonpic.o: weak_undef_file1.cc
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -o $@ $<
>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file2_nonpic.o: weak_undef_file2.cc
> diff --git a/gold/testsuite/weak_undef_lib_4.c b/gold/testsuite/weak_undef_lib_4.c
> new file mode 100644
> index 00000000000..f8609c64745
> --- /dev/null
> +++ b/gold/testsuite/weak_undef_lib_4.c
> @@ -0,0 +1,40 @@
> +/* weak_undef_lib_4.c -- test non-default weak undefined symbol in DSO.
> +
> +   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.  */
> +
> +/* Non-default weak undefined symbol in DSO should be resolved to 0 at
> +   runtime.  */
> +
> +#include <stdlib.h>
> +
> +extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
> +extern void protected (void) __attribute__((visibility("protected")))  __attribute__((weak));
> +
> +extern void foo (void);
> +
> +void
> +foo (void)
> +{
> +  if (&undefined != NULL)
> +    abort ();
> +
> +  if (&protected != NULL)
> +    abort ();
> +}
> diff --git a/gold/testsuite/weak_undef_test_3.c b/gold/testsuite/weak_undef_test_3.c
> new file mode 100644
> index 00000000000..a7b7750c03f
> --- /dev/null
> +++ b/gold/testsuite/weak_undef_test_3.c
> @@ -0,0 +1,40 @@
> +/* weak_undef_test_3.c -- test non-default 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.  */
> +
> +/* Non-default weak undefined symbol in PIE should be resolved to 0 at
> +   runtime.  */
> +
> +#include <stdlib.h>
> +
> +extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
> +extern void protected (void) __attribute__((visibility("protected")))  __attribute__((weak));
> +
> +int
> +main (void)
> +{
> +  if (&undefined != NULL)
> +    abort ();
> +
> +  if (&protected != NULL)
> +    abort ();
> +
> +  return 0;
> +}
> diff --git a/gold/testsuite/weak_undef_test_4.c b/gold/testsuite/weak_undef_test_4.c
> new file mode 100644
> index 00000000000..ab2f8bc224d
> --- /dev/null
> +++ b/gold/testsuite/weak_undef_test_4.c
> @@ -0,0 +1,29 @@
> +/* weak_undef_test_4.c -- test non-default weak undefined symbol in DSO.
> +
> +   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.  */
> +
> +extern void foo (void);
> +
> +int
> +main (void)
> +{
> +  foo ();
> +  return 0;
> +}
> --
> 2.46.0
>
  
H.J. Lu Aug. 28, 2024, 12:35 p.m. UTC | #2
On Sun, Aug 25, 2024 at 7:16 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, Aug 21, 2024 at 8:06 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > Non-default weak undefined symbols in executable and shared library are
> > always resolved to 0 at runtime and don't need dynamic relocation.
> >
> > Tested on i686, x86-64, powerpc64le and aarch64_be.
>
> I tested it on aarch64, not aarch64_be.  Any comments or objections?

I am checking it in this weekend.

> Thanks.
>
> >
> >         PR gold/32071
> >         * symtab.cc (Symbol::final_value_is_known): Always resolve
> >         non-default weak undefined symbol in executable and shared library
> >         to 0 at runtime.
> >         * symtab.h (Symbol::needs_dynamic_reloc): Return false for
> >         non-default weak undefined symbol in executable and shared library.
> >         * testsuite/Makefile.am: Add weak_undef_test_3 and
> >         weak_undef_test_4 tests.
> >         * testsuite/Makefile.in: Regenerated.
> >         * testsuite/weak_undef_lib_4.c: New file.
> >         * testsuite/weak_undef_test_3.c: Likewise.
> >         * testsuite/weak_undef_test_4.c: Likewise.
> >
> > Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> > ---
> >  gold/symtab.cc                     | 11 ++++-
> >  gold/symtab.h                      |  7 +++
> >  gold/testsuite/Makefile.am         | 17 +++++++
> >  gold/testsuite/Makefile.in         | 75 ++++++++++++++++++++++++++++--
> >  gold/testsuite/weak_undef_lib_4.c  | 40 ++++++++++++++++
> >  gold/testsuite/weak_undef_test_3.c | 40 ++++++++++++++++
> >  gold/testsuite/weak_undef_test_4.c | 29 ++++++++++++
> >  7 files changed, 214 insertions(+), 5 deletions(-)
> >  create mode 100644 gold/testsuite/weak_undef_lib_4.c
> >  create mode 100644 gold/testsuite/weak_undef_test_3.c
> >  create mode 100644 gold/testsuite/weak_undef_test_4.c
> >
> > diff --git a/gold/symtab.cc b/gold/symtab.cc
> > index 5857dd7b098..91b551cae1d 100644
> > --- a/gold/symtab.cc
> > +++ b/gold/symtab.cc
> > @@ -450,7 +450,16 @@ Symbol::final_value_is_known() const
> >         || parameters->options().relocatable())
> >        && !(this->type() == elfcpp::STT_TLS
> >             && parameters->options().pie()))
> > -    return false;
> > +    {
> > +      // Non-default weak undefined symbols in executable and shared
> > +      // library are always resolved to 0 at runtime.
> > +      if (this->visibility() != elfcpp::STV_DEFAULT
> > +         && this->is_weak_undefined()
> > +         && !parameters->options().relocatable())
> > +       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/symtab.h b/gold/symtab.h
> > index 0a1f6a63a76..9c255599d69 100644
> > --- a/gold/symtab.h
> > +++ b/gold/symtab.h
> > @@ -709,6 +709,13 @@ class Symbol
> >      if (this->is_absolute())
> >        return false;
> >
> > +    // Non-default weak undefined symbols in executable and shared
> > +    // library are always resolved to 0 at runtime.
> > +    if (this->visibility() != elfcpp::STV_DEFAULT
> > +       && this->is_weak_undefined()
> > +       && !parameters->options().relocatable())
> > +      return false;
> > +
> >      // An absolute reference within a position-independent output file
> >      // will need a dynamic relocation.
> >      if ((flags & ABSOLUTE_REF)
> > diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
> > index a40f7624395..c1e7e07f431 100644
> > --- a/gold/testsuite/Makefile.am
> > +++ b/gold/testsuite/Makefile.am
> > @@ -865,6 +865,23 @@ weak_undef_file3.o: weak_undef_file3.cc
> >  weak_undef_file4.o: weak_undef_file4.cc
> >         $(CXXCOMPILE) -c -o $@ $<
> >
> > +check_PROGRAMS += weak_undef_test_3
> > +weak_undef_test_3_SOURCES = weak_undef_test_3.c
> > +weak_undef_test_3_DEPENDENCIES = gcctestdir/ld
> > +weak_undef_test_3_CFLAGS = -fPIE
> > +weak_undef_test_3_LDFLAGS = -pie
> > +
> > +check_PROGRAMS += weak_undef_test_4
> > +weak_undef_test_4_SOURCES = weak_undef_test_4.c
> > +weak_undef_test_4_DEPENDENCIES = gcctestdir/ld weak_undef_lib_4.so
> > +weak_undef_test_4_LDADD = weak_undef_lib_4.so
> > +weak_undef_test_4_LDFLAGS =  -Wl,-R,.
> > +MOSTLYCLEANFILES += weak_undef_lib_4.o weak_undef_lib_4.so
> > +weak_undef_lib_4.o:  weak_undef_lib_4.c
> > +       $(COMPILE) -fPIC -c -o $@ $<
> > +weak_undef_lib_4.so: gcctestdir/ld weak_undef_lib_4.o
> > +       $(LINK) -shared -o $@ weak_undef_lib_4.o
> > +
> >  if FN_PTRS_IN_SO_WITHOUT_PIC
> >  check_PROGRAMS += weak_undef_nonpic_test
> >  MOSTLYCLEANFILES += alt/weak_undef_lib_nonpic.so
> > diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
> > index 78ca73acb17..6df050f4a01 100644
> > --- a/gold/testsuite/Makefile.in
> > +++ b/gold/testsuite/Makefile.in
> > @@ -211,7 +211,9 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ eh_test_2.sects \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ two_file_shared.dbg \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ alt/weak_undef_lib.so \
> > -@GCC_TRUE@@NATIVE_LINKER_TRUE@ libweak_undef_2.a
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ libweak_undef_2.a \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_lib_4.o \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_lib_4.so
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_5 = icf_virtual_function_folding_test \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ large_symbol_alignment \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ basic_test basic_pic_test \
> > @@ -269,7 +271,9 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
> >  @GCC_TRUE@@HAVE_STATIC_TRUE@@NATIVE_LINKER_TRUE@am__append_16 = exception_static_test
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_17 = weak_test \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test \
> > -@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2 \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_3 \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_4
> >  @GCC_FALSE@weak_test_DEPENDENCIES =
> >  @NATIVE_LINKER_FALSE@weak_test_DEPENDENCIES =
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_18 = weak_undef_nonpic_test
> > @@ -1255,7 +1259,9 @@ libgoldtest_a_OBJECTS = $(am_libgoldtest_a_OBJECTS)
> >  @GCC_TRUE@@HAVE_STATIC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_13 = exception_static_test$(EXEEXT)
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_14 = weak_test$(EXEEXT) \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test$(EXEEXT) \
> > -@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2$(EXEEXT)
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_2$(EXEEXT) \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_3$(EXEEXT) \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_4$(EXEEXT)
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_15 = weak_undef_nonpic_test$(EXEEXT)
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_16 =  \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_alias_test$(EXEEXT) \
> > @@ -2271,6 +2277,16 @@ weak_undef_test_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
> >  weak_undef_test_2_OBJECTS = $(am_weak_undef_test_2_OBJECTS)
> >  weak_undef_test_2_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
> >         $(weak_undef_test_2_LDFLAGS) $(LDFLAGS) -o $@
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_undef_test_3_OBJECTS = weak_undef_test_3-weak_undef_test_3.$(OBJEXT)
> > +weak_undef_test_3_OBJECTS = $(am_weak_undef_test_3_OBJECTS)
> > +weak_undef_test_3_LDADD = $(LDADD)
> > +weak_undef_test_3_LINK = $(CCLD) $(weak_undef_test_3_CFLAGS) $(CFLAGS) \
> > +       $(weak_undef_test_3_LDFLAGS) $(LDFLAGS) -o $@
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_undef_test_4_OBJECTS =  \
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_undef_test_4.$(OBJEXT)
> > +weak_undef_test_4_OBJECTS = $(am_weak_undef_test_4_OBJECTS)
> > +weak_undef_test_4_LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
> > +       $(weak_undef_test_4_LDFLAGS) $(LDFLAGS) -o $@
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_unresolved_symbols_test_OBJECTS = weak_unresolved_symbols_test-weak_unresolved_symbols_test.$(OBJEXT)
> >  weak_unresolved_symbols_test_OBJECTS =  \
> >         $(am_weak_unresolved_symbols_test_OBJECTS)
> > @@ -2416,7 +2432,8 @@ SOURCES = $(libgoldtest_a_SOURCES) $(aarch64_pr23870_SOURCES) \
> >         $(ver_test_8_SOURCES) $(ver_test_9_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_undef_test_2_SOURCES) $(weak_undef_test_3_SOURCES) \
> > +       $(weak_undef_test_4_SOURCES) \
> >         $(weak_unresolved_symbols_test_SOURCES)
> >  am__can_run_installinfo = \
> >    case $$AM_UPDATE_INFO_DIR in \
> > @@ -3143,6 +3160,14 @@ DEPENDENCIES = \
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_DEPENDENCIES = gcctestdir/ld libweak_undef_2.a
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_LDFLAGS = -u weak_undef_2
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_LDADD = -L . -lweak_undef_2
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_SOURCES = weak_undef_test_3.c
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_DEPENDENCIES = gcctestdir/ld
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_CFLAGS = -fPIE
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_LDFLAGS = -pie
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_SOURCES = weak_undef_test_4.c
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_DEPENDENCIES = gcctestdir/ld weak_undef_lib_4.so
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_LDADD = weak_undef_lib_4.so
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_LDFLAGS = -Wl,-R,.
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_SOURCES = weak_undef_test.cc
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_DEPENDENCIES = gcctestdir/ld weak_undef_lib_nonpic.so alt/weak_undef_lib_nonpic.so
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_LDFLAGS = -Wl,-R,alt
> > @@ -4818,6 +4843,14 @@ weak_undef_test_2$(EXEEXT): $(weak_undef_test_2_OBJECTS) $(weak_undef_test_2_DEP
> >         @rm -f weak_undef_test_2$(EXEEXT)
> >         $(AM_V_CXXLD)$(weak_undef_test_2_LINK) $(weak_undef_test_2_OBJECTS) $(weak_undef_test_2_LDADD) $(LIBS)
> >
> > +weak_undef_test_3$(EXEEXT): $(weak_undef_test_3_OBJECTS) $(weak_undef_test_3_DEPENDENCIES) $(EXTRA_weak_undef_test_3_DEPENDENCIES)
> > +       @rm -f weak_undef_test_3$(EXEEXT)
> > +       $(AM_V_CCLD)$(weak_undef_test_3_LINK) $(weak_undef_test_3_OBJECTS) $(weak_undef_test_3_LDADD) $(LIBS)
> > +
> > +weak_undef_test_4$(EXEEXT): $(weak_undef_test_4_OBJECTS) $(weak_undef_test_4_DEPENDENCIES) $(EXTRA_weak_undef_test_4_DEPENDENCIES)
> > +       @rm -f weak_undef_test_4$(EXEEXT)
> > +       $(AM_V_CCLD)$(weak_undef_test_4_LINK) $(weak_undef_test_4_OBJECTS) $(weak_undef_test_4_LDADD) $(LIBS)
> > +
> >  weak_unresolved_symbols_test$(EXEEXT): $(weak_unresolved_symbols_test_OBJECTS) $(weak_unresolved_symbols_test_DEPENDENCIES) $(EXTRA_weak_unresolved_symbols_test_DEPENDENCIES)
> >         @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)
> > @@ -4991,6 +5024,8 @@ distclean-compile:
> >  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_test.Po@am__quote@
> >  @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_undef_test_3-weak_undef_test_3.Po@am__quote@
> > +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_undef_test_4.Po@am__quote@
> >  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_unresolved_symbols_test-weak_unresolved_symbols_test.Po@am__quote@
> >
> >  .c.o:
> > @@ -5273,6 +5308,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`
> >
> > +weak_undef_test_3-weak_undef_test_3.o: weak_undef_test_3.c
> > +@am__fastdepCC_TRUE@   $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -MT weak_undef_test_3-weak_undef_test_3.o -MD -MP -MF $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo -c -o weak_undef_test_3-weak_undef_test_3.o `test -f 'weak_undef_test_3.c' || echo '$(srcdir)/'`weak_undef_test_3.c
> > +@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Po
> > +@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='weak_undef_test_3.c' object='weak_undef_test_3-weak_undef_test_3.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) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -c -o weak_undef_test_3-weak_undef_test_3.o `test -f 'weak_undef_test_3.c' || echo '$(srcdir)/'`weak_undef_test_3.c
> > +
> > +weak_undef_test_3-weak_undef_test_3.obj: weak_undef_test_3.c
> > +@am__fastdepCC_TRUE@   $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -MT weak_undef_test_3-weak_undef_test_3.obj -MD -MP -MF $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo -c -o weak_undef_test_3-weak_undef_test_3.obj `if test -f 'weak_undef_test_3.c'; then $(CYGPATH_W) 'weak_undef_test_3.c'; else $(CYGPATH_W) '$(srcdir)/weak_undef_test_3.c'; fi`
> > +@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Po
> > +@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='weak_undef_test_3.c' object='weak_undef_test_3-weak_undef_test_3.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) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -c -o weak_undef_test_3-weak_undef_test_3.obj `if test -f 'weak_undef_test_3.c'; then $(CYGPATH_W) 'weak_undef_test_3.c'; else $(CYGPATH_W) '$(srcdir)/weak_undef_test_3.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
> > @@ -6950,6 +6999,20 @@ weak_undef_test_2.log: weak_undef_test_2$(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)
> > +weak_undef_test_3.log: weak_undef_test_3$(EXEEXT)
> > +       @p='weak_undef_test_3$(EXEEXT)'; \
> > +       b='weak_undef_test_3'; \
> > +       $(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)
> > +weak_undef_test_4.log: weak_undef_test_4$(EXEEXT)
> > +       @p='weak_undef_test_4$(EXEEXT)'; \
> > +       b='weak_undef_test_4'; \
> > +       $(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)
> >  weak_undef_nonpic_test.log: weak_undef_nonpic_test$(EXEEXT)
> >         @p='weak_undef_nonpic_test$(EXEEXT)'; \
> >         b='weak_undef_nonpic_test'; \
> > @@ -8389,6 +8452,10 @@ uninstall-am:
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -o $@ $<
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file4.o: weak_undef_file4.cc
> >  @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -o $@ $<
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_lib_4.o:  weak_undef_lib_4.c
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(COMPILE) -fPIC -c -o $@ $<
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_lib_4.so: gcctestdir/ld weak_undef_lib_4.o
> > +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -shared -o $@ weak_undef_lib_4.o
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file1_nonpic.o: weak_undef_file1.cc
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -o $@ $<
> >  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file2_nonpic.o: weak_undef_file2.cc
> > diff --git a/gold/testsuite/weak_undef_lib_4.c b/gold/testsuite/weak_undef_lib_4.c
> > new file mode 100644
> > index 00000000000..f8609c64745
> > --- /dev/null
> > +++ b/gold/testsuite/weak_undef_lib_4.c
> > @@ -0,0 +1,40 @@
> > +/* weak_undef_lib_4.c -- test non-default weak undefined symbol in DSO.
> > +
> > +   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.  */
> > +
> > +/* Non-default weak undefined symbol in DSO should be resolved to 0 at
> > +   runtime.  */
> > +
> > +#include <stdlib.h>
> > +
> > +extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
> > +extern void protected (void) __attribute__((visibility("protected")))  __attribute__((weak));
> > +
> > +extern void foo (void);
> > +
> > +void
> > +foo (void)
> > +{
> > +  if (&undefined != NULL)
> > +    abort ();
> > +
> > +  if (&protected != NULL)
> > +    abort ();
> > +}
> > diff --git a/gold/testsuite/weak_undef_test_3.c b/gold/testsuite/weak_undef_test_3.c
> > new file mode 100644
> > index 00000000000..a7b7750c03f
> > --- /dev/null
> > +++ b/gold/testsuite/weak_undef_test_3.c
> > @@ -0,0 +1,40 @@
> > +/* weak_undef_test_3.c -- test non-default 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.  */
> > +
> > +/* Non-default weak undefined symbol in PIE should be resolved to 0 at
> > +   runtime.  */
> > +
> > +#include <stdlib.h>
> > +
> > +extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
> > +extern void protected (void) __attribute__((visibility("protected")))  __attribute__((weak));
> > +
> > +int
> > +main (void)
> > +{
> > +  if (&undefined != NULL)
> > +    abort ();
> > +
> > +  if (&protected != NULL)
> > +    abort ();
> > +
> > +  return 0;
> > +}
> > diff --git a/gold/testsuite/weak_undef_test_4.c b/gold/testsuite/weak_undef_test_4.c
> > new file mode 100644
> > index 00000000000..ab2f8bc224d
> > --- /dev/null
> > +++ b/gold/testsuite/weak_undef_test_4.c
> > @@ -0,0 +1,29 @@
> > +/* weak_undef_test_4.c -- test non-default weak undefined symbol in DSO.
> > +
> > +   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.  */
> > +
> > +extern void foo (void);
> > +
> > +int
> > +main (void)
> > +{
> > +  foo ();
> > +  return 0;
> > +}
> > --
> > 2.46.0
> >
>
>
> --
> H.J.
  

Patch

diff --git a/gold/symtab.cc b/gold/symtab.cc
index 5857dd7b098..91b551cae1d 100644
--- a/gold/symtab.cc
+++ b/gold/symtab.cc
@@ -450,7 +450,16 @@  Symbol::final_value_is_known() const
        || parameters->options().relocatable())
       && !(this->type() == elfcpp::STT_TLS
            && parameters->options().pie()))
-    return false;
+    {
+      // Non-default weak undefined symbols in executable and shared
+      // library are always resolved to 0 at runtime.
+      if (this->visibility() != elfcpp::STV_DEFAULT
+	  && this->is_weak_undefined()
+	  && !parameters->options().relocatable())
+	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/symtab.h b/gold/symtab.h
index 0a1f6a63a76..9c255599d69 100644
--- a/gold/symtab.h
+++ b/gold/symtab.h
@@ -709,6 +709,13 @@  class Symbol
     if (this->is_absolute())
       return false;
 
+    // Non-default weak undefined symbols in executable and shared
+    // library are always resolved to 0 at runtime.
+    if (this->visibility() != elfcpp::STV_DEFAULT
+	&& this->is_weak_undefined()
+	&& !parameters->options().relocatable())
+      return false;
+
     // An absolute reference within a position-independent output file
     // will need a dynamic relocation.
     if ((flags & ABSOLUTE_REF)
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index a40f7624395..c1e7e07f431 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -865,6 +865,23 @@  weak_undef_file3.o: weak_undef_file3.cc
 weak_undef_file4.o: weak_undef_file4.cc
 	$(CXXCOMPILE) -c -o $@ $<
 
+check_PROGRAMS += weak_undef_test_3
+weak_undef_test_3_SOURCES = weak_undef_test_3.c
+weak_undef_test_3_DEPENDENCIES = gcctestdir/ld
+weak_undef_test_3_CFLAGS = -fPIE
+weak_undef_test_3_LDFLAGS = -pie
+
+check_PROGRAMS += weak_undef_test_4
+weak_undef_test_4_SOURCES = weak_undef_test_4.c
+weak_undef_test_4_DEPENDENCIES = gcctestdir/ld weak_undef_lib_4.so
+weak_undef_test_4_LDADD = weak_undef_lib_4.so
+weak_undef_test_4_LDFLAGS =  -Wl,-R,.
+MOSTLYCLEANFILES += weak_undef_lib_4.o weak_undef_lib_4.so
+weak_undef_lib_4.o:  weak_undef_lib_4.c
+	$(COMPILE) -fPIC -c -o $@ $<
+weak_undef_lib_4.so: gcctestdir/ld weak_undef_lib_4.o
+	$(LINK) -shared -o $@ weak_undef_lib_4.o
+
 if FN_PTRS_IN_SO_WITHOUT_PIC
 check_PROGRAMS += weak_undef_nonpic_test
 MOSTLYCLEANFILES += alt/weak_undef_lib_nonpic.so
diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
index 78ca73acb17..6df050f4a01 100644
--- a/gold/testsuite/Makefile.in
+++ b/gold/testsuite/Makefile.in
@@ -211,7 +211,9 @@  check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	eh_test_2.sects \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	two_file_shared.dbg \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	alt/weak_undef_lib.so \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	libweak_undef_2.a
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	libweak_undef_2.a \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_lib_4.o \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_lib_4.so
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_5 = icf_virtual_function_folding_test \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	large_symbol_alignment \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	basic_test basic_pic_test \
@@ -269,7 +271,9 @@  check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 @GCC_TRUE@@HAVE_STATIC_TRUE@@NATIVE_LINKER_TRUE@am__append_16 = exception_static_test
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_17 = weak_test \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_2
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_2 \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_3 \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_4
 @GCC_FALSE@weak_test_DEPENDENCIES =
 @NATIVE_LINKER_FALSE@weak_test_DEPENDENCIES =
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_18 = weak_undef_nonpic_test
@@ -1255,7 +1259,9 @@  libgoldtest_a_OBJECTS = $(am_libgoldtest_a_OBJECTS)
 @GCC_TRUE@@HAVE_STATIC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_13 = exception_static_test$(EXEEXT)
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_14 = weak_test$(EXEEXT) \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test$(EXEEXT) \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_2$(EXEEXT)
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_2$(EXEEXT) \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_3$(EXEEXT) \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_4$(EXEEXT)
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_15 = weak_undef_nonpic_test$(EXEEXT)
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_16 =  \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_alias_test$(EXEEXT) \
@@ -2271,6 +2277,16 @@  weak_undef_test_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
 weak_undef_test_2_OBJECTS = $(am_weak_undef_test_2_OBJECTS)
 weak_undef_test_2_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
 	$(weak_undef_test_2_LDFLAGS) $(LDFLAGS) -o $@
+@GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_undef_test_3_OBJECTS = weak_undef_test_3-weak_undef_test_3.$(OBJEXT)
+weak_undef_test_3_OBJECTS = $(am_weak_undef_test_3_OBJECTS)
+weak_undef_test_3_LDADD = $(LDADD)
+weak_undef_test_3_LINK = $(CCLD) $(weak_undef_test_3_CFLAGS) $(CFLAGS) \
+	$(weak_undef_test_3_LDFLAGS) $(LDFLAGS) -o $@
+@GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_undef_test_4_OBJECTS =  \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_undef_test_4.$(OBJEXT)
+weak_undef_test_4_OBJECTS = $(am_weak_undef_test_4_OBJECTS)
+weak_undef_test_4_LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
+	$(weak_undef_test_4_LDFLAGS) $(LDFLAGS) -o $@
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am_weak_unresolved_symbols_test_OBJECTS = weak_unresolved_symbols_test-weak_unresolved_symbols_test.$(OBJEXT)
 weak_unresolved_symbols_test_OBJECTS =  \
 	$(am_weak_unresolved_symbols_test_OBJECTS)
@@ -2416,7 +2432,8 @@  SOURCES = $(libgoldtest_a_SOURCES) $(aarch64_pr23870_SOURCES) \
 	$(ver_test_8_SOURCES) $(ver_test_9_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_undef_test_2_SOURCES) $(weak_undef_test_3_SOURCES) \
+	$(weak_undef_test_4_SOURCES) \
 	$(weak_unresolved_symbols_test_SOURCES)
 am__can_run_installinfo = \
   case $$AM_UPDATE_INFO_DIR in \
@@ -3143,6 +3160,14 @@  DEPENDENCIES = \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_DEPENDENCIES = gcctestdir/ld libweak_undef_2.a
 @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_LDFLAGS = -u weak_undef_2
 @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_2_LDADD = -L . -lweak_undef_2
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_SOURCES = weak_undef_test_3.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_DEPENDENCIES = gcctestdir/ld
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_CFLAGS = -fPIE
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_3_LDFLAGS = -pie
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_SOURCES = weak_undef_test_4.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_DEPENDENCIES = gcctestdir/ld weak_undef_lib_4.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_LDADD = weak_undef_lib_4.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_test_4_LDFLAGS = -Wl,-R,.
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_SOURCES = weak_undef_test.cc
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_DEPENDENCIES = gcctestdir/ld weak_undef_lib_nonpic.so alt/weak_undef_lib_nonpic.so
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_nonpic_test_LDFLAGS = -Wl,-R,alt
@@ -4818,6 +4843,14 @@  weak_undef_test_2$(EXEEXT): $(weak_undef_test_2_OBJECTS) $(weak_undef_test_2_DEP
 	@rm -f weak_undef_test_2$(EXEEXT)
 	$(AM_V_CXXLD)$(weak_undef_test_2_LINK) $(weak_undef_test_2_OBJECTS) $(weak_undef_test_2_LDADD) $(LIBS)
 
+weak_undef_test_3$(EXEEXT): $(weak_undef_test_3_OBJECTS) $(weak_undef_test_3_DEPENDENCIES) $(EXTRA_weak_undef_test_3_DEPENDENCIES) 
+	@rm -f weak_undef_test_3$(EXEEXT)
+	$(AM_V_CCLD)$(weak_undef_test_3_LINK) $(weak_undef_test_3_OBJECTS) $(weak_undef_test_3_LDADD) $(LIBS)
+
+weak_undef_test_4$(EXEEXT): $(weak_undef_test_4_OBJECTS) $(weak_undef_test_4_DEPENDENCIES) $(EXTRA_weak_undef_test_4_DEPENDENCIES) 
+	@rm -f weak_undef_test_4$(EXEEXT)
+	$(AM_V_CCLD)$(weak_undef_test_4_LINK) $(weak_undef_test_4_OBJECTS) $(weak_undef_test_4_LDADD) $(LIBS)
+
 weak_unresolved_symbols_test$(EXEEXT): $(weak_unresolved_symbols_test_OBJECTS) $(weak_unresolved_symbols_test_DEPENDENCIES) $(EXTRA_weak_unresolved_symbols_test_DEPENDENCIES) 
 	@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)
@@ -4991,6 +5024,8 @@  distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_test.Po@am__quote@
 @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_undef_test_3-weak_undef_test_3.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_undef_test_4.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/weak_unresolved_symbols_test-weak_unresolved_symbols_test.Po@am__quote@
 
 .c.o:
@@ -5273,6 +5308,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`
 
+weak_undef_test_3-weak_undef_test_3.o: weak_undef_test_3.c
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -MT weak_undef_test_3-weak_undef_test_3.o -MD -MP -MF $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo -c -o weak_undef_test_3-weak_undef_test_3.o `test -f 'weak_undef_test_3.c' || echo '$(srcdir)/'`weak_undef_test_3.c
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='weak_undef_test_3.c' object='weak_undef_test_3-weak_undef_test_3.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) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -c -o weak_undef_test_3-weak_undef_test_3.o `test -f 'weak_undef_test_3.c' || echo '$(srcdir)/'`weak_undef_test_3.c
+
+weak_undef_test_3-weak_undef_test_3.obj: weak_undef_test_3.c
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -MT weak_undef_test_3-weak_undef_test_3.obj -MD -MP -MF $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo -c -o weak_undef_test_3-weak_undef_test_3.obj `if test -f 'weak_undef_test_3.c'; then $(CYGPATH_W) 'weak_undef_test_3.c'; else $(CYGPATH_W) '$(srcdir)/weak_undef_test_3.c'; fi`
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Tpo $(DEPDIR)/weak_undef_test_3-weak_undef_test_3.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='weak_undef_test_3.c' object='weak_undef_test_3-weak_undef_test_3.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) $(weak_undef_test_3_CFLAGS) $(CFLAGS) -c -o weak_undef_test_3-weak_undef_test_3.obj `if test -f 'weak_undef_test_3.c'; then $(CYGPATH_W) 'weak_undef_test_3.c'; else $(CYGPATH_W) '$(srcdir)/weak_undef_test_3.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
@@ -6950,6 +6999,20 @@  weak_undef_test_2.log: weak_undef_test_2$(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)
+weak_undef_test_3.log: weak_undef_test_3$(EXEEXT)
+	@p='weak_undef_test_3$(EXEEXT)'; \
+	b='weak_undef_test_3'; \
+	$(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)
+weak_undef_test_4.log: weak_undef_test_4$(EXEEXT)
+	@p='weak_undef_test_4$(EXEEXT)'; \
+	b='weak_undef_test_4'; \
+	$(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)
 weak_undef_nonpic_test.log: weak_undef_nonpic_test$(EXEEXT)
 	@p='weak_undef_nonpic_test$(EXEEXT)'; \
 	b='weak_undef_nonpic_test'; \
@@ -8389,6 +8452,10 @@  uninstall-am:
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXCOMPILE) -c -o $@ $<
 @GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file4.o: weak_undef_file4.cc
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXCOMPILE) -c -o $@ $<
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_lib_4.o:  weak_undef_lib_4.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(COMPILE) -fPIC -c -o $@ $<
+@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_lib_4.so: gcctestdir/ld weak_undef_lib_4.o
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(LINK) -shared -o $@ weak_undef_lib_4.o
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file1_nonpic.o: weak_undef_file1.cc
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXCOMPILE) -c -o $@ $<
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_undef_file2_nonpic.o: weak_undef_file2.cc
diff --git a/gold/testsuite/weak_undef_lib_4.c b/gold/testsuite/weak_undef_lib_4.c
new file mode 100644
index 00000000000..f8609c64745
--- /dev/null
+++ b/gold/testsuite/weak_undef_lib_4.c
@@ -0,0 +1,40 @@ 
+/* weak_undef_lib_4.c -- test non-default weak undefined symbol in DSO.
+
+   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.  */
+
+/* Non-default weak undefined symbol in DSO should be resolved to 0 at
+   runtime.  */
+
+#include <stdlib.h>
+
+extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
+extern void protected (void) __attribute__((visibility("protected")))  __attribute__((weak));
+
+extern void foo (void);
+
+void
+foo (void)
+{
+  if (&undefined != NULL)
+    abort ();
+
+  if (&protected != NULL)
+    abort ();
+}
diff --git a/gold/testsuite/weak_undef_test_3.c b/gold/testsuite/weak_undef_test_3.c
new file mode 100644
index 00000000000..a7b7750c03f
--- /dev/null
+++ b/gold/testsuite/weak_undef_test_3.c
@@ -0,0 +1,40 @@ 
+/* weak_undef_test_3.c -- test non-default 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.  */
+
+/* Non-default weak undefined symbol in PIE should be resolved to 0 at
+   runtime.  */
+
+#include <stdlib.h>
+
+extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak));
+extern void protected (void) __attribute__((visibility("protected")))  __attribute__((weak));
+
+int
+main (void)
+{
+  if (&undefined != NULL)
+    abort ();
+
+  if (&protected != NULL)
+    abort ();
+
+  return 0;
+}
diff --git a/gold/testsuite/weak_undef_test_4.c b/gold/testsuite/weak_undef_test_4.c
new file mode 100644
index 00000000000..ab2f8bc224d
--- /dev/null
+++ b/gold/testsuite/weak_undef_test_4.c
@@ -0,0 +1,29 @@ 
+/* weak_undef_test_4.c -- test non-default weak undefined symbol in DSO.
+
+   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.  */
+
+extern void foo (void);
+
+int
+main (void)
+{
+  foo ();
+  return 0;
+}