[PATCH] elf: Set ELFOSABI_GNU for SHF_GNU_RETAIN only for ld -r

H.J. Lu hjl.tools@gmail.com
Thu Dec 17 14:41:36 GMT 2020


Set ELFOSABI_GNU for SHF_GNU_RETAIN sections only when generating
relocatable output since SHF_GNU_RETAIN has no impact on shared object
nor executable.

I will check it in tomorrow if there are no objections.


H.J.
---
bfd/

	PR ld/27091
	* elf.c (_bfd_elf_final_write_processing): Set ELFOSABI_GNU
	for SHF_GNU_RETAIN sections only when generating ET_REL output.

ld/

	PR ld/27091
	* testsuite/ld-elf/retain7.s: New file.
	* testsuite/ld-elf/retain7a.d: Likewise.
	* testsuite/ld-elf/retain7b.d: Likewise.
---
 bfd/elf.c                      |  8 +++++---
 ld/testsuite/ld-elf/retain7.s  |  5 +++++
 ld/testsuite/ld-elf/retain7a.d | 11 +++++++++++
 ld/testsuite/ld-elf/retain7b.d | 10 ++++++++++
 4 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/retain7.s
 create mode 100644 ld/testsuite/ld-elf/retain7a.d
 create mode 100644 ld/testsuite/ld-elf/retain7b.d

diff --git a/bfd/elf.c b/bfd/elf.c
index 9c82912aeb..e601bfc1de 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -12466,9 +12466,11 @@ _bfd_elf_final_write_processing (bfd *abfd)
     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
 
   /* Set the osabi field to ELFOSABI_GNU if the binary contains
-     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
-     or STB_GNU_UNIQUE binding.  */
-  if (elf_tdata (abfd)->has_gnu_osabi != 0)
+     SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
+     or STB_GNU_UNIQUE binding.  Set the osabi field to ELFOSABI_GNU
+     for SHF_GNU_RETAIN sections only when generating ET_REL output.  */
+  if ((elf_tdata (abfd)->has_gnu_osabi
+      & (i_ehdrp->e_type == ET_REL ? -1 : ~elf_gnu_osabi_retain)) != 0)
     {
       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
diff --git a/ld/testsuite/ld-elf/retain7.s b/ld/testsuite/ld-elf/retain7.s
new file mode 100644
index 0000000000..a6cc86988c
--- /dev/null
+++ b/ld/testsuite/ld-elf/retain7.s
@@ -0,0 +1,5 @@
+	.type _start,"function"
+	.section .text,"axR",%progbits
+	.global _start
+_start:
+	.nop
diff --git a/ld/testsuite/ld-elf/retain7a.d b/ld/testsuite/ld-elf/retain7a.d
new file mode 100644
index 0000000000..544ff8a173
--- /dev/null
+++ b/ld/testsuite/ld-elf/retain7a.d
@@ -0,0 +1,11 @@
+#name: SHF_GNU_RETAIN 7a
+#source: retain7.s
+#ld: -e _start --gc-sections
+#notarget: ![supports_gnu_osabi] ![check_gc_sections_available]
+#readelf: -h
+
+#failif
+ELF Header:
+#...
+  OS/ABI:                            UNIX - GNU
+#pass
diff --git a/ld/testsuite/ld-elf/retain7b.d b/ld/testsuite/ld-elf/retain7b.d
new file mode 100644
index 0000000000..0e5b12b335
--- /dev/null
+++ b/ld/testsuite/ld-elf/retain7b.d
@@ -0,0 +1,10 @@
+#name: SHF_GNU_RETAIN 7a
+#source: retain7.s
+#ld: -r
+#notarget: ![supports_gnu_osabi] ![check_gc_sections_available]
+#readelf: -h
+
+ELF Header:
+#...
+  OS/ABI:                            UNIX - GNU
+#pass
-- 
2.29.2



More information about the Binutils mailing list