Add an --extract-symbol option to objcopy
Richard Sandiford
richard@codesourcery.com
Thu Mar 1 14:06:00 GMT 2007
"H. J. Lu" <hjl@lucon.org> writes:
> On Thu, Mar 01, 2007 at 01:00:13PM +0000, Richard Sandiford wrote:
>> 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.
>>
>> 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.
>
> Please see
>
> ld/testsuite/ld-elf/binutils.exp
>
> Linker isn't supported for all targets. If the newly built linker
> is used, the test should go into ld/testsuite.
Doh! I knew I must be missing something...
Here's the patch with the tests in the right place. Tested in the
same way as before. 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.
ld/testsuite/
* ld-elf/extract-symbol-1sym.d, ld-elf/extract-symbol-1sec.d,
* ld-elf/extract-symbol-1.s, ld-elf/extract-symbol-1.ld: New tests.
* ld-elf/binutils.exp: Run them.
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 14:04:33 -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 14:04:33 -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 14:04:35 -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: ld/testsuite/ld-elf/extract-symbol-1sym.d
===================================================================
RCS file: ld/testsuite/ld-elf/extract-symbol-1sym.d
diff -N ld/testsuite/ld-elf/extract-symbol-1sym.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-elf/extract-symbol-1sym.d 1 Mar 2007 14:04:35 -0000
@@ -0,0 +1,10 @@
+#name: --extract-symbol test 1 (symbols)
+#source: extract-symbol-1.s
+#ld: -Textract-symbol-1.ld
+#objcopy_linked_file: --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: ld/testsuite/ld-elf/extract-symbol-1sec.d
===================================================================
RCS file: ld/testsuite/ld-elf/extract-symbol-1sec.d
diff -N ld/testsuite/ld-elf/extract-symbol-1sec.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-elf/extract-symbol-1sec.d 1 Mar 2007 14:04:35 -0000
@@ -0,0 +1,12 @@
+#name: --extract-symbol test 1 (sections)
+#source: extract-symbol-1.s
+#ld: -Textract-symbol-1.ld
+#objcopy_linked_file: --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: ld/testsuite/ld-elf/extract-symbol-1.s
===================================================================
RCS file: ld/testsuite/ld-elf/extract-symbol-1.s
diff -N ld/testsuite/ld-elf/extract-symbol-1.s
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-elf/extract-symbol-1.s 1 Mar 2007 14:04:35 -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: ld/testsuite/ld-elf/extract-symbol-1.ld
===================================================================
RCS file: ld/testsuite/ld-elf/extract-symbol-1.ld
diff -N ld/testsuite/ld-elf/extract-symbol-1.ld
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-elf/extract-symbol-1.ld 1 Mar 2007 14:04:35 -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: ld/testsuite/ld-elf/binutils.exp
===================================================================
RCS file: /cvs/src/src/ld/testsuite/ld-elf/binutils.exp,v
retrieving revision 1.3
diff -u -p -r1.3 binutils.exp
--- ld/testsuite/ld-elf/binutils.exp 17 Aug 2006 02:04:53 -0000 1.3
+++ ld/testsuite/ld-elf/binutils.exp 1 Mar 2007 14:04:35 -0000
@@ -122,3 +122,6 @@ binutils_test objcopy "-z max-page-size=
binutils_test objcopy "" tdata2
binutils_test objcopy "-shared" tdata2
binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
+
+run_dump_test extract-symbol-1sec
+run_dump_test extract-symbol-1sym
More information about the Binutils
mailing list