objcopy/strip: Add option --remove-relocations=SECTIONPATTERN
authorAndrew Burgess <andrew.burgess@embecosm.com>
Fri, 21 Aug 2015 19:08:26 +0000 (20:08 +0100)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Thu, 14 Jul 2016 10:34:10 +0000 (11:34 +0100)
commitd3e5f6c8f1e58e86689909019a3756d4308b66eb
tree9a3ca06fe702efd4d768389edb7c3074ec9af020
parente511c9b19faec4d21aef49d12224316dea3d51eb
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.
12 files changed:
binutils/ChangeLog
binutils/NEWS
binutils/doc/binutils.texi
binutils/objcopy.c
binutils/testsuite/binutils-all/objcopy.exp
binutils/testsuite/binutils-all/remove-relocs-01.d [new file with mode: 0644]
binutils/testsuite/binutils-all/remove-relocs-01.s [new file with mode: 0644]
binutils/testsuite/binutils-all/remove-relocs-02.d [new file with mode: 0644]
binutils/testsuite/binutils-all/remove-relocs-03.d [new file with mode: 0644]
binutils/testsuite/binutils-all/remove-relocs-04.d [new file with mode: 0644]
binutils/testsuite/binutils-all/remove-relocs-05.d [new file with mode: 0644]
binutils/testsuite/binutils-all/remove-relocs-06.d [new file with mode: 0644]
This page took 0.025009 seconds and 4 git commands to generate.