ROSE 0.11.145.147
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
Rose::BinaryAnalysis::Assembler Class Referenceabstract

Description

Virtual base class for instruction assemblers.

The Assembler class is a virtual class providing all non-architecture-specific functionality for the assembly of instructions; architecture-specific components are in subclasses such as AssemblerAarch64, AssemblerPowerpc, and AssemblerX86.

This example shows how to test the disassembler against the assembler by disassembling and then reassembling all instructions. Generate debugging output for instructions that cannot be reassembled into an encoding identical to the original bytes:

// Disassemble the interpretation (normally happens automatically)
SgAsmInterpretation *interp = ....;
Disassembler::BasePtr d = Disassembler::lookup(interp);
d->disassemble(interp);
// Create an assembler that can handle instructions in this interpretation.
// Cause the assembler to choose encodings that are identical to the
// bytes originally disassebled. The assembler still goes through all the
// work of assembling, but then checks the result against the original
// bytes.
asm->set_encoding_type(Assembler::ET_MATCHES);
// Attempt to reassemble each instruction. If the assembly fails to produce
// the original bytes then an exception is thrown and we try again for
// debugging side effects.
std::vector<SgNode*> insns = NodeQuery::querySubTree(interp, V_SgAsmInstruction);
for (size_t i=0; i<insns.size(); i++) {
SgAsmInstruction *insn = isSgAsmInstruction(insns[i]);
try {
asm->assembleOne(insn);
} catch(const Assembler::Exception &e) {
fprintf(stderr, "assembly failed at 0x%08llx: %s\n",
insn->get_address(), e.mesg.c_str());
asm->set_debug(stderr);
try {
asm->assembleOne(insn);
} catch(const Assembler::Exception&) {
}
asm->set_debug(NULL);
}
}
Exception thrown by the assemblers.
Definition Assembler.h:61
Virtual base class for instruction assemblers.
Definition Assembler.h:58
@ ET_MATCHES
Returns an encoding that matches the SgAsmInstruction::p_raw_bytes.
Definition Assembler.h:84
static Assembler * create(SgAsmInterpretation *interp)
Creates an assembler that is appropriate for assembling instructions in the specified interpretation.
Base class for machine instructions.
Represents an interpretation of a binary container.
rose_addr_t const & get_address() const
Property: Starting virtual address.

Definition at line 58 of file Assembler.h.

#include <frontend/Disassemblers/Assembler.h>

Inheritance diagram for Rose::BinaryAnalysis::Assembler:
Inheritance graph
[legend]

Classes

class  Exception
 Exception thrown by the assemblers. More...
 

Public Types

enum  EncodingType {
  ET_SHORTEST ,
  ET_LONGEST ,
  ET_MATCHES
}
 Assemblers can often assemble a single instruction various ways. More...
 

Public Member Functions

virtual SgUnsignedCharList assembleOne (SgAsmInstruction *insn)=0
 This is the lowest level architecture-independent assembly function and is implemented in the architecture-specific subclasses (there may be other architecture-dependent assembly methods also).
 
SgUnsignedCharList assembleBlock (SgAsmBlock *)
 Assembles a single basic block of instructions, packing them together and adjusting their virtual addresses.
 
SgUnsignedCharList assembleBlock (const std::vector< SgAsmInstruction * > &insns, rose_addr_t starting_rva)
 Assembles a single basic block of instructions like the version that takes an SgAsmBlock pointer.
 
virtual SgUnsignedCharList assembleProgram (const std::string &source)=0
 Assembles a program from an assembly listing.
 
void set_encoding_type (EncodingType et)
 Controls how the assembleOne() method determines which encoding to return.
 
EncodingType get_encoding_type () const
 Returns the encoding type employed by this assembler.
 
void set_debug (FILE *f)
 Sends assembler diagnostics to the specified output stream.
 
FILE * get_debug () const
 Returns the file currently used for debugging; null implies no debugging.
 

Static Public Member Functions

static Assemblercreate (SgAsmInterpretation *interp)
 Creates an assembler that is appropriate for assembling instructions in the specified interpretation.
 
static Assemblercreate (SgAsmGenericHeader *)
 Creates an assembler that is appropriate for assembling instructions in the specified header.
 

Protected Attributes

FILE * p_debug
 Set to non-null to get debugging info.
 
EncodingType p_encoding_type
 Which encoding should be returned by assembleOne.
 

Member Enumeration Documentation

◆ EncodingType

Assemblers can often assemble a single instruction various ways.

For instance, on x86 the immediate value -53 can be assembled into a single byte, or sign extended into 2, 4, or 8 bytes. These enumeration constants control how the assembleOne() method determines which encoding to return.

Enumerator
ET_SHORTEST 

Returns the shortest possible encoding.

This is the default.

ET_LONGEST 

Returns the longest encoding.

ET_MATCHES 

Returns an encoding that matches the SgAsmInstruction::p_raw_bytes.

This is used mainly for testing that the assembler can produce the same encoding that was originally used by the disassembler when the instruction was created.

Definition at line 80 of file Assembler.h.

Constructor & Destructor Documentation

◆ Assembler()

Rose::BinaryAnalysis::Assembler::Assembler ( )
inline

Definition at line 89 of file Assembler.h.

◆ ~Assembler()

virtual Rose::BinaryAnalysis::Assembler::~Assembler ( )
inlinevirtual

Definition at line 93 of file Assembler.h.

Member Function Documentation

◆ assembleOne()

virtual SgUnsignedCharList Rose::BinaryAnalysis::Assembler::assembleOne ( SgAsmInstruction insn)
pure virtual

This is the lowest level architecture-independent assembly function and is implemented in the architecture-specific subclasses (there may be other architecture-dependent assembly methods also).

It assembles one instruction and returns the encoding, throwing an exception if anything goes wrong.

Implemented in Rose::BinaryAnalysis::AssemblerX86.

◆ assembleBlock() [1/2]

SgUnsignedCharList Rose::BinaryAnalysis::Assembler::assembleBlock ( SgAsmBlock )

Assembles a single basic block of instructions, packing them together and adjusting their virtual addresses.

The virtual address of the first instruction of the block determines the starting address. An exception is thrown if any of the instructions cannot be assembled.

◆ assembleBlock() [2/2]

SgUnsignedCharList Rose::BinaryAnalysis::Assembler::assembleBlock ( const std::vector< SgAsmInstruction * > &  insns,
rose_addr_t  starting_rva 
)

Assembles a single basic block of instructions like the version that takes an SgAsmBlock pointer.

In this case, the instructions are stored in a vector instead. The will be treated like a single basic block: no control flow adjustments will be made. An exception is thrown if any of the instructions cannot be disassembled.

◆ assembleProgram()

virtual SgUnsignedCharList Rose::BinaryAnalysis::Assembler::assembleProgram ( const std::string &  source)
pure virtual

Assembles a program from an assembly listing.

This method may call an external assembler to do its work.

Implemented in Rose::BinaryAnalysis::AssemblerX86.

◆ set_encoding_type()

void Rose::BinaryAnalysis::Assembler::set_encoding_type ( EncodingType  et)
inline

Controls how the assembleOne() method determines which encoding to return.

Definition at line 128 of file Assembler.h.

References p_encoding_type.

◆ get_encoding_type()

EncodingType Rose::BinaryAnalysis::Assembler::get_encoding_type ( ) const
inline

Returns the encoding type employed by this assembler.

See set_encoding_type().

Definition at line 133 of file Assembler.h.

References p_encoding_type.

◆ set_debug()

void Rose::BinaryAnalysis::Assembler::set_debug ( FILE *  f)
inline

Sends assembler diagnostics to the specified output stream.

Null (the default) turns off debugging.

Definition at line 138 of file Assembler.h.

References p_debug.

◆ get_debug()

FILE * Rose::BinaryAnalysis::Assembler::get_debug ( ) const
inline

Returns the file currently used for debugging; null implies no debugging.

Definition at line 143 of file Assembler.h.

References p_debug.

Member Data Documentation

◆ p_debug

FILE* Rose::BinaryAnalysis::Assembler::p_debug
protected

Set to non-null to get debugging info.

Definition at line 151 of file Assembler.h.

Referenced by get_debug(), and set_debug().

◆ p_encoding_type

EncodingType Rose::BinaryAnalysis::Assembler::p_encoding_type
protected

Which encoding should be returned by assembleOne.

Definition at line 152 of file Assembler.h.

Referenced by get_encoding_type(), and set_encoding_type().


The documentation for this class was generated from the following file: