elf: Remove zero-sized relocation section from section group

Message ID CAMe9rOr==MEhtCngohFy1g814Jfd1mJbL27LVT_8pPCjKzoJYA@mail.gmail.com
State New
Headers show
Series
  • elf: Remove zero-sized relocation section from section group
Related show

Commit Message

H.J. Lu via Binutils April 3, 2020, 8:48 p.m.
On Thu, Apr 2, 2020 at 9:45 PM Alan Modra <amodra@gmail.com> wrote:
>

> On Thu, Apr 02, 2020 at 08:41:18PM -0700, H.J. Lu via Binutils wrote:

> > We must keep zero-sized relocation section in a section group.  Otherwise,

> > the relocation section will be missing from the section group.

>

> Wouldn't it be better to get rid of these bogus relocation sections?

> Should be possible with a change to _bfd_elf_fixup_group_sections, I

> think.


Here is the patch which does that.  OK for master?

Thanks.

-- 
H.J.

Comments

H.J. Lu via Binutils April 4, 2020, 12:36 a.m. | #1
On Fri, Apr 03, 2020 at 01:48:42PM -0700, H.J. Lu wrote:
> On Thu, Apr 2, 2020 at 9:45 PM Alan Modra <amodra@gmail.com> wrote:

> >

> > On Thu, Apr 02, 2020 at 08:41:18PM -0700, H.J. Lu via Binutils wrote:

> > > We must keep zero-sized relocation section in a section group.  Otherwise,

> > > the relocation section will be missing from the section group.

> >

> > Wouldn't it be better to get rid of these bogus relocation sections?

> > Should be possible with a change to _bfd_elf_fixup_group_sections, I

> > think.

> 

> Here is the patch which does that.  OK for master?


OK, thanks.

Hmm, do we want a warning message?  I'm OK with it as a reminder to
people to fix their compilers and/or assembly, but other than that it
seems to me that removing an empty relocation section is hardly of
concern to users.  How was your testcase generated?

-- 
Alan Modra
Australia Development Lab, IBM
H.J. Lu via Binutils April 4, 2020, 1:56 a.m. | #2
On Fri, Apr 3, 2020 at 5:36 PM Alan Modra <amodra@gmail.com> wrote:
>

> On Fri, Apr 03, 2020 at 01:48:42PM -0700, H.J. Lu wrote:

> > On Thu, Apr 2, 2020 at 9:45 PM Alan Modra <amodra@gmail.com> wrote:

> > >

> > > On Thu, Apr 02, 2020 at 08:41:18PM -0700, H.J. Lu via Binutils wrote:

> > > > We must keep zero-sized relocation section in a section group.  Otherwise,

> > > > the relocation section will be missing from the section group.

> > >

> > > Wouldn't it be better to get rid of these bogus relocation sections?

> > > Should be possible with a change to _bfd_elf_fixup_group_sections, I

> > > think.

> >

> > Here is the patch which does that.  OK for master?

>

> OK, thanks.

>

> Hmm, do we want a warning message?  I'm OK with it as a reminder to


I will remove the warning.

> people to fix their compilers and/or assembly, but other than that it

> seems to me that removing an empty relocation section is hardly of

> concern to users.  How was your testcase generated?

>


I was given an object file generated by an "unnamed" compiler :-).
I have told them to fix their compiler.

Thanks.

-- 
H.J.
H.J. Lu via Binutils April 4, 2020, 2:05 a.m. | #3
On Fri, Apr 3, 2020 at 6:56 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>

> On Fri, Apr 3, 2020 at 5:36 PM Alan Modra <amodra@gmail.com> wrote:

> >

> > On Fri, Apr 03, 2020 at 01:48:42PM -0700, H.J. Lu wrote:

> > > On Thu, Apr 2, 2020 at 9:45 PM Alan Modra <amodra@gmail.com> wrote:

> > > >

> > > > On Thu, Apr 02, 2020 at 08:41:18PM -0700, H.J. Lu via Binutils wrote:

> > > > > We must keep zero-sized relocation section in a section group.  Otherwise,

> > > > > the relocation section will be missing from the section group.

> > > >

> > > > Wouldn't it be better to get rid of these bogus relocation sections?

> > > > Should be possible with a change to _bfd_elf_fixup_group_sections, I

> > > > think.

> > >

> > > Here is the patch which does that.  OK for master?

> >

> > OK, thanks.

> >

> > Hmm, do we want a warning message?  I'm OK with it as a reminder to

>

> I will remove the warning.

>

> > people to fix their compilers and/or assembly, but other than that it

> > seems to me that removing an empty relocation section is hardly of

> > concern to users.  How was your testcase generated?

> >

>

> I was given an object file generated by an "unnamed" compiler :-).

> I have told them to fix their compiler.

>

> Thanks.


This is what I am checking in.

-- 
H.J.
From fad689e7b5d92d50986ec040071e84b5eb50604f Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 2 Apr 2020 20:38:02 -0700
Subject: [PATCH] elf: Remove zero-sized relocation section from section group

Remove zero-sized relocation section from a section group since it has
been removed from the output.

	PR ld/25767
	* elf.c (_bfd_elf_fixup_group_sections): Remove zero-sized
	relocation section from section group.
---
 bfd/elf.c | 37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 86dadea05c..1780074f5a 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -7929,19 +7929,34 @@ _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
 		elf_group_name (s->output_section) = NULL;
 	      }
-	    /* Conversely, if the member section is not being output
-	       but the SHT_GROUP section is, then adjust its size.  */
-	    else if (s->output_section == discarded
-		     && isec->output_section != discarded)
+	    else
 	      {
 		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;
+		if (s->output_section == discarded
+		    && isec->output_section != discarded)
+		  {
+		    /* Conversely, if the member section is not being
+		       output but the SHT_GROUP section is, then adjust
+		       its size.  */
+		    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;
+		  }
+		else
+		  {
+		    /* Also adjust for zero-sized relocation member
+		       section.  */
+		    if (elf_sec->rel.hdr != NULL
+			&& elf_sec->rel.hdr->sh_size == 0)
+		      removed += 4;
+		    if (elf_sec->rela.hdr != NULL
+			&& elf_sec->rela.hdr->sh_size == 0)
+		      removed += 4;
+		  }
 	      }
 	    s = elf_next_in_group (s);
 	    if (s == first)

Patch

From db62e0099b194aa7a91bf7b705feb23d6f3f1756 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 2 Apr 2020 20:38:02 -0700
Subject: [PATCH] elf: Remove zero-sized relocation section from section group

Remove zero-sized relocation section from a section group since it has
been removed from the output.

	PR ld/25767
	* elflink.c (_bfd_elf_fixup_group_sections): Remove zero-sized
	relocation section from section group.
---
 bfd/elf.c | 58 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 11 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 86dadea05c..249cb3ff1f 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -7929,19 +7929,55 @@  _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
 		elf_group_name (s->output_section) = NULL;
 	      }
-	    /* Conversely, if the member section is not being output
-	       but the SHT_GROUP section is, then adjust its size.  */
-	    else if (s->output_section == discarded
-		     && isec->output_section != discarded)
+	    else
 	      {
 		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;
+		if (s->output_section == discarded
+		    && isec->output_section != discarded)
+		  {
+		    /* Conversely, if the member section is not being
+		       output but the SHT_GROUP section is, then adjust
+		       its size.  */
+		    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;
+		  }
+		else
+		  {
+		    const char *name;
+		    /* Also adjust for zero-sized relocation member
+		       section.  */
+		    if (elf_sec->rel.hdr != NULL
+			&& elf_sec->rel.hdr->sh_size == 0)
+		      {
+			removed += 4;
+			name = bfd_elf_string_from_elf_section
+			  (ibfd, (elf_elfheader (ibfd)->e_shstrndx),
+			   elf_sec->rel.hdr->sh_name);
+			_bfd_error_handler
+			  /* xgettext:c-format */
+			  (_("%pB: warning: zero-sized relocation section "
+			     "%s' for section %pA found - ignoring"),
+			   ibfd, name, s);
+		      }
+		    if (elf_sec->rela.hdr != NULL
+			&& elf_sec->rela.hdr->sh_size == 0)
+		      {
+			removed += 4;
+			name = bfd_elf_string_from_elf_section
+			  (ibfd, (elf_elfheader (ibfd)->e_shstrndx),
+			   elf_sec->rela.hdr->sh_name);
+			_bfd_error_handler
+			  /* xgettext:c-format */
+			  (_("%pB: warning: zero-sized relocation section "
+			     "%s' for section %pA found - ignoring"),
+			   ibfd, name, s);
+		      }
+		  }
 	      }
 	    s = elf_next_in_group (s);
 	    if (s == first)
-- 
2.25.1