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/MCFixup.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include <cassert>
16 #include <cstdint>
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 class BPFAsmBackend : public MCAsmBackend {
23 public:
24   bool IsLittleEndian;
25 
26   BPFAsmBackend(bool IsLittleEndian)
27     : MCAsmBackend(), IsLittleEndian(IsLittleEndian) {}
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 IsPCRel) const override;
33 
34   MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
35 
36   // No instruction requires relaxation
37   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
38                             const MCRelaxableFragment *DF,
39                             const MCAsmLayout &Layout) const override {
40     return false;
41   }
42 
43   unsigned getNumFixupKinds() const override { return 1; }
44 
45   bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
46 
47   void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
48                         MCInst &Res) const override {}
49 
50   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
51 };
52 
53 } // end anonymous namespace
54 
55 bool BPFAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
56   if ((Count % 8) != 0)
57     return false;
58 
59   for (uint64_t i = 0; i < Count; i += 8)
60     OW->write64(0x15000000);
61 
62   return true;
63 }
64 
65 void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
66                                const MCValue &Target,
67                                MutableArrayRef<char> Data, uint64_t Value,
68                                bool IsPCRel) const {
69   if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
70     assert(Value == 0);
71   } else if (Fixup.getKind() == FK_Data_4 || Fixup.getKind() == FK_Data_8) {
72     unsigned Size = Fixup.getKind() == FK_Data_4 ? 4 : 8;
73 
74     for (unsigned i = 0; i != Size; ++i) {
75       unsigned Idx = IsLittleEndian ? i : Size - i - 1;
76       Data[Fixup.getOffset() + Idx] = uint8_t(Value >> (i * 8));
77     }
78   } else {
79     assert(Fixup.getKind() == FK_PCRel_2);
80     Value = (uint16_t)((Value - 8) / 8);
81     if (IsLittleEndian) {
82       Data[Fixup.getOffset() + 2] = Value & 0xFF;
83       Data[Fixup.getOffset() + 3] = Value >> 8;
84     } else {
85       Data[Fixup.getOffset() + 2] = Value >> 8;
86       Data[Fixup.getOffset() + 3] = Value & 0xFF;
87     }
88   }
89 }
90 
91 MCObjectWriter *BPFAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
92   return createBPFELFObjectWriter(OS, 0, IsLittleEndian);
93 }
94 
95 MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
96                                         const MCRegisterInfo &MRI,
97                                         const Triple &TT, StringRef CPU,
98                                         const MCTargetOptions&) {
99   return new BPFAsmBackend(/*IsLittleEndian=*/true);
100 }
101 
102 MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
103                                           const MCRegisterInfo &MRI,
104                                           const Triple &TT, StringRef CPU,
105                                           const MCTargetOptions&) {
106   return new BPFAsmBackend(/*IsLittleEndian=*/false);
107 }
108