1 //===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===//
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 "llvm/MC/MCAsmBackend.h"
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCCodePadder.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCFixupKindInfo.h"
15 #include "llvm/MC/MCMachObjectWriter.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCWasmObjectWriter.h"
18 #include "llvm/MC/MCWinCOFFObjectWriter.h"
19 #include "llvm/MC/MCXCOFFObjectWriter.h"
26 MCAsmBackend::MCAsmBackend(support::endianness Endian
)
27 : CodePadder(new MCCodePadder()), Endian(Endian
) {}
29 MCAsmBackend::~MCAsmBackend() = default;
31 std::unique_ptr
<MCObjectWriter
>
32 MCAsmBackend::createObjectWriter(raw_pwrite_stream
&OS
) const {
33 auto TW
= createObjectTargetWriter();
34 switch (TW
->getFormat()) {
36 return createELFObjectWriter(cast
<MCELFObjectTargetWriter
>(std::move(TW
)), OS
,
37 Endian
== support::little
);
39 return createMachObjectWriter(cast
<MCMachObjectTargetWriter
>(std::move(TW
)),
40 OS
, Endian
== support::little
);
42 return createWinCOFFObjectWriter(
43 cast
<MCWinCOFFObjectTargetWriter
>(std::move(TW
)), OS
);
45 return createWasmObjectWriter(cast
<MCWasmObjectTargetWriter
>(std::move(TW
)),
48 return createXCOFFObjectWriter(
49 cast
<MCXCOFFObjectTargetWriter
>(std::move(TW
)), OS
);
51 llvm_unreachable("unexpected object format");
55 std::unique_ptr
<MCObjectWriter
>
56 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream
&OS
,
57 raw_pwrite_stream
&DwoOS
) const {
58 auto TW
= createObjectTargetWriter();
59 if (TW
->getFormat() != Triple::ELF
)
60 report_fatal_error("dwo only supported with ELF");
61 return createELFDwoObjectWriter(cast
<MCELFObjectTargetWriter
>(std::move(TW
)),
62 OS
, DwoOS
, Endian
== support::little
);
65 Optional
<MCFixupKind
> MCAsmBackend::getFixupKind(StringRef Name
) const {
69 const MCFixupKindInfo
&MCAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
70 static const MCFixupKindInfo Builtins
[] = {
72 {"FK_Data_1", 0, 8, 0},
73 {"FK_Data_2", 0, 16, 0},
74 {"FK_Data_4", 0, 32, 0},
75 {"FK_Data_8", 0, 64, 0},
76 {"FK_Data_6b", 0, 6, 0},
77 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel
},
78 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
79 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel
},
80 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel
},
81 {"FK_GPRel_1", 0, 8, 0},
82 {"FK_GPRel_2", 0, 16, 0},
83 {"FK_GPRel_4", 0, 32, 0},
84 {"FK_GPRel_8", 0, 64, 0},
85 {"FK_DTPRel_4", 0, 32, 0},
86 {"FK_DTPRel_8", 0, 64, 0},
87 {"FK_TPRel_4", 0, 32, 0},
88 {"FK_TPRel_8", 0, 64, 0},
89 {"FK_SecRel_1", 0, 8, 0},
90 {"FK_SecRel_2", 0, 16, 0},
91 {"FK_SecRel_4", 0, 32, 0},
92 {"FK_SecRel_8", 0, 64, 0},
93 {"FK_Data_Add_1", 0, 8, 0},
94 {"FK_Data_Add_2", 0, 16, 0},
95 {"FK_Data_Add_4", 0, 32, 0},
96 {"FK_Data_Add_8", 0, 64, 0},
97 {"FK_Data_Add_6b", 0, 6, 0},
98 {"FK_Data_Sub_1", 0, 8, 0},
99 {"FK_Data_Sub_2", 0, 16, 0},
100 {"FK_Data_Sub_4", 0, 32, 0},
101 {"FK_Data_Sub_8", 0, 64, 0},
102 {"FK_Data_Sub_6b", 0, 6, 0}};
104 assert((size_t)Kind
<= array_lengthof(Builtins
) && "Unknown fixup kind");
105 return Builtins
[Kind
];
108 bool MCAsmBackend::fixupNeedsRelaxationAdvanced(
109 const MCFixup
&Fixup
, bool Resolved
, uint64_t Value
,
110 const MCRelaxableFragment
*DF
, const MCAsmLayout
&Layout
,
111 const bool WasForced
) const {
114 return fixupNeedsRelaxation(Fixup
, Value
, DF
, Layout
);
117 void MCAsmBackend::handleCodePaddingBasicBlockStart(
118 MCObjectStreamer
*OS
, const MCCodePaddingContext
&Context
) {
119 CodePadder
->handleBasicBlockStart(OS
, Context
);
122 void MCAsmBackend::handleCodePaddingBasicBlockEnd(
123 const MCCodePaddingContext
&Context
) {
124 CodePadder
->handleBasicBlockEnd(Context
);
127 void MCAsmBackend::handleCodePaddingInstructionBegin(const MCInst
&Inst
) {
128 CodePadder
->handleInstructionBegin(Inst
);
131 void MCAsmBackend::handleCodePaddingInstructionEnd(const MCInst
&Inst
) {
132 CodePadder
->handleInstructionEnd(Inst
);
135 bool MCAsmBackend::relaxFragment(MCPaddingFragment
*PF
, MCAsmLayout
&Layout
) {
136 return CodePadder
->relaxFragment(PF
, Layout
);