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/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/MC/MCWinCOFFObjectWriter.h"
19 #include "llvm/Support/ErrorHandling.h"
25 class ARMWinCOFFObjectWriter
: public MCWinCOFFObjectTargetWriter
{
27 ARMWinCOFFObjectWriter()
28 : MCWinCOFFObjectTargetWriter(COFF::IMAGE_FILE_MACHINE_ARMNT
) {
31 ~ARMWinCOFFObjectWriter() override
= default;
33 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
34 const MCFixup
&Fixup
, bool IsCrossSection
,
35 const MCAsmBackend
&MAB
) const override
;
37 bool recordRelocation(const MCFixup
&) const override
;
40 } // end anonymous namespace
42 unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext
&Ctx
,
43 const MCValue
&Target
,
46 const MCAsmBackend
&MAB
) const {
47 MCSymbolRefExpr::VariantKind Modifier
=
48 Target
.isAbsolute() ? MCSymbolRefExpr::VK_None
: Target
.getSymA()->getKind();
50 unsigned FixupKind
= Fixup
.getKind();
52 if (FixupKind
!= FK_Data_4
) {
53 Ctx
.reportError(Fixup
.getLoc(), "Cannot represent this expression");
54 return COFF::IMAGE_REL_ARM_ADDR32
;
56 FixupKind
= FK_PCRel_4
;
62 Ctx
.reportError(Fixup
.getLoc(), "unsupported relocation type");
63 return COFF::IMAGE_REL_ARM_ABSOLUTE
;
67 case MCSymbolRefExpr::VK_COFF_IMGREL32
:
68 return COFF::IMAGE_REL_ARM_ADDR32NB
;
69 case MCSymbolRefExpr::VK_SECREL
:
70 return COFF::IMAGE_REL_ARM_SECREL
;
72 return COFF::IMAGE_REL_ARM_ADDR32
;
75 return COFF::IMAGE_REL_ARM_REL32
;
77 return COFF::IMAGE_REL_ARM_SECTION
;
79 return COFF::IMAGE_REL_ARM_SECREL
;
80 case ARM::fixup_t2_condbranch
:
81 return COFF::IMAGE_REL_ARM_BRANCH20T
;
82 case ARM::fixup_t2_uncondbranch
:
83 case ARM::fixup_arm_thumb_bl
:
84 return COFF::IMAGE_REL_ARM_BRANCH24T
;
85 case ARM::fixup_arm_thumb_blx
:
86 return COFF::IMAGE_REL_ARM_BLX23T
;
87 case ARM::fixup_t2_movw_lo16
:
88 case ARM::fixup_t2_movt_hi16
:
89 return COFF::IMAGE_REL_ARM_MOV32T
;
93 bool ARMWinCOFFObjectWriter::recordRelocation(const MCFixup
&Fixup
) const {
94 return static_cast<unsigned>(Fixup
.getKind()) != ARM::fixup_t2_movt_hi16
;
99 std::unique_ptr
<MCObjectTargetWriter
>
100 createARMWinCOFFObjectWriter() {
101 return std::make_unique
<ARMWinCOFFObjectWriter
>();
104 } // end namespace llvm