From 6be0b9a8ddca0b2c937b31e0ee33fcbb7eb03dda Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 30 Dec 2020 18:47:26 -0800 Subject: [PATCH] [X86] Don't fold negative offset into 32-bit absolute address (e.g. movl $foo-1, %eax) When building abseil-cpp `bin/absl_hash_test` with Clang in -fno-pic mode, an instruction like `movl $foo-2147483648, $eax` may be produced (subtracting a number from the address of a static variable). If foo's address is smaller than 2147483648, GNU ld/gold/LLD will error because R_X86_64_32 cannot represent a negative value. ``` using absl::Hash; struct NoOp { template < typename HashCode > friend HashCode AbslHashValue(HashCode , NoOp ); }; template class HashIntTest : public testing::Test {}; TYPED_TEST_SUITE_P(HashIntTest); TYPED_TEST_P(HashIntTest, BasicUsage) { if (std::numeric_limits< TypeParam >::min ) EXPECT_NE(Hash< NoOp >()({}), Hash< TypeParam >()(std::numeric_limits< TypeParam >::min())); } REGISTER_TYPED_TEST_CASE_P(HashIntTest, BasicUsage); using IntTypes = testing::Types< int32_t>; INSTANTIATE_TYPED_TEST_CASE_P(My, HashIntTest, IntTypes); ld: error: hash_test.cc:(function (anonymous namespace)::gtest_suite_HashIntTest_::BasicUsage::TestBody(): .text+0x4E472): relocation R_X86_64_32 out of range: 18446744071564237392 is not in [0, 4294967295]; references absl::hash_internal::HashState::kSeed ``` Actually any negative offset is not allowed because the symbol address can be zero (e.g. set by `-Wl,--defsym=foo=0`). So disallow such folding. Reviewed By: pengfei Differential Revision: https://reviews.llvm.org/D93931 --- llvm/lib/Target/X86/X86ISelLowering.cpp | 7 +++++-- llvm/lib/Target/X86/X86ISelLowering.h | 2 +- llvm/test/CodeGen/X86/fold-add.ll | 8 ++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index f1803c4..8113180 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -19102,9 +19102,12 @@ SDValue X86TargetLowering::LowerGlobalOrExternal(SDValue Op, SelectionDAG &DAG, if (GV) { // Create a target global address if this is a global. If possible, fold the // offset into the global address reference. Otherwise, ADD it on later. + // Suppress the folding if Offset is negative: movl foo-1, %eax is not + // allowed because if the address of foo is 0, the ELF R_X86_64_32 + // relocation will compute to a negative value, which is invalid. int64_t GlobalOffset = 0; - if (OpFlags == X86II::MO_NO_FLAG && - X86::isOffsetSuitableForCodeModel(Offset, M)) { + if (OpFlags == X86II::MO_NO_FLAG && Offset >= 0 && + X86::isOffsetSuitableForCodeModel(Offset, M, true)) { std::swap(GlobalOffset, Offset); } Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, GlobalOffset, OpFlags); diff --git a/llvm/lib/Target/X86/X86ISelLowering.h b/llvm/lib/Target/X86/X86ISelLowering.h index faf2cc6..6681322 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.h +++ b/llvm/lib/Target/X86/X86ISelLowering.h @@ -855,7 +855,7 @@ namespace llvm { /// Returns true of the given offset can be /// fit into displacement field of the instruction. bool isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M, - bool hasSymbolicDisplacement = true); + bool hasSymbolicDisplacement); /// Determines whether the callee is required to pop its /// own arguments. Callee pop is necessary to support tail calls. diff --git a/llvm/test/CodeGen/X86/fold-add.ll b/llvm/test/CodeGen/X86/fold-add.ll index fc21e3b..5dfbccd 100644 --- a/llvm/test/CodeGen/X86/fold-add.ll +++ b/llvm/test/CodeGen/X86/fold-add.ll @@ -54,10 +54,12 @@ entry: ret i64 add (i64 ptrtoint (i32* @foo to i64), i64 1701208431) } +;; Test we don't emit movl foo-1, %eax. ELF R_X86_64_32 does not allow +;; a negative value. define dso_local i64 @neg_1() #0 { ; CHECK-LABEL: neg_1: ; CHECK: # %bb.0: -; STATIC-NEXT: movl $foo-1, %eax +; STATIC-NEXT: leaq foo-1(%rip), %rax ; PIC-NEXT: leaq foo-1(%rip), %rax ; MSTATIC-NEXT: movabsq $foo, %rax ; MSTATIC-NEXT: decq %rax @@ -68,10 +70,12 @@ entry: ret i64 add (i64 ptrtoint (i32* @foo to i64), i64 -1) } +;; Test we don't emit movl foo-2147483648, %eax. ELF R_X86_64_32 does not allow +;; a negative value. define dso_local i64 @neg_0x80000000() #0 { ; CHECK-LABEL: neg_0x80000000: ; CHECK: # %bb.0: -; STATIC-NEXT: movl $foo-2147483648, %eax +; STATIC-NEXT: leaq foo-2147483648(%rip), %rax ; PIC-NEXT: leaq foo-2147483648(%rip), %rax ; MSTATIC-NEXT: movabsq $foo, %rax ; MSTATIC-NEXT: addq $-2147483648, %rax -- 2.7.4