xref: /freebsd/contrib/llvm-project/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 //===-- X86WinCOFFObjectWriter.cpp - X86 Win COFF Writer ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MCTargetDesc/X86FixupKinds.h"
10 #include "MCTargetDesc/X86MCTargetDesc.h"
11 #include "llvm/BinaryFormat/COFF.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCFixup.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/MC/MCWinCOFFObjectWriter.h"
18 #include "llvm/Support/ErrorHandling.h"
19 
20 using namespace llvm;
21 
22 namespace {
23 
24 class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
25 public:
26   X86WinCOFFObjectWriter(bool Is64Bit);
27   ~X86WinCOFFObjectWriter() override = default;
28 
29   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30                         const MCFixup &Fixup, bool IsCrossSection,
31                         const MCAsmBackend &MAB) const override;
32 };
33 
34 } // end anonymous namespace
35 
36 X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
37     : MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64
38                                           : COFF::IMAGE_FILE_MACHINE_I386) {}
39 
40 unsigned X86WinCOFFObjectWriter::getRelocType(MCContext &Ctx,
41                                               const MCValue &Target,
42                                               const MCFixup &Fixup,
43                                               bool IsCrossSection,
44                                               const MCAsmBackend &MAB) const {
45   unsigned FixupKind = Fixup.getKind();
46   if (IsCrossSection) {
47     if (FixupKind != FK_Data_4 && FixupKind != llvm::X86::reloc_signed_4byte) {
48       Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
49       return COFF::IMAGE_REL_AMD64_ADDR32;
50     }
51     FixupKind = FK_PCRel_4;
52   }
53 
54   MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
55     MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
56 
57   if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
58     switch (FixupKind) {
59     case FK_PCRel_4:
60     case X86::reloc_riprel_4byte:
61     case X86::reloc_riprel_4byte_movq_load:
62     case X86::reloc_riprel_4byte_relax:
63     case X86::reloc_riprel_4byte_relax_rex:
64     case X86::reloc_branch_4byte_pcrel:
65       return COFF::IMAGE_REL_AMD64_REL32;
66     case FK_Data_4:
67     case X86::reloc_signed_4byte:
68     case X86::reloc_signed_4byte_relax:
69       if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
70         return COFF::IMAGE_REL_AMD64_ADDR32NB;
71       if (Modifier == MCSymbolRefExpr::VK_SECREL)
72         return COFF::IMAGE_REL_AMD64_SECREL;
73       return COFF::IMAGE_REL_AMD64_ADDR32;
74     case FK_Data_8:
75       return COFF::IMAGE_REL_AMD64_ADDR64;
76     case FK_SecRel_2:
77       return COFF::IMAGE_REL_AMD64_SECTION;
78     case FK_SecRel_4:
79       return COFF::IMAGE_REL_AMD64_SECREL;
80     default:
81       Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
82       return COFF::IMAGE_REL_AMD64_ADDR32;
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       Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
104       return COFF::IMAGE_REL_I386_DIR32;
105     }
106   } else
107     llvm_unreachable("Unsupported COFF machine type.");
108 }
109 
110 std::unique_ptr<MCObjectTargetWriter>
111 llvm::createX86WinCOFFObjectWriter(bool Is64Bit) {
112   return std::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
113 }
114