ROSE  0.11.21.0
Disassembler.h
1 #ifndef ROSE_DISASSEMBLER_H
2 #define ROSE_DISASSEMBLER_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
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;
110 
111 public:
115  // Serialization
118 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
119 private:
120  friend class boost::serialization::access;
121 
122  template<class S>
123  void serialize(S &s, const unsigned version) {
124  s & BOOST_SERIALIZATION_NVP(p_registers);
125  s & BOOST_SERIALIZATION_NVP(REG_IP);
126  s & BOOST_SERIALIZATION_NVP(REG_SS);
127  if (version >= 1)
128  s & BOOST_SERIALIZATION_NVP(REG_SF);
129  s & BOOST_SERIALIZATION_NVP(p_byteOrder);
130  s & BOOST_SERIALIZATION_NVP(p_wordSizeBytes);
131  s & BOOST_SERIALIZATION_NVP(p_name);
132  if (version >= 2)
133  s & BOOST_SERIALIZATION_NVP(instructionAlignment_);
134  }
135 #endif
136 
137 
139  // Constructors
141 public:
142  Disassembler(): p_registers(NULL), p_byteOrder(ByteOrder::ORDER_LSB), p_wordSizeBytes(4), instructionAlignment_(1) {}
143  virtual ~Disassembler() {}
144 
145 
147  // Registration and lookup methods
149 
150 public:
156  static void registerSubclass(Disassembler*);
157 
163  virtual bool canDisassemble(SgAsmGenericHeader*) const = 0;
164 
171  static Disassembler *lookup(SgAsmGenericHeader*);
172 
176  const std::string& name() const {
177  return p_name;
178  }
179  void name(const std::string &s) {
180  p_name = s;
181  }
188  static std::vector<std::string> isaNames();
189 
199 
202  static Disassembler *lookup(const std::string&);
203 
207  virtual Disassembler *clone() const = 0;
208 
209 
210  /***************************************************************************************************************************
211  * Disassembler properties and settings
212  ***************************************************************************************************************************/
213 public:
218  virtual Unparser::BasePtr unparser() const = 0;
219 
223  ByteOrder::Endianness byteOrder() const { return p_byteOrder; }
224  void byteOrder(ByteOrder::Endianness sex) { p_byteOrder = sex; }
230  size_t wordSizeBytes() const { return p_wordSizeBytes; }
231  void wordSizeBytes(size_t nbytes) { p_wordSizeBytes = nbytes; }
237  size_t instructionAlignment() const;
238 
251  p_registers = rdict;
252  }
254  return p_registers;
255  }
256 
262  const CallingConvention::Dictionary& callingConventions() const { return callingConventions_; }
263  CallingConvention::Dictionary& callingConventions() { return callingConventions_; }
264  void callingConventions(const CallingConvention::Dictionary &d) { callingConventions_ = d; }
269  ASSERT_forbid(REG_IP.isEmpty());
270  return REG_IP;
271  }
272 
275  ASSERT_forbid(REG_SP.isEmpty());
276  return REG_SP;
277  }
278 
281  return REG_SF; // need not be valid
282  }
283 
287  return REG_SS; // need not be valid
288  }
289 
294  return REG_LINK; // need not be valid
295  }
296 
303  return p_proto_dispatcher;
304  }
305 
307  static void initDiagnostics();
308 
309  /***************************************************************************************************************************
310  * Instruction disassembly functions
311  ***************************************************************************************************************************/
312 public:
323  virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL) = 0;
324 
331  SgAsmInstruction *disassembleOne(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
332  AddressSet *successors=NULL);
333 
334 
335  /***************************************************************************************************************************
336  * Miscellaneous methods
337  ***************************************************************************************************************************/
338 public:
343 
347  void mark_referenced_instructions(SgAsmInterpretation*, const MemoryMap::Ptr&, const InstructionMap&);
348 
354  AddressSet get_block_successors(const InstructionMap&, bool &complete/*out*/);
355 
356 private:
358  static void initclass();
359  static void initclassHelper();
360 
366  static SgAsmInstruction *find_instruction_containing(const InstructionMap &insns, rose_addr_t va);
367 };
368 
369 } // namespace
370 } // namespace
371 
372 // Class versions must be at global scope
373 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Disassembler, 2);
374 
375 #endif
376 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:250
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:230
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:253
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:112
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:176
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:280
Main namespace for the ROSE library.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:286
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:264
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:223
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.
size_t instructionAlignment() const
Property: Instruction alignment requirement.
virtual RegisterDescriptor instructionPointerRegister() const
Returns the register that points to instructions.
Definition: Disassembler.h:268
virtual RegisterDescriptor callReturnRegister() const
Returns the register that holds the return address for a function.
Definition: Disassembler.h:293
InstructionSemantics2::BaseSemantics::DispatcherPtr p_proto_dispatcher
Prototypical dispatcher for creating real dispatchers.
Definition: Disassembler.h:109
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
size_t instructionAlignment_
Positive alignment constraint for instruction addresses.
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: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.
Extends std::map with methods that return optional values.
Definition: Map.h:10
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:224
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:262
CallingConvention::Dictionary & callingConventions()
Properties: Register dictionary.
Definition: Disassembler.h:263
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:302
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:274
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:231
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:179
virtual Unparser::BasePtr unparser() const =0
Unparser.