Skip to content

Add optional metal binary archive/lib parsing to disassembler #19

@dougallj

Description

@dougallj

I have a terrible mockup of this that I've been using on compute shaders. You shouldn't trust this, but being able to find the code and print names is worthwhile.

(Pre-compilation is now supported, so that's nice: https://developer.apple.com/videos/play/wwdc2022/10102/)

Terrible mockup:

import re
import subprocess

with open(sys.argv[1], 'rb') as inf:
    data = inf.read()

for i in list(re.finditer(b'\xcf\xfa\xed\xfe', data)):
    current = data[i.start():]
    with open('tmp.bin', 'wb') as outf:
        outf.write(current)

    code = None
    for i in re.finditer('Section\n  sectname (.*)\n   segname (.*)\n      addr (.*)\n      size (.*)\n    offset (.*)',
                         subprocess.check_output('otool -l tmp.bin', shell=True).decode('ascii')):
        sectname, segname, addr, size, offset = i.groups()
        addr = int(addr, 16)
        size = int(size, 16)
        offset = int(offset)
        if segname == '__GPU_LD_MD':
            print('-' * 80)
            with open('tmp1.bin', 'wb') as f:
                f.write(current[offset:offset+size])
            print(subprocess.check_output('strings - tmp1.bin', shell=True).decode('ascii').strip()) # prints name (and other junk)
        if sectname == '__text':
            code = current[offset:offset+size]

    if code:
        if code.startswith(b'\xcf\xfa\xed\xfe'):
            continue
        if code.startswith(b'MTLPSBIN'):
            continue
        for line in subprocess.check_output(['nm', 'tmp.bin']).decode('ascii').strip().split('\n'):
            a, b, c = line.split()
            if 'ltmp' not in c:
                print()
                print(c + ':')
                disassemble(code[int(a,16):]) # relies on STOP_ON_STOP

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions