PR22836, "-r -s" doesn't work with -g3 using GCC 7
authorAlan Modra <amodra@gmail.com>
Tue, 13 Feb 2018 03:39:48 +0000 (14:09 +1030)
committerAlan Modra <amodra@gmail.com>
Tue, 13 Feb 2018 12:25:49 +0000 (22:55 +1030)
This fixes the case where all of a group is removed with ld -r, the
situation in the PR, and failures where part of a group is removed
that contain relocs.

bfd/
PR 22836
* elf.c (_bfd_elf_fixup_group_sections): Account for removed
relocation sections.  If size reduces to just the flag word,
remove that too and mark with SEC_EXCLUDE.
* elflink.c (bfd_elf_final_link): Strip empty group sections.
binutils/
* testsuite/binutils-all/group-7.s,
* testsuite/binutils-all/group-7a.d,
* testsuite/binutils-all/group-7b.d,
* testsuite/binutils-all/group-7c.d: New tests.
* testsuite/binutils-all/objcopy.exp: Run them.
ld/
* testsuite/ld-elf/pr22836-2.d,
* testsuite/ld-elf/pr22836-2.s: New test.

12 files changed:
bfd/ChangeLog
bfd/elf.c
bfd/elflink.c
binutils/ChangeLog
binutils/testsuite/binutils-all/group-7.s [new file with mode: 0644]
binutils/testsuite/binutils-all/group-7a.d [new file with mode: 0644]
binutils/testsuite/binutils-all/group-7b.d [new file with mode: 0644]
binutils/testsuite/binutils-all/group-7c.d [new file with mode: 0644]
binutils/testsuite/binutils-all/objcopy.exp
ld/ChangeLog
ld/testsuite/ld-elf/pr22836-2.d [new file with mode: 0644]
ld/testsuite/ld-elf/pr22836-2.s [new file with mode: 0644]

index c93141458040a43bbcdf9e1350dc3ae26b234a27..35fc174685cba482cccfa14d162ac91c18444b94 100644 (file)
@@ -1,3 +1,11 @@
+2018-02-13  Alan Modra  <amodra@gmail.com>
+
+       PR 22836
+       * elf.c (_bfd_elf_fixup_group_sections): Account for removed
+       relocation sections.  If size reduces to just the flag word,
+       remove that too and mark with SEC_EXCLUDE.
+       * elflink.c (bfd_elf_final_link): Strip empty group sections.
+
 2018-02-13  Alan Modra  <amodra@gmail.com>
 
        PR 22829
index 05031546f3f483b6920964f84e9558b0fda72a01..934052d7fd114ad247ff95256b9d9112dbbf68cb 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -7604,7 +7604,16 @@ _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
               but the SHT_GROUP section is, then adjust its size.  */
            else if (s->output_section == discarded
                     && isec->output_section != discarded)
-             removed += 4;
+             {
+               struct bfd_elf_section_data *elf_sec = elf_section_data (s);
+               removed += 4;
+               if (elf_sec->rel.hdr != NULL
+                   && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
+                 removed += 4;
+               if (elf_sec->rela.hdr != NULL
+                   && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
+                 removed += 4;
+             }
            s = elf_next_in_group (s);
            if (s == first)
              break;
@@ -7614,18 +7623,26 @@ _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
            if (discarded != NULL)
              {
                /* If we've been called for ld -r, then we need to
-                  adjust the input section size.  This function may
-                  be called multiple times, so save the original
-                  size.  */
+                  adjust the input section size.  */
                if (isec->rawsize == 0)
                  isec->rawsize = isec->size;
                isec->size = isec->rawsize - removed;
+               if (isec->size <= 4)
+                 {
+                   isec->size = 0;
+                   isec->flags |= SEC_EXCLUDE;
+                 }
              }
            else
              {
                /* Adjust the output section size when called from
                   objcopy. */
                isec->output_section->size -= removed;
+               if (isec->output_section->size <= 4)
+                 {
+                   isec->output_section->size = 0;
+                   isec->output_section->flags |= SEC_EXCLUDE;
+                 }
              }
          }
       }
index d1eb82020cf4c5cef4cd609d5a1e0909fa8856d2..6eb47ee77cea1e26a4358ef6529fa48096f84270 100644 (file)
@@ -11618,6 +11618,13 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
          else
            o->flags |= SEC_EXCLUDE;
        }
+      else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
+       {
+         /* Remove empty group section from linker output.  */
+         o->flags |= SEC_EXCLUDE;
+         bfd_section_list_remove (abfd, o);
+         abfd->section_count--;
+       }
     }
 
   /* Count up the number of relocations we will output for each output
index 5fe9d97b386bd1b17247e397b2ff02d895616b6e..19b95072b08a55d1bbebd800d5ce4adb2f7719be 100644 (file)
@@ -1,3 +1,12 @@
+2018-02-13  Alan Modra  <amodra@gmail.com>
+
+       PR 22836
+       * testsuite/binutils-all/group-7.s,
+       * testsuite/binutils-all/group-7a.d,
+       * testsuite/binutils-all/group-7b.d,
+       * testsuite/binutils-all/group-7c.d: New tests.
+       * testsuite/binutils-all/objcopy.exp: Run them.
+
 2018-02-08  Nick Clifton  <nickc@redhat.com>
 
        PR 22802
diff --git a/binutils/testsuite/binutils-all/group-7.s b/binutils/testsuite/binutils-all/group-7.s
new file mode 100644 (file)
index 0000000..5028afc
--- /dev/null
@@ -0,0 +1,6 @@
+       .section        .data.foo,"awG",%progbits,foo,comdat
+here:
+       .dc.a   here
+
+       .section        .data2.foo,"awG",%progbits,foo,comdat
+       .dc.a   0
diff --git a/binutils/testsuite/binutils-all/group-7a.d b/binutils/testsuite/binutils-all/group-7a.d
new file mode 100644 (file)
index 0000000..fa8db60
--- /dev/null
@@ -0,0 +1,16 @@
+#name: copy removing reloc group member
+#source: group-7.s
+#PROG: objcopy
+#DUMPPROG: readelf
+#objcopy: --remove-section .data.foo
+#readelf: -Sg --wide
+
+#...
+  \[[ 0-9]+\] \.group[ \t]+GROUP[ \t]+.*
+#...
+  \[[ 0-9]+\] \.data2\.foo[ \t]+PROGBITS[ \t0-9a-f]+WAG.*
+#...
+COMDAT group section \[[ 0-9]+\] `\.group' \[foo\] contains 1 section.*
+   \[Index\]    Name
+   \[[ 0-9]+\]   \.data2\.foo
+#pass
diff --git a/binutils/testsuite/binutils-all/group-7b.d b/binutils/testsuite/binutils-all/group-7b.d
new file mode 100644 (file)
index 0000000..b674545
--- /dev/null
@@ -0,0 +1,19 @@
+#name: copy removing non-reloc group member
+#source: group-7.s
+#PROG: objcopy
+#DUMPPROG: readelf
+#objcopy: --remove-section .data2.foo
+#readelf: -Sg --wide
+
+#...
+  \[[ 0-9]+\] \.group[ \t]+GROUP[ \t]+.*
+#...
+  \[[ 0-9]+\] \.data\.foo[ \t]+PROGBITS[ \t0-9a-f]+WAG.*
+#...
+  \[[ 0-9]+\] \.rela?\.data\.foo[ \t]+RELA?[ \t0-9a-f]+IG.*
+#...
+COMDAT group section \[[ 0-9]+\] `\.group' \[foo\] contains 2 sections:
+   \[Index\]    Name
+   \[[ 0-9]+\]   \.data\.foo
+   \[[ 0-9]+\]   \.rela?\.data\.foo
+#pass
diff --git a/binutils/testsuite/binutils-all/group-7c.d b/binutils/testsuite/binutils-all/group-7c.d
new file mode 100644 (file)
index 0000000..83e9115
--- /dev/null
@@ -0,0 +1,8 @@
+#name: copy removing reloc and non-reloc group member
+#source: group-7.s
+#PROG: objcopy
+#DUMPPROG: readelf
+#objcopy: -R .data.foo -R .data2.foo
+#readelf: -g --wide
+
+There are no section groups in this file\.
index 377f88c0e16de365e543d40791306c5fdfab2a39..f4a7692cdf36bafc1f52e961f71add1eb5c94a61 100644 (file)
@@ -1051,6 +1051,9 @@ if [is_elf_format] {
     objcopy_test_readelf "GNU_MBIND section" mbind1.s
     run_dump_test "group-5"
     run_dump_test "group-6"
+    run_dump_test "group-7a"
+    run_dump_test "group-7b"
+    run_dump_test "group-7c"
     run_dump_test "copy-1"
     run_dump_test "note-1"
     if [is_elf64 tmpdir/bintest.o] {
index 236517ae5e862c64aab504ea1a30c59fe4020455..c5b35e648c789b2bf700223ce7da1743d36f7b68 100644 (file)
@@ -1,3 +1,9 @@
+2018-02-13  Alan Modra  <amodra@gmail.com>
+
+       PR 22836
+       * testsuite/ld-elf/pr22836-2.d,
+       * testsuite/ld-elf/pr22836-2.s: New test.
+
 2018-02-13  Alan Modra  <amodra@gmail.com>
 
        * testsuite/ld-x86-64/pr14207.d: Adjust relro p_filesz.
diff --git a/ld/testsuite/ld-elf/pr22836-2.d b/ld/testsuite/ld-elf/pr22836-2.d
new file mode 100644 (file)
index 0000000..10133e4
--- /dev/null
@@ -0,0 +1,7 @@
+#source: pr22836-2.s
+#ld: -r -S
+#readelf: -g --wide
+
+group section \[[ 0-9]+\] `\.group' \[foo\] contains 1 section.*
+   \[Index\]    Name
+   \[[ 0-9]+\]   \.comment
diff --git a/ld/testsuite/ld-elf/pr22836-2.s b/ld/testsuite/ld-elf/pr22836-2.s
new file mode 100644 (file)
index 0000000..77cd83a
--- /dev/null
@@ -0,0 +1,7 @@
+       .section        .debug_macro,"G",%progbits,foo
+       .long   .LASF0
+.LASF0:
+       .string "__STDC__ 1"
+
+       .section        .comment,"G",%progbits,foo
+       .asciz "hi"