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_SECTION_HPP
|
|
|
|
#define ELFIO_SECTION_HPP
|
|
|
|
|
|
|
|
#include <string>
|
2023-01-05 16:26:11 +01:00
|
|
|
#include <new>
|
|
|
|
#include <limits>
|
2021-12-25 20:14:56 +01:00
|
|
|
|
|
|
|
namespace ELFIO {
|
|
|
|
|
|
|
|
class section
|
|
|
|
{
|
|
|
|
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 ~section() = default;
|
|
|
|
|
|
|
|
ELFIO_GET_ACCESS_DECL( Elf_Half, index );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( std::string, name );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, flags );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, info );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, link );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, addr_align );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, entry_size );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, address );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, size );
|
|
|
|
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset );
|
|
|
|
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
|
|
|
|
|
|
|
|
virtual const char* get_data() const = 0;
|
|
|
|
virtual void set_data( const char* raw_data, Elf_Word size ) = 0;
|
|
|
|
virtual void set_data( const std::string& data ) = 0;
|
|
|
|
virtual void append_data( const char* raw_data, Elf_Word size ) = 0;
|
|
|
|
virtual void append_data( const std::string& data ) = 0;
|
|
|
|
virtual void
|
|
|
|
insert_data( Elf_Xword pos, const char* raw_data, Elf_Word size ) = 0;
|
|
|
|
virtual void insert_data( Elf_Xword pos, const std::string& data ) = 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 bool load( const char * pBuffer, size_t pBufferSize,
|
|
|
|
off_t header_offset) = 0;
|
|
|
|
virtual bool is_address_initialized() const = 0;
|
|
|
|
};
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
template <class T> class section_impl : public section
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
public:
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
section_impl( const endianess_convertor* convertor,
|
|
|
|
const std::shared_ptr<compression_interface>& compression )
|
|
|
|
: convertor( convertor ), compression( compression )
|
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, header.sh_type );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Xword, flags, header.sh_flags );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Xword, size, header.sh_size );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Word, link, header.sh_link );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Word, info, header.sh_info );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Xword, addr_align, header.sh_addralign );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Xword, entry_size, header.sh_entsize );
|
|
|
|
ELFIO_GET_SET_ACCESS( Elf_Word, name_string_offset, header.sh_name );
|
|
|
|
ELFIO_GET_ACCESS( Elf64_Addr, address, header.sh_addr );
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Half get_index() const override { return index; }
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::string get_name() const override { return name; }
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_name( const std::string& name_prm ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
this->name = name_prm;
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_address( const Elf64_Addr& value ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
header.sh_addr = decltype( header.sh_addr )( value );
|
|
|
|
header.sh_addr = ( *convertor )( header.sh_addr );
|
2021-12-25 20:14:56 +01:00
|
|
|
is_address_set = true;
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
bool is_address_initialized() const override { return is_address_set; }
|
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
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_data( const char* raw_data, Elf_Word size ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
if ( get_type() != SHT_NOBITS ) {
|
2023-01-05 16:26:11 +01:00
|
|
|
data = std::unique_ptr<char[]>( new ( std::nothrow ) char[size] );
|
|
|
|
if ( nullptr != data.get() && nullptr != raw_data ) {
|
2021-12-25 20:14:56 +01:00
|
|
|
data_size = size;
|
2023-01-05 16:26:11 +01:00
|
|
|
std::copy( raw_data, raw_data + size, data.get() );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data_size = 0;
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
set_size( data_size );
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_data( const std::string& str_data ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
return set_data( str_data.c_str(), (Elf_Word)str_data.size() );
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void append_data( const char* raw_data, Elf_Word size ) override
|
|
|
|
{
|
|
|
|
insert_data( get_size(), raw_data, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void append_data( const std::string& str_data ) override
|
|
|
|
{
|
|
|
|
return append_data( str_data.c_str(), (Elf_Word)str_data.size() );
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2021-12-25 20:14:56 +01:00
|
|
|
void
|
2023-01-05 16:26:11 +01:00
|
|
|
insert_data( Elf_Xword pos, const char* raw_data, Elf_Word size ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
|
|
|
if ( get_type() != SHT_NOBITS ) {
|
|
|
|
if ( get_size() + size < data_size ) {
|
2023-01-05 16:26:11 +01:00
|
|
|
char* d = data.get();
|
|
|
|
std::copy_backward( d + pos, d + get_size(),
|
|
|
|
d + get_size() + size );
|
|
|
|
std::copy( raw_data, raw_data + size, d + pos );
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-01-05 16:26:11 +01:00
|
|
|
data_size = 2 * ( data_size + size );
|
|
|
|
std::unique_ptr<char[]> new_data(
|
|
|
|
new ( std::nothrow ) char[data_size] );
|
|
|
|
|
|
|
|
if ( nullptr != new_data ) {
|
|
|
|
char* d = data.get();
|
|
|
|
std::copy( d, d + pos, new_data.get() );
|
|
|
|
std::copy( raw_data, raw_data + size,
|
|
|
|
new_data.get() + pos );
|
|
|
|
std::copy( d + pos, d + get_size(),
|
|
|
|
new_data.get() + pos + size );
|
|
|
|
data = std::move( new_data );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = 0;
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
set_size( get_size() + size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void insert_data( Elf_Xword pos, const std::string& str_data ) override
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
return insert_data( pos, str_data.c_str(), (Elf_Word)str_data.size() );
|
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
|
|
|
ELFIO_GET_SET_ACCESS( Elf64_Off, offset, header.sh_offset );
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void set_index( const Elf_Half& value ) override { index = value; }
|
|
|
|
|
|
|
|
bool is_compressed() const
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
return ( ( get_flags() & SHF_RPX_DEFLATE ) ||
|
|
|
|
( get_flags() & SHF_COMPRESSED ) ) &&
|
|
|
|
compression != nullptr;
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
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
|
|
|
header = { };
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
if( header_offset + sizeof( header ) > pBufferSize ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy( reinterpret_cast<char*>( &header ), pBuffer + header_offset, sizeof( header ) );
|
|
|
|
|
|
|
|
bool ret = load_data(pBuffer, pBufferSize);
|
|
|
|
|
|
|
|
if (ret && is_compressed() ) {
|
|
|
|
Elf_Xword size = get_size();
|
|
|
|
Elf_Xword uncompressed_size = 0;
|
|
|
|
auto decompressed_data = compression->inflate(
|
|
|
|
data.get(), convertor, size, uncompressed_size );
|
|
|
|
if ( decompressed_data != nullptr ) {
|
|
|
|
set_size( uncompressed_size );
|
|
|
|
data = std::move( decompressed_data );
|
2021-12-25 20:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-05 16:26:11 +01:00
|
|
|
|
|
|
|
return ret;
|
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
|
2021-12-25 20:14:56 +01:00
|
|
|
{
|
2023-01-05 16:26:11 +01:00
|
|
|
Elf_Xword size = get_size();
|
|
|
|
if ( nullptr == data && SHT_NULL != get_type() &&
|
|
|
|
SHT_NOBITS != get_type() && size < pBufferSize ) {
|
|
|
|
data.reset( new ( std::nothrow ) char[size_t( size ) + 1] );
|
|
|
|
|
|
|
|
if ( ( 0 != size ) && ( nullptr != data ) ) {
|
|
|
|
auto offset = ( *convertor )( header.sh_offset );
|
|
|
|
if(offset + size > pBufferSize) {
|
|
|
|
data = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy( data.get(), pBuffer + offset, size );
|
2021-12-25 20:14:56 +01:00
|
|
|
|
2023-01-05 16:26:11 +01:00
|
|
|
// refresh size because it may have changed if we had to decompress data
|
|
|
|
size = get_size();
|
|
|
|
data.get()[size] =
|
|
|
|
0; // Ensure data is ended with 0 to avoid oob read
|
|
|
|
data_size = decltype( data_size )( size );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data_size = 0;
|
|
|
|
}
|
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
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
private:
|
|
|
|
private:
|
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 header = {};
|
|
|
|
Elf_Half index = 0;
|
|
|
|
std::string name;
|
|
|
|
mutable std::unique_ptr<char[]> data;
|
|
|
|
mutable Elf_Word data_size = 0;
|
|
|
|
const endianess_convertor* convertor = nullptr;
|
|
|
|
const std::shared_ptr<compression_interface> compression = nullptr;
|
|
|
|
bool is_address_set = false;
|
2021-12-25 20:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ELFIO
|
|
|
|
|
|
|
|
#endif // ELFIO_SECTION_HPP
|