]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
RELOC_AGAINST_DISCARDED_SECTION zero size reloc sections
authorAlan Modra <amodra@gmail.com>
Thu, 17 Jul 2025 23:56:10 +0000 (09:26 +0930)
committerAlan Modra <amodra@gmail.com>
Fri, 18 Jul 2025 23:04:15 +0000 (08:34 +0930)
For some reason the initial implementation (commit 0672748ac053) of
this macro didn't allow discarding of all relocs in a section, perhaps
because doing so would require a testsuite change.  This patch allows
zero size relocation sections to result, and adjusts the testsuite.
i386, x86_64, ppc and ppc64 code that avoids a memmove is also changed
to allow zero size reloc sections, and arc fixed to actually adjust
the reloc section header.

bfd/elf-bfd.h
bfd/elf32-arc.c
bfd/elf32-i386.c
bfd/elf32-ppc.c
bfd/elf64-ppc.c
bfd/elf64-x86-64.c
ld/testsuite/ld-elf/linkonce1.d

index fbb1cd9e8905e931548eabdf3c523499ee412091..c02e34e75ba0345e86522188a5cff8a577f66c24 100644 (file)
@@ -3287,25 +3287,20 @@ extern asection _bfd_elf_large_com_section;
        && ((input_section->flags & SEC_DEBUGGING) != 0                 \
            || elf_section_type (input_section) == SHT_GNU_SFRAME))     \
       {                                                                        \
-       Elf_Internal_Shdr *rel_hdr;                                     \
+       Elf_Internal_Shdr *rel_hdr                                      \
+         = _bfd_elf_single_rel_hdr (input_section->output_section);    \
                                                                        \
-       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
+       rel_hdr->sh_size -= rel_hdr->sh_entsize;                        \
+       rel_hdr = _bfd_elf_single_rel_hdr (input_section);              \
+       rel_hdr->sh_size -= rel_hdr->sh_entsize;                        \
                                                                        \
-       /* Avoid empty output section.  */                              \
-       if (rel_hdr->sh_size > rel_hdr->sh_entsize)                     \
-         {                                                             \
-           rel_hdr->sh_size -= rel_hdr->sh_entsize;                    \
-           rel_hdr = _bfd_elf_single_rel_hdr (input_section);          \
-           rel_hdr->sh_size -= rel_hdr->sh_entsize;                    \
+       memmove (rel, rel + count,                                      \
+                (relend - rel - count) * sizeof (*rel));               \
                                                                        \
-           memmove (rel, rel + count,                                  \
-                    (relend - rel - count) * sizeof (*rel));           \
-                                                                       \
-           input_section->reloc_count -= count;                        \
-           relend -= count;                                            \
-           rel--;                                                      \
-           continue;                                                   \
-         }                                                             \
+       input_section->reloc_count -= count;                            \
+       relend -= count;                                                \
+       rel--;                                                          \
+       continue;                                                       \
       }                                                                        \
                                                                        \
     for (int i_ = 0; i_ < count; i_++)                                 \
index 0a6e66569ba9872426b33118bcf2d8551272154d..a78516dc12f6d280ff33bea4c10f76dd1ae3e166 100644 (file)
@@ -1947,6 +1947,18 @@ elf_arc_relocate_section (bfd *                    output_bfd,
       return false;
     }
 
+  if (wrel != rel)
+    {
+      Elf_Internal_Shdr *rel_hdr;
+      size_t deleted = rel - wrel;
+
+      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
+      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
+      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
+      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
+      input_section->reloc_count -= deleted;
+    }
+
   return true;
 }
 
index b417cb0d67a7503e804611df6cc4ae7d38953477..abe26e476ff0c480fb91c972dd104df12af425a7 100644 (file)
@@ -3600,14 +3600,6 @@ elf_i386_relocate_section (bfd *output_bfd,
 
       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
-      if (rel_hdr->sh_size == 0)
-       {
-         /* It is too late to remove an empty reloc section.  Leave
-            one NONE reloc.
-            ??? What is wrong with an empty section???  */
-         rel_hdr->sh_size = rel_hdr->sh_entsize;
-         deleted -= 1;
-       }
       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
       input_section->reloc_count -= deleted;
index f17effdf1766399274e1bee9541d8378ad391bd9..3fd9f28cd5289d4b0f0c53ee601d8f6b25a14deb 100644 (file)
@@ -9142,15 +9142,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
 
       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
-      if (rel_hdr->sh_size == 0)
-       {
-         /* It is too late to remove an empty reloc section.  Leave
-            one NONE reloc.
-            ??? What is wrong with an empty section???  */
-         rel_hdr->sh_size = rel_hdr->sh_entsize;
-         deleted -= 1;
-         wrel++;
-       }
       relend = wrel;
       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
index 00c1c32ffd2baa7b5b9c4b8811b789674ac942d9..e6c90a68b82dec39f00e5d599485c55c79aa8d44 100644 (file)
@@ -18082,14 +18082,6 @@ ppc64_elf_relocate_section (bfd *output_bfd,
 
       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
-      if (rel_hdr->sh_size == 0)
-       {
-         /* It is too late to remove an empty reloc section.  Leave
-            one NONE reloc.
-            ??? What is wrong with an empty section???  */
-         rel_hdr->sh_size = rel_hdr->sh_entsize;
-         deleted -= 1;
-       }
       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
       input_section->reloc_count -= deleted;
index ebd2eb4299fec975f11f84cf4fdcc9f63fbfcc0d..b6f97b5b69be482d40a0bda21eeee70f9826de1d 100644 (file)
@@ -5099,14 +5099,6 @@ elf_x86_64_relocate_section (bfd *output_bfd,
 
       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
-      if (rel_hdr->sh_size == 0)
-       {
-         /* It is too late to remove an empty reloc section.  Leave
-            one NONE reloc.
-            ??? What is wrong with an empty section???  */
-         rel_hdr->sh_size = rel_hdr->sh_entsize;
-         deleted -= 1;
-       }
       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
       input_section->reloc_count -= deleted;
index e4f9a70826f89c934479b78181fba70ebcf1d3ba..654171dc7a6cec5638dcfe91905729fb996905f4 100644 (file)
@@ -2,11 +2,10 @@
 #source: linkonce1b.s
 #ld: -r
 #objdump: -r
+#xfail: [is_generic]
 
 .*:     file format .*
 #...
-RELOCATION RECORDS FOR \[.debug_frame\]:
-OFFSET[        ]+TYPE[         ]+VALUE[        ]*
-.*(NONE|unused|UNUSED).*\*ABS\*
+RELOCATION RECORDS FOR \[.debug_frame\]: \(none\)
 
 #pass