This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix assign_file_positions_for_load_sections() for STRIP_NONDEBUG


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;
 		}
 	    }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]