diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-08-24 21:04:52 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-08-24 21:04:52 +0000 |
commit | 82c84fdd23669d23c02a07498c83b83702979829 (patch) | |
tree | 245805e02b15f9a5b662ec25729dbf557698f373 /lib | |
parent | 3e242f86ebe6105ceb855d6640d2c49ca7c7ded2 (diff) | |
download | external_llvm-82c84fdd23669d23c02a07498c83b83702979829.zip external_llvm-82c84fdd23669d23c02a07498c83b83702979829.tar.gz external_llvm-82c84fdd23669d23c02a07498c83b83702979829.tar.bz2 |
Fix COFF x86-64 relocations. PR7960.
Multiple symbol reloc handling part of the patch by Cameron Esfahani.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111963 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/MC/WinCOFFObjectWriter.cpp | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index eab2ba9..eeb2b96 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -125,6 +125,7 @@ public: typedef DenseMap<MCSectionData const *, COFFSection *> section_map; // Root level file contents. + bool Is64Bit; COFF::header Header; sections Sections; symbols Symbols; @@ -274,10 +275,11 @@ size_t StringTable::insert(llvm::StringRef String) { // WinCOFFObjectWriter class implementation WinCOFFObjectWriter::WinCOFFObjectWriter(raw_ostream &OS, bool is64Bit) - : MCObjectWriter(OS, true) { + : MCObjectWriter(OS, true) + , Is64Bit(is64Bit) { memset(&Header, 0, sizeof(Header)); - is64Bit ? Header.Machine = COFF::IMAGE_FILE_MACHINE_AMD64 + Is64Bit ? Header.Machine = COFF::IMAGE_FILE_MACHINE_AMD64 : Header.Machine = COFF::IMAGE_FILE_MACHINE_I386; } @@ -565,22 +567,34 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, MCValue Target, uint64_t &FixedValue) { assert(Target.getSymA() != NULL && "Relocation must reference a symbol!"); - assert(Target.getSymB() == NULL && - "Relocation must reference only one symbol!"); + + const MCSymbol *A = &Target.getSymA()->getSymbol(); + MCSymbolData &A_SD = Asm.getSymbolData(*A); MCSectionData const *SectionData = Fragment->getParent(); - MCSymbolData const *SymbolData = - &Asm.getSymbolData(Target.getSymA()->getSymbol()); + // Mark this symbol as requiring an entry in the symbol table. assert(SectionMap.find(SectionData) != SectionMap.end() && "Section must already have been defined in ExecutePostLayoutBinding!"); - assert(SymbolMap.find(SymbolData) != SymbolMap.end() && + assert(SymbolMap.find(&A_SD) != SymbolMap.end() && "Symbol must already have been defined in ExecutePostLayoutBinding!"); COFFSection *coff_section = SectionMap[SectionData]; - COFFSymbol *coff_symbol = SymbolMap[SymbolData]; + COFFSymbol *coff_symbol = SymbolMap[&A_SD]; + + if (Target.getSymB()) { + const MCSymbol *B = &Target.getSymB()->getSymbol(); + MCSymbolData &B_SD = Asm.getSymbolData(*B); - FixedValue = Target.getConstant(); + FixedValue = Layout.getSymbolAddress(&A_SD) - Layout.getSymbolAddress(&B_SD); + + // In the case where we have SymbA and SymB, we just need to store the delta + // between the two symbols. Update FixedValue to account for the delta, and + // skip recording the relocation. + return; + } else { + FixedValue = Target.getConstant(); + } COFFRelocation Reloc; @@ -590,40 +604,29 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, Reloc.Data.VirtualAddress += Fixup.getOffset(); - COFF::RelocationTypeX86 Type; - - if (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386) { - switch (Fixup.getKind()) { - case X86::reloc_pcrel_4byte: - Type = COFF::IMAGE_REL_I386_REL32; - FixedValue += 4; - break; - case FK_Data_4: - Type = COFF::IMAGE_REL_I386_DIR32; - break; - default: + switch (Fixup.getKind()) { + case X86::reloc_pcrel_4byte: + case X86::reloc_riprel_4byte: + case X86::reloc_riprel_4byte_movq_load: + Reloc.Data.Type = Is64Bit ? COFF::IMAGE_REL_AMD64_REL32 + : COFF::IMAGE_REL_I386_REL32; + // FIXME: Can anyone explain what this does other than adjust for the size + // of the offset? + FixedValue += 4; + break; + case FK_Data_4: + Reloc.Data.Type = Is64Bit ? COFF::IMAGE_REL_AMD64_ADDR32 + : COFF::IMAGE_REL_I386_DIR32; + break; + case FK_Data_8: + if (Is64Bit) + Reloc.Data.Type = COFF::IMAGE_REL_AMD64_ADDR64; + else llvm_unreachable("unsupported relocation type"); - } - } else if (Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) { - switch (Fixup.getKind()) { - case FK_Data_8: - Type = COFF::IMAGE_REL_AMD64_ADDR64; - break; - case X86::reloc_pcrel_4byte: - case X86::reloc_riprel_4byte: - Type = COFF::IMAGE_REL_AMD64_REL32; - FixedValue += 4; - break; - case FK_Data_4: - Type = COFF::IMAGE_REL_AMD64_ADDR32; - break; - default: - llvm_unreachable("unsupported relocation type"); - } - } else - llvm_unreachable("unknown target architecture"); - - Reloc.Data.Type = Type; + break; + default: + llvm_unreachable("unsupported relocation type"); + } coff_section->Relocations.push_back(Reloc); } |