[llvm] r310756 - Fix access to undefined weak symbols in pic code
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 11 13:49:27 PDT 2017
Author: rafael
Date: Fri Aug 11 13:49:27 2017
New Revision: 310756
URL: http://llvm.org/viewvc/llvm-project?rev=310756&view=rev
Log:
Fix access to undefined weak symbols in pic code
When the access to a weak symbol is not a call, the access has to be
able to produce the value 0 at runtime.
We were sometimes producing code sequences where that was not possible
if the code was leaded more than 4g away from 0.
Added:
llvm/trunk/test/CodeGen/X86/weak-undef.ll
Modified:
llvm/trunk/lib/Target/X86/X86Subtarget.cpp
Modified: llvm/trunk/lib/Target/X86/X86Subtarget.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86Subtarget.cpp?rev=310756&r1=310755&r2=310756&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86Subtarget.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86Subtarget.cpp Fri Aug 11 13:49:27 2017
@@ -99,6 +99,22 @@ X86Subtarget::classifyLocalReference(con
return X86II::MO_GOTOFF;
}
+static bool shouldAssumeGlobalReferenceLocal(const X86Subtarget *ST,
+ const TargetMachine &TM,
+ const Module &M,
+ const GlobalValue *GV) {
+ if (!TM.shouldAssumeDSOLocal(M, GV))
+ return false;
+ // A weak reference can end up being 0. If the code can be more that 4g away
+ // from zero and we are using the small code model we have to treat it as non
+ // local.
+ if (GV && GV->hasExternalWeakLinkage() &&
+ TM.getCodeModel() == CodeModel::Small && TM.isPositionIndependent() &&
+ ST->is64Bit() && ST->isTargetELF())
+ return false;
+ return true;
+}
+
unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV,
const Module &M) const {
// Large model never uses stubs.
@@ -118,7 +134,7 @@ unsigned char X86Subtarget::classifyGlob
}
}
- if (TM.shouldAssumeDSOLocal(M, GV))
+ if (shouldAssumeGlobalReferenceLocal(this, TM, M, GV))
return classifyLocalReference(GV);
if (isTargetCOFF())
Added: llvm/trunk/test/CodeGen/X86/weak-undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/weak-undef.ll?rev=310756&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/weak-undef.ll (added)
+++ llvm/trunk/test/CodeGen/X86/weak-undef.ll Fri Aug 11 13:49:27 2017
@@ -0,0 +1,58 @@
+; RUN: llc < %s -relocation-model=pic -mtriple=x86_64-pc-linux | FileCheck %s
+; RUN: llc < %s -relocation-model=pic -mtriple=i386-pc-linux | FileCheck --check-prefix=I386 %s
+
+ at foo1 = extern_weak hidden global i32, align 4
+define i32* @bar1() {
+ ret i32* @foo1
+}
+; CHECK: bar1:
+; CHECK: movq foo1 at GOTPCREL(%rip), %rax
+; I386: bar1:
+; I386: leal foo1 at GOTOFF(%eax), %eax
+
+ at foo2 = external hidden global i32, align 4
+define i32* @bar2() {
+ ret i32* @foo2
+}
+; CHECK: bar2:
+; CHECK: leaq foo2(%rip), %rax
+; I386: bar2:
+; I386: leal foo2 at GOTOFF(%eax), %eax
+
+declare extern_weak hidden void @foo3()
+define void @bar3() {
+ call void @foo3()
+ ret void
+}
+; CHECK: bar3:
+; CHECK: callq foo3
+; I386: bar3:
+; I386: calll foo3
+
+declare external hidden void @foo4()
+define void @bar4() {
+ call void @foo4()
+ ret void
+}
+; CHECK: bar4:
+; CHECK: callq foo4
+; I386: bar4:
+; I386: calll foo4
+
+declare extern_weak hidden i32 @foo5()
+define i32()* @bar5() {
+ ret i32()* @foo5
+}
+; CHECK: bar5:
+; CHECK: movq foo5 at GOTPCREL(%rip), %rax
+; I386: bar5:
+; I386: leal foo5 at GOTOFF(%eax), %eax
+
+declare external hidden i32 @foo6()
+define i32()* @bar6() {
+ ret i32()* @foo6
+}
+; CHECK: bar6:
+; CHECK: leaq foo6(%rip), %rax
+; I386: bar6:
+; I386: leal foo6 at GOTOFF(%eax), %eax
More information about the llvm-commits
mailing list