diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index aa432702b011b9d6ad69b368ba1a885550f1dd89..23cbe802ccdf1f678b8ff0814e424fa5e722a1a8 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -38,7 +38,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} @@ -59,4 +59,4 @@ jobs: cmake --build build - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 87de54123a88f8c4d25530727d6742dfcd30a139..ad65d6b048446ee530566207e99251504f665536 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,9 @@ if(IS_TOP_PROJECT) # Turns this on in order to build tests option(ELFIO_BUILD_TESTS "Build ELFIO tests" OFF) + + # Generate output of compile commands during generation + set(CMAKE_EXPORT_COMPILE_COMMANDS, ON) endif() # Read version from header file @@ -44,9 +47,9 @@ target_include_directories( # If this is the top level project, add in logic to install elfio if(IS_TOP_PROJECT) - # Enable C++11 for examples and tests + # Enable C++14 for examples and tests if(ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS) - set(CMAKE_CXX_STANDARD 14) + set(CMAKE_CXX_STANDARD 17) endif() if(ELFIO_BUILD_EXAMPLES) @@ -54,7 +57,7 @@ if(IS_TOP_PROJECT) add_subdirectory(examples) endif() - if(ELFIO_BUILD_TESTS AND IS_TOP_PROJECT) + if(ELFIO_BUILD_TESTS) # set (CMAKE_CXX_FLAGS "-Wall") enable_testing() add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} USES_TERMINAL) diff --git a/LICENSE b/LICENSE deleted file mode 100644 index c6badc63f51d53c29bb8ecb486b3396728587f25..0000000000000000000000000000000000000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -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. diff --git a/README.OpenSource b/README.OpenSource index d9df97f83aee239c749574048e6c4af7cf84ed5c..3c31df7cf61a028e94ef1c203d6262ef4da66e63 100644 --- a/README.OpenSource +++ b/README.OpenSource @@ -2,8 +2,8 @@ { "Name" : "elfio", "License" : "The MIT License", - "License File" : "COPYING", - "Version Number" : "Release_3.11", + "License File" : "LICENSE.txt", + "Version Number" : "Release_3.12", "Owner" : "lichunlin2@huawei.com", "Upstream URL" : "http://elfio.sourceforge.net/", "Description" : "ELFIO is a small, header-only C++ library that provides a simple interface for reading and generating files in ELF binary format." diff --git a/bundle.json b/bundle.json index b41f683cc0b603c7bf047c2385d4e5b5b2fa1671..600c3681d20895a63d4ca90eabbda85fab61b5d9 100644 --- a/bundle.json +++ b/bundle.json @@ -1,7 +1,7 @@ { "name": "@ohos/elfio", "description": "ELFIO is a small, header-only C++ library that provides a simple interface for reading and generating files in ELF binary format.", - "version": "3.9", + "version": "3.12", "license": "MIT", "publishAs": "code-segment", "segment": { diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 74d515cf04d495cc11b51ce2d37b617f812e040b..ddde61b2b2cead6f65aae83be8044d4d9eb744de 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -41,18 +41,15 @@ THE SOFTWARE. #include "elfio_segment.hpp" #include "elfio_strings.hpp" -#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \ - TYPE get_##FNAME() const noexcept \ - { \ - return header ? ( header->get_##FNAME() ) : 0; \ - } +#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \ + TYPE get_##FNAME() const { return header ? ( header->get_##FNAME() ) : 0; } #define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \ - TYPE get_##FNAME() const noexcept \ + TYPE get_##FNAME() const \ { \ return header ? ( header->get_##FNAME() ) : 0; \ } \ - void set_##FNAME( TYPE val ) noexcept \ + void set_##FNAME( TYPE val ) \ { \ if ( header ) { \ header->set_##FNAME( val ); \ @@ -123,7 +120,7 @@ class elfio // clang-format on //------------------------------------------------------------------------------ - void create( unsigned char file_class, unsigned char encoding ) noexcept + void create( unsigned char file_class, unsigned char encoding ) { sections_.clear(); segments_.clear(); @@ -132,14 +129,13 @@ class elfio create_mandatory_sections(); } - void set_address_translation( - std::vector& addr_trans ) noexcept + 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 ) noexcept + bool load( const std::string& file_name, bool is_lazy = false ) { pstream = std::make_unique(); pstream->open( file_name.c_str(), std::ios::in | std::ios::binary ); @@ -150,14 +146,14 @@ class elfio bool ret = load( *pstream, is_lazy ); if ( !is_lazy ) { - pstream.release(); + pstream.reset(); } return ret; } //------------------------------------------------------------------------------ - bool load( std::istream& stream, bool is_lazy = false ) noexcept + bool load( std::istream& stream, bool is_lazy = false ) { sections_.clear(); segments_.clear(); @@ -199,7 +195,7 @@ class elfio } //------------------------------------------------------------------------------ - bool save( const std::string& file_name ) noexcept + bool save( const std::string& file_name ) { std::ofstream stream; stream.open( file_name.c_str(), std::ios::out | std::ios::binary ); @@ -211,7 +207,7 @@ class elfio } //------------------------------------------------------------------------------ - bool save( std::ostream& stream ) noexcept + bool save( std::ostream& stream ) { if ( !stream || header == nullptr ) { return false; @@ -267,13 +263,10 @@ class elfio ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, section_name_str_index ); //------------------------------------------------------------------------------ - const endianess_convertor& get_convertor() const noexcept - { - return convertor; - } + const endianess_convertor& get_convertor() const { return convertor; } //------------------------------------------------------------------------------ - Elf_Xword get_default_entry_size( Elf_Word section_type ) const noexcept + Elf_Xword get_default_entry_size( Elf_Word section_type ) const { switch ( section_type ) { case SHT_RELA: @@ -313,7 +306,7 @@ class elfio //! returns an empty string if no problems are detected, //! or a string containing an error message if problems are found, //! with one error per line. - std::string validate() const noexcept + std::string validate() const { // clang-format off @@ -375,23 +368,20 @@ class elfio private: //------------------------------------------------------------------------------ - static bool is_offset_in_section( Elf64_Off offset, - const section* sec ) noexcept + static bool is_offset_in_section( Elf64_Off offset, const section* sec ) { return ( offset >= sec->get_offset() ) && ( offset < ( sec->get_offset() + sec->get_size() ) ); } //------------------------------------------------------------------------------ - static Elf64_Addr get_virtual_addr( Elf64_Off offset, - const section* sec ) noexcept + static Elf64_Addr get_virtual_addr( Elf64_Off offset, const section* sec ) { return sec->get_address() + offset - sec->get_offset(); } //------------------------------------------------------------------------------ - const section* - find_prog_section_for_offset( Elf64_Off offset ) const noexcept + const section* find_prog_section_for_offset( Elf64_Off offset ) const { for ( const auto& sec : sections ) { if ( sec->get_type() == SHT_PROGBITS && @@ -404,7 +394,7 @@ class elfio //------------------------------------------------------------------------------ std::unique_ptr create_header( unsigned char file_class, - unsigned char encoding ) noexcept + unsigned char encoding ) { std::unique_ptr new_header; @@ -426,11 +416,9 @@ class elfio } //------------------------------------------------------------------------------ - section* create_section() noexcept + section* create_section() { - unsigned char file_class = get_class(); - - if ( file_class == ELFCLASS64 ) { + if ( auto file_class = get_class(); file_class == ELFCLASS64 ) { sections_.emplace_back( new ( std::nothrow ) section_impl( &convertor, &addr_translator, compression ) ); @@ -452,11 +440,9 @@ class elfio } //------------------------------------------------------------------------------ - segment* create_segment() noexcept + segment* create_segment() { - unsigned char file_class = header->get_class(); - - if ( file_class == ELFCLASS64 ) { + if ( auto file_class = header->get_class(); file_class == ELFCLASS64 ) { segments_.emplace_back( new ( std::nothrow ) segment_impl( &convertor, &addr_translator ) ); @@ -478,7 +464,7 @@ class elfio } //------------------------------------------------------------------------------ - void create_mandatory_sections() noexcept + void create_mandatory_sections() { // Create null section without calling to 'add_section' as no string // section containing section names exists yet @@ -494,7 +480,7 @@ class elfio } //------------------------------------------------------------------------------ - bool load_sections( std::istream& stream, bool is_lazy ) noexcept + bool load_sections( std::istream& stream, bool is_lazy ) { unsigned char file_class = header->get_class(); Elf_Half entry_size = header->get_section_entry_size(); @@ -519,9 +505,8 @@ class elfio sec->set_address( sec->get_address() ); } - Elf_Half shstrndx = get_section_name_str_index(); - - if ( SHN_UNDEF != shstrndx ) { + if ( Elf_Half shstrndx = get_section_name_str_index(); + SHN_UNDEF != shstrndx ) { string_section_accessor str_reader( sections[shstrndx] ); for ( Elf_Half i = 0; i < num; ++i ) { Elf_Word section_offset = sections[i]->get_name_string_offset(); @@ -542,7 +527,7 @@ class elfio static bool is_sect_in_seg( Elf64_Off sect_begin, Elf_Xword sect_size, Elf64_Off seg_begin, - Elf64_Off seg_end ) noexcept + Elf64_Off seg_end ) { return ( seg_begin <= sect_begin ) && ( sect_begin + sect_size <= seg_end ) && @@ -553,7 +538,7 @@ class elfio } //------------------------------------------------------------------------------ - bool load_segments( std::istream& stream, bool is_lazy ) noexcept + bool load_segments( std::istream& stream, bool is_lazy ) { unsigned char file_class = header->get_class(); Elf_Half entry_size = header->get_segment_entry_size(); @@ -621,13 +606,13 @@ class elfio } //------------------------------------------------------------------------------ - bool save_header( std::ostream& stream ) const noexcept + bool save_header( std::ostream& stream ) const { return header->save( stream ); } //------------------------------------------------------------------------------ - bool save_sections( std::ostream& stream ) const noexcept + bool save_sections( std::ostream& stream ) const { for ( const auto& sec : sections_ ) { std::streampos headerPosition = @@ -642,7 +627,7 @@ class elfio } //------------------------------------------------------------------------------ - bool save_segments( std::ostream& stream ) const noexcept + bool save_segments( std::ostream& stream ) const { for ( const auto& seg : segments_ ) { std::streampos headerPosition = @@ -657,7 +642,7 @@ class elfio } //------------------------------------------------------------------------------ - bool is_section_without_segment( unsigned int section_index ) const noexcept + bool is_section_without_segment( unsigned int section_index ) const { bool found = false; @@ -672,8 +657,7 @@ class elfio } //------------------------------------------------------------------------------ - static bool is_subsequence_of( const segment* seg1, - const segment* seg2 ) noexcept + static bool is_subsequence_of( const segment* seg1, const segment* seg2 ) { // Return 'true' if sections of seg1 are a subset of sections in seg2 const std::vector& sections1 = seg1->get_sections(); @@ -689,7 +673,7 @@ class elfio } //------------------------------------------------------------------------------ - std::vector get_ordered_segments() const noexcept + std::vector get_ordered_segments() const { std::vector res; std::deque worklist; @@ -735,14 +719,14 @@ class elfio } //------------------------------------------------------------------------------ - bool layout_sections_without_segments() noexcept + bool layout_sections_without_segments() { for ( unsigned int i = 0; i < sections_.size(); ++i ) { if ( is_section_without_segment( i ) ) { const auto& sec = sections_[i]; - Elf_Xword section_align = sec->get_addr_align(); - if ( section_align > 1 && + if ( Elf_Xword section_align = sec->get_addr_align(); + section_align > 1 && current_file_pos % section_align != 0 ) { current_file_pos += section_align - current_file_pos % section_align; @@ -763,7 +747,7 @@ class elfio } //------------------------------------------------------------------------------ - void calc_segment_alignment() const noexcept + void calc_segment_alignment() const { for ( const auto& seg : segments_ ) { for ( Elf_Half i = 0; i < seg->get_sections_num(); ++i ) { @@ -776,7 +760,7 @@ class elfio } //------------------------------------------------------------------------------ - bool layout_segments_and_their_sections() noexcept + bool layout_segments_and_their_sections() { std::vector worklist; std::vector section_generated( sections.size(), false ); @@ -845,7 +829,7 @@ class elfio } //------------------------------------------------------------------------------ - bool layout_section_table() noexcept + bool layout_section_table() { // Simply place the section table at the end for now Elf64_Off alignmentError = current_file_pos % 4; @@ -859,7 +843,7 @@ class elfio std::vector& section_generated, Elf_Xword& segment_memory, Elf_Xword& segment_filesize, - const Elf_Xword& seg_start_pos ) noexcept + const Elf_Xword& seg_start_pos ) { for ( Elf_Half j = 0; j < seg->get_sections_num(); ++j ) { Elf_Half index = seg->get_section_index_at( j ); @@ -956,13 +940,13 @@ class elfio explicit Sections( elfio* parent ) : parent( parent ) {} //------------------------------------------------------------------------------ - Elf_Half size() const noexcept + Elf_Half size() const { return static_cast( parent->sections_.size() ); } //------------------------------------------------------------------------------ - section* operator[]( unsigned int index ) const noexcept + section* operator[]( unsigned int index ) const { section* sec = nullptr; @@ -974,7 +958,7 @@ class elfio } //------------------------------------------------------------------------------ - section* operator[]( const std::string& name ) const noexcept + section* operator[]( const std::string_view& name ) const { section* sec = nullptr; @@ -989,7 +973,7 @@ class elfio } //------------------------------------------------------------------------------ - section* add( const std::string& name ) const noexcept + section* add( const std::string& name ) const { section* new_section = parent->create_section(); new_section->set_name( name ); @@ -1004,27 +988,25 @@ class elfio } //------------------------------------------------------------------------------ - std::vector>::iterator begin() noexcept + std::vector>::iterator begin() { return parent->sections_.begin(); } //------------------------------------------------------------------------------ - std::vector>::iterator end() noexcept + std::vector>::iterator end() { return parent->sections_.end(); } //------------------------------------------------------------------------------ - std::vector>::const_iterator - begin() const noexcept + std::vector>::const_iterator begin() const { return parent->sections_.cbegin(); } //------------------------------------------------------------------------------ - std::vector>::const_iterator - end() const noexcept + std::vector>::const_iterator end() const { return parent->sections_.cend(); } @@ -1044,42 +1026,40 @@ class elfio explicit Segments( elfio* parent ) : parent( parent ) {} //------------------------------------------------------------------------------ - Elf_Half size() const noexcept + Elf_Half size() const { return static_cast( parent->segments_.size() ); } //------------------------------------------------------------------------------ - segment* operator[]( unsigned int index ) const noexcept + segment* operator[]( unsigned int index ) const { return parent->segments_[index].get(); } //------------------------------------------------------------------------------ - segment* add() noexcept { return parent->create_segment(); } + segment* add() { return parent->create_segment(); } //------------------------------------------------------------------------------ - std::vector>::iterator begin() noexcept + std::vector>::iterator begin() { return parent->segments_.begin(); } //------------------------------------------------------------------------------ - std::vector>::iterator end() noexcept + std::vector>::iterator end() { return parent->segments_.end(); } //------------------------------------------------------------------------------ - std::vector>::const_iterator - begin() const noexcept + std::vector>::const_iterator begin() const { return parent->segments_.cbegin(); } //------------------------------------------------------------------------------ - std::vector>::const_iterator - end() const noexcept + std::vector>::const_iterator end() const { return parent->segments_.cend(); } diff --git a/elfio/elfio_modinfo.hpp b/elfio/elfio_modinfo.hpp index 84deda6a6f02b6572159dd6170b2f951b0e1f903..2d42ba3de780d5f954133b22abe5ba4bfcf6900b 100644 --- a/elfio/elfio_modinfo.hpp +++ b/elfio/elfio_modinfo.hpp @@ -23,7 +23,7 @@ THE SOFTWARE. #ifndef ELFIO_MODINFO_HPP #define ELFIO_MODINFO_HPP -#include +#include #include namespace ELFIO { @@ -56,12 +56,12 @@ template class modinfo_section_accessor_template } //------------------------------------------------------------------------------ - bool get_attribute( const std::string& field_name, - std::string& value ) const + bool get_attribute( const std::string_view& field_name, + std::string& value ) const { - for ( auto& i : content ) { - if ( field_name == i.first ) { - value = i.second; + for ( const auto [first, second] : content ) { + if ( field_name == first ) { + value = second; return true; } } diff --git a/elfio/elfio_section.hpp b/elfio/elfio_section.hpp index 87499bd82a847528b919cae81f04552bb7c77139..8c6aed451fedb3066f4873eeb19ece57b5ae1974 100644 --- a/elfio/elfio_section.hpp +++ b/elfio/elfio_section.hpp @@ -50,14 +50,16 @@ class section ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset ); ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); - virtual const char* get_data() const noexcept = 0; - virtual void set_data( const char* raw_data, Elf_Word size ) noexcept = 0; - virtual void set_data( const std::string& data ) noexcept = 0; - virtual void append_data( const char* raw_data, - Elf_Word size ) noexcept = 0; - virtual void append_data( const std::string& data ) noexcept = 0; - virtual size_t get_stream_size() const noexcept = 0; - virtual void set_stream_size( size_t value ) noexcept = 0; + 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; + virtual size_t get_stream_size() const = 0; + virtual void set_stream_size( size_t value ) = 0; protected: ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); @@ -65,11 +67,11 @@ class section virtual bool load( std::istream& stream, std::streampos header_offset, - bool is_lazy ) noexcept = 0; + bool is_lazy ) = 0; virtual void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) noexcept = 0; - virtual bool is_address_initialized() const noexcept = 0; + std::streampos data_offset ) = 0; + virtual bool is_address_initialized() const = 0; }; template class section_impl : public section @@ -96,19 +98,19 @@ template class section_impl : public section 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 noexcept override { return index; } + Elf_Half get_index() const override { return index; } //------------------------------------------------------------------------------ - std::string get_name() const noexcept override { return name; } + std::string get_name() const override { return name; } //------------------------------------------------------------------------------ - void set_name( const std::string& name_prm ) noexcept override + void set_name( const std::string& name_prm ) override { this->name = name_prm; } //------------------------------------------------------------------------------ - void set_address( const Elf64_Addr& value ) noexcept override + void set_address( const Elf64_Addr& value ) override { header.sh_addr = decltype( header.sh_addr )( value ); header.sh_addr = ( *convertor )( header.sh_addr ); @@ -116,13 +118,10 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - bool is_address_initialized() const noexcept override - { - return is_address_set; - } + bool is_address_initialized() const override { return is_address_set; } //------------------------------------------------------------------------------ - const char* get_data() const noexcept override + const char* get_data() const override { if ( is_lazy ) { load_data(); @@ -131,7 +130,7 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void set_data( const char* raw_data, Elf_Word size ) noexcept override + void set_data( const char* raw_data, Elf_Word size ) override { if ( get_type() != SHT_NOBITS ) { data = std::unique_ptr( new ( std::nothrow ) char[size] ); @@ -151,17 +150,33 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void set_data( const std::string& str_data ) noexcept override + void set_data( const std::string& str_data ) override { return set_data( str_data.c_str(), (Elf_Word)str_data.size() ); } //------------------------------------------------------------------------------ - void append_data( const char* raw_data, Elf_Word size ) noexcept override + 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() ); + } + + //------------------------------------------------------------------------------ + void + insert_data( Elf_Xword pos, const char* raw_data, Elf_Word size ) override { if ( get_type() != SHT_NOBITS ) { if ( get_size() + size < data_size ) { - std::copy( raw_data, raw_data + size, data.get() + get_size() ); + 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 ); } else { data_size = 2 * ( data_size + size ); @@ -169,10 +184,12 @@ template class section_impl : public section new ( std::nothrow ) char[data_size] ); if ( nullptr != new_data ) { - std::copy( data.get(), data.get() + get_size(), - new_data.get() ); + char* d = data.get(); + std::copy( d, d + pos, new_data.get() ); std::copy( raw_data, raw_data + size, - new_data.get() + get_size() ); + new_data.get() + pos ); + std::copy( d + pos, d + get_size(), + new_data.get() + pos + size ); data = std::move( new_data ); } else { @@ -187,19 +204,15 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void append_data( const std::string& str_data ) noexcept override + void insert_data( Elf_Xword pos, const std::string& str_data ) override { - return append_data( str_data.c_str(), (Elf_Word)str_data.size() ); + return insert_data( pos, str_data.c_str(), (Elf_Word)str_data.size() ); } - //------------------------------------------------------------------------------ - size_t get_stream_size() const noexcept override { return stream_size; } + size_t get_stream_size() const override { return stream_size; } //------------------------------------------------------------------------------ - void set_stream_size( size_t value ) noexcept override - { - stream_size = value; - } + void set_stream_size( size_t value ) override { stream_size = value; } //------------------------------------------------------------------------------ protected: @@ -207,9 +220,9 @@ template class section_impl : public section ELFIO_GET_SET_ACCESS( Elf64_Off, offset, header.sh_offset ); //------------------------------------------------------------------------------ - void set_index( const Elf_Half& value ) noexcept override { index = value; } + void set_index( const Elf_Half& value ) override { index = value; } - bool is_compressed() const noexcept + bool is_compressed() const { return ( ( get_flags() & SHF_RPX_DEFLATE ) || ( get_flags() & SHF_COMPRESSED ) ) && @@ -219,11 +232,10 @@ template class section_impl : public section //------------------------------------------------------------------------------ bool load( std::istream& stream, std::streampos header_offset, - bool is_lazy_ ) noexcept override + bool is_lazy_ ) override { pstream = &stream; is_lazy = is_lazy_; - header = { 0 }; if ( translator->empty() ) { stream.seekg( 0, std::istream::end ); @@ -244,7 +256,7 @@ template class section_impl : public section Elf_Xword size = get_size(); Elf_Xword uncompressed_size = 0; auto decompressed_data = compression->inflate( - data.get(), convertor, size, uncompressed_size ); + data.get(), convertor, size, uncompressed_size ); if ( decompressed_data != nullptr ) { set_size( uncompressed_size ); data = std::move( decompressed_data ); @@ -257,7 +269,7 @@ template class section_impl : public section return true; } - bool load_data() const noexcept + bool load_data() const { is_lazy = false; Elf_Xword size = get_size(); @@ -291,7 +303,7 @@ template class section_impl : public section //------------------------------------------------------------------------------ void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) noexcept override + std::streampos data_offset ) override { if ( 0 != get_index() ) { header.sh_offset = decltype( header.sh_offset )( data_offset ); @@ -308,8 +320,7 @@ template class section_impl : public section //------------------------------------------------------------------------------ private: //------------------------------------------------------------------------------ - void save_header( std::ostream& stream, - std::streampos header_offset ) const noexcept + void save_header( std::ostream& stream, std::streampos header_offset ) const { adjust_stream_size( stream, header_offset ); stream.write( reinterpret_cast( &header ), @@ -317,7 +328,7 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void save_data( std::ostream& stream, std::streampos data_offset ) noexcept + void save_data( std::ostream& stream, std::streampos data_offset ) { adjust_stream_size( stream, data_offset ); @@ -327,7 +338,7 @@ template class section_impl : public section Elf_Xword decompressed_size = get_size(); Elf_Xword compressed_size = 0; auto compressed_ptr = compression->deflate( - data.get(), convertor, decompressed_size, compressed_size ); + data.get(), convertor, decompressed_size, compressed_size ); stream.write( compressed_ptr.get(), compressed_size ); } else { diff --git a/elfio/elfio_segment.hpp b/elfio/elfio_segment.hpp index 5deae67af7a1c72ea0956d4ac79bb690b44c319b..6b0f81db345ccdd0a59864a166c27ed142403e6f 100644 --- a/elfio/elfio_segment.hpp +++ b/elfio/elfio_segment.hpp @@ -47,28 +47,27 @@ class segment ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size ); ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); - virtual const char* get_data() const noexcept = 0; + virtual const char* get_data() const = 0; - virtual Elf_Half add_section( section* psec, - Elf_Xword addr_align ) noexcept = 0; + 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 ) noexcept = 0; - virtual Elf_Half get_sections_num() const noexcept = 0; - virtual Elf_Half get_section_index_at( Elf_Half num ) const noexcept = 0; - virtual bool is_offset_initialized() const noexcept = 0; + 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; protected: ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); ELFIO_SET_ACCESS_DECL( Elf_Half, index ); - virtual const std::vector& get_sections() const noexcept = 0; + virtual const std::vector& get_sections() const = 0; virtual bool load( std::istream& stream, std::streampos header_offset, - bool is_lazy ) noexcept = 0; + bool is_lazy ) = 0; virtual void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) noexcept = 0; + std::streampos data_offset ) = 0; }; //------------------------------------------------------------------------------ @@ -94,10 +93,10 @@ template class segment_impl : public segment ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset ); //------------------------------------------------------------------------------ - Elf_Half get_index() const noexcept override { return index; } + Elf_Half get_index() const override { return index; } //------------------------------------------------------------------------------ - const char* get_data() const noexcept override + const char* get_data() const override { if ( is_lazy ) { load_data(); @@ -107,7 +106,7 @@ template class segment_impl : public segment //------------------------------------------------------------------------------ Elf_Half add_section_index( Elf_Half sec_index, - Elf_Xword addr_align ) noexcept override + Elf_Xword addr_align ) override { sections.emplace_back( sec_index ); if ( addr_align > get_align() ) { @@ -118,20 +117,19 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - Elf_Half add_section( section* psec, - Elf_Xword addr_align ) noexcept override + 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 noexcept override + Elf_Half get_sections_num() const override { return (Elf_Half)sections.size(); } //------------------------------------------------------------------------------ - Elf_Half get_section_index_at( Elf_Half num ) const noexcept override + Elf_Half get_section_index_at( Elf_Half num ) const override { if ( num < sections.size() ) { return sections[num]; @@ -145,7 +143,7 @@ template class segment_impl : public segment //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ - void set_offset( const Elf64_Off& value ) noexcept override + void set_offset( const Elf64_Off& value ) override { ph.p_offset = decltype( ph.p_offset )( value ); ph.p_offset = ( *convertor )( ph.p_offset ); @@ -153,24 +151,21 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - bool is_offset_initialized() const noexcept override - { - return is_offset_set; - } + bool is_offset_initialized() const override { return is_offset_set; } //------------------------------------------------------------------------------ - const std::vector& get_sections() const noexcept override + const std::vector& get_sections() const override { return sections; } //------------------------------------------------------------------------------ - void set_index( const Elf_Half& value ) noexcept override { index = value; } + void set_index( const Elf_Half& value ) override { index = value; } //------------------------------------------------------------------------------ bool load( std::istream& stream, std::streampos header_offset, - bool is_lazy_ ) noexcept override + bool is_lazy_ ) override { pstream = &stream; is_lazy = is_lazy_; @@ -195,7 +190,7 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - bool load_data() const noexcept + bool load_data() const { is_lazy = false; if ( PT_NULL == get_type() || 0 == get_file_size() ) { @@ -226,7 +221,7 @@ template class segment_impl : public segment //------------------------------------------------------------------------------ void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) noexcept override + std::streampos data_offset ) override { ph.p_offset = decltype( ph.p_offset )( data_offset ); ph.p_offset = ( *convertor )( ph.p_offset ); @@ -235,10 +230,10 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - size_t get_stream_size() const noexcept { return stream_size; } + size_t get_stream_size() const { return stream_size; } //------------------------------------------------------------------------------ - void set_stream_size( size_t value ) noexcept { stream_size = value; } + void set_stream_size( size_t value ) { stream_size = value; } //------------------------------------------------------------------------------ private: diff --git a/elfio/elfio_utils.hpp b/elfio/elfio_utils.hpp index 8d696c9e79415005b97673ec63bf6ffd61c0605d..ecb0961ae879a251a6c6a05b641c7f60174aba3f 100644 --- a/elfio/elfio_utils.hpp +++ b/elfio/elfio_utils.hpp @@ -26,37 +26,30 @@ THE SOFTWARE. #include #include -#define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) \ - virtual TYPE get_##NAME() const noexcept = 0 +#define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) virtual TYPE get_##NAME() const = 0 #define ELFIO_SET_ACCESS_DECL( TYPE, NAME ) \ - virtual void set_##NAME( const TYPE& value ) noexcept = 0 + virtual void set_##NAME( const TYPE& value ) = 0 -#define ELFIO_GET_SET_ACCESS_DECL( TYPE, NAME ) \ - virtual TYPE get_##NAME() const noexcept = 0; \ - virtual void set_##NAME( const TYPE& value ) noexcept = 0 +#define ELFIO_GET_SET_ACCESS_DECL( TYPE, NAME ) \ + virtual TYPE get_##NAME() const = 0; \ + virtual void set_##NAME( const TYPE& value ) = 0 #define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \ - TYPE get_##NAME() const noexcept override \ - { \ - return ( *convertor )( FIELD ); \ - } + TYPE get_##NAME() const override { return ( *convertor )( FIELD ); } -#define ELFIO_SET_ACCESS( TYPE, NAME, FIELD ) \ - void set_##NAME( const TYPE& value ) noexcept override \ - { \ - FIELD = decltype( FIELD )( value ); \ - FIELD = ( *convertor )( FIELD ); \ +#define ELFIO_SET_ACCESS( TYPE, NAME, FIELD ) \ + void set_##NAME( const TYPE& value ) override \ + { \ + FIELD = decltype( FIELD )( value ); \ + FIELD = ( *convertor )( FIELD ); \ } -#define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \ - TYPE get_##NAME() const noexcept override \ - { \ - return ( *convertor )( FIELD ); \ - } \ - void set_##NAME( const TYPE& value ) noexcept override \ - { \ - FIELD = decltype( FIELD )( value ); \ - FIELD = ( *convertor )( FIELD ); \ +#define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \ + TYPE get_##NAME() const override { return ( *convertor )( FIELD ); } \ + void set_##NAME( const TYPE& value ) override \ + { \ + FIELD = decltype( FIELD )( value ); \ + FIELD = ( *convertor )( FIELD ); \ } namespace ELFIO { @@ -184,11 +177,11 @@ class address_translator { 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::sort( addr_translations.begin(), addr_translations.end(), + []( const address_translation& a, + const address_translation& b ) -> bool { + return a.start < b.start; + } ); } //------------------------------------------------------------------------------ @@ -243,8 +236,7 @@ inline std::string to_hex_string( uint64_t value ) std::string str; while ( value ) { - auto digit = value & 0xF; - if ( digit < 0xA ) { + if ( auto digit = value & 0xF; digit < 0xA ) { str = char( '0' + digit ) + str; } else { diff --git a/elfio/elfio_version.hpp b/elfio/elfio_version.hpp index a62d26960c4c76aad63f5bcab36d35744bb84ca5..5b62d0d0bf23c6f126dc6ed7a234280cb7ddef55 100644 --- a/elfio/elfio_version.hpp +++ b/elfio/elfio_version.hpp @@ -1 +1 @@ -#define ELFIO_VERSION "3.11" +#define ELFIO_VERSION "3.12" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 28f39b9c404b1fe2746c0ff2b39f47a5c0358aa7..c65d3d10af74555495dc895a5b0460c201aec88c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,9 +1,18 @@ include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/9a32aee22d771387c494be2d8519fbdf46a713b2.zip -) +if(${CMAKE_VERSION} VERSION_LESS "3.24.0") + FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/41fe6be7d738237d1ca53070bd6ddebb73190b58.zip + ) +else() + FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/41fe6be7d738237d1ca53070bd6ddebb73190b58.zip + FIND_PACKAGE_ARGS NAMES GTest + DOWNLOAD_EXTRACT_TIMESTAMP = TRUE + ) +endif() # For Windows: Prevent overriding the parent project's compiler/linker settings set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) @@ -44,7 +53,8 @@ target_link_libraries( ELFIOTest PRIVATE elfio::elfio - gtest_main) + gtest_main + GTest::gtest_main) add_test( NAME