Mach-O : cannot set bind_opcodes
Describe the bug
I try to set target.dyld_info.bind_opcodes on a MachO binary. The property seems to be set to the expected value before the write method, but is back to the previous value when write the modified binary.
To Reproduce
binary = lief.MachO.parse("/bin/ls")
target = binary.at(1) # with target.header.cpu_type == lief.MachO.CPU_TYPES.ARM64
opcodes = target.dyld_info.bind_opcodes.tobytes()
print("origin =", opcodes[:20].hex())
# change opcode
target.dyld_info.bind_opcodes = [0] * len(opcodes)
print("change =", target.dyld_info.bind_opcodes.tobytes()[:20].hex())
# write library in outputfile
target.write(outputfile)
print("write =", target.dyld_info.bind_opcodes.tobytes()[:20].hex())
# get opcode of outputfile
binary2 = lief.MachO.parse(outputfile)
print("outputfile =", binary2.at(0).dyld_info.bind_opcodes.tobytes()[:20].hex())
returns
origin = d05a11405f68756d616e697a655f6e756d626572
change = 0000000000000000000000000000000000000000
write = d05a11405f68756d616e697a655f6e756d626572
outputfile = d05a11405f68756d616e697a655f6e756d626572
Expected behavior
The opcode is replaced in the output binary.
origin = d05a11405f68756d616e697a655f6e756d626572
change = 0000000000000000000000000000000000000000
write = 0000000000000000000000000000000000000000
outputfile = 0000000000000000000000000000000000000000
Environment (please complete the following information):
- System and Version : macOS 13.1 / Python3.9
- Target format : Mach-O
- LIEF commit version: 0.12.3-e34a2fb7 (Installed with
pip install lief)
Additional context n/a
Hi Nicolas,
Yes actually you can't modify the raw Dyld opcodes as they are recomputed in the build process. Nevertheless I could add an option in the build process to avoid this re-computation.
I finally didn't manage to do what I wanted by modifying bind_opcodes outside of lief.
Another solution could be to mark the property as readonly.