LLVM 20.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 unsigned getNumFixupKinds() const override {
41 }
42 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
43
45 const MCSubtargetInfo *STI) const override;
46};
47
48} // end anonymous namespace
49
50const MCFixupKindInfo &
51BPFAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
52 const static MCFixupKindInfo Infos[BPF::NumTargetFixupKinds] = {
53 { "FK_BPF_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
54 };
55
56 if (Kind < FirstTargetFixupKind)
58
59 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
60 "Invalid kind!");
61 return Infos[Kind - FirstTargetFixupKind];
62}
63
64bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
65 const MCSubtargetInfo *STI) const {
66 if ((Count % 8) != 0)
67 return false;
68
69 for (uint64_t i = 0; i < Count; i += 8)
70 support::endian::write<uint64_t>(OS, 0x15000000, Endian);
71
72 return true;
73}
74
75void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
76 const MCValue &Target,
78 bool IsResolved,
79 const MCSubtargetInfo *STI) const {
80 if (Fixup.getKind() == FK_SecRel_8) {
81 // The Value is 0 for global variables, and the in-section offset
82 // for static variables. Write to the immediate field of the inst.
83 assert(Value <= UINT32_MAX);
84 support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4],
85 static_cast<uint32_t>(Value),
86 Endian);
87 } else if (Fixup.getKind() == FK_Data_4) {
88 support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
89 } else if (Fixup.getKind() == FK_Data_8) {
90 support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, Endian);
91 } else if (Fixup.getKind() == FK_PCRel_4) {
92 Value = (uint32_t)((Value - 8) / 8);
93 if (Endian == llvm::endianness::little) {
94 Data[Fixup.getOffset() + 1] = 0x10;
95 support::endian::write32le(&Data[Fixup.getOffset() + 4], Value);
96 } else {
97 Data[Fixup.getOffset() + 1] = 0x1;
98 support::endian::write32be(&Data[Fixup.getOffset() + 4], Value);
99 }
100 } else if (Fixup.getTargetKind() == BPF::FK_BPF_PCRel_4) {
101 // The input Value represents the number of bytes.
102 Value = (uint32_t)((Value - 8) / 8);
103 support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4], Value,
104 Endian);
105 } else {
106 assert(Fixup.getKind() == FK_PCRel_2);
107
108 int64_t ByteOff = (int64_t)Value - 8;
109 if (ByteOff > INT16_MAX * 8 || ByteOff < INT16_MIN * 8)
110 report_fatal_error("Branch target out of insn range");
111
112 Value = (uint16_t)((Value - 8) / 8);
113 support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
114 Endian);
115 }
116}
117
118std::unique_ptr<MCObjectTargetWriter>
119BPFAsmBackend::createObjectTargetWriter() const {
120 return createBPFELFObjectWriter(0);
121}
122
124 const MCSubtargetInfo &STI,
125 const MCRegisterInfo &MRI,
126 const MCTargetOptions &) {
127 return new BPFAsmBackend(llvm::endianness::little);
128}
129
131 const MCSubtargetInfo &STI,
132 const MCRegisterInfo &MRI,
133 const MCTargetOptions &) {
134 return new BPFAsmBackend(llvm::endianness::big);
135}
unsigned const MachineRegisterInfo * MRI
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:42
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 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...
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...
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:468
void write32be(void *P, uint32_t V)
Definition: Endian.h:477
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:167
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...