2021-12-25 20:14:56 +01:00
|
|
|
/*
|
2023-01-05 16:26:11 +01:00
|
|
|
Copyright (C) 2001-present by Serge Lamikhov-Center
|
2021-12-25 20:14:56 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ELFIO_SEGMENT_HPP
|
|
|
|
#define ELFIO_SEGMENT_HPP
|
|
|
|
|
|
|
|
#include <vector>
|
2023-01-05 16:26:11 +01:00
|
|
|
#include <new>
|
|
|
|
#include <limits>
|
2021-12-25 20:14:56 +01:00
|
|
|
|
|
|
|
namespace ELFIO {
|
|
|
|
|
|
|
|
class segment
|
|
|
|
{
|
|
|
|
friend class elfio;
|
2023-01-05 16:26:11 +01:00
|
|
|
|
2021-12-25 20:14:56 +01:00
|
|
|
public:
|
2023-01-05 16:26:11 +01:00
|
|
|
virtual ~segment() = default;
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01: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 );
|
2021-12-25 20:14:56 +01:00
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, physical_address );
|
2023-01-05 16:26:11 +01:00
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size );
|
2021-12-25 20:14:56 +01:00
|
|
|
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
|
|
|
|
|
|
|
|
virtual const char* get_data() const = 0;
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
virtual Elf_Half add_section( section* psec, Elf_Xword addr_align ) = 0;
|
|
|
|
virtual Elf_Half add_section_index( Elf_Half index,
|
|
|
|
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;
|
2021-12-25 20:14:56 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
ELFIO_SET_ACCESS_DECL( Elf64_Off, offset );
|
2023-01-05 16:26:11 +01:00
|
|
|
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
virtual const std::vector<Elf_Half>& get_sections() const = 0;
|
|
|
|
|
|
|
|
virtual bool load( const char * pBuffer, size_t pBufferSize,
|
|
|
|
off_t header_offset ) = 0;
|
|
|
|
};
|
2021-12-25 20:14:56 +01:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2023-01-05 16:26:11 +01:00
|
|
|
template <class T> class segment_impl : public segment
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
public:
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
segment_impl( const endianess_convertor* convertor )
|
|
|
|
: convertor( convertor )
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2021-12-25 20:14:56 +01:00
|
|
|
// Section info functions
|
2023-01-05 16:26:11 +01: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 );
|
2021-12-25 20:14:56 +01:00
|
|
|
ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset );
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Half get_index() const override { return index; }
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
const char* get_data() const override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
return data.get();
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Half add_section_index( Elf_Half sec_index,
|
|
|
|
Elf_Xword addr_align ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
sections.emplace_back( sec_index );
|
2021-12-25 20:14:56 +01:00
|
|
|
if ( addr_align > get_align() ) {
|
|
|
|
set_align( addr_align );
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Elf_Half)sections.size();
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Half add_section( section* psec, Elf_Xword addr_align ) override
|
|
|
|
{
|
|
|
|
return add_section_index( psec->get_index(), addr_align );
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Half get_sections_num() const override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
return (Elf_Half)sections.size();
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Half get_section_index_at( Elf_Half num ) const override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
if ( num < sections.size() ) {
|
|
|
|
return sections[num];
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
return Elf_Half( -1 );
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2021-12-25 20:14:56 +01:00
|
|
|
protected:
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_offset( const Elf64_Off& value ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
ph.p_offset = decltype( ph.p_offset )( value );
|
|
|
|
ph.p_offset = ( *convertor )( ph.p_offset );
|
2021-12-25 20:14:56 +01:00
|
|
|
is_offset_set = true;
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
bool is_offset_initialized() const override { return is_offset_set; }
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
const std::vector<Elf_Half>& get_sections() const override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_index( const Elf_Half& value ) override { index = value; }
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
bool load( const char * pBuffer, size_t pBufferSize,
|
|
|
|
off_t header_offset) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
if( header_offset + sizeof( ph ) > pBufferSize ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy( reinterpret_cast<char*>( &ph ), pBuffer + header_offset, sizeof( ph ) );
|
|
|
|
is_offset_set = true;
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
return load_data(pBuffer, pBufferSize);
|
|
|
|
}
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
bool load_data(const char * pBuffer, size_t pBufferSize) const
|
|
|
|
{
|
|
|
|
if ( PT_NULL == get_type() || 0 == get_file_size() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto offset = ( *convertor )( ph.p_offset );
|
|
|
|
Elf_Xword size = get_file_size();
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
if ( size > pBufferSize ) {
|
|
|
|
data = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.reset( new ( std::nothrow ) char[(size_t)size + 1] );
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
if ( nullptr != data.get()) {
|
|
|
|
memcpy(data.get(), pBuffer + offset, size);
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-01-05 16:26:11 +01:00
|
|
|
data = nullptr;
|
|
|
|
return false;
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
return true;
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2021-12-25 20:14:56 +01:00
|
|
|
private:
|
2023-01-05 16:26:11 +01:00
|
|
|
T ph = {};
|
|
|
|
Elf_Half index = 0;
|
|
|
|
mutable std::unique_ptr<char[]> data;
|
|
|
|
std::vector<Elf_Half> sections;
|
|
|
|
const endianess_convertor* convertor = nullptr;
|
|
|
|
bool is_offset_set = false;
|
2021-12-25 20:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ELFIO
|
|
|
|
|
|
|
|
#endif // ELFIO_SEGMENT_HPP
|