src/blfwk/DataSource.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(_DataSource_h_)
#define _DataSource_h_
#include <vector>
#include "Value.h"
#include "smart_ptr.h"
#include "StExecutableImage.h"
namespace blfwk
{
// Forward declaration
class DataTarget;
class DataSource
{
public:
class Segment
{
public:
Segment(DataSource &source)
: m_source(source)
{
}
virtual ~Segment() {}
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer) = 0;
virtual unsigned getLength() = 0;
virtual bool hasNaturalLocation() = 0;
virtual uint32_t getBaseAddress() { return 0; }
protected:
DataSource &m_source;
};
class PatternSegment : public Segment
{
public:
PatternSegment(DataSource &source);
PatternSegment(DataSource &source, const SizedIntegerValue &pattern);
PatternSegment(DataSource &source, uint8_t pattern);
PatternSegment(DataSource &source, uint16_t pattern);
PatternSegment(DataSource &source, uint32_t pattern);
virtual bool hasNaturalLocation() { return false; }
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
virtual unsigned getLength();
inline void setPattern(const SizedIntegerValue &newPattern) { m_pattern = newPattern; }
inline SizedIntegerValue &getPattern() { return m_pattern; }
PatternSegment &operator=(const SizedIntegerValue &value)
{
m_pattern = value;
return *this;
}
protected:
SizedIntegerValue m_pattern;
};
public:
DataSource()
: m_target(0)
{
}
virtual ~DataSource() {}
inline void setTarget(DataTarget *target) { m_target = target; }
inline DataTarget *getTarget() const { return m_target; }
virtual unsigned getSegmentCount() = 0;
virtual Segment *getSegmentAt(unsigned index) = 0;
protected:
DataTarget *m_target;
};
class PatternSource : public DataSource, public DataSource::PatternSegment
{
public:
PatternSource();
PatternSource(const SizedIntegerValue &value);
virtual unsigned getSegmentCount() { return 1; }
virtual DataSource::Segment *getSegmentAt(unsigned index) { return this; }
PatternSource &operator=(const SizedIntegerValue &value)
{
setPattern(value);
return *this;
}
};
class UnmappedDataSource : public DataSource, public DataSource::Segment
{
public:
UnmappedDataSource();
UnmappedDataSource(const uint8_t *data, unsigned length);
void setData(const uint8_t *data, unsigned length);
virtual unsigned getSegmentCount() { return 1; }
virtual DataSource::Segment *getSegmentAt(unsigned index) { return this; }
virtual bool hasNaturalLocation() { return false; }
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
virtual unsigned getLength() { return m_length; }
virtual uint32_t getBaseAddress();
protected:
smart_array_ptr<uint8_t> m_data;
unsigned m_length;
};
class MemoryImageDataSource : public DataSource
{
public:
MemoryImageDataSource(StExecutableImage *image);
virtual ~MemoryImageDataSource();
virtual unsigned getSegmentCount();
virtual DataSource::Segment *getSegmentAt(unsigned index);
protected:
class TextSegment : public DataSource::Segment
{
public:
TextSegment(MemoryImageDataSource &source, StExecutableImage *image, unsigned index);
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
virtual unsigned getLength();
virtual bool hasNaturalLocation() { return true; }
virtual uint32_t getBaseAddress();
protected:
StExecutableImage *m_image;
unsigned m_index;
};
class FillSegment : public DataSource::PatternSegment
{
public:
FillSegment(MemoryImageDataSource &source, StExecutableImage *image, unsigned index);
virtual unsigned getLength();
virtual bool hasNaturalLocation() { return true; }
virtual uint32_t getBaseAddress();
protected:
StExecutableImage *m_image;
unsigned m_index;
};
protected:
StExecutableImage *m_image;
typedef std::vector<DataSource::Segment *> segment_array_t;
segment_array_t m_segments;
};
}; // namespace blfwk
#endif // _DataSource_h_
blfwk::DataSource::getTarget
DataTarget * getTarget() const
Gets the associated data target.
Definition: src/blfwk/DataSource.h:171
blfwk::MemoryImageDataSource::FillSegment::m_index
unsigned m_index
Record index.
Definition: src/blfwk/DataSource.h:314
blfwk::UnmappedDataSource::getLength
virtual unsigned getLength()
Returns the number of bytes of data managed by the source.
Definition: src/blfwk/DataSource.h:247
blfwk::DataSource::Segment::getData
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer)=0
Gets all or a portion of the segment&#39;s data.
blfwk
Definition: BlfwkErrors.h:16
blfwk::PatternSource
Data source for a repeating pattern.
Definition: src/blfwk/DataSource.h:196
blfwk::MemoryImageDataSource::m_segments
segment_array_t m_segments
The array of Segment instances.
Definition: src/blfwk/DataSource.h:321
blfwk::DataSource::Segment::~Segment
virtual ~Segment()
Destructor.
Definition: src/blfwk/DataSource.h:75
blfwk::DataSource::setTarget
void setTarget(DataTarget *target)
Sets the associated data target.
Definition: src/blfwk/DataSource.h:169
blfwk::DataSource::getSegmentCount
virtual unsigned getSegmentCount()=0
Returns the number of segments in this data source.
smart_array_ptr< uint8_t >
blfwk::UnmappedDataSource::m_length
unsigned m_length
Byte count of the data.
Definition: src/blfwk/DataSource.h:255
blfwk::DataSource::Segment::getBaseAddress
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: src/blfwk/DataSource.h:95
blfwk::DataSource::m_target
DataTarget * m_target
Corresponding target for this source.
Definition: src/blfwk/DataSource.h:184
blfwk::PatternSource::operator=
PatternSource & operator=(const SizedIntegerValue &value)
Assignment operator, sets the pattern value and length.
Definition: src/blfwk/DataSource.h:210
blfwk::DataSource::PatternSegment::operator=
PatternSegment & operator=(const SizedIntegerValue &value)
Assignment operator, sets the pattern value and length.
Definition: src/blfwk/DataSource.h:146
blfwk::MemoryImageDataSource::TextSegment::m_image
StExecutableImage * m_image
Coalesced image of the file.
Definition: src/blfwk/DataSource.h:295
blfwk::MemoryImageDataSource::TextSegment::m_index
unsigned m_index
Record index.
Definition: src/blfwk/DataSource.h:296
blfwk::DataTarget
Abstract base class for the target address or range of data.
Definition: src/blfwk/DataTarget.h:57
StExecutableImage
Used to build a representation of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:27
blfwk::DataSource::getSegmentAt
virtual Segment * getSegmentAt(unsigned index)=0
Returns segment number index of the data source.
blfwk::MemoryImageDataSource::TextSegment::hasNaturalLocation
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: src/blfwk/DataSource.h:291
blfwk::DataSource::PatternSegment::m_pattern
SizedIntegerValue m_pattern
The fill pattern.
Definition: src/blfwk/DataSource.h:154
blfwk::DataSource::Segment::hasNaturalLocation
virtual bool hasNaturalLocation()=0
Returns whether the segment has an associated address.
blfwk::SizedIntegerValue
Adds a word size attribute to IntegerValue.
Definition: src/blfwk/Value.h:95
blfwk::DataSource::Segment::Segment
Segment(DataSource &source)
Default constructor.
Definition: src/blfwk/DataSource.h:69
blfwk::MemoryImageDataSource::TextSegment
Segment corresponding to a text region of the executable image.
Definition: src/blfwk/DataSource.h:282
blfwk::MemoryImageDataSource::m_image
StExecutableImage * m_image
The memory image that is the data source.
Definition: src/blfwk/DataSource.h:318
blfwk::UnmappedDataSource
Data source for data that is not memory mapped (has no natural address).
Definition: src/blfwk/DataSource.h:223
blfwk::PatternSource::getSegmentCount
virtual unsigned getSegmentCount()
There is only one segment.
Definition: src/blfwk/DataSource.h:206
blfwk::DataSource::DataSource
DataSource()
Default constructor.
Definition: src/blfwk/DataSource.h:159
blfwk::PatternSource::getSegmentAt
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns this object, as it is its own segment.
Definition: src/blfwk/DataSource.h:208
blfwk::MemoryImageDataSource::FillSegment
Segment corresponding to a fill region of the executable image.
Definition: src/blfwk/DataSource.h:302
blfwk::DataSource::Segment
Discrete, contiguous part of the source&#39;s data.
Definition: src/blfwk/DataSource.h:65
blfwk::MemoryImageDataSource::segment_array_t
std::vector< DataSource::Segment * > segment_array_t
An array of segments.
Definition: src/blfwk/DataSource.h:320
blfwk::UnmappedDataSource::getSegmentCount
virtual unsigned getSegmentCount()
There is only one segment.
Definition: src/blfwk/DataSource.h:236
blfwk::MemoryImageDataSource
Data source that takes its data from an executable image.
Definition: src/blfwk/DataSource.h:263
blfwk::MemoryImageDataSource::FillSegment::m_image
StExecutableImage * m_image
Coalesced image of the file.
Definition: src/blfwk/DataSource.h:313
blfwk::DataSource::PatternSegment::getPattern
SizedIntegerValue & getPattern()
Return the fill pattern for the segment.
Definition: src/blfwk/DataSource.h:144
blfwk::DataSource::Segment::m_source
DataSource & m_source
The data source to which this segment belongs.
Definition: src/blfwk/DataSource.h:97
blfwk::DataSource::~DataSource
virtual ~DataSource()
Destructor.
Definition: src/blfwk/DataSource.h:165
blfwk::UnmappedDataSource::getSegmentAt
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns this object, as it is its own segment.
Definition: src/blfwk/DataSource.h:238
blfwk::DataSource
Abstract base class for data sources.
Definition: src/blfwk/DataSource.h:56
blfwk::MemoryImageDataSource::FillSegment::hasNaturalLocation
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: src/blfwk/DataSource.h:309
blfwk::UnmappedDataSource::m_data
smart_array_ptr< uint8_t > m_data
The data.
Definition: src/blfwk/DataSource.h:254
blfwk::DataSource::Segment::getLength
virtual unsigned getLength()=0
Gets the length of the segment&#39;s data.
blfwk::DataSource::PatternSegment
This is a special type of segment containing a repeating pattern.
Definition: src/blfwk/DataSource.h:110