From 91e61ec4b2f74f717c50d3a62b7b3348487f86ce Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Wed, 3 Dec 2014 10:38:44 +0100 Subject: [PATCH] use the NULL section instead of offset == 0 This change makes it possible to use the regular API to build up elf files which include the file header. --- elfio/elfio.hpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 41413f4..7b55f3e 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -412,14 +412,13 @@ class elfio // SHF_ALLOC sections are matched based on the virtual address // otherwise the file offset is matched - if( psec->get_type() != SHT_NULL - && (psec->get_flags() & SHF_ALLOC + if( psec->get_flags() & SHF_ALLOC ? (segVBaseAddr <= psec->get_address() && psec->get_address() + psec->get_size() <= segVEndAddr) : (segBaseOffset <= psec->get_offset() && psec->get_offset() + psec->get_size() - <= segEndOffset))) { + <= segEndOffset)) { seg->add_section_index( psec->get_index(), psec->get_addr_align() ); } @@ -583,15 +582,17 @@ class elfio Elf_Xword segment_filesize = 0; Elf_Xword seg_start_pos = current_file_pos; segment* seg = worklist[i]; - + // special case: PHDR segments // this segment contains the program headers but no sections if( seg->get_type() == PT_PHDR && seg->get_sections_num() == 0 ) { seg_start_pos = header->get_segments_offset(); segment_memory = segment_filesize = header->get_segment_entry_size() * header->get_segments_num(); - // special case: segments with offset 0 - } else if ( seg->is_offset_initialized() && seg->get_offset() == 0 ) { + // special case: + // segments which start with the NULL section and have further sections + } else if ( seg->get_sections_num() > 1 + && sections[seg->get_section_index_at( 0 )]->get_type() == SHT_NULL ) { seg_start_pos = 0; if( seg->get_sections_num() ) segment_memory = segment_filesize = current_file_pos; @@ -615,6 +616,12 @@ class elfio section* sec = sections[ index ]; + // the NULL section is always generated + if( SHT_NULL == sec->get_type()) { + section_generated[index] = true; + continue; + } + Elf_Xword secAlign = 0; // fix up the alignment if ( !section_generated[index] && sec->is_address_initialized()