OSDN Git Service

MC: Fatally error if subtraction operand is bad
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 6 Jan 2014 07:39:46 +0000 (07:39 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 6 Jan 2014 07:39:46 +0000 (07:39 +0000)
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
lib/MC/WinCOFFObjectWriter.cpp

index 972c64c..6a48d62 100644 (file)
@@ -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);
 
index 6d27020..ed19b7e 100644 (file)
@@ -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);