Skip to content

Commit

Permalink
unstrip: Also check sh_size in compare_unalloc_sections.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Mark Wielaard committed Jul 27, 2018
1 parent 98ec973 commit 9718c94
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2018-07-24 Mark Wielaard <[email protected]>

* unstrip.c (compare_unalloc_sections): Also compare sh_size.

2018-07-21 Mark Wielaard <[email protected]>

* unstrip.c (adjust_all_relocs): Skip SHT_GROUP sections.
Expand Down
6 changes: 6 additions & 0 deletions src/unstrip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2018-07-24 Mark Wielaard <[email protected]>

* 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 <[email protected]>

* run-annobingroup.sh: New test.
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
55 changes: 55 additions & 0 deletions tests/run-annobingroup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added tests/testfile-annobingroup-i386.o.bz2
Binary file not shown.

0 comments on commit 9718c94

Please sign in to comment.