summaryrefslogtreecommitdiffstats
path: root/common/DataTarget.h
blob: e154491d0795dfb1a61c231bfbbe7916648f171b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * File:	DataTarget.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */
#if !defined(_DataTarget_h_)
#define _DataTarget_h_

#include "stdafx.h"
#include "DataSource.h"

namespace elftosb
{

// Forward declaration
class DataSource;

/*!
 * \brief Abstract base class for the target address or range of data.
 *
 * Targets at the most basic level have a single address, and potentially
 * an address range. Unbounded targets have a beginning address but no
 * specific end address, while bounded targets do have an end address.
 *
 * Users of a data target can access the begin and end addresses directly.
 * However, the most powerful way to use a target is with the
 * getRangeForSegment() method. This method returns the target address range
 * for a segment of a data source. The value of the resulting range can be
 * completely dependent upon the segment's properties, those of its data
 * source, and the type of data target.
 *
 * \see elftosb::DataSource
 */
class DataTarget
{
public:
	//! \brief Simple structure that describes an addressed region of memory.
	//! \todo Decide if the end address is inclusive or not.
	struct AddressRange
	{
		uint32_t m_begin;
		uint32_t m_end;
	};
	
public:
	//! \brief Default constructor.
	DataTarget() : m_source(0) {}
	
	//! \brief Destructor.
	virtual ~DataTarget() {}
	
	//! \brief Whether the target is just a single address or has an end to it.
	virtual bool isBounded() { return false; }
	
	virtual uint32_t getBeginAddress() { return 0; }
	virtual uint32_t getEndAddress() { return 0; }
	
	//! \brief Return the address range for a segment of a data source.
	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment)=0;
	
	inline void setSource(DataSource * source) { m_source = source; }
	inline DataSource * getSource() const { return m_source; }
	
protected:
	DataSource * m_source;	//!< Corresponding data source for this target.
};

/*!
 * \brief Target with a constant values for the addresses.
 *
 * This target type supports can be both bounded and unbounded. It always has
 * at least one address, the beginning address. The end address is optional,
 * and if not provided makes the target unbounded.
 */
class ConstantDataTarget : public DataTarget
{
public:
	//! \brief Constructor taking only a begin address.
	ConstantDataTarget(uint32_t start) : DataTarget(), m_begin(start), m_end(0), m_hasEnd(false) {}
	
	//! \brief Constructor taking both begin and end addresses.
	ConstantDataTarget(uint32_t start, uint32_t end) : DataTarget(), m_begin(start), m_end(end), m_hasEnd(true) {}
	
	//! \brief The target is bounded if an end address was specified.
	virtual bool isBounded() { return m_hasEnd; }
	
	virtual uint32_t getBeginAddress() { return m_begin; }
	virtual uint32_t getEndAddress() { return m_end; }
	
	//! \brief Return the address range for a segment of a data source.
	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment);
	
protected:
	uint32_t m_begin;	//!< Start address.
	uint32_t m_end;		//!< End address.
	bool m_hasEnd;		//!< Was an end address specified?
};

/*!
 * \brief Target address that is the "natural" location of whatever the source data is.
 *
 * The data source used with the target must have a natural location. If
 * getRangeForSegment() is called with a segment that does not have a natural
 * location, a semantic_error will be thrown.
 */
class NaturalDataTarget : public DataTarget
{
public:
	//! \brief Default constructor.
	NaturalDataTarget() : DataTarget() {}
	
	//! \brief Natural data targets are bounded by their source's segment lengths.
	virtual bool isBounded() { return true; }
	
	//! \brief Return the address range for a segment of a data source.
	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment);
};

}; // namespace elftosb

#endif // _DataTarget_h_