[PATCH] unstrip: Also check sh_size in compare_unalloc_sections.

Mark Wielaard mark@klomp.org
Tue Jul 24 22:15:00 GMT 2018


compare_unalloc_sections only checked sh_flags and the section names.
This would cause stripped/debug section mismatches when there were
multiple sections with the same name and flags. Fix this by also checking
the size of the section matches.

Add a testcase that has two ".group" sections created on i386 with the
gcc annobin plugin.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog                          |   4 +++
 src/unstrip.c                          |   6 ++++
 tests/ChangeLog                        |   6 ++++
 tests/Makefile.am                      |   1 +
 tests/run-annobingroup.sh              |  55 +++++++++++++++++++++++++++++++++
 tests/testfile-annobingroup-i386.o.bz2 | Bin 0 -> 1387 bytes
 6 files changed, 72 insertions(+)
 create mode 100644 tests/testfile-annobingroup-i386.o.bz2

diff --git a/src/ChangeLog b/src/ChangeLog
index 37e7b62..eee144d 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2018-07-24  Mark Wielaard  <mark@klomp.org>
+
+	* unstrip.c (compare_unalloc_sections): Also compare sh_size.
+
 2018-07-21  Mark Wielaard  <mark@klomp.org>
 
 	* unstrip.c (adjust_all_relocs): Skip SHT_GROUP sections.
diff --git a/src/unstrip.c b/src/unstrip.c
index cb1f7dc..ec46c95 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -709,6 +709,12 @@ compare_unalloc_sections (const GElf_Shdr *shdr1, const GElf_Shdr *shdr2,
   if (shdr1->sh_flags > shdr2->sh_flags)
     return 1;
 
+  /* Sizes should be the same.  */
+  if (shdr1->sh_size < shdr2->sh_size)
+    return -1;
+  if (shdr1->sh_size > shdr2->sh_size)
+    return 1;
+
   /* Sort by name as last resort.  */
   return strcmp (name1, name2);
 }
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 45844b1..2a20ffa 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,9 @@
+2018-07-24  Mark Wielaard  <mark@klomp.org>
+
+	* run-annobingroup.sh: Add testfile-annobingroup-i386.o tests.
+	* testfile-annobingroup-i386.o.bz2: New test file.
+	* Makefile.am (EXTRA_DIST): Add testfile-annobingroup-i386.o.bz2.
+
 2018-07-21  Mark Wielaard  <mark@klomp.org>
 
 	* run-annobingroup.sh: New test.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b7a0e17..2946083 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -195,6 +195,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 	     run-strip-test12.sh \
 	     run-strip-nothing.sh run-strip-remove-keep.sh run-strip-g.sh \
 	     run-annobingroup.sh testfile-annobingroup.o.bz2 \
+	     testfile-annobingroup-i386.o.bz2 \
 	     run-strip-strmerge.sh run-strip-nobitsalign.sh \
 	     testfile-nobitsalign.bz2 testfile-nobitsalign.strip.bz2 \
 	     run-strip-reloc.sh hello_i386.ko.bz2 hello_x86_64.ko.bz2 \
diff --git a/tests/run-annobingroup.sh b/tests/run-annobingroup.sh
index 5f08b35..700df32 100755
--- a/tests/run-annobingroup.sh
+++ b/tests/run-annobingroup.sh
@@ -65,4 +65,59 @@ EOF
 
 testrun ${abs_top_builddir}/src/elfcmp testfile-annobingroup.o remerged.elf
 
+# echo "void * __attribute__((cold)) foo (void) { return foo; }"
+#      > testfile-annobingroup-i386.c
+# gcc -fpic -g -O2 -fplugin=annobin -c testfile-annobingroup-i386.c
+testfiles testfile-annobingroup-i386.o
+
+testrun_compare ${abs_top_builddir}/src/readelf -g testfile-annobingroup-i386.o << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun ${abs_top_builddir}/src/strip -o stripped.elf -f debugfile.elf testfile-annobingroup-i386.o
+
+testrun_compare ${abs_top_builddir}/src/readelf -g stripped.elf << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun_compare ${abs_top_builddir}/src/readelf -g debugfile.elf << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun ${abs_top_builddir}/src/unstrip -o remerged.elf stripped.elf debugfile.elf
+
+testrun_compare ${abs_top_builddir}/src/readelf -g remerged.elf << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun ${abs_top_builddir}/src/elfcmp testfile-annobingroup-i386.o remerged.elf
+
 exit 0
diff --git a/tests/testfile-annobingroup-i386.o.bz2 b/tests/testfile-annobingroup-i386.o.bz2
new file mode 100644
index 0000000000000000000000000000000000000000..798eefda9c6018d7a2a7e46bfd9f850c67b1dcb6
GIT binary patch
literal 1387
zcmV-x1(f<iT4*^jL0KkKS?+Yv0RRP&|NsC0{{QCx|Nj5=-+=$`-*oUu5XBU!00e9V
zjKE?*AWYB%(iR4Ul#Bs}NQtIY@usJ#sPQs-A(DEGjUJj!G-&jmpwXe~XwjfFX`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