xref: /freebsd/contrib/llvm-project/llvm/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp (revision 02e9120893770924227138ba49df1edb3896112a)
1 //===-- ARMWinCOFFObjectWriter.cpp - ARM Windows COFF Object Writer -- C++ -==//
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/ARMFixupKinds.h"
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/BinaryFormat/COFF.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/MC/MCWinCOFFObjectWriter.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cassert>
23 
24 using namespace llvm;
25 
26 namespace {
27 
28 class ARMWinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
29 public:
30   ARMWinCOFFObjectWriter()
31     : MCWinCOFFObjectTargetWriter(COFF::IMAGE_FILE_MACHINE_ARMNT) {
32   }
33 
34   ~ARMWinCOFFObjectWriter() override = default;
35 
36   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
37                         const MCFixup &Fixup, bool IsCrossSection,
38                         const MCAsmBackend &MAB) const override;
39 
40   bool recordRelocation(const MCFixup &) const override;
41 };
42 
43 } // end anonymous namespace
44 
45 unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext &Ctx,
46                                               const MCValue &Target,
47                                               const MCFixup &Fixup,
48                                               bool IsCrossSection,
49                                               const MCAsmBackend &MAB) const {
50   MCSymbolRefExpr::VariantKind Modifier =
51     Target.isAbsolute() ? MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
52 
53   unsigned FixupKind = Fixup.getKind();
54   if (IsCrossSection) {
55     if (FixupKind != FK_Data_4) {
56       Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
57       return COFF::IMAGE_REL_ARM_ADDR32;
58     }
59     FixupKind = FK_PCRel_4;
60   }
61 
62 
63   switch (FixupKind) {
64   default: {
65     const MCFixupKindInfo &Info = MAB.getFixupKindInfo(Fixup.getKind());
66     report_fatal_error(Twine("unsupported relocation type: ") + Info.Name);
67   }
68   case FK_Data_4:
69     switch (Modifier) {
70     case MCSymbolRefExpr::VK_COFF_IMGREL32:
71       return COFF::IMAGE_REL_ARM_ADDR32NB;
72     case MCSymbolRefExpr::VK_SECREL:
73       return COFF::IMAGE_REL_ARM_SECREL;
74     default:
75       return COFF::IMAGE_REL_ARM_ADDR32;
76     }
77   case FK_PCRel_4:
78     return COFF::IMAGE_REL_ARM_REL32;
79   case FK_SecRel_2:
80     return COFF::IMAGE_REL_ARM_SECTION;
81   case FK_SecRel_4:
82     return COFF::IMAGE_REL_ARM_SECREL;
83   case ARM::fixup_t2_condbranch:
84     return COFF::IMAGE_REL_ARM_BRANCH20T;
85   case ARM::fixup_t2_uncondbranch:
86   case ARM::fixup_arm_thumb_bl:
87     return COFF::IMAGE_REL_ARM_BRANCH24T;
88   case ARM::fixup_arm_thumb_blx:
89     return COFF::IMAGE_REL_ARM_BLX23T;
90   case ARM::fixup_t2_movw_lo16:
91   case ARM::fixup_t2_movt_hi16:
92     return COFF::IMAGE_REL_ARM_MOV32T;
93   }
94 }
95 
96 bool ARMWinCOFFObjectWriter::recordRelocation(const MCFixup &Fixup) const {
97   return static_cast<unsigned>(Fixup.getKind()) != ARM::fixup_t2_movt_hi16;
98 }
99 
100 namespace llvm {
101 
102 std::unique_ptr<MCObjectTargetWriter>
103 createARMWinCOFFObjectWriter() {
104   return std::make_unique<ARMWinCOFFObjectWriter>();
105 }
106 
107 } // end namespace llvm
108