objcopy --only-keep-debug and ELF program headers

Alan Modra amodra@bigpond.net.au
Tue May 15 08:39:00 GMT 2007


On Thu, May 10, 2007 at 03:28:08PM +0930, Alan Modra wrote:
> 	PR 4479
> 	* objcopy.c (copy_object): Don't copy ELF program headers for
> 	--only-keep-debug.

Sigh, that broke ia64, which twiddles section header types away from
SHT_NOBITS.  So do quite a few other backends.  I see mips has
apparently already run into this problem with strip --only-keep-debug,
and solved it in its elf_backend_fake_sections function.  This patch
moves the fix to elf_fake_sections, but uses a different heuristic to
detect --only-keep-debug.

	PR 4479
	* elf.c (elf_fake_sections): Don't allow backend to change
	SHT_NOBITS if called for strip/objcopy --only-keep-debug.
	* elfxx-mips.c (_bfd_mips_elf_fake_sections): Remove similar
	fix from here.

Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.388
diff -u -p -r1.388 elf.c
--- bfd/elf.c	14 May 2007 04:15:53 -0000	1.388
+++ bfd/elf.c	15 May 2007 06:42:13 -0000
@@ -2744,6 +2744,7 @@ elf_fake_sections (bfd *abfd, asection *
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   bfd_boolean *failedptr = failedptrarg;
   Elf_Internal_Shdr *this_hdr;
+  unsigned int sh_type;
 
   if (*failedptr)
     {
@@ -2901,10 +2902,19 @@ elf_fake_sections (bfd *abfd, asection *
     }
 
   /* Check for processor-specific section types.  */
+  sh_type = this_hdr->sh_type;
   if (bed->elf_backend_fake_sections
       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
     *failedptr = TRUE;
 
+  if (sh_type == SHT_NOBITS
+      && elf_elfheader (abfd)->e_phnum == 0)
+    {
+      /* Don't change the header type from NOBITS if we are being
+	 called for strip/objcopy --only-keep-debug.  */
+      this_hdr->sh_type = sh_type;
+    }
+
   /* If the section has relocs, set up a section header for the
      SHT_REL[A] section.  If two relocation sections are required for
      this section, it is up to the processor-specific back-end to
Index: bfd/elfxx-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-mips.c,v
retrieving revision 1.203
diff -u -p -r1.203 elfxx-mips.c
--- bfd/elfxx-mips.c	28 Apr 2007 22:31:45 -0000	1.203
+++ bfd/elfxx-mips.c	15 May 2007 08:36:07 -0000
@@ -5593,11 +5593,7 @@ _bfd_mips_elf_section_from_shdr (bfd *ab
 bfd_boolean
 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
 {
-  register const char *name;
-  unsigned int sh_type;
-
-  name = bfd_get_section_name (abfd, sec);
-  sh_type = hdr->sh_type;
+  const char *name = bfd_get_section_name (abfd, sec);
 
   if (strcmp (name, ".liblist") == 0)
     {
@@ -5698,12 +5694,6 @@ _bfd_mips_elf_fake_sections (bfd *abfd, 
       hdr->sh_entsize = 8;
     }
 
-  /* In the unlikely event a special section is empty it has to lose its
-     special meaning.  This may happen e.g. when using `strip' with the
-     "--only-keep-debug" option.  */
-  if (sec->size > 0 && !(sec->flags & SEC_HAS_CONTENTS))
-    hdr->sh_type = sh_type;
-
   /* The generic elf_fake_sections will set up REL_HDR using the default
    kind of relocations.  We used to set up a second header for the
    non-default kind of relocations here, but only NewABI would use

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre



More information about the Binutils mailing list