From bb3a08a4b926c99f334947aedc96f8fa6bf9f1dd Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Fri, 14 Jun 2024 22:44:22 +0300 Subject: [PATCH] Relate TLS sections to TLS segments only --- elfio/elfio.hpp | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index a75c9f5..49cfd3c 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -319,15 +319,15 @@ class elfio const section* b = sections[j]; if ( ( ( a->get_type() & SHT_NOBITS) == 0 ) && ( ( b->get_type() & SHT_NOBITS) == 0 ) - && ( a->get_size() > 0 ) - && ( b->get_size() > 0 ) - && ( a->get_offset() > 0 ) - && ( b->get_offset() > 0 ) - && ( is_offset_in_section( a->get_offset(), b ) - || is_offset_in_section( a->get_offset()+a->get_size()-1, b ) - || is_offset_in_section( b->get_offset(), a ) - || is_offset_in_section( b->get_offset()+b->get_size()-1, a ) ) ) { - errors += "Sections " + a->get_name() + " and " + b->get_name() + " overlap in file\n"; + && ( a->get_size() > 0 ) + && ( b->get_size() > 0 ) + && ( a->get_offset() > 0 ) + && ( b->get_offset() > 0 ) + && ( is_offset_in_section( a->get_offset(), b ) + || is_offset_in_section( a->get_offset()+a->get_size()-1, b ) + || is_offset_in_section( b->get_offset(), a ) + || is_offset_in_section( b->get_offset()+b->get_size()-1, a ) ) ) { + errors += "Sections " + a->get_name() + " and " + b->get_name() + " overlap in file\n"; } } } @@ -595,6 +595,14 @@ class elfio segVEndAddr ) : is_sect_in_seg( psec->get_offset(), psec->get_size(), segBaseOffset, segEndOffset ) ) { + + // If it is a TLS segment, add TLS sections only and vice versa + if ( ( ( seg->get_type() == PT_TLS ) && + ( ( psec->get_flags() & SHF_TLS ) != SHF_TLS ) ) || + ( ( ( psec->get_flags() & SHF_TLS ) == SHF_TLS ) && + ( seg->get_type() != PT_TLS ) ) ) + continue; + // Alignment of segment shall not be updated, to preserve original value // It will be re-calculated on saving. seg->add_section_index( psec->get_index(), 0 ); @@ -796,9 +804,9 @@ class elfio Elf64_Off cur_page_alignment = current_file_pos % align; Elf64_Off req_page_alignment = seg->get_virtual_address() % align; - Elf64_Off error = req_page_alignment - cur_page_alignment; + Elf64_Off adjustment = req_page_alignment - cur_page_alignment; - current_file_pos += ( seg->get_align() + error ) % align; + current_file_pos += ( seg->get_align() + adjustment ) % align; seg_start_pos = current_file_pos; } else if ( seg->get_sections_num() > 0 ) { @@ -1076,7 +1084,7 @@ class elfio std::unique_ptr header = nullptr; std::vector> sections_; std::vector> segments_; - endianness_convertor convertor; + endianness_convertor convertor; address_translator addr_translator; std::shared_ptr compression = nullptr;