xref: /netbsd-src/external/bsd/elftosb/dist/common/DataTarget.h (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
1*993229b6Sjkunz /*
2*993229b6Sjkunz  * File:	DataTarget.h
3*993229b6Sjkunz  *
4*993229b6Sjkunz  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5*993229b6Sjkunz  * See included license file for license details.
6*993229b6Sjkunz  */
7*993229b6Sjkunz #if !defined(_DataTarget_h_)
8*993229b6Sjkunz #define _DataTarget_h_
9*993229b6Sjkunz 
10*993229b6Sjkunz #include "stdafx.h"
11*993229b6Sjkunz #include "DataSource.h"
12*993229b6Sjkunz 
13*993229b6Sjkunz namespace elftosb
14*993229b6Sjkunz {
15*993229b6Sjkunz 
16*993229b6Sjkunz // Forward declaration
17*993229b6Sjkunz class DataSource;
18*993229b6Sjkunz 
19*993229b6Sjkunz /*!
20*993229b6Sjkunz  * \brief Abstract base class for the target address or range of data.
21*993229b6Sjkunz  *
22*993229b6Sjkunz  * Targets at the most basic level have a single address, and potentially
23*993229b6Sjkunz  * an address range. Unbounded targets have a beginning address but no
24*993229b6Sjkunz  * specific end address, while bounded targets do have an end address.
25*993229b6Sjkunz  *
26*993229b6Sjkunz  * Users of a data target can access the begin and end addresses directly.
27*993229b6Sjkunz  * However, the most powerful way to use a target is with the
28*993229b6Sjkunz  * getRangeForSegment() method. This method returns the target address range
29*993229b6Sjkunz  * for a segment of a data source. The value of the resulting range can be
30*993229b6Sjkunz  * completely dependent upon the segment's properties, those of its data
31*993229b6Sjkunz  * source, and the type of data target.
32*993229b6Sjkunz  *
33*993229b6Sjkunz  * \see elftosb::DataSource
34*993229b6Sjkunz  */
35*993229b6Sjkunz class DataTarget
36*993229b6Sjkunz {
37*993229b6Sjkunz public:
38*993229b6Sjkunz 	//! \brief Simple structure that describes an addressed region of memory.
39*993229b6Sjkunz 	//! \todo Decide if the end address is inclusive or not.
40*993229b6Sjkunz 	struct AddressRange
41*993229b6Sjkunz 	{
42*993229b6Sjkunz 		uint32_t m_begin;
43*993229b6Sjkunz 		uint32_t m_end;
44*993229b6Sjkunz 	};
45*993229b6Sjkunz 
46*993229b6Sjkunz public:
47*993229b6Sjkunz 	//! \brief Default constructor.
DataTarget()48*993229b6Sjkunz 	DataTarget() : m_source(0) {}
49*993229b6Sjkunz 
50*993229b6Sjkunz 	//! \brief Destructor.
~DataTarget()51*993229b6Sjkunz 	virtual ~DataTarget() {}
52*993229b6Sjkunz 
53*993229b6Sjkunz 	//! \brief Whether the target is just a single address or has an end to it.
isBounded()54*993229b6Sjkunz 	virtual bool isBounded() { return false; }
55*993229b6Sjkunz 
getBeginAddress()56*993229b6Sjkunz 	virtual uint32_t getBeginAddress() { return 0; }
getEndAddress()57*993229b6Sjkunz 	virtual uint32_t getEndAddress() { return 0; }
58*993229b6Sjkunz 
59*993229b6Sjkunz 	//! \brief Return the address range for a segment of a data source.
60*993229b6Sjkunz 	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment)=0;
61*993229b6Sjkunz 
setSource(DataSource * source)62*993229b6Sjkunz 	inline void setSource(DataSource * source) { m_source = source; }
getSource()63*993229b6Sjkunz 	inline DataSource * getSource() const { return m_source; }
64*993229b6Sjkunz 
65*993229b6Sjkunz protected:
66*993229b6Sjkunz 	DataSource * m_source;	//!< Corresponding data source for this target.
67*993229b6Sjkunz };
68*993229b6Sjkunz 
69*993229b6Sjkunz /*!
70*993229b6Sjkunz  * \brief Target with a constant values for the addresses.
71*993229b6Sjkunz  *
72*993229b6Sjkunz  * This target type supports can be both bounded and unbounded. It always has
73*993229b6Sjkunz  * at least one address, the beginning address. The end address is optional,
74*993229b6Sjkunz  * and if not provided makes the target unbounded.
75*993229b6Sjkunz  */
76*993229b6Sjkunz class ConstantDataTarget : public DataTarget
77*993229b6Sjkunz {
78*993229b6Sjkunz public:
79*993229b6Sjkunz 	//! \brief Constructor taking only a begin address.
ConstantDataTarget(uint32_t start)80*993229b6Sjkunz 	ConstantDataTarget(uint32_t start) : DataTarget(), m_begin(start), m_end(0), m_hasEnd(false) {}
81*993229b6Sjkunz 
82*993229b6Sjkunz 	//! \brief Constructor taking both begin and end addresses.
ConstantDataTarget(uint32_t start,uint32_t end)83*993229b6Sjkunz 	ConstantDataTarget(uint32_t start, uint32_t end) : DataTarget(), m_begin(start), m_end(end), m_hasEnd(true) {}
84*993229b6Sjkunz 
85*993229b6Sjkunz 	//! \brief The target is bounded if an end address was specified.
isBounded()86*993229b6Sjkunz 	virtual bool isBounded() { return m_hasEnd; }
87*993229b6Sjkunz 
getBeginAddress()88*993229b6Sjkunz 	virtual uint32_t getBeginAddress() { return m_begin; }
getEndAddress()89*993229b6Sjkunz 	virtual uint32_t getEndAddress() { return m_end; }
90*993229b6Sjkunz 
91*993229b6Sjkunz 	//! \brief Return the address range for a segment of a data source.
92*993229b6Sjkunz 	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment);
93*993229b6Sjkunz 
94*993229b6Sjkunz protected:
95*993229b6Sjkunz 	uint32_t m_begin;	//!< Start address.
96*993229b6Sjkunz 	uint32_t m_end;		//!< End address.
97*993229b6Sjkunz 	bool m_hasEnd;		//!< Was an end address specified?
98*993229b6Sjkunz };
99*993229b6Sjkunz 
100*993229b6Sjkunz /*!
101*993229b6Sjkunz  * \brief Target address that is the "natural" location of whatever the source data is.
102*993229b6Sjkunz  *
103*993229b6Sjkunz  * The data source used with the target must have a natural location. If
104*993229b6Sjkunz  * getRangeForSegment() is called with a segment that does not have a natural
105*993229b6Sjkunz  * location, a semantic_error will be thrown.
106*993229b6Sjkunz  */
107*993229b6Sjkunz class NaturalDataTarget : public DataTarget
108*993229b6Sjkunz {
109*993229b6Sjkunz public:
110*993229b6Sjkunz 	//! \brief Default constructor.
NaturalDataTarget()111*993229b6Sjkunz 	NaturalDataTarget() : DataTarget() {}
112*993229b6Sjkunz 
113*993229b6Sjkunz 	//! \brief Natural data targets are bounded by their source's segment lengths.
isBounded()114*993229b6Sjkunz 	virtual bool isBounded() { return true; }
115*993229b6Sjkunz 
116*993229b6Sjkunz 	//! \brief Return the address range for a segment of a data source.
117*993229b6Sjkunz 	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment);
118*993229b6Sjkunz };
119*993229b6Sjkunz 
120*993229b6Sjkunz }; // namespace elftosb
121*993229b6Sjkunz 
122*993229b6Sjkunz #endif // _DataTarget_h_
123