summaryrefslogtreecommitdiffstats
path: root/common/DataSource.cpp
blob: 0d91dbfac0cb5e021bcdf82137f82d3a10cad43b (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * File:	DataSource.cpp
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */

#include "DataSource.h"
#include "DataTarget.h"
#include <assert.h>
#include <string.h>
using namespace elftosb;

#pragma mark *** DataSource::PatternSegment ***

DataSource::PatternSegment::PatternSegment(DataSource & source)
:	DataSource::Segment(source), m_pattern()
{
}

DataSource::PatternSegment::PatternSegment(DataSource & source, const SizedIntegerValue & pattern)
:	DataSource::Segment(source), m_pattern(pattern)
{
}

DataSource::PatternSegment::PatternSegment(DataSource & source, uint8_t pattern)
:	DataSource::Segment(source), m_pattern(static_cast<uint8_t>(pattern))
{
}

DataSource::PatternSegment::PatternSegment(DataSource & source, uint16_t pattern)
:	DataSource::Segment(source), m_pattern(static_cast<uint16_t>(pattern))
{
}

DataSource::PatternSegment::PatternSegment(DataSource & source, uint32_t pattern)
:	DataSource::Segment(source), m_pattern(static_cast<uint32_t>(pattern))
{
}

unsigned DataSource::PatternSegment::getData(unsigned /*offset*/, unsigned maxBytes, uint8_t * buffer)
{
	memset(buffer, 0, maxBytes);
	
	return maxBytes;
}

//! The pattern segment's length is a function of the data target. If the
//! target is bounded, then the segment's length is simply the target's
//! length. Otherwise, if no target has been set or the target is unbounded,
//! then the length returned is 0.
unsigned DataSource::PatternSegment::getLength()
{
	DataTarget * target = m_source.getTarget();
	if (!target)
	{
		return 0;
	}
	
	uint32_t length;
	if (target->isBounded())
	{
		length = target->getEndAddress() - target->getBeginAddress();
	}
	else
	{
		length = m_pattern.getSize();
	}
	return length;
}

#pragma mark *** PatternSource ***

PatternSource::PatternSource()
:	DataSource(), DataSource::PatternSegment((DataSource&)*this)
{
}

PatternSource::PatternSource(const SizedIntegerValue & value)
:	DataSource(), DataSource::PatternSegment((DataSource&)*this, value)
{
}

#pragma mark *** UnmappedDataSource ***

UnmappedDataSource::UnmappedDataSource()
:	DataSource(), DataSource::Segment((DataSource&)*this), m_data(), m_length(0)
{
}

UnmappedDataSource::UnmappedDataSource(const uint8_t * data, unsigned length)
:	DataSource(), DataSource::Segment((DataSource&)*this), m_data(), m_length(0)
{
	setData(data, length);
}

//! Makes a copy of \a data that is freed when the data source is
//! destroyed. The caller does not have to maintain \a data after this call
//! returns.
void UnmappedDataSource::setData(const uint8_t * data, unsigned length)
{
	m_data.safe_delete();
	
	uint8_t * dataCopy = new uint8_t[length];
	memcpy(dataCopy, data, length);
	m_data = dataCopy;
	m_length = length;
}

unsigned UnmappedDataSource::getData(unsigned offset, unsigned maxBytes, uint8_t * buffer)
{
	assert(offset < m_length);
	unsigned copyBytes = std::min<unsigned>(m_length - offset, maxBytes);
	memcpy(buffer, m_data.get(), copyBytes);
	return copyBytes;
}

#pragma mark *** MemoryImageDataSource ***

MemoryImageDataSource::MemoryImageDataSource(StExecutableImage * image)
:	DataSource(), m_image(image)
{
	// reserve enough room for all segments
	m_segments.reserve(m_image->getRegionCount());
}

MemoryImageDataSource::~MemoryImageDataSource()
{
	segment_array_t::iterator it = m_segments.begin();
	for (; it != m_segments.end(); ++it)
	{
		// delete this segment if it has been created
		if (*it)
		{
			delete *it;
		}
	}
}

unsigned MemoryImageDataSource::getSegmentCount()
{
	return m_image->getRegionCount();
}

DataSource::Segment * MemoryImageDataSource::getSegmentAt(unsigned index)
{
	// return previously created segment
	if (index < m_segments.size() && m_segments[index])
	{
		return m_segments[index];
	}
	
	// extend array out to this index
	if (index >= m_segments.size() && index < m_image->getRegionCount())
	{
		m_segments.resize(index + 1, NULL);
	}
	
	// create the new segment object
	DataSource::Segment * newSegment = 0;
	const StExecutableImage::MemoryRegion & region = m_image->getRegionAtIndex(index);
	if (region.m_type == StExecutableImage::TEXT_REGION)
	{
		newSegment = new TextSegment(*this, m_image, index);
	}
	else if (region.m_type == StExecutableImage::FILL_REGION)
	{
		newSegment = new FillSegment(*this, m_image, index);
	}
	
	m_segments[index] = newSegment;
	return newSegment;
}

#pragma mark *** MemoryImageDataSource::TextSegment ***

MemoryImageDataSource::TextSegment::TextSegment(MemoryImageDataSource & source, StExecutableImage * image, unsigned index)
:	DataSource::Segment(source), m_image(image), m_index(index)
{
}

unsigned MemoryImageDataSource::TextSegment::getData(unsigned offset, unsigned maxBytes, uint8_t * buffer)
{
	const StExecutableImage::MemoryRegion & region = m_image->getRegionAtIndex(m_index);
	assert(region.m_type == StExecutableImage::TEXT_REGION);
	
	unsigned copyBytes = std::min<unsigned>(region.m_length - offset, maxBytes);	
	memcpy(buffer, &region.m_data[offset], copyBytes);
	
	return copyBytes;
}

unsigned MemoryImageDataSource::TextSegment::getLength()
{
	const StExecutableImage::MemoryRegion & region = m_image->getRegionAtIndex(m_index);
	return region.m_length;
}

uint32_t MemoryImageDataSource::TextSegment::getBaseAddress()
{
	const StExecutableImage::MemoryRegion & region = m_image->getRegionAtIndex(m_index);
	return region.m_address;
}

#pragma mark *** MemoryImageDataSource::FillSegment ***

MemoryImageDataSource::FillSegment::FillSegment(MemoryImageDataSource & source, StExecutableImage * image, unsigned index)
:	DataSource::PatternSegment(source), m_image(image), m_index(index)
{
	SizedIntegerValue zero(0, kWordSize);
	setPattern(zero);
}

unsigned MemoryImageDataSource::FillSegment::getLength()
{
	const StExecutableImage::MemoryRegion & region = m_image->getRegionAtIndex(m_index);
	return region.m_length;
}

uint32_t MemoryImageDataSource::FillSegment::getBaseAddress()
{
	const StExecutableImage::MemoryRegion & region = m_image->getRegionAtIndex(m_index);
	return region.m_address;
}