ROSE  0.9.9.109
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 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 
41 class Disassembler {
42 public:
44  class Exception: public std::runtime_error {
45  public:
47  Exception(const std::string &reason)
48  : std::runtime_error(reason), ip(0), bit(0), insn(NULL)
49  {}
50 
52  Exception(const std::string &reason, rose_addr_t ip)
53  : std::runtime_error(reason), ip(ip), bit(0), insn(NULL)
54  {}
55 
57  Exception(const std::string &reason, rose_addr_t ip, const SgUnsignedCharList &raw_data, size_t bit)
58  : std::runtime_error(reason), ip(ip), bytes(raw_data), bit(bit), insn(NULL)
59  {}
60 
62  Exception(const std::string &reason, SgAsmInstruction *insn)
63  : std::runtime_error(reason), ip(insn->get_address()), bit(0), insn(insn)
64  {}
65 
66  ~Exception() throw() {}
67 
68  void print(std::ostream&) const;
69  friend std::ostream& operator<<(std::ostream &o, const Exception &e);
70 
71  rose_addr_t ip;
72  SgUnsignedCharList bytes;
75  size_t bit;
78  };
79 
82  typedef std::set<rose_addr_t> AddressSet;
83 
86 
87 
89  // Data members
91 private:
92  CallingConvention::Dictionary callingConventions_;
93 
94 protected:
96  RegisterDescriptor REG_IP, REG_SP, REG_SS;
97  static std::vector<Disassembler*> disassemblers;
98  ByteOrder::Endianness p_byteOrder;
99  size_t p_wordSizeBytes;
100  std::string p_name;
104 
105 public:
109  // Serialization
112 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
113 private:
114  friend class boost::serialization::access;
115 
116  template<class S>
117  void serialize(S &s, const unsigned version) {
118  s & BOOST_SERIALIZATION_NVP(p_registers);
119  s & BOOST_SERIALIZATION_NVP(REG_IP);
120  s & BOOST_SERIALIZATION_NVP(REG_SS);
121  s & BOOST_SERIALIZATION_NVP(p_byteOrder);
122  s & BOOST_SERIALIZATION_NVP(p_wordSizeBytes);
123  s & BOOST_SERIALIZATION_NVP(p_name);
124  }
125 #endif
126 
127 
129  // Constructors
131 public:
132  Disassembler(): p_registers(NULL), p_byteOrder(ByteOrder::ORDER_LSB), p_wordSizeBytes(4) {}
133  virtual ~Disassembler() {}
134 
135 
137  // Registration and lookup methods
139 
140 public:
146  static void registerSubclass(Disassembler*);
147 
153  virtual bool canDisassemble(SgAsmGenericHeader*) const = 0;
154 
161  static Disassembler *lookup(SgAsmGenericHeader*);
162 
166  const std::string& name() const {
167  return p_name;
168  }
169  void name(const std::string &s) {
170  p_name = s;
171  }
177  static std::vector<std::string> isaNames();
178 
188 
191  static Disassembler *lookup(const std::string&);
192 
196  virtual Disassembler *clone() const = 0;
197 
198 
199  /***************************************************************************************************************************
200  * Disassembler properties and settings
201  ***************************************************************************************************************************/
202 public:
207  virtual Unparser::BasePtr unparser() const = 0;
208 
212  ByteOrder::Endianness byteOrder() const { return p_byteOrder; }
213  void byteOrder(ByteOrder::Endianness sex) { p_byteOrder = sex; }
219  size_t wordSizeBytes() const { return p_wordSizeBytes; }
220  void wordSizeBytes(size_t nbytes) { p_wordSizeBytes = nbytes; }
235  p_registers = rdict;
236  }
238  return p_registers;
239  }
240 
246  const CallingConvention::Dictionary& callingConventions() const { return callingConventions_; }
247  CallingConvention::Dictionary& callingConventions() { return callingConventions_; }
248  void callingConventions(const CallingConvention::Dictionary &d) { callingConventions_ = d; }
253  ASSERT_require(REG_IP.is_valid());
254  return REG_IP;
255  }
256 
259  ASSERT_require(REG_SP.is_valid());
260  return REG_SP;
261  }
262 
266  return REG_SS; // need not be valid
267  }
268 
275  return p_proto_dispatcher;
276  }
277 
279  static void initDiagnostics();
280 
281  /***************************************************************************************************************************
282  * Instruction disassembly functions
283  ***************************************************************************************************************************/
284 public:
295  virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL) = 0;
296 
303  SgAsmInstruction *disassembleOne(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
304  AddressSet *successors=NULL);
305 
306 
307  /***************************************************************************************************************************
308  * Miscellaneous methods
309  ***************************************************************************************************************************/
310 public:
314  virtual SgAsmInstruction* makeUnknownInstruction(const Exception&) = 0;
315 
319  void mark_referenced_instructions(SgAsmInterpretation*, const MemoryMap::Ptr&, const InstructionMap&);
320 
326  AddressSet get_block_successors(const InstructionMap&, bool *complete);
327 
328 private:
330  static void initclass();
331  static void initclassHelper();
332 
338  static SgAsmInstruction *find_instruction_containing(const InstructionMap &insns, rose_addr_t va);
339 };
340 
341 } // namespace
342 } // namespace
343 
344 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:234
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:219
const RegisterDictionary * registerDictionary() const
Properties: Register dictionary.
Definition: Disassembler.h:237
rose_addr_t ip
Virtual address where failure occurred; zero if no associated instruction.
Definition: Disassembler.h:71
Map< rose_addr_t, SgAsmInstruction * > InstructionMap
The InstructionMap is a mapping from (absolute) virtual address to disassembled instruction.
Definition: Disassembler.h:85
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
static Sawyer::Message::Facility mlog
Disassembler diagnostic streams.
Definition: Disassembler.h:106
size_t bit
Bit offset in instruction byte sequence where disassembly failed (bit/8 is the index into the "bytes"...
Definition: Disassembler.h:75
ByteOrder::Endianness p_byteOrder
Byte order of instructions in memory.
Definition: Disassembler.h:98
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:166
RegisterDescriptor REG_SS
Register descriptors initialized during construction.
Definition: Disassembler.h:96
const RegisterDictionary * p_registers
Description of registers available for this platform.
Definition: Disassembler.h:95
SgAsmInstruction * insn
Instruction associated with an assembly error.
Definition: Disassembler.h:77
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:265
Exception(const std::string &reason, SgAsmInstruction *insn)
An exception bound to a particular instruction being assembled.
Definition: Disassembler.h:62
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:57
void callingConventions(const CallingConvention::Dictionary &d)
Properties: Register dictionary.
Definition: Disassembler.h:248
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:212
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:252
InstructionSemantics2::BaseSemantics::DispatcherPtr p_proto_dispatcher
Prototypical dispatcher for creating real dispatchers.
Definition: Disassembler.h:103
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:47
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:100
size_t p_wordSizeBytes
Basic word size in bytes.
Definition: Disassembler.h:99
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:52
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:213
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:246
CallingConvention::Dictionary & callingConventions()
Properties: Register dictionary.
Definition: Disassembler.h:247
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:274
static void registerSubclass(Disassembler *)
Register a disassembler instance.
SgUnsignedCharList bytes
Bytes (partial) of failed disassembly, including byte at failure.
Definition: Disassembler.h:72
Exception thrown by the disassemblers.
Definition: Disassembler.h:44
static std::vector< Disassembler * > disassemblers
List of disassembler subclasses.
Definition: Disassembler.h:97
virtual RegisterDescriptor stackPointerRegister() const
Returns the register that points to the stack.
Definition: Disassembler.h:258
Represents an interpretation of a binary container.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:41
void wordSizeBytes(size_t nbytes)
Property: Basic word size in bytes.
Definition: Disassembler.h:220
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:169
std::set< rose_addr_t > AddressSet
An AddressSet contains virtual addresses (alternatively, relative virtual addresses) for such things ...
Definition: Disassembler.h:82
virtual Unparser::BasePtr unparser() const =0
Unparser.