From b77f8919bc1d246ebce0affe7f81eaf1ebeac725 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 1 May 2014 00:10:17 +0000 Subject: [PATCH] Start fixing pr19147. This makes the coff writer compute the correct symbol value for the test in pr19147. The section is still incorrect, that will be fixed in a followup patch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207728 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/WinCOFFObjectWriter.cpp | 23 +++++++++++++---------- test/MC/COFF/offset.s | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 test/MC/COFF/offset.s diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index fc0d51ca076..be7a9c8c0d3 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -385,6 +385,18 @@ void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) { SectionMap[&SectionData.getSection()] = coff_section; } +static uint64_t getSymbolValue(const MCSymbolData &Data, + const MCAsmLayout &Layout) { + if (Data.isCommon() && Data.isExternal()) + return Data.getCommonSize(); + + uint64_t Res; + if (!Layout.getSymbolOffset(&Data, Res)) + return 0; + + return Res; +} + /// This function takes a symbol data object from the assembler /// and creates the associated COFF symbol staging object. void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData, @@ -429,14 +441,7 @@ void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData, } else { const MCSymbolData &ResSymData = Assembler.getSymbolData(Symbol.AliasedSymbol()); - - if (Symbol.isVariable()) { - int64_t Addr; - if (Symbol.getVariableValue()->EvaluateAsAbsolute(Addr, Layout)) - coff_symbol->Data.Value = Addr; - } else if (SymbolData.isExternal() && SymbolData.isCommon()) { - coff_symbol->Data.Value = SymbolData.getCommonSize(); - } + coff_symbol->Data.Value = getSymbolValue(ResSymData, Layout); coff_symbol->Data.Type = (ResSymData.getFlags() & 0x0000FFFF) >> 0; coff_symbol->Data.StorageClass = (ResSymData.getFlags() & 0x00FF0000) >> 16; @@ -828,8 +833,6 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, assert(Symbol->Section != nullptr); Symbol->Data.SectionNumber = Symbol->Section->Number; - Symbol->Data.Value = Layout.getFragmentOffset(SymbolData->Fragment) - + SymbolData->Offset; } if (Symbol->should_keep()) { diff --git a/test/MC/COFF/offset.s b/test/MC/COFF/offset.s new file mode 100644 index 00000000000..9e6f42c4ddd --- /dev/null +++ b/test/MC/COFF/offset.s @@ -0,0 +1,19 @@ +// RUN: llvm-mc -filetype=obj -triple i686-pc-win32 %s -o - | llvm-readobj -t -r | FileCheck %s + + .data + .globl test1_foo +test1_foo: + .long 42 + + .globl test1_zed +test1_zed = test1_foo + 1 + +// CHECK: Symbol { +// CHECK: Name: test1_zed +// CHECK-NEXT: Value: 1 +// CHECK-NEXT: Section: +// CHECK-NEXT: BaseType: Null +// CHECK-NEXT: ComplexType: Null +// CHECK-NEXT: StorageClass: External +// CHECK-NEXT: AuxSymbolCount: 0 +// CHECK-NEXT: }