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 unsigned getNumFixupKinds() const override {
40 return BPF::NumTargetFixupKinds;
41 }
42 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
43
44 bool writeNopData(raw_ostream &OS, uint64_t Count,
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 { .Name: "FK_BPF_PCRel_4", .TargetOffset: 0, .TargetSize: 32, .Flags: MCFixupKindInfo::FKF_IsPCRel },
54 };
55
56 if (Kind < FirstTargetFixupKind)
57 return MCAsmBackend::getFixupKindInfo(Kind);
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&: OS, value: 0x15000000, endian: Endian);
71
72 return true;
73}
74
75void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
76 const MCValue &Target,
77 MutableArrayRef<char> Data, uint64_t Value,
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>(memory: &Data[Fixup.getOffset() + 4],
85 value: static_cast<uint32_t>(Value),
86 endian: Endian);
87 } else if (Fixup.getKind() == FK_Data_4) {
88 support::endian::write<uint32_t>(memory: &Data[Fixup.getOffset()], value: Value, endian: Endian);
89 } else if (Fixup.getKind() == FK_Data_8) {
90 support::endian::write<uint64_t>(memory: &Data[Fixup.getOffset()], value: Value, endian: 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(P: &Data[Fixup.getOffset() + 4], V: Value);
96 } else {
97 Data[Fixup.getOffset() + 1] = 0x1;
98 support::endian::write32be(P: &Data[Fixup.getOffset() + 4], V: 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>(memory: &Data[Fixup.getOffset() + 4], value: Value,
104 endian: 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(reason: "Branch target out of insn range");
111
112 Value = (uint16_t)((Value - 8) / 8);
113 support::endian::write<uint16_t>(memory: &Data[Fixup.getOffset() + 2], value: Value,
114 endian: Endian);
115 }
116}
117
118std::unique_ptr<MCObjectTargetWriter>
119BPFAsmBackend::createObjectTargetWriter() const {
120 return createBPFELFObjectWriter(OSABI: 0);
121}
122
123MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
124 const MCSubtargetInfo &STI,
125 const MCRegisterInfo &MRI,
126 const MCTargetOptions &) {
127 return new BPFAsmBackend(llvm::endianness::little);
128}
129
130MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
131 const MCSubtargetInfo &STI,
132 const MCRegisterInfo &MRI,
133 const MCTargetOptions &) {
134 return new BPFAsmBackend(llvm::endianness::big);
135}
136