Refactoring

This commit is contained in:
Serge Lamikhov-Center 2016-04-23 17:35:32 +03:00
parent ecc45ede27
commit 8e7a29e128
6 changed files with 32 additions and 19 deletions

View File

@ -75,6 +75,7 @@ class elfio
elfio() : sections( this ), segments( this ) elfio() : sections( this ), segments( this )
{ {
header = 0; header = 0;
current_file_pos = 0;
create( ELFCLASS32, ELFDATA2LSB ); create( ELFCLASS32, ELFDATA2LSB );
} }
@ -573,7 +574,6 @@ class elfio
{ {
std::vector<segment*> worklist; std::vector<segment*> worklist;
std::vector<bool> section_generated(sections.size(),false); std::vector<bool> section_generated(sections.size(),false);
std::vector<Elf_Xword> section_alignment(sections.size(),0);
// Get segments in a order in where segments which contain a // Get segments in a order in where segments which contain a
// sub sequence of other segments are located at the end // sub sequence of other segments are located at the end

View File

@ -74,7 +74,12 @@ class dynamic_section_accessor
tag == DT_RUNPATH ) { tag == DT_RUNPATH ) {
string_section_accessor strsec = string_section_accessor strsec =
elf_file.sections[ get_string_table_index() ]; elf_file.sections[ get_string_table_index() ];
str = strsec.get_string( value ); const char* result = strsec.get_string( value );
if ( 0 == result ) {
str.clear();
return false;
}
str = result;
} }
else { else {
str.clear(); str.clear();

View File

@ -154,7 +154,7 @@ class relocation_section_accessor
unsigned char other; unsigned char other;
symbol_section_accessor symbols( elf_file, elf_file.sections[get_symbol_table_index()] ); symbol_section_accessor symbols( elf_file, elf_file.sections[get_symbol_table_index()] );
ret = symbols.get_symbol( symbol, symbolName, symbolValue, ret = ret && symbols.get_symbol( symbol, symbolName, symbolValue,
size, bind, symbolType, section, other ); size, bind, symbolType, section, other );
if ( ret ) { // Was it successful? if ( ret ) { // Was it successful?
@ -217,10 +217,10 @@ class relocation_section_accessor
{ {
Elf_Xword info; Elf_Xword info;
if ( elf_file.get_class() == ELFCLASS32 ) { if ( elf_file.get_class() == ELFCLASS32 ) {
info = ELF32_R_INFO( symbol, type ); info = ELF32_R_INFO( (Elf_Xword)symbol, type );
} }
else { else {
info = ELF64_R_INFO( symbol, type ); info = ELF64_R_INFO((Elf_Xword)symbol, type );
} }
add_entry( offset, info ); add_entry( offset, info );
@ -245,10 +245,10 @@ class relocation_section_accessor
{ {
Elf_Xword info; Elf_Xword info;
if ( elf_file.get_class() == ELFCLASS32 ) { if ( elf_file.get_class() == ELFCLASS32 ) {
info = ELF32_R_INFO( symbol, type ); info = ELF32_R_INFO( (Elf_Xword)symbol, type );
} }
else { else {
info = ELF64_R_INFO( symbol, type ); info = ELF64_R_INFO( (Elf_Xword)symbol, type );
} }
add_entry( offset, info, addend ); add_entry( offset, info, addend );

View File

@ -150,7 +150,9 @@ class section_impl : public section
try { try {
data = new char[size]; data = new char[size];
} catch (const std::bad_alloc&) { } catch (const std::bad_alloc&) {
data = 0;
data_size = 0; data_size = 0;
size = 0;
} }
if ( 0 != data && 0 != raw_data ) { if ( 0 != data && 0 != raw_data ) {
data_size = size; data_size = size;
@ -183,6 +185,7 @@ class section_impl : public section
new_data = new char[data_size]; new_data = new char[data_size];
} catch (const std::bad_alloc&) { } catch (const std::bad_alloc&) {
new_data = 0; new_data = 0;
size = 0;
} }
if ( 0 != new_data ) { if ( 0 != new_data ) {
std::copy( data, data + get_size(), new_data ); std::copy( data, data + get_size(), new_data );
@ -228,6 +231,7 @@ class section_impl : public section
try { try {
data = new char[size]; data = new char[size];
} catch (const std::bad_alloc&) { } catch (const std::bad_alloc&) {
data = 0;
data_size = 0; data_size = 0;
} }
if ( 0 != size ) { if ( 0 != size ) {

View File

@ -109,9 +109,9 @@ class segment_impl : public segment
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
Elf_Half Elf_Half
add_section_index( Elf_Half index, Elf_Xword addr_align ) add_section_index( Elf_Half sec_index, Elf_Xword addr_align )
{ {
sections.push_back( index ); sections.push_back( sec_index );
if ( addr_align > get_align() ) { if ( addr_align > get_align() ) {
set_align( addr_align ); set_align( addr_align );
} }

View File

@ -61,8 +61,11 @@ class string_section_accessor
Elf_Word Elf_Word
add_string( const char* str ) add_string( const char* str )
{ {
Elf_Word current_position = 0;
if (string_section) {
// Strings are addeded to the end of the current section data // Strings are addeded to the end of the current section data
Elf_Word current_position = (Elf_Word)string_section->get_size(); current_position = (Elf_Word)string_section->get_size();
if ( current_position == 0 ) { if ( current_position == 0 ) {
char empty_string = '\0'; char empty_string = '\0';
@ -70,6 +73,7 @@ class string_section_accessor
current_position++; current_position++;
} }
string_section->append_data( str, (Elf_Word)std::strlen( str ) + 1 ); string_section->append_data( str, (Elf_Word)std::strlen( str ) + 1 );
}
return current_position; return current_position;
} }