objcopy/strip: Add option --remove-relocations=SECTIONPATTERN

The objcopy and strip tools make use of the bfd library to manipulate
the state of the input file (to produce an output file).  Within the
input file (for ELF at least), relocations are held within a section,
and so, if the user wanted to remove the relocations, but keep the
section to which the relocations would have been applied, it is tempting
to think that specifying the name of a relocation section to objcopy's
--remove-section option might do what you want, for example:

   objcopy --remove-section=.rela.text input.elf output.elf

However, this does not work.  The reason is that when the input file is
loaded, relocations are not managed as sections, but are, instead,
loaded as data associated with the section to which the relocations
would be applied.  In our example above the relocations in '.rela.text'
are held as data on the section '.text' once 'input.elf' is loaded.

One task that objcopy and strip do is copy the relocations from the
input file to the output file if the section is also being copied from
the input file to the output file.

This commit adds a new command line option for objcopy and strip,
--remove-relocations, which can be used to remove the relocations, while
keeping the section that the relocations would have been applied to, for
example:

    objcopy --remove-relocations=.text input.elf output.elf

in this case the section '.text' will appear in both 'input.elf' and
'output.elf', but any relocations in 'input.elf' that apply to '.text'
will not be present in 'output.elf'.

I have also added a special case to the handling of --remove-section
that spots if a user tries to remove a relocation section (currently
this is done by spotting the '.rela.' or '.rel.' prefix) and forwards
the request to --remove-relocations.

As with --remove-section and --only-section the --remove-relocations
option supports the '!' prefix on the section-patterns it takes to allow
for sections to be specifically not matched.

There are tests for all the new functionality.

binutils/ChangeLog:

	* doc/binutils.texi (objcopy): Document 'remove-relocations'.
	(strip): Likewise.
	* objcopy.c (SECTION_CONTEXT_REMOVE_RELOCS): Define.
	(enum command_line_switch): Add 'OPTION_REMOVE_RELOCS'.
	(struct option strip_options): Add 'remove-relocations'.
	(struct option copy_options): Likewise.
	(copy_usage): Likewise.
	(strip_usage): Likewise.
	(handle_remove_relocations_option): New function.
	(discard_relocations): New function.
	(handle_remove_section_option): New function.
	(copy_relocations_in_section): Use discard_relocations.
	(strip_main): Use handle_remove_section_option for
	'remove-section', and handle 'remove-relocations' option.
	(copy_main): Likewise.
	* testsuite/binutils-all/objcopy.exp: Run new tests.
	* testsuite/binutils-all/remove-relocs-01.d: New file.
	* testsuite/binutils-all/remove-relocs-01.s: New file.
	* testsuite/binutils-all/remove-relocs-02.d: New file.
	* testsuite/binutils-all/remove-relocs-03.d: New file.
	* testsuite/binutils-all/remove-relocs-04.d: New file.
	* testsuite/binutils-all/remove-relocs-05.d: New file.
	* testsuite/binutils-all/remove-relocs-06.d: New file.
This commit is contained in:
Andrew Burgess
2015-08-21 20:08:26 +01:00
parent e511c9b19f
commit d3e5f6c8f1
12 changed files with 242 additions and 5 deletions

View File

@@ -1075,6 +1075,7 @@ objcopy [@option{-F} @var{bfdname}|@option{--target=}@var{bfdname}]
[@option{--interleave-width=}@var{width}]
[@option{-j} @var{sectionpattern}|@option{--only-section=}@var{sectionpattern}]
[@option{-R} @var{sectionpattern}|@option{--remove-section=}@var{sectionpattern}]
[@option{--remove-relocations=}@var{sectionpattern}]
[@option{-p}|@option{--preserve-dates}]
[@option{-D}|@option{--enable-deterministic-archives}]
[@option{-U}|@option{--disable-deterministic-archives}]
@@ -1254,6 +1255,34 @@ would otherwise remove it. For example:
will remove all sections matching the pattern '.text.*', but will not
remove the section '.text.foo'.
@item --remove-relocations=@var{sectionpattern}
Remove relocations from the output file for any section matching
@var{sectionpattern}. This option may be given more than once. Note
that using this option inappropriately may make the output file
unusable. Wildcard characters are accepted in @var{sectionpattern}.
For example:
@smallexample
--remove-relocations=.text.*
@end smallexample
will remove the relocations for all sections matching the patter
'.text.*'.
If the first character of @var{sectionpattern} is the exclamation
point (!) then matching sections will not have their relocation
removed even if an earlier use of @option{--remove-relocations} on the
same command line would otherwise cause the relocations to be removed.
For example:
@smallexample
--remove-relocations=.text.* --remove-relocations=!.text.foo
@end smallexample
will remove all relocations for sections matching the pattern
'.text.*', but will not remove relocations for the section
'.text.foo'.
@item -S
@itemx --strip-all
Do not copy relocation and symbol information from the source file.
@@ -2988,6 +3017,7 @@ strip [@option{-F} @var{bfdname} |@option{--target=}@var{bfdname}]
[@option{-w}|@option{--wildcard}]
[@option{-x}|@option{--discard-all}] [@option{-X} |@option{--discard-locals}]
[@option{-R} @var{sectionname} |@option{--remove-section=}@var{sectionname}]
[@option{--remove-relocations=}@var{sectionpattern}]
[@option{-o} @var{file}] [@option{-p}|@option{--preserve-dates}]
[@option{-D}|@option{--enable-deterministic-archives}]
[@option{-U}|@option{--disable-deterministic-archives}]
@@ -3057,6 +3087,34 @@ would otherwise remove it. For example:
will remove all sections matching the pattern '.text.*', but will not
remove the section '.text.foo'.
@item --remove-relocations=@var{sectionpattern}
Remove relocations from the output file for any section matching
@var{sectionpattern}. This option may be given more than once. Note
that using this option inappropriately may make the output file
unusable. Wildcard characters are accepted in @var{sectionpattern}.
For example:
@smallexample
--remove-relocations=.text.*
@end smallexample
will remove the relocations for all sections matching the patter
'.text.*'.
If the first character of @var{sectionpattern} is the exclamation
point (!) then matching sections will not have their relocation
removed even if an earlier use of @option{--remove-relocations} on the
same command line would otherwise cause the relocations to be removed.
For example:
@smallexample
--remove-relocations=.text.* --remove-relocations=!.text.foo
@end smallexample
will remove all relocations for sections matching the pattern
'.text.*', but will not remove relocations for the section
'.text.foo'.
@item -s
@itemx --strip-all
Remove all symbols.