[PATCH] strip: Make sure old .shstrab is removed when eu-strip recreates it.

2017-06-07 Thread Mark Wielaard
Although we always recreate the .shstrtab section for the new output
file we never explicitly assumed it could be removed. It might not be
possible to remove it when the section string table is shared with
a symbol table. But if it is removable we should (and recreate it for
the new section list).

Regression introduced in commit elfutils-0.163-33-gdf7dfab.
"Handle merged strtab/shstrtab string tables in strip and unstrip."
Add extra testcase to explicitly check for this case.

https://sourceware.org/bugzilla/show_bug.cgi?id=21525

Signed-off-by: Mark Wielaard 
---
 src/ChangeLog   |  4 
 src/strip.c | 16 ++--
 tests/ChangeLog |  4 
 tests/run-strip-test.sh |  8 
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index cbb77fc..6ac0ef2 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2017-06-06  Mark Wielaard  
+
+   * strip.c (handle_elf): Assume e_shstrndx section can be removed.
+
 2017-04-20  Ulf Hermann  
 
* readelf.c: Include strings.h.
diff --git a/src/strip.c b/src/strip.c
index f747441..11b2a37 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -711,11 +711,13 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
in the sh_link or sh_info element it cannot be removed either
   */
   for (cnt = 1; cnt < shnum; ++cnt)
-/* Check whether the section can be removed.  */
+/* Check whether the section can be removed.  Since we will create
+   a new .shstrtab assume it will be removed too.  */
 if (remove_shdrs ? !(shdr_info[cnt].shdr.sh_flags & SHF_ALLOC)
-   : ebl_section_strip_p (ebl, ehdr, &shdr_info[cnt].shdr,
-  shdr_info[cnt].name, remove_comment,
-  remove_debug))
+   : (ebl_section_strip_p (ebl, ehdr, &shdr_info[cnt].shdr,
+   shdr_info[cnt].name, remove_comment,
+   remove_debug)
+  || cnt == ehdr->e_shstrndx))
   {
/* For now assume this section will be removed.  */
shdr_info[cnt].idx = 0;
@@ -1062,8 +1064,10 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
   }
 
   /* Test whether we are doing anything at all.  */
-  if (cnt == idx)
-/* Nope, all removable sections are already gone.  */
+  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.  */
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 5b0d486..5800946 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,7 @@
+2017-06-06  Mark Wielaard  
+
+   * run-strip-test.sh: Test strip -g doesn't introduce extra .shstrtab.
+
 2017-02-13  Ulf Hermann  
Mark Wielaard  
 
diff --git a/tests/run-strip-test.sh b/tests/run-strip-test.sh
index 42aa988..280814e 100755
--- a/tests/run-strip-test.sh
+++ b/tests/run-strip-test.sh
@@ -49,6 +49,14 @@ testrun ${abs_top_builddir}/src/unstrip -o testfile.unstrip 
testfile.temp testfi
 testrun ${abs_top_builddir}/src/elfcmp --hash-inexact $original 
testfile.unstrip
 }
 
+# test strip -g
+testrun ${abs_top_builddir}/src/strip -g -o testfile.temp $original
+
+# Buggy eu-strip created multiple .shstrtab sections
+shstrtab_SECS=$(testrun ${abs_top_builddir}/src/readelf -S testfile.temp | 
grep '.shstrtab' | wc --lines)
+test $shstrtab_SECS -eq 1 ||
+  { echo "*** failure not just one '.shstrtab' testfile.temp 
($shstrtab_SECS)"; status=1; }
+
 # Now strip in-place and make sure it is smaller.
 SIZE_original=$(stat -c%s $original)
 testrun ${abs_top_builddir}/src/strip $original
-- 
1.8.3.1



[Bug tools/21525] Multiple .shstrtab sections since eu-readelf 0.166

2017-06-07 Thread mark at klomp dot org
https://sourceware.org/bugzilla/show_bug.cgi?id=21525

Mark Wielaard  changed:

   What|Removed |Added

 Status|UNCONFIRMED |ASSIGNED
   Last reconfirmed||2017-06-07
 Ever confirmed|0   |1

--- Comment #2 from Mark Wielaard  ---
Full patch posted:
https://sourceware.org/ml/elfutils-devel/2017-q2/msg00235.html

-- 
You are receiving this mail because:
You are on the CC list for the bug.

[PATCH] strip: Don't generate empty output file when nothing to do.

2017-06-07 Thread Mark Wielaard
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 
---
 src/ChangeLog  |  6 +
 src/strip.c| 31 ++-
 tests/ChangeLog|  6 +
 tests/Makefile.am  |  2 ++
 tests/run-strip-nothing.sh | 62 ++
 5 files changed, 95 insertions(+), 12 deletions(-)
 create mode 100755 tests/run-strip-nothing.sh

diff --git a/src/ChangeLog b/src/ChangeLog
index 6ac0ef2..e19122e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2017-06-07  Mark Wielaard  
+
+   * 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  
 
* strip.c (handle_elf): Assume e_shstrndx section can be removed.
diff --git a/src/strip.c b/src/strip.c
index 11b2a37..2bf95f9 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;
 }
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 5800946..5550eac 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,9 @@
+2017-06-07  Mark Wielaard  
+
+   * run-strip-nothing.sh: New test.
+   * Makefile.am (TESTS): Add run-strip-nothing.sh.
+   (EXTRA_DIST): Likewise.
+
 2017-06-06  Mark Wielaard  
 
* run-strip-test.sh: Test strip -g doesn't introduce extra .shstrtab.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3a12fe3..28e997c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -81,6 +81,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
run-strip-test3.sh run-strip-test4.sh run-strip-test5.sh \
run-strip-test6.sh run-strip-test7.sh run-strip-test8.sh \
run-strip-test9.sh run-strip-test10.sh run-strip-test11.sh \
+   run-strip-nothing.sh \
run-strip-groups.sh run-strip-reloc.sh run-strip-strmerge.sh \
run-

[Bug tools/21522] eu-strip generates empty output if there is nothing to do

2017-06-07 Thread mark at klomp dot org
https://sourceware.org/bugzilla/show_bug.cgi?id=21522

Mark Wielaard  changed:

   What|Removed |Added

 Status|UNCONFIRMED |ASSIGNED
   Last reconfirmed||2017-06-07
 Ever confirmed|0   |1

--- Comment #2 from Mark Wielaard  ---
The patch that I came up with works slightly different. While writing a
testcase I noticed we have a similar (though opposite) issue with -f debug.
Also it felt more correct to always make sure the -o output file was correct
(and not just not generate it if nothing was stripped out).

https://sourceware.org/ml/elfutils-devel/2017-q2/msg00237.html

-- 
You are receiving this mail because:
You are on the CC list for the bug.