Stripping empty sections

Alan Modra amodra@bigpond.net.au
Fri Jun 10 10:37:00 GMT 2005


We now have everything in place to strip empty sections at the right
time, before .dynsym space is allocated.  Which means we shouldn't
see those weird NOTYPE LOCAL DEFAULT UND syms any more, except for
the very first entry of .dynsym, as it should be.  Unfortunately, this
change requires updating so many of the testsuite results that it isn't
funny at all.

	* ldlang.c (strip_excluded_output_sections): Strip zero size
	sections here.
	* emultempl/elf32.em (gld*_strip_empty_sections): Delete.
	(gld*_finish): Don't call the above.
	* emultempl/hppaelf.em (hppaelf_finish): Likewise.
	* emultempl/ppc64elf.em (ppc_finish): Likewise.

I'll be committing this patch later, when I have most of the testsuite
changes done too.

Index: ld/ldlang.c
===================================================================
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.189
diff -u -p -r1.189 ldlang.c
--- ld/ldlang.c	9 Jun 2005 02:05:46 -0000	1.189
+++ ld/ldlang.c	10 Jun 2005 09:58:42 -0000
@@ -3086,39 +3086,40 @@ strip_excluded_output_sections (void)
       if (output_section == NULL)
 	continue;
 
-      exclude = TRUE;
-      if (output_section->map_head.s != NULL)
+      exclude = (output_section->rawsize == 0
+		 && (output_section->flags & SEC_KEEP) == 0
+		 && !bfd_section_removed_from_list (output_bfd,
+						    output_section));
+
+      /* Some sections have not yet been sized, notably .gnu.version,
+	 .dynsym, .dynstr and .hash.  These all have SEC_LINKER_CREATED
+	 input sections, so don't drop output sections that have such
+	 input sections unless they are also marked SEC_EXCLUDE.  */
+      if (exclude && output_section->map_head.s != NULL)
 	{
 	  asection *s;
 
-	  for (s = output_section->map_head.s; s != NULL;
-	       s = s->map_head.s)
-	    if ((s->flags & SEC_EXCLUDE) == 0)
+	  for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
+	    if ((s->flags & SEC_LINKER_CREATED) != 0
+		&& (s->flags & SEC_EXCLUDE) == 0)
 	      {
 		exclude = FALSE;
 		break;
 	      }
-
-	  output_section->map_head.link_order = NULL;
-	  output_section->map_tail.link_order = NULL;
 	}
 
-      if (exclude
-	  && (output_section->flags & SEC_KEEP) == 0
-	  && output_section->rawsize == 0
-	  && !bfd_is_abs_section (output_section))
+      /* TODO: Don't just junk map_head.s, turn them into link_orders.  */
+      output_section->map_head.link_order = NULL;
+      output_section->map_tail.link_order = NULL;
+
+      if (exclude)
 	{
 	  /* We don't set bfd_section to NULL since bfd_section of the
 	     removed output section statement may still be used.  */
 	  os->ignored = TRUE;
 	  output_section->flags |= SEC_EXCLUDE;
-
-	  if (!bfd_section_removed_from_list (output_bfd,
-					      output_section))
-	    {
-	      bfd_section_list_remove (output_bfd, output_section);
-	      output_bfd->section_count--;
-	    }
+	  bfd_section_list_remove (output_bfd, output_section);
+	  output_bfd->section_count--;
 	}
     }
 
Index: ld/emultempl/elf32.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/elf32.em,v
retrieving revision 1.148
diff -u -p -r1.148 elf32.em
--- ld/emultempl/elf32.em	9 Jun 2005 02:05:47 -0000	1.148
+++ ld/emultempl/elf32.em	10 Jun 2005 09:58:43 -0000
@@ -61,7 +61,6 @@ static void gld${EMULATION_NAME}_before_
 static bfd_boolean gld${EMULATION_NAME}_place_orphan
   (lang_input_statement_type *file, asection *s);
 static void gld${EMULATION_NAME}_layout_sections_again (void);
-static void gld${EMULATION_NAME}_strip_empty_sections (void);
 static void gld${EMULATION_NAME}_provide_init_fini_syms (void);
 static void gld${EMULATION_NAME}_finish (void) ATTRIBUTE_UNUSED;
 
@@ -1507,40 +1506,11 @@ gld${EMULATION_NAME}_layout_sections_aga
 }
 
 static void
-gld${EMULATION_NAME}_strip_empty_sections (void)
-{
-  if (!link_info.relocatable)
-    {
-      lang_output_section_statement_type *os;
-
-      for (os = &lang_output_section_statement.head->output_section_statement;
-	   os != NULL;
-	   os = os->next)
-	{
-	  asection *s;
-
-	  if (os == abs_output_section || os->constraint == -1)
-	    continue;
-	  s = os->bfd_section;
-	  if (s != NULL
-	      && s->size == 0
-	      && (s->flags & SEC_KEEP) == 0
-	      && !bfd_section_removed_from_list (output_bfd, s))
-	    {
-	      bfd_section_list_remove (output_bfd, s);
-	      output_bfd->section_count--;
-	    }
-	}
-    }
-}
-
-static void
 gld${EMULATION_NAME}_finish (void)
 {
   if (bfd_elf_discard_info (output_bfd, &link_info))
     gld${EMULATION_NAME}_layout_sections_again ();
 
-  gld${EMULATION_NAME}_strip_empty_sections ();
   gld${EMULATION_NAME}_provide_init_fini_syms ();
 }
 EOF
Index: ld/emultempl/hppaelf.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/hppaelf.em,v
retrieving revision 1.41
diff -u -p -r1.41 hppaelf.em
--- ld/emultempl/hppaelf.em	12 May 2005 07:32:03 -0000	1.41
+++ ld/emultempl/hppaelf.em	10 Jun 2005 09:58:43 -0000
@@ -305,7 +305,6 @@ hppaelf_finish (void)
 	}
     }
 
-  gld${EMULATION_NAME}_strip_empty_sections ();
   gld${EMULATION_NAME}_provide_init_fini_syms ();
 }
 
Index: ld/emultempl/ppc64elf.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/ppc64elf.em,v
retrieving revision 1.45
diff -u -p -r1.45 ppc64elf.em
--- ld/emultempl/ppc64elf.em	9 Jun 2005 02:05:47 -0000	1.45
+++ ld/emultempl/ppc64elf.em	10 Jun 2005 09:58:44 -0000
@@ -379,7 +379,6 @@ ppc_finish (void)
     }
 
   ppc64_elf_restore_symbols (&link_info);
-  gld${EMULATION_NAME}_strip_empty_sections ();
   gld${EMULATION_NAME}_provide_init_fini_syms ();
 }
 

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre



More information about the Binutils mailing list