From ba7548302b23b1dd8ce4e6fa941c143fda49fe5e Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Mon, 6 Jan 2014 07:39:46 +0000 Subject: [PATCH] MC: Fatally error if subtraction operand is bad Instead of crashing, raise an error when a subtraction expression involves an undefined symbol. This fixes PR18375. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198590 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/ELFObjectWriter.cpp | 6 ++++++ lib/MC/WinCOFFObjectWriter.cpp | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index 972c64cc565..6a48d62c8e1 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -720,6 +720,12 @@ void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, MCSymbolData &SDB = Asm.getSymbolData(SymbolB); IsPCRel = true; + if (!SDB.getFragment()) + Asm.getContext().FatalError( + Fixup.getLoc(), + Twine("symbol '") + SymbolB.getName() + + "' can not be undefined in a subtraction expression"); + // Offset of the symbol in the section int64_t a = Layout.getSymbolOffset(&SDB); diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index 6d270209b74..ed19b7ec69d 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -648,14 +648,25 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, COFFSection *coff_section = SectionMap[&SectionData->getSection()]; COFFSymbol *coff_symbol = SymbolMap[&A_SD.getSymbol()]; - const MCSymbolRefExpr *SymA = Target.getSymA(); const MCSymbolRefExpr *SymB = Target.getSymB(); - const bool CrossSection = SymB && - &SymA->getSymbol().getSection() != &SymB->getSymbol().getSection(); + bool CrossSection = false; - if (Target.getSymB()) { - const MCSymbol *B = &Target.getSymB()->getSymbol(); + if (SymB) { + const MCSymbol *B = &SymB->getSymbol(); MCSymbolData &B_SD = Asm.getSymbolData(*B); + if (!B_SD.getFragment()) + Asm.getContext().FatalError( + Fixup.getLoc(), + Twine("symbol '") + B->getName() + + "' can not be undefined in a subtraction expression"); + + if (!A_SD.getFragment()) + Asm.getContext().FatalError( + Fixup.getLoc(), + Twine("symbol '") + Symbol.getName() + + "' can not be undefined in a subtraction expression"); + + CrossSection = &Symbol.getSection() != &B->getSection(); // Offset of the symbol in the section int64_t a = Layout.getSymbolOffset(&B_SD); -- 2.11.0