1 //===-- ARMWinCOFFObjectWriter.cpp - ARM Windows COFF Object Writer -- C++ -==//
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
7 //===----------------------------------------------------------------------===//
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"
28 class ARMWinCOFFObjectWriter
: public MCWinCOFFObjectTargetWriter
{
30 ARMWinCOFFObjectWriter()
31 : MCWinCOFFObjectTargetWriter(COFF::IMAGE_FILE_MACHINE_ARMNT
) {
34 ~ARMWinCOFFObjectWriter() override
= default;
36 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
37 const MCFixup
&Fixup
, bool IsCrossSection
,
38 const MCAsmBackend
&MAB
) const override
;
40 bool recordRelocation(const MCFixup
&) const override
;
43 } // end anonymous namespace
45 unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext
&Ctx
,
46 const MCValue
&Target
,
49 const MCAsmBackend
&MAB
) const {
50 MCSymbolRefExpr::VariantKind Modifier
=
51 Target
.isAbsolute() ? MCSymbolRefExpr::VK_None
: Target
.getSymA()->getKind();
53 unsigned FixupKind
= Fixup
.getKind();
55 if (FixupKind
!= FK_Data_4
) {
56 Ctx
.reportError(Fixup
.getLoc(), "Cannot represent this expression");
57 return COFF::IMAGE_REL_ARM_ADDR32
;
59 FixupKind
= FK_PCRel_4
;
65 const MCFixupKindInfo
&Info
= MAB
.getFixupKindInfo(Fixup
.getKind());
66 report_fatal_error(Twine("unsupported relocation type: ") + Info
.Name
);
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
;
75 return COFF::IMAGE_REL_ARM_ADDR32
;
78 return COFF::IMAGE_REL_ARM_REL32
;
80 return COFF::IMAGE_REL_ARM_SECTION
;
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
;
96 bool ARMWinCOFFObjectWriter::recordRelocation(const MCFixup
&Fixup
) const {
97 return static_cast<unsigned>(Fixup
.getKind()) != ARM::fixup_t2_movt_hi16
;
102 std::unique_ptr
<MCObjectTargetWriter
>
103 createARMWinCOFFObjectWriter() {
104 return std::make_unique
<ARMWinCOFFObjectWriter
>();
107 } // end namespace llvm