LLVM 19.0.0git
MCAsmBackend.cpp
Go to the documentation of this file.
1//===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
20#include <cassert>
21#include <cstddef>
22#include <cstdint>
23
24using namespace llvm;
25
26MCAsmBackend::MCAsmBackend(llvm::endianness Endian, unsigned RelaxFixupKind)
27 : Endian(Endian), RelaxFixupKind(RelaxFixupKind) {}
28
30
31std::unique_ptr<MCObjectWriter>
33 auto TW = createObjectTargetWriter();
34 switch (TW->getFormat()) {
35 case Triple::ELF:
36 return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)),
38 case Triple::MachO:
39 return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)),
41 case Triple::COFF:
43 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
44 case Triple::SPIRV:
46 cast<MCSPIRVObjectTargetWriter>(std::move(TW)), OS);
47 case Triple::Wasm:
48 return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)),
49 OS);
50 case Triple::GOFF:
51 return createGOFFObjectWriter(cast<MCGOFFObjectTargetWriter>(std::move(TW)),
52 OS);
53 case Triple::XCOFF:
55 cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
58 cast<MCDXContainerTargetWriter>(std::move(TW)), OS);
59 default:
60 llvm_unreachable("unexpected object format");
61 }
62}
63
64std::unique_ptr<MCObjectWriter>
66 raw_pwrite_stream &DwoOS) const {
67 auto TW = createObjectTargetWriter();
68 switch (TW->getFormat()) {
69 case Triple::COFF:
71 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS, DwoOS);
72 case Triple::ELF:
74 cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS,
76 case Triple::Wasm:
78 cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS);
79 default:
80 report_fatal_error("dwo only supported with COFF, ELF, and Wasm");
81 }
82}
83
84std::optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const {
85 return std::nullopt;
86}
87
89 static const MCFixupKindInfo Builtins[] = {
90 {"FK_NONE", 0, 0, 0},
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},
112 };
113
114 assert((size_t)Kind <= std::size(Builtins) && "Unknown fixup kind");
115 return Builtins[Kind];
116}
117
119 const MCFixup &Fixup, bool Resolved, uint64_t Value,
120 const MCRelaxableFragment *DF, const MCAsmLayout &Layout,
121 const bool WasForced) const {
122 if (!Resolved)
123 return true;
124 return fixupNeedsRelaxation(Fixup, Value, DF, Layout);
125}
126
128 // Consider a NULL personality (ie., no personality encoding) to be canonical
129 // because it's always at 0.
130 if (!Sym)
131 return true;
132
133 if (!Sym->isMachO())
134 llvm_unreachable("Expected MachO symbols only");
135
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";
141}
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
endianness Endian
raw_pwrite_stream & OS
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file.
std::unique_ptr< MCObjectWriter > createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const
Create an MCObjectWriter that writes two object files: a .o file which is linked into the final progr...
const llvm::endianness Endian
Definition: MCAsmBackend.h:52
MCAsmBackend(llvm::endianness Endian, unsigned RelaxFixupKind=MaxFixupKind)
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
virtual ~MCAsmBackend()
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:274
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
@ DXContainer
Definition: Triple.h:289
LLVM Value Representation.
Definition: Value.h:74
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:444
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectWriter > createGOFFObjectWriter(std::unique_ptr< MCGOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new GOFF writer instance.
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
std::unique_ptr< MCObjectWriter > createMachObjectWriter(std::unique_ptr< MCMachObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
std::unique_ptr< MCObjectWriter > createDXContainerObjectWriter(std::unique_ptr< MCDXContainerTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new DXContainer writer instance.
std::unique_ptr< MCObjectWriter > createELFDwoObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, bool IsLittleEndian)
std::unique_ptr< MCObjectWriter > createWasmObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
std::unique_ptr< MCObjectWriter > createWinCOFFDwoObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
std::unique_ptr< MCObjectWriter > createSPIRVObjectWriter(std::unique_ptr< MCSPIRVObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new SPIR-V writer instance.
endianness
Definition: bit.h:70
std::unique_ptr< MCObjectWriter > createXCOFFObjectWriter(std::unique_ptr< MCXCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
std::unique_ptr< MCObjectWriter > createWasmDwoObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
Target independent information on a fixup kind.
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...