public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Fix strip when sections are not sorted
@ 2007-07-25 15:46 Daniel Jacobowitz
  2007-07-27  0:29 ` Alan Modra
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Jacobowitz @ 2007-07-25 15:46 UTC (permalink / raw)
  To: binutils

I don't see anything in the gABI that requires sections to be sorted
by increasing address in the section table, and I can get GNU ld to
produce files where this is not the case; e.g. linking a shared
library with -Tbss will put .bss first in the section table, even if
you specify a high address.  Stripping such files fails, because we
assume the first section we encounter sets the base for the containing
segment.  Later on we discover that .data's file offset does not fit
in the segment, because .data has a lower address than the adjusted
.bss.

I think we should use the lowest section address, rather than the
first section encountered.  Is this patch OK?

-- 
Daniel Jacobowitz
CodeSourcery

2007-07-25  Daniel Jacobowitz  <dan@codesourcery.com>

	* elf.c (rewrite_elf_program_header): Handle sections not sorted
	by address.
	(copy_elf_program_header): Likewise.

---
 bfd/elf.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

Index: binutils-2.17.50/bfd/elf.c
===================================================================
--- binutils-2.17.50.orig/bfd/elf.c	2007-07-25 08:12:44.000000000 -0700
+++ binutils-2.17.50/bfd/elf.c	2007-07-25 08:14:31.000000000 -0700
@@ -5605,7 +5605,7 @@ rewrite_elf_program_header (bfd *ibfd, b
 		      IS_CONTAINED_BY_VMA (output_section, segment))
                 )
 		{
-		  if (matching_lma == 0)
+		  if (matching_lma == 0 || output_section->lma < matching_lma)
 		    matching_lma = output_section->lma;
 
 		  /* We assume that if the section fits within the segment
@@ -5858,6 +5858,7 @@ copy_elf_program_header (bfd *ibfd, bfd 
       bfd_size_type amt;
       Elf_Internal_Shdr *this_hdr;
       asection *first_section = NULL;
+      asection *lowest_section = NULL;
 
       /* FIXME: Do we need to copy PT_NULL segment?  */
       if (segment->p_type == PT_NULL)
@@ -5872,7 +5873,9 @@ copy_elf_program_header (bfd *ibfd, bfd 
 	  if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
 	    {
 	      if (!first_section)
-		first_section = section;
+		first_section = lowest_section = section;
+	      if (section->vma < lowest_section->vma)
+		lowest_section = section;
 	      section_count++;
 	    }
 	}
@@ -5918,7 +5921,7 @@ copy_elf_program_header (bfd *ibfd, bfd 
 
       if (!map->includes_phdrs && !map->includes_filehdr)
 	/* There is some other padding before the first section.  */
-	map->p_vaddr_offset = ((first_section ? first_section->lma : 0)
+	map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
 			       - segment->p_paddr);
       
       if (section_count != 0)

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

* Re: Fix strip when sections are not sorted
  2007-07-25 15:46 Fix strip when sections are not sorted Daniel Jacobowitz
@ 2007-07-27  0:29 ` Alan Modra
  2007-08-06 18:44   ` Daniel Jacobowitz
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Modra @ 2007-07-27  0:29 UTC (permalink / raw)
  To: binutils

On Wed, Jul 25, 2007 at 11:28:46AM -0400, Daniel Jacobowitz wrote:
> I think we should use the lowest section address, rather than the
> first section encountered.  Is this patch OK?

Looks reasonable to me,

>  	      if (!first_section)
> -		first_section = section;
> +		first_section = lowest_section = section;
> +	      if (section->vma < lowest_section->vma)
> +		lowest_section = section;
>  	      section_count++;

except that I'd be inclined to use lma here rather than vma.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: Fix strip when sections are not sorted
  2007-07-27  0:29 ` Alan Modra
@ 2007-08-06 18:44   ` Daniel Jacobowitz
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Jacobowitz @ 2007-08-06 18:44 UTC (permalink / raw)
  To: binutils

On Fri, Jul 27, 2007 at 09:57:53AM +0930, Alan Modra wrote:
> except that I'd be inclined to use lma here rather than vma.

Makes sense to me so I did that.  Thanks.

-- 
Daniel Jacobowitz
CodeSourcery

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

end of thread, other threads:[~2007-08-06 18:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-25 15:46 Fix strip when sections are not sorted Daniel Jacobowitz
2007-07-27  0:29 ` Alan Modra
2007-08-06 18:44   ` Daniel Jacobowitz

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).