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/MCELFObjectWriter.h"
13 #include "llvm/MC/MCFixupKindInfo.h"
14 #include "llvm/MC/MCMachObjectWriter.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCWasmObjectWriter.h"
17 #include "llvm/MC/MCWinCOFFObjectWriter.h"
18 #include "llvm/MC/MCXCOFFObjectWriter.h"
25 MCAsmBackend::MCAsmBackend(support::endianness Endian
) : Endian(Endian
) {}
27 MCAsmBackend::~MCAsmBackend() = default;
29 std::unique_ptr
<MCObjectWriter
>
30 MCAsmBackend::createObjectWriter(raw_pwrite_stream
&OS
) const {
31 auto TW
= createObjectTargetWriter();
32 switch (TW
->getFormat()) {
34 return createELFObjectWriter(cast
<MCELFObjectTargetWriter
>(std::move(TW
)), OS
,
35 Endian
== support::little
);
37 return createMachObjectWriter(cast
<MCMachObjectTargetWriter
>(std::move(TW
)),
38 OS
, Endian
== support::little
);
40 return createWinCOFFObjectWriter(
41 cast
<MCWinCOFFObjectTargetWriter
>(std::move(TW
)), OS
);
43 return createWasmObjectWriter(cast
<MCWasmObjectTargetWriter
>(std::move(TW
)),
46 return createXCOFFObjectWriter(
47 cast
<MCXCOFFObjectTargetWriter
>(std::move(TW
)), OS
);
49 llvm_unreachable("unexpected object format");
53 std::unique_ptr
<MCObjectWriter
>
54 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream
&OS
,
55 raw_pwrite_stream
&DwoOS
) const {
56 auto TW
= createObjectTargetWriter();
57 switch (TW
->getFormat()) {
59 return createELFDwoObjectWriter(
60 cast
<MCELFObjectTargetWriter
>(std::move(TW
)), OS
, DwoOS
,
61 Endian
== support::little
);
63 return createWasmDwoObjectWriter(
64 cast
<MCWasmObjectTargetWriter
>(std::move(TW
)), OS
, DwoOS
);
66 report_fatal_error("dwo only supported with ELF and Wasm");
70 Optional
<MCFixupKind
> MCAsmBackend::getFixupKind(StringRef Name
) const {
74 const MCFixupKindInfo
&MCAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
75 static const MCFixupKindInfo Builtins
[] = {
77 {"FK_Data_1", 0, 8, 0},
78 {"FK_Data_2", 0, 16, 0},
79 {"FK_Data_4", 0, 32, 0},
80 {"FK_Data_8", 0, 64, 0},
81 {"FK_Data_6b", 0, 6, 0},
82 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel
},
83 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
84 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel
},
85 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel
},
86 {"FK_GPRel_1", 0, 8, 0},
87 {"FK_GPRel_2", 0, 16, 0},
88 {"FK_GPRel_4", 0, 32, 0},
89 {"FK_GPRel_8", 0, 64, 0},
90 {"FK_DTPRel_4", 0, 32, 0},
91 {"FK_DTPRel_8", 0, 64, 0},
92 {"FK_TPRel_4", 0, 32, 0},
93 {"FK_TPRel_8", 0, 64, 0},
94 {"FK_SecRel_1", 0, 8, 0},
95 {"FK_SecRel_2", 0, 16, 0},
96 {"FK_SecRel_4", 0, 32, 0},
97 {"FK_SecRel_8", 0, 64, 0},
100 assert((size_t)Kind
<= array_lengthof(Builtins
) && "Unknown fixup kind");
101 return Builtins
[Kind
];
104 bool MCAsmBackend::fixupNeedsRelaxationAdvanced(
105 const MCFixup
&Fixup
, bool Resolved
, uint64_t Value
,
106 const MCRelaxableFragment
*DF
, const MCAsmLayout
&Layout
,
107 const bool WasForced
) const {
110 return fixupNeedsRelaxation(Fixup
, Value
, DF
, Layout
);