1 //===-- MCFunction.h ------------------------------------------------------===// 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 defines the data structures to hold a CFG reconstructed from 11 // machine code. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_OBJECTDUMP_MCFUNCTION_H 16 #define LLVM_OBJECTDUMP_MCFUNCTION_H 17 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/DenseSet.h" 20 #include "llvm/MC/MCInst.h" 21 #include <map> 22 23 namespace llvm { 24 25 class MCDisassembler; 26 class MCInstrAnalysis; 27 class MemoryObject; 28 class raw_ostream; 29 30 /// MCDecodedInst - Small container to hold an MCInst and associated info like 31 /// address and size. 32 struct MCDecodedInst { 33 uint64_t Address; 34 uint64_t Size; 35 MCInst Inst; 36 MCDecodedInstMCDecodedInst37 MCDecodedInst() {} MCDecodedInstMCDecodedInst38 MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst) 39 : Address(Address), Size(Size), Inst(Inst) {} 40 41 bool operator<(const MCDecodedInst &RHS) const { 42 return Address < RHS.Address; 43 } 44 }; 45 46 /// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing 47 /// MCBasicBlocks. 48 class MCBasicBlock { 49 std::vector<MCDecodedInst> Insts; 50 typedef DenseSet<uint64_t> SetTy; 51 SetTy Succs; 52 public: getInsts()53 ArrayRef<MCDecodedInst> getInsts() const { return Insts; } 54 55 typedef SetTy::const_iterator succ_iterator; succ_begin()56 succ_iterator succ_begin() const { return Succs.begin(); } succ_end()57 succ_iterator succ_end() const { return Succs.end(); } 58 contains(uint64_t Addr)59 bool contains(uint64_t Addr) const { return Succs.count(Addr); } 60 addInst(const MCDecodedInst & Inst)61 void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); } addSucc(uint64_t Addr)62 void addSucc(uint64_t Addr) { Succs.insert(Addr); } 63 64 bool operator<(const MCBasicBlock &RHS) const { 65 return Insts.size() < RHS.Insts.size(); 66 } 67 }; 68 69 /// MCFunction - Represents a named function in machine code, containing 70 /// multiple MCBasicBlocks. 71 class MCFunction { 72 const StringRef Name; 73 // Keep BBs sorted by address. 74 typedef std::vector<std::pair<uint64_t, MCBasicBlock> > MapTy; 75 MapTy Blocks; 76 public: MCFunction(StringRef Name)77 MCFunction(StringRef Name) : Name(Name) {} 78 79 // Create an MCFunction from a region of binary machine code. 80 static MCFunction 81 createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, 82 const MemoryObject &Region, uint64_t Start, uint64_t End, 83 const MCInstrAnalysis *Ana, raw_ostream &DebugOut, 84 SmallVectorImpl<uint64_t> &Calls); 85 86 typedef MapTy::const_iterator iterator; begin()87 iterator begin() const { return Blocks.begin(); } end()88 iterator end() const { return Blocks.end(); } 89 getName()90 StringRef getName() const { return Name; } 91 addBlock(uint64_t Address,const MCBasicBlock & BB)92 MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) { 93 Blocks.push_back(std::make_pair(Address, BB)); 94 return Blocks.back().second; 95 } 96 }; 97 98 } 99 100 #endif 101