Fix PR23025.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 26 Mar 2015 21:11:00 +0000 (21:11 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 26 Mar 2015 21:11:00 +0000 (21:11 +0000)
There is something in link.exe that requires a relocation to use a
global symbol. Not doing so breaks the chrome build on windows.

This patch sets isWeak for that to work. To compensate,
we then need to look past those symbols when not creating relocations.

This patch includes an ELF test that matches GNU as behaviour.

I am still reducing the chrome build issue and will add a test
once that is done.

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

include/llvm/MC/MCExpr.h
lib/MC/MCAssembler.cpp
lib/MC/MCExpr.cpp
lib/MC/WinCOFFObjectWriter.cpp
test/MC/X86/expand-var.s

index 11da711ab9ca99bfa99cb4ecf255e46e1d6b4d56..d5a68be3ec652f80688e5312b38b95da0afc5909 100644 (file)
@@ -105,6 +105,13 @@ public:
   bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
                              const MCFixup *Fixup) const;
 
+  /// \brief Try to evaluate the expression to the form (a - b + constant) where
+  /// neither a nor b are variables.
+  ///
+  /// This is a more aggressive variant of EvaluateAsRelocatable. The intended
+  /// use is for when relocations are not available, like the .size directive.
+  bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
+
   /// FindAssociatedSection - Find the "associated section" for this expression,
   /// which is currently defined as the absolute section for constants, or
   /// otherwise the section associated with the first defined symbol in the
index 6345bd534171a0c4ae8a02452ac521e65e306c81..857eafc0b7e4585c9ecafcab234021c483e138e3 100644 (file)
@@ -188,7 +188,7 @@ const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
 
   const MCExpr *Expr = Symbol.getVariableValue();
   MCValue Value;
-  if (!Expr->EvaluateAsRelocatable(Value, this, nullptr))
+  if (!Expr->evaluateAsValue(Value, *this))
     llvm_unreachable("Invalid Expression");
 
   const MCSymbolRefExpr *RefB = Value.getSymB();
index 7508ef6712333286e79062cac2838f1a700d8a7e..8a64403362c03cbced1b72b880fbd645263b510e 100644 (file)
@@ -584,7 +584,15 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
                                    false);
 }
 
-static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm) {
+bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
+  MCAssembler *Assembler = &Layout.getAssembler();
+  return EvaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
+                                   true);
+}
+
+static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) {
+  if (InSet)
+    return true;
   if (!Asm)
     return false;
   const MCSymbolData &SD = Asm->getSymbolData(Sym);
@@ -613,10 +621,11 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
 
     // Evaluate recursively if this is a variable.
     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
-        canExpand(Sym, Asm)) {
+        canExpand(Sym, Asm, InSet)) {
+      bool IsMachO = SRE->hasSubsectionsViaSymbols();
       if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
-              Res, Asm, Layout, Fixup, Addrs, true)) {
-        if (!SRE->hasSubsectionsViaSymbols())
+              Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
+        if (!IsMachO)
           return true;
 
         const MCSymbolRefExpr *A = Res.getSymA();
index c519a9d6d5e895c68712b0e678ec54fc0813e97c..c6bc81ddc564a38077ac5ce7b45df88bbe8a5e12 100644 (file)
@@ -175,6 +175,8 @@ public:
                                               const MCFragment &FB, bool InSet,
                                               bool IsPCRel) const override;
 
+  bool isWeak(const MCSymbolData &SD) const override;
+
   void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
                         const MCFragment *Fragment, const MCFixup &Fixup,
                         MCValue Target, bool &IsPCRel,
@@ -661,6 +663,12 @@ bool WinCOFFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
                                                                 InSet, IsPCRel);
 }
 
+bool WinCOFFObjectWriter::isWeak(const MCSymbolData &SD) const {
+  // FIXME: this is for PR23025. Write a good description on
+  // why this is needed.
+  return SD.isExternal();
+}
+
 void WinCOFFObjectWriter::RecordRelocation(
     MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
     const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
index b3311b4a8440b9b3129fb44af8866eb74b924bf9..ef62d8a260facbd42e343d9279704edca0b249a7 100644 (file)
@@ -10,3 +10,9 @@ a:
         d = a
         .weak d
         .long d + (b - c)
+
+
+a2:
+        .weak b2
+        b2 = a2
+        c2 = b2 - a2