diff --git a/elf_examples/write_exe_i386_32_match b/elf_examples/write_exe_i386_32_match index 4d1a9da4710c28767ee1bcf03e4c2fd8fd1fd40d..dc085d3701d94c88466c5641aafacc2e3374f0d4 100644 Binary files a/elf_examples/write_exe_i386_32_match and b/elf_examples/write_exe_i386_32_match differ diff --git a/elf_examples/write_obj_i386_32_match.o b/elf_examples/write_obj_i386_32_match.o index 48f0942d0f8e0fa0f3386ee0f350ad8a32c530cb..5137cf1dc99c5e5273363fc786189cbf901cb6c5 100644 Binary files a/elf_examples/write_obj_i386_32_match.o and b/elf_examples/write_obj_i386_32_match.o differ diff --git a/elf_examples/write_obj_i386_64_match.o b/elf_examples/write_obj_i386_64_match.o index 8335573feecfdb3b9004c53faf053f6eb551b11a..62c606717cbe34f9e826c455d62edde6a06a656e 100644 Binary files a/elf_examples/write_obj_i386_64_match.o and b/elf_examples/write_obj_i386_64_match.o differ diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 5007058b1b4d2439218c77f847168e3a7c43865e..ad71e43bd27ee5e2229f059141a1c773c6c24331 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -155,11 +155,23 @@ class elfio } bool is_still_good = true; - fill_final_attributes(); - set_current_file_position(); + + // 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 + // before saving. + header->set_segments_num( segments.size() ); + header->set_segments_offset( segments.size() ? header->get_header_size() : 0 ); + header->set_sections_num( sections.size() ); + header->set_sections_offset( 0 ); + + // layout the first section right after the segment table + current_file_pos = header->get_header_size() + + header->get_segment_entry_size() * header->get_segments_num(); 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(); is_still_good = is_still_good && save_header( f ); is_still_good = is_still_good && save_sections( f ); @@ -419,24 +431,6 @@ class elfio return true; } -//------------------------------------------------------------------------------ - void fill_final_attributes() - { - // Fill not completed fields in the header - header->set_segments_num( segments.size() ); - - if ( segments.size() == 0 ) { - header->set_segments_offset( 0 ); - } - else { - header->set_segments_offset( header->get_header_size() ); - } - - header->set_sections_num( sections.size() ); - header->set_sections_offset( header->get_header_size() + - header->get_segment_entry_size() * segments.size() ); - } - //------------------------------------------------------------------------------ bool save_header( std::ofstream& f ) { @@ -472,16 +466,6 @@ class elfio return true; } - -//------------------------------------------------------------------------------ - void set_current_file_position() - { - current_file_pos = header->get_header_size() + - header->get_segment_entry_size() * header->get_segments_num() + - header->get_section_entry_size() * header->get_sections_num(); - } - - //------------------------------------------------------------------------------ bool is_section_without_segment( unsigned int section_index ) { @@ -662,6 +646,15 @@ class elfio return true; } +//------------------------------------------------------------------------------ + bool layout_section_table() + { + // simply place the section table at the end for now + // TODO should this table be aligned? + header->set_sections_offset(current_file_pos); + return true; + } + //------------------------------------------------------------------------------ public: