Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2086,8 +2086,12 @@ 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 @@ 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: test/MC/MachO/cstexpr-gotpcrel-64.ll =================================================================== --- test/MC/MachO/cstexpr-gotpcrel-64.ll +++ test/MC/MachO/cstexpr-gotpcrel-64.ll @@ -84,3 +84,12 @@ 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