From be5574ee9fdbf7bc5aeb0ac793e748160a29f3bf Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 4 Jan 2023 14:42:11 +0100 Subject: [PATCH] Remove address translator from ELFIO --- source/elfio/elfio.hpp | 26 +++++++----------- source/elfio/elfio_header.hpp | 8 +++--- source/elfio/elfio_section.hpp | 24 +++-------------- source/elfio/elfio_segment.hpp | 18 +++---------- source/elfio/elfio_utils.hpp | 48 ---------------------------------- 5 files changed, 19 insertions(+), 105 deletions(-) diff --git a/source/elfio/elfio.hpp b/source/elfio/elfio.hpp index 849dd2a..1cf2b82 100644 --- a/source/elfio/elfio.hpp +++ b/source/elfio/elfio.hpp @@ -86,7 +86,6 @@ class elfio sections_ = std::move( other.sections_ ); segments_ = std::move( other.segments_ ); convertor = std::move( other.convertor ); - addr_translator = std::move( other.addr_translator ); compression = std::move( other.compression ); other.header = nullptr; @@ -102,7 +101,6 @@ class elfio sections_ = std::move( other.sections_ ); segments_ = std::move( other.segments_ ); convertor = std::move( other.convertor ); - addr_translator = std::move( other.addr_translator ); current_file_pos = other.current_file_pos; compression = std::move( other.compression ); @@ -132,11 +130,6 @@ class elfio create_mandatory_sections(); } - void set_address_translation( std::vector& addr_trans ) - { - addr_translator.set_address_translation( addr_trans ); - } - //------------------------------------------------------------------------------ bool load( const std::string& file_name, bool is_lazy = false ) { @@ -163,7 +156,7 @@ class elfio std::array e_ident = { 0 }; // Read ELF file signature - stream.seekg( addr_translator[0] ); + stream.seekg( 0 ); stream.read( e_ident.data(), sizeof( e_ident ) ); // Is it ELF file? @@ -237,12 +230,12 @@ class elfio if ( file_class == ELFCLASS64 ) { new_header = std::unique_ptr( new ( std::nothrow ) elf_header_impl( - &convertor, encoding, &addr_translator ) ); + &convertor, encoding ) ); } else if ( file_class == ELFCLASS32 ) { new_header = std::unique_ptr( new ( std::nothrow ) elf_header_impl( - &convertor, encoding, &addr_translator ) ); + &convertor, encoding ) ); } else { return nullptr; @@ -259,12 +252,12 @@ class elfio if ( file_class == ELFCLASS64 ) { sections_.emplace_back( new ( std::nothrow ) section_impl( - &convertor, &addr_translator, compression ) ); + &convertor, compression ) ); } else if ( file_class == ELFCLASS32 ) { sections_.emplace_back( new ( std::nothrow ) section_impl( - &convertor, &addr_translator, compression ) ); + &convertor, compression ) ); } else { sections_.pop_back(); @@ -285,12 +278,12 @@ class elfio if ( file_class == ELFCLASS64 ) { segments_.emplace_back( new ( std::nothrow ) - segment_impl( &convertor, &addr_translator ) ); + segment_impl( &convertor ) ); } else if ( file_class == ELFCLASS32 ) { segments_.emplace_back( new ( std::nothrow ) - segment_impl( &convertor, &addr_translator ) ); + segment_impl( &convertor ) ); } else { segments_.pop_back(); @@ -397,12 +390,12 @@ class elfio if ( file_class == ELFCLASS64 ) { segments_.emplace_back( new ( std::nothrow ) segment_impl( - &convertor, &addr_translator ) ); + &convertor) ); } else if ( file_class == ELFCLASS32 ) { segments_.emplace_back( new ( std::nothrow ) segment_impl( - &convertor, &addr_translator ) ); + &convertor ) ); } else { segments_.pop_back(); @@ -592,7 +585,6 @@ class elfio std::vector> sections_; std::vector> segments_; endianess_convertor convertor; - address_translator addr_translator; std::shared_ptr compression = nullptr; Elf_Xword current_file_pos = 0; diff --git a/source/elfio/elfio_header.hpp b/source/elfio/elfio_header.hpp index c783496..e89009a 100644 --- a/source/elfio/elfio_header.hpp +++ b/source/elfio/elfio_header.hpp @@ -75,9 +75,8 @@ template class elf_header_impl : public elf_header public: //------------------------------------------------------------------------------ elf_header_impl( endianess_convertor* convertor, - unsigned char encoding, - const address_translator* translator ) - : convertor( convertor ), translator( translator ) + unsigned char encoding ) + : convertor( convertor ) { header.e_ident[EI_MAG0] = ELFMAG0; header.e_ident[EI_MAG1] = ELFMAG1; @@ -101,7 +100,7 @@ template class elf_header_impl : public elf_header //------------------------------------------------------------------------------ bool load( std::istream& stream ) override { - stream.seekg( ( *translator )[0] ); + stream.seekg(0); stream.read( reinterpret_cast( &header ), sizeof( header ) ); return ( stream.gcount() == sizeof( header ) ); @@ -134,7 +133,6 @@ template class elf_header_impl : public elf_header private: T header = {}; endianess_convertor* convertor = nullptr; - const address_translator* translator = nullptr; }; } // namespace ELFIO diff --git a/source/elfio/elfio_section.hpp b/source/elfio/elfio_section.hpp index 3e262ec..2789b2f 100644 --- a/source/elfio/elfio_section.hpp +++ b/source/elfio/elfio_section.hpp @@ -76,10 +76,8 @@ template class section_impl : public section public: //------------------------------------------------------------------------------ section_impl( const endianess_convertor* convertor, - const address_translator* translator, const std::shared_ptr& compression ) - : convertor( convertor ), translator( translator ), - compression( compression ) + : convertor( convertor ), compression( compression ) { } @@ -141,9 +139,6 @@ template class section_impl : public section } set_size( data_size ); - if ( translator->empty() ) { - set_stream_size( data_size ); - } } //------------------------------------------------------------------------------ @@ -194,9 +189,6 @@ template class section_impl : public section } } set_size( get_size() + size ); - if ( translator->empty() ) { - set_stream_size( get_stream_size() + size ); - } } } @@ -235,15 +227,7 @@ template class section_impl : public section is_lazy = is_lazy_; header = { 0 }; - if ( translator->empty() ) { - stream.seekg( 0, std::istream::end ); - set_stream_size( size_t( stream.tellg() ) ); - } - else { - set_stream_size( std::numeric_limits::max() ); - } - - stream.seekg( ( *translator )[header_offset] ); + stream.seekg( header_offset); stream.read( reinterpret_cast( &header ), sizeof( header ) ); if ( !is_lazy || is_compressed() ) { @@ -276,8 +260,7 @@ template class section_impl : public section data.reset( new ( std::nothrow ) char[size_t( size ) + 1] ); if ( ( 0 != size ) && ( nullptr != data ) ) { - pstream->seekg( - ( *translator )[( *convertor )( header.sh_offset )] ); + pstream->seekg(( *convertor )( header.sh_offset )); pstream->read( data.get(), size ); if ( static_cast( pstream->gcount() ) != size ) { data = nullptr; @@ -311,7 +294,6 @@ template class section_impl : public section mutable std::unique_ptr data; mutable Elf_Word data_size = 0; const endianess_convertor* convertor = nullptr; - const address_translator* translator = nullptr; const std::shared_ptr compression = nullptr; bool is_address_set = false; size_t stream_size = 0; diff --git a/source/elfio/elfio_segment.hpp b/source/elfio/elfio_segment.hpp index 03dcd0e..753639a 100644 --- a/source/elfio/elfio_segment.hpp +++ b/source/elfio/elfio_segment.hpp @@ -72,9 +72,8 @@ template class segment_impl : public segment { public: //------------------------------------------------------------------------------ - segment_impl( const endianess_convertor* convertor, - const address_translator* translator ) - : convertor( convertor ), translator( translator ) + segment_impl( const endianess_convertor* convertor ) + : convertor( convertor ) { } @@ -167,15 +166,7 @@ template class segment_impl : public segment 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::max() ); - } - - stream.seekg( ( *translator )[header_offset] ); + stream.seekg( header_offset); stream.read( reinterpret_cast( &ph ), sizeof( ph ) ); is_offset_set = true; @@ -194,7 +185,7 @@ template class segment_impl : public segment return true; } - pstream->seekg( ( *translator )[( *convertor )( ph.p_offset )] ); + pstream->seekg(( *convertor )( ph.p_offset )); Elf_Xword size = get_file_size(); if ( size > get_stream_size() ) { @@ -229,7 +220,6 @@ template class segment_impl : public segment mutable std::unique_ptr data; std::vector 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; diff --git a/source/elfio/elfio_utils.hpp b/source/elfio/elfio_utils.hpp index 1e99960..5741dfb 100644 --- a/source/elfio/elfio_utils.hpp +++ b/source/elfio/elfio_utils.hpp @@ -158,54 +158,6 @@ class endianess_convertor bool need_conversion = false; }; -//------------------------------------------------------------------------------ -struct address_translation -{ - address_translation( uint64_t start, uint64_t size, uint64_t mapped_to ) - : start( start ), size( size ), mapped_to( mapped_to ){}; - std::streampos start; - std::streampos size; - std::streampos mapped_to; -}; - -//------------------------------------------------------------------------------ -class address_translator -{ - public: - //------------------------------------------------------------------------------ - void set_address_translation( std::vector& addr_trans ) - { - addr_translations = addr_trans; - - std::sort( - addr_translations.begin(), addr_translations.end(), - []( address_translation& a, address_translation& b ) -> bool { - return a.start < b.start; - } ); - } - - //------------------------------------------------------------------------------ - std::streampos operator[]( std::streampos value ) const - { - if ( addr_translations.empty() ) { - return value; - } - - for ( auto& t : addr_translations ) { - if ( ( t.start <= value ) && ( ( value - t.start ) < t.size ) ) { - return value - t.start + t.mapped_to; - } - } - - return value; - } - - bool empty() const { return addr_translations.empty(); } - - private: - std::vector addr_translations; -}; - //------------------------------------------------------------------------------ inline uint32_t elf_hash( const unsigned char* name ) {