public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix assign_file_positions_for_load_sections() for  STRIP_NONDEBUG
@ 2008-10-07 16:39 Jan Blunck
  2008-10-08  8:32 ` Alan Modra
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Blunck @ 2008-10-07 16:39 UTC (permalink / raw)
  To: binutils

When objcopy is called with the option --only-keep-debug it might happen that
the first section in a load segment isn't marked for loading since it is
stripped. This doesn't mean that all section in a segment are not marked for
loading.

This failed on certain binaries when some section couldn't be allocated in a
segment because of the sh_offset was larger than the end of the segment in the
binary. Typically these binaries also had some padding at the beginning of the
file (e.g. introduced through either "-Wl,-zcommon-page-size=4194304
-Wl,-zmax-page-size=4194304" or "-Wl,--section-start,.interp=0x7bf00400")
which I couldn't find a reason for.

Signed-off-by: Jan Blunck <jblunck@suse.de>
---
 bfd/elf.c |   19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

Index: b/bfd/elf.c
===================================================================
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4258,18 +4258,23 @@ assign_file_positions_for_load_sections
 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
 
 	  /* Find out whether this segment contains any loadable
-	     sections.  If the first section isn't loadable, the same
-	     holds for any other sections.  */
-	  i = 0;
-	  while (elf_section_type (m->sections[i]) == SHT_NOBITS)
+	     sections.
+	     We have to look at all the sections until we find a loadable one,
+	     since with STRIP_NONDEBUG the first sections might just be set to
+	     SHT_NOBITS */
+	  no_contents = TRUE;
+	  for (i = 0; i < m->count; i++)
 	    {
 	      /* If a segment starts with .tbss, we need to look
 		 at the next section to decide whether the segment
 		 has any loadable sections.  */
-	      if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
-		  || ++i >= m->count)
+	      if ((elf_section_flags (m->sections[i]) & SHF_TLS) != 0
+		  && (i + 1 < m->count))
+		continue;
+
+	      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
 		{
-		  no_contents = TRUE;
+		  no_contents = FALSE;
 		  break;
 		}
 	    }

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix assign_file_positions_for_load_sections() for  STRIP_NONDEBUG
  2008-10-07 16:39 [PATCH] Fix assign_file_positions_for_load_sections() for STRIP_NONDEBUG Jan Blunck
@ 2008-10-08  8:32 ` Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2008-10-08  8:32 UTC (permalink / raw)
  To: Jan Blunck; +Cc: binutils

On Tue, Oct 07, 2008 at 06:38:44PM +0200, Jan Blunck wrote:
> When objcopy is called with the option --only-keep-debug it might happen that
> the first section in a load segment isn't marked for loading since it is
> stripped. This doesn't mean that all section in a segment are not marked for
> loading.
[snip]

Thanks for the analysis and patch.  I'm committing this simpler version.

	* elf.c (assign_file_positions_for_load_sections): When checking
	a segment for contents, don't assume that a non-TLS nobits section
	must only be followed by nobits sections.

Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.465
diff -u -p -r1.465 elf.c
--- bfd/elf.c	3 Oct 2008 09:40:48 -0000	1.465
+++ bfd/elf.c	8 Oct 2008 07:43:51 -0000
@@ -4288,21 +4288,14 @@ assign_file_positions_for_load_sections 
 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
 
 	  /* Find out whether this segment contains any loadable
-	     sections.  If the first section isn't loadable, the same
-	     holds for any other sections.  */
-	  i = 0;
-	  while (elf_section_type (m->sections[i]) == SHT_NOBITS)
-	    {
-	      /* If a segment starts with .tbss, we need to look
-		 at the next section to decide whether the segment
-		 has any loadable sections.  */
-	      if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
-		  || ++i >= m->count)
-		{
-		  no_contents = TRUE;
-		  break;
-		}
-	    }
+	     sections.  */
+	  no_contents = TRUE;
+	  for (i = 0; i < m->count; i++)
+	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
+	      {
+		no_contents = FALSE;
+		break;
+	      }
 
 	  off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
 	  off += off_adjust;

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-10-08  8:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-07 16:39 [PATCH] Fix assign_file_positions_for_load_sections() for STRIP_NONDEBUG Jan Blunck
2008-10-08  8:32 ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).