From 9a4f609aa675571d506a54b5d97ebcf767cb338a Mon Sep 17 00:00:00 2001 From: Richard Dzenis Date: Tue, 7 Jan 2025 18:44:22 +0200 Subject: [PATCH] MachO::Binary::extend_section fill gap by default The previous implementation extended section by strictly requested amount, possibly creating a gap in case we needed to satisfy alignment requirements. However, information about the size of the gap is lost, and not trivial to recover. This patch changes `extend_section` to extend section by more than requested to avoid creating the gap. --- include/LIEF/MachO/Binary.hpp | 5 +++-- src/MachO/Binary.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/LIEF/MachO/Binary.hpp b/include/LIEF/MachO/Binary.hpp index 47a43e2b6..8775e7efb 100644 --- a/include/LIEF/MachO/Binary.hpp +++ b/include/LIEF/MachO/Binary.hpp @@ -411,8 +411,9 @@ class LIEF_API Binary : public LIEF::Binary { bool extend_segment(const SegmentCommand& segment, size_t size); /// Extend the **content** of the given Section. - /// @note This method may create a gap between the current section and the next one, if `size` - /// is not multiple of the maximum alignment of sections before the current one. + /// @note This method may extend the section more than `size` preventing creation a gap + /// between the current section and the next one. + /// This may happen trying to satisfy alignment requirement of sections. /// @note This method works only with sections that belong to the first segment. bool extend_section(Section& section, size_t size); diff --git a/src/MachO/Binary.cpp b/src/MachO/Binary.cpp index 58618e87a..2a4665355 100644 --- a/src/MachO/Binary.cpp +++ b/src/MachO/Binary.cpp @@ -1305,7 +1305,7 @@ bool Binary::extend_section(Section& section, size_t size) { } // Extend the given `section`. - section.size(section.size() + size); + section.size(section.size() + shift_value); return true; }