src/blfwk/StExecutableImage.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(_StExecutableImage_h_)
#define _StExecutableImage_h_
#include "blfwk/stdafx.h"
#include <list>
class StExecutableImage
{
public:
typedef enum
{
TEXT_REGION,
FILL_REGION
} MemoryRegionType;
enum
{
REGION_READ_FLAG = 1,
REGION_WRITE_FLAG = 2,
REGION_EXEC_FLAG = 4,
REGION_RW_FLAG = REGION_READ_FLAG | REGION_WRITE_FLAG,
REGION_PERM_FLAG_MASK = 0x7
};
struct MemoryRegion
{
MemoryRegionType m_type;
uint32_t m_address;
uint32_t m_length;
uint8_t *m_data;
unsigned m_flags;
inline uint32_t endAddress() const { return m_address + m_length - 1; }
bool operator==(const MemoryRegion &other) const;
};
typedef std::list<MemoryRegion> MemoryRegionList;
typedef MemoryRegionList::const_iterator const_iterator;
typedef enum
{
ADDR_FILTER_NONE,
ADDR_FILTER_ERROR,
ADDR_FILTER_WARNING,
ADDR_FILTER_CROP
} AddressFilterAction;
struct AddressFilter
{
AddressFilterAction m_action;
uint32_t m_fromAddress;
uint32_t m_toAddress;
unsigned m_priority;
AddressFilter(AddressFilterAction action, uint32_t from, uint32_t to, unsigned priority = 0)
: m_action(action)
, m_fromAddress(from)
, m_toAddress(to)
, m_priority(priority)
{
}
bool matchesMemoryRegion(const MemoryRegion &region) const;
int compare(const AddressFilter &other) const;
inline bool operator<(const AddressFilter &other) const { return compare(other) == -1; }
inline bool operator>(const AddressFilter &other) const { return compare(other) == 1; }
inline bool operator==(const AddressFilter &other) const { return compare(other) == 0; }
inline bool operator<=(const AddressFilter &other) const { return compare(other) != 1; }
inline bool operator>=(const AddressFilter &other) const { return compare(other) != -1; }
};
typedef std::list<AddressFilter> AddressFilterList;
class address_filter_exception
{
public:
address_filter_exception(bool isError, std::string &imageName, const AddressFilter &matchingFilter)
: m_isError(isError)
, m_imageName(imageName)
, m_filter(matchingFilter)
{
}
inline bool isError() const { return m_isError; }
inline std::string getImageName() const { return m_imageName; }
inline const AddressFilter &getMatchingFilter() const { return m_filter; }
protected:
bool m_isError;
std::string m_imageName;
AddressFilter m_filter;
};
public:
StExecutableImage(int inAlignment = 256);
StExecutableImage(const StExecutableImage &inOther);
virtual ~StExecutableImage();
virtual void setName(const std::string &inName);
virtual std::string getName() const;
virtual void addFillRegion(uint32_t inAddress, unsigned inLength);
virtual void addTextRegion(uint32_t inAddress, const uint8_t *inData, unsigned inLength);
inline unsigned getRegionCount() const { return static_cast<unsigned>(m_image.size()); }
const MemoryRegion &getRegionAtIndex(unsigned inIndex) const;
inline const_iterator getRegionBegin() const { return m_image.begin(); }
inline const_iterator getRegionEnd() const { return m_image.end(); }
inline void setEntryPoint(uint32_t inEntryAddress)
{
m_entry = inEntryAddress;
m_hasEntry = true;
}
inline bool hasEntryPoint() const { return m_hasEntry; }
inline uint32_t getEntryPoint() const { return hasEntryPoint() ? m_entry : 0; }
virtual void addAddressFilter(const AddressFilter &filter);
template <typename _T>
void addAddressFilters(_T from, _T to)
{
_T it = from;
for (; it != to; ++it)
{
addAddressFilter(*it);
}
}
virtual void clearAddressFilters();
virtual void applyAddressFilters();
protected:
std::string m_name;
int m_alignment;
bool m_hasEntry;
uint32_t m_entry;
MemoryRegionList m_image;
AddressFilterList m_filters;
void cropRegionToFilter(MemoryRegion &region, const AddressFilter &filter);
void insertOrMergeRegion(MemoryRegion &inRegion);
void mergeRegions(MemoryRegion &inOldRegion, MemoryRegion &inNewRegion);
};
#endif // _StExecutableImage_h_
StExecutableImage::MemoryRegion::m_type
MemoryRegionType m_type
Memory region type.
Definition: apps/elftosb/common/StExecutableImage.h:57
StExecutableImage::getRegionBegin
const_iterator getRegionBegin() const
Return an iterator to the first region.
Definition: src/blfwk/StExecutableImage.h:215
StExecutableImage::addAddressFilter
virtual void addAddressFilter(const AddressFilter &filter)
Add a new address filter.
Definition: apps/elftosb/common/StExecutableImage.cpp:129
StExecutableImage::addFillRegion
virtual void addFillRegion(uint32_t inAddress, unsigned inLength)
Add a region to be filled with zeroes.
Definition: apps/elftosb/common/StExecutableImage.cpp:74
StExecutableImage::MemoryRegion::m_length
uint32_t m_length
Number of bytes in this region.
Definition: apps/elftosb/common/StExecutableImage.h:59
StExecutableImage::getRegionEnd
const_iterator getRegionEnd() const
Return an iterator to the next-after-last region.
Definition: src/blfwk/StExecutableImage.h:217
StExecutableImage::StExecutableImage
StExecutableImage(int inAlignment=256)
Constructor.
Definition: apps/elftosb/common/StExecutableImage.cpp:14
StExecutableImage::MemoryRegion::m_flags
unsigned m_flags
Flags for the region.
Definition: apps/elftosb/common/StExecutableImage.h:61
StExecutableImage::REGION_READ_FLAG
Region is readable.
Definition: apps/elftosb/common/StExecutableImage.h:40
StExecutableImage::TEXT_REGION
A region containing data or instructions.
Definition: apps/elftosb/common/StExecutableImage.h:33
StExecutableImage::~StExecutableImage
virtual ~StExecutableImage()
Destructor.
Definition: apps/elftosb/common/StExecutableImage.cpp:46
StExecutableImage::clearAddressFilters
virtual void clearAddressFilters()
Remove all active filters.
Definition: apps/elftosb/common/StExecutableImage.cpp:136
StExecutableImage::address_filter_exception
Definition: apps/elftosb/common/StExecutableImage.h:127
StExecutableImage::FILL_REGION
Region to be initialized with zero bytes.
Definition: apps/elftosb/common/StExecutableImage.h:34
StExecutableImage::applyAddressFilters
virtual void applyAddressFilters()
Process all active filters and perform associated actions.
Definition: apps/elftosb/common/StExecutableImage.cpp:148
StExecutableImage::m_alignment
int m_alignment
The required address alignment for each memory region.
Definition: apps/elftosb/common/StExecutableImage.h:241
StExecutableImage
Used to build a representation of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:27
StExecutableImage::getEntryPoint
uint32_t getEntryPoint() const
Returns the entry point address.
Definition: src/blfwk/StExecutableImage.h:232
StExecutableImage::hasEntryPoint
bool hasEntryPoint() const
Returns true if an entry point has been set.
Definition: src/blfwk/StExecutableImage.h:230
StExecutableImage::addTextRegion
virtual void addTextRegion(uint32_t inAddress, const uint8_t *inData, unsigned inLength)
Add a region containing data to be loaded.
Definition: apps/elftosb/common/StExecutableImage.cpp:93
StExecutableImage::m_name
std::string m_name
The name of the image (can be a file name, for instance).
Definition: apps/elftosb/common/StExecutableImage.h:240
StExecutableImage::AddressFilter
Definition: apps/elftosb/common/StExecutableImage.h:90
StExecutableImage::getRegionAtIndex
const MemoryRegion & getRegionAtIndex(unsigned inIndex) const
Returns a reference to the region specified by inIndex.
Definition: apps/elftosb/common/StExecutableImage.cpp:110
StExecutableImage::setName
virtual void setName(const std::string &inName)
Sets the image&#39;s name to inName.
Definition: apps/elftosb/common/StExecutableImage.cpp:61
StExecutableImage::ADDR_FILTER_NONE
Do nothing.
Definition: apps/elftosb/common/StExecutableImage.h:79
StExecutableImage::m_hasEntry
bool m_hasEntry
True if an entry point has been set.
Definition: apps/elftosb/common/StExecutableImage.h:242
StExecutableImage::mergeRegions
void mergeRegions(MemoryRegion &inOldRegion, MemoryRegion &inNewRegion)
Merges two memory regions into one.
Definition: apps/elftosb/common/StExecutableImage.cpp:333
StExecutableImage::AddressFilter::AddressFilter
AddressFilter(AddressFilterAction action, uint32_t from, uint32_t to, unsigned priority=0)
Constructor.
Definition: src/blfwk/StExecutableImage.h:121
StExecutableImage::addAddressFilters
void addAddressFilters(_T from, _T to)
Add multiple address filters at once.
Definition: src/blfwk/StExecutableImage.h:246
StExecutableImage::m_image
MemoryRegionList m_image
The memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:244
StExecutableImage::setEntryPoint
void setEntryPoint(uint32_t inEntryAddress)
Sets the entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:200
StExecutableImage::m_filters
AddressFilterList m_filters
List of active address filters.
Definition: apps/elftosb/common/StExecutableImage.h:245
StExecutableImage::MemoryRegion::operator==
bool operator==(const MemoryRegion &other)
Equality operator.
Definition: apps/elftosb/common/StExecutableImage.cpp:402
StExecutableImage::MemoryRegion::endAddress
uint32_t endAddress() const
Calculates the address of the last byte occupied by this region.
Definition: src/blfwk/StExecutableImage.h:87
StExecutableImage::MemoryRegion::m_address
uint32_t m_address
The 32-bit start address of this region.
Definition: apps/elftosb/common/StExecutableImage.h:58
StExecutableImage::REGION_PERM_FLAG_MASK
Mask to access only permissions flags for a region.
Definition: apps/elftosb/common/StExecutableImage.h:47
StExecutableImage::insertOrMergeRegion
void insertOrMergeRegion(MemoryRegion &inRegion)
Inserts the region in sorted order or merges with one already in the image.
Definition: apps/elftosb/common/StExecutableImage.cpp:286
StExecutableImage::address_filter_exception::address_filter_exception
address_filter_exception(bool isError, std::string &imageName, const AddressFilter &matchingFilter)
Constructor.
Definition: src/blfwk/StExecutableImage.h:157
StExecutableImage::getRegionCount
unsigned getRegionCount() const
Returns the total number of regions.
Definition: src/blfwk/StExecutableImage.h:210
StExecutableImage::MemoryRegionType
MemoryRegionType
Possible types of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:31
StExecutableImage::AddressFilterAction
AddressFilterAction
The possible actions for regions matching an address filter range.
Definition: apps/elftosb/common/StExecutableImage.h:77
StExecutableImage::address_filter_exception::isError
bool isError() const
Returns true if the exception is an error. Otherwise the exception is for a warning.
Definition: src/blfwk/StExecutableImage.h:166
StExecutableImage::MemoryRegion::m_data
uint8_t * m_data
Pointer to data. Will be NULL for FILL_REGION type.
Definition: apps/elftosb/common/StExecutableImage.h:60
StExecutableImage::cropRegionToFilter
void cropRegionToFilter(MemoryRegion &region, const AddressFilter &filter)
Deletes the portion region that overlaps filter.
Definition: apps/elftosb/common/StExecutableImage.cpp:205
StExecutableImage::MemoryRegionList
std::list< MemoryRegion > MemoryRegionList
A list of StExecutableImage::MemoryRegion objects.
Definition: src/blfwk/StExecutableImage.h:93
StExecutableImage::getName
virtual std::string getName() const
Returns a copy of the image&#39;s name.
Definition: apps/elftosb/common/StExecutableImage.cpp:66
StExecutableImage::ADDR_FILTER_WARNING
Raise a warning exception.
Definition: apps/elftosb/common/StExecutableImage.h:81
StExecutableImage::const_iterator
MemoryRegionList::const_iterator const_iterator
Definition: src/blfwk/StExecutableImage.h:97
StExecutableImage::ADDR_FILTER_CROP
Don&#39;t include the matching address range in the executable image.
Definition: apps/elftosb/common/StExecutableImage.h:82
StExecutableImage::REGION_WRITE_FLAG
Region is writable.
Definition: apps/elftosb/common/StExecutableImage.h:41
StExecutableImage::MemoryRegion
Definition: apps/elftosb/common/StExecutableImage.h:55
StExecutableImage::REGION_RW_FLAG
Region is read-write.
Definition: apps/elftosb/common/StExecutableImage.h:44
StExecutableImage::REGION_EXEC_FLAG
Region may contain executable code.
Definition: apps/elftosb/common/StExecutableImage.h:42
StExecutableImage::AddressFilterList
std::list< AddressFilter > AddressFilterList
List of StExecutableImage::AddressFilter objects.
Definition: src/blfwk/StExecutableImage.h:146
StExecutableImage::m_entry
uint32_t m_entry
Entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:243
StExecutableImage::ADDR_FILTER_ERROR
Raise an error exception.
Definition: apps/elftosb/common/StExecutableImage.h:80