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
9#include "MCTargetDesc/BPFMCFixups.h"
10#include "MCTargetDesc/BPFMCTargetDesc.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/MC/MCAsmBackend.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCFixup.h"
16#include "llvm/MC/MCFixupKindInfo.h"
17#include "llvm/MC/MCObjectWriter.h"
18#include "llvm/Support/EndianStream.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,
32 const MCValue &Target, MutableArrayRef<char> Data,
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
40 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
41 const MCRelaxableFragment *DF,
42 const MCAsmLayout &Layout) const override {
43 return false;
44 }
45
46 unsigned getNumFixupKinds() const override {
47 return BPF::NumTargetFixupKinds;
48 }
49 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
50
51 bool writeNopData(raw_ostream &OS, uint64_t Count,
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 { .Name: "FK_BPF_PCRel_4", .TargetOffset: 0, .TargetSize: 32, .Flags: MCFixupKindInfo::FKF_IsPCRel },
61 };
62
63 if (Kind < FirstTargetFixupKind)
64 return MCAsmBackend::getFixupKindInfo(Kind);
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&: OS, value: 0x15000000, endian: Endian);
78
79 return true;
80}
81
82void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
83 const MCValue &Target,
84 MutableArrayRef<char> Data, uint64_t Value,
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>(memory: &Data[Fixup.getOffset() + 4],
92 value: static_cast<uint32_t>(Value),
93 endian: Endian);
94 } else if (Fixup.getKind() == FK_Data_4) {
95 support::endian::write<uint32_t>(memory: &Data[Fixup.getOffset()], value: Value, endian: Endian);
96 } else if (Fixup.getKind() == FK_Data_8) {
97 support::endian::write<uint64_t>(memory: &Data[Fixup.getOffset()], value: Value, endian: 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(P: &Data[Fixup.getOffset() + 4], V: Value);
103 } else {
104 Data[Fixup.getOffset() + 1] = 0x1;
105 support::endian::write32be(P: &Data[Fixup.getOffset() + 4], V: 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>(memory: &Data[Fixup.getOffset() + 4], value: Value,
111 endian: 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(reason: "Branch target out of insn range");
118
119 Value = (uint16_t)((Value - 8) / 8);
120 support::endian::write<uint16_t>(memory: &Data[Fixup.getOffset() + 2], value: Value,
121 endian: Endian);
122 }
123}
124
125std::unique_ptr<MCObjectTargetWriter>
126BPFAsmBackend::createObjectTargetWriter() const {
127 return createBPFELFObjectWriter(OSABI: 0);
128}
129
130MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
131 const MCSubtargetInfo &STI,
132 const MCRegisterInfo &MRI,
133 const MCTargetOptions &) {
134 return new BPFAsmBackend(llvm::endianness::little);
135}
136
137MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
138 const MCSubtargetInfo &STI,
139 const MCRegisterInfo &MRI,
140 const MCTargetOptions &) {
141 return new BPFAsmBackend(llvm::endianness::big);
142}
143

source code of llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp