Skip to content

MachO: adjust __init_offsets when add_section shifts __TEXT - #1368

Closed
je-empty wants to merge 1 commit into
lief-project:mainfrom
je-empty:fix/macho-init-offsets-shift
Closed

MachO: adjust __init_offsets when add_section shifts __TEXT#1368
je-empty wants to merge 1 commit into
lief-project:mainfrom
je-empty:fix/macho-init-offsets-shift

Conversation

@je-empty

@je-empty je-empty commented Aug 6, 2026

Copy link
Copy Markdown

add_section into __TEXT shifts the following content via Binary::shift, but shift doesn't update the image-base offsets stored in __TEXT,__init_offsets (S_INIT_FUNC_OFFSETS). They keep pointing at the old location, so dyld jumps into the wrong place and the binary crashes at launch (SIGILL). The fix bumps each entry past the shift point.

b = lief.MachO.parse("app").at(0)   # app has a constructor -> __init_offsets
b.add_section(lief.MachO.Section.create("__mysec", [0x41] * 0x4000))  # into __TEXT
b.write("out")   # SIGILLs on launch without the fix, runs with it

The constructor offset stays 0x4b0 while the code moves to 0x44b0; a section too small to force a shift is unaffected. Verified on macOS arm64 and an iPhone Air (iOS 26.6). Closes #1229.

@je-empty
je-empty force-pushed the fix/macho-init-offsets-shift branch from b7b747e to 9c322e9 Compare August 6, 2026 16:57
@je-empty je-empty changed the title [MachO] Adjust __init_offsets when add_section shifts __TEXT MachO: adjust __init_offsets when add_section shifts __TEXT Aug 6, 2026
@je-empty
je-empty force-pushed the fix/macho-init-offsets-shift branch 3 times, most recently from afa6327 to 14973dc Compare August 6, 2026 17:08
@je-empty
je-empty force-pushed the fix/macho-init-offsets-shift branch from 14973dc to d183861 Compare August 6, 2026 17:10
@je-empty

je-empty commented Aug 6, 2026

Copy link
Copy Markdown
Author

Superseded by #1369 — same fix, clean single-commit history.

@je-empty je-empty closed this Aug 6, 2026
@je-empty
je-empty deleted the fix/macho-init-offsets-shift branch August 6, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shifting the text segment invalidates load address relative addresses in MachO binaries

1 participant