Refactoring

This commit is contained in:
Serge Lamikhov-Center 2021-08-16 22:32:48 +03:00
parent 7dcfe3d86d
commit 1f79600cec
5 changed files with 38 additions and 56 deletions

View File

@ -37,7 +37,6 @@ THE SOFTWARE.
#include <algorithm>
#include <vector>
#include <deque>
#include <iterator>
#include <elfio/elf_types.hpp>
#include <elfio/elfio_version.hpp>
@ -167,7 +166,6 @@ class elfio
return false;
}
bool is_still_good = true;
// Define layout specific header fields
// The position of the segment table is fixed after the header.
// The position of the section table is variable and needs to be fixed
@ -185,7 +183,7 @@ class elfio
calc_segment_alignment();
is_still_good = layout_segments_and_their_sections();
bool is_still_good = layout_segments_and_their_sections();
is_still_good = is_still_good && layout_sections_without_segments();
is_still_good = is_still_good && layout_section_table();
@ -298,9 +296,9 @@ class elfio
for ( int h = 0; h < segments.size(); ++h ) {
const segment* seg = segments[h];
if ( seg->get_type() == PT_LOAD && seg->get_file_size() > 0 ) {
auto sec = find_prog_section_for_offset( seg->get_offset() );
const section* sec = find_prog_section_for_offset( seg->get_offset() );
if ( sec ) {
auto sec_addr = get_virtual_addr( seg->get_offset(), sec );
Elf64_Addr sec_addr = get_virtual_addr( seg->get_offset(), sec );
if ( sec_addr != seg->get_virtual_address() ) {
errors += "Virtual address of segment " + std::to_string( h ) + " (" + to_hex_string( seg->get_virtual_address() ) + ")"
+ " conflicts with address of section " + sec->get_name() + " (" + to_hex_string( sec_addr ) + ")"
@ -333,11 +331,10 @@ class elfio
//------------------------------------------------------------------------------
const section* find_prog_section_for_offset( Elf64_Off offset ) const
{
for ( int i = 0; i < sections.size(); ++i ) {
const section* sec = sections[i];
if ( sec->get_type() == SHT_PROGBITS )
if ( is_offset_in_section( offset, sec ) )
return sec;
for ( auto sec : sections ) {
if ( sec->get_type() == SHT_PROGBITS &&
is_offset_in_section( offset, sec ) )
return sec;
}
return NULL;
}
@ -348,15 +345,13 @@ class elfio
delete header;
header = 0;
std::vector<section*>::const_iterator it;
for ( it = sections_.begin(); it != sections_.end(); ++it ) {
delete *it;
for ( auto it : sections_ ) {
delete it;
}
sections_.clear();
std::vector<segment*>::const_iterator it1;
for ( it1 = segments_.begin(); it1 != segments_.end(); ++it1 ) {
delete *it1;
for ( auto it : segments_ ) {
delete it;
}
segments_.clear();
}
@ -399,7 +394,7 @@ class elfio
}
new_section->set_index( (Elf_Half)sections_.size() );
sections_.push_back( new_section );
sections_.emplace_back( new_section );
return new_section;
}
@ -421,7 +416,7 @@ class elfio
}
new_segment->set_index( (Elf_Half)segments_.size() );
segments_.push_back( new_segment );
segments_.emplace_back( new_segment );
return new_segment;
}
@ -522,9 +517,7 @@ class elfio
Elf64_Off segEndOffset = segBaseOffset + seg->get_file_size();
Elf64_Off segVBaseAddr = seg->get_virtual_address();
Elf64_Off segVEndAddr = segVBaseAddr + seg->get_memory_size();
for ( Elf_Half j = 0; j < sections.size(); ++j ) {
const section* psec = sections[j];
for ( auto psec : sections ) {
// SHF_ALLOC sections are matched based on the virtual address
// otherwise the file offset is matched
if ( ( psec->get_flags() & SHF_ALLOC )
@ -540,7 +533,7 @@ class elfio
}
// Add section into the segments' container
segments_.push_back( seg );
segments_.emplace_back( seg );
}
return true;
@ -552,9 +545,7 @@ class elfio
//------------------------------------------------------------------------------
bool save_sections( std::ostream& stream )
{
for ( unsigned int i = 0; i < sections_.size(); ++i ) {
section* sec = sections_.at( i );
for ( auto sec : sections_ ) {
std::streampos headerPosition =
(std::streamoff)header->get_sections_offset() +
(std::streampos)header->get_section_entry_size() *
@ -568,9 +559,7 @@ class elfio
//------------------------------------------------------------------------------
bool save_segments( std::ostream& stream )
{
for ( unsigned int i = 0; i < segments_.size(); ++i ) {
segment* seg = segments_.at( i );
for ( auto seg : segments_ ) {
std::streampos headerPosition =
header->get_segments_offset() +
(std::streampos)header->get_segment_entry_size() *
@ -647,9 +636,9 @@ class elfio
}
if ( i < worklist.size() )
worklist.push_back( seg );
worklist.emplace_back( seg );
else
res.push_back( seg );
res.emplace_back( seg );
}
return res;
@ -685,9 +674,7 @@ class elfio
//------------------------------------------------------------------------------
void calc_segment_alignment()
{
for ( std::vector<segment*>::iterator s = segments_.begin();
s != segments_.end(); ++s ) {
segment* seg = *s;
for ( auto seg : segments_ ) {
for ( int i = 0; i < seg->get_sections_num(); ++i ) {
section* sect = sections_[seg->get_section_index_at( i )];
if ( sect->get_addr_align() > seg->get_align() ) {
@ -707,7 +694,7 @@ class elfio
// sub sequence of other segments are located at the end
worklist = get_ordered_segments();
for ( unsigned int i = 0; i < worklist.size(); ++i ) {
for ( auto i = 0; i < worklist.size(); ++i ) {
Elf_Xword segment_memory = 0;
Elf_Xword segment_filesize = 0;
Elf_Xword seg_start_pos = current_file_pos;
@ -747,7 +734,7 @@ class elfio
}
// Write segment's data
for ( unsigned int j = 0; j < seg->get_sections_num(); ++j ) {
for ( auto j = 0; j < seg->get_sections_num(); ++j ) {
Elf_Half index = seg->get_section_index_at( j );
section* sec = sections[index];
@ -880,11 +867,9 @@ class elfio
{
section* sec = 0;
std::vector<section*>::const_iterator it;
for ( it = parent->sections_.begin(); it != parent->sections_.end();
++it ) {
if ( ( *it )->get_name() == name ) {
sec = *it;
for ( auto it : parent->sections_ ) {
if ( it->get_name() == name ) {
sec = it;
break;
}
}

View File

@ -57,9 +57,9 @@ template <class S> class modinfo_section_accessor_template
//------------------------------------------------------------------------------
bool get_attribute( const std::string field_name, std::string& value ) const
{
for ( auto i = content.begin(); i != content.end(); i++ ) {
if ( field_name == i->first ) {
value = i->second;
for ( auto i : content ) {
if ( field_name == i.first ) {
value = i.second;
return true;
}
}
@ -79,8 +79,7 @@ template <class S> class modinfo_section_accessor_template
std::string attribute = field + "=" + value;
modinfo_section->append_data( attribute + '\0' );
content.push_back(
std::pair<std::string, std::string>( field, value ) );
content.emplace_back( field, value );
}
return current_position;
@ -97,12 +96,10 @@ template <class S> class modinfo_section_accessor_template
while ( i < modinfo_section->get_size() && !pdata[i] )
i++;
if ( i < modinfo_section->get_size() ) {
std::string info = pdata + i;
size_t loc = info.find( '=' );
std::pair<std::string, std::string> attribute(
info.substr( 0, loc ), info.substr( loc + 1 ) );
content.push_back( attribute );
std::string info = pdata + i;
size_t loc = info.find( '=' );
content.emplace_back( info.substr( 0, loc ),
info.substr( loc + 1 ) );
i += info.length();
}

View File

@ -123,7 +123,7 @@ template <class S> class note_section_accessor_template
}
}
note_start_positions.push_back( note_section->get_size() );
note_start_positions.emplace_back( note_section->get_size() );
note_section->append_data( buffer );
}
@ -145,7 +145,7 @@ template <class S> class note_section_accessor_template
Elf_Word align = sizeof( Elf_Word );
while ( current + (Elf_Xword)3 * align <= size ) {
note_start_positions.push_back( current );
note_start_positions.emplace_back( current );
Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) );
Elf_Word descsz = convertor(
*(const Elf_Word*)( data + current + sizeof( namesz ) ) );

View File

@ -108,7 +108,7 @@ template <class T> class segment_impl : public segment
//------------------------------------------------------------------------------
Elf_Half add_section_index( Elf_Half sec_index, Elf_Xword addr_align )
{
sections.push_back( sec_index );
sections.emplace_back( sec_index );
if ( addr_align > get_align() ) {
set_align( addr_align );
}

View File

@ -849,7 +849,7 @@ BOOST_AUTO_TEST_CASE( rearrange_local_symbols_with_reallocation )
rela.get_entry( i, offset, symbol, rtype, addend );
symbols.get_symbol( symbol, name, value, size, bind, type,
section_index, other );
before.push_back( name );
before.emplace_back( name );
}
symbols.arrange_local_symbols( [&]( Elf_Xword first, Elf_Xword second ) {
@ -883,7 +883,7 @@ BOOST_AUTO_TEST_CASE( rearrange_local_symbols_with_reallocation )
rel.get_entry( i, offset, symbol, rtype, addend );
syms.get_symbol( symbol, name, value, size, bind, type, section_index,
other );
after.push_back( name );
after.emplace_back( name );
}
BOOST_CHECK_EQUAL_COLLECTIONS( before.begin(), before.end(), after.begin(),