[PATCH] elf: Get the real kept section

H.J. Lu hjl.tools@gmail.com
Wed Nov 25 13:15:11 GMT 2020


When mixing linkonce and comdat sections, we need to keep searching to
get the real kept section.

bfd/

	PR ld/26936
	* elflink.c (_bfd_elf_check_kept_section): Get the real kept
	section.

ld/

	PR ld/26936
	* testsuite/ld-elf/pr26936.d: New file.
	* testsuite/ld-elf/pr26936a.s: Likewise.
	* testsuite/ld-elf/pr26936b.s: Likewise.
	* testsuite/ld-elf/pr26936c.s: Likewise.
---
 bfd/elflink.c                  |  9 +++++++++
 ld/testsuite/ld-elf/pr26936.d  | 12 ++++++++++++
 ld/testsuite/ld-elf/pr26936a.s |  6 ++++++
 ld/testsuite/ld-elf/pr26936b.s | 11 +++++++++++
 ld/testsuite/ld-elf/pr26936c.s |  6 ++++++
 5 files changed, 44 insertions(+)
 create mode 100644 ld/testsuite/ld-elf/pr26936.d
 create mode 100644 ld/testsuite/ld-elf/pr26936a.s
 create mode 100644 ld/testsuite/ld-elf/pr26936b.s
 create mode 100644 ld/testsuite/ld-elf/pr26936c.s

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 7ba667a824..94ed560e04 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -10702,6 +10702,15 @@ _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
 	kept = NULL;
+      else
+	{
+	  /* Get the real kept section.  */
+	  asection *next;
+	  for (next = kept->kept_section;
+	       next != NULL;
+	       next = next->kept_section)
+	    kept = next;
+	}
       sec->kept_section = kept;
     }
   return kept;
diff --git a/ld/testsuite/ld-elf/pr26936.d b/ld/testsuite/ld-elf/pr26936.d
new file mode 100644
index 0000000000..b007e83ff2
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26936.d
@@ -0,0 +1,12 @@
+#source: pr26936a.s
+#source: pr26936b.s
+#source: pr26936c.s
+#as: -g
+#ld: -z noseparate-code -Ttext-segment 0x10000 -z max-page-size=0x1000
+#readelf: -wL
+
+#...
+CU: .*/pr26936c.s:
+File name +Line number +Starting address +View +Stmt
+pr26936c.s +6 +0x10[0-9a-f][0-9a-f][0-9a-f] +x
+#pass
diff --git a/ld/testsuite/ld-elf/pr26936a.s b/ld/testsuite/ld-elf/pr26936a.s
new file mode 100644
index 0000000000..24b90daa20
--- /dev/null
+++ b/ld/testsuite/ld-elf/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/ld/testsuite/ld-elf/pr26936b.s b/ld/testsuite/ld-elf/pr26936b.s
new file mode 100644
index 0000000000..433528c33e
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26936b.s
@@ -0,0 +1,11 @@
+	.text
+	.globl _start
+_start:
+	.type   _start, %function
+	.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/ld/testsuite/ld-elf/pr26936c.s b/ld/testsuite/ld-elf/pr26936c.s
new file mode 100644
index 0000000000..379cb71d15
--- /dev/null
+++ b/ld/testsuite/ld-elf/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
-- 
2.28.0



More information about the Binutils mailing list