LLVM  4.0.0
BPFDisassembler.cpp
Go to the documentation of this file.
1 //===- BPFDisassembler.cpp - Disassembler for BPF ---------------*- C++ -*-===//
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 // This file is part of the BPF Disassembler.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BPF.h"
15 #include "BPFSubtarget.h"
17 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/MC/MCInst.h"
23 #include <cstdint>
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "bpf-disassembler"
28 
30 
31 namespace {
32 
33 /// A disassembler class for BPF.
34 class BPFDisassembler : public MCDisassembler {
35 public:
36  BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
37  : MCDisassembler(STI, Ctx) {}
38  ~BPFDisassembler() override = default;
39 
40  DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
41  ArrayRef<uint8_t> Bytes, uint64_t Address,
42  raw_ostream &VStream,
43  raw_ostream &CStream) const override;
44 };
45 
46 } // end anonymous namespace
47 
49  const MCSubtargetInfo &STI,
50  MCContext &Ctx) {
51  return new BPFDisassembler(STI, Ctx);
52 }
53 
54 
55 extern "C" void LLVMInitializeBPFDisassembler() {
56  // Register the disassembler.
63 }
64 
65 static const unsigned GPRDecoderTable[] = {
66  BPF::R0, BPF::R1, BPF::R2, BPF::R3, BPF::R4, BPF::R5,
67  BPF::R6, BPF::R7, BPF::R8, BPF::R9, BPF::R10, BPF::R11};
68 
69 static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
70  uint64_t /*Address*/,
71  const void * /*Decoder*/) {
72  if (RegNo > 11)
73  return MCDisassembler::Fail;
74 
75  unsigned Reg = GPRDecoderTable[RegNo];
78 }
79 
80 static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn,
81  uint64_t Address, const void *Decoder) {
82  unsigned Register = (Insn >> 16) & 0xf;
84  unsigned Offset = (Insn & 0xffff);
85  Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset)));
86 
88 }
89 
90 #include "BPFGenDisassemblerTables.inc"
91 
93  uint64_t &Size, uint64_t &Insn) {
94  uint64_t Lo, Hi;
95 
96  if (Bytes.size() < 8) {
97  Size = 0;
98  return MCDisassembler::Fail;
99  }
100 
101  Size = 8;
102  Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8);
103  Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24);
104  Insn = Make_64(Hi, Lo);
105 
107 }
108 
109 DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
110  ArrayRef<uint8_t> Bytes,
111  uint64_t Address,
112  raw_ostream &VStream,
113  raw_ostream &CStream) const {
114  uint64_t Insn;
115  DecodeStatus Result;
116 
117  Result = readInstruction64(Bytes, Address, Size, Insn);
118  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
119 
120  Result = decodeInstruction(DecoderTableBPF64, Instr, Insn,
121  Address, this, STI);
122  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
123 
124  switch (Instr.getOpcode()) {
125  case BPF::LD_imm64: {
126  if (Bytes.size() < 16) {
127  Size = 0;
128  return MCDisassembler::Fail;
129  }
130  Size = 16;
131  uint64_t Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24);
132  auto& Op = Instr.getOperand(1);
133  Op.setImm(Make_64(Hi, Op.getImm()));
134  break;
135  }
136  case BPF::LD_ABS_B:
137  case BPF::LD_ABS_H:
138  case BPF::LD_ABS_W:
139  case BPF::LD_IND_B:
140  case BPF::LD_IND_H:
141  case BPF::LD_IND_W: {
142  auto Op = Instr.getOperand(0);
143  Instr.clear();
145  Instr.addOperand(Op);
146  break;
147  }
148  }
149 
150  return Result;
151 }
152 
153 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
154  const void *Decoder);
#define R4(n)
void clear()
Definition: MCInst.h:172
DecodeStatus
Ternary decode status.
Superclass for all disassemblers.
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
#define R2(n)
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:111
Reg
All possible values of the reg field in the ModR/M byte.
Context object for machine code objects.
Definition: MCContext.h:51
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t, const void *)
int decodeInstruction(InternalInstruction *insn, byteReader_t reader, const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg, uint64_t startLoc, DisassemblerMode mode)
Decode one instruction and store the decoding results in a buffer provided by the consumer...
static const unsigned GPRDecoderTable[]
DecodeStatus(* DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, const void *Decoder)
static DecodeStatus readInstruction64(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint64_t &Insn)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
uint32_t Offset
#define R6(n)
static MCDisassembler * createBPFDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
Promote Memory to Register
Definition: Mem2Reg.cpp:100
void LLVMInitializeBPFDisassembler()
unsigned getOpcode() const
Definition: MCInst.h:159
Target - Wrapper for Target specific information.
static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, uint64_t Address, const void *Decoder)
MCSubtargetInfo - Generic base class for all target subtargets.
Target & getTheBPFleTarget()
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make_64 - This functions makes a 64-bit integer from a high / low pair of 32-bit integers.
Definition: MathExtras.h:259
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
Target & getTheBPFbeTarget()
MCDisassembler::DecodeStatus DecodeStatus
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164
Target & getTheBPFTarget()