public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] don't strip symbols involved in relocations
@ 2007-04-23 15:31 Nathan Froyd
  2007-04-23 16:11 ` H. J. Lu
  0 siblings, 1 reply; 5+ messages in thread
From: Nathan Froyd @ 2007-04-23 15:31 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 613 bytes --]

The attached patch adds a safety check when stripping a symbol specified
on the command line: if the symbol is involved in a relocation, it
should not be stripped.

Tested on i586-none-vxworks.

OK?

-Nathan

binutils/
2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
	    Phil Edwards  <phil@codesourcery.com>

	* objcopy.c (filter_symbols): Explicitly stripping a symbol
	used in relocations is an error.

binutils/testsuite/
2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
	    Phil Edwards  <phil@codesourcery.com>

	* binutils-all/objcopy.exp: Add test for stripping a symbol
	used in a relocation.

[-- Attachment #2: dont-strip.patch --]
[-- Type: text/plain, Size: 3386 bytes --]

Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.107
diff -u -p -r1.107 objcopy.c
--- binutils/objcopy.c	12 Apr 2007 19:20:46 -0000	1.107
+++ binutils/objcopy.c	23 Apr 2007 15:03:08 -0000
@@ -910,6 +910,7 @@ filter_symbols (bfd *abfd, bfd *obfd, as
       flagword flags = sym->flags;
       char *name = (char *) bfd_asymbol_name (sym);
       int keep;
+      int used_in_reloc = 0;
       bfd_boolean undefined;
       bfd_boolean rem_leading_char;
       bfd_boolean add_leading_char;
@@ -982,7 +983,10 @@ filter_symbols (bfd *abfd, bfd *obfd, as
 	       || ((flags & BSF_SECTION_SYM) != 0
 		   && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
 		       & BSF_KEEP) != 0))
-	keep = 1;
+	{
+	  keep = 1;
+	  used_in_reloc = 1;
+	}
       else if (relocatable			/* Relocatable file.  */
 	       && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
 	keep = 1;
@@ -1012,7 +1016,18 @@ filter_symbols (bfd *abfd, bfd *obfd, as
 			|| ! bfd_is_local_label (abfd, sym))));
 
       if (keep && is_specified_symbol (name, strip_specific_list))
-	keep = 0;
+	{
+	  /* There are multiple ways to set 'keep' above, but if it was
+	     the relocatable symbol case, then that's an error.  */
+	  if (used_in_reloc)
+	    {
+	      non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
+	      status = 1;
+	      keep = 1;
+	    }
+	  else
+	    keep = 0;
+	}
       if (keep
 	  && !(flags & BSF_KEEP)
 	  && is_specified_symbol (name, strip_unneeded_list))
Index: binutils/testsuite/binutils-all/needed-reloc.s
===================================================================
RCS file: binutils/testsuite/binutils-all/needed-reloc.s
diff -N binutils/testsuite/binutils-all/needed-reloc.s
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/needed-reloc.s	23 Apr 2007 15:03:08 -0000
@@ -0,0 +1,7 @@
+        .globl foo
+
+	.data
+	.4byte	foo
+        .text
+foo:
+        .long 1
Index: binutils/testsuite/binutils-all/objcopy.exp
===================================================================
RCS file: /cvs/src/src/binutils/testsuite/binutils-all/objcopy.exp,v
retrieving revision 1.41
diff -u -p -r1.41 objcopy.exp
--- binutils/testsuite/binutils-all/objcopy.exp	12 Apr 2007 19:20:46 -0000	1.41
+++ binutils/testsuite/binutils-all/objcopy.exp	23 Apr 2007 15:03:08 -0000
@@ -723,6 +723,27 @@ if { ([istarget "ia64-*-elf*"]
     objcopy_test "ia64 link order" link-order.s
 }
 
+# This test is really only valid on platforms that have the right relocation
+# types for this to matter.  We choose i*86-*-vxworks arbitrarily.
+if { [istarget "i*86-*-vxworks"] } {
+    # Check to make sure we don't strip a symbol named in relocations.
+    set test "objcopy doesn't strip needed symbols"
+
+    set srcfile $srcdir/$subdir/needed-reloc.s
+
+    if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
+        unresolved $test
+    } else {
+        set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -N foo tmpdir/bintest.o ${copyfile}.o"]
+
+        if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
+            pass $test
+        } else {
+            fail $test
+        }
+    }
+}
+
 # ELF specific tests
 if [is_elf_format] {
     objcopy_test "ELF unknown section type" unknown.s

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

* Re: [PATCH] don't strip symbols involved in relocations
  2007-04-23 15:31 [PATCH] don't strip symbols involved in relocations Nathan Froyd
@ 2007-04-23 16:11 ` H. J. Lu
  2007-04-23 18:57   ` Nathan Froyd
  0 siblings, 1 reply; 5+ messages in thread
From: H. J. Lu @ 2007-04-23 16:11 UTC (permalink / raw)
  To: binutils

On Mon, Apr 23, 2007 at 08:07:52AM -0700, Nathan Froyd wrote:
> The attached patch adds a safety check when stripping a symbol specified
> on the command line: if the symbol is involved in a relocation, it
> should not be stripped.
> 
> Tested on i586-none-vxworks.
> 
> OK?
> 
> -Nathan
> 
> binutils/
> 2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
> 	    Phil Edwards  <phil@codesourcery.com>
> 
> 	* objcopy.c (filter_symbols): Explicitly stripping a symbol
> 	used in relocations is an error.
> 
> binutils/testsuite/
> 2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
> 	    Phil Edwards  <phil@codesourcery.com>
> 
> 	* binutils-all/objcopy.exp: Add test for stripping a symbol
> 	used in a relocation.


There is no entry for

binutils/testsuite/binutils-all/needed-reloc.s

Does it have to be vsworks specific?


H.J.

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

* Re: [PATCH] don't strip symbols involved in relocations
  2007-04-23 16:11 ` H. J. Lu
@ 2007-04-23 18:57   ` Nathan Froyd
  2007-04-23 23:07     ` H. J. Lu
  2007-04-24 13:35     ` Nick Clifton
  0 siblings, 2 replies; 5+ messages in thread
From: Nathan Froyd @ 2007-04-23 18:57 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 734 bytes --]

On Mon, Apr 23, 2007 at 08:55:34AM -0700, H. J. Lu wrote:
> There is no entry for
> 
> binutils/testsuite/binutils-all/needed-reloc.s

Argh, yes, of course.

> Does it have to be vsworks specific?

On second look, no, it doesn't.

Revised patch attached, ChangeLog entry below.

-Nathan

binutils/
2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
	    Phil Edwards  <phil@codesourcery.com>

	* objcopy.c (filter_symbols): Explicitly stripping a symbol
	used in relocations is an error.

binutils/testsuite/
2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
	    Phil Edwards  <phil@codesourcery.com>

	* binutils-all/objcopy.exp: Add test for stripping a symbol
	used in a relocation.
	* binutils-all/needed-reloc.s: New file.

[-- Attachment #2: dont-strip.patch --]
[-- Type: text/plain, Size: 3232 bytes --]

Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.107
diff -u -p -r1.107 objcopy.c
--- binutils/objcopy.c	12 Apr 2007 19:20:46 -0000	1.107
+++ binutils/objcopy.c	23 Apr 2007 16:09:35 -0000
@@ -910,6 +910,7 @@ filter_symbols (bfd *abfd, bfd *obfd, as
       flagword flags = sym->flags;
       char *name = (char *) bfd_asymbol_name (sym);
       int keep;
+      int used_in_reloc = 0;
       bfd_boolean undefined;
       bfd_boolean rem_leading_char;
       bfd_boolean add_leading_char;
@@ -982,7 +983,10 @@ filter_symbols (bfd *abfd, bfd *obfd, as
 	       || ((flags & BSF_SECTION_SYM) != 0
 		   && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
 		       & BSF_KEEP) != 0))
-	keep = 1;
+	{
+	  keep = 1;
+	  used_in_reloc = 1;
+	}
       else if (relocatable			/* Relocatable file.  */
 	       && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
 	keep = 1;
@@ -1012,7 +1016,18 @@ filter_symbols (bfd *abfd, bfd *obfd, as
 			|| ! bfd_is_local_label (abfd, sym))));
 
       if (keep && is_specified_symbol (name, strip_specific_list))
-	keep = 0;
+	{
+	  /* There are multiple ways to set 'keep' above, but if it was
+	     the relocatable symbol case, then that's an error.  */
+	  if (used_in_reloc)
+	    {
+	      non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
+	      status = 1;
+	      keep = 1;
+	    }
+	  else
+	    keep = 0;
+	}
       if (keep
 	  && !(flags & BSF_KEEP)
 	  && is_specified_symbol (name, strip_unneeded_list))
Index: binutils/testsuite/binutils-all/needed-reloc.s
===================================================================
RCS file: binutils/testsuite/binutils-all/needed-reloc.s
diff -N binutils/testsuite/binutils-all/needed-reloc.s
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/needed-reloc.s	23 Apr 2007 16:09:35 -0000
@@ -0,0 +1,7 @@
+        .globl foo
+
+	.data
+	.4byte	foo
+        .text
+foo:
+        .long 1
Index: binutils/testsuite/binutils-all/objcopy.exp
===================================================================
RCS file: /cvs/src/src/binutils/testsuite/binutils-all/objcopy.exp,v
retrieving revision 1.41
diff -u -p -r1.41 objcopy.exp
--- binutils/testsuite/binutils-all/objcopy.exp	12 Apr 2007 19:20:46 -0000	1.41
+++ binutils/testsuite/binutils-all/objcopy.exp	23 Apr 2007 16:09:35 -0000
@@ -723,6 +723,25 @@ if { ([istarget "ia64-*-elf*"]
     objcopy_test "ia64 link order" link-order.s
 }
 
+if { [istarget "i*86-*"] } {
+    # Check to make sure we don't strip a symbol named in relocations.
+    set test "objcopy doesn't strip needed symbols"
+
+    set srcfile $srcdir/$subdir/needed-reloc.s
+
+    if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
+        unresolved $test
+    } else {
+        set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -N foo tmpdir/bintest.o ${copyfile}.o"]
+
+        if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
+            pass $test
+        } else {
+            fail $test
+        }
+    }
+}
+
 # ELF specific tests
 if [is_elf_format] {
     objcopy_test "ELF unknown section type" unknown.s

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

* Re: [PATCH] don't strip symbols involved in relocations
  2007-04-23 18:57   ` Nathan Froyd
@ 2007-04-23 23:07     ` H. J. Lu
  2007-04-24 13:35     ` Nick Clifton
  1 sibling, 0 replies; 5+ messages in thread
From: H. J. Lu @ 2007-04-23 23:07 UTC (permalink / raw)
  To: binutils

On Mon, Apr 23, 2007 at 09:11:11AM -0700, Nathan Froyd wrote:
> On Mon, Apr 23, 2007 at 08:55:34AM -0700, H. J. Lu wrote:
> > There is no entry for
> > 
> > binutils/testsuite/binutils-all/needed-reloc.s
> 
> Argh, yes, of course.
> 
> > Does it have to be vsworks specific?
> 
> On second look, no, it doesn't.
> 

Can you enable the test for x86-64 also?


H.J.

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

* Re: [PATCH] don't strip symbols involved in relocations
  2007-04-23 18:57   ` Nathan Froyd
  2007-04-23 23:07     ` H. J. Lu
@ 2007-04-24 13:35     ` Nick Clifton
  1 sibling, 0 replies; 5+ messages in thread
From: Nick Clifton @ 2007-04-24 13:35 UTC (permalink / raw)
  To: binutils

Hi Nathan,

> binutils/
> 2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
> 	    Phil Edwards  <phil@codesourcery.com>
> 
> 	* objcopy.c (filter_symbols): Explicitly stripping a symbol
> 	used in relocations is an error.
> 
> binutils/testsuite/
> 2007-04-23  Nathan Froyd  <froydnj@codesourcery.com>
> 	    Phil Edwards  <phil@codesourcery.com>
> 
> 	* binutils-all/objcopy.exp: Add test for stripping a symbol
> 	used in a relocation.
> 	* binutils-all/needed-reloc.s: New file.

Approved and applied.

Note: I also changed the type of the 'keep' variable to a bfd_boolean in 
keeping with the rest of the code in that function.  Also I enabled the 
test for x86_64 targets (per H.J.'s request) and renamed the test file 
to needed-by-reloc.s.

Cheers
   Nick

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

end of thread, other threads:[~2007-04-24 10:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-23 15:31 [PATCH] don't strip symbols involved in relocations Nathan Froyd
2007-04-23 16:11 ` H. J. Lu
2007-04-23 18:57   ` Nathan Froyd
2007-04-23 23:07     ` H. J. Lu
2007-04-24 13:35     ` Nick Clifton

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