summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2017-06-07 20:32:38 +0200
committerMark Wielaard <mark@klomp.org>2017-06-14 14:51:33 +0200
commitb58aebe71e0b4863db1b7fd3e942e36303257f3a (patch)
treeb35449ab65eef87b6645130e5a86c5f8be4725c6 /src
parente0b3232e9a0bc8215ca1be29b1ec6df43811eb87 (diff)
downloadelfutils-b58aebe71e0b4863db1b7fd3e942e36303257f3a.tar.gz
strip: Don't generate empty output file when nothing to do.
If there was nothing to do strip would skip generating a separate debug file if one was requested, but it would also not finish the creation of a new output file (with the non-stripped sections). Also if there was an error any partially created output would be kept. Make sure that when the -o output file option is given we always generate a complete output file (except on error). Also make sure that when the -f debug file option is given it is only generated when it is not empty. Add testcase run-strip-nothing.sh that tests the various combinations. https://sourceware.org/bugzilla/show_bug.cgi?id=21522 Signed-off-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog6
-rw-r--r--src/strip.c31
2 files changed, 25 insertions, 12 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 6ac0ef2d..e19122e8 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2017-06-07 Mark Wielaard <mark@klomp.org>
+
+ * strip.c (handle_elf): Introduce new handle_elf boolean. Use it to
+ determine whether to create an output and/or debug file. Remove new
+ output file on error.
+
2017-06-06 Mark Wielaard <mark@klomp.org>
* strip.c (handle_elf): Assume e_shstrndx section can be removed.
diff --git a/src/strip.c b/src/strip.c
index 11b2a379..2bf95f90 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -1063,15 +1063,17 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
shdr_info[cnt].se = dwelf_strtab_add (shst, shdr_info[cnt].name);
}
- /* Test whether we are doing anything at all. */
- if (cnt == idx
- || (cnt == idx + 1 && shdr_info[ehdr->e_shstrndx].idx == 0))
- /* Nope, all removable sections are already gone. Or the only section
- we would remove is the .shstrtab section which we will add again. */
- goto fail_close;
-
- /* Create the reference to the file with the debug info. */
- if (debug_fname != NULL && !remove_shdrs)
+ /* Test whether we are doing anything at all. Either all removable
+ sections are already gone. Or the only section we would remove is
+ the .shstrtab section which we would add again. */
+ bool removing_sections = !(cnt == idx
+ || (cnt == idx + 1
+ && shdr_info[ehdr->e_shstrndx].idx == 0));
+ if (output_fname == NULL && !removing_sections)
+ goto fail_close;
+
+ /* Create the reference to the file with the debug info (if any). */
+ if (debug_fname != NULL && !remove_shdrs && removing_sections)
{
/* Add the section header string table section name. */
shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".gnu_debuglink", 15);
@@ -1759,7 +1761,8 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
/* Remove any relocations between debug sections in ET_REL
for the debug file when requested. These relocations are always
zero based between the unallocated sections. */
- if (debug_fname != NULL && reloc_debug && ehdr->e_type == ET_REL)
+ if (debug_fname != NULL && removing_sections
+ && reloc_debug && ehdr->e_type == ET_REL)
{
scn = NULL;
cnt = 0;
@@ -1997,7 +2000,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
/* Now that we have done all adjustments to the data,
we can actually write out the debug file. */
- if (debug_fname != NULL)
+ if (debug_fname != NULL && removing_sections)
{
/* Finally write the file. */
if (unlikely (elf_update (debugelf, ELF_C_WRITE) == -1))
@@ -2230,7 +2233,11 @@ cannot set access and modification date of '%s'"),
/* Close the file descriptor if we created a new file. */
if (output_fname != NULL)
- close (fd);
+ {
+ close (fd);
+ if (result != 0)
+ unlink (output_fname);
+ }
return result;
}