From patchwork Sat Aug 17 13:53:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 96031 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C18833861011 for ; Sat, 17 Aug 2024 13:53:52 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 5390D386075E for ; Sat, 17 Aug 2024 13:53:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5390D386075E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5390D386075E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::135 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723902806; cv=none; b=nGMb73R1Ihy8VnncjTbMZOAgyqL8nLEfrqHR38W1WgqHpO0weqOvsoXwg1JvkFFIVVCchevrBGPcCl2PwLTAHceP5ZdpbM8x0kOBs1R1/Bqe641I3gagT1NVwxl3rraNUct6gf4mPOHpzZAec67c6WNH7sDvF4RNNDjuew1+5Lk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723902806; c=relaxed/simple; bh=OrUacciOZF3FLSK0UlQmNM1W7c+yNnhcuM7aiQHHXxM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vF/t+/1p8YsYQ5bkl/bp+Ar6UDpG8ZKUgye7hnH+hsjggsRbYRXM4Y94IUSPxDcHOL4JfIVZ8QltWS+yrTjp772KyGDpEEvIkFsJr8yhi89ZDNrDUL6BRpvBjwib/g6b3H7rHiMCk1yWaSQajHP5rrOa1AXIFjxVkfu+hOEURyM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-39d37d9767dso1217565ab.3 for ; Sat, 17 Aug 2024 06:53:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723902803; x=1724507603; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=2z2S4KXgibpA17Lsbnq8i5e+cr1M3oi1b3Ok/M8MWos=; b=QUPKX9HIVGp0wHD/AFxc7PKSaeIIoLD4AX4Ckrf3tJ9cXOVp7urQ31RjYP4J4cXOgC 1cKqUwy/hwoQWoI0ItNgi9IAJf2SnXzw2M1WytpStNT57UEnJTGOUaCkM2nnchHvY17f 9GZSBjWpHX0b7KdEkjYt3QRjJIe1opj2l9cs183fNwn3XwVT9UEZY7PNLAeHDnJuevlK s4vYnzrseVyof7vnUwTWZ7Ghe9yzy/idEluvkSBxTjw62WjUFB/YqTmlNuMwyiX9ifI8 nIJQ9xGfDVbJ35lgX/yYduay93E6hlF8AFNEGFiaWGTW9vix1L70vXE6CIWuGugFg0aI UFfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723902803; x=1724507603; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2z2S4KXgibpA17Lsbnq8i5e+cr1M3oi1b3Ok/M8MWos=; b=NTwORTGJNQfcP66UqUQ0i3XZJzcPl7BeW9Mu8ay8zr/c18CDzv+ZLmnIY/aRBIPcuP Xxm622vGbLv/M+xToPzvZMufhfxVsuwn38/TR9sizK64Z/Ey2qd68ZTMvi+1mRuiROzo 6pWh6D9oNFy+nuCpfd2uv6AmTeKWPWBTz/SwhCprOCS+0n49R0gS6mY9YPrj3aaB2yDW xaUaG8/V/cEZ4MQ3paMWaKBiXpb2aYIjInxGUcgIcNAmknEMmOtkwS254LYRm/c718Zo oQjej+zkiOaBrtReGFfMC9nlrrEl1Hnm144KP5TLkJ20xwqmanH3uqaPAQMj+LPT1eMQ sOvQ== X-Gm-Message-State: AOJu0YykHcRbMkLvORnlDelYiG4nxxrTyhWVfPhSyp7r1xKM2N0yClND 6wqUphSvNIHm3E9aLqF0vPZacclggUB7zMzjWn5xFZ9ngyVncUWCL6hqrXg3 X-Google-Smtp-Source: AGHT+IHBU0worKC3cRLHPIOumdc7y5woM3nUioq/WYYetxchlIM6Je7Ony9Ivu/ycffKgqCU5Hnu0A== X-Received: by 2002:a05:6e02:164e:b0:39b:388c:3697 with SMTP id e9e14a558f8ab-39d26d8040bmr76235175ab.28.1723902803108; Sat, 17 Aug 2024 06:53:23 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.169.70]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7c6b61ce853sm4647232a12.37.2024.08.17.06.53.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Aug 2024 06:53:22 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id F126A74032C for ; Sat, 17 Aug 2024 06:53:20 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH] gold: Always resolve weak undefined in PIE to 0 Date: Sat, 17 Aug 2024 06:53:20 -0700 Message-ID: <20240817135320.1632000-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3017.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org 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 --- 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 + +extern void undefined (void) __attribute__((visibility("hidden"))) __attribute__((weak)); + +int +main (void) +{ + if (&undefined != NULL) + abort (); + + return 0; +}