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
18 using namespace llvm;
19
20 namespace llvm {
21 class MCObjectWriter;
22 }
23
24 namespace {
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
X86WinCOFFObjectWriter(bool Is64Bit)36 X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
37 : MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64
38 : COFF::IMAGE_FILE_MACHINE_I386) {}
39
~X86WinCOFFObjectWriter()40 X86WinCOFFObjectWriter::~X86WinCOFFObjectWriter() {}
41
getRelocType(const MCValue & Target,const MCFixup & Fixup,bool IsCrossSection,const MCAsmBackend & MAB) const42 unsigned 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 case X86::reloc_riprel_4byte_relax:
57 case X86::reloc_riprel_4byte_relax_rex:
58 return COFF::IMAGE_REL_AMD64_REL32;
59 case FK_Data_4:
60 case X86::reloc_signed_4byte:
61 case X86::reloc_signed_4byte_relax:
62 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
63 return COFF::IMAGE_REL_AMD64_ADDR32NB;
64 if (Modifier == MCSymbolRefExpr::VK_SECREL)
65 return COFF::IMAGE_REL_AMD64_SECREL;
66 return COFF::IMAGE_REL_AMD64_ADDR32;
67 case FK_Data_8:
68 return COFF::IMAGE_REL_AMD64_ADDR64;
69 case FK_SecRel_2:
70 return COFF::IMAGE_REL_AMD64_SECTION;
71 case FK_SecRel_4:
72 return COFF::IMAGE_REL_AMD64_SECREL;
73 default:
74 llvm_unreachable("unsupported relocation type");
75 }
76 } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
77 switch (FixupKind) {
78 case FK_PCRel_4:
79 case X86::reloc_riprel_4byte:
80 case X86::reloc_riprel_4byte_movq_load:
81 return COFF::IMAGE_REL_I386_REL32;
82 case FK_Data_4:
83 case X86::reloc_signed_4byte:
84 case X86::reloc_signed_4byte_relax:
85 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
86 return COFF::IMAGE_REL_I386_DIR32NB;
87 if (Modifier == MCSymbolRefExpr::VK_SECREL)
88 return COFF::IMAGE_REL_AMD64_SECREL;
89 return COFF::IMAGE_REL_I386_DIR32;
90 case FK_SecRel_2:
91 return COFF::IMAGE_REL_I386_SECTION;
92 case FK_SecRel_4:
93 return COFF::IMAGE_REL_I386_SECREL;
94 default:
95 llvm_unreachable("unsupported relocation type");
96 }
97 } else
98 llvm_unreachable("Unsupported COFF machine type.");
99 }
100
createX86WinCOFFObjectWriter(raw_pwrite_stream & OS,bool Is64Bit)101 MCObjectWriter *llvm::createX86WinCOFFObjectWriter(raw_pwrite_stream &OS,
102 bool Is64Bit) {
103 MCWinCOFFObjectTargetWriter *MOTW = new X86WinCOFFObjectWriter(Is64Bit);
104 return createWinCOFFObjectWriter(MOTW, OS);
105 }
106