Skip to content

Commit

Permalink
Resolve #1130
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Dec 9, 2024
1 parent b5df89f commit bd6f627
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/MachO/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ ok_error_t Binary::shift(size_t value) {
}

shift_command(value, loadcommands_end);
const uint64_t loadcommands_end_va = loadcommands_end + load_cmd_segment->virtual_address();

LIEF_DEBUG("loadcommands_end: 0x{:016x}", loadcommands_end);
LIEF_DEBUG("loadcommands_end_va: 0x{:016x}", loadcommands_end_va);

// Shift Segment and sections
// ==========================
Expand All @@ -904,19 +908,23 @@ ok_error_t Binary::shift(size_t value) {
}
}
} else {
if (segment->virtual_address() >= loadcommands_end_va) {
segment->virtual_address(segment->virtual_address() + value);
}

if (segment->file_offset() >= loadcommands_end) {
segment->file_offset(segment->file_offset() + value);
segment->virtual_address(segment->virtual_address() + value);
}

for (const std::unique_ptr<Section>& section : segment->sections_) {
if (section->offset() >= loadcommands_end) {
section->offset(section->offset() + value);
if (section->virtual_address() >= loadcommands_end_va ||
section->type() == Section::TYPE::ZEROFILL)
{
section->virtual_address(section->virtual_address() + value);
}

if (section->type() == Section::TYPE::ZEROFILL) {
section->virtual_address(section->virtual_address() + value);
if (section->offset() >= loadcommands_end_va) {
section->offset(section->offset() + value);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/macho/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ def test_endianness():
target = lief.MachO.parse(get_sample("MachO/macho-issue-1110.bin")).at(0)

assert len(target.segments) == 3

def test_1130(tmp_path: Path):
target = lief.MachO.parse(get_sample("MachO/issue_1130.macho")).at(0)
target.shift(0x4000)

output = tmp_path / "new.macho"
target.write(output.as_posix())

new = lief.MachO.parse(output).at(0)
assert lief.MachO.check_layout(new)[0]

assert new.get_segment("__DATA").virtual_address == 0x100008000

0 comments on commit bd6f627

Please sign in to comment.