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.

llvm-svn: 204527
This commit is contained in:
Rafael Espindola 2014-03-21 22:00:29 +00:00
parent c97727a492
commit 66f96fe0cb
2 changed files with 58 additions and 23 deletions

View 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) { const MCAsmLayout &Layout) {
if (Data.isCommon() && Data.isExternal()) MCSymbolData *Data = &OrigData;
return Data.getCommonAlignment(); 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()) { uint64_t Res = 0;
if (const MCExpr *Value = Symbol.getVariableValue()) { if (Symbol->isVariable()) {
int64_t IntValue; const MCExpr *Expr = Symbol->getVariableValue();
if (Value->EvaluateAsAbsolute(IntValue, Layout)) { MCValue Value;
if (Data.getFlags() & ELF_Other_ThumbFunc) if (!Expr->EvaluateAsRelocatable(Value, &Layout))
return static_cast<uint64_t>(IntValue | 1); llvm_unreachable("Invalid expression");
else
return static_cast<uint64_t>(IntValue); 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()) if (IsThumbFunc)
return 0; Res |= 1;
if (Data.getFragment()) { if (!Symbol || !Symbol->isInSection())
if (Data.getFlags() & ELF_Other_ThumbFunc) return Res;
return Layout.getSymbolOffset(&Data) | 1;
else
return Layout.getSymbolOffset(&Data);
}
return 0; Res += Layout.getSymbolOffset(Data);
return Res;
} }
void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, 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); uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
Other |= Visibility; Other |= Visibility;
uint64_t Value = SymbolValue(Data, Layout); uint64_t Value = SymbolValue(OrigData, Layout);
if (OrigData.getFlags() & ELF_Other_ThumbFunc) if (OrigData.getFlags() & ELF_Other_ThumbFunc)
Value |= 1; Value |= 1;
uint64_t Size = 0; uint64_t Size = 0;

27
llvm/test/MC/ELF/offset.s Normal file
View File

@ -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: }