decompiler  1.0.0
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
ghidra::Sleigh Class Reference

A full SLEIGH engine. More...

#include <sleigh.hh>

Inheritance diagram for ghidra::Sleigh:
ghidra::SleighBase ghidra::Translate ghidra::AddrSpaceManager

Public Member Functions

 Sleigh (LoadImage *ld, ContextDatabase *c_db)
 Constructor. More...
 
virtual ~Sleigh (void)
 Destructor.
 
void reset (LoadImage *ld, ContextDatabase *c_db)
 Reset the engine for a new program. More...
 
virtual void initialize (DocumentStorage &store)
 
virtual void registerContext (const string &name, int4 sbit, int4 ebit)
 Add a new context variable to the model for this processor. More...
 
virtual void setContextDefault (const string &nm, uintm val)
 Set the default value for a particular context variable. More...
 
virtual void allowContextSet (bool val) const
 Toggle whether disassembly is allowed to affect context. More...
 
virtual int4 instructionLength (const Address &baseaddr) const
 Get the length of a machine instruction. More...
 
virtual int4 oneInstruction (PcodeEmit &emit, const Address &baseaddr) const
 Transform a single machine instruction into pcode. More...
 
virtual int4 printAssembly (AssemblyEmit &emit, const Address &baseaddr) const
 Disassemble a single machine instruction. More...
 
- Public Member Functions inherited from ghidra::SleighBase
 SleighBase (void)
 Construct an uninitialized translator.
 
bool isInitialized (void) const
 Return true if this is initialized.
 
virtual ~SleighBase (void)
 Destructor.
 
virtual const VarnodeDatagetRegister (const string &nm) const
 Get a register as VarnodeData given its name. More...
 
virtual string getRegisterName (AddrSpace *base, uintb off, int4 size) const
 Get the name of a register given its location. More...
 
virtual void getAllRegisters (map< VarnodeData, string > &reglist) const
 Get a list of all register names and the corresponding location. More...
 
virtual void getUserOpNames (vector< string > &res) const
 Get a list of all user-defined pcode ops. More...
 
SleighSymbol * findSymbol (const string &nm) const
 Find a specific SLEIGH symbol by name in the current scope.
 
SleighSymbol * findSymbol (uintm id) const
 Find a specific SLEIGH symbol by id.
 
SleighSymbol * findGlobalSymbol (const string &nm) const
 Find a specific global SLEIGH symbol by name.
 
void saveXml (ostream &s) const
 Write out the SLEIGH specification as an XML <sleigh> tag. More...
 
- Public Member Functions inherited from ghidra::Translate
 Translate (void)
 Constructor for the translator. More...
 
void setDefaultFloatFormats (void)
 If no explicit float formats, set up default formats. More...
 
bool isBigEndian (void) const
 Is the processor big endian? More...
 
const FloatFormatgetFloatFormat (int4 size) const
 Get format for a particular floating point encoding. More...
 
int4 getAlignment (void) const
 Get the instruction alignment for the processor. More...
 
uint4 getUniqueBase (void) const
 Get the base offset for new temporary registers. More...
 
uint4 getUniqueStart (UniqueLayout layout) const
 Get a tagged address within the unique space. More...
 
- Public Member Functions inherited from ghidra::AddrSpaceManager
 AddrSpaceManager (void)
 Construct an empty address space manager. More...
 
virtual ~AddrSpaceManager (void)
 Destroy the manager. More...
 
int4 getDefaultSize (void) const
 Get size of addresses for the default space. More...
 
AddrSpacegetSpaceByName (const string &nm) const
 Get address space by name. More...
 
AddrSpacegetSpaceByShortcut (char sc) const
 Get address space from its shortcut. More...
 
AddrSpacegetIopSpace (void) const
 Get the internal pcode op space. More...
 
AddrSpacegetFspecSpace (void) const
 Get the internal callspec space. More...
 
AddrSpacegetJoinSpace (void) const
 Get the joining space. More...
 
AddrSpacegetStackSpace (void) const
 Get the stack space for this processor. More...
 
AddrSpacegetUniqueSpace (void) const
 Get the temporary register space for this processor. More...
 
AddrSpacegetDefaultCodeSpace (void) const
 Get the default address space of this processor. More...
 
AddrSpacegetDefaultDataSpace (void) const
 Get the default address space where data is stored. More...
 
AddrSpacegetConstantSpace (void) const
 Get the constant space. More...
 
Address getConstant (uintb val) const
 Get a constant encoded as an Address. More...
 
Address createConstFromSpace (AddrSpace *spc) const
 Create a constant address encoding an address space. More...
 
Address resolveConstant (AddrSpace *spc, uintb val, int4 sz, const Address &point, uintb &fullEncoding) const
 Resolve a native constant into an Address. More...
 
int4 numSpaces (void) const
 Get the number of address spaces for this processor. More...
 
AddrSpacegetSpace (int4 i) const
 Get an address space via its index. More...
 
AddrSpacegetNextSpaceInOrder (AddrSpace *spc) const
 Get the next contiguous address space. More...
 
JoinRecordfindAddJoin (const vector< VarnodeData > &pieces, uint4 logicalsize)
 Get (or create) JoinRecord for pieces. More...
 
JoinRecordfindJoin (uintb offset) const
 Find JoinRecord for offset in the join space. More...
 
void setDeadcodeDelay (AddrSpace *spc, int4 delaydelta)
 Set the deadcodedelay for a specific space. More...
 
void truncateSpace (const TruncationTag &tag)
 Mark a space as truncated from its original size. More...
 
Address constructFloatExtensionAddress (const Address &realaddr, int4 realsize, int4 logicalsize)
 Build a logically lower precision storage location for a bigger floating point register. More...
 
Address constructJoinAddress (const Translate *translate, const Address &hiaddr, int4 hisz, const Address &loaddr, int4 losz)
 Build a logical whole from register pairs. More...
 
void renormalizeJoinAddress (Address &addr, int4 size)
 Make sure a possibly offset join address has a proper JoinRecord. More...
 
Address parseAddressSimple (const string &val)
 Parse a string with just an address space name and a hex offset. More...
 

Protected Member Functions

ParserContext * obtainContext (const Address &addr, int4 state) const
 Obtain a parse tree for the instruction at the given address. More...
 
void resolve (ParserContext &pos) const
 Generate a parse tree suitable for disassembly. More...
 
void resolveHandles (ParserContext &pos) const
 Prepare the parse tree for p-code generation. More...
 
- Protected Member Functions inherited from ghidra::SleighBase
void buildXrefs (vector< string > &errorPairs)
 Build register map. Collect user-ops and context-fields. More...
 
void reregisterContext (void)
 Reregister context fields for a new executable. More...
 
void restoreXml (const Element *el)
 Read a SLEIGH specification from XML. More...
 
- Protected Member Functions inherited from ghidra::Translate
void setBigEndian (bool val)
 Set general endianness to big if val is true. More...
 
void setUniqueBase (uint4 val)
 Set the base offset for new temporary registers. More...
 
- Protected Member Functions inherited from ghidra::AddrSpaceManager
AddrSpacedecodeSpace (Decoder &decoder, const Translate *trans)
 Add a space to the model based an on XML tag. More...
 
void decodeSpaces (Decoder &decoder, const Translate *trans)
 Restore address spaces in the model from an XML tag. More...
 
void setDefaultCodeSpace (int4 index)
 Set the default address space (for code) More...
 
void setDefaultDataSpace (int4 index)
 Set the default address space for data. More...
 
void setReverseJustified (AddrSpace *spc)
 Set reverse justified property on this space. More...
 
void assignShortcut (AddrSpace *spc)
 Select a shortcut character for a new space. More...
 
void markNearPointers (AddrSpace *spc, int4 size)
 Mark that given space can be accessed with near pointers. More...
 
void insertSpace (AddrSpace *spc)
 Add a new address space to the model. More...
 
void copySpaces (const AddrSpaceManager *op2)
 Copy spaces from another manager. More...
 
void addSpacebasePointer (SpacebaseSpace *basespace, const VarnodeData &ptrdata, int4 truncSize, bool stackGrowth)
 Set the base register of a spacebase space. More...
 
void insertResolver (AddrSpace *spc, AddressResolver *rsolv)
 Override the base resolver for a space. More...
 
void setInferPtrBounds (const Range &range)
 Set the range of addresses that can be inferred as pointers. More...
 
JoinRecordfindJoinInternal (uintb offset) const
 Find JoinRecord for offset in the join space. More...
 

Private Member Functions

void clearForDelete (void)
 Delete the context and disassembly caches.
 

Private Attributes

LoadImageloader
 The mapped bytes in the program.
 
ContextDatabasecontext_db
 Database of context values steering disassembly.
 
ContextCachecache
 Cache of recently used context values.
 
DisassemblyCachediscache
 Cache of recently parsed instructions.
 
PcodeCacher pcode_cache
 Cache of p-code data just prior to emitting.
 

Additional Inherited Members

- Public Types inherited from ghidra::Translate
enum  UniqueLayout {
  RUNTIME_BOOLEAN_INVERT =0, RUNTIME_RETURN_LOCATION =0x80, RUNTIME_BITRANGE_EA =0x100, INJECT =0x200,
  ANALYSIS =0x10000000
}
 Tagged addresses in the unique address space. More...
 
- Static Public Attributes inherited from ghidra::SleighBase
static const uint4 MAX_UNIQUE_SIZE = 128
 Maximum size of a varnode in the unique space (should match value in SleighBase.java)
 
- Protected Attributes inherited from ghidra::SleighBase
SubtableSymbol * root
 The root SLEIGH decoding symbol.
 
SymbolTable symtab
 The SLEIGH symbol table.
 
uint4 maxdelayslotbytes
 Maximum number of bytes in a delay-slot directive.
 
uint4 unique_allocatemask
 Bits that are guaranteed to be zero in the unique allocation scheme.
 
uint4 numSections
 Number of named sections.
 
SourceFileIndexer indexer
 source file index used when generating SLEIGH constructor debug info
 
- Protected Attributes inherited from ghidra::Translate
int4 alignment
 Byte modulo on which instructions are aligned.
 
vector< FloatFormatfloatformats
 Floating point formats utilized by the processor.
 

Detailed Description

A full SLEIGH engine.

Its provided with a LoadImage of the bytes to be disassembled and a ContextDatabase.

Assembly is produced via the printAssembly() method, provided with an AssemblyEmit object and an Address.

P-code is produced via the oneInstruction() method, provided with a PcodeEmit object and an Address.

Constructor & Destructor Documentation

◆ Sleigh()

ghidra::Sleigh::Sleigh ( LoadImage ld,
ContextDatabase c_db 
)

Constructor.

Parameters
ldis the LoadImage to draw program bytes from
c_dbis the context database

References cache, context_db, discache, and loader.

Member Function Documentation

◆ allowContextSet()

void ghidra::Sleigh::allowContextSet ( bool  val) const
virtual

Toggle whether disassembly is allowed to affect context.

By default the disassembly/pcode translation engine can change the global context, thereby affecting later disassembly. Context may be getting determined by something other than control flow in, the disassembly, in which case this function can turn off changes made by the disassembly

Parameters
valis true to allow context changes, false prevents changes

Reimplemented from ghidra::Translate.

References ghidra::ContextCache::allowSet(), and cache.

◆ initialize()

void ghidra::Sleigh::initialize ( DocumentStorage store)
virtual

The .sla file from the document store is loaded and cache objects are prepared

Parameters
storeis the document store containing the main <sleigh> tag.

Implements ghidra::Translate.

References cache, discache, ghidra::AddrSpaceManager::getConstantSpace(), ghidra::DocumentStorage::getTag(), ghidra::SleighBase::isInitialized(), ghidra::SleighBase::maxdelayslotbytes, ghidra::SleighBase::reregisterContext(), ghidra::SleighBase::restoreXml(), and ghidra::SleighBase::unique_allocatemask.

◆ instructionLength()

int4 ghidra::Sleigh::instructionLength ( const Address baseaddr) const
virtual

Get the length of a machine instruction.

This method decodes an instruction at a specific address just enough to find the number of bytes it uses within the instruction stream.

Parameters
baseaddris the Address of the instruction
Returns
the number of bytes in the instruction

Implements ghidra::Translate.

References obtainContext().

◆ obtainContext()

ParserContext * ghidra::Sleigh::obtainContext ( const Address addr,
int4  state 
) const
protected

Obtain a parse tree for the instruction at the given address.

The tree may be cached from a previous access. If the address has not been parsed, disassembly is performed, and a new parse tree is prepared. Depending on the desired state, the parse tree can be prepared either for disassembly or for p-code generation.

Parameters
addris the given address of the instruction
stateis the desired parse state.
Returns
the parse tree object (ParseContext)

References discache, ghidra::DisassemblyCache::getParserContext(), resolve(), and resolveHandles().

Referenced by instructionLength(), oneInstruction(), and printAssembly().

◆ oneInstruction()

int4 ghidra::Sleigh::oneInstruction ( PcodeEmit emit,
const Address baseaddr 
) const
virtual

Transform a single machine instruction into pcode.

This is the main interface to the pcode translation engine. The dump method in the emit object is invoked exactly once for each pcode operation in the translation for the machine instruction at the given address. This routine can throw either

Parameters
emitis the tailored pcode emitting object
baseaddris the Address of the machine instruction
Returns
the number of bytes in the machine instruction

Implements ghidra::Translate.

References ghidra::Translate::alignment, ghidra::PcodeCacher::clear(), discache, ghidra::PcodeCacher::emit(), ghidra::LowlevelError::explain, ghidra::AddrSpaceManager::getConstantSpace(), ghidra::Address::getOffset(), ghidra::AddrSpaceManager::getUniqueSpace(), ghidra::UnimplError::instruction_length, obtainContext(), pcode_cache, ghidra::PcodeCacher::resolveRelatives(), and ghidra::SleighBase::unique_allocatemask.

◆ printAssembly()

int4 ghidra::Sleigh::printAssembly ( AssemblyEmit emit,
const Address baseaddr 
) const
virtual

Disassemble a single machine instruction.

This is the main interface to the disassembler for the processor. It disassembles a single instruction and returns the result to the application via the dump method in the emit object.

Parameters
emitis the disassembly emitting object
baseaddris the address of the machine instruction to disassemble

Implements ghidra::Translate.

References ghidra::AssemblyEmit::dump(), and obtainContext().

◆ registerContext()

void ghidra::Sleigh::registerContext ( const string &  name,
int4  sbit,
int4  ebit 
)
virtual

Add a new context variable to the model for this processor.

Add the name of a context register used by the processor and how that register is packed into the context state. This information is used by a ContextDatabase to associate names with context information and to pack context into a single state variable for the translation engine.

Parameters
nameis the name of the new context variable
sbitis the first bit of the variable in the packed state
ebitis the last bit of the variable in the packed state

Reimplemented from ghidra::Translate.

References context_db, and ghidra::ContextDatabase::registerVariable().

◆ reset()

void ghidra::Sleigh::reset ( LoadImage ld,
ContextDatabase c_db 
)

Reset the engine for a new program.

Completely clear everything except the base and reconstruct with a new LoadImage and ContextDatabase

Parameters
ldis the new LoadImage
c_dbis the new ContextDatabase

References cache, ghidra::PcodeCacher::clear(), clearForDelete(), context_db, discache, loader, and pcode_cache.

Referenced by ghidra::SleighArchitecture::buildTranslator().

◆ resolve()

void ghidra::Sleigh::resolve ( ParserContext &  pos) const
protected

Generate a parse tree suitable for disassembly.

Resolve all the constructors involved in the instruction at the indicated address

Parameters
posis the parse object that will hold the resulting tree

References loader, ghidra::LoadImage::loadFill(), and ghidra::SleighBase::root.

Referenced by obtainContext().

◆ resolveHandles()

void ghidra::Sleigh::resolveHandles ( ParserContext &  pos) const
protected

Prepare the parse tree for p-code generation.

Resolve handle templates for the given parse tree, assuming Constructors are already resolved.

Parameters
posis the given parse tree

Referenced by obtainContext().

◆ setContextDefault()

void ghidra::Sleigh::setContextDefault ( const string &  name,
uintm  val 
)
virtual

Set the default value for a particular context variable.

Set the value to be returned for a context variable when there are no explicit address ranges specifying a value for the variable.

Parameters
nameis the name of the context variable
valis the value to be considered default

Reimplemented from ghidra::Translate.

References context_db, and ghidra::ContextDatabase::setVariableDefault().


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