Skip to content
Snippets Groups Projects
Commit ed6a40ad authored by Serge Lamikhov-Center's avatar Serge Lamikhov-Center
Browse files

Proper calculate size difference between file_size/memory_size in case of section type SHT_NOBITS

parent 415b3d97
No related branches found
No related tags found
No related merge requests found
......@@ -228,20 +228,29 @@ void checkObjestsAreEqual( std::string file_name1, std::string file_name2 )
elfio file2;
BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true );
BOOST_CHECK_EQUAL( file1.save( file_name2 ), true );
BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true );
BOOST_REQUIRE_EQUAL( file2.load( file_name2 ), true );
for (int i = 0; i < file1.sections.size(); ++i ) {
const char* pdata1 = file1.sections[i]->get_data();
const char* pdata2 = file2.sections[i]->get_data();
if ( file1.sections[i]->get_type() == SHT_NULL ||
file1.sections[i]->get_type() == SHT_NOBITS ) {
continue;
}
BOOST_REQUIRE_NE( file1.sections[i]->get_data(), (const char*)0 );
BOOST_REQUIRE_NE( file2.sections[i]->get_data(), (const char*)0 );
std::string pdata1( file1.sections[i]->get_data(),
file1.sections[i]->get_data() +
file1.sections[i]->get_size() );
std::string pdata2( file2.sections[i]->get_data(),
file2.sections[i]->get_data() +
file2.sections[i]->get_size() );
BOOST_CHECK_EQUAL( file1.sections[i]->get_size(),
file2.sections[i]->get_size() );
if ( ( file2.sections[i]->get_type() != SHT_NULL ) &&
( file2.sections[i]->get_type() != SHT_NOBITS ) ) {
BOOST_CHECK_EQUAL_COLLECTIONS( pdata1,
pdata1 + file1.sections[i]->get_size(),
pdata2,
pdata2 + file2.sections[i]->get_size() );
BOOST_CHECK_EQUAL_COLLECTIONS( pdata1.begin(), pdata1.end(),
pdata2.begin(), pdata2.end() );
}
}
}
......@@ -255,21 +264,25 @@ void checkExeAreEqual( std::string file_name1, std::string file_name2 )
elfio file2;
BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true );
BOOST_CHECK_EQUAL( file1.save( file_name2 ), true );
BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true );
BOOST_REQUIRE_EQUAL( file2.load( file_name2 ), true );
for (int i = 0; i < file1.segments.size(); ++i ) {
const char* pdata1 = file1.segments[i]->get_data();
const char* pdata2 = file2.segments[i]->get_data();
BOOST_REQUIRE_NE( file1.segments[i]->get_data(), (const char*)0 );
BOOST_REQUIRE_NE( file2.segments[i]->get_data(), (const char*)0 );
std::string pdata1( file1.segments[i]->get_data(),
file1.segments[i]->get_data() +
file1.segments[i]->get_file_size() );
std::string pdata2( file2.segments[i]->get_data(),
file2.segments[i]->get_data() +
file2.segments[i]->get_file_size() );
BOOST_CHECK_EQUAL( file1.segments[i]->get_file_size(),
file2.segments[i]->get_file_size() );
if ( ( file2.segments[i]->get_type() != SHT_NULL ) &&
( file2.segments[i]->get_type() != SHT_NOBITS ) ) {
BOOST_CHECK_EQUAL_COLLECTIONS( pdata1,
pdata1 + file1.segments[i]->get_file_size(),
pdata2,
pdata2 + file2.segments[i]->get_file_size() );
}
BOOST_CHECK_EQUAL( file1.segments[i]->get_memory_size(),
file2.segments[i]->get_memory_size() );
// BOOST_CHECK_EQUAL_COLLECTIONS( pdata1.begin(), pdata1.end(),
// pdata2.begin(), pdata2.end() );
}
}
......@@ -289,22 +302,24 @@ BOOST_AUTO_TEST_CASE( elf_object_copy_32 )
"../elf_examples/write_obj_i386_64_copy.o" );
}
/*
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE( elf_exe_copy_32 )
{
checkExeAreEqual( "../elf_examples/ls",
"../elf_examples/ls_copy" );
checkExeAreEqual( "../elf_examples/hello_32",
"../elf_examples/hello_32_copy" );
checkExeAreEqual( "../elf_examples/asm",
"../elf_examples/asm_copy" );
checkExeAreEqual( "../elf_examples/asm64",
"../elf_examples/asm64_copy" );
checkExeAreEqual( "../elf_examples/hello_32",
"../elf_examples/hello_32_copy" );
checkExeAreEqual( "../elf_examples/hello_64",
"../elf_examples/hello_64_copy" );
checkExeAreEqual( "../elf_examples/test_ppc",
"../elf_examples/test_ppc_copy" );
}
*/
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE( section_header_address_update )
......
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x8048080
Start of program headers: 52 (bytes into file)
Start of section headers: 200 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 2
Size of section headers: 40 (bytes)
Number of section headers: 4
Section header string table index: 3
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 08048080 000080 00001d 00 AX 0 0 16
[ 2] .data PROGBITS 080490a0 0000a0 00000e 00 WA 0 0 4
[ 3] .shstrtab STRTAB 00000000 0000ae 000017 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000000 0x08048000 0x08048000 0x0009d 0x0009d R E 0x1000
LOAD 0x0000a0 0x080490a0 0x080490a0 0x0000e 0x0000e RW 0x1000
Section to Segment mapping:
Segment Sections...
00 .text
01 .data
There is no dynamic section in this file.
There are no relocations in this file.
The decoding of unwind sections for machine type Intel 80386 is not currently supported.
No version information found in this file.
No preview for this file type
No preview for this file type
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x8048080
Start of program headers: 52 (bytes into file)
Start of section headers: 116 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 2
Size of section headers: 40 (bytes)
Number of section headers: 4
Section header string table index: 3
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 08048000 001000 00001d 00 AX 0 0 16
[ 2] .data PROGBITS 080490a0 002000 00000e 00 WA 0 0 4
[ 3] .shstrtab STRTAB 00000000 000114 000017 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x001000 0x08048000 0x08048000 0x0001d 0x0001d R E 0x1000
LOAD 0x002000 0x080490a0 0x080490a0 0x0000e 0x0000e RW 0x1000
Section to Segment mapping:
Segment Sections...
00 .text
01 .data
There is no dynamic section in this file.
There are no relocations in this file.
The decoding of unwind sections for machine type Intel 80386 is not currently supported.
No version information found in this file.
No preview for this file type
No preview for this file type
File added
This diff is collapsed.
File added
This diff is collapsed.
No preview for this file type
......@@ -333,6 +333,9 @@ class elfio
section* sec = create_section();
sec->load( stream, (std::streamoff)offset + i * entry_size );
sec->set_index( i );
// To mark that the section is not permitted to reassign address
// during layout calculation
sec->set_address( sec->get_address() );
}
Elf_Half shstrndx = get_section_name_str_index();
......@@ -444,7 +447,8 @@ class elfio
sections_[i]->save( f, headerPosition, (std::streamoff)current_file_pos );
if ( SHT_NOBITS != sections_[i]->get_type() ) {
if ( SHT_NOBITS != sections_[i]->get_type() &&
SHT_NULL != sections_[i]->get_type() ) {
current_file_pos += sections_[i]->get_size();
}
}
......@@ -466,6 +470,7 @@ class elfio
}
Elf_Xword current_data_pos = current_file_pos;
Elf_Xword add_to_memory_size = 0;
// Write segment's data
for ( unsigned int j = 0; j <segments[i]->get_sections_num(); ++j ) {
section* sec = sections[ segments[i]->get_section_index_at( j )];
......@@ -481,12 +486,18 @@ class elfio
sec->set_address( segments[i]->get_virtual_address() );
}
sec->save( f, headerPosition, (std::streamoff)current_data_pos );
current_data_pos += sec->get_size();
if ( SHT_NOBITS != sec->get_type() && SHT_NULL != sec->get_type() ) {
current_data_pos += sec->get_size();
}
else {
add_to_memory_size += sec->get_size();
}
}
segments[i]->set_file_size( current_data_pos - current_file_pos );
segments[i]->set_memory_size( current_data_pos - current_file_pos +
segments[i]->get_memory_size() );
add_to_memory_size );
segments[i]->save( f, (std::streamoff)segment_header_position, (std::streamoff)current_file_pos );
current_file_pos = current_data_pos;
segment_header_position += header->get_segment_entry_size();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment