unstrip: Also check sh_size in compare_unalloc_sections.
authorMark Wielaard <mark@klomp.org>
Tue, 24 Jul 2018 21:34:19 +0000 (23:34 +0200)
committerMark Wielaard <mark@klomp.org>
Fri, 27 Jul 2018 12:09:21 +0000 (14:09 +0200)
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
src/unstrip.c
tests/ChangeLog
tests/Makefile.am
tests/run-annobingroup.sh
tests/testfile-annobingroup-i386.o.bz2 [new file with mode: 0644]

index 791b627..a01bd75 100644 (file)
@@ -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.
index cb1f7dc..ec46c95 100644 (file)
@@ -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);
 }
index 45844b1..2a20ffa 100644 (file)
@@ -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.
index b7a0e17..2946083 100644 (file)
@@ -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 \
index 5f08b35..700df32 100755 (executable)
@@ -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 (file)
index 0000000..798eefd
Binary files /dev/null and b/tests/testfile-annobingroup-i386.o.bz2 differ