Fix the value computation in
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 21 Mar 2014 22:00:29 +0000 (22:00 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 21 Mar 2014 22:00:29 +0000 (22:00 +0000)
sym_a:
sym_d = sym_a + 1

This is the smallest fix I was able to extract from what got reverted in
r204203.

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

lib/MC/ELFObjectWriter.cpp
test/MC/ELF/offset.s [new file with mode: 0644]

index 9168fea53bd58432abce05fc48b12bce6f3f76e5..dc77e0361c5aa246b54e4e362637f531a6a3b6f4 100644 (file)
@@ -462,36 +462,44 @@ void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
   }
 }
 
-uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
+uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &OrigData,
                                       const MCAsmLayout &Layout) {
-  if (Data.isCommon() && Data.isExternal())
-    return Data.getCommonAlignment();
+  MCSymbolData *Data = &OrigData;
+  if (Data->isCommon() && Data->isExternal())
+    return Data->getCommonAlignment();
 
-  const MCSymbol &Symbol = Data.getSymbol();
+  const MCSymbol *Symbol = &Data->getSymbol();
+  bool IsThumbFunc = OrigData.getFlags() & ELF_Other_ThumbFunc;
 
-  if (Symbol.isAbsolute() && Symbol.isVariable()) {
-    if (const MCExpr *Value = Symbol.getVariableValue()) {
-      int64_t IntValue;
-      if (Value->EvaluateAsAbsolute(IntValue, Layout)) {
-        if (Data.getFlags() & ELF_Other_ThumbFunc)
-          return static_cast<uint64_t>(IntValue | 1);
-        else
-          return static_cast<uint64_t>(IntValue);
-      }
+  uint64_t Res = 0;
+  if (Symbol->isVariable()) {
+    const MCExpr *Expr = Symbol->getVariableValue();
+    MCValue Value;
+    if (!Expr->EvaluateAsRelocatable(Value, &Layout))
+      llvm_unreachable("Invalid expression");
+
+    assert(!Value.getSymB());
+
+    Res = Value.getConstant();
+
+    if (const MCSymbolRefExpr *A = Value.getSymA()) {
+      Symbol = &A->getSymbol();
+      Data = &Layout.getAssembler().getSymbolData(*Symbol);
+    } else {
+      Symbol = 0;
+      Data = 0;
     }
   }
 
-  if (!Symbol.isInSection())
-    return 0;
+  if (IsThumbFunc)
+    Res |= 1;
 
-  if (Data.getFragment()) {
-    if (Data.getFlags() & ELF_Other_ThumbFunc)
-      return Layout.getSymbolOffset(&Data) | 1;
-    else
-      return Layout.getSymbolOffset(&Data);
-  }
+  if (!Symbol || !Symbol->isInSection())
+    return Res;
+
+  Res += Layout.getSymbolOffset(Data);
 
-  return 0;
+  return Res;
 }
 
 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
@@ -591,7 +599,7 @@ void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
   uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
   Other |= Visibility;
 
-  uint64_t Value = SymbolValue(Data, Layout);
+  uint64_t Value = SymbolValue(OrigData, Layout);
   if (OrigData.getFlags() & ELF_Other_ThumbFunc)
     Value |= 1;
   uint64_t Size = 0;
diff --git a/test/MC/ELF/offset.s b/test/MC/ELF/offset.s
new file mode 100644 (file)
index 0000000..962d397
--- /dev/null
@@ -0,0 +1,27 @@
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -t - | FileCheck %s
+
+// Test that a variable declared with "var = other_var + cst" is in the same
+// section as other_var and its value is the value of other_var + cst.
+
+sym_a:
+sym_d = sym_a + 1
+
+
+// CHECK:       Symbol {
+// CHECK:         Name: sym_a
+// CHECK-NEXT:    Value: 0x0
+// CHECK-NEXT:    Size: 0
+// CHECK-NEXT:    Binding: Local (0x0)
+// CHECK-NEXT:    Type: None (0x0)
+// CHECK-NEXT:    Other: 0
+// CHECK-NEXT:    Section: .text (0x1)
+// CHECK-NEXT:  }
+// CHECK-NEXT:  Symbol {
+// CHECK-NEXT:    Name: sym_d
+// CHECK-NEXT:    Value: 0x1
+// CHECK-NEXT:    Size: 0
+// CHECK-NEXT:    Binding: Local (0x0)
+// CHECK-NEXT:    Type: None (0x0)
+// CHECK-NEXT:    Other: 0
+// CHECK-NEXT:    Section:  (0xFFF1)
+// CHECK-NEXT:  }