From 1c46aed72e9094e062e2d42138041e2171caf85c Mon Sep 17 00:00:00 2001 From: Richard Dzenis Date: Sat, 21 Dec 2024 07:13:42 +0200 Subject: [PATCH] MachO::Binary: treat all sections in the same way during shift (#1134) Previously there were exceptions for zerofill sections because offset checks were not correct. After fixing checks for offsets/va, special handling of zerofill sections is no longer needed. --- src/MachO/Binary.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/MachO/Binary.cpp b/src/MachO/Binary.cpp index e8a1d9e15..1fff52bc5 100644 --- a/src/MachO/Binary.cpp +++ b/src/MachO/Binary.cpp @@ -931,13 +931,10 @@ ok_error_t Binary::shift(size_t value) { } for (const std::unique_ptr
& section : segment->sections_) { - if (section->virtual_address() >= loadcommands_end_va || - section->type() == Section::TYPE::ZEROFILL) - { + if (section->virtual_address() >= loadcommands_end_va) { section->virtual_address(section->virtual_address() + value); } - - if (section->offset() >= loadcommands_end_va) { + if (section->offset() >= loadcommands_end) { section->offset(section->offset() + value); } } @@ -1207,21 +1204,19 @@ bool Binary::extend_segment(const SegmentCommand& segment, size_t size) { // Shift Segment and sections // ========================== for (SegmentCommand* segment : segments_) { + if (segment->virtual_address() >= last_va) { + segment->virtual_address(segment->virtual_address() + size_aligned); + } if (segment->file_offset() >= last_offset) { segment->file_offset(segment->file_offset() + size_aligned); - segment->virtual_address(segment->virtual_address() + size_aligned); } for (const std::unique_ptr
& section : segment->sections_) { - if (section->offset() >= last_offset) { - section->offset(section->offset() + size_aligned); + if (section->virtual_address() >= last_va) { section->virtual_address(section->virtual_address() + size_aligned); } - - if (section->type() == Section::TYPE::ZEROFILL && - section->virtual_address() > last_va) - { - section->virtual_address(section->virtual_address() + size_aligned); + if (section->offset() >= last_offset) { + section->offset(section->offset() + size_aligned); } } }