From: Mark Wielaard Date: Thu, 4 Feb 2016 08:53:41 +0000 (+0100) Subject: libelf: elf_getdata should not adjust alignment for SHT_NOBITS sections. X-Git-Tag: elfutils-0.166~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22035605b9da4a6ee07385c0e7715fe8d35488a4;p=thirdparty%2Felfutils.git libelf: elf_getdata should not adjust alignment for SHT_NOBITS sections. In commit c0748e "libelf: More checking of valid sh_addralign values." we adjusted bogus alignment of data buffers if they were greater than the offset of the data in the file. This works OK, except when there is no data in the file. So make sure to not adjust any NOBITS sections. Also adds a test that shows the issue and makes sure elflint is called with --gnu in run-strip-test.sh. https://bugzilla.redhat.com/show_bug.cgi?id=1303845 Signed-off-by: Mark Wielaard --- diff --git a/libelf/ChangeLog b/libelf/ChangeLog index afe6a6a5e..ec56b5347 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,8 @@ +2016-02-04 Mark Wielaard + + * elf_getdata.c (__libelf_set_rawdata_wrlock): Don't adjust align + for SHT_NOBITS sections. + 2016-01-22 Chih-Hung Hsieh * elf_compress.c (__libelf_compress): Move nested function diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c index 4ec94b980..d1fafbfe7 100644 --- a/libelf/elf_getdata.c +++ b/libelf/elf_getdata.c @@ -1,5 +1,5 @@ /* Return the next data element from the section after possibly converting it. - Copyright (C) 1998-2005, 2006, 2007, 2015 Red Hat, Inc. + Copyright (C) 1998-2005, 2006, 2007, 2015, 2016 Red Hat, Inc. This file is part of elfutils. Written by Ulrich Drepper , 1998. @@ -363,7 +363,7 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn) at least an ehdr this will only trigger for alignment values > 64 which should be uncommon. */ align = align ?: 1; - if (align > offset) + if (type != SHT_NOBITS && align > offset) align = offset; scn->rawdata.d.d_align = align; if (elf->class == ELFCLASS32 diff --git a/tests/ChangeLog b/tests/ChangeLog index 37f981126..a043ade6d 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,13 @@ +2016-02-04 Mark Wielaard + + * run-strip-nobitsalign.sh: New test. + * testfile-nobitsalign.bz2: New testfile. + * testfile-nobitsalign.strip.bz2: Likewise. + * Makefile.am (TESTS): Add run-strip-nobitsalign.sh. + (EXTRA_DIST): Add run-strip-nobitsalign.sh, testfile-nobitsalign.bz2 + and testfile-nobitsalign.strip.bz2. + * run-strip-test.sh: Add --gnu to elflint calls. + 2016-01-13 Mark Wielaard * dwfl-bug-fd-leak.c: Skip test unless on __linux__. diff --git a/tests/Makefile.am b/tests/Makefile.am index 7b9e10839..f3e7c01ca 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to create Makefile.in ## -## Copyright (C) 1996-2015 Red Hat, Inc. +## Copyright (C) 1996-2016 Red Hat, Inc. ## This file is part of elfutils. ## ## This file is free software; you can redistribute it and/or modify @@ -80,6 +80,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \ run-strip-test6.sh run-strip-test7.sh run-strip-test8.sh \ run-strip-test9.sh run-strip-test10.sh \ run-strip-groups.sh run-strip-reloc.sh run-strip-strmerge.sh \ + run-strip-nobitsalign.sh \ run-unstrip-test.sh run-unstrip-test2.sh \ run-unstrip-test3.sh run-unstrip-M.sh run-elfstrmerge-test.sh \ run-ecp-test.sh run-ecp-test2.sh run-alldts.sh \ @@ -163,6 +164,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \ run-strip-test4.sh run-strip-test5.sh run-strip-test6.sh \ run-strip-test7.sh run-strip-test8.sh run-strip-groups.sh \ run-strip-test9.sh run-strip-test10.sh 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 \ hello_ppc64.ko.bz2 hello_s390.ko.bz2 hello_aarch64.ko.bz2 \ run-unstrip-test.sh run-unstrip-test2.sh \ diff --git a/tests/run-strip-nobitsalign.sh b/tests/run-strip-nobitsalign.sh new file mode 100755 index 000000000..db9b1d9eb --- /dev/null +++ b/tests/run-strip-nobitsalign.sh @@ -0,0 +1,35 @@ +#! /bin/sh +# Copyright (C) Red Hat, Inc., 2016. +# This file is part of elfutils. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# static unsigned char buffer[4096] __attribute((aligned (4096))); +# +# char +# f (int i) +# { +# return buffer[i]; +# } +# +# int +# main (int argc, char **argv) +# { +# return buffer[argc] == 0; +# } + +original=testfile-nobitsalign +stripped=testfile-nobitsalign.strip + +. $srcdir/run-strip-test.sh diff --git a/tests/run-strip-test.sh b/tests/run-strip-test.sh index 43d27e533..42aa9888c 100755 --- a/tests/run-strip-test.sh +++ b/tests/run-strip-test.sh @@ -34,13 +34,13 @@ status=0 cmp $stripped testfile.temp || status=$? # Check elflint and the expected result. -testrun ${abs_top_builddir}/src/elflint -q testfile.temp || status=$? +testrun ${abs_top_builddir}/src/elflint --gnu -q testfile.temp || status=$? test -z "$debugfile" || { cmp $debugfile testfile.debug.temp || status=$? # Check elflint and the expected result. -testrun ${abs_top_builddir}/src/elflint -q -d testfile.debug.temp || status=$? +testrun ${abs_top_builddir}/src/elflint --gnu -q -d testfile.debug.temp || status=$? # Now test unstrip recombining those files. testrun ${abs_top_builddir}/src/unstrip -o testfile.unstrip testfile.temp testfile.debug.temp diff --git a/tests/testfile-nobitsalign.bz2 b/tests/testfile-nobitsalign.bz2 new file mode 100755 index 000000000..7f0d4249a Binary files /dev/null and b/tests/testfile-nobitsalign.bz2 differ diff --git a/tests/testfile-nobitsalign.strip.bz2 b/tests/testfile-nobitsalign.strip.bz2 new file mode 100755 index 000000000..f72000cc5 Binary files /dev/null and b/tests/testfile-nobitsalign.strip.bz2 differ