[llvm] r369671 - [MachO][TLOF] Use hasLocalLinkage to determine if indirect symbol is local

Francis Visoiu Mistrih via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 09:59:00 PDT 2019


Author: thegameg
Date: Thu Aug 22 09:59:00 2019
New Revision: 369671

URL: http://llvm.org/viewvc/llvm-project?rev=369671&view=rev
Log:
[MachO][TLOF] Use hasLocalLinkage to determine if indirect symbol is local

Local symbols in the indirect symbol table contain the value
`INDIRECT_SYMBOL_LOCAL` and the corresponding __pointers entry must
contain the address of the target.

In r349060, I added support for local symbols in the indirect symbol
table, which was checking if the symbol `isDefined` && `!isExternal` to
determine if the symbol is local or not.

It turns out that `isDefined` will return false if the user of the
symbol comes before its definition, and we'll again generate .long 0
which will be the symbol at the adress 0x0.

Instead of doing that, use GlobalValue::hasLocalLinkage() to check if
the symbol is local.

Differential Revision: https://reviews.llvm.org/D66563

Modified:
    llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
    llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.cpp
    llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.h
    llvm/trunk/lib/Target/X86/X86TargetObjectFile.cpp
    llvm/trunk/lib/Target/X86/X86TargetObjectFile.h
    llvm/trunk/test/MC/MachO/cstexpr-gotpcrel-32.ll

Modified: llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h (original)
+++ llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h Thu Aug 22 09:59:00 2019
@@ -127,7 +127,8 @@ public:
                                     MachineModuleInfo *MMI) const override;
 
   /// Get MachO PC relative GOT entry relocation
-  const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
+  const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                          const MCSymbol *Sym,
                                           const MCValue &MV, int64_t Offset,
                                           MachineModuleInfo *MMI,
                                           MCStreamer &Streamer) const override;

Modified: llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h (original)
+++ llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h Thu Aug 22 09:59:00 2019
@@ -191,7 +191,8 @@ public:
   }
 
   /// Get the target specific PC relative GOT entry relocation
-  virtual const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
+  virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                                  const MCSymbol *Sym,
                                                   const MCValue &MV,
                                                   int64_t Offset,
                                                   MachineModuleInfo *MMI,

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Thu Aug 22 09:59:00 2019
@@ -2635,7 +2635,7 @@ static void handleIndirectSymViaGOTPCRel
   const GlobalValue *FinalGV = dyn_cast<GlobalValue>(GV->getOperand(0));
   const MCSymbol *FinalSym = AP.getSymbol(FinalGV);
   *ME = AP.getObjFileLowering().getIndirectSymViaGOTPCRel(
-      FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer);
+      FinalGV, FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer);
 
   // Update GOT equivalent usage information
   --NumUses;

Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Thu Aug 22 09:59:00 2019
@@ -1108,8 +1108,8 @@ MCSymbol *TargetLoweringObjectFileMachO:
 }
 
 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
-    const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
-    MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
   // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
   // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
   // through a non_lazy_ptr stub instead. One advantage is that it allows the
@@ -1166,12 +1166,10 @@ const MCExpr *TargetLoweringObjectFileMa
   MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
 
   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
-  if (!StubSym.getPointer()) {
-    bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal();
-    // With the assumption that IsIndirectLocal == GV->hasLocalLinkage().
+
+  if (!StubSym.getPointer())
     StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
-                                                 !IsIndirectLocal);
-  }
+                                                 !GV->hasLocalLinkage());
 
   const MCExpr *BSymExpr =
     MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);

Modified: llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.cpp?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.cpp (original)
+++ llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.cpp Thu Aug 22 09:59:00 2019
@@ -59,8 +59,8 @@ MCSymbol *AArch64_MachoTargetObjectFile:
 }
 
 const MCExpr *AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
-    const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
-    MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
   assert((Offset+MV.getConstant() == 0) &&
          "Arch64 does not support GOT PC rel with extra offset");
   // On ARM64 Darwin, we can reference symbols with foo at GOT-., which

Modified: llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.h?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.h (original)
+++ llvm/trunk/lib/Target/AArch64/AArch64TargetObjectFile.h Thu Aug 22 09:59:00 2019
@@ -35,7 +35,8 @@ public:
                                     const TargetMachine &TM,
                                     MachineModuleInfo *MMI) const override;
 
-  const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
+  const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                          const MCSymbol *Sym,
                                           const MCValue &MV, int64_t Offset,
                                           MachineModuleInfo *MMI,
                                           MCStreamer &Streamer) const override;

Modified: llvm/trunk/lib/Target/X86/X86TargetObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86TargetObjectFile.cpp?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86TargetObjectFile.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86TargetObjectFile.cpp Thu Aug 22 09:59:00 2019
@@ -47,8 +47,8 @@ MCSymbol *X86_64MachoTargetObjectFile::g
 }
 
 const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
-    const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
-    MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
   // On Darwin/X86-64, we need to use foo at GOTPCREL+4 to access the got entry
   // from a data section. In case there's an additional offset, then use
   // foo at GOTPCREL+4+<offset>.

Modified: llvm/trunk/lib/Target/X86/X86TargetObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86TargetObjectFile.h?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86TargetObjectFile.h (original)
+++ llvm/trunk/lib/Target/X86/X86TargetObjectFile.h Thu Aug 22 09:59:00 2019
@@ -30,7 +30,8 @@ namespace llvm {
                                       const TargetMachine &TM,
                                       MachineModuleInfo *MMI) const override;
 
-    const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
+    const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                            const MCSymbol *Sym,
                                             const MCValue &MV, int64_t Offset,
                                             MachineModuleInfo *MMI,
                                             MCStreamer &Streamer) const override;

Modified: llvm/trunk/test/MC/MachO/cstexpr-gotpcrel-32.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/MachO/cstexpr-gotpcrel-32.ll?rev=369671&r1=369670&r2=369671&view=diff
==============================================================================
--- llvm/trunk/test/MC/MachO/cstexpr-gotpcrel-32.ll (original)
+++ llvm/trunk/test/MC/MachO/cstexpr-gotpcrel-32.ll Thu Aug 22 09:59:00 2019
@@ -73,7 +73,7 @@ define i32 @t0(i32 %a) {
   ret i32 %x
 }
 
-; Text indirect local symbols.
+; Test indirect local symbols.
 ; CHECK-LABEL: _localindirect
 ; CHECK: .long 65603
 @localindirect = internal constant i32  65603
@@ -85,11 +85,38 @@ define i32 @t0(i32 %a) {
   i32 sub (i32 ptrtoint (i32** @got.localindirect to i32),
            i32 ptrtoint (i32* @localindirectuser to i32))
 
+; Test internal indirect local symbols where the user doesn't see the
+; definition of the other symbols yet.
+
+; We used to check if the symbol is defined and not external to guess if it has
+; local linkage, but that doesn't work if the symbol is defined after. The code
+; should check if the GlobalValue itself has local linkage.
+
+; CHECK-LABEL: _undeflocalindirectuser:
+; CHECK: .long L_undeflocalindirect$non_lazy_ptr-_undeflocalindirectuser
+ at undeflocalindirectuser = internal constant
+  i32 sub (i32 ptrtoint (i32** @got.undeflocalindirect to i32),
+           i32 ptrtoint (i32* @undeflocalindirectuser to i32)),
+  section "__TEXT,__const"
+
+; CHECK-LABEL: _undeflocalindirect:
+; CHECK: .long 65603
+ at undeflocalindirect = internal constant i32  65603
+ at got.undeflocalindirect = private unnamed_addr constant i32* @undeflocalindirect
+
+; CHECK-LABEL: .section __IMPORT,__pointers
+
+; CHECK-LABEL: L_localfoo$non_lazy_ptr:
+; CHECK: .indirect_symbol _localfoo
+; CHECK-NOT: .long _localfoo
+; CHECK-NEXT: .long 0
+
 ; CHECK-LABEL: L_localindirect$non_lazy_ptr:
 ; CHECK: .indirect_symbol _localindirect
 ; CHECK-NOT: .long 0
 ; CHECK-NEXT: .long _localindirect
-define i8* @testRelativeIndirectSymbol() {
-  %1 = bitcast i32* @localindirectuser to i8*
-  ret i8* %1
-}
+
+; CHECK-LABEL: L_undeflocalindirect$non_lazy_ptr:
+; CHECK: .indirect_symbol _undeflocalindirect
+; CHECK-NOT: .long 0
+; CHECK-NEXT: .long _undeflocalindirect




More information about the llvm-commits mailing list