Fix overflow checking for 32-bit pc-relative relocations on x32. The problem here is that x32 is really using 64-bit addressing, while pretending to be 32-bit. Even though the object file format is 32-bit, we need to do the overflow checking with 64-bit arithmetic (because that's what the hardware will be using). This patch overrides the pcrela32_check functions in reloc.h with target-specific versions that do 64-bit checking. I've also updated the test case to use -Tdata instead of adding a huge .space directive, to reduce the size of the .o files. 2016-02-06 Cary Coutant gold/ PR gold/19567 * reloc.h (Relocate_functions::Overflow_check): Add comments. * x86_64.cc (X86_64_relocate_functions): New class. (Target_x86_64::Relocate::relocate): Use the new class. * testsuite/Makefile.am (x86_64_overflow_pc32): Add -Tdata option. (x32_overflow_pc32): New test case. * testsuite/Makefile.in: Regenerate. * testsuite/x32_overflow_pc32.sh: New script. * testsuite/x86_64_overflow_pc32.s: Remove .space directive. diff --git a/gold/reloc.h b/gold/reloc.h index 9c09c7c..fa5e36c 100644 --- a/gold/reloc.h +++ b/gold/reloc.h @@ -336,10 +336,15 @@ class Relocate_functions enum Overflow_check { + // No overflow checking. CHECK_NONE, + // Check for overflow of a signed value. CHECK_SIGNED, + // Check for overflow of an unsigned value. CHECK_UNSIGNED, - CHECK_SIGNED_OR_UNSIGNED + // Check for overflow of a signed or unsigned value. + // (i.e., no error if either signed or unsigned fits.) + CHECK_SIGNED_OR_UNSIGNED }; enum Reloc_status diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am index 81f3464..ca24205 100644 --- a/gold/testsuite/Makefile.am +++ b/gold/testsuite/Makefile.am @@ -1080,14 +1080,30 @@ MOSTLYCLEANFILES += x86_64_overflow_pc32.err x86_64_overflow_pc32.o: x86_64_overflow_pc32.s $(TEST_AS) -o $@ $< x86_64_overflow_pc32.err: x86_64_overflow_pc32.o gcctestdir/ld - @echo $(CXXLINK) -Bgcctestdir/ -shared -o x86_64_overflow_pc32 x86_64_overflow_pc32.o "2>$@" - @if $(CXXLINK) -Bgcctestdir/ -shared -o x86_64_overflow_pc32 x86_64_overflow_pc32.o 2>$@; \ + @echo gcctestdir/ld -e bar -Tdata=0x81000000 -o x86_64_overflow_pc32 x86_64_overflow_pc32.o "2>$@" + @if gcctestdir/ld -e bar -Tdata=0x81000000 -o x86_64_overflow_pc32 x86_64_overflow_pc32.o 2>$@; \ then \ echo 1>&2 "Link of x86_64_overflow_pc32 should have failed"; \ rm -f $@; \ exit 1; \ fi +check_SCRIPTS += x32_overflow_pc32.sh +check_DATA += x32_overflow_pc32.err +MOSTLYCLEANFILES += x32_overflow_pc32.err +x86_64_overflow_pc32.o: x86_64_overflow_pc32.s + $(TEST_AS) -o $@ $< +x32_overflow_pc32.o: x86_64_overflow_pc32.s + $(TEST_AS) --x32 -o $@ $< +x32_overflow_pc32.err: x32_overflow_pc32.o gcctestdir/ld + @echo gcctestdir/ld -e bar -Tdata=0x81000000 -o x32_overflow_pc32 x32_overflow_pc32.o "2>$@" + @if gcctestdir/ld -e bar -Tdata=0x81000000 -o x32_overflow_pc32 x32_overflow_pc32.o 2>$@; \ + then \ + echo 1>&2 "Link of x32_overflow_pc32 should have failed"; \ + rm -f $@; \ + exit 1; \ + fi + endif DEFAULT_TARGET_X86_64 if DEFAULT_TARGET_I386 diff --git a/gold/testsuite/x32_overflow_pc32.sh b/gold/testsuite/x32_overflow_pc32.sh new file mode 100755 index 0000000..3a5bcba --- /dev/null +++ b/gold/testsuite/x32_overflow_pc32.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +# x86_64_overflow_pc32.sh -- a test case for overflow checking. + +# Copyright (C) 2016 Free Software Foundation, Inc. +# Written by Cary Coutant . + +# This file is part of gold. + +# This program 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. + +# This program 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, write to the Free Software +# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, +# MA 02110-1301, USA. + +check() +{ + if ! grep -q "$2" "$1" + then + echo "Did not find expected error in $1:" + echo " $2" + echo "" + echo "Actual error output below:" + cat "$1" + exit 1 + fi +} + +# We don't know how the compiler might order these variables, so we +# can't test for the actual offset from .data, hence the regexp. +check x86_64_overflow_pc32.err "function bar: error: relocation overflow" + +exit 0 diff --git a/gold/testsuite/x86_64_overflow_pc32.s b/gold/testsuite/x86_64_overflow_pc32.s index 7494c53..85d9e82 100644 --- a/gold/testsuite/x86_64_overflow_pc32.s +++ b/gold/testsuite/x86_64_overflow_pc32.s @@ -1,5 +1,4 @@ .data - .space 0x8ff00000 .hidden foo .globl foo foo: diff --git a/gold/x86_64.cc b/gold/x86_64.cc index 82bb658..494b312 100644 --- a/gold/x86_64.cc +++ b/gold/x86_64.cc @@ -3347,6 +3347,52 @@ Target_x86_64::do_finalize_sections( } } +// For x32, we need to handle PC-relative relocations using full 64-bit +// arithmetic, so that we can detect relocation overflows properly. +// This class overrides the pcrela32_check methods from the defaults in +// Relocate_functions in reloc.h. + +template +class X86_64_relocate_functions : public Relocate_functions +{ + public: + typedef Relocate_functions Base; + + // Do a simple PC relative relocation with the addend in the + // relocation. + static inline typename Base::Reloc_status + pcrela32_check(unsigned char* view, + typename elfcpp::Elf_types<64>::Elf_Addr value, + typename elfcpp::Elf_types<64>::Elf_Swxword addend, + typename elfcpp::Elf_types<64>::Elf_Addr address) + { + typedef typename elfcpp::Swap<32, false>::Valtype Valtype; + Valtype* wv = reinterpret_cast(view); + value = value + addend - address; + elfcpp::Swap<32, false>::writeval(wv, value); + return (Bits<32>::has_overflow(value) + ? Base::RELOC_OVERFLOW : Base::RELOC_OK); + } + + // Do a simple PC relative relocation with a Symbol_value with the + // addend in the relocation. + static inline typename Base::Reloc_status + pcrela32_check(unsigned char* view, + const Sized_relobj_file* object, + const Symbol_value* psymval, + typename elfcpp::Elf_types<64>::Elf_Swxword addend, + typename elfcpp::Elf_types<64>::Elf_Addr address) + { + typedef typename elfcpp::Swap<32, false>::Valtype Valtype; + Valtype* wv = reinterpret_cast(view); + typename elfcpp::Elf_types<64>::Elf_Addr value = + psymval->value(object, addend) - address; + elfcpp::Swap<32, false>::writeval(wv, value); + return (Bits<32>::has_overflow(value) + ? Base::RELOC_OVERFLOW : Base::RELOC_OK); + } +}; + // Perform a relocation. template @@ -3364,7 +3410,7 @@ Target_x86_64::Relocate::relocate( typename elfcpp::Elf_types::Elf_Addr address, section_size_type view_size) { - typedef Relocate_functions Reloc_funcs; + typedef X86_64_relocate_functions Reloc_funcs; const elfcpp::Rela rela(preloc); unsigned int r_type = elfcpp::elf_r_type(rela.get_r_info()); @@ -3476,7 +3522,7 @@ Target_x86_64::Relocate::relocate( case elfcpp::R_X86_64_PC32: case elfcpp::R_X86_64_PC32_BND: rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend, - address, Reloc_funcs::CHECK_SIGNED); + address); break; case elfcpp::R_X86_64_16: @@ -3507,7 +3553,7 @@ Target_x86_64::Relocate::relocate( // behaves differently because psymval was set to point to // the PLT entry, rather than the symbol, in Scan::global(). rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend, - address, Reloc_funcs::CHECK_SIGNED); + address); break; case elfcpp::R_X86_64_PLTOFF64: @@ -3532,7 +3578,7 @@ Target_x86_64::Relocate::relocate( gold_assert(gsym); typename elfcpp::Elf_types::Elf_Addr value; value = target->got_plt_section()->address(); - Reloc_funcs::pcrela32(view, value, addend, address); + Reloc_funcs::pcrela32_check(view, value, addend, address); } break; @@ -3577,8 +3623,7 @@ Target_x86_64::Relocate::relocate( && Target_x86_64::can_convert_mov_to_lea(gsym)))) { view[-2] = 0x8d; - Reloc_funcs::pcrela32(view, object, psymval, addend, - address); + Reloc_funcs::pcrela32(view, object, psymval, addend, address); } else { @@ -3596,7 +3641,7 @@ Target_x86_64::Relocate::relocate( } typename elfcpp::Elf_types::Elf_Addr value; value = target->got_plt_section()->address() + got_offset; - Reloc_funcs::pcrela32(view, value, addend, address); + Reloc_funcs::pcrela32_check(view, value, addend, address); } } break;