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
22 using namespace llvm;
23
24 namespace {
25
26 class BPFAsmBackend : public MCAsmBackend {
27 public:
BPFAsmBackend(llvm::endianness Endian)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
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * DF,const MCAsmLayout & Layout) const40 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
41 const MCRelaxableFragment *DF,
42 const MCAsmLayout &Layout) const override {
43 return false;
44 }
45
getNumFixupKinds() const46 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
57 const MCFixupKindInfo &
getFixupKindInfo(MCFixupKind Kind) const58 BPFAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
59 const static MCFixupKindInfo Infos[BPF::NumTargetFixupKinds] = {
60 { "FK_BPF_PCRel_4", 0, 32, 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
writeNopData(raw_ostream & OS,uint64_t Count,const MCSubtargetInfo * STI) const71 bool 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, 0x15000000, Endian);
78
79 return true;
80 }
81
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsResolved,const MCSubtargetInfo * STI) const82 void 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>(&Data[Fixup.getOffset() + 4],
92 static_cast<uint32_t>(Value),
93 Endian);
94 } else if (Fixup.getKind() == FK_Data_4) {
95 support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
96 } else if (Fixup.getKind() == FK_Data_8) {
97 support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, 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(&Data[Fixup.getOffset() + 4], Value);
103 } else {
104 Data[Fixup.getOffset() + 1] = 0x1;
105 support::endian::write32be(&Data[Fixup.getOffset() + 4], 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>(&Data[Fixup.getOffset() + 4], Value,
111 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("Branch target out of insn range");
118
119 Value = (uint16_t)((Value - 8) / 8);
120 support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
121 Endian);
122 }
123 }
124
125 std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const126 BPFAsmBackend::createObjectTargetWriter() const {
127 return createBPFELFObjectWriter(0);
128 }
129
createBPFAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions &)130 MCAsmBackend *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
createBPFbeAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions &)137 MCAsmBackend *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