diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2015-02-13 17:04:42 +0000 |
---|---|---|
committer | Andrew Burgess <andrew.burgess@embecosm.com> | 2015-02-28 21:25:51 +0000 |
commit | acf1419f9c52d06ee70169b85c5f8980c7359f8f (patch) | |
tree | d8a193b0f2a2411aa63f1cf4a4284c33368cb5df /binutils/objcopy.c | |
parent | b7236fbee40060bcf5e90740a5c5706317749f91 (diff) | |
download | gdb-acf1419f9c52d06ee70169b85c5f8980c7359f8f.zip gdb-acf1419f9c52d06ee70169b85c5f8980c7359f8f.tar.gz gdb-acf1419f9c52d06ee70169b85c5f8980c7359f8f.tar.bz2 |
objcopy: Add --update-section option.
New option for objcopy --update-section allows the contents of a section
to be updated while maintaining the section flags, and, for ELF files,
the section to segment mapping.
New test uses --dump-section and --update-section to check that a
section can be made larger and smaller with an update.
binutils/ChangeLog:
* objcopy.c (update_sections): New list.
(command_line_switch): Add OPTION_UPDATE_SECTION.
(copy_options): Add update-section.
(copy_usage): Document new option.
(is_update_section): New function.
(is_strip_section_1): Add check for attempt to update and remove
the same section.
(copy_object): Update size and content of requested sections.
(skip_section): Don't copy for updated sections.
(copy_main): Handle --update-section.
* doc/binutils.texi (objcopy): Add description of --update-section
option.
* NEWS: Mention --update-section option.
binutils/testsuite/ChangeLog:
* binutils-all/update-1.s: New file.
* binutils-all/update-2.s: New file.
* binutils-all/update-3.s: New file.
* binutils-all/update-4.s: New file.
* binutils-all/update-section.exp: New file.
Diffstat (limited to 'binutils/objcopy.c')
-rw-r--r-- | binutils/objcopy.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/binutils/objcopy.c b/binutils/objcopy.c index 7f094d3..f340c8a 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -186,6 +186,9 @@ struct section_add /* List of sections to add to the output BFD. */ static struct section_add *add_sections; +/* List of sections to update in the output BFD. */ +static struct section_add *update_sections; + /* List of sections to dump from the output BFD. */ static struct section_add *dump_sections; @@ -262,6 +265,7 @@ static enum long_section_name_handling long_section_names = KEEP; enum command_line_switch { OPTION_ADD_SECTION=150, + OPTION_UPDATE_SECTION, OPTION_DUMP_SECTION, OPTION_CHANGE_ADDRESSES, OPTION_CHANGE_LEADING_CHAR, @@ -361,6 +365,7 @@ static struct option copy_options[] = { {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK}, {"add-section", required_argument, 0, OPTION_ADD_SECTION}, + {"update-section", required_argument, 0, OPTION_UPDATE_SECTION}, {"adjust-start", required_argument, 0, OPTION_CHANGE_START}, {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES}, {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, @@ -553,6 +558,9 @@ copy_usage (FILE *stream, int exit_status) --set-section-flags <name>=<flags>\n\ Set section <name>'s properties to <flags>\n\ --add-section <name>=<file> Add section <name> found in <file> to output\n\ + --update-section <name>=<file>\n\ + Update contents of section <name> with\n\ + contents found in <file>\n\ --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\ --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\ --long-section-names {enable|disable|keep}\n\ @@ -1044,6 +1052,27 @@ is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) return strncmp (name + len - 4, ".dwo", 4) == 0; } +/* Return TRUE if section SEC is in the update list. */ + +static bfd_boolean +is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) +{ + if (update_sections != NULL) + { + struct section_add *pupdate; + + for (pupdate = update_sections; + pupdate != NULL; + pupdate = pupdate->next) + { + if (strcmp (sec->name, pupdate->name) == 0) + return TRUE; + } + } + + return FALSE; +} + /* See if a non-group section is being removed. */ static bfd_boolean @@ -1062,6 +1091,9 @@ is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) if (p && q) fatal (_("error: section %s matches both remove and copy options"), bfd_get_section_name (abfd, sec)); + if (p && is_update_section (abfd, sec)) + fatal (_("error: section %s matches both update and remove options"), + bfd_get_section_name (abfd, sec)); if (p != NULL) return TRUE; @@ -1865,6 +1897,29 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) } } + if (update_sections != NULL) + { + struct section_add *pupdate; + + for (pupdate = update_sections; + pupdate != NULL; + pupdate = pupdate->next) + { + asection *osec; + + pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name); + if (pupdate->section == NULL) + fatal (_("error: %s not found, can't be updated"), pupdate->name); + + osec = pupdate->section->output_section; + if (! bfd_set_section_size (obfd, osec, pupdate->size)) + { + bfd_nonfatal_message (NULL, obfd, osec, NULL); + return FALSE; + } + } + } + if (dump_sections != NULL) { struct section_add * pdump; @@ -2150,6 +2205,26 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) } } + if (update_sections != NULL) + { + struct section_add *pupdate; + + for (pupdate = update_sections; + pupdate != NULL; + pupdate = pupdate->next) + { + asection *osec; + + osec = pupdate->section->output_section; + if (! bfd_set_section_contents (obfd, osec, pupdate->contents, + 0, pupdate->size)) + { + bfd_nonfatal_message (NULL, obfd, osec, NULL); + return FALSE; + } + } + } + if (gnu_debuglink_filename != NULL) { if (! bfd_fill_in_gnu_debuglink_section @@ -2881,6 +2956,9 @@ skip_section (bfd *ibfd, sec_ptr isection) if (is_strip_section (ibfd, isection)) return TRUE; + if (is_update_section (ibfd, isection)) + return TRUE; + flags = bfd_get_section_flags (ibfd, isection); if ((flags & SEC_GROUP) != 0) return TRUE; @@ -3795,6 +3873,12 @@ copy_main (int argc, char *argv[]) section_add_load_file (add_sections); break; + case OPTION_UPDATE_SECTION: + update_sections = init_section_add (optarg, update_sections, + "--update-section"); + section_add_load_file (update_sections); + break; + case OPTION_DUMP_SECTION: dump_sections = init_section_add (optarg, dump_sections, "--dump-section"); |