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
92 stars 19 forks source link

-m macholib.MachOGraph doesn't output much #34

Open altendky opened 4 years ago

altendky commented 4 years ago

python -m macholib.MachOGraph doesn't seem to provide much output despite otool -L providing several system and relative path libraries. Is it missing something?

My (next layer out) actual goal is to take a given executable or bundle and list out absolute paths to all 'libraries' it would link to if run (recursively) . This is part of https://github.com/altendky/pyqt5-tools/pull/43 where I am trying to collect many Qt applications and package them into a wheel.

Thanks for the library and any help with this scenario.

1197:~ administrator$ venv/bin/python -m macholib.MachOGraph qt/5.14.2/clang_64/bin/Designer.app/Contents/MacOS/Designer 
digraph G {
        rankdir="LR";
        concentrate="true";
}
1197:~ administrator$ otool -L qt/5.14.2/clang_64/bin/Designer.app/Contents/MacOS/Designer 
qt/5.14.2/clang_64/bin/Designer.app/Contents/MacOS/Designer:
        @rpath/QtDesignerComponents.framework/Versions/5/QtDesignerComponents (compatibility version 5.14.0, current version 5.14.2)
        @rpath/QtDesigner.framework/Versions/5/QtDesigner (compatibility version 5.14.0, current version 5.14.2)
        @rpath/QtPrintSupport.framework/Versions/5/QtPrintSupport (compatibility version 5.14.0, current version 5.14.2)
        @rpath/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.14.0, current version 5.14.2)
        @rpath/QtGui.framework/Versions/5/QtGui (compatibility version 5.14.0, current version 5.14.2)
        /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1671.10.106)
        /System/Library/Frameworks/Metal.framework/Versions/A/Metal (compatibility version 1.0.0, current version 1.0.0)
        @rpath/QtXml.framework/Versions/5/QtXml (compatibility version 5.14.0, current version 5.14.2)
        @rpath/QtNetwork.framework/Versions/5/QtNetwork (compatibility version 5.14.0, current version 5.14.2)
        @rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.14.0, current version 5.14.2)
        /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration (compatibility version 1.0.0, current version 1.0.0)
        /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
        /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
        /System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
1197:~ administrator$ venv/bin/pip list
Package        Version            
-------------- -------------------
altgraph       0.17               
aqtinstall     0.8                
attrs          19.3.0             
certifi        2020.4.5.1         
chardet        3.0.4              
click          7.1.1              
idna           2.9                
macholib       1.14               
more-itertools 8.2.0              
packaging      20.3               
pip            19.2.3             
pluggy         0.13.1             
py             1.8.1              
py7zr          0.6                
pycryptodome   3.9.7              
pyparsing      2.4.7              
PyQt5          5.14.2             
PyQt5-sip      12.7.2             
pyqt5-tools    5.14.2.1.7b3.post31
pytest         5.4.1              
python-dotenv  0.13.0             
requests       2.23.0             
setuptools     41.2.0             
six            1.14.0             
texttable      1.6.2              
urllib3        1.25.9             
wcwidth        0.1.9              
wheel          0.34.2             
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
dreness commented 4 years ago

It took a bit of tinkering, but I arrived at a process for generating Graphviz documents. Try this:

from macholib import MachO, MachOGraph
from altgraph import Dot

m = MachO.MachO("/bin/ls")
g = MachOGraph.MachOGraph()
g.run_file(m.filename)
# remove top-level graph node
g.removeNode(g.graph.node_list()[0])
dot = Dot.Dot(g.graph)
dot.save_dot("ls.dot")
dot.save_img("ls", file_type="pdf")

... and here's the resulting PDF: ls.pdf