[llvm] r316799 - Handle undefined weak hidden symbols on all architectures.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 27 14:18:48 PDT 2017


Author: rafael
Date: Fri Oct 27 14:18:48 2017
New Revision: 316799

URL: http://llvm.org/viewvc/llvm-project?rev=316799&view=rev
Log:
Handle undefined weak hidden symbols on all architectures.

We were handling the non-hidden case in lib/Target/TargetMachine.cpp,
but the hidden case was handled in architecture dependent code and
only X86_64 and AArch64 were covered.

While it is true that some code sequences in some ABIs might be able
to produce the correct value at runtime, that doesn't seem to be the
common case.

I left the AArch64 code in place since it also forces a got access for
non-pic code. It is not clear if that is needed, but it is probably
better to change that in another commit.

Modified:
    llvm/trunk/lib/Target/TargetMachine.cpp
    llvm/trunk/lib/Target/X86/X86Subtarget.cpp
    llvm/trunk/test/CodeGen/X86/global-access-pie-copyrelocs.ll
    llvm/trunk/test/CodeGen/X86/weak-undef.ll

Modified: llvm/trunk/lib/Target/TargetMachine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetMachine.cpp?rev=316799&r1=316798&r2=316799&view=diff
==============================================================================
--- llvm/trunk/lib/Target/TargetMachine.cpp (original)
+++ llvm/trunk/lib/Target/TargetMachine.cpp Fri Oct 27 14:18:48 2017
@@ -128,6 +128,15 @@ bool TargetMachine::shouldAssumeDSOLocal
   if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
     return true;
 
+  // Most PIC code sequences that assume that a symbol is local cannot
+  // produce a 0 if it turns out the symbol is undefined. While this
+  // is ABI and relocation depended, it seems worth it to handle it
+  // here.
+  // FIXME: this is probably not ELF specific.
+  if (GV && isPositionIndependent() && TT.isOSBinFormatELF() &&
+      GV->hasExternalWeakLinkage())
+    return false;
+
   if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility() ||
               GV->isDSOLocal()))
     return true;
@@ -149,9 +158,8 @@ bool TargetMachine::shouldAssumeDSOLocal
       return true;
 
     bool IsTLS = GV && GV->isThreadLocal();
-    bool IsAccessViaCopyRelocs = Options.MCOptions.MCPIECopyRelocations && GV &&
-                                 isa<GlobalVariable>(GV) &&
-                                 !GV->hasExternalWeakLinkage();
+    bool IsAccessViaCopyRelocs =
+        Options.MCOptions.MCPIECopyRelocations && GV && isa<GlobalVariable>(GV);
     Triple::ArchType Arch = TT.getArch();
     bool IsPPC =
         Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le;

Modified: llvm/trunk/lib/Target/X86/X86Subtarget.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86Subtarget.cpp?rev=316799&r1=316798&r2=316799&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86Subtarget.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86Subtarget.cpp Fri Oct 27 14:18:48 2017
@@ -99,22 +99,6 @@ 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.
@@ -134,7 +118,7 @@ unsigned char X86Subtarget::classifyGlob
     }
   }
 
-  if (shouldAssumeGlobalReferenceLocal(this, TM, M, GV))
+  if (TM.shouldAssumeDSOLocal(M, GV))
     return classifyLocalReference(GV);
 
   if (isTargetCOFF())

Modified: llvm/trunk/test/CodeGen/X86/global-access-pie-copyrelocs.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/global-access-pie-copyrelocs.ll?rev=316799&r1=316798&r2=316799&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/global-access-pie-copyrelocs.ll (original)
+++ llvm/trunk/test/CodeGen/X86/global-access-pie-copyrelocs.ll Fri Oct 27 14:18:48 2017
@@ -77,6 +77,19 @@ entry:
   ret i32* @e
 }
 
+; ExternalWeak hidden Linkage
+ at he = extern_weak hidden global i32, align 4
+
+define i32* @my_access_global_he() #0 {
+; X32-LABEL: my_access_global_he:
+; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
+; X32:       movl he at GOT(%eax), %eax
+; X64-LABEL: my_access_global_he:
+; X64:       movq he at GOTPCREL(%rip), %rax
+  ret i32* @he
+}
+
+
 ; External Linkage, only declaration, store a value.
 
 define i32 @my_access_global_store_d() #0 {

Modified: llvm/trunk/test/CodeGen/X86/weak-undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/weak-undef.ll?rev=316799&r1=316798&r2=316799&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/weak-undef.ll (original)
+++ llvm/trunk/test/CodeGen/X86/weak-undef.ll Fri Oct 27 14:18:48 2017
@@ -8,7 +8,7 @@ define i32* @bar1() {
 ; CHECK: bar1:
 ; CHECK: movq foo1 at GOTPCREL(%rip), %rax
 ; I386: bar1:
-; I386: leal foo1 at GOTOFF(%eax), %eax
+; I386: movl foo1 at GOT(%eax), %eax
 
 @foo2 = external hidden global i32, align 4
 define i32* @bar2() {
@@ -46,7 +46,7 @@ define i32()* @bar5() {
 ; CHECK: bar5:
 ; CHECK: movq foo5 at GOTPCREL(%rip), %rax
 ; I386: bar5:
-; I386: leal foo5 at GOTOFF(%eax), %eax
+; I386: movl foo5 at GOT(%eax), %eax
 
 declare external hidden i32 @foo6()
 define i32()* @bar6() {




More information about the llvm-commits mailing list