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]

Add an --extract-symbol option to objcopy


This is unfortunately one of those patches that is being submitted
upstream long after it has been used in the wild (namely for VxWorks).
I don't know how far back it dates, or who came up with the option
specification originally.

Anyway, the patch adds a --extract-symbol option to objcopy:

-------------------------------------------------------------------------
@item --extract-symbol
Keep the file's section flags and symbols but remove all section data.
Specifically, the option:

@itemize
@item sets the virtual and load addresses of every section to zero;
@item removes the contents of all sections;
@item sets the size of every section to zero; and
@item sets the file's start address to zero.
@end itemize
 
This option is used to build a @file{.sym} file for a VxWorks kernel.
It can also be a useful way of reducing the size of a @option{--just-symbols}
linker input file.
-------------------------------------------------------------------------

That last point might make --extract-symbol useful to a wider audience
than VxWorks users.  For example, it could be used to reduce the size of
uClinux *.gdb files.  Its primary purpose is still to create a VxWorks
symbol file though.

Of course, VxWorks users would get maximum benefit from this patch
if the option could keep the name --extract-symbol.  (The option is
hard-coded into the kernel build system.)  However, if your dislike of
the name overrides that -- and it might! -- then I'd still be interested
in adding the option under a more acceptable name.  The version of
binutils shipped with VxWorks could then treat --extract-symbol as
an alias for the renamed option.

The test needs to assemble something _and_ link it.  No binutils test
seems to do that at the moment, so I added support to run_test.
Also, I noticed that the use of binutils_assemble in run_test
was wrong; binutils_assemble returns a boolean success value,
not the assembler output.  I fixed that while I was there.

Tested on ia64-linux-gnu, x86_64-linux-gnu, mips64-linux-gnu and
mipsel-elf.  OK to install?

Richard


binutils/
200x-xx-xx  Phil Edwards  <phil@codesourcery.com>
	    Richard Sandiford  <richard@codesourcery.com>

	* NEWS: Mention addition of --extract-symbol.
	* doc/binutils.texi: Document it.
	* objcopy.c (extract_symbol): New variable.
	(OPTION_EXTRACT_SYMBOLS): New command_line_switch.
	(copy_options): Add an entry for --extract-symbol.
	(copy_usage): Mention --extract-symbol.
	(copy_object): Set the start address to zero for --extract-symbol.
	Do not copy private BFD data in that case.
	(setup_section): Set the size, LMA and VMA to zero for
	--extract-symbol.  Do not copy private BFD data in that case.
	(copy_section): Do not copy section contents if --extract-symbol
	is passed.
	(copy_main): Set extract_symbol to TRUE if --extract-symbol
	is passed.

binutils/testsuite/
	* config/default.exp (binutils_link): New procedure.
	* lib/utils-lib.exp (default_binutils_link): Likewise.
	(run_dump_test): Add support for "ld:".  Fix use of binutils_assemble.
	* binutils-all/extract-symbol-1.d, binutils-all/extract-symbol-1.s
	* binutils-all/extract-symbol-1.ld, binutils-all/extract-symbol-2.s
	* binutils-all/extract-symbol-2.d: New tests.
	* binutils-all/objcopy.exp: Run them on ELF targets.

Index: binutils/NEWS
===================================================================
RCS file: /cvs/src/src/binutils/NEWS,v
retrieving revision 1.57
diff -u -p -r1.57 NEWS
--- binutils/NEWS	14 Nov 2005 15:00:30 -0000	1.57
+++ binutils/NEWS	1 Mar 2007 12:55:30 -0000
@@ -1,5 +1,7 @@
 -*- text -*-
 
+* Add --extract-symbol to objcopy.
+
 * Add "-x NAME" to readelf in addition to "-x NUMBER".
 
 * Add -i and -t switches to cxxfilt.  -i disables the display of implementation
Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.105
diff -u -p -r1.105 objcopy.c
--- binutils/objcopy.c	17 Feb 2007 13:33:54 -0000	1.105
+++ binutils/objcopy.c	1 Mar 2007 12:55:31 -0000
@@ -215,6 +215,9 @@ static char *prefix_symbols_string = 0;
 static char *prefix_sections_string = 0;
 static char *prefix_alloc_sections_string = 0;
 
+/* True if --extract-symbol was passed on the command line.  */
+static bfd_boolean extract_symbol = FALSE;
+
 /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
 enum command_line_switch
   {
@@ -261,7 +264,8 @@ enum command_line_switch
     OPTION_READONLY_TEXT,
     OPTION_WRITABLE_TEXT,
     OPTION_PURE,
-    OPTION_IMPURE
+    OPTION_IMPURE,
+    OPTION_EXTRACT_SYMBOL
   };
 
 /* Options to handle if running as "strip".  */
@@ -317,6 +321,7 @@ static struct option copy_options[] =
   {"debugging", no_argument, 0, OPTION_DEBUGGING},
   {"discard-all", no_argument, 0, 'x'},
   {"discard-locals", no_argument, 0, 'X'},
+  {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
   {"format", required_argument, 0, 'F'}, /* Obsolete */
   {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
   {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
@@ -431,6 +436,7 @@ copy_usage (FILE *stream, int exit_statu
                                    Do not copy symbol <name> unless needed by\n\
                                      relocations\n\
      --only-keep-debug             Strip everything but the debug information\n\
+     --extract-symbol              Remove section contents but keep symbols\n\
   -K --keep-symbol <name>          Do not strip symbol <name>\n\
      --keep-file-symbols           Do not strip file symbol(s)\n\
      --localize-hidden             Turn all ELF hidden symbols into locals\n\
@@ -1310,11 +1316,16 @@ copy_object (bfd *ibfd, bfd *obfd)
 	    bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
 	    bfd_get_filename (obfd), bfd_get_target (obfd));
 
-  if (set_start_set)
-    start = set_start;
+  if (extract_symbol)
+    start = 0;
   else
-    start = bfd_get_start_address (ibfd);
-  start += change_start;
+    {
+      if (set_start_set)
+	start = set_start;
+      else
+	start = bfd_get_start_address (ibfd);
+      start += change_start;
+    }
 
   /* Neither the start address nor the flags
      need to be set for a core file.  */
@@ -1725,6 +1736,11 @@ copy_object (bfd *ibfd, bfd *obfd)
 	}
     }
 
+  /* Do not copy backend data if --extract-symbol is passed; anything
+     that needs to look at the section contents will fail.  */
+  if (extract_symbol)
+    return TRUE;
+
   /* Allow the BFD backend to copy any private data it understands
      from the input BFD to the output BFD.  This is done last to
      permit the routine to look at the filtered symbol table, which is
@@ -2184,6 +2200,8 @@ setup_section (bfd *ibfd, sec_ptr isecti
   size = bfd_section_size (ibfd, isection);
   if (copy_byte >= 0)
     size = (size + interleave - 1) / interleave;
+  else if (extract_symbol)
+    size = 0;
   if (! bfd_set_section_size (obfd, osection, size))
     {
       err = _("size");
@@ -2198,7 +2216,7 @@ setup_section (bfd *ibfd, sec_ptr isecti
   else
     vma += change_section_address;
 
-  if (! bfd_set_section_vma (obfd, osection, vma))
+  if (! bfd_set_section_vma (obfd, osection, extract_symbol ? 0 : vma))
     {
       err = _("vma");
       goto loser;
@@ -2217,7 +2235,7 @@ setup_section (bfd *ibfd, sec_ptr isecti
   else
     lma += change_section_address;
 
-  osection->lma = lma;
+  osection->lma = extract_symbol ? 0 : lma;
 
   /* FIXME: This is probably not enough.  If we change the LMA we
      may have to recompute the header for the file as well.  */
@@ -2236,7 +2254,12 @@ setup_section (bfd *ibfd, sec_ptr isecti
      bfd_get_section_by_name since some formats allow multiple
      sections with the same name.  */
   isection->output_section = osection;
-  isection->output_offset = 0;
+  isection->output_offset = extract_symbol ? vma : 0;
+
+  /* Do not copy backend data if --extract-symbol is passed; anything
+     that needs to look at the section contents will fail.  */
+  if (extract_symbol)
+    return;
 
   /* Allow the BFD backend to copy any private data it understands
      from the input section to the output section.  */
@@ -2354,6 +2377,9 @@ copy_section (bfd *ibfd, sec_ptr isectio
 	free (relpp);
     }
 
+  if (extract_symbol)
+    return;
+
   if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
       && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
     {
@@ -3231,6 +3257,10 @@ copy_main (int argc, char *argv[])
 	  bfd_flags_to_set &= ~D_PAGED;
 	  break;
 
+	case OPTION_EXTRACT_SYMBOL:
+	  extract_symbol = TRUE;
+	  break;
+
 	case 0:
 	  /* We've been given a long option.  */
 	  break;
Index: binutils/doc/binutils.texi
===================================================================
RCS file: /cvs/src/src/binutils/doc/binutils.texi,v
retrieving revision 1.99
diff -u -p -r1.99 binutils.texi
--- binutils/doc/binutils.texi	2 Feb 2007 15:27:04 -0000	1.99
+++ binutils/doc/binutils.texi	1 Mar 2007 12:55:32 -0000
@@ -996,6 +996,7 @@ objcopy [@option{-F} @var{bfdname}|@opti
         [@option{--add-gnu-debuglink=}@var{path-to-file}]
         [@option{--keep-file-symbols}]
         [@option{--only-keep-debug}]
+        [@option{--extract-symbol}]
         [@option{--writable-text}]
         [@option{--readonly-text}]
         [@option{--pure}]
@@ -1489,6 +1490,21 @@ currently only supports the presence of 
 debugging information, not multiple filenames on a one-per-object-file
 basis.
 
+@item --extract-symbol
+Keep the file's section flags and symbols but remove all section data.
+Specifically, the option:
+
+@itemize
+@item sets the virtual and load addresses of every section to zero;
+@item removes the contents of all sections;
+@item sets the size of every section to zero; and
+@item sets the file's start address to zero.
+@end itemize
+ 
+This option is used to build a @file{.sym} file for a VxWorks kernel.
+It can also be a useful way of reducing the size of a @option{--just-symbols}
+linker input file.
+
 @item -V
 @itemx --version
 Show the version number of @command{objcopy}.
Index: binutils/testsuite/config/default.exp
===================================================================
RCS file: /cvs/src/src/binutils/testsuite/config/default.exp,v
retrieving revision 1.11
diff -u -p -r1.11 default.exp
--- binutils/testsuite/config/default.exp	24 Dec 2005 11:17:21 -0000	1.11
+++ binutils/testsuite/config/default.exp	1 Mar 2007 12:55:33 -0000
@@ -113,3 +113,11 @@ proc binutils_run { prog progargs } {
 proc binutils_assemble { source object } {
     default_binutils_assemble $source $object
 }
+
+#
+# binutils_link
+#	link a file; see default_binutils_link for details
+#
+proc binutils_link { object destfile flags } {
+    default_binutils_link $object $destfile $flags
+}
Index: binutils/testsuite/lib/utils-lib.exp
===================================================================
RCS file: /cvs/src/src/binutils/testsuite/lib/utils-lib.exp,v
retrieving revision 1.13
diff -u -p -r1.13 utils-lib.exp
--- binutils/testsuite/lib/utils-lib.exp	20 Sep 2006 11:35:11 -0000	1.13
+++ binutils/testsuite/lib/utils-lib.exp	1 Mar 2007 12:55:33 -0000
@@ -119,6 +119,23 @@ proc default_binutils_assemble { source 
 }
 
 #
+# default_binutils_link
+#	Link a file.  The arguments are the same as for target_link.
+#	Return true on success and print an error on failure.
+#
+proc default_binutils_link { object destfile flags } {
+    set exec_output [prune_warnings [target_link $object $destfile $flags]]
+    if [string match "" $exec_output] {
+	return 1
+    } else {
+	send_log "$exec_output\n"
+	verbose "$exec_output"
+	perror "$object: link failed"
+	return 0
+    }
+}
+
+#
 # is_elf_format
 #	true if the object format is known to be ELF
 #
@@ -212,6 +229,11 @@ proc exe_ext {} {
 #   as: FLAGS
 #	When assembling FILE.s, pass FLAGS to the assembler.
 #
+#   ld: FLAGS
+#	Link the assembler output and pass the linked file through PROG.
+#	FLAGS are a list of flags to pass; they can refer directly to
+#	linker scripts in the source directory.
+#
 #   PROG: PROGRAM-NAME
 #       The name of the program to run to analyze the .o file produced
 #       by the assembler.  This can be omitted; run_dump_test will guess
@@ -316,6 +338,7 @@ proc run_dump_test { name {extra_options
     set opts(not-target) {}
     set opts(skip) {}
     set opts(not-skip) {}
+    set opts(ld) {}
 
     foreach i $opt_array {
 	set opt_name [lindex $i 0]
@@ -462,10 +485,19 @@ proc run_dump_test { name {extra_options
 	set srcfile $srcdir/$subdir/$opts(source)
     }
 
-    set exec_output [binutils_assemble ${srcfile} tmpdir/bintest.o]
-    if [string match "" $exec_output] then {
-	send_log "$exec_output\n"
-	verbose "$exec_output"
+    if { $opts(ld) != "" } {
+	set output tmpdir/binas.o
+    } else {
+	set output tmpdir/bintest.o
+    }
+    if { ![binutils_assemble ${srcfile} $output] } {
+	fail $testname
+	return
+    }
+
+    if { $opts(ld) != ""
+	 && ![binutils_link $output tmpdir/bintest.o \
+		  "-L$srcdir/$subdir $opts(ld)"] } {
 	fail $testname
 	return
     }
Index: binutils/testsuite/binutils-all/extract-symbol-1.d
===================================================================
RCS file: binutils/testsuite/binutils-all/extract-symbol-1.d
diff -N binutils/testsuite/binutils-all/extract-symbol-1.d
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/extract-symbol-1.d	1 Mar 2007 12:55:33 -0000
@@ -0,0 +1,12 @@
+#PROG: objcopy
+#name: --extract-symbol test 1
+#ld: -Textract-symbol-1.ld
+#objcopy: --extract-symbol
+#objdump: --headers
+#...
+Sections:
+ *Idx +Name +Size +VMA +LMA .*
+ *0 +\.foo +0+ +0+ +0+ .*
+ *CONTENTS, ALLOC, LOAD, CODE
+ *1 +\.bar +0+ +0+ +0+ .*
+ *ALLOC, READONLY, CODE
Index: binutils/testsuite/binutils-all/extract-symbol-1.s
===================================================================
RCS file: binutils/testsuite/binutils-all/extract-symbol-1.s
diff -N binutils/testsuite/binutils-all/extract-symbol-1.s
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/extract-symbol-1.s	1 Mar 2007 12:55:33 -0000
@@ -0,0 +1,15 @@
+	.globl		B
+	.globl		C
+
+	.section	.foo,"awx",%progbits
+	.4byte		1,2,3,4
+B:
+	.4byte		5,6,7
+
+	.section	.bar,"ax",%nobits
+	.space		0x123
+C:
+	.space		0x302
+
+	.globl		D
+	.equ		D,0x12345678
Index: binutils/testsuite/binutils-all/extract-symbol-1.ld
===================================================================
RCS file: binutils/testsuite/binutils-all/extract-symbol-1.ld
diff -N binutils/testsuite/binutils-all/extract-symbol-1.ld
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/extract-symbol-1.ld	1 Mar 2007 12:55:33 -0000
@@ -0,0 +1,18 @@
+ENTRY(_entry)
+PHDRS
+{
+  data PT_LOAD;
+}
+SECTIONS
+{
+  . = 0x10000;
+  .foo : { *(.foo) } :data
+
+  . = 0x20000;
+  .bar : { *(.bar) } :data
+
+  /DISCARD/ : { *(*) }
+
+  _entry = 0x30000;
+  linker_symbol = 0x40000;
+}
Index: binutils/testsuite/binutils-all/extract-symbol-2.s
===================================================================
RCS file: binutils/testsuite/binutils-all/extract-symbol-2.s
diff -N binutils/testsuite/binutils-all/extract-symbol-2.s
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/extract-symbol-2.s	1 Mar 2007 12:55:33 -0000
@@ -0,0 +1,15 @@
+	.globl		B
+	.globl		C
+
+	.section	.foo,"awx",%progbits
+	.4byte		1,2,3,4
+B:
+	.4byte		5,6,7
+
+	.section	.bar,"ax",%nobits
+	.space		0x123
+C:
+	.space		0x302
+
+	.globl		D
+	.equ		D,0x12345678
Index: binutils/testsuite/binutils-all/extract-symbol-2.d
===================================================================
RCS file: binutils/testsuite/binutils-all/extract-symbol-2.d
diff -N binutils/testsuite/binutils-all/extract-symbol-2.d
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ binutils/testsuite/binutils-all/extract-symbol-2.d	1 Mar 2007 12:55:33 -0000
@@ -0,0 +1,10 @@
+#PROG: objcopy
+#name: --extract-symbol test 2
+#ld: -Textract-symbol-1.ld -static
+#objcopy: --extract-symbol
+#nm: -n
+0*00010010 T B
+0*00020123 T C
+0*00030000 A _entry
+0*00040000 A linker_symbol
+0*12345678 A D
Index: binutils/testsuite/binutils-all/objcopy.exp
===================================================================
RCS file: /cvs/src/src/binutils/testsuite/binutils-all/objcopy.exp,v
retrieving revision 1.40
diff -u -p -r1.40 objcopy.exp
--- binutils/testsuite/binutils-all/objcopy.exp	27 Feb 2007 12:19:16 -0000	1.40
+++ binutils/testsuite/binutils-all/objcopy.exp	1 Mar 2007 12:55:33 -0000
@@ -737,5 +737,7 @@ if [is_elf_format] {
     run_dump_test "localize-hidden-1"
     run_dump_test "strip-1"
     run_dump_test "strip-2"
+    run_dump_test "extract-symbol-1"
+    run_dump_test "extract-symbol-2"
 }
 run_dump_test "localize-hidden-2"


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