X86WinCOFFObjectWriter.cpp revision 303975
1//===-- X86WinCOFFObjectWriter.cpp - X86 Win COFF Writer ------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "MCTargetDesc/X86FixupKinds.h" 11#include "MCTargetDesc/X86MCTargetDesc.h" 12#include "llvm/MC/MCExpr.h" 13#include "llvm/MC/MCValue.h" 14#include "llvm/MC/MCWinCOFFObjectWriter.h" 15#include "llvm/Support/COFF.h" 16#include "llvm/Support/ErrorHandling.h" 17 18using namespace llvm; 19 20namespace llvm { 21 class MCObjectWriter; 22} 23 24namespace { 25 class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter { 26 public: 27 X86WinCOFFObjectWriter(bool Is64Bit); 28 ~X86WinCOFFObjectWriter() override; 29 30 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup, 31 bool IsCrossSection, 32 const MCAsmBackend &MAB) const override; 33 }; 34} 35 36X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit) 37 : MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64 38 : COFF::IMAGE_FILE_MACHINE_I386) {} 39 40X86WinCOFFObjectWriter::~X86WinCOFFObjectWriter() {} 41 42unsigned X86WinCOFFObjectWriter::getRelocType(const MCValue &Target, 43 const MCFixup &Fixup, 44 bool IsCrossSection, 45 const MCAsmBackend &MAB) const { 46 unsigned FixupKind = IsCrossSection ? FK_PCRel_4 : Fixup.getKind(); 47 48 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? 49 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); 50 51 if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) { 52 switch (FixupKind) { 53 case FK_PCRel_4: 54 case X86::reloc_riprel_4byte: 55 case X86::reloc_riprel_4byte_movq_load: 56 return COFF::IMAGE_REL_AMD64_REL32; 57 case FK_Data_4: 58 case X86::reloc_signed_4byte: 59 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32) 60 return COFF::IMAGE_REL_AMD64_ADDR32NB; 61 return COFF::IMAGE_REL_AMD64_ADDR32; 62 case FK_Data_8: 63 return COFF::IMAGE_REL_AMD64_ADDR64; 64 case FK_SecRel_2: 65 return COFF::IMAGE_REL_AMD64_SECTION; 66 case FK_SecRel_4: 67 return COFF::IMAGE_REL_AMD64_SECREL; 68 default: 69 llvm_unreachable("unsupported relocation type"); 70 } 71 } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) { 72 switch (FixupKind) { 73 case FK_PCRel_4: 74 case X86::reloc_riprel_4byte: 75 case X86::reloc_riprel_4byte_movq_load: 76 return COFF::IMAGE_REL_I386_REL32; 77 case FK_Data_4: 78 case X86::reloc_signed_4byte: 79 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32) 80 return COFF::IMAGE_REL_I386_DIR32NB; 81 return COFF::IMAGE_REL_I386_DIR32; 82 case FK_SecRel_2: 83 return COFF::IMAGE_REL_I386_SECTION; 84 case FK_SecRel_4: 85 return COFF::IMAGE_REL_I386_SECREL; 86 default: 87 llvm_unreachable("unsupported relocation type"); 88 } 89 } else 90 llvm_unreachable("Unsupported COFF machine type."); 91} 92 93MCObjectWriter *llvm::createX86WinCOFFObjectWriter(raw_pwrite_stream &OS, 94 bool Is64Bit) { 95 MCWinCOFFObjectTargetWriter *MOTW = new X86WinCOFFObjectWriter(Is64Bit); 96 return createWinCOFFObjectWriter(MOTW, OS); 97} 98