1 //===-- BPFAsmBackend.cpp - BPF Assembler Backend -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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/MCObjectWriter.h"
17 #include "llvm/Support/EndianStream.h"
18 #include <cassert>
19 #include <cstdint>
20 
21 using namespace llvm;
22 
23 namespace {
24 
25 class BPFAsmBackend : public MCAsmBackend {
26 public:
BPFAsmBackend(support::endianness Endian)27   BPFAsmBackend(support::endianness Endian) : MCAsmBackend(Endian) {}
28   ~BPFAsmBackend() override = default;
29 
30   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
31                   const MCValue &Target, MutableArrayRef<char> Data,
32                   uint64_t Value, bool IsResolved,
33                   const MCSubtargetInfo *STI) const override;
34 
35   std::unique_ptr<MCObjectTargetWriter>
36   createObjectTargetWriter() const override;
37 
38   // No instruction requires relaxation
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * DF,const MCAsmLayout & Layout) const39   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
40                             const MCRelaxableFragment *DF,
41                             const MCAsmLayout &Layout) const override {
42     return false;
43   }
44 
getNumFixupKinds() const45   unsigned getNumFixupKinds() const override { return 1; }
46 
mayNeedRelaxation(const MCInst & Inst,const MCSubtargetInfo & STI) const47   bool mayNeedRelaxation(const MCInst &Inst,
48                          const MCSubtargetInfo &STI) const override {
49     return false;
50   }
51 
relaxInstruction(const MCInst & Inst,const MCSubtargetInfo & STI,MCInst & Res) const52   void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
53                         MCInst &Res) const override {}
54 
55   bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
56 };
57 
58 } // end anonymous namespace
59 
writeNopData(raw_ostream & OS,uint64_t Count) const60 bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
61   if ((Count % 8) != 0)
62     return false;
63 
64   for (uint64_t i = 0; i < Count; i += 8)
65     support::endian::write<uint64_t>(OS, 0x15000000, Endian);
66 
67   return true;
68 }
69 
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsResolved,const MCSubtargetInfo * STI) const70 void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
71                                const MCValue &Target,
72                                MutableArrayRef<char> Data, uint64_t Value,
73                                bool IsResolved,
74                                const MCSubtargetInfo *STI) const {
75   if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
76     if (Value) {
77       MCContext &Ctx = Asm.getContext();
78       Ctx.reportError(Fixup.getLoc(),
79                       "Unsupported relocation: try to compile with -O2 or above, "
80                       "or check your static variable usage");
81     }
82   } else if (Fixup.getKind() == FK_Data_4) {
83     support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
84   } else if (Fixup.getKind() == FK_Data_8) {
85     support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, Endian);
86   } else if (Fixup.getKind() == FK_PCRel_4) {
87     Value = (uint32_t)((Value - 8) / 8);
88     if (Endian == support::little) {
89       Data[Fixup.getOffset() + 1] = 0x10;
90       support::endian::write32le(&Data[Fixup.getOffset() + 4], Value);
91     } else {
92       Data[Fixup.getOffset() + 1] = 0x1;
93       support::endian::write32be(&Data[Fixup.getOffset() + 4], Value);
94     }
95   } else {
96     assert(Fixup.getKind() == FK_PCRel_2);
97     Value = (uint16_t)((Value - 8) / 8);
98     support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
99                                      Endian);
100   }
101 }
102 
103 std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const104 BPFAsmBackend::createObjectTargetWriter() const {
105   return createBPFELFObjectWriter(0);
106 }
107 
createBPFAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions &)108 MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
109                                         const MCSubtargetInfo &STI,
110                                         const MCRegisterInfo &MRI,
111                                         const MCTargetOptions &) {
112   return new BPFAsmBackend(support::little);
113 }
114 
createBPFbeAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions &)115 MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
116                                           const MCSubtargetInfo &STI,
117                                           const MCRegisterInfo &MRI,
118                                           const MCTargetOptions &) {
119   return new BPFAsmBackend(support::big);
120 }
121