2012-11-27 09:45:28 +00:00
|
|
|
/*
|
2021-01-19 07:43:01 +00:00
|
|
|
Copyright (C) 2001-present by Serge Lamikhov-Center
|
2012-11-27 09:45:28 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-01-13 02:29:43 +00:00
|
|
|
#ifndef ELFIO_SEGMENT_HPP
|
|
|
|
#define ELFIO_SEGMENT_HPP
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2013-11-25 19:02:39 +00:00
|
|
|
#include <iostream>
|
2012-11-27 09:45:28 +00:00
|
|
|
#include <vector>
|
2020-08-29 05:38:40 +00:00
|
|
|
#include <new>
|
2021-12-31 18:42:07 +00:00
|
|
|
#include <limits>
|
2012-11-27 09:45:28 +00:00
|
|
|
|
|
|
|
namespace ELFIO {
|
|
|
|
|
|
|
|
class segment
|
|
|
|
{
|
|
|
|
friend class elfio;
|
2020-08-21 14:56:08 +00:00
|
|
|
|
2012-11-27 09:45:28 +00:00
|
|
|
public:
|
2021-08-26 10:01:12 +00:00
|
|
|
virtual ~segment() = default;
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
ELFIO_GET_ACCESS_DECL( Elf_Half, index );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, virtual_address );
|
2012-11-27 09:45:28 +00:00
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, physical_address );
|
2020-08-21 14:56:08 +00:00
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size );
|
2014-11-15 21:08:52 +00:00
|
|
|
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2023-10-10 19:29:01 +00:00
|
|
|
virtual const char* get_data() const = 0;
|
|
|
|
virtual void free_data() const = 0;
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2022-11-24 13:56:37 +00:00
|
|
|
virtual Elf_Half add_section( section* psec, Elf_Xword addr_align ) = 0;
|
2020-08-21 14:56:08 +00:00
|
|
|
virtual Elf_Half add_section_index( Elf_Half index,
|
2022-11-24 13:56:37 +00:00
|
|
|
Elf_Xword addr_align ) = 0;
|
|
|
|
virtual Elf_Half get_sections_num() const = 0;
|
|
|
|
virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0;
|
|
|
|
virtual bool is_offset_initialized() const = 0;
|
2012-11-27 09:45:28 +00:00
|
|
|
|
|
|
|
protected:
|
2014-11-15 21:08:52 +00:00
|
|
|
ELFIO_SET_ACCESS_DECL( Elf64_Off, offset );
|
2020-08-21 14:56:08 +00:00
|
|
|
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
|
|
|
|
|
2022-11-24 13:56:37 +00:00
|
|
|
virtual const std::vector<Elf_Half>& get_sections() const = 0;
|
2022-02-11 08:00:32 +00:00
|
|
|
|
|
|
|
virtual bool load( std::istream& stream,
|
|
|
|
std::streampos header_offset,
|
2022-11-24 13:56:37 +00:00
|
|
|
bool is_lazy ) = 0;
|
2020-08-21 14:56:08 +00:00
|
|
|
virtual void save( std::ostream& stream,
|
|
|
|
std::streampos header_offset,
|
2022-11-24 13:56:37 +00:00
|
|
|
std::streampos data_offset ) = 0;
|
2012-11-27 09:45:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2020-08-21 14:56:08 +00:00
|
|
|
template <class T> class segment_impl : public segment
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2021-09-19 20:26:58 +00:00
|
|
|
segment_impl( const endianess_convertor* convertor,
|
|
|
|
const address_translator* translator )
|
2022-08-31 19:13:18 +00:00
|
|
|
: convertor( convertor ), translator( translator )
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-11-27 09:45:28 +00:00
|
|
|
// Section info functions
|
2020-08-21 14:56:08 +00:00
|
|
|
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 );
|
2014-11-15 21:08:52 +00:00
|
|
|
ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset );
|
2017-07-04 20:35:24 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
Elf_Half get_index() const override { return index; }
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
const char* get_data() const override
|
2022-02-11 08:00:32 +00:00
|
|
|
{
|
2023-10-13 17:03:48 +00:00
|
|
|
if ( !is_loaded ) {
|
2022-02-11 08:00:32 +00:00
|
|
|
load_data();
|
|
|
|
}
|
2022-11-11 15:29:11 +00:00
|
|
|
return data.get();
|
2023-09-22 16:49:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 19:29:01 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2023-09-22 16:49:39 +00:00
|
|
|
void free_data() const override
|
|
|
|
{
|
2023-10-13 17:03:48 +00:00
|
|
|
if ( is_lazy ) {
|
|
|
|
data.reset( nullptr );
|
|
|
|
is_loaded = false;
|
|
|
|
}
|
2022-02-11 08:00:32 +00:00
|
|
|
}
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2021-08-26 10:01:12 +00:00
|
|
|
Elf_Half add_section_index( Elf_Half sec_index,
|
2022-11-24 13:56:37 +00:00
|
|
|
Elf_Xword addr_align ) override
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
2021-08-16 19:32:48 +00:00
|
|
|
sections.emplace_back( sec_index );
|
2012-11-27 09:45:28 +00:00
|
|
|
if ( addr_align > get_align() ) {
|
|
|
|
set_align( addr_align );
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Elf_Half)sections.size();
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:03:05 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
Elf_Half add_section( section* psec, Elf_Xword addr_align ) override
|
2021-04-08 14:03:05 +00:00
|
|
|
{
|
|
|
|
return add_section_index( psec->get_index(), addr_align );
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
Elf_Half get_sections_num() const override
|
2021-08-26 10:01:12 +00:00
|
|
|
{
|
|
|
|
return (Elf_Half)sections.size();
|
|
|
|
}
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
Elf_Half get_section_index_at( Elf_Half num ) const override
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
|
|
|
if ( num < sections.size() ) {
|
|
|
|
return sections[num];
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
return Elf_Half( -1 );
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-11-27 09:45:28 +00:00
|
|
|
protected:
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2014-02-20 15:21:43 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
void set_offset( const Elf64_Off& value ) override
|
2014-11-15 21:08:52 +00:00
|
|
|
{
|
2022-02-11 07:20:38 +00:00
|
|
|
ph.p_offset = decltype( ph.p_offset )( value );
|
2020-08-21 14:56:08 +00:00
|
|
|
ph.p_offset = ( *convertor )( ph.p_offset );
|
2014-11-15 21:08:52 +00:00
|
|
|
is_offset_set = true;
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
bool is_offset_initialized() const override { return is_offset_set; }
|
2014-11-15 21:08:52 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
const std::vector<Elf_Half>& get_sections() const override
|
2021-08-26 10:01:12 +00:00
|
|
|
{
|
|
|
|
return sections;
|
|
|
|
}
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
void set_index( const Elf_Half& value ) override { index = value; }
|
2020-08-21 14:56:08 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2022-02-11 08:00:32 +00:00
|
|
|
bool load( std::istream& stream,
|
|
|
|
std::streampos header_offset,
|
2022-11-24 13:56:37 +00:00
|
|
|
bool is_lazy_ ) override
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
2022-02-11 08:00:32 +00:00
|
|
|
pstream = &stream;
|
|
|
|
is_lazy = is_lazy_;
|
|
|
|
|
2021-09-19 20:26:58 +00:00
|
|
|
if ( translator->empty() ) {
|
2022-08-31 19:13:18 +00:00
|
|
|
stream.seekg( 0, std::istream::end );
|
2022-02-11 07:20:38 +00:00
|
|
|
set_stream_size( size_t( stream.tellg() ) );
|
2021-09-19 20:26:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-12-31 18:42:07 +00:00
|
|
|
set_stream_size( std::numeric_limits<size_t>::max() );
|
2021-09-19 20:26:58 +00:00
|
|
|
}
|
2017-07-04 20:35:24 +00:00
|
|
|
|
2021-09-19 21:03:21 +00:00
|
|
|
stream.seekg( ( *translator )[header_offset] );
|
2012-11-27 09:45:28 +00:00
|
|
|
stream.read( reinterpret_cast<char*>( &ph ), sizeof( ph ) );
|
2014-11-15 21:08:52 +00:00
|
|
|
is_offset_set = true;
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2023-10-13 17:03:48 +00:00
|
|
|
if ( !( is_lazy || is_loaded ) ) {
|
2022-02-11 08:00:32 +00:00
|
|
|
return load_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-12 15:00:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
bool load_data() const
|
2022-02-11 08:00:32 +00:00
|
|
|
{
|
2022-08-31 19:13:18 +00:00
|
|
|
if ( PT_NULL == get_type() || 0 == get_file_size() ) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-19 11:46:22 +00:00
|
|
|
|
2022-11-11 15:29:11 +00:00
|
|
|
pstream->seekg( ( *translator )[( *convertor )( ph.p_offset )] );
|
2022-08-31 19:13:18 +00:00
|
|
|
Elf_Xword size = get_file_size();
|
|
|
|
|
2022-11-12 15:00:08 +00:00
|
|
|
if ( size > get_stream_size() ) {
|
2022-08-31 19:13:18 +00:00
|
|
|
data = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.reset( new ( std::nothrow ) char[(size_t)size + 1] );
|
|
|
|
|
2022-11-11 15:29:11 +00:00
|
|
|
if ( nullptr != data.get() && pstream->read( data.get(), size ) ) {
|
2022-08-31 19:13:18 +00:00
|
|
|
data.get()[size] = 0;
|
2019-04-19 11:46:22 +00:00
|
|
|
}
|
2022-09-01 15:49:56 +00:00
|
|
|
else {
|
|
|
|
data = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
2022-06-16 14:30:22 +00:00
|
|
|
|
2023-10-13 17:03:48 +00:00
|
|
|
is_loaded = true;
|
|
|
|
|
2022-06-16 14:30:22 +00:00
|
|
|
return true;
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2019-04-19 11:46:22 +00:00
|
|
|
void save( std::ostream& stream,
|
2012-11-27 09:45:28 +00:00
|
|
|
std::streampos header_offset,
|
2022-11-24 13:56:37 +00:00
|
|
|
std::streampos data_offset ) override
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
2022-02-11 07:20:38 +00:00
|
|
|
ph.p_offset = decltype( ph.p_offset )( data_offset );
|
2020-08-21 14:56:08 +00:00
|
|
|
ph.p_offset = ( *convertor )( ph.p_offset );
|
2021-03-27 18:14:56 +00:00
|
|
|
adjust_stream_size( stream, header_offset );
|
2019-04-19 11:46:22 +00:00
|
|
|
stream.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) );
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 17:43:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
size_t get_stream_size() const { return stream_size; }
|
2021-09-20 17:43:27 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2022-11-24 13:56:37 +00:00
|
|
|
void set_stream_size( size_t value ) { stream_size = value; }
|
2021-09-20 17:43:27 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-11-27 09:45:28 +00:00
|
|
|
private:
|
2022-11-11 15:29:11 +00:00
|
|
|
mutable std::istream* pstream = nullptr;
|
2022-11-12 17:03:39 +00:00
|
|
|
T ph = {};
|
2022-11-11 15:29:11 +00:00
|
|
|
Elf_Half index = 0;
|
|
|
|
mutable std::unique_ptr<char[]> data;
|
|
|
|
std::vector<Elf_Half> sections;
|
|
|
|
const endianess_convertor* convertor = nullptr;
|
|
|
|
const address_translator* translator = nullptr;
|
|
|
|
size_t stream_size = 0;
|
|
|
|
bool is_offset_set = false;
|
|
|
|
mutable bool is_lazy = false;
|
2023-10-13 17:03:48 +00:00
|
|
|
mutable bool is_loaded = false;
|
2012-11-27 09:45:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ELFIO
|
|
|
|
|
2013-01-13 02:29:43 +00:00
|
|
|
#endif // ELFIO_SEGMENT_HPP
|