ROSE  0.9.10.95
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 "Registers.h"
8 #include "MemoryMap.h"
9 #include "integerOps.h"
10 #include "Map.h"
11 #include "BaseSemantics2.h"
12 
13 #include <boost/serialization/access.hpp>
14 #include <boost/serialization/string.hpp>
15 #include <boost/serialization/version.hpp>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 
42 class Disassembler {
43 public:
45  class Exception: public std::runtime_error {
46  public:
48  Exception(const std::string &reason)
49  : std::runtime_error(reason), ip(0), bit(0), insn(NULL)
50  {}
51 
53  Exception(const std::string &reason, rose_addr_t ip)
54  : std::runtime_error(reason), ip(ip), bit(0), insn(NULL)
55  {}
56 
58  Exception(const std::string &reason, rose_addr_t ip, const SgUnsignedCharList &raw_data, size_t bit)
59  : std::runtime_error(reason), ip(ip), bytes(raw_data), bit(bit), insn(NULL)
60  {}
61 
63  Exception(const std::string &reason, SgAsmInstruction *insn)
64  : std::runtime_error(reason), ip(insn->get_address()), bit(0), insn(insn)
65  {}
66 
67  ~Exception() throw() {}
68 
69  void print(std::ostream&) const;
70  friend std::ostream& operator<<(std::ostream &o, const Exception &e);
71 
72  rose_addr_t ip;
73  SgUnsignedCharList bytes;
76  size_t bit;
79  };
80 
83  typedef std::set<rose_addr_t> AddressSet;
84 
87 
88 
90  // Data members
92 private:
93  CallingConvention::Dictionary callingConventions_;
94 
95 protected:
97  RegisterDescriptor REG_IP, REG_SP, REG_SS, REG_SF;
98  static std::vector<Disassembler*> disassemblers;
99  ByteOrder::Endianness p_byteOrder;
101  std::string p_name;
105 
106 public:
110  // Serialization
113 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
114 private:
115  friend class boost::serialization::access;
116 
117  template<class S>
118  void serialize(S &s, const unsigned version) {
119  s & BOOST_SERIALIZATION_NVP(p_registers);
120  s & BOOST_SERIALIZATION_NVP(REG_IP);
121  s & BOOST_SERIALIZATION_NVP(REG_SS);
122  if (version >= 1)
123  s & BOOST_SERIALIZATION_NVP(REG_SF);
124  s & BOOST_SERIALIZATION_NVP(p_byteOrder);
125  s & BOOST_SERIALIZATION_NVP(p_wordSizeBytes);
126  s & BOOST_SERIALIZATION_NVP(p_name);
127  }
128 #endif
129 
130 
132  // Constructors
134 public:
135  Disassembler(): p_registers(NULL), p_byteOrder(ByteOrder::ORDER_LSB), p_wordSizeBytes(4) {}
136  virtual ~Disassembler() {}
137 
138 
140  // Registration and lookup methods
142 
143 public:
149  static void registerSubclass(Disassembler*);
150 
156  virtual bool canDisassemble(SgAsmGenericHeader*) const = 0;
157 
164  static Disassembler *lookup(SgAsmGenericHeader*);
165 
169  const std::string& name() const {
170  return p_name;
171  }
172  void name(const std::string &s) {
173  p_name = s;
174  }
180  static std::vector<std::string> isaNames();
181 
191 
194  static Disassembler *lookup(const std::string&);
195 
199  virtual Disassembler *clone() const = 0;
200 
201 
202  /***************************************************************************************************************************
203  * Disassembler properties and settings
204  ***************************************************************************************************************************/
205 public:
210  virtual Unparser::BasePtr unparser() const = 0;
211 
215  ByteOrder::Endianness byteOrder() const { return p_byteOrder; }
216  void byteOrder(ByteOrder::Endianness sex) { p_byteOrder = sex; }
222  size_t wordSizeBytes() const { return p_wordSizeBytes; }
223  void wordSizeBytes(size_t nbytes) { p_wordSizeBytes = nbytes; }
238  p_registers = rdict;
239  }
241  return p_registers;
242  }
243 
249  const CallingConvention::Dictionary& callingConventions() const { return callingConventions_; }
250  CallingConvention::Dictionary& callingConventions() { return callingConventions_; }
251  void callingConventions(const CallingConvention::Dictionary &d) { callingConventions_ = d; }
256  ASSERT_require(REG_IP.is_valid());
257  return REG_IP;
258  }
259 
262  ASSERT_require(REG_SP.is_valid());
263  return REG_SP;
264  }
265 
268  return REG_SF; // need not be valid
269  }
270 
274  return REG_SS; // need not be valid
275  }
276 
283  return p_proto_dispatcher;
284  }
285 
287  static void initDiagnostics();
288 
289  /***************************************************************************************************************************
290  * Instruction disassembly functions
291  ***************************************************************************************************************************/
292 public:
303  virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL) = 0;
304 
311  SgAsmInstruction *disassembleOne(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
312  AddressSet *successors=NULL);
313 
314 
315  /***************************************************************************************************************************
316  * Miscellaneous methods
317  ***************************************************************************************************************************/
318 public:
322  virtual SgAsmInstruction* makeUnknownInstruction(const Exception&) = 0;
323 
327  void mark_referenced_instructions(SgAsmInterpretation*, const MemoryMap::Ptr&, const InstructionMap&);
328 
334  AddressSet get_block_successors(const InstructionMap&, bool *complete);
335 
336 private:
338  static void initclass();
339  static void initclassHelper();
340 
346  static SgAsmInstruction *find_instruction_containing(const InstructionMap &insns, rose_addr_t va);
347 };
348 
349 } // namespace
350 } // namespace
351 
352 // Class versions must be at global scope
353 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Disassembler, 1);
354 
355 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:237
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:222
const RegisterDictionary * registerDictionary() const
Properties: Register dictionary.
Definition: Disassembler.h:240
rose_addr_t ip
Virtual address where failure occurred; zero if no associated instruction.
Definition: Disassembler.h:72
Map< rose_addr_t, SgAsmInstruction * > InstructionMap
The InstructionMap is a mapping from (absolute) virtual address to disassembled instruction.
Definition: Disassembler.h:86
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
static Sawyer::Message::Facility mlog
Disassembler diagnostic streams.
Definition: Disassembler.h:107
size_t bit
Bit offset in instruction byte sequence where disassembly failed (bit/8 is the index into the "bytes"...
Definition: Disassembler.h:76
ByteOrder::Endianness p_byteOrder
Byte order of instructions in memory.
Definition: Disassembler.h:99
STL namespace.
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:169
RegisterDescriptor REG_SF
Register descriptors initialized during construction.
Definition: Disassembler.h:97
const RegisterDictionary * p_registers
Description of registers available for this platform.
Definition: Disassembler.h:96
SgAsmInstruction * insn
Instruction associated with an assembly error.
Definition: Disassembler.h:78
virtual RegisterDescriptor stackFrameRegister() const
Returns the register that ponts to the stack frame.
Definition: Disassembler.h:267
Main namespace for the ROSE library.
Describes (part of) a physical CPU register.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:273
Exception(const std::string &reason, SgAsmInstruction *insn)
An exception bound to a particular instruction being assembled.
Definition: Disassembler.h:63
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:58
void callingConventions(const CallingConvention::Dictionary &d)
Properties: Register dictionary.
Definition: Disassembler.h:251
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:215
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:255
InstructionSemantics2::BaseSemantics::DispatcherPtr p_proto_dispatcher
Prototypical dispatcher for creating real dispatchers.
Definition: Disassembler.h:104
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:48
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:101
size_t p_wordSizeBytes
Basic word size in bytes.
Definition: Disassembler.h:100
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:53
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.
Defines registers available for a particular architecture.
Definition: Registers.h:32
void byteOrder(ByteOrder::Endianness sex)
Property: Byte order of instructions in memory.
Definition: Disassembler.h:216
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:249
CallingConvention::Dictionary & callingConventions()
Properties: Register dictionary.
Definition: Disassembler.h:250
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:282
static void registerSubclass(Disassembler *)
Register a disassembler instance.
SgUnsignedCharList bytes
Bytes (partial) of failed disassembly, including byte at failure.
Definition: Disassembler.h:73
Exception thrown by the disassemblers.
Definition: Disassembler.h:45
static std::vector< Disassembler * > disassemblers
List of disassembler subclasses.
Definition: Disassembler.h:98
virtual RegisterDescriptor stackPointerRegister() const
Returns the register that points to the stack.
Definition: Disassembler.h:261
Represents an interpretation of a binary container.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:42
void wordSizeBytes(size_t nbytes)
Property: Basic word size in bytes.
Definition: Disassembler.h:223
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:172
std::set< rose_addr_t > AddressSet
An AddressSet contains virtual addresses (alternatively, relative virtual addresses) for such things ...
Definition: Disassembler.h:83
virtual Unparser::BasePtr unparser() const =0
Unparser.