[binutils-gdb] ld: Lookup section in output with the same name

H.J.Lu hjl@sourceware.org
Fri Aug 31 16:27:00 GMT 2018


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4d1c6335455aeeda9de8a5eb58998f919ea35a1e

commit 4d1c6335455aeeda9de8a5eb58998f919ea35a1e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Aug 31 09:25:31 2018 -0700

    ld: Lookup section in output with the same name
    
    When there are more than one input sections with the same section name,
    SECNAME, linker picks the first one to define __start_SECNAME and
    __stop_SECNAME symbols.  When the first input section is removed by
    comdat group, we need to check if there is still an output section
    with section name SECNAME.
    
    	PR ld/23591
    	* ldlang.c (undef_start_stop): Lookup section in output with
    	the same name.
    	* testsuite/ld-elf/pr23591.d: New file.
    	* testsuite/ld-elf/pr23591a.s: Likewise.
    	* testsuite/ld-elf/pr23591b.s: Likewise.
    	* testsuite/ld-elf/pr23591c.s: Likewise.

Diff:
---
 ld/ChangeLog                   | 10 ++++++++++
 ld/ldlang.c                    | 18 ++++++++++++++++++
 ld/testsuite/ld-elf/pr23591.d  |  9 +++++++++
 ld/testsuite/ld-elf/pr23591a.s | 14 ++++++++++++++
 ld/testsuite/ld-elf/pr23591b.s | 11 +++++++++++
 ld/testsuite/ld-elf/pr23591c.s | 26 ++++++++++++++++++++++++++
 6 files changed, 88 insertions(+)

diff --git a/ld/ChangeLog b/ld/ChangeLog
index 6d929c7..8736047 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,13 @@
+018-08-31  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR ld/23591
+	* ldlang.c (undef_start_stop): Lookup section in output with
+	the same name.
+	* testsuite/ld-elf/pr23591.d: New file.
+	* testsuite/ld-elf/pr23591a.s: Likewise.
+	* testsuite/ld-elf/pr23591b.s: Likewise.
+	* testsuite/ld-elf/pr23591c.s: Likewise.
+
 2018-08-31  H.J. Lu  <hongjiu.lu@intel.com>
 
 	* testsuite/ld-i386/i386.exp: Also run for x86_64-*-elf* targets.
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 8878ccd..d644b56 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -6097,6 +6097,24 @@ undef_start_stop (struct bfd_link_hash_entry *h)
       || strcmp (h->u.def.section->name,
 		 h->u.def.section->output_section->name) != 0)
     {
+      asection *sec = bfd_get_section_by_name (link_info.output_bfd,
+					       h->u.def.section->name);
+      if (sec != NULL)
+	{
+	  /* When there are more than one input sections with the same
+	     section name, SECNAME, linker picks the first one to define
+	     __start_SECNAME and __stop_SECNAME symbols.  When the first
+	     input section is removed by comdat group, we need to check
+	     if there is still an output section with section name
+	     SECNAME.  */
+	  asection *i;
+	  for (i = sec->map_head.s; i != NULL; i = i->map_head.s)
+	    if (strcmp (h->u.def.section->name, i->name) == 0)
+	      {
+		h->u.def.section = i;
+		return;
+	      }
+	}
       h->type = bfd_link_hash_undefined;
       h->u.undef.abfd = NULL;
     }
diff --git a/ld/testsuite/ld-elf/pr23591.d b/ld/testsuite/ld-elf/pr23591.d
new file mode 100644
index 0000000..e002d73
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23591.d
@@ -0,0 +1,9 @@
+#source: pr23591a.s
+#source: pr23591b.s
+#source: pr23591c.s
+#ld: -e _start
+#readelf: -sW
+
+#...
+ +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +HIDDEN +[0-9]+ +___?start___sancov_cntrs
+#pass
diff --git a/ld/testsuite/ld-elf/pr23591a.s b/ld/testsuite/ld-elf/pr23591a.s
new file mode 100644
index 0000000..ebdb7f8
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23591a.s
@@ -0,0 +1,14 @@
+.ifdef UNDERSCORE
+	.hidden ___start___sancov_cntrs
+.else
+	.hidden __start___sancov_cntrs
+.endif
+	.text
+	.globl	_start
+	.type	_start, %function
+_start:
+.ifdef UNDERSCORE
+	.dc.a	___start___sancov_cntrs
+.else
+	.dc.a	__start___sancov_cntrs
+.endif
diff --git a/ld/testsuite/ld-elf/pr23591b.s b/ld/testsuite/ld-elf/pr23591b.s
new file mode 100644
index 0000000..646e681
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23591b.s
@@ -0,0 +1,11 @@
+	.section .text,"axG",%progbits,foo1,comdat
+.ifdef UNDERSCORE
+	.globl _foo1
+	.type _foo1, %function
+_foo1:
+.else
+	.globl foo1
+	.type foo1, %function
+foo1:
+.endif
+	.byte 0
diff --git a/ld/testsuite/ld-elf/pr23591c.s b/ld/testsuite/ld-elf/pr23591c.s
new file mode 100644
index 0000000..338671c
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23591c.s
@@ -0,0 +1,26 @@
+	.section        __sancov_cntrs,"aG",%progbits,foo1,comdat
+	.long 0
+	.section .text,"axG",%progbits,foo1,comdat
+.ifdef UNDERSCORE
+	.globl _foo1
+	.type _foo1, %function
+_foo1:
+.else
+	.globl foo1
+	.type foo1, %function
+foo1:
+.endif
+	.long 0
+	.section        __sancov_cntrs,"aG",%progbits,foo2,comdat
+	.long 1
+	.section .text,"axG",%progbits,foo2,comdat
+.ifdef UNDERSCORE
+	.globl _foo2
+	.type _foo2, %function
+_foo2:
+	.long 1
+.else
+	.globl foo2
+	.type foo2, %function
+foo2:
+.endif



More information about the Binutils-cvs mailing list