From 8ca751ee637ce1b8d1b284a794ffdb327951d0f8 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Sun, 29 Nov 2020 05:55:23 -0800
Subject: [PATCH] gold: Get linkonce/comdate sections for debugging sections

When relocating debug sections, get the section index for the linkonce
section.  Since symbols referenced in debugging sections can be defined
a single comdat section with a different section name, also check the
single comdat section.

	PR gold/26937
	* object.cc (Sized_relobj_file::map_to_kept_section): Get the
	section index for linkonce section.  Also check the single
	comdat section.
	* testsuite/Makefile.am (check_SCRIPTS): Add pr26936.sh.
	(check_DATA): Add pr26936a.stdout and pr26936b.stdout.
	(MOSTLYCLEANFILES): Add pr26936a and pr26936b.
	(pr26936a.stdout): New target.
	(pr26936a): Likewise.
	(pr26936b.stdout): Likewise.
	(pr26936b): Likewise.
	(pr26936a.o): Likewise.
	(pr26936b.o): Likewise.
	(pr26936c.o): Likewise.
	(pr26936d.o): Likewise.
	* testsuite/Makefile.in: Regenerated.
	* testsuite/pr26936.sh: New file.
	* testsuite/pr26936a.s: Likewise.
	* testsuite/pr26936b.s: Likewise.
	* testsuite/pr26936c.s: Likewise.
	* testsuite/pr26936d.s: Likewise.
---
 gold/ChangeLog             | 24 +++++++++++++++++
 gold/object.cc             | 10 ++++---
 gold/testsuite/Makefile.am | 22 +++++++++++++++
 gold/testsuite/Makefile.in | 33 ++++++++++++++++++++---
 gold/testsuite/pr26936.sh  | 55 ++++++++++++++++++++++++++++++++++++++
 gold/testsuite/pr26936a.s  |  6 +++++
 gold/testsuite/pr26936b.s  | 11 ++++++++
 gold/testsuite/pr26936c.s  |  6 +++++
 gold/testsuite/pr26936d.s  |  6 +++++
 9 files changed, 166 insertions(+), 7 deletions(-)
 create mode 100755 gold/testsuite/pr26936.sh
 create mode 100644 gold/testsuite/pr26936a.s
 create mode 100644 gold/testsuite/pr26936b.s
 create mode 100644 gold/testsuite/pr26936c.s
 create mode 100644 gold/testsuite/pr26936d.s

diff --git a/gold/ChangeLog b/gold/ChangeLog
index 4739ed5cd59..1e90cbdda63 100644
--- a/gold/ChangeLog
+++ b/gold/ChangeLog
@@ -1,3 +1,27 @@
+2020-11-29  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR gold/26937
+	* object.cc (Sized_relobj_file::map_to_kept_section): Get the
+	section index for linkonce section.  Also check the single
+	comdat section.
+	* testsuite/Makefile.am (check_SCRIPTS): Add pr26936.sh.
+	(check_DATA): Add pr26936a.stdout and pr26936b.stdout.
+	(MOSTLYCLEANFILES): Add pr26936a and pr26936b.
+	(pr26936a.stdout): New target.
+	(pr26936a): Likewise.
+	(pr26936b.stdout): Likewise.
+	(pr26936b): Likewise.
+	(pr26936a.o): Likewise.
+	(pr26936b.o): Likewise.
+	(pr26936c.o): Likewise.
+	(pr26936d.o): Likewise.
+	* testsuite/Makefile.in: Regenerated.
+	* testsuite/pr26936.sh: New file.
+	* testsuite/pr26936a.s: Likewise.
+	* testsuite/pr26936b.s: Likewise.
+	* testsuite/pr26936c.s: Likewise.
+	* testsuite/pr26936d.s: Likewise.
+
 2020-11-17  Alan Modra  <amodra@gmail.com>
 
 	* powerpc.cc (Target_powerpc::no_tprel_opt_): Rename from tprel_opt_.
diff --git a/gold/object.cc b/gold/object.cc
index c486a2011d5..c0d0f0289fc 100644
--- a/gold/object.cc
+++ b/gold/object.cc
@@ -2981,17 +2981,20 @@ Sized_relobj_file<size, big_endian>::map_to_kept_section(
         {
 	  // The kept section is a linkonce section.
 	  if (sh_size == kept_section->linkonce_size())
-	    found = true;
+	    {
+	      kept_shndx = kept_section->shndx();
+	      found = true;
+	    }
         }
       else
 	{
+	  uint64_t kept_size = 0;
 	  if (is_comdat)
 	    {
 	      // Find the corresponding kept section.
 	      // Since we're using this mapping for relocation processing,
 	      // we don't want to match sections unless they have the same
 	      // size.
-	      uint64_t kept_size = 0;
 	      if (kept_section->find_comdat_section(section_name, &kept_shndx,
 						    &kept_size))
 		{
@@ -2999,9 +3002,8 @@ Sized_relobj_file<size, big_endian>::map_to_kept_section(
 		    found = true;
 		}
 	    }
-	  else
+	  if (!found)
 	    {
-	      uint64_t kept_size = 0;
 	      if (kept_section->find_single_comdat_section(&kept_shndx,
 							   &kept_size)
 		  && sh_size == kept_size)
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index 3bfba9feb64..ae962a817e8 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -4385,4 +4385,26 @@ dwp_test_2a.dwp: ../dwp dwp_test_main.dwo dwp_test_1.dwo
 dwp_test_2b.dwp: ../dwp dwp_test_1b.dwo dwp_test_2.dwo
 	../dwp -o $@ dwp_test_1b.dwo dwp_test_2.dwo
 
+check_SCRIPTS += pr26936.sh
+check_DATA += pr26936a.stdout pr26936b.stdout
+MOSTLYCLEANFILES += pr26936a pr26936b
+pr26936a.stdout: pr26936a
+	$(TEST_READELF) -wL -wR -wr $< >$@ 2>/dev/null
+pr26936a: pr26936a.o pr26936b.o pr26936c.o ../ld-new
+	../ld-new -Ttext-segment 0x10000 -z max-page-size=0x1000 \
+		-e _start -o $@ pr26936a.o pr26936b.o pr26936c.o
+pr26936b.stdout: pr26936b
+	$(TEST_READELF) -wL -wR -wr $< >$@ 2>/dev/null
+pr26936b: pr26936d.o pr26936b.o pr26936c.o ../ld-new
+	../ld-new -Ttext-segment 0x10000 -z max-page-size=0x1000 \
+		-e _start -o $@ pr26936d.o pr26936b.o pr26936c.o
+pr26936a.o: pr26936a.s
+	$(TEST_AS) --gen-debug -o $@ $<
+pr26936b.o: pr26936b.s
+	$(TEST_AS) --gen-debug -o $@ $<
+pr26936c.o: pr26936c.s
+	$(TEST_AS) --gen-debug -o $@ $<
+pr26936d.o: pr26936d.s
+	$(TEST_AS) --gen-debug -o $@ $<
+
 endif DEFAULT_TARGET_X86_64
diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
index 49fd4e95a09..695eeb1f337 100644
--- a/gold/testsuite/Makefile.in
+++ b/gold/testsuite/Makefile.in
@@ -1126,11 +1126,13 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 @DEFAULT_TARGET_S390_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@	split_s390x_z1_ns split_s390x_z2_ns split_s390x_z3_ns \
 @DEFAULT_TARGET_S390_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@	split_s390x_z4_ns split_s390x_n1_ns split_s390x_n2_ns split_s390x_r
 
-@DEFAULT_TARGET_X86_64_TRUE@am__append_116 = *.dwo *.dwp
+@DEFAULT_TARGET_X86_64_TRUE@am__append_116 = *.dwo *.dwp pr26936a \
+@DEFAULT_TARGET_X86_64_TRUE@	pr26936b
 @DEFAULT_TARGET_X86_64_TRUE@am__append_117 = dwp_test_1.sh \
-@DEFAULT_TARGET_X86_64_TRUE@	dwp_test_2.sh
+@DEFAULT_TARGET_X86_64_TRUE@	dwp_test_2.sh pr26936.sh
 @DEFAULT_TARGET_X86_64_TRUE@am__append_118 = dwp_test_1.stdout \
-@DEFAULT_TARGET_X86_64_TRUE@	dwp_test_2.stdout
+@DEFAULT_TARGET_X86_64_TRUE@	dwp_test_2.stdout pr26936a.stdout \
+@DEFAULT_TARGET_X86_64_TRUE@	pr26936b.stdout
 subdir = testsuite
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/../config/ax_pthread.m4 \
@@ -6439,6 +6441,13 @@ dwp_test_2.sh.log: dwp_test_2.sh
 	--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)
+pr26936.sh.log: pr26936.sh
+	@p='pr26936.sh'; \
+	b='pr26936.sh'; \
+	$(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)
 object_unittest.log: object_unittest$(EXEEXT)
 	@p='object_unittest$(EXEEXT)'; \
 	b='object_unittest'; \
@@ -10352,6 +10361,24 @@ uninstall-am:
 @DEFAULT_TARGET_X86_64_TRUE@	../dwp -o $@ dwp_test_main.dwo dwp_test_1.dwo
 @DEFAULT_TARGET_X86_64_TRUE@dwp_test_2b.dwp: ../dwp dwp_test_1b.dwo dwp_test_2.dwo
 @DEFAULT_TARGET_X86_64_TRUE@	../dwp -o $@ dwp_test_1b.dwo dwp_test_2.dwo
+@DEFAULT_TARGET_X86_64_TRUE@pr26936a.stdout: pr26936a
+@DEFAULT_TARGET_X86_64_TRUE@	$(TEST_READELF) -wL -wR -wr $< >$@ 2>/dev/null
+@DEFAULT_TARGET_X86_64_TRUE@pr26936a: pr26936a.o pr26936b.o pr26936c.o ../ld-new
+@DEFAULT_TARGET_X86_64_TRUE@	../ld-new -Ttext-segment 0x10000 -z max-page-size=0x1000 \
+@DEFAULT_TARGET_X86_64_TRUE@		-e _start -o $@ pr26936a.o pr26936b.o pr26936c.o
+@DEFAULT_TARGET_X86_64_TRUE@pr26936b.stdout: pr26936b
+@DEFAULT_TARGET_X86_64_TRUE@	$(TEST_READELF) -wL -wR -wr $< >$@ 2>/dev/null
+@DEFAULT_TARGET_X86_64_TRUE@pr26936b: pr26936d.o pr26936b.o pr26936c.o ../ld-new
+@DEFAULT_TARGET_X86_64_TRUE@	../ld-new -Ttext-segment 0x10000 -z max-page-size=0x1000 \
+@DEFAULT_TARGET_X86_64_TRUE@		-e _start -o $@ pr26936d.o pr26936b.o pr26936c.o
+@DEFAULT_TARGET_X86_64_TRUE@pr26936a.o: pr26936a.s
+@DEFAULT_TARGET_X86_64_TRUE@	$(TEST_AS) --gen-debug -o $@ $<
+@DEFAULT_TARGET_X86_64_TRUE@pr26936b.o: pr26936b.s
+@DEFAULT_TARGET_X86_64_TRUE@	$(TEST_AS) --gen-debug -o $@ $<
+@DEFAULT_TARGET_X86_64_TRUE@pr26936c.o: pr26936c.s
+@DEFAULT_TARGET_X86_64_TRUE@	$(TEST_AS) --gen-debug -o $@ $<
+@DEFAULT_TARGET_X86_64_TRUE@pr26936d.o: pr26936d.s
+@DEFAULT_TARGET_X86_64_TRUE@	$(TEST_AS) --gen-debug -o $@ $<
 
 # Tell versions [3.59,3.63) of GNU make to not export all variables.
 # Otherwise a system limit (for SysV at least) may be exceeded.
diff --git a/gold/testsuite/pr26936.sh b/gold/testsuite/pr26936.sh
new file mode 100755
index 00000000000..364f50f1a9b
--- /dev/null
+++ b/gold/testsuite/pr26936.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+# pr26936.sh -- Tests for relocations in debug sections against linkonce
+# and comdat sections.
+
+# Copyright (C) 2020 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.
+
+set -e
+
+check()
+{
+    number_of_occurrence=`egrep "$2" ./$1 -o| wc -l`
+    if [ $number_of_occurrence != $3 ]
+    then
+	echo "$1: \"$2\" $3: Failed"
+	status=1
+    fi
+}
+
+status=0
+check pr26936a.stdout "^pr26936a.s +6 +0x10108 +x" 1
+check pr26936a.stdout "^pr26936b.s +5 +0x10109 +x" 1
+check pr26936a.stdout "^pr26936b.s +11 +0x10108 +x" 1
+check pr26936a.stdout "^pr26936c.s +6 +0x10108 +x" 1
+check pr26936a.stdout "^ +0+10108 0+1" 3
+check pr26936a.stdout "^ +0+10109 0+1" 1
+check pr26936a.stdout "^ +0+ 0+10109 0+1010a" 1
+check pr26936a.stdout "^ +0+ 0+10108 0+10109" 1
+check pr26936b.stdout "^pr26936d.s +6 +0x10108 +x" 1
+check pr26936b.stdout "^pr26936b.s +5 +0x10109 +x" 1
+check pr26936b.stdout "^pr26936b.s +11 +0x10108 +x" 1
+check pr26936b.stdout "^pr26936c.s +6 +0x10108 +x" 1
+check pr26936b.stdout "^ +0+10108 0+1" 3
+check pr26936b.stdout "^ +0+10109 0+1" 1
+check pr26936b.stdout "^ +0+ 0+10109 0+1010a" 1
+check pr26936b.stdout "^ +0+ 0+10108 0+10109" 1
+
+exit $status
diff --git a/gold/testsuite/pr26936a.s b/gold/testsuite/pr26936a.s
new file mode 100644
index 00000000000..24b90daa20b
--- /dev/null
+++ b/gold/testsuite/pr26936a.s
@@ -0,0 +1,6 @@
+	.section .gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",%progbits
+	.globl  __x86.get_pc_thunk.bx
+	.hidden __x86.get_pc_thunk.bx
+	.type   __x86.get_pc_thunk.bx, %function
+__x86.get_pc_thunk.bx:
+	.nop
diff --git a/gold/testsuite/pr26936b.s b/gold/testsuite/pr26936b.s
new file mode 100644
index 00000000000..20f064d051d
--- /dev/null
+++ b/gold/testsuite/pr26936b.s
@@ -0,0 +1,11 @@
+	.text
+	.type _start,"function"
+	.global _start
+_start:
+	.nop
+	.section .text.__x86.get_pc_thunk.bx,"axG",%progbits,__x86.get_pc_thunk.bx,comdat
+	.globl  __x86.get_pc_thunk.bx
+	.hidden __x86.get_pc_thunk.bx
+	.type   __x86.get_pc_thunk.bx, %function
+__x86.get_pc_thunk.bx:
+	.nop
diff --git a/gold/testsuite/pr26936c.s b/gold/testsuite/pr26936c.s
new file mode 100644
index 00000000000..379cb71d159
--- /dev/null
+++ b/gold/testsuite/pr26936c.s
@@ -0,0 +1,6 @@
+	.section .text.__x86.get_pc_thunk.bx,"axG",%progbits,__x86.get_pc_thunk.bx,comdat
+	.globl  __x86.get_pc_thunk.bx
+	.hidden __x86.get_pc_thunk.bx
+	.type   __x86.get_pc_thunk.bx, %function
+__x86.get_pc_thunk.bx:
+	.nop
diff --git a/gold/testsuite/pr26936d.s b/gold/testsuite/pr26936d.s
new file mode 100644
index 00000000000..379cb71d159
--- /dev/null
+++ b/gold/testsuite/pr26936d.s
@@ -0,0 +1,6 @@
+	.section .text.__x86.get_pc_thunk.bx,"axG",%progbits,__x86.get_pc_thunk.bx,comdat
+	.globl  __x86.get_pc_thunk.bx
+	.hidden __x86.get_pc_thunk.bx
+	.type   __x86.get_pc_thunk.bx, %function
+__x86.get_pc_thunk.bx:
+	.nop