decompiler  1.0.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | Friends | List of all members
ghidra::MemoryBank Class Referenceabstract

Memory storage/state for a single AddressSpace. More...

#include <memstate.hh>

Inheritance diagram for ghidra::MemoryBank:
ghidra::MemoryHashOverlay ghidra::MemoryImage ghidra::MemoryPageOverlay

Public Member Functions

 MemoryBank (AddrSpace *spc, int4 ws, int4 ps)
 Generic constructor for a memory bank. More...
 
int4 getWordSize (void) const
 Get the number of bytes in a word for this memory bank. More...
 
int4 getPageSize (void) const
 Get the number of bytes in a page for this memory bank. More...
 
AddrSpacegetSpace (void) const
 Get the address space associated with this memory bank. More...
 
void setValue (uintb offset, int4 size, uintb val)
 Set the value of a (small) range of bytes. More...
 
uintb getValue (uintb offset, int4 size) const
 Retrieve the value encoded in a (small) range of bytes. More...
 
void setChunk (uintb offset, int4 size, const uint1 *val)
 Set values of an arbitrary sequence of bytes. More...
 
void getChunk (uintb offset, int4 size, uint1 *res) const
 Retrieve an arbitrary sequence of bytes. More...
 

Static Public Member Functions

static uintb constructValue (const uint1 *ptr, int4 size, bool bigendian)
 Decode bytes to value. More...
 
static void deconstructValue (uint1 *ptr, uintb val, int4 size, bool bigendian)
 Encode value to bytes. More...
 

Protected Member Functions

virtual void insert (uintb addr, uintb val)=0
 Insert a word in memory bank at an aligned location.
 
virtual uintb find (uintb addr) const =0
 Retrieve a word from memory bank at an aligned location.
 
virtual void getPage (uintb addr, uint1 *res, int4 skip, int4 size) const
 Retrieve data from a memory page. More...
 
virtual void setPage (uintb addr, const uint1 *val, int4 skip, int4 size)
 Write data into a memory page. More...
 

Private Attributes

int4 wordsize
 Number of bytes in an aligned word access.
 
int4 pagesize
 Number of bytes in an aligned page access.
 
AddrSpacespace
 The address space associated with this memory.
 

Friends

class MemoryPageOverlay
 
class MemoryHashOverlay
 

Detailed Description

Memory storage/state for a single AddressSpace.

Class for setting and getting memory values within a space The basic API is to get/set arrays of byte values via offset within the space. Helper functions getValue and setValue easily retrieve/store integers of various sizes from memory, using the endianness encoding specified by the space. Accesses through the public interface, are automatically broken down into word accesses, through the private insert/find methods, and page accesses through getPage/setPage. So these are the virtual methods that need to be overridden in the derived classes.

Constructor & Destructor Documentation

◆ MemoryBank()

ghidra::MemoryBank::MemoryBank ( AddrSpace spc,
int4  ws,
int4  ps 
)

Generic constructor for a memory bank.

A MemoryBank must be associated with a specific address space, have a preferred or natural wordsize and a natural pagesize. Both the wordsize and pagesize must be a power of 2.

Parameters
spcis the associated address space
wsis the number of bytes in the preferred wordsize
psis the number of bytes in a page

References pagesize, space, and wordsize.

Member Function Documentation

◆ constructValue()

uintb ghidra::MemoryBank::constructValue ( const uint1 *  ptr,
int4  size,
bool  bigendian 
)
static

Decode bytes to value.

This is a static convenience routine for decoding a value from a sequence of bytes depending on the desired endianness

Parameters
ptris the pointer to the bytes to decode
sizeis the number of bytes
bigendianis true if the bytes are encoded in big endian form
Returns
the decoded value

Referenced by ghidra::MemoryPageOverlay::find().

◆ deconstructValue()

void ghidra::MemoryBank::deconstructValue ( uint1 *  ptr,
uintb  val,
int4  size,
bool  bigendian 
)
static

Encode value to bytes.

This is a static convenience routine for encoding bytes from a given value, depending on the desired endianness

Parameters
ptris a pointer to the location to write the encoded bytes
valis the value to be encoded
sizeis the number of bytes to encode
bigendianis true if a big endian encoding is desired

Referenced by ghidra::MemoryPageOverlay::insert().

◆ getChunk()

void ghidra::MemoryBank::getChunk ( uintb  offset,
int4  size,
uint1 *  res 
) const

Retrieve an arbitrary sequence of bytes.

This is the most general method for reading a sequence of bytes from the memory bank. There is no restriction on the offset or the number of bytes to read, except that the range must be contained in the address space.

Parameters
offsetis the start of the byte range to read
sizeis the number of bytes to read
resis a pointer to where the retrieved bytes should be stored

References getPage(), and pagesize.

Referenced by ghidra::MemoryState::getChunk().

◆ getPage()

void ghidra::MemoryBank::getPage ( uintb  addr,
uint1 *  res,
int4  skip,
int4  size 
) const
protectedvirtual

Retrieve data from a memory page.

This routine only retrieves data from a single page in the memory bank. Bytes need not be retrieved from the exact start of a page, but all bytes must come from one page. A page is a fixed number of bytes, and the address of a page is always aligned based on that number of bytes. This routine may be overridden for a page based implementation of the MemoryBank. The default implementation retrieves the page as aligned words using the find method.

Parameters
addris the aligned offset of the desired page
resis a pointer to where fetched data should be written
skipis the offset into the page to get the bytes from
sizeis the number of bytes to retrieve

Reimplemented in ghidra::MemoryPageOverlay, and ghidra::MemoryImage.

References find(), ghidra::AddrSpace::isBigEndian(), space, and wordsize.

Referenced by getChunk(), and ghidra::MemoryImage::insert().

◆ getPageSize()

int4 ghidra::MemoryBank::getPageSize ( void  ) const
inline

Get the number of bytes in a page for this memory bank.

A MemoryBank is instantiated with a natural page size. Requests for large chunks of data may be broken down into units of this size.

Returns
the number of bytes in a page.

References pagesize.

Referenced by ghidra::MemoryPageOverlay::find(), ghidra::MemoryPageOverlay::insert(), and ghidra::MemoryPageOverlay::setPage().

◆ getSpace()

AddrSpace * ghidra::MemoryBank::getSpace ( void  ) const
inline

Get the address space associated with this memory bank.

A MemoryBank is a contiguous sequence of bytes associated with a particular address space.

Returns
the AddressSpace associated with this bank.

References space.

Referenced by ghidra::MemoryImage::find(), ghidra::MemoryPageOverlay::find(), ghidra::MemoryImage::getPage(), ghidra::MemoryPageOverlay::insert(), and ghidra::MemoryState::setMemoryBank().

◆ getValue()

uintb ghidra::MemoryBank::getValue ( uintb  offset,
int4  size 
) const

Retrieve the value encoded in a (small) range of bytes.

This routine gets the value from a range of bytes at an arbitrary address. It takes into account the endianness of the underlying space when decoding the value. The value is constructed by making one or more aligned word queries, using the find method. The desired value may span multiple words and is reconstructed properly.

Parameters
offsetis the start of the byte range encoding the value
sizeis the number of bytes in the range
Returns
the decoded value

References ghidra::calc_mask(), find(), ghidra::AddrSpace::isBigEndian(), space, and wordsize.

Referenced by ghidra::JumpBasic::backup2Switch(), ghidra::JumpBasic::findNormalized(), and ghidra::MemoryState::getValue().

◆ getWordSize()

int4 ghidra::MemoryBank::getWordSize ( void  ) const
inline

Get the number of bytes in a word for this memory bank.

A MemoryBank is instantiated with a natural word size. Requests for arbitrary byte ranges may be broken down into units of this size.

Returns
the number of bytes in a word.

References wordsize.

Referenced by ghidra::MemoryImage::find(), ghidra::MemoryPageOverlay::find(), and ghidra::MemoryPageOverlay::insert().

◆ setChunk()

void ghidra::MemoryBank::setChunk ( uintb  offset,
int4  size,
const uint1 *  val 
)

Set values of an arbitrary sequence of bytes.

This the most general method for writing a sequence of bytes into the memory bank. There is no restriction on the offset to write to or the number of bytes to be written, except that the range must be contained in the address space.

Parameters
offsetis the start of the byte range to be written
sizeis the number of bytes to write
valis a pointer to the sequence of bytes to be written into the bank

References pagesize, and setPage().

Referenced by ghidra::MemoryState::setChunk().

◆ setPage()

void ghidra::MemoryBank::setPage ( uintb  addr,
const uint1 *  val,
int4  skip,
int4  size 
)
protectedvirtual

Write data into a memory page.

This routine writes data only to a single page of the memory bank. Bytes need not be written to the exact start of the page, but all bytes must be written to only one page when using this routine. A page is a fixed number of bytes, and the address of a page is always aligned based on this size. This routine may be overridden for a page based implementation of the MemoryBank. The default implementation writes the page as a sequence of aligned words, using the insert method.

Parameters
addris the aligned offset of the desired page
valis a pointer to the bytes to be written into the page
skipis the offset into the page where bytes will be written
sizeis the number of bytes to be written

Reimplemented in ghidra::MemoryPageOverlay.

References find(), insert(), ghidra::AddrSpace::isBigEndian(), space, and wordsize.

Referenced by setChunk().

◆ setValue()

void ghidra::MemoryBank::setValue ( uintb  offset,
int4  size,
uintb  val 
)

Set the value of a (small) range of bytes.

This routine is used to set a single value in the memory bank at an arbitrary address It takes into account the endianness of the associated address space when encoding the value as bytes in the bank. The value is broken up into aligned pieces of wordsize and the actual write is performed with the insert routine. If only parts of aligned words are written to, then the remaining parts are filled in with the original value, via the find routine.

Parameters
offsetis the start of the byte range to write
sizeis the number of bytes in the range to write
valis the value to be written

References ghidra::calc_mask(), find(), insert(), ghidra::AddrSpace::isBigEndian(), space, and wordsize.

Referenced by ghidra::MemoryState::setValue().


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