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/BinaryFormat/COFF.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/MC/MCWinCOFFObjectWriter.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
26 public:
27 X86WinCOFFObjectWriter(bool Is64Bit);
28 ~X86WinCOFFObjectWriter() override = default;
29
30 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
31 const MCFixup &Fixup, bool IsCrossSection,
32 const MCAsmBackend &MAB) const override;
33 };
34
35 } // end anonymous namespace
36
X86WinCOFFObjectWriter(bool Is64Bit)37 X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
38 : MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64
39 : COFF::IMAGE_FILE_MACHINE_I386) {}
40
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsCrossSection,const MCAsmBackend & MAB) const41 unsigned X86WinCOFFObjectWriter::getRelocType(MCContext &Ctx,
42 const MCValue &Target,
43 const MCFixup &Fixup,
44 bool IsCrossSection,
45 const MCAsmBackend &MAB) const {
46 unsigned FixupKind = Fixup.getKind();
47 if (IsCrossSection) {
48 if (FixupKind != FK_Data_4 && FixupKind != llvm::X86::reloc_signed_4byte) {
49 Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
50 return COFF::IMAGE_REL_AMD64_ADDR32;
51 }
52 FixupKind = FK_PCRel_4;
53 }
54
55 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
56 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
57
58 if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
59 switch (FixupKind) {
60 case FK_PCRel_4:
61 case X86::reloc_riprel_4byte:
62 case X86::reloc_riprel_4byte_movq_load:
63 case X86::reloc_riprel_4byte_relax:
64 case X86::reloc_riprel_4byte_relax_rex:
65 case X86::reloc_branch_4byte_pcrel:
66 return COFF::IMAGE_REL_AMD64_REL32;
67 case FK_Data_4:
68 case X86::reloc_signed_4byte:
69 case X86::reloc_signed_4byte_relax:
70 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
71 return COFF::IMAGE_REL_AMD64_ADDR32NB;
72 if (Modifier == MCSymbolRefExpr::VK_SECREL)
73 return COFF::IMAGE_REL_AMD64_SECREL;
74 return COFF::IMAGE_REL_AMD64_ADDR32;
75 case FK_Data_8:
76 return COFF::IMAGE_REL_AMD64_ADDR64;
77 case FK_SecRel_2:
78 return COFF::IMAGE_REL_AMD64_SECTION;
79 case FK_SecRel_4:
80 return COFF::IMAGE_REL_AMD64_SECREL;
81 default:
82 llvm_unreachable("unsupported relocation type");
83 }
84 } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
85 switch (FixupKind) {
86 case FK_PCRel_4:
87 case X86::reloc_riprel_4byte:
88 case X86::reloc_riprel_4byte_movq_load:
89 return COFF::IMAGE_REL_I386_REL32;
90 case FK_Data_4:
91 case X86::reloc_signed_4byte:
92 case X86::reloc_signed_4byte_relax:
93 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
94 return COFF::IMAGE_REL_I386_DIR32NB;
95 if (Modifier == MCSymbolRefExpr::VK_SECREL)
96 return COFF::IMAGE_REL_AMD64_SECREL;
97 return COFF::IMAGE_REL_I386_DIR32;
98 case FK_SecRel_2:
99 return COFF::IMAGE_REL_I386_SECTION;
100 case FK_SecRel_4:
101 return COFF::IMAGE_REL_I386_SECREL;
102 default:
103 llvm_unreachable("unsupported relocation type");
104 }
105 } else
106 llvm_unreachable("Unsupported COFF machine type.");
107 }
108
109 std::unique_ptr<MCObjectTargetWriter>
createX86WinCOFFObjectWriter(bool Is64Bit)110 llvm::createX86WinCOFFObjectWriter(bool Is64Bit) {
111 return llvm::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
112 }
113