ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MemoryMap Class Reference

An efficient mapping from an address space to stored data. More...

#include <MemoryMap.h>

Collaboration diagram for MemoryMap:

Classes

class  AnonymousBuffer
 Buffer of bytes. More...
 
class  Buffer
 Base class for data associated with a memory segment. More...
 
class  ByteBuffer
 Buffer of bytes. More...
 
class  Exception
 Exception for MemoryMap operations. More...
 
class  ExternBuffer
 Buffer of data owned by someone else. More...
 
struct  Inconsistent
 Exception for an inconsistent mapping. More...
 
class  MmapBuffer
 Buffer whose underlying storage is from mmap. More...
 
struct  NoFreeSpace
 Exception thrown by find_free() when there's not enough free space left. More...
 
struct  NotMapped
 Exception for when we try to access a virtual address that isn't mapped. More...
 
class  NullBuffer
 Buffer that has no data. More...
 
class  Segment
 A contiguous, homogeneous region of an address space. More...
 
struct  SyntaxError
 Exception thrown by load() when there's a syntax error in the index file. More...
 
class  Visitor
 Visitor for traversing a memory map. More...
 

Public Types

enum  Protection {
  MM_PROT_BITS = 0x00000007,
  MM_PROT_READ = 0x00000001,
  MM_PROT_WRITE = 0x00000002,
  MM_PROT_EXEC = 0x00000004,
  MM_PROT_NONE = 0x00000000,
  MM_PROT_ANY = 0x00000007,
  MM_PROT_RW = (MM_PROT_READ|MM_PROT_WRITE),
  MM_PROT_RX = (MM_PROT_READ|MM_PROT_EXEC),
  MM_PROT_RWX = (MM_PROT_ANY),
  MM_PROT_FLAGS = 0xfffffff0,
  MM_PROT_PRIVATE = 0x00000010
}
 Mapping permissions. More...
 
enum  CopyLevel {
  COPY_SHALLOW,
  COPY_DEEP,
  COPY_ON_WRITE
}
 Map copying. More...
 
typedef boost::shared_ptr< BufferBufferPtr
 
typedef RangeMap< Extent, SegmentSegments
 
typedef Segments::iterator iterator
 
typedef Segments::const_iterator const_iterator
 
typedef Segments::reverse_iterator reverse_iterator
 
typedef
Segments::const_reverse_iterator 
const_reverse_iterator
 

Public Member Functions

 MemoryMap ()
 Constructs an empty memory map. More...
 
 MemoryMap (const MemoryMap &other, CopyLevel copy_level=COPY_SHALLOW)
 Shallow copy constructor. More...
 
MemoryMapinit (const MemoryMap &source, CopyLevel copy_level=COPY_SHALLOW)
 Initialize this memory map with info from another. More...
 
bool empty () const
 Determines if a memory map is empty. More...
 
void clear ()
 Clear the entire memory map by erasing all addresses that are defined. More...
 
size_t size () const
 Number of bytes mapped. More...
 
void insert (const Extent &range, const Segment &segment, bool erase_prior=true)
 Define a new area of memory. More...
 
size_t insert_file (const std::string &filename, rose_addr_t va, bool writable=false, bool erase_prior=true, const std::string &sgmtname="")
 Insert the contents of a file into the memory map at the specified address. More...
 
void erase (const Extent &range)
 Erase parts of the mapping that correspond to the specified virtual address range. More...
 
void erase (const Segment &)
 Erase a single extent from a memory map. More...
 
std::pair< Extent, Segmentat (rose_addr_t va) const
 Get information about an address. More...
 
rose_addr_t find_free (rose_addr_t start_va, size_t size, rose_addr_t mem_alignment=1) const
 Search for free space in the mapping. More...
 
rose_addr_t find_last_free (rose_addr_t max=(rose_addr_t)(-1)) const
 Finds the highest area of unmapped addresses. More...
 
void traverse (Visitor &visitor) const
 Traverses the segments of a map. More...
 
void prune (Visitor &predicate)
 Removes segments for which predicate returns true. More...
 
void prune (unsigned required, unsigned prohibited=MM_PROT_NONE)
 Removes segments based on permissions. More...
 
void erase_zeros (size_t minsize)
 Erases regions of zero bytes that are executable and readable and at least minsize in size. More...
 
const Segmentssegments () const
 List of map segments. More...
 
SgUnsignedCharList read (rose_addr_t start_va, size_t desired, unsigned req_perms=MM_PROT_READ) const
 Reads data from a memory map. More...
 
std::string read_string (rose_addr_t start_va, size_t desired, int(*valid_char)(int)=NULL, int(*invalid_char)(int)=NULL, unsigned req_perms=MM_PROT_READ) const
 Reads a NUL-terminated string from the memory map. More...
 
ExtentMap va_extents () const
 Returns just the virtual address extents for a memory map. More...
 
void mprotect (Extent range, unsigned perms, bool relax=false)
 Sets protection bits for the specified address range. More...
 
void dump (const std::string &basename) const
 Dumps the entire map and its contents into a set of files. More...
 
bool load (const std::string &basename)
 Read a memory map from a set of memory dump files. More...
 
ByteOrder::Endianness get_byte_order () const
 Every map has a default byte order property which can be used by functions that read and write multi-byte values. More...
 
void set_byte_order (ByteOrder::Endianness order)
 Every map has a default byte order property which can be used by functions that read and write multi-byte values. More...
 
bool exists (rose_addr_t va, unsigned required_perms=0) const
 Determines whether a virtual address is defined. More...
 
bool exists (Extent range, unsigned required_perms=0) const
 Determines whether a virtual address is defined. More...
 
size_t read (void *dst_buf, rose_addr_t start_va, size_t desired, unsigned req_perms=MM_PROT_READ) const
 Copies data from a contiguous region of the virtual address space into a user supplied buffer. More...
 
size_t read1 (void *dst_buf, rose_addr_t start_va, size_t desired, unsigned req_perms=MM_PROT_READ) const
 Copies data from a contiguous region of the virtual address space into a user supplied buffer. More...
 
size_t write (const void *src_buf, rose_addr_t start_va, size_t desired, unsigned req_perms=MM_PROT_WRITE)
 Copies data from a supplied buffer into the specified virtual addresses. More...
 
size_t write1 (const void *src_buf, rose_addr_t start_va, size_t desired, unsigned req_perms=MM_PROT_WRITE)
 Copies data from a supplied buffer into the specified virtual addresses. More...
 
void dump (FILE *, const char *prefix="") const
 Prints the contents of the map for debugging. More...
 
void dump (std::ostream &, std::string prefix="") const
 Prints the contents of the map for debugging. More...
 
void print (std::ostream &o, std::string prefix="") const
 Prints the contents of the map for debugging. More...
 

Protected Attributes

Segments p_segments
 
ByteOrder::Endianness sex
 

Friends

std::ostream & operator<< (std::ostream &, const MemoryMap &)
 

Detailed Description

An efficient mapping from an address space to stored data.

This class maps addresses in a 64-bit virtual address space to data stored in buffers. The address space is segmented into non-overlapping, contiguous regions called "segments" (see MemoryMap::Segment) and the addresses in a segment are mapped 1:1 onto data storage containers (see MemoryMap::Buffer). A MemoryMap stores pairs of address ranges and segments, and each Segment points to a Buffer.

Buffers come in a variety of kinds, all derived from MemoryMap::Buffer, and they are reference counted via Boost smart pointers. Always refer to a buffer with the MemoryMap::BufferPtr type. They should be created with various create() class methods, and they should never be explicitly freed.

Here's an example of mapping a file into an address space at virtual address 0x08040000 and then temporarily replacing the second 8k page of the file with our own data. We demonstrate using an MmapBuffer because these are very fast for large files, especially if only small parts of the file are accessed. We could have also used MemoryMap::ByteBuffer::create_from_file(), but this copies the entire file contents into a heap-allocated buffer, which is slower.

// Create and initialize the overlay data
my_data_size = 8192;
uint8_t *my_data = new uint8_t[my_data_size];
initialize(my_data, my_data_size);
// Create the two buffers: one for the file, one for the overlay data
MemoryMap::BufferPtr file_buf = MemoryMap::MmapBuffer("the_file", O_RDONLY, PROT_READ, MAP_PRIVATE);
MemoryMap::BufferPtr data_buf = MemoryMap::ByteBuffer(my_data, my_data_size);
my_data = NULL; // it is now owned by data_buf and will be deleted automatically
// Create the memory map.
map.insert(Extent(0x08040000, file_buf->size()),
MemoryMap::Segment(file_buf, 0, MemoryMap::MM_PROT_READ, "the file contents"));
map.insert(Extent(0x08042000, data_buf->size()),
MemoryMap::Segment(data_buf, 0, MemoryMap::MM_PROT_RW, "data overlay"));

A MemoryMap provides methods to easily read from and write to the underlying data storage, addressing it in terms of the virtual address space. These functions return the number of bytes copied.

// read part of the data, right across the file/overlay boundary
uint8_t data[4096];
size_t nread = map.read(data, 0x0804ff00, sizeof data);
assert(nread==sizeof data);

A MemoryMap is built on top of a RangeMap<Extent,Segment> and the map is available with via the segments() method. Therefore, all the usual RangeMap operations are available. For instance, here's one way to determine if there's a large free area at 0xc0000000:

// The set of addresses that are unmapped
ExtentMap free_areas = map.segments().invert<ExtentMap>();
bool b = free_areas.contains(Extent(0xc0000000,0x20000000));

Definition at line 70 of file MemoryMap.h.

Member Typedef Documentation

typedef boost::shared_ptr<Buffer> MemoryMap::BufferPtr

Definition at line 106 of file MemoryMap.h.

Definition at line 401 of file MemoryMap.h.

Definition at line 402 of file MemoryMap.h.

Member Enumeration Documentation

Mapping permissions.

Enumerator
MM_PROT_BITS 

Bits used to indication memory region protections.

MM_PROT_READ 

Pages can be read.

MM_PROT_WRITE 

Pages can be written.

MM_PROT_EXEC 

Pages can be executed.

MM_PROT_NONE 

Pages cannot be accessed.

MM_PROT_ANY 

Any access.

MM_PROT_RW 

Read or write.

MM_PROT_RX 

Read or execute.

MM_PROT_RWX 
MM_PROT_FLAGS 

Mask of protection bits that are available for use by other layers.

MM_PROT_PRIVATE 

Pages are not shared between mapped regions.

Definition at line 74 of file MemoryMap.h.

Map copying.

Enumerator
COPY_SHALLOW 

Copy segments, but not the buffers to which they point.

COPY_DEEP 

Copy segments and their buffers.

This copies buffer data.

COPY_ON_WRITE 

Copy segments and mark them so they copy buffers on write.

Definition at line 96 of file MemoryMap.h.

Constructor & Destructor Documentation

MemoryMap::MemoryMap ( )
inline

Constructs an empty memory map.

Definition at line 490 of file MemoryMap.h.

MemoryMap::MemoryMap ( const MemoryMap other,
CopyLevel  copy_level = COPY_SHALLOW 
)
inline

Shallow copy constructor.

The new memory map describes the same mapping and points to shared copies of the underlying data. In other words, changing the mapping of one map (clear(), insert(), erase()) does not change the mapping of the other, but changing the data (write()) in one map changes it in the other. See also init(), which takes an argument describing how to copy.

Definition at line 496 of file MemoryMap.h.

References init().

Member Function Documentation

MemoryMap & MemoryMap::init ( const MemoryMap source,
CopyLevel  copy_level = COPY_SHALLOW 
)

Initialize this memory map with info from another.

This map is first cleared and then initialized with a copy of the source map. A reference to this map is returned for convenience since init is often used in conjunction with constructors.

Definition at line 578 of file MemoryMap.C.

References RangeMap< R, T >::begin(), COPY_DEEP, COPY_ON_WRITE, COPY_SHALLOW, RangeMap< R, T >::end(), p_segments, and sex.

Referenced by MemoryMap().

bool MemoryMap::empty ( ) const
inline

Determines if a memory map is empty.

Returns true if this memory map contains no mappings. Returns false if at least one address is mapped.

Definition at line 508 of file MemoryMap.h.

References RangeMap< R, T >::empty(), and p_segments.

Referenced by MemoryMap::Segment::merge_names(), and MemoryMap::Segment::print().

void MemoryMap::clear ( )

Clear the entire memory map by erasing all addresses that are defined.

Definition at line 572 of file MemoryMap.C.

References RangeMap< R, T >::clear(), and p_segments.

Referenced by load(), and Partitioner::set_map().

ByteOrder::Endianness MemoryMap::get_byte_order ( ) const
inline

Every map has a default byte order property which can be used by functions that read and write multi-byte values.

The default byte order is little-endian.

Definition at line 519 of file MemoryMap.h.

References sex.

void MemoryMap::set_byte_order ( ByteOrder::Endianness  order)
inline

Every map has a default byte order property which can be used by functions that read and write multi-byte values.

The default byte order is little-endian.

Definition at line 520 of file MemoryMap.h.

References sex.

void MemoryMap::insert ( const Extent range,
const Segment segment,
bool  erase_prior = true 
)

Define a new area of memory.

The segment is copied into the memory map and the reference count for the Buffer to which it points (if any) is incremented. A check is performed to ensure that the range and segment are compatible (that the size of the range is not greater than the size of the segment's underlying buffer). This operation is somewhat like the POSIX mmap() function.

If the range overlaps with existing segments and erase_prior is set (the default), then the overlapping parts of the virtual address space are first removed from the mapping. Otherwise an overlap throws a MemoryMap::Inconsistent exception. If an exception is thrown, then the memory map is not changed.

Definition at line 611 of file MemoryMap.C.

References RangeMap< R, T >::end(), Range< T >::first(), RangeMap< R, T >::insert(), RangeMap< R, T >::lower_bound(), Range< T >::overlaps(), and p_segments.

Referenced by Disassembler::disassembleBlock(), Disassembler::disassembleBuffer(), Disassembler::disassembleOne(), Disassembler::disassembleSection(), insert_file(), load(), and Partitioner::mark_ipd_configuration().

size_t MemoryMap::insert_file ( const std::string &  filename,
rose_addr_t  va,
bool  writable = false,
bool  erase_prior = true,
const std::string &  sgmtname = "" 
)

Insert the contents of a file into the memory map at the specified address.

This is just a convenience wrapper that creates a new MmapBuffer and inserts it into the mapping. Returns the size of the file mapping.

Definition at line 625 of file MemoryMap.C.

References MemoryMap::MmapBuffer::create(), insert(), MM_PROT_READ, and MM_PROT_RW.

bool MemoryMap::exists ( rose_addr_t  va,
unsigned  required_perms = 0 
) const
inline

Determines whether a virtual address is defined.

Returns true if the specified virtual address (or all addresses in a range of addresses) are defined, false otherwise. An address is defined if it is associated with a Segment. If required_perms is non-zero, then the address (or all addresses in the range) must be mapped with at least those permission bits.

Definition at line 543 of file MemoryMap.h.

References exists().

Referenced by SgAsmPEFileHeader::create_table_sections(), Disassembler::disassembleBuffer(), exists(), SgAsmGenericFile::read_content(), Disassembler::search_following(), Disassembler::search_function_symbols(), Disassembler::search_immediate(), and Disassembler::search_words().

bool MemoryMap::exists ( Extent  range,
unsigned  required_perms = 0 
) const

Determines whether a virtual address is defined.

Returns true if the specified virtual address (or all addresses in a range of addresses) are defined, false otherwise. An address is defined if it is associated with a Segment. If required_perms is non-zero, then the address (or all addresses in the range) must be mapped with at least those permission bits.

Definition at line 648 of file MemoryMap.C.

References Range< T >::begins_before(), RangeMap< R, T >::contains(), Range< T >::empty(), RangeMap< R, T >::end(), RangeMap< R, T >::find(), Range< T >::first(), MemoryMap::Segment::get_mapperms(), Range< rose_addr_t >::inin(), Range< T >::last(), and p_segments.

void MemoryMap::erase ( const Extent range)

Erase parts of the mapping that correspond to the specified virtual address range.

The addresses to be erased don't necessarily need to correspond to a similar add() call; for instance, it's possible to add a large address space and then erase parts of it to make holes. This operation is somewhat like the POSIX munmap() function.

It is not an error to erase parts of the virtual address space that are not defined. Note that it is more efficient to call clear() than to erase the entire virtual address space.

Definition at line 671 of file MemoryMap.C.

References RangeMap< R, T >::erase(), and p_segments.

Referenced by erase(), erase_zeros(), and Partitioner::mark_ipd_configuration().

void MemoryMap::erase ( const Segment segment)

Erase a single extent from a memory map.

Erasing by range is more efficient (O(ln N) vs O(N)) but sometimes it's more convenient to erase a single segment when we don't know it's range.

Definition at line 677 of file MemoryMap.C.

References RangeMap< R, T >::begin(), RangeMap< R, T >::end(), erase(), and p_segments.

std::pair< Extent, MemoryMap::Segment > MemoryMap::at ( rose_addr_t  va) const

Get information about an address.

The return value is a pair containing the range of virtual addresses in the segment and a copy of the Segment object. If the value is not found, then a RangeMap::NotMapped exception is thrown. See also, exists().

Definition at line 688 of file MemoryMap.C.

References RangeMap< R, T >::end(), RangeMap< R, T >::find(), and p_segments.

Referenced by SgAsmPEFileHeader::create_table_sections(), MemoryMap::Buffer::is_zero(), and SgAsmGenericFile::read_content().

rose_addr_t MemoryMap::find_free ( rose_addr_t  start_va,
size_t  size,
rose_addr_t  mem_alignment = 1 
) const

Search for free space in the mapping.

This is done by looking for the lowest possible address not less than start_va and with the specified alignment where there are at least size free bytes. Throws a MemoryMap::NoFreeSpace exception if the search fails to find free space.

Definition at line 697 of file MemoryMap.C.

References ALIGN_UP, RangeMap< R, T >::end(), Range< T >::first(), RangeMap< R, T >::first_fit(), RangeMap< R, T >::invert(), Range< T >::last(), RangeMap< R, T >::lower_bound(), and p_segments.

Referenced by Partitioner::mark_ipd_configuration().

rose_addr_t MemoryMap::find_last_free ( rose_addr_t  max = (rose_addr_t)(-1)) const

Finds the highest area of unmapped addresses.

The return value is the starting address of the highest contiguous region of unmapped address space that starts at or below the specified maximum. If no unmapped region exists then a MemoryMap::NoFreeSpace exception is thrown.

Definition at line 713 of file MemoryMap.C.

References RangeMap< R, T >::end(), RangeMap< R, T >::find_prior(), RangeMap< R, T >::invert(), and p_segments.

void MemoryMap::traverse ( Visitor visitor) const

Traverses the segments of a map.

The visitor is called for each segment. The visitor's return value is ignored.

Definition at line 723 of file MemoryMap.C.

References RangeMap< R, T >::begin(), RangeMap< R, T >::end(), and p_segments.

Referenced by Disassembler::search_words().

void MemoryMap::prune ( Visitor predicate)

Removes segments for which predicate returns true.

If the predicate always returns false then nothing is removed from the map, and the predicate can be used for its side effect (such as counting how many map segments satisfy certain criteria.

Definition at line 730 of file MemoryMap.C.

References RangeMap< R, T >::begin(), RangeMap< R, T >::end(), RangeMap< R, T >::erase(), RangeMap< R, T >::insert(), and p_segments.

Referenced by Partitioner::post_cfg(), prune(), and Partitioner::set_map().

void MemoryMap::prune ( unsigned  required,
unsigned  prohibited = MM_PROT_NONE 
)

Removes segments based on permissions.

Keeps segments that have any of the required bits and none of the prohibited bits. No bits are required if required is zero.

Definition at line 743 of file MemoryMap.C.

References MemoryMap::Segment::get_mapperms(), prune(), and segment.

void MemoryMap::erase_zeros ( size_t  minsize)

Erases regions of zero bytes that are executable and readable and at least minsize in size.

Definition at line 923 of file MemoryMap.C.

References RangeMap< R, T >::begin(), RangeMap< R, T >::end(), erase(), Range< T >::first(), MemoryMap::Segment::get_buffer(), MemoryMap::Segment::get_mapperms(), RangeMap< R, T >::insert(), MM_PROT_EXEC, p_segments, read(), segment, and Range< T >::size().

const Segments& MemoryMap::segments ( ) const
inline

List of map segments.

Definition at line 590 of file MemoryMap.h.

References p_segments.

Referenced by Disassembler::mark_referenced_instructions(), and Disassembler::search_next_address().

size_t MemoryMap::read ( void *  dst_buf,
rose_addr_t  start_va,
size_t  desired,
unsigned  req_perms = MM_PROT_READ 
) const

Copies data from a contiguous region of the virtual address space into a user supplied buffer.

The portion of the virtual address space to copy begins at start_va and continues for desired bytes. The data is copied into the beginning of the dst_buf buffer. The return value is the number of bytes that were copied, which might be fewer than the number of bytes desired if the mapping does not include part of the address space requested or part of the address space does not have MM_PROT_READ permission (or the specified permissions). The dst_buf bytes that do not correpond to mapped virtual addresses will be zero filled so that desired bytes are always initialized.

If dst_buf is the null pointer then this method only measures how many bytes could have been read.

The read() and read1() methods behave identically except read1() restricts the readable area to be from a single segment. Thus, read1() may return fewer bytes than read().

Definition at line 777 of file MemoryMap.C.

References read1().

Referenced by MemoryMap::ByteBuffer::create_from_file(), DisassemblerArm::disassembleOne(), DisassemblerX86::disassembleOne(), erase_zeros(), MemoryMap::Buffer::is_zero(), Partitioner::FindDataPadding::operator()(), SgAsmPEImportDirectory::parse(), and MemoryMap::Buffer::save().

size_t MemoryMap::read1 ( void *  dst_buf,
rose_addr_t  start_va,
size_t  desired,
unsigned  req_perms = MM_PROT_READ 
) const

Copies data from a contiguous region of the virtual address space into a user supplied buffer.

The portion of the virtual address space to copy begins at start_va and continues for desired bytes. The data is copied into the beginning of the dst_buf buffer. The return value is the number of bytes that were copied, which might be fewer than the number of bytes desired if the mapping does not include part of the address space requested or part of the address space does not have MM_PROT_READ permission (or the specified permissions). The dst_buf bytes that do not correpond to mapped virtual addresses will be zero filled so that desired bytes are always initialized.

If dst_buf is the null pointer then this method only measures how many bytes could have been read.

The read() and read1() methods behave identically except read1() restricts the readable area to be from a single segment. Thus, read1() may return fewer bytes than read().

Definition at line 757 of file MemoryMap.C.

References MemoryMap::Segment::check(), Range< T >::contains(), RangeMap< R, T >::end(), RangeMap< R, T >::find(), Range< T >::first(), MemoryMap::Segment::get_buffer(), MemoryMap::Segment::get_buffer_offset(), MemoryMap::Segment::get_mapperms(), Range< T >::last(), p_segments, and segment.

Referenced by read(), SgAsmGenericFile::read_content(), read_string(), and Disassembler::search_words().

SgUnsignedCharList MemoryMap::read ( rose_addr_t  start_va,
size_t  desired,
unsigned  req_perms = MM_PROT_READ 
) const

Reads data from a memory map.

Reads data beginning at the start_va virtual address in the memory map and continuing for up to desired bytes, returning the result as an SgUnsignedCharList. The read may be shorter than requested if we reach a point in the memory map that is not defined or which does not have the requested permissions. The size of the return value indicates that number of bytes that were read (i.e., the return value is not zero-filled).

Definition at line 791 of file MemoryMap.C.

References read1().

std::string MemoryMap::read_string ( rose_addr_t  start_va,
size_t  desired,
int(*)(int)  valid_char = NULL,
int(*)(int)  invalid_char = NULL,
unsigned  req_perms = MM_PROT_READ 
) const

Reads a NUL-terminated string from the memory map.

Reads data beginning at start_va in the memory map and continuing until one of the following conditions:

  • The return value contains the desired number of characters.
  • The next character to be read is a NUL character.
  • A valid_char function is specified but the next character causes it to return zero.
  • An invalid_char function is specified and the next character causes it to return non-zero.

The valid_char and invalid_char take an integer argument and return an integer value so that the C character classification functions from <ctype.h> can be used directly.

Definition at line 810 of file MemoryMap.C.

References read1().

size_t MemoryMap::write ( const void *  src_buf,
rose_addr_t  start_va,
size_t  desired,
unsigned  req_perms = MM_PROT_WRITE 
)

Copies data from a supplied buffer into the specified virtual addresses.

If part of the destination address space is not mapped, then all bytes up to that location are copied and no additional bytes are copied. The write is also aborted early if a segment lacks the MM_PROT_WRITE bit (or specified bits) or its buffer is marked as read-only. The return value is the number of bytes copied.

If src_buf is the null pointer then this method only measures how many bytes could have been written.

The write() and write1() methods behave identically, except write1() restricts the operation to a single segment. Thus, write1() may write fewer bytes than write().

Definition at line 861 of file MemoryMap.C.

References write1().

size_t MemoryMap::write1 ( const void *  src_buf,
rose_addr_t  start_va,
size_t  desired,
unsigned  req_perms = MM_PROT_WRITE 
)

Copies data from a supplied buffer into the specified virtual addresses.

If part of the destination address space is not mapped, then all bytes up to that location are copied and no additional bytes are copied. The write is also aborted early if a segment lacks the MM_PROT_WRITE bit (or specified bits) or its buffer is marked as read-only. The return value is the number of bytes copied.

If src_buf is the null pointer then this method only measures how many bytes could have been written.

The write() and write1() methods behave identically, except write1() restricts the operation to a single segment. Thus, write1() may write fewer bytes than write().

Definition at line 830 of file MemoryMap.C.

References RangeMap< R, T >::begin(), Range< T >::contains(), RangeMap< R, T >::end(), RangeMap< R, T >::find(), Range< T >::first(), p_segments, and segment.

Referenced by write().

ExtentMap MemoryMap::va_extents ( ) const

Returns just the virtual address extents for a memory map.

Definition at line 874 of file MemoryMap.C.

References p_segments.

Referenced by Partitioner::scan_unassigned_bytes().

void MemoryMap::mprotect ( Extent  range,
unsigned  perms,
bool  relax = false 
)

Sets protection bits for the specified address range.

The entire address range must already be mapped, but if relax is set then no exception is thrown if part of the range is not mapped (that part is just ignored). This operation is somewhat like the POSIX mprotect() function.

Definition at line 880 of file MemoryMap.C.

References Range< T >::begins_before(), Range< T >::contains(), Range< T >::empty(), RangeMap< R, T >::end(), Range< T >::first(), MemoryMap::Segment::get_buffer_offset(), Range< rose_addr_t >::inin(), RangeMap< R, T >::insert(), Range< T >::last(), RangeMap< R, T >::lower_bound(), p_segments, segment, MemoryMap::Segment::set_buffer_offset(), and MemoryMap::Segment::set_mapperms().

void MemoryMap::dump ( FILE *  f,
const char *  prefix = "" 
) const

Prints the contents of the map for debugging.

The prefix string is added to the beginning of every line of output and typically is used to indent the output.

Definition at line 960 of file MemoryMap.C.

Referenced by SgAsmPEExportDirectory::ctor(), Disassembler::disassembleInterp(), SgAsmPEFileHeader::dump(), Partitioner::mark_ipd_configuration(), SgAsmPEExportSection::parse(), SgAsmPEImportDirectory::parse(), SgAsmPEImportDirectory::parse_ilt_iat(), and print().

void MemoryMap::dump ( std::ostream &  out,
std::string  prefix = "" 
) const

Prints the contents of the map for debugging.

The prefix string is added to the beginning of every line of output and typically is used to indent the output.

Definition at line 968 of file MemoryMap.C.

References StringUtility::addrToString(), RangeMap< R, T >::begin(), RangeMap< R, T >::end(), Range< T >::first(), MemoryMap::Segment::get_buffer(), Range< T >::last(), p_segments, segment, Range< T >::size(), and RangeMap< R, T >::size().

void MemoryMap::print ( std::ostream &  o,
std::string  prefix = "" 
) const
inline

Prints the contents of the map for debugging.

The prefix string is added to the beginning of every line of output and typically is used to indent the output.

Definition at line 657 of file MemoryMap.h.

References dump(), and prefix.

Referenced by operator<<().

void MemoryMap::dump ( const std::string &  basename) const

Dumps the entire map and its contents into a set of files.

The file names are constructed from the basename by appending a hypen and a hexadecimal address (without the leading "0x") and the extension ".data". The text file whose name is constructed by appending ".index" to the basename contains an index of the memory map.

Definition at line 992 of file MemoryMap.C.

References StringUtility::addrToString(), RangeMap< R, T >::begin(), RangeMap< R, T >::end(), Range< T >::first(), MemoryMap::Segment::get_buffer(), p_segments, and segment.

bool MemoryMap::load ( const std::string &  basename)

Read a memory map from a set of memory dump files.

The argument should be the same basename that was given to an invocation of the dump() method. The memory map is adjusted according to the contents of the index file. Returns true if the data was successfully read in its entirety; note that when returning false, this memory map object might be partially changed (although still in a consistent state).

This method also understands a more user-friendly dump index format. Each line of the index is either blank (containing only white space), a comment (introduced with a '#') or a segment specification. A segment specification contains the following fields separated by white space (and/or a comma):

  • The virtual address for the start of this memory area.
  • The size of this memory area in bytes.
  • Mapping permissions consisting of the letters "r" (read), "w" (write), "x" (execute), or "p" (private). Hyphens also be present in this field and do not affect the permissions.
  • The source of the data. This field consists of everything up to the next "0x" string (but leading and trailing white space is stripped). It may be the name of a file or the name of a buffer. Buffer names are only used for debugging.
  • The byte offset of the start of data within the file. It allows a single file to contain multiple memory areas.
  • An optional comment which will appear as the map element name for debugging.

If an error occurs an exception is thrown.

Definition at line 1007 of file MemoryMap.C.

References clear(), MemoryMap::ByteBuffer::create_from_file(), insert(), MM_PROT_EXEC, MM_PROT_PRIVATE, MM_PROT_READ, MM_PROT_WRITE, offset, and segment.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
const MemoryMap x 
)
friend

Definition at line 30 of file MemoryMap.C.

Member Data Documentation

ByteOrder::Endianness MemoryMap::sex
protected

Definition at line 696 of file MemoryMap.h.

Referenced by get_byte_order(), init(), and set_byte_order().


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