[AsmPrinter] Fix crash in handleIndirectSymViaGOTPCRel
authorBruno Cardoso Lopes <bruno.cardoso@gmail.com>
Thu, 25 Jun 2015 15:17:23 +0000 (15:17 +0000)
committerBruno Cardoso Lopes <bruno.cardoso@gmail.com>
Thu, 25 Jun 2015 15:17:23 +0000 (15:17 +0000)
Check for symbols in MCValue before using them. Bail out early in case
they are null. This fixes PR23779.

Differential Revision: http://reviews.llvm.org/D10712

rdar://problem/21532830

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240649 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/AsmPrinter.cpp
test/MC/MachO/cstexpr-gotpcrel-64.ll

index 9e845e0e99bf1f585bcd8fe85928f41849555b9f..28f5bc49dcabd2cf914680904b6fbafaddd852ec 100644 (file)
@@ -2086,8 +2086,12 @@ static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
   MCValue MV;
   if (!(*ME)->evaluateAsRelocatable(MV, nullptr, nullptr) || MV.isAbsolute())
     return;
+  const MCSymbolRefExpr *SymA = MV.getSymA();
+  if (!SymA)
+    return;
 
-  const MCSymbol *GOTEquivSym = &MV.getSymA()->getSymbol();
+  // Check that GOT equivalent symbol is cached.
+  const MCSymbol *GOTEquivSym = &SymA->getSymbol();
   if (!AP.GlobalGOTEquivs.count(GOTEquivSym))
     return;
 
@@ -2095,8 +2099,11 @@ static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
   if (!BaseGV)
     return;
 
+  // Check for a valid base symbol
   const MCSymbol *BaseSym = AP.getSymbol(BaseGV);
-  if (BaseSym != &MV.getSymB()->getSymbol())
+  const MCSymbolRefExpr *SymB = MV.getSymB();
+
+  if (!SymB || BaseSym != &SymB->getSymbol())
     return;
 
   // Make sure to match:
index bf155647f12a876d01e873757c44aadcc017d447..bafddcb3db698331aee558e47ae4a3971bbaa5a7 100644 (file)
@@ -84,3 +84,12 @@ define i32 @t0(i32 %a) {
 define i32** @t1() {
   ret i32** @bargotequiv
 }
+
+; Do not crash when a pattern cannot be matched as a GOT equivalent
+
+@a = external global i8
+@b = internal unnamed_addr constant i8* @a
+
+; X86-LABEL: _c:
+; X86:   .quad _b
+@c = global i8** @b