LLVM 19.0.0git
BPFAsmBackend.cpp
Go to the documentation of this file.
1//===-- BPFAsmBackend.cpp - BPF 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
11#include "llvm/ADT/StringRef.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCFixup.h"
19#include <cassert>
20#include <cstdint>
21
22using namespace llvm;
23
24namespace {
25
26class BPFAsmBackend : public MCAsmBackend {
27public:
28 BPFAsmBackend(llvm::endianness Endian) : MCAsmBackend(Endian) {}
29 ~BPFAsmBackend() override = default;
30
31 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
33 uint64_t Value, bool IsResolved,
34 const MCSubtargetInfo *STI) const override;
35
36 std::unique_ptr<MCObjectTargetWriter>
37 createObjectTargetWriter() const override;
38
39 // No instruction requires relaxation
42 const MCAsmLayout &Layout) const override {
43 return false;
44 }
45
46 unsigned getNumFixupKinds() const override {
48 }
49 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
50
52 const MCSubtargetInfo *STI) const override;
53};
54
55} // end anonymous namespace
56
57const MCFixupKindInfo &
58BPFAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
59 const static MCFixupKindInfo Infos[BPF::NumTargetFixupKinds] = {
60 { "FK_BPF_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
61 };
62
63 if (Kind < FirstTargetFixupKind)
65
66 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
67 "Invalid kind!");
68 return Infos[Kind - FirstTargetFixupKind];
69}
70
71bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
72 const MCSubtargetInfo *STI) const {
73 if ((Count % 8) != 0)
74 return false;
75
76 for (uint64_t i = 0; i < Count; i += 8)
77 support::endian::write<uint64_t>(OS, 0x15000000, Endian);
78
79 return true;
80}
81
82void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
83 const MCValue &Target,
85 bool IsResolved,
86 const MCSubtargetInfo *STI) const {
87 if (Fixup.getKind() == FK_SecRel_8) {
88 // The Value is 0 for global variables, and the in-section offset
89 // for static variables. Write to the immediate field of the inst.
90 assert(Value <= UINT32_MAX);
91 support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4],
92 static_cast<uint32_t>(Value),
93 Endian);
94 } else if (Fixup.getKind() == FK_Data_4) {
95 support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
96 } else if (Fixup.getKind() == FK_Data_8) {
97 support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, Endian);
98 } else if (Fixup.getKind() == FK_PCRel_4) {
99 Value = (uint32_t)((Value - 8) / 8);
100 if (Endian == llvm::endianness::little) {
101 Data[Fixup.getOffset() + 1] = 0x10;
102 support::endian::write32le(&Data[Fixup.getOffset() + 4], Value);
103 } else {
104 Data[Fixup.getOffset() + 1] = 0x1;
105 support::endian::write32be(&Data[Fixup.getOffset() + 4], Value);
106 }
107 } else if (Fixup.getTargetKind() == BPF::FK_BPF_PCRel_4) {
108 // The input Value represents the number of bytes.
109 Value = (uint32_t)((Value - 8) / 8);
110 support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4], Value,
111 Endian);
112 } else {
113 assert(Fixup.getKind() == FK_PCRel_2);
114
115 int64_t ByteOff = (int64_t)Value - 8;
116 if (ByteOff > INT16_MAX * 8 || ByteOff < INT16_MIN * 8)
117 report_fatal_error("Branch target out of insn range");
118
119 Value = (uint16_t)((Value - 8) / 8);
120 support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
121 Endian);
122 }
123}
124
125std::unique_ptr<MCObjectTargetWriter>
126BPFAsmBackend::createObjectTargetWriter() const {
127 return createBPFELFObjectWriter(0);
128}
129
131 const MCSubtargetInfo &STI,
132 const MCRegisterInfo &MRI,
133 const MCTargetOptions &) {
134 return new BPFAsmBackend(llvm::endianness::little);
135}
136
138 const MCSubtargetInfo &STI,
139 const MCRegisterInfo &MRI,
140 const MCTargetOptions &) {
141 return new BPFAsmBackend(llvm::endianness::big);
142}
unsigned const MachineRegisterInfo * MRI
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
raw_pwrite_stream & OS
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:43
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 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.
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
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
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:274
Generic base class for all target subtargets.
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ FK_BPF_PCRel_4
Definition: BPFMCFixups.h:18
@ NumTargetFixupKinds
Definition: BPFMCFixups.h:22
void write32le(void *P, uint32_t V)
Definition: Endian.h:452
void write32be(void *P, uint32_t V)
Definition: Endian.h:461
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCAsmBackend * createBPFAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
MCAsmBackend * createBPFbeAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstTargetFixupKind
Definition: MCFixup.h:45
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
@ FK_PCRel_2
A two-byte pc relative fixup.
Definition: MCFixup.h:29
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition: MCFixup.h:43
std::unique_ptr< MCObjectTargetWriter > createBPFELFObjectWriter(uint8_t OSABI)
endianness
Definition: bit.h:70
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...