LLVM 22.0.0git
PPCAsmBackend.cpp
Go to the documentation of this file.
1//===-- PPCAsmBackend.cpp - PPC Assembler 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
15#include "llvm/MC/MCAssembler.h"
16#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCSymbolELF.h"
23#include "llvm/MC/MCValue.h"
26using namespace llvm;
27
28static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
29 switch (Kind) {
30 default:
31 llvm_unreachable("Unknown fixup kind!");
32 case FK_Data_1:
33 case FK_Data_2:
34 case FK_Data_4:
35 case FK_Data_8:
37 return Value;
40 return Value & 0xfffc;
44 return Value & 0x3fffffc;
46 return Value & 0xffff;
49 return Value & 0xfffc;
52 return Value & 0x3ffffffff;
53 }
54}
55
56static unsigned getFixupKindNumBytes(unsigned Kind) {
57 switch (Kind) {
58 default:
59 llvm_unreachable("Unknown fixup kind!");
60 case FK_Data_1:
61 return 1;
62 case FK_Data_2:
66 return 2;
67 case FK_Data_4:
73 return 4;
76 case FK_Data_8:
77 return 8;
79 return 0;
80 }
81}
82
83namespace {
84
85class PPCAsmBackend : public MCAsmBackend {
86protected:
87 Triple TT;
88public:
89 PPCAsmBackend(const Target &T, const Triple &TT)
90 : MCAsmBackend(TT.isLittleEndian() ? llvm::endianness::little
91 : llvm::endianness::big),
92 TT(TT) {}
93
94 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
95
96 void applyFixup(const MCFragment &, const MCFixup &Fixup,
97 const MCValue &Target, uint8_t *Data, uint64_t Value,
98 bool IsResolved) override;
99
100 bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
101 // If there is a @ specifier, unless it is optimized out (e.g. constant @l),
102 // force a relocation.
103 if (Target.getSpecifier())
104 return true;
105 MCFixupKind Kind = Fixup.getKind();
106 switch ((unsigned)Kind) {
107 default:
108 return false;
112 // If the target symbol has a local entry point we must not attempt
113 // to resolve the fixup directly. Emit a relocation and leave
114 // resolution of the final target address to the linker.
115 if (const auto *A = Target.getAddSym()) {
116 if (getContext().isELF()) {
117 // The "other" values are stored in the last 6 bits of the second
118 // byte. The traditional defines for STO values assume the full byte
119 // and thus the shift to pack it.
120 unsigned Other = static_cast<const MCSymbolELF *>(A)->getOther() << 2;
121 if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
122 return true;
123 } else if (getContext().isXCOFF()) {
124 auto *S = static_cast<const MCSymbolXCOFF *>(A);
125 return !Target.isAbsolute() && S->isExternal() &&
126 S->getStorageClass() == XCOFF::C_WEAKEXT;
127 }
128 }
129 return false;
130 }
131 }
132
133 bool writeNopData(raw_ostream &OS, uint64_t Count,
134 const MCSubtargetInfo *STI) const override {
135 uint64_t NumNops = Count / 4;
136 for (uint64_t i = 0; i != NumNops; ++i)
137 support::endian::write<uint32_t>(OS, 0x60000000, Endian);
138
139 OS.write_zeros(Count % 4);
140
141 return true;
142 }
143};
144} // end anonymous namespace
145
146MCFixupKindInfo PPCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
147 // clang-format off
148 const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
149 // name offset bits flags
150 {"fixup_ppc_br24", 6, 24, 0},
151 {"fixup_ppc_br24_notoc", 6, 24, 0},
152 {"fixup_ppc_brcond14", 16, 14, 0},
153 {"fixup_ppc_br24abs", 6, 24, 0},
154 {"fixup_ppc_brcond14abs", 16, 14, 0},
155 {"fixup_ppc_half16", 0, 16, 0},
156 {"fixup_ppc_half16ds", 0, 14, 0},
157 {"fixup_ppc_pcrel34", 0, 34, 0},
158 {"fixup_ppc_imm34", 0, 34, 0},
159 {"fixup_ppc_nofixup", 0, 0, 0}};
160 const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
161 // name offset bits flags
162 {"fixup_ppc_br24", 2, 24, 0},
163 {"fixup_ppc_br24_notoc", 2, 24, 0},
164 {"fixup_ppc_brcond14", 2, 14, 0},
165 {"fixup_ppc_br24abs", 2, 24, 0},
166 {"fixup_ppc_brcond14abs", 2, 14, 0},
167 {"fixup_ppc_half16", 0, 16, 0},
168 {"fixup_ppc_half16ds", 2, 14, 0},
169 {"fixup_ppc_pcrel34", 0, 34, 0},
170 {"fixup_ppc_imm34", 0, 34, 0},
171 {"fixup_ppc_nofixup", 0, 0, 0}};
172 // clang-format on
173
174 // Fixup kinds from .reloc directive are like R_PPC_NONE/R_PPC64_NONE. They
175 // do not require any extra processing.
176 if (mc::isRelocation(Kind))
177 return {};
178
179 if (Kind < FirstTargetFixupKind)
181
183 "Invalid kind!");
184 return (Endian == llvm::endianness::little
185 ? InfosLE
186 : InfosBE)[Kind - FirstTargetFixupKind];
187}
188
189void PPCAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
190 const MCValue &TargetVal, uint8_t *Data,
191 uint64_t Value, bool IsResolved) {
192 // In PPC64 ELFv1, .quad .TOC.@tocbase in the .opd section is expected to
193 // reference the null symbol.
194 auto Target = TargetVal;
195 if (Target.getSpecifier() == PPC::S_TOCBASE)
196 Target.setAddSym(nullptr);
197 if (IsResolved && shouldForceRelocation(Fixup, Target))
198 IsResolved = false;
199 if (!IsResolved)
200 Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
201
202 MCFixupKind Kind = Fixup.getKind();
203 if (mc::isRelocation(Kind))
204 return;
206 if (!Value)
207 return; // Doesn't change encoding.
208
209 unsigned NumBytes = getFixupKindNumBytes(Kind);
210
211 // For each byte of the fragment that the fixup touches, mask in the bits
212 // from the fixup value. The Value has been "split up" into the appropriate
213 // bitfields above.
214 for (unsigned i = 0; i != NumBytes; ++i) {
215 unsigned Idx = Endian == llvm::endianness::little ? i : (NumBytes - 1 - i);
216 Data[i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
217 }
218}
219
220// FIXME: This should be in a separate file.
221namespace {
222
223class ELFPPCAsmBackend : public PPCAsmBackend {
224public:
225 ELFPPCAsmBackend(const Target &T, const Triple &TT) : PPCAsmBackend(T, TT) {}
226
227 std::unique_ptr<MCObjectTargetWriter>
228 createObjectTargetWriter() const override {
230 bool Is64 = TT.isPPC64();
231 return createPPCELFObjectWriter(Is64, OSABI);
232 }
233
234 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
235};
236
237class XCOFFPPCAsmBackend : public PPCAsmBackend {
238public:
239 XCOFFPPCAsmBackend(const Target &T, const Triple &TT)
240 : PPCAsmBackend(T, TT) {}
241
242 std::unique_ptr<MCObjectTargetWriter>
243 createObjectTargetWriter() const override {
244 return createPPCXCOFFObjectWriter(TT.isArch64Bit());
245 }
246};
247
248} // end anonymous namespace
249
250std::optional<MCFixupKind>
251ELFPPCAsmBackend::getFixupKind(StringRef Name) const {
252 if (TT.isOSBinFormatELF()) {
253 unsigned Type;
254 if (TT.isPPC64()) {
256#define ELF_RELOC(X, Y) .Case(#X, Y)
257#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
258#undef ELF_RELOC
259 .Case("BFD_RELOC_NONE", ELF::R_PPC64_NONE)
260 .Case("BFD_RELOC_16", ELF::R_PPC64_ADDR16)
261 .Case("BFD_RELOC_32", ELF::R_PPC64_ADDR32)
262 .Case("BFD_RELOC_64", ELF::R_PPC64_ADDR64)
263 .Default(-1u);
264 } else {
266#define ELF_RELOC(X, Y) .Case(#X, Y)
267#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
268#undef ELF_RELOC
269 .Case("BFD_RELOC_NONE", ELF::R_PPC_NONE)
270 .Case("BFD_RELOC_16", ELF::R_PPC_ADDR16)
271 .Case("BFD_RELOC_32", ELF::R_PPC_ADDR32)
272 .Default(-1u);
273 }
274 if (Type != -1u)
275 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
276 }
277 return std::nullopt;
278}
279
281 const MCSubtargetInfo &STI,
282 const MCRegisterInfo &MRI,
283 const MCTargetOptions &Options) {
284 const Triple &TT = STI.getTargetTriple();
285 if (TT.isOSBinFormatXCOFF())
286 return new XCOFFPPCAsmBackend(T, TT);
287
288 return new ELFPPCAsmBackend(T, TT);
289}
unsigned const MachineRegisterInfo * MRI
static bool shouldForceRelocation(const MCFixup &Fixup)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1328
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value)
static unsigned getFixupKindNumBytes(unsigned Kind)
PowerPC TLS Dynamic Call Fixup
endianness Endian
raw_pwrite_stream & OS
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:55
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
MCContext & getContext() const
virtual void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved)=0
bool isXCOFF() const
Definition: MCContext.h:395
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:61
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
R Default(T Value)
Definition: StringSwitch.h:177
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ STO_PPC64_LOCAL_MASK
Definition: ELF.h:424
@ fixup_ppc_pcrel34
Definition: PPCFixupKinds.h:44
@ fixup_ppc_brcond14abs
14-bit absolute relocation for conditional branches.
Definition: PPCFixupKinds.h:33
@ fixup_ppc_half16
A 16-bit fixup corresponding to lo16(_foo) or ha16(_foo) for instrs like 'li' or 'addis'.
Definition: PPCFixupKinds.h:37
@ NumTargetFixupKinds
Definition: PPCFixupKinds.h:60
@ fixup_ppc_br24_notoc
Definition: PPCFixupKinds.h:24
@ fixup_ppc_brcond14
14-bit PC relative relocation for conditional branches.
Definition: PPCFixupKinds.h:27
@ fixup_ppc_half16dq
A 16-bit fixup corresponding to lo16(_foo) with implied 3 zero bits for instrs like 'lxv'.
Definition: PPCFixupKinds.h:56
@ fixup_ppc_half16ds
A 14-bit fixup corresponding to lo16(_foo) with implied 2 zero bits for instrs like 'std'.
Definition: PPCFixupKinds.h:41
@ fixup_ppc_nofixup
Not a true fixup, but ties a symbol to a call to __tls_get_addr for the TLS general and local dynamic...
Definition: PPCFixupKinds.h:52
@ fixup_ppc_br24abs
24-bit absolute relocation for direct branches like 'ba' and 'bla'.
Definition: PPCFixupKinds.h:30
VE::Fixups getFixupKind(uint8_t S)
Definition: VEMCAsmInfo.cpp:38
@ C_WEAKEXT
Definition: XCOFF.h:200
bool isRelocation(MCFixupKind FixupKind)
Definition: MCFixup.h:130
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ FirstTargetFixupKind
Definition: MCFixup.h:44
@ FirstLiteralRelocationKind
Definition: MCFixup.h:29
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:36
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:35
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
std::unique_ptr< MCObjectTargetWriter > createPPCXCOFFObjectWriter(bool Is64Bit)
Construct a PPC XCOFF object writer.
std::unique_ptr< MCObjectTargetWriter > createPPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
Construct an PPC ELF object writer.
endianness
Definition: bit.h:71
Target independent information on a fixup kind.
Definition: MCAsmBackend.h:38