[PATCH] elf: Drop GRP_COMDAT when localizing a global symbol

H.J. Lu hjl.tools@gmail.com
Mon Dec 6 16:54:42 GMT 2021


When a global symbol in a COMDAT group was changed to local, drop
GRP_COMDAT on the group.

	PR binutils/27931
	* objcopy.c (filter_symbols): Drop GRP_COMDAT when localizing a
	a global symbol in a COMDAT group.
	* testsuite/binutils-all/group-8.d: New file.
	* testsuite/binutils-all/group-8.s: Likewise.
	* testsuite/binutils-all/objcopy.exp: Run group-8.
---
 binutils/objcopy.c                          | 15 +++++++++++++++
 binutils/testsuite/binutils-all/group-8.d   | 10 ++++++++++
 binutils/testsuite/binutils-all/group-8.s   |  6 ++++++
 binutils/testsuite/binutils-all/objcopy.exp |  1 +
 4 files changed, 32 insertions(+)
 create mode 100644 binutils/testsuite/binutils-all/group-8.d
 create mode 100644 binutils/testsuite/binutils-all/group-8.s

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 6faf8f33379..2fab71481df 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -1696,6 +1696,21 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
 		      && ! is_specified_symbol (name, keepglobal_specific_htab))
 		  || (localize_hidden && is_hidden_symbol (sym))))
 	    {
+	      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+		{
+		  sec_ptr s = sym->section;
+		  if (s != NULL
+		      && elf_section_data (s) != NULL
+		      && elf_group_name (s) != NULL)
+		    {
+		      /* When a global symbol in a COMDAT group was
+			 changed to local, drop GRP_COMDAT on the
+			 group.  */
+		      s = elf_sec_group (s);
+		      if (s != NULL && s->output_section != NULL)
+			s->output_section->flags &= ~SEC_LINK_ONCE;
+		    }
+		}
 	      sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
 	      sym->flags |= BSF_LOCAL;
 	    }
diff --git a/binutils/testsuite/binutils-all/group-8.d b/binutils/testsuite/binutils-all/group-8.d
new file mode 100644
index 00000000000..7d1eb45be14
--- /dev/null
+++ b/binutils/testsuite/binutils-all/group-8.d
@@ -0,0 +1,10 @@
+#PROG: objcopy
+#name: copy --localize-hidden
+#readelf: -Sg --wide
+#objcopy: --localize-hidden
+
+#...
+group section \[[ 0-9]+\] `\.group' \[foo\] contains . sections:
+   \[Index\]    Name
+   \[[ 0-9]+\]   \.text\.foo
+#pass
diff --git a/binutils/testsuite/binutils-all/group-8.s b/binutils/testsuite/binutils-all/group-8.s
new file mode 100644
index 00000000000..8fac59f9efb
--- /dev/null
+++ b/binutils/testsuite/binutils-all/group-8.s
@@ -0,0 +1,6 @@
+	.section .text.foo,"axG",%progbits,foo,comdat
+	.hidden foo
+	.weak   foo
+	.type   foo,%function
+foo:
+	.nop
diff --git a/binutils/testsuite/binutils-all/objcopy.exp b/binutils/testsuite/binutils-all/objcopy.exp
index 88ed3100ed0..6f85292366d 100644
--- a/binutils/testsuite/binutils-all/objcopy.exp
+++ b/binutils/testsuite/binutils-all/objcopy.exp
@@ -1135,6 +1135,7 @@ if [is_elf_format] {
     run_dump_test "group-7a"
     run_dump_test "group-7b"
     run_dump_test "group-7c"
+    run_dump_test "group-8"
     run_dump_test "copy-1"
     run_dump_test "note-1"
     # Use copytest.o from the note-1 test to determine ELF32 or ELF64
-- 
2.33.1



More information about the Binutils mailing list