PATCH: Preserve segment virtual address only if needed

H. J. Lu hjl@lucon.org
Thu Nov 23 16:54:00 GMT 2006


On Wed, Nov 22, 2006 at 05:36:27PM +0100, Javier Cabezas Rodr?guez wrote:
> El mi??, 22-11-2006 a las 16:03 +0000, Nick Clifton escribi??:
> > Hi Javier,
> > 
> > > Now I have stripped some of these sections, but the size of the binary
> > > has not decreased. 
> > 
> > Possibly the sections have alignment requirements so that after 
> > stripping they are still padded out to some fixed size ?
> 
> Each section has a 4096 alignment requirement, but they are 4096bytes
> sized too, so it should not be a problem. I have attached the "readelf
> -Sl" output of each kernel.
> 
> 1- Original kernel:       2396464 bytes
> 2- Partitioned kernel: 2144924 bytes
> 3- Stripped kernel:       2143660 bytes
> 
> > Possibly the kernel is already frugal with the symbols and debug 
> > information it exports and there is nothing to strip.
> 
> The sections I want to strip only contain code (initially located in
> the .text section). The debug sections are already stripped in standard
> kernels.
> 

We don't need to preserve segment virtual address if the first section
is removed.


H.J.
----
2006-11-23  H.J. Lu  <hongjiu.lu@intel.com>

	* elf.c (rewrite_elf_program_header): Don't preserve segment
	virtual address if the first section is removed.

--- bfd/elf.c.strip	2006-11-23 07:55:03.000000000 -0800
+++ bfd/elf.c	2006-11-23 08:49:58.000000000 -0800
@@ -5427,16 +5427,30 @@ rewrite_elf_program_header (bfd *ibfd, b
       bfd_vma       suggested_lma;
       unsigned int  j;
       bfd_size_type amt;
+      asection *    first_section;
 
       if (segment->p_type == PT_NULL)
 	continue;
 
+      first_section = NULL;
       /* Compute how many sections might be placed into this segment.  */
       for (section = ibfd->sections, section_count = 0;
 	   section != NULL;
 	   section = section->next)
-	if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
-	  ++section_count;
+	{
+	  /* Find the first section in this segment.  */
+	  if (first_section == NULL)
+	    {
+	      Elf_Internal_Shdr *hdr;
+	      hdr = &(elf_section_data(section)->this_hdr);
+	      if ((bfd_vma) hdr->sh_offset == segment->p_offset
+		  && hdr->sh_addr == segment->p_vaddr
+		  && ELF_IS_SECTION_IN_SEGMENT_FILE (hdr, segment))
+		first_section = section;
+	    }
+	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
+	    ++section_count;
+	}
 
       /* Allocate a segment map big enough to contain
 	 all of the sections we have selected.  */
@@ -5452,8 +5466,13 @@ rewrite_elf_program_header (bfd *ibfd, b
       map->p_type        = segment->p_type;
       map->p_flags       = segment->p_flags;
       map->p_flags_valid = 1;
-      map->p_paddr       = segment->p_paddr;
-      map->p_paddr_valid = 1;
+      /* If the first section is removed, there is no need to preserve
+	 segment virtual address.  */
+      if (!first_section || first_section->output_section != NULL)
+	{
+	  map->p_paddr = segment->p_paddr;
+	  map->p_paddr_valid = 1;
+	}
 
       /* Determine if this segment contains the ELF file header
 	 and if it contains the program headers themselves.  */



More information about the Binutils mailing list