LLVM 22.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
10#include "llvm/MC/MCAssembler.h"
11#include "llvm/MC/MCContext.h"
21#include <cassert>
22#include <cstddef>
23#include <cstdint>
24
25using namespace llvm;
26
28
29MCContext &MCAsmBackend::getContext() const { return Asm->getContext(); }
30
31std::unique_ptr<MCObjectWriter>
33 auto TW = createObjectTargetWriter();
34 bool IsLE = Endian == llvm::endianness::little;
35 switch (TW->getFormat()) {
36 case Triple::MachO:
37 return std::make_unique<MachObjectWriter>(
38 cast<MCMachObjectTargetWriter>(std::move(TW)), OS, IsLE);
39 case Triple::COFF:
41 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
42 case Triple::ELF:
43 return std::make_unique<ELFObjectWriter>(
44 cast<MCELFObjectTargetWriter>(std::move(TW)), OS, IsLE);
45 case Triple::SPIRV:
47 cast<MCSPIRVObjectTargetWriter>(std::move(TW)), OS);
48 case Triple::Wasm:
50 OS);
51 case Triple::GOFF:
53 OS);
54 case Triple::XCOFF:
56 cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
58 return std::make_unique<DXContainerObjectWriter>(
59 cast<MCDXContainerTargetWriter>(std::move(TW)), OS);
60 default:
61 llvm_unreachable("unexpected object format");
62 }
63}
64
65std::unique_ptr<MCObjectWriter>
67 raw_pwrite_stream &DwoOS) const {
68 auto TW = createObjectTargetWriter();
69 switch (TW->getFormat()) {
70 case Triple::COFF:
72 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS, DwoOS);
73 case Triple::ELF:
74 return std::make_unique<ELFObjectWriter>(
75 cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS,
77 case Triple::Wasm:
79 cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS);
80 default:
81 report_fatal_error("dwo only supported with COFF, ELF, and Wasm");
82 }
83}
84
85std::optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const {
86 return std::nullopt;
87}
88
90 // clang-format off
91 static const MCFixupKindInfo Builtins[] = {
92 {"FK_NONE", 0, 0, 0},
93 {"FK_Data_1", 0, 8, 0},
94 {"FK_Data_2", 0, 16, 0},
95 {"FK_Data_4", 0, 32, 0},
96 {"FK_Data_8", 0, 64, 0},
97 {"FK_Data_leb128", 0, 0, 0},
98 {"FK_SecRel_1", 0, 8, 0},
99 {"FK_SecRel_2", 0, 16, 0},
100 {"FK_SecRel_4", 0, 32, 0},
101 {"FK_SecRel_8", 0, 64, 0},
102 };
103 // clang-format on
104
105 assert(size_t(Kind - FK_NONE) < std::size(Builtins) && "Unknown fixup kind");
106 return Builtins[Kind - FK_NONE];
107}
108
110 const MCFixup &Fixup,
111 const MCValue &, uint64_t Value,
112 bool Resolved) const {
113 if (!Resolved)
114 return true;
116}
117
119 const MCValue &Target, uint64_t &Value,
120 bool IsResolved) {
121 if (!IsResolved)
122 Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
123}
124
126 assert(getContext().isMachO());
127 // Consider a NULL personality (ie., no personality encoding) to be canonical
128 // because it's always at 0.
129 if (!Sym)
130 return true;
131
132 StringRef name = Sym->getName();
133 // XXX: We intentionally leave out "___gcc_personality_v0" because, despite
134 // being system-defined like these two, it is not very commonly-used.
135 // Reserving an empty slot for it seems silly.
136 return name == "___gxx_personality_v0" || name == "___objc_personality_v0";
137}
138
140 const MCSubtargetInfo *STI = nullptr;
141 STI = F.getSubtargetInfo();
142 assert(!F.hasInstructions() || STI != nullptr);
143 return STI;
144}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:55
PowerPC TLS Dynamic Call Fixup
static const char * name
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
static const MCSubtargetInfo * getSubtargetInfo(const MCFragment &F)
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value) const
Simple predicate for targets where !Resolved implies requiring relaxation.
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
MCAssembler * Asm
virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &, const MCValue &, uint64_t, bool Resolved) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
MCContext & getContext() const
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &, uint64_t &Value, bool IsResolved)
virtual ~MCAsmBackend()
Context object for machine code objects.
Definition MCContext.h:83
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
An abstract base class for streams implementations that also support a pwrite operation.
#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.
std::unique_ptr< MCObjectWriter > createGOFFObjectWriter(std::unique_ptr< MCGOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new GOFF writer instance.
std::unique_ptr< MCObjectWriter > createWasmObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
std::unique_ptr< MCObjectWriter > createWinCOFFDwoObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
@ FK_NONE
A no-op fixup.
Definition MCFixup.h:33
std::unique_ptr< MCObjectWriter > createSPIRVObjectWriter(std::unique_ptr< MCSPIRVObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new SPIR-V writer instance.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
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.