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/MC/MCDXContainerWriter.h"
11 #include "llvm/MC/MCELFObjectWriter.h"
12 #include "llvm/MC/MCFixupKindInfo.h"
13 #include "llvm/MC/MCGOFFObjectWriter.h"
14 #include "llvm/MC/MCMachObjectWriter.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSPIRVObjectWriter.h"
17 #include "llvm/MC/MCWasmObjectWriter.h"
18 #include "llvm/MC/MCWinCOFFObjectWriter.h"
19 #include "llvm/MC/MCXCOFFObjectWriter.h"
26 MCAsmBackend::MCAsmBackend(llvm::endianness Endian
, unsigned RelaxFixupKind
)
27 : Endian(Endian
), RelaxFixupKind(RelaxFixupKind
) {}
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
)),
37 OS
, Endian
== llvm::endianness::little
);
39 return createMachObjectWriter(cast
<MCMachObjectTargetWriter
>(std::move(TW
)),
40 OS
, Endian
== llvm::endianness::little
);
42 return createWinCOFFObjectWriter(
43 cast
<MCWinCOFFObjectTargetWriter
>(std::move(TW
)), OS
);
45 return createSPIRVObjectWriter(
46 cast
<MCSPIRVObjectTargetWriter
>(std::move(TW
)), OS
);
48 return createWasmObjectWriter(cast
<MCWasmObjectTargetWriter
>(std::move(TW
)),
51 return createGOFFObjectWriter(cast
<MCGOFFObjectTargetWriter
>(std::move(TW
)),
54 return createXCOFFObjectWriter(
55 cast
<MCXCOFFObjectTargetWriter
>(std::move(TW
)), OS
);
56 case Triple::DXContainer
:
57 return createDXContainerObjectWriter(
58 cast
<MCDXContainerTargetWriter
>(std::move(TW
)), OS
);
60 llvm_unreachable("unexpected object format");
64 std::unique_ptr
<MCObjectWriter
>
65 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream
&OS
,
66 raw_pwrite_stream
&DwoOS
) const {
67 auto TW
= createObjectTargetWriter();
68 switch (TW
->getFormat()) {
70 return createWinCOFFDwoObjectWriter(
71 cast
<MCWinCOFFObjectTargetWriter
>(std::move(TW
)), OS
, DwoOS
);
73 return createELFDwoObjectWriter(
74 cast
<MCELFObjectTargetWriter
>(std::move(TW
)), OS
, DwoOS
,
75 Endian
== llvm::endianness::little
);
77 return createWasmDwoObjectWriter(
78 cast
<MCWasmObjectTargetWriter
>(std::move(TW
)), OS
, DwoOS
);
80 report_fatal_error("dwo only supported with COFF, ELF, and Wasm");
84 std::optional
<MCFixupKind
> MCAsmBackend::getFixupKind(StringRef Name
) const {
88 const MCFixupKindInfo
&MCAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
89 static const MCFixupKindInfo Builtins
[] = {
91 {"FK_Data_1", 0, 8, 0},
92 {"FK_Data_2", 0, 16, 0},
93 {"FK_Data_4", 0, 32, 0},
94 {"FK_Data_8", 0, 64, 0},
95 {"FK_Data_leb128", 0, 0, 0},
96 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel
},
97 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
98 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel
},
99 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel
},
100 {"FK_GPRel_1", 0, 8, 0},
101 {"FK_GPRel_2", 0, 16, 0},
102 {"FK_GPRel_4", 0, 32, 0},
103 {"FK_GPRel_8", 0, 64, 0},
104 {"FK_DTPRel_4", 0, 32, 0},
105 {"FK_DTPRel_8", 0, 64, 0},
106 {"FK_TPRel_4", 0, 32, 0},
107 {"FK_TPRel_8", 0, 64, 0},
108 {"FK_SecRel_1", 0, 8, 0},
109 {"FK_SecRel_2", 0, 16, 0},
110 {"FK_SecRel_4", 0, 32, 0},
111 {"FK_SecRel_8", 0, 64, 0},
114 assert((size_t)Kind
<= std::size(Builtins
) && "Unknown fixup kind");
115 return Builtins
[Kind
];
118 bool MCAsmBackend::fixupNeedsRelaxationAdvanced(
119 const MCFixup
&Fixup
, bool Resolved
, uint64_t Value
,
120 const MCRelaxableFragment
*DF
, const MCAsmLayout
&Layout
,
121 const bool WasForced
) const {
124 return fixupNeedsRelaxation(Fixup
, Value
, DF
, Layout
);
127 bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol
*Sym
) const {
128 // Consider a NULL personality (ie., no personality encoding) to be canonical
129 // because it's always at 0.
134 llvm_unreachable("Expected MachO symbols only");
136 StringRef name
= Sym
->getName();
137 // XXX: We intentionally leave out "___gcc_personality_v0" because, despite
138 // being system-defined like these two, it is not very commonly-used.
139 // Reserving an empty slot for it seems silly.
140 return name
== "___gxx_personality_v0" || name
== "___objc_personality_v0";