[llvm] r233139 - Don't be over eager in evaluating a subtraction with a weak symbol.

Rafael Espindola rafael.espindola at gmail.com
Tue Mar 24 16:48:44 PDT 2015


Author: rafael
Date: Tue Mar 24 18:48:44 2015
New Revision: 233139

URL: http://llvm.org/viewvc/llvm-project?rev=233139&view=rev
Log:
Don't be over eager in evaluating a subtraction with a weak symbol.

In a subtraction of the form A - B, if B is weak, there is no way to represent
that on ELF since all relocations add the value of a symbol.

Added:
    llvm/trunk/test/MC/ELF/weak-diff.s
Modified:
    llvm/trunk/lib/MC/ELFObjectWriter.cpp

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=233139&r1=233138&r2=233139&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Tue Mar 24 18:48:44 2015
@@ -803,6 +803,10 @@ static const MCSymbol *getWeakRef(const
   return nullptr;
 }
 
+static bool isWeak(const MCSymbolData &D) {
+  return D.getFlags() & ELF_STB_Weak || MCELF::GetType(D) == ELF::STT_GNU_IFUNC;
+}
+
 void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
                                        const MCAsmLayout &Layout,
                                        const MCFragment *Fragment,
@@ -843,6 +847,10 @@ void ELFObjectWriter::RecordRelocation(M
           Fixup.getLoc(), "Cannot represent a difference across sections");
 
     const MCSymbolData &SymBD = Asm.getSymbolData(SymB);
+    if (isWeak(SymBD))
+      Asm.getContext().FatalError(
+          Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
+
     uint64_t SymBOffset = Layout.getSymbolOffset(&SymBD);
     uint64_t K = SymBOffset - FixupOffset;
     IsPCRel = true;
@@ -1809,7 +1817,7 @@ ELFObjectWriter::IsSymbolRefDifferenceFu
                                                       const MCFragment &FB,
                                                       bool InSet,
                                                       bool IsPCRel) const {
-  if (DataA.getFlags() & ELF_STB_Weak || MCELF::GetType(DataA) == ELF::STT_GNU_IFUNC)
+  if (isWeak(DataA))
     return false;
   return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
                                                  Asm, DataA, FB,InSet, IsPCRel);

Added: llvm/trunk/test/MC/ELF/weak-diff.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/weak-diff.s?rev=233139&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/weak-diff.s (added)
+++ llvm/trunk/test/MC/ELF/weak-diff.s Tue Mar 24 18:48:44 2015
@@ -0,0 +1,12 @@
+// RUN: not llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o %t 2>&1 | FileCheck %s
+
+// CHECK: error: Cannot represent a subtraction with a weak symbol
+
+.weak f
+.weak g
+f:
+    nop
+g:
+    nop
+
+.quad g - f





More information about the llvm-commits mailing list