ROSE  0.10.13.0
Disassembler.h
1 #ifndef ROSE_DISASSEMBLER_H
2 #define ROSE_DISASSEMBLER_H
3 #include <featureTests.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include "BinaryCallingConvention.h"
7 #include "BinaryUnparser.h"
8 #include "Diagnostics.h" // Rose::Diagnostics
9 #include "MemoryMap.h"
10 #include "Registers.h"
11 #include "RoseException.h"
12 #include "integerOps.h"
13 #include "Map.h"
14 #include "BaseSemantics2.h"
15 
16 #include <boost/serialization/access.hpp>
17 #include <boost/serialization/string.hpp>
18 #include <boost/serialization/version.hpp>
19 
20 // REG_SP possibly defined on __sun
21 // REG_LINK possibly defined on Windows
22 #undef REG_SP
23 #undef REG_LINK
24 
25 namespace Rose {
26 namespace BinaryAnalysis {
27 
50 class Disassembler {
51 public:
53  class Exception: public Rose::Exception {
54  public:
56  Exception(const std::string &reason)
57  : Rose::Exception(reason), ip(0), bit(0), insn(NULL)
58  {}
59 
61  Exception(const std::string &reason, rose_addr_t ip)
62  : Rose::Exception(reason), ip(ip), bit(0), insn(NULL)
63  {}
64 
66  Exception(const std::string &reason, rose_addr_t ip, const SgUnsignedCharList &raw_data, size_t bit)
67  : Rose::Exception(reason), ip(ip), bytes(raw_data), bit(bit), insn(NULL)
68  {}
69 
71  Exception(const std::string &reason, SgAsmInstruction *insn)
72  : Rose::Exception(reason), ip(insn->get_address()), bit(0), insn(insn)
73  {}
74 
75  ~Exception() throw() {}
76 
77  void print(std::ostream&) const;
78  friend std::ostream& operator<<(std::ostream &o, const Exception &e);
79 
80  rose_addr_t ip;
81  SgUnsignedCharList bytes;
84  size_t bit;
87  };
88 
91 
92 
94  // Data members
96 private:
97  CallingConvention::Dictionary callingConventions_;
98 
99 protected:
101  RegisterDescriptor REG_IP, REG_SP, REG_SS, REG_SF, REG_LINK;
102  static std::vector<Disassembler*> disassemblers;
103  ByteOrder::Endianness p_byteOrder;
105  std::string p_name;
109 
110 public:
114  // Serialization
117 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
118 private:
119  friend class boost::serialization::access;
120 
121  template<class S>
122  void serialize(S &s, const unsigned version) {
123  s & BOOST_SERIALIZATION_NVP(p_registers);
124  s & BOOST_SERIALIZATION_NVP(REG_IP);
125  s & BOOST_SERIALIZATION_NVP(REG_SS);
126  if (version >= 1)
127  s & BOOST_SERIALIZATION_NVP(REG_SF);
128  s & BOOST_SERIALIZATION_NVP(p_byteOrder);
129  s & BOOST_SERIALIZATION_NVP(p_wordSizeBytes);
130  s & BOOST_SERIALIZATION_NVP(p_name);
131  }
132 #endif
133 
134 
136  // Constructors
138 public:
139  Disassembler(): p_registers(NULL), p_byteOrder(ByteOrder::ORDER_LSB), p_wordSizeBytes(4) {}
140  virtual ~Disassembler() {}
141 
142 
144  // Registration and lookup methods
146 
147 public:
153  static void registerSubclass(Disassembler*);
154 
160  virtual bool canDisassemble(SgAsmGenericHeader*) const = 0;
161 
168  static Disassembler *lookup(SgAsmGenericHeader*);
169 
173  const std::string& name() const {
174  return p_name;
175  }
176  void name(const std::string &s) {
177  p_name = s;
178  }
185  static std::vector<std::string> isaNames();
186 
196 
199  static Disassembler *lookup(const std::string&);
200 
204  virtual Disassembler *clone() const = 0;
205 
206 
207  /***************************************************************************************************************************
208  * Disassembler properties and settings
209  ***************************************************************************************************************************/
210 public:
215  virtual Unparser::BasePtr unparser() const = 0;
216 
220  ByteOrder::Endianness byteOrder() const { return p_byteOrder; }
221  void byteOrder(ByteOrder::Endianness sex) { p_byteOrder = sex; }
227  size_t wordSizeBytes() const { return p_wordSizeBytes; }
228  void wordSizeBytes(size_t nbytes) { p_wordSizeBytes = nbytes; }
243  p_registers = rdict;
244  }
246  return p_registers;
247  }
248 
254  const CallingConvention::Dictionary& callingConventions() const { return callingConventions_; }
255  CallingConvention::Dictionary& callingConventions() { return callingConventions_; }
256  void callingConventions(const CallingConvention::Dictionary &d) { callingConventions_ = d; }
261  ASSERT_forbid(REG_IP.isEmpty());
262  return REG_IP;
263  }
264 
267  ASSERT_forbid(REG_SP.isEmpty());
268  return REG_SP;
269  }
270 
273  return REG_SF; // need not be valid
274  }
275 
279  return REG_SS; // need not be valid
280  }
281 
286  return REG_LINK; // need not be valid
287  }
288 
295  return p_proto_dispatcher;
296  }
297 
299  static void initDiagnostics();
300 
301  /***************************************************************************************************************************
302  * Instruction disassembly functions
303  ***************************************************************************************************************************/
304 public:
315  virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL) = 0;
316 
323  SgAsmInstruction *disassembleOne(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
324  AddressSet *successors=NULL);
325 
326 
327  /***************************************************************************************************************************
328  * Miscellaneous methods
329  ***************************************************************************************************************************/
330 public:
335 
339  void mark_referenced_instructions(SgAsmInterpretation*, const MemoryMap::Ptr&, const InstructionMap&);
340 
346  AddressSet get_block_successors(const InstructionMap&, bool &complete/*out*/);
347 
348 private:
350  static void initclass();
351  static void initclassHelper();
352 
358  static SgAsmInstruction *find_instruction_containing(const InstructionMap &insns, rose_addr_t va);
359 };
360 
361 } // namespace
362 } // namespace
363 
364 // Class versions must be at global scope
365 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Disassembler, 1);
366 
367 #endif
368 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:242
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:227
AddressSet get_block_successors(const InstructionMap &, bool &complete)
Calculates the successor addresses of a basic block and adds them to a successors set...
const RegisterDictionary * registerDictionary() const
Properties: Register dictionary.
Definition: Disassembler.h:245
rose_addr_t ip
Virtual address where failure occurred; zero if no associated instruction.
Definition: Disassembler.h:80
Map< rose_addr_t, SgAsmInstruction * > InstructionMap
The InstructionMap is a mapping from (absolute) virtual address to disassembled instruction.
Definition: Disassembler.h:90
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1599
static Sawyer::Message::Facility mlog
Disassembler diagnostic streams.
Definition: Disassembler.h:111
size_t bit
Bit offset in instruction byte sequence where disassembly failed (bit/8 is the index into the "bytes"...
Definition: Disassembler.h:84
ByteOrder::Endianness p_byteOrder
Byte order of instructions in memory.
Definition: Disassembler.h:103
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:173
const RegisterDictionary * p_registers
Description of registers available for this platform.
Definition: Disassembler.h:100
SgAsmInstruction * insn
Instruction associated with an assembly error.
Definition: Disassembler.h:86
virtual RegisterDescriptor stackFrameRegister() const
Returns the register that ponts to the stack frame.
Definition: Disassembler.h:272
Main namespace for the ROSE library.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:278
Exception(const std::string &reason, SgAsmInstruction *insn)
An exception bound to a particular instruction being assembled.
Definition: Disassembler.h:71
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:66
void callingConventions(const CallingConvention::Dictionary &d)
Properties: Register dictionary.
Definition: Disassembler.h:256
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:220
static std::vector< std::string > isaNames()
List of names recognized by lookup.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual RegisterDescriptor instructionPointerRegister() const
Returns the register that points to instructions.
Definition: Disassembler.h:260
virtual RegisterDescriptor callReturnRegister() const
Returns the register that holds the return address for a function.
Definition: Disassembler.h:285
InstructionSemantics2::BaseSemantics::DispatcherPtr p_proto_dispatcher
Prototypical dispatcher for creating real dispatchers.
Definition: Disassembler.h:108
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:56
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:105
Describes (part of) a physical CPU register.
size_t p_wordSizeBytes
Basic word size in bytes.
Definition: Disassembler.h:104
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:61
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:101
void byteOrder(ByteOrder::Endianness sex)
Property: Byte order of instructions in memory.
Definition: Disassembler.h:221
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:254
CallingConvention::Dictionary & callingConventions()
Properties: Register dictionary.
Definition: Disassembler.h:255
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:294
static void registerSubclass(Disassembler *)
Register a disassembler instance.
SgUnsignedCharList bytes
Bytes (partial) of failed disassembly, including byte at failure.
Definition: Disassembler.h:81
Exception thrown by the disassemblers.
Definition: Disassembler.h:53
Defines registers available for a particular architecture.
Definition: Registers.h:38
static std::vector< Disassembler * > disassemblers
List of disassembler subclasses.
Definition: Disassembler.h:102
virtual RegisterDescriptor stackPointerRegister() const
Returns the register that points to the stack.
Definition: Disassembler.h:266
Base class for all ROSE exceptions.
Definition: RoseException.h:9
bool isEmpty() const
Predicate returns true if the width is zero.
Represents an interpretation of a binary container.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50
void wordSizeBytes(size_t nbytes)
Property: Basic word size in bytes.
Definition: Disassembler.h:228
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:176
virtual Unparser::BasePtr unparser() const =0
Unparser.