ronaldoussoren / macholib

macholib can be used to analyze and edit Mach-O headers, the executable format used by Mac OS X. It's typically used as a dependency analysis tool, and also to rewrite dylib references in Mach-O headers to be @executable_path relative. Though this tool targets a platform specific file format, it is pure python code that is platform and endian independent.
MIT License
91 stars 19 forks source link

ValueError: Unknown Mach-O header: 0x213c6172 #49

Open HexDecimal opened 2 months ago

HexDecimal commented 2 months ago

Downstream project Delocate is unable to read this file (libnpymath.a):

...
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/delocate/delocating.py", line 619, in _get_macos_min_version
    for header in MachO(dylib_path).headers:
                  ^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/macholib/MachO.py", line 121, in __init__
    self.load(fp)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/macholib/MachO.py", line 131, in load
    self.load_fat(fh)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/macholib/MachO.py", line 148, in load_fat
    self.load_header(fh, arch.offset, arch.size)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/macholib/MachO.py", line 170, in load_header
    raise ValueError("Unknown Mach-O header: 0x%08x in %r" % (header, fh))
ValueError: Unknown Mach-O header: 0x213c6172 in <_io.BufferedReader name='.../numpy/_core/lib/libnpymath.a'>

Full traceback, relevant files, and more discussion are here: https://github.com/matthew-brett/delocate/issues/227

Apparently this binary is compiled from Fortran code.

HexDecimal commented 2 months ago

More info: https://github.com/matthew-brett/delocate/issues/229

If it's invalid for macholib to read static libraries then could this give a better error?

HexDecimal commented 2 months ago

Caused by a static library with multiple architectures.