5454#include " Binary.tcc"
5555#include " Object.tcc"
5656#include " internal_utils.hpp"
57+ #include " paging.hpp"
5758
5859
5960namespace LIEF ::ELF {
@@ -2767,12 +2768,72 @@ uint64_t Binary::relocate_phdr_table(PHDR_RELOC type) {
27672768 }
27682769}
27692770
2770- uint64_t Binary::relocate_phdr_table_auto () {
2771- if (phdr_reloc_info_.new_offset > 0 ) {
2772- // Already relocated
2771+ /* If the segment table has already been relocated (which is the case for the
2772+ * binaries already processed by LIEF), this area can still have room for the
2773+ * segments we need to add.
2774+ */
2775+ uint64_t Binary::reuse_phdr_table () {
2776+ const Segment* pt_phdr = get (Segment::TYPE ::PHDR );
2777+ if (pt_phdr == nullptr ) {
2778+ return 0 ;
2779+ }
2780+
2781+ const uint64_t phdr_offset = header_.program_headers_offset ();
2782+
2783+ // PT_PHDR must describe the current location of the segment table
2784+ if (pt_phdr->file_offset () != phdr_offset || phdr_offset == 0 ) {
2785+ return 0 ;
2786+ }
2787+
2788+ const uint64_t phdr_size = type () == Header::CLASS ::ELF32 ?
2789+ sizeof (details::ELF32 ::Elf_Phdr) :
2790+ sizeof (details::ELF64 ::Elf_Phdr);
2791+
2792+ const uint64_t used = (uint64_t )header_.numberof_segments () * phdr_size;
2793+ const uint64_t reserved = pt_phdr->physical_size ();
2794+
2795+ if (reserved <= used) {
2796+ return 0 ;
2797+ }
2798+
2799+ const size_t available = (reserved - used) / phdr_size;
2800+
2801+ // The reserved area must be loaded, otherwise the new segments would not be
2802+ // mapped by the loader
2803+ const uint64_t phdr_end = phdr_offset + reserved;
2804+ const bool wrapped =
2805+ std::any_of (segments_.begin (), segments_.end (),
2806+ [phdr_offset, phdr_end](const std::unique_ptr<Segment>& s) {
2807+ return s->is_load () && s->file_offset () <= phdr_offset &&
2808+ phdr_end <= (s->file_offset () + s->physical_size ());
2809+ });
2810+
2811+ if (!wrapped) {
2812+ return 0 ;
2813+ }
2814+
2815+ LIEF_DEBUG (" Reusing the current segment table location ({:#x}) which can "
2816+ " still hold {} segments" ,
2817+ phdr_offset, available);
2818+
2819+ phdr_reloc_info_.new_offset = phdr_offset;
2820+ phdr_reloc_info_.nb_segments = available;
2821+ return phdr_offset;
2822+ }
2823+
2824+ uint64_t Binary::relocate_phdr_table_auto (size_t required_segments) {
2825+ if (phdr_reloc_info_.new_offset > 0 &&
2826+ phdr_reloc_info_.nb_segments >= required_segments)
2827+ {
27732828 return phdr_reloc_info_.new_offset ;
27742829 }
27752830
2831+ if (uint64_t offset = reuse_phdr_table ();
2832+ offset > 0 && phdr_reloc_info_.nb_segments >= required_segments)
2833+ {
2834+ return offset;
2835+ }
2836+
27762837 const bool has_phdr_s = has (Segment::TYPE ::PHDR );
27772838 const bool has_interp_s = has (Segment::TYPE ::INTERP );
27782839 const bool has_soname = has (DynamicEntry::TAG ::SONAME );
@@ -2782,15 +2843,19 @@ uint64_t Binary::relocate_phdr_table_auto() {
27822843
27832844 uint64_t offset = 0 ;
27842845 if (is_dyn && (has_phdr_s || has_interp_s || has_soname)) {
2785- if (offset = relocate_phdr_table_pie (); offset > 0 ) {
2846+ if (offset = relocate_phdr_table_pie ();
2847+ offset > 0 && phdr_reloc_info_.nb_segments >= required_segments)
2848+ {
27862849 return offset;
27872850 }
27882851 LIEF_ERR (" Failed to relocate PHDR table for this PIE binary" );
27892852 }
27902853
27912854 if (is_dyn && !(has_phdr_s || has_interp_s) && !has_ep) {
27922855 // See libm-ubuntu24.so
2793- if (offset = relocate_phdr_table_pie (); offset > 0 ) {
2856+ if (offset = relocate_phdr_table_pie ();
2857+ offset > 0 && phdr_reloc_info_.nb_segments >= required_segments)
2858+ {
27942859 return offset;
27952860 }
27962861 }
@@ -2801,7 +2866,7 @@ uint64_t Binary::relocate_phdr_table_auto() {
28012866 if (is_valid_for_v3) {
28022867 LIEF_DEBUG (" Try v3 relocator" );
28032868 offset = relocate_phdr_table_v3 ();
2804- if (offset > 0 ) {
2869+ if (offset > 0 && phdr_reloc_info_. nb_segments >= required_segments ) {
28052870 return offset;
28062871 }
28072872 }
@@ -2815,10 +2880,14 @@ uint64_t Binary::relocate_phdr_table_auto() {
28152880 return 0 ;
28162881 }
28172882 }
2818- return offset;
2883+ if (phdr_reloc_info_.new_offset == 0 ||
2884+ phdr_reloc_info_.nb_segments < required_segments)
2885+ {
2886+ return 0 ;
2887+ }
2888+ return phdr_reloc_info_.new_offset ;
28192889}
28202890
2821-
28222891uint64_t Binary::relocate_phdr_table_pie () {
28232892
28242893 if (phdr_reloc_info_.new_offset > 0 ) {
@@ -2861,7 +2930,12 @@ uint64_t Binary::relocate_phdr_table_pie() {
28612930 * header_.numberof_segments();
28622931 * }
28632932 */
2864- static constexpr size_t shift = 0x1000 ;
2933+ static constexpr uint64_t DEFAULT_SHIFT = 0x1000 ;
2934+
2935+ const uint64_t pagesize = layout_pagesize ();
2936+ const uint64_t shift = align (DEFAULT_SHIFT , pagesize);
2937+
2938+ LIEF_DEBUG (" Shift: {:#x} (default: {:#x})" , shift, DEFAULT_SHIFT );
28652939
28662940 phdr_reloc_info_.new_offset = from;
28672941 phdr_reloc_info_.nb_segments = shift / phdr_size - header_.numberof_segments ();
@@ -2872,8 +2946,6 @@ uint64_t Binary::relocate_phdr_table_pie() {
28722946 return 0 ;
28732947 }
28742948
2875- LIEF_DEBUG (" Header shift: {:#x}" , shift);
2876-
28772949 header ().section_headers_offset (header ().section_headers_offset () + shift);
28782950
28792951 shift_sections (from, shift);
@@ -2922,7 +2994,8 @@ uint64_t Binary::relocate_phdr_table_v3() {
29222994 std::max (last_off, segment->physical_size () + segment->file_offset ());
29232995 }
29242996 }
2925- uint64_t last_off_aligned = align (last_off, page_size ());
2997+ const uint64_t pagesize = layout_pagesize ();
2998+ uint64_t last_off_aligned = align (last_off, pagesize);
29262999
29273000 if (phdr_reloc_info_.new_offset > 0 ) {
29283001 return phdr_reloc_info_.new_offset ;
@@ -2962,9 +3035,12 @@ uint64_t Binary::relocate_phdr_table_v3() {
29623035 phdr_load_segment->file_offset (phdr_reloc_info_.new_offset );
29633036 phdr_load_segment->physical_size (new_segtbl_sz);
29643037 phdr_load_segment->virtual_size (new_segtbl_sz);
2965- phdr_load_segment->virtual_address (imagebase () + virtual_size ());
3038+ // This segment must not share a page with the last PT_LOAD segment, otherwise
3039+ // the loader would map both at the same address (cf. issue #1366)
3040+ phdr_load_segment->virtual_address (align (imagebase () + virtual_size (),
3041+ pagesize));
29663042 phdr_load_segment->physical_address (phdr_load_segment->virtual_address ());
2967- phdr_load_segment->alignment (0x1000 );
3043+ phdr_load_segment->alignment (pagesize );
29683044 phdr_load_segment->add (Segment::FLAGS ::R);
29693045 phdr_load_segment->datahandler_ = datahandler_.get ();
29703046
@@ -3073,10 +3149,15 @@ uint64_t Binary::relocate_phdr_table_v2() {
30733149 * LOAD_2.virtual_size
30743150 * \ /
30753151 * ------------ Offset --------------------/
3076- * which should be aligned on a page size to avoid error.
3152+ * which should be aligned on the page size for which the binary has been
3153+ * laid out to avoid error. Aligning on a smaller value would let this new
3154+ * segment share a page with the bss-like segment, which the loader can't map
3155+ * independently (cf. issue #1366).
30773156 *
3078- * The issue https://github.com/lief-project/LIEF/issues/671
3079- * is a good example of what could go wrong.
3157+ * The issues
3158+ * - https://github.com/lief-project/LIEF/issues/671
3159+ * - https://github.com/lief-project/LIEF/issues/1366
3160+ * are a good example of what could go wrong.
30803161 *
30813162 * |WARNING|
30823163 * This modification can increase the binary size drastically
@@ -3094,7 +3175,7 @@ uint64_t Binary::relocate_phdr_table_v2() {
30943175 const uint64_t new_phdr_offset =
30953176 align (bss_segment->virtual_address () - imagebase () +
30963177 bss_segment->virtual_size (),
3097- 0x1000 );
3178+ layout_pagesize () );
30983179
30993180 const size_t nb_segments = header.numberof_segments () +
31003181 /* custom PT_LOAD */ 1 + USER_SEGMENTS ;
@@ -3158,6 +3239,11 @@ uint64_t Binary::relocate_phdr_table_v2() {
31583239 phdr_segment->file_offset (nsegment_addr->file_offset ());
31593240 phdr_segment->virtual_address (nsegment_addr->virtual_address ());
31603241 phdr_segment->physical_address (nsegment_addr->physical_address ());
3242+ // PT_PHDR must describe the area we just reserved. Otherwise, it would
3243+ // keep the size of its previous location which is likely to no longer be
3244+ // wrapped by a PT_LOAD segment.
3245+ phdr_segment->physical_size (nsegment_addr->physical_size ());
3246+ phdr_segment->virtual_size (nsegment_addr->virtual_size ());
31613247 phdr_segment->clear ();
31623248 }
31633249
@@ -3439,6 +3525,27 @@ uint64_t Binary::page_size() const {
34393525 return LIEF::Binary::page_size ();
34403526}
34413527
3528+ uint64_t Binary::layout_pagesize () const {
3529+ uint64_t alignment = 0 ;
3530+ for (const Segment& segment : segments ()) {
3531+ if (segment.is_load ()) {
3532+ alignment = std::max (alignment, segment.alignment ());
3533+ }
3534+ }
3535+
3536+ const uint64_t page_size = this ->page_size ();
3537+
3538+ // Check that the alignment used by the PT_LOAD segment is valid.
3539+ // Otherwise, default to the page_size
3540+ if (alignment == 0 || (alignment & (alignment - 1 )) != 0 ) {
3541+ alignment = page_size;
3542+ }
3543+
3544+ return std::max<uint64_t >(
3545+ page_size, std::min<uint64_t >(alignment, get_max_pagesize (*this ))
3546+ );
3547+ }
3548+
34423549const SymbolVersionRequirement*
34433550 Binary::find_version_requirement (const std::string& libname) const {
34443551 auto it = std::find_if (
0 commit comments