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