ROSE  0.9.11.42
Disassembler.h
1 #ifndef ROSE_DISASSEMBLER_H
2 #define ROSE_DISASSEMBLER_H
3 
4 #include "BinaryCallingConvention.h"
5 #include "BinaryUnparser.h"
6 #include "Diagnostics.h" // Rose::Diagnostics
7 #include "MemoryMap.h"
8 #include "Registers.h"
9 #include "RoseException.h"
10 #include "integerOps.h"
11 #include "Map.h"
12 #include "BaseSemantics2.h"
13 
14 #include <boost/serialization/access.hpp>
15 #include <boost/serialization/string.hpp>
16 #include <boost/serialization/version.hpp>
17 
18 // REG_PP possibly defined on __sun
19 // REG_LINK possibly defined on Windows
20 #undef REG_SP
21 #undef REG_LINK
22 
23 namespace Rose {
24 namespace BinaryAnalysis {
25 
48 class Disassembler {
49 public:
51  class Exception: public Rose::Exception {
52  public:
54  Exception(const std::string &reason)
55  : Rose::Exception(reason), ip(0), bit(0), insn(NULL)
56  {}
57 
59  Exception(const std::string &reason, rose_addr_t ip)
60  : Rose::Exception(reason), ip(ip), bit(0), insn(NULL)
61  {}
62 
64  Exception(const std::string &reason, rose_addr_t ip, const SgUnsignedCharList &raw_data, size_t bit)
65  : Rose::Exception(reason), ip(ip), bytes(raw_data), bit(bit), insn(NULL)
66  {}
67 
69  Exception(const std::string &reason, SgAsmInstruction *insn)
70  : Rose::Exception(reason), ip(insn->get_address()), bit(0), insn(insn)
71  {}
72 
73  ~Exception() throw() {}
74 
75  void print(std::ostream&) const;
76  friend std::ostream& operator<<(std::ostream &o, const Exception &e);
77 
78  rose_addr_t ip;
79  SgUnsignedCharList bytes;
82  size_t bit;
85  };
86 
89  typedef std::set<rose_addr_t> AddressSet;
90 
93 
94 
96  // Data members
98 private:
99  CallingConvention::Dictionary callingConventions_;
100 
101 protected:
103  RegisterDescriptor REG_IP, REG_SP, REG_SS, REG_SF, REG_LINK;
104  static std::vector<Disassembler*> disassemblers;
105  ByteOrder::Endianness p_byteOrder;
107  std::string p_name;
111 
112 public:
116  // Serialization
119 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
120 private:
121  friend class boost::serialization::access;
122 
123  template<class S>
124  void serialize(S &s, const unsigned version) {
125  s & BOOST_SERIALIZATION_NVP(p_registers);
126  s & BOOST_SERIALIZATION_NVP(REG_IP);
127  s & BOOST_SERIALIZATION_NVP(REG_SS);
128  if (version >= 1)
129  s & BOOST_SERIALIZATION_NVP(REG_SF);
130  s & BOOST_SERIALIZATION_NVP(p_byteOrder);
131  s & BOOST_SERIALIZATION_NVP(p_wordSizeBytes);
132  s & BOOST_SERIALIZATION_NVP(p_name);
133  }
134 #endif
135 
136 
138  // Constructors
140 public:
141  Disassembler(): p_registers(NULL), p_byteOrder(ByteOrder::ORDER_LSB), p_wordSizeBytes(4) {}
142  virtual ~Disassembler() {}
143 
144 
146  // Registration and lookup methods
148 
149 public:
155  static void registerSubclass(Disassembler*);
156 
162  virtual bool canDisassemble(SgAsmGenericHeader*) const = 0;
163 
170  static Disassembler *lookup(SgAsmGenericHeader*);
171 
175  const std::string& name() const {
176  return p_name;
177  }
178  void name(const std::string &s) {
179  p_name = s;
180  }
186  static std::vector<std::string> isaNames();
187 
197 
200  static Disassembler *lookup(const std::string&);
201 
205  virtual Disassembler *clone() const = 0;
206 
207 
208  /***************************************************************************************************************************
209  * Disassembler properties and settings
210  ***************************************************************************************************************************/
211 public:
216  virtual Unparser::BasePtr unparser() const = 0;
217 
221  ByteOrder::Endianness byteOrder() const { return p_byteOrder; }
222  void byteOrder(ByteOrder::Endianness sex) { p_byteOrder = sex; }
228  size_t wordSizeBytes() const { return p_wordSizeBytes; }
229  void wordSizeBytes(size_t nbytes) { p_wordSizeBytes = nbytes; }
244  p_registers = rdict;
245  }
247  return p_registers;
248  }
249 
255  const CallingConvention::Dictionary& callingConventions() const { return callingConventions_; }
256  CallingConvention::Dictionary& callingConventions() { return callingConventions_; }
257  void callingConventions(const CallingConvention::Dictionary &d) { callingConventions_ = d; }
262  ASSERT_require(REG_IP.is_valid());
263  return REG_IP;
264  }
265 
268  ASSERT_require(REG_SP.is_valid());
269  return REG_SP;
270  }
271 
274  return REG_SF; // need not be valid
275  }
276 
280  return REG_SS; // need not be valid
281  }
282 
287  return REG_LINK; // need not be valid
288  }
289 
296  return p_proto_dispatcher;
297  }
298 
300  static void initDiagnostics();
301 
302  /***************************************************************************************************************************
303  * Instruction disassembly functions
304  ***************************************************************************************************************************/
305 public:
316  virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL) = 0;
317 
324  SgAsmInstruction *disassembleOne(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
325  AddressSet *successors=NULL);
326 
327 
328  /***************************************************************************************************************************
329  * Miscellaneous methods
330  ***************************************************************************************************************************/
331 public:
336 
340  void mark_referenced_instructions(SgAsmInterpretation*, const MemoryMap::Ptr&, const InstructionMap&);
341 
347  AddressSet get_block_successors(const InstructionMap&, bool *complete);
348 
349 private:
351  static void initclass();
352  static void initclassHelper();
353 
359  static SgAsmInstruction *find_instruction_containing(const InstructionMap &insns, rose_addr_t va);
360 };
361 
362 } // namespace
363 } // namespace
364 
365 // Class versions must be at global scope
366 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Disassembler, 1);
367 
368 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:243
AddressSet get_block_successors(const InstructionMap &, bool *complete)
Calculates the successor addresses of a basic block and adds them to a successors set...
virtual SgAsmInstruction * makeUnknownInstruction(const Exception &)=0
Makes an unknown instruction from an exception.
size_t wordSizeBytes() const
Property: Basic word size in bytes.
Definition: Disassembler.h:228
const RegisterDictionary * registerDictionary() const
Properties: Register dictionary.
Definition: Disassembler.h:246
rose_addr_t ip
Virtual address where failure occurred; zero if no associated instruction.
Definition: Disassembler.h:78
Map< rose_addr_t, SgAsmInstruction * > InstructionMap
The InstructionMap is a mapping from (absolute) virtual address to disassembled instruction.
Definition: Disassembler.h:92
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
static Sawyer::Message::Facility mlog
Disassembler diagnostic streams.
Definition: Disassembler.h:113
size_t bit
Bit offset in instruction byte sequence where disassembly failed (bit/8 is the index into the "bytes"...
Definition: Disassembler.h:82
ByteOrder::Endianness p_byteOrder
Byte order of instructions in memory.
Definition: Disassembler.h:105
void mark_referenced_instructions(SgAsmInterpretation *, const MemoryMap::Ptr &, const InstructionMap &)
Marks parts of the file that correspond to instructions as having been referenced.
const std::string & name() const
Property: Name by which disassembler is registered.
Definition: Disassembler.h:175
const RegisterDictionary * p_registers
Description of registers available for this platform.
Definition: Disassembler.h:102
SgAsmInstruction * insn
Instruction associated with an assembly error.
Definition: Disassembler.h:84
virtual RegisterDescriptor stackFrameRegister() const
Returns the register that ponts to the stack frame.
Definition: Disassembler.h:273
Main namespace for the ROSE library.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:279
Exception(const std::string &reason, SgAsmInstruction *insn)
An exception bound to a particular instruction being assembled.
Definition: Disassembler.h:69
Exception(const std::string &reason, rose_addr_t ip, const SgUnsignedCharList &raw_data, size_t bit)
An exception bound to a particular instruction being disassembled.
Definition: Disassembler.h:64
void callingConventions(const CallingConvention::Dictionary &d)
Properties: Register dictionary.
Definition: Disassembler.h:257
virtual SgAsmInstruction * disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL)=0
This is the lowest level disassembly function and is implemented in the architecture-specific subclas...
ByteOrder::Endianness byteOrder() const
Property: Byte order of instructions in memory.
Definition: Disassembler.h:221
static std::vector< std::string > isaNames()
List of names recognized by lookup.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual RegisterDescriptor instructionPointerRegister() const
Returns the register that points to instructions.
Definition: Disassembler.h:261
virtual RegisterDescriptor callReturnRegister() const
Returns the register that holds the return address for a function.
Definition: Disassembler.h:286
InstructionSemantics2::BaseSemantics::DispatcherPtr p_proto_dispatcher
Prototypical dispatcher for creating real dispatchers.
Definition: Disassembler.h:110
Base class for container file headers.
std::vector< Definition::Ptr > Dictionary
A ordered collection of calling convention definitions.
Exception(const std::string &reason)
A bare exception not bound to any particular instruction.
Definition: Disassembler.h:54
virtual Disassembler * clone() const =0
Creates a new copy of a disassembler.
std::string p_name
Name by which this dissassembler is registered.
Definition: Disassembler.h:107
Describes (part of) a physical CPU register.
size_t p_wordSizeBytes
Basic word size in bytes.
Definition: Disassembler.h:106
Exception(const std::string &reason, rose_addr_t ip)
An exception bound to a virtual address but no raw data or instruction.
Definition: Disassembler.h:59
static Disassembler * lookup(SgAsmGenericHeader *)
Finds a suitable disassembler.
virtual bool canDisassemble(SgAsmGenericHeader *) const =0
Predicate determining the suitability of a disassembler for a specific file header.
RegisterDescriptor REG_LINK
Register descriptors initialized during construction.
Definition: Disassembler.h:103
void byteOrder(ByteOrder::Endianness sex)
Property: Byte order of instructions in memory.
Definition: Disassembler.h:222
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:255
CallingConvention::Dictionary & callingConventions()
Properties: Register dictionary.
Definition: Disassembler.h:256
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:295
static void registerSubclass(Disassembler *)
Register a disassembler instance.
SgUnsignedCharList bytes
Bytes (partial) of failed disassembly, including byte at failure.
Definition: Disassembler.h:79
Exception thrown by the disassemblers.
Definition: Disassembler.h:51
Defines registers available for a particular architecture.
Definition: Registers.h:35
static std::vector< Disassembler * > disassemblers
List of disassembler subclasses.
Definition: Disassembler.h:104
virtual RegisterDescriptor stackPointerRegister() const
Returns the register that points to the stack.
Definition: Disassembler.h:267
Base class for all ROSE exceptions.
Definition: RoseException.h:9
Represents an interpretation of a binary container.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:48
void wordSizeBytes(size_t nbytes)
Property: Basic word size in bytes.
Definition: Disassembler.h:229
static void initDiagnostics()
Initializes and registers disassembler diagnostic streams.
void name(const std::string &s)
Property: Name by which disassembler is registered.
Definition: Disassembler.h:178
std::set< rose_addr_t > AddressSet
An AddressSet contains virtual addresses (alternatively, relative virtual addresses) for such things ...
Definition: Disassembler.h:89
virtual Unparser::BasePtr unparser() const =0
Unparser.