LLVM 22.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"
17#include <cassert>
18#include <cstdint>
19
20using namespace llvm;
21
22namespace {
23
24class BPFAsmBackend : public MCAsmBackend {
25public:
26 BPFAsmBackend(llvm::endianness Endian) : MCAsmBackend(Endian) {}
27 ~BPFAsmBackend() override = default;
28
29 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
30 uint8_t *Data, uint64_t Value, bool IsResolved) override;
31
32 std::unique_ptr<MCObjectTargetWriter>
33 createObjectTargetWriter() const override;
34
35 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
36
38 const MCSubtargetInfo *STI) const override;
39};
40
41} // end anonymous namespace
42
43MCFixupKindInfo BPFAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
44 const static MCFixupKindInfo Infos[BPF::NumTargetFixupKinds] = {
45 {"FK_BPF_PCRel_4", 0, 32, 0},
46 };
47
48 if (Kind < FirstTargetFixupKind)
50
52 "Invalid kind!");
53 return Infos[Kind - FirstTargetFixupKind];
54}
55
56bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
57 const MCSubtargetInfo *STI) const {
58 if ((Count % 8) != 0)
59 return false;
60
61 for (uint64_t i = 0; i < Count; i += 8)
62 support::endian::write<uint64_t>(OS, 0x15000000, Endian);
63
64 return true;
65}
66
67void BPFAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
68 const MCValue &Target, uint8_t *Data,
69 uint64_t Value, bool IsResolved) {
70 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
71 if (Fixup.getKind() == FK_SecRel_8) {
72 // The Value is 0 for global variables, and the in-section offset
73 // for static variables. Write to the immediate field of the inst.
74 assert(Value <= UINT32_MAX);
75 support::endian::write<uint32_t>(Data + 4, static_cast<uint32_t>(Value),
76 Endian);
77 } else if (Fixup.getKind() == FK_Data_4 && !Fixup.isPCRel()) {
78 support::endian::write<uint32_t>(Data, Value, Endian);
79 } else if (Fixup.getKind() == FK_Data_8) {
80 support::endian::write<uint64_t>(Data, Value, Endian);
81 } else if (Fixup.getKind() == FK_Data_4 && Fixup.isPCRel()) {
82 Value = (uint32_t)((Value - 8) / 8);
83 if (Endian == llvm::endianness::little) {
84 Data[1] = 0x10;
86 } else {
87 Data[1] = 0x1;
89 }
90 } else if (Fixup.getKind() == BPF::FK_BPF_PCRel_4) {
91 // The input Value represents the number of bytes.
92 Value = (uint32_t)((Value - 8) / 8);
93 support::endian::write<uint32_t>(Data + 4, Value, Endian);
94 } else {
95 assert(Fixup.getKind() == FK_Data_2 && Fixup.isPCRel());
96
97 int64_t ByteOff = (int64_t)Value - 8;
98 if (ByteOff > INT16_MAX * 8 || ByteOff < INT16_MIN * 8)
99 report_fatal_error("Branch target out of insn range");
100
101 Value = (uint16_t)((Value - 8) / 8);
102 support::endian::write<uint16_t>(Data + 2, Value, Endian);
103 }
104}
105
106std::unique_ptr<MCObjectTargetWriter>
107BPFAsmBackend::createObjectTargetWriter() const {
108 return createBPFELFObjectWriter(0);
109}
110
112 const MCSubtargetInfo &STI,
113 const MCRegisterInfo &MRI,
114 const MCTargetOptions &) {
115 return new BPFAsmBackend(llvm::endianness::little);
116}
117
119 const MCSubtargetInfo &STI,
120 const MCRegisterInfo &MRI,
121 const MCTargetOptions &) {
122 return new BPFAsmBackend(llvm::endianness::big);
123}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition: MD5.cpp:55
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.
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved)=0
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.
Target - Wrapper for Target specific information.
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
@ FK_BPF_PCRel_4
Definition: BPFMCFixups.h:18
@ NumTargetFixupKinds
Definition: BPFMCFixups.h:22
void write32le(void *P, uint32_t V)
Definition: Endian.h:472
void write32be(void *P, uint32_t V)
Definition: Endian.h:481
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ FirstTargetFixupKind
Definition: MCFixup.h:44
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:37
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:36
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition: MCFixup.h:42
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:35
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)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
std::unique_ptr< MCObjectTargetWriter > createBPFELFObjectWriter(uint8_t OSABI)
endianness
Definition: bit.h:71
Target independent information on a fixup kind.
Definition: MCAsmBackend.h:38