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]

Re: [PATCH] Fix section symbols in objcopy when removing sections with relocations from Elf objects


Julian Brown wrote:
Hi,

This patch fixes a problem where, if sections are removed during objcopy with Elf object files, the section symbols corresponding to the relocation sections for the removed sections may be left in the target object file.

This is a slightly-smarter version (thanks to offlist discussion with Dan) which knows about recoving Elf symbols from BFD symbols, hence avoids some unnecessary work.


Changelog as before.

Cheers,

Julian
Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.329
diff -c -p -r1.329 elf.c
*** bfd/elf.c	16 Mar 2006 12:20:15 -0000	1.329
--- bfd/elf.c	24 May 2006 14:21:21 -0000
*************** bfd_section_from_shdr (bfd *abfd, unsign
*** 1935,1940 ****
--- 1935,1941 ----
        BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
        elf_symtab_shndx (abfd) = shindex;
        elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
+       elf_tdata (abfd)->symtab_shndx_section = shindex;
        elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
        return TRUE;
  
*************** bfd_section_from_shdr (bfd *abfd, unsign
*** 1944,1949 ****
--- 1945,1951 ----
        if (ehdr->e_shstrndx == shindex)
  	{
  	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
+ 	  elf_tdata (abfd)->shstrtab_section = shindex;
  	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
  	  return TRUE;
  	}
*************** bfd_section_from_shdr (bfd *abfd, unsign
*** 1951,1956 ****
--- 1953,1959 ----
  	{
  	symtab_strtab:
  	  elf_tdata (abfd)->strtab_hdr = *hdr;
+ 	  elf_tdata (abfd)->strtab_section = shindex;
  	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
  	  return TRUE;
  	}
Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.95
diff -c -p -r1.95 objcopy.c
*** binutils/objcopy.c	28 Feb 2006 16:09:01 -0000	1.95
--- binutils/objcopy.c	24 May 2006 14:21:21 -0000
*************** filter_symbols (bfd *abfd, bfd *obfd, as
*** 915,920 ****
--- 915,945 ----
  	       || undefined
  	       || bfd_is_com_section (bfd_get_section (sym)))
  	keep = strip_symbols != STRIP_UNNEEDED;
+       else if ((flags & BSF_SECTION_SYM) != 0)  /* Section symbol.  */
+         {
+           struct bfd_section *section = sym->section;
+ 
+           keep = 1;
+ 
+           if (!bfd_is_abs_section (section)
+               && is_strip_section (abfd, section))
+ 	    keep = 0;
+           else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+             {
+               Elf_Internal_Shdr **elfsections = elf_elfsections (abfd);
+               elf_symbol_type *elfsym = elf_symbol_from (abfd, sym);
+               unsigned int sec = elfsym->internal_elf_sym.st_shndx;
+               Elf_Internal_Shdr *elfhdr = elfsections[sec];
+               
+               if (elfhdr->sh_type == SHT_REL || elfhdr->sh_type == SHT_RELA)
+                 {
+                   struct bfd_section *targsec =
+                     bfd_section_from_elf_index (abfd, elfhdr->sh_info);
+                   if (targsec && is_strip_section (abfd, targsec))
+                     keep = 0;
+                 }
+             }
+         }
        else if ((flags & BSF_DEBUGGING) != 0)	/* Debugging symbol.  */
  	keep = (strip_symbols != STRIP_DEBUG
  		&& strip_symbols != STRIP_UNNEEDED
*************** filter_symbols (bfd *abfd, bfd *obfd, as
*** 969,975 ****
  	      sym->flags |= BSF_GLOBAL;
  	    }
  
! 	  to[dst_count++] = sym;
  	}
      }
  
--- 994,1002 ----
  	      sym->flags |= BSF_GLOBAL;
  	    }
  
! 	  to[dst_count] = sym;
!           bfd_copy_private_symbol_data (abfd, sym, obfd, to[dst_count]);
!           dst_count++;
  	}
      }
  

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