-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathelfio_segment.hpp
More file actions
416 lines (366 loc) · 16.9 KB
/
Copy pathelfio_segment.hpp
File metadata and controls
416 lines (366 loc) · 16.9 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright (C) 2001-present by Serge Lamikhov-Center
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef ELFIO_SEGMENT_HPP
#define ELFIO_SEGMENT_HPP
#include <iostream>
#include <vector>
#include <new>
#include <limits>
namespace ELFIO {
//------------------------------------------------------------------------------
//! \class segment
//! \brief Class for accessing segment data
class segment
{
friend class elfio;
public:
virtual ~segment() = default;
//------------------------------------------------------------------------------
//! \brief Get the index of the segment
//! \return Index of the segment
ELFIO_GET_ACCESS_DECL( Elf_Half, index );
//------------------------------------------------------------------------------
//! \brief Get the type of the segment
//! \return Type of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
//------------------------------------------------------------------------------
//! \brief Get the flags of the segment
//! \return Flags of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags );
//------------------------------------------------------------------------------
//! \brief Get the alignment of the segment
//! \return Alignment of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align );
//------------------------------------------------------------------------------
//! \brief Get the virtual address of the segment
//! \return Virtual address of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, virtual_address );
//------------------------------------------------------------------------------
//! \brief Get the physical address of the segment
//! \return Physical address of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, physical_address );
//------------------------------------------------------------------------------
//! \brief Get the file size of the segment
//! \return File size of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size );
//------------------------------------------------------------------------------
//! \brief Get the memory size of the segment
//! \return Memory size of the segment
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size );
//------------------------------------------------------------------------------
//! \brief Get the offset of the segment
//! \return Offset of the segment
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
//------------------------------------------------------------------------------
//! \brief Get the data of the segment
//! \return Pointer to the data
virtual const char* get_data() const = 0;
//------------------------------------------------------------------------------
//! \brief Free the data of the segment
virtual void free_data() const = 0;
//------------------------------------------------------------------------------
//! \brief Add a section to the segment
//! \param psec Pointer to the section
//! \param addr_align Alignment of the section
//! \return Index of the added section
virtual Elf_Half add_section( section* psec, Elf_Xword addr_align ) = 0;
//------------------------------------------------------------------------------
//! \brief Add a section index to the segment
//! \param index Index of the section
//! \param addr_align Alignment of the section
//! \return Index of the added section
virtual Elf_Half add_section_index( Elf_Half index,
Elf_Xword addr_align ) = 0;
//------------------------------------------------------------------------------
//! \brief Get the number of sections in the segment
//! \return Number of sections in the segment
virtual Elf_Half get_sections_num() const = 0;
//------------------------------------------------------------------------------
//! \brief Get the index of a section at a given position
//! \param num Position of the section
//! \return Index of the section
virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0;
//------------------------------------------------------------------------------
//! \brief Check if the offset is initialized
//! \return True if the offset is initialized, false otherwise
virtual bool is_offset_initialized() const = 0;
//------------------------------------------------------------------------------
//! \brief Sort sections in a segment according to offset
virtual void sort_sections( std::vector<Elf64_Addr>& offsets ) = 0;
protected:
//------------------------------------------------------------------------------
//! \brief Set the offset of the segment
//! \param offset Offset of the segment
ELFIO_SET_ACCESS_DECL( Elf64_Off, offset );
//------------------------------------------------------------------------------
//! \brief Set the index of the segment
//! \param index Index of the segment
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
//------------------------------------------------------------------------------
//! \brief Get the sections of the segment
//! \return Vector of section indices
virtual const std::vector<Elf_Half>& get_sections() const = 0;
//------------------------------------------------------------------------------
//! \brief Load the segment from a stream
//! \param stream Input stream
//! \param header_offset Offset of the segment header
//! \param is_lazy Whether to load the segment lazily
//! \return True if successful, false otherwise
virtual bool load( std::istream& stream,
std::streampos header_offset,
bool is_lazy ) = 0;
//------------------------------------------------------------------------------
//! \brief Save the segment to a stream
//! \param stream Output stream
//! \param header_offset Offset of the segment header
//! \param data_offset Offset of the segment data
virtual void save( std::ostream& stream,
std::streampos header_offset,
std::streampos data_offset ) = 0;
};
//------------------------------------------------------------------------------
//! \class segment_impl
//! \brief Implementation of the segment class
template <class T> class segment_impl : public segment
{
public:
//------------------------------------------------------------------------------
//! \brief Constructor
//! \param convertor Pointer to the endianness convertor
//! \param translator Pointer to the address translator
segment_impl( std::shared_ptr<endianness_convertor> convertor,
std::shared_ptr<address_translator> translator )
: convertor( convertor ), translator( translator )
{
}
//------------------------------------------------------------------------------
// Section info functions
ELFIO_GET_SET_ACCESS( Elf_Word, type, ph.p_type );
ELFIO_GET_SET_ACCESS( Elf_Word, flags, ph.p_flags );
ELFIO_GET_SET_ACCESS( Elf_Xword, align, ph.p_align );
ELFIO_GET_SET_ACCESS( Elf64_Addr, virtual_address, ph.p_vaddr );
ELFIO_GET_SET_ACCESS( Elf64_Addr, physical_address, ph.p_paddr );
ELFIO_GET_SET_ACCESS( Elf_Xword, file_size, ph.p_filesz );
ELFIO_GET_SET_ACCESS( Elf_Xword, memory_size, ph.p_memsz );
ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset );
//------------------------------------------------------------------------------
//! \brief Get the index of the segment
//! \return Index of the segment
Elf_Half get_index() const override { return index; }
//------------------------------------------------------------------------------
//! \brief Get the data of the segment
//! \return Pointer to the data
const char* get_data() const override
{
if ( !is_loaded ) {
load_data();
}
return data.get();
}
//------------------------------------------------------------------------------
//! \brief Free the data of the segment
void free_data() const override
{
if ( is_lazy ) {
data.reset( nullptr );
is_loaded = false;
}
}
//------------------------------------------------------------------------------
//! \brief Add a section index to the segment
//! \param sec_index Index of the section
//! \param addr_align Alignment of the section
//! \return Index of the added section
Elf_Half add_section_index( Elf_Half sec_index,
Elf_Xword addr_align ) override
{
sections.emplace_back( sec_index );
if ( addr_align > get_align() ) {
set_align( addr_align );
}
return (Elf_Half)sections.size();
}
//------------------------------------------------------------------------------
//! \brief Add a section to the segment
//! \param psec Pointer to the section
//! \param addr_align Alignment of the section
//! \return Index of the added section
Elf_Half add_section( section* psec, Elf_Xword addr_align ) override
{
return add_section_index( psec->get_index(), addr_align );
}
//------------------------------------------------------------------------------
//! \brief Get the number of sections in the segment
//! \return Number of sections in the segment
Elf_Half get_sections_num() const override
{
return (Elf_Half)sections.size();
}
//------------------------------------------------------------------------------
//! \brief Get the index of a section at a given position
//! \param num Position of the section
//! \return Index of the section
Elf_Half get_section_index_at( Elf_Half num ) const override
{
if ( num < sections.size() ) {
return sections[num];
}
return Elf_Half( -1 );
}
//------------------------------------------------------------------------------
protected:
//------------------------------------------------------------------------------
//! \brief Set the offset of the segment
//! \param value Offset of the segment
void set_offset( const Elf64_Off& value ) override
{
ph.p_offset = decltype( ph.p_offset )( value );
ph.p_offset = ( *convertor )( ph.p_offset );
is_offset_set = true;
}
//------------------------------------------------------------------------------
//! \brief Check if the offset is initialized
//! \return True if the offset is initialized, false otherwise
bool is_offset_initialized() const override { return is_offset_set; }
//------------------------------------------------------------------------------
//! \brief Get the sections of the segment
//! \return Vector of section indices
const std::vector<Elf_Half>& get_sections() const override
{
return sections;
}
//------------------------------------------------------------------------------
//! \brief Set the index of the segment
//! \param value Index of the segment
void set_index( const Elf_Half& value ) override { index = value; }
//------------------------------------------------------------------------------
//! \brief Load the segment from a stream
//! \param stream Input stream
//! \param header_offset Offset of the segment header
//! \param is_lazy_ Whether to load the segment lazily
//! \return True if successful, false otherwise
bool load( std::istream& stream,
std::streampos header_offset,
bool is_lazy_ ) override
{
pstream = &stream;
is_lazy = is_lazy_;
if ( translator->empty() ) {
stream.seekg( 0, std::istream::end );
set_stream_size( size_t( stream.tellg() ) );
}
else {
set_stream_size( std::numeric_limits<size_t>::max() );
}
stream.seekg( ( *translator )[header_offset] );
stream.read( reinterpret_cast<char*>( &ph ), sizeof( ph ) );
is_offset_set = true;
if ( !( is_lazy || is_loaded ) ) {
return load_data();
}
return true;
}
//------------------------------------------------------------------------------
//! \brief Load the data of the segment
//! \return True if successful, false otherwise
bool load_data() const
{
if ( PT_NULL == get_type() || 0 == get_file_size() ) {
return true;
}
Elf_Xword p_offset = ( *translator )[( *convertor )( ph.p_offset )];
Elf_Xword size = get_file_size();
// Check for integer overflow in offset calculation
if ( p_offset > get_stream_size() ) {
data = nullptr;
return false;
}
// Check for integer overflow in size calculation
if ( size > get_stream_size() ||
size > ( get_stream_size() - p_offset ) ) {
data = nullptr;
return false;
}
// Check if size can be safely converted to size_t
if ( size > std::numeric_limits<size_t>::max() - 1 ) {
data = nullptr;
return false;
}
data.reset( new ( std::nothrow ) char[(size_t)size + 1] );
pstream->seekg( p_offset );
if ( nullptr != data.get() && pstream->read( data.get(), size ) ) {
data.get()[size] = 0;
}
else {
data = nullptr;
return false;
}
is_loaded = true;
return true;
}
//------------------------------------------------------------------------------
//! \brief Save the segment to a stream
//! \param stream Output stream
//! \param header_offset Offset of the segment header
//! \param data_offset Offset of the segment data
void save( std::ostream& stream,
std::streampos header_offset,
std::streampos data_offset ) override
{
ph.p_offset = decltype( ph.p_offset )( data_offset );
ph.p_offset = ( *convertor )( ph.p_offset );
adjust_stream_size( stream, header_offset );
stream.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) );
}
//------------------------------------------------------------------------------
//! \brief Get the stream size
//! \return Stream size
size_t get_stream_size() const { return stream_size; }
//------------------------------------------------------------------------------
//! \brief Set the stream size
//! \param value Stream size
void set_stream_size( size_t value ) { stream_size = value; }
//------------------------------------------------------------------------------
//! \brief Sort sections in a segment according to offset
virtual void sort_sections( std::vector<Elf64_Addr> &offsets ) override
{
std::sort( sections.begin(), sections.end(), [&]( Elf_Half& a, Elf_Half& b) {
return offsets[a] < offsets[b];
} );
}
//------------------------------------------------------------------------------
private:
mutable std::istream* pstream = nullptr; //!< Pointer to the input stream
T ph = {}; //!< Segment header
Elf_Half index = 0; //!< Index of the segment
mutable std::unique_ptr<char[]> data; //!< Pointer to the segment data
std::vector<Elf_Half> sections; //!< Vector of section indices
std::shared_ptr<endianness_convertor> convertor =
nullptr; //!< Pointer to the endianness convertor
std::shared_ptr<address_translator> translator =
nullptr; //!< Pointer to the address translator
size_t stream_size = 0; //!< Stream size
bool is_offset_set = false; //!< Flag indicating if the offset is set
mutable bool is_lazy =
false; //!< Flag indicating if the segment is loaded lazily
mutable bool is_loaded =
false; //!< Flag indicating if the segment is loaded
};
} // namespace ELFIO
#endif // ELFIO_SEGMENT_HPP