Skip to content

Commit

Permalink
MachO::Binary: treat all sections in the same way during shift (#1134)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DzenIsRich authored Dec 21, 2024
1 parent a2f0f26 commit 1c46aed
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/MachO/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,13 +931,10 @@ ok_error_t Binary::shift(size_t value) {
}

for (const std::unique_ptr<Section>& 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);
}
}
Expand Down Expand Up @@ -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>& 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);
}
}
}
Expand Down

0 comments on commit 1c46aed

Please sign in to comment.