[llvm] r204527 - Fix the value computation in
Rafael Espindola
rafael.espindola at gmail.com
Fri Mar 21 15:00:30 PDT 2014
Author: rafael
Date: Fri Mar 21 17:00:29 2014
New Revision: 204527
URL: http://llvm.org/viewvc/llvm-project?rev=204527&view=rev
Log:
Fix the value computation in
sym_a:
sym_d = sym_a + 1
This is the smallest fix I was able to extract from what got reverted in
r204203.
Added:
llvm/trunk/test/MC/ELF/offset.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=204527&r1=204526&r2=204527&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Fri Mar 21 17:00:29 2014
@@ -462,36 +462,44 @@ void ELFObjectWriter::WriteSymbolEntry(M
}
}
-uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
+uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &OrigData,
const MCAsmLayout &Layout) {
- if (Data.isCommon() && Data.isExternal())
- return Data.getCommonAlignment();
-
- const MCSymbol &Symbol = Data.getSymbol();
-
- 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);
- }
+ MCSymbolData *Data = &OrigData;
+ if (Data->isCommon() && Data->isExternal())
+ return Data->getCommonAlignment();
+
+ const MCSymbol *Symbol = &Data->getSymbol();
+ bool IsThumbFunc = OrigData.getFlags() & ELF_Other_ThumbFunc;
+
+ 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(MCData
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;
Added: llvm/trunk/test/MC/ELF/offset.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/offset.s?rev=204527&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/offset.s (added)
+++ llvm/trunk/test/MC/ELF/offset.s Fri Mar 21 17:00:29 2014
@@ -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: }
More information about the llvm-commits
mailing list