Skip to content

Instantly share code, notes, and snippets.

@0cyn
Last active December 13, 2021 10:10
Show Gist options
  • Select an option

  • Save 0cyn/b577dafe844d26350b051b482bb71268 to your computer and use it in GitHub Desktop.

Select an option

Save 0cyn/b577dafe844d26350b051b482bb71268 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys, os
from io import BytesIO
from argparse import ArgumentParser
# pip3 install k2l
import ktool
from kmacho import CPUSubTypeARM64
slices = []
def handle_file(fp) -> BytesIO:
macho_file = ktool.load_macho_file(fp)
new_slice_fp = BytesIO()
for macho_slice in macho_file.slices:
if macho_slice.subtype == CPUSubTypeARM64.ARM64E2:
new_slice_fp.write(macho_slice.full_bytes_for_slice())
new_slice_fp.seek(0)
slices.append(macho_slice)
img = ktool.load_image(new_slice_fp, use_mmaped_io=False)
img.macho_header.dyld_header.cpu_subtype = CPUSubTypeARM64.ARM64E.value
# isn't really designed to operate perfectly on non-existient files,
# but we can make it work
img.slice.patch(0, img.macho_header.dyld_header.raw)
new_image_fp = BytesIO()
new_image_fp.write(img.slice.full_bytes_for_slice())
new_image_fp.seek(0)
img = ktool.load_image(new_image_fp, use_mmaped_io=False)
slices.append(img.slice)
fat_out = ktool.macho_combine(slices)
fat_out.seek(0) # ktool should be doing this, but just to be safe.
return fat_out
if __name__ == "__main__":
parser = ArgumentParser(description="remap")
parser.add_argument('--out', dest='out', default=None)
parser.add_argument('filename', nargs='?', default='')
args = parser.parse_args()
if args.filename == '':
parser.print_help()
exit(1)
try:
with open(args.filename, 'rb') as fp:
out = handle_file(fp)
with open(args.out if args.out else args.filename, 'wb') as fp:
fp.write(out.read())
except FileNotFoundError:
print('File does not exist')
exit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment