-f print the fat headers -a print the archive header -h print the mach header -l print the load commands -L print shared libraries used -D print shared library id name -t print the text section (disassemble with -v) -p <routine name> start dissassemble from routine name -s <segname> <sectname> print contents of section -d print the data section -o print the Objective-C segment -r print the relocation entries -S print the table of contents of a library -T print the table of contents of a dynamic shared library -M print the module table of a dynamic shared library -R print the reference table of a dynamic shared library -I print the indirect symbol table -H print the two-level hints table -G print the data in code table -v print verbosely (symbolically) when possible -V print disassembled operands symbolically -c print argument strings of a core file -X print no leading addresses or headers -m don't use archive(member) syntax -B force Thumb disassembly (ARM objects only) -q use llvm's disassembler (the default) -Q use otool(1)'s disassembler -mcpu=arg use `arg' as the cpu for disassembly -j print opcode bytes -P print the info plist section as strings -C print linker optimization hints --version print the version of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool
$ otool -L YourMacho libGCloud.dylib: @rpath/libGCloud.dylib (compatibility version 1.0.0, current version 1.0.0) /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 58286.51.6) /System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork (compatibility version 1.0.0, current version 897.15.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11) /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1452.23.0) /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4) /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1452.23.0)
-B - Alias for --format=bsd -P - Alias for --format=posix --add-dyldinfo - Add symbols from the dyldinfo not already in the symbol table, Mach-O only --arch=<string> - architecture(s) from a Mach-O file to dump --debug-syms - Show all symbols, even debugger only --defined-only - Show only defined symbols --demangle - Demangle C++ symbol names --dyldinfo-only - Show only symbols from the dyldinfo, Mach-O only --dynamic - Display the dynamic symbols instead of normal symbols. --extern-only - Show only external symbols --format=<value> - Specify output format =bsd - BSD format =sysv - System V format =posix - POSIX.2 format =darwin - Darwin -m format --just-symbol-name - Print just the symbol's name -m - Alias for --format=darwin --no-demangle - Don't demangle symbol names --no-dyldinfo - Don't add any symbols from the dyldinfo, Mach-O only --no-llvm-bc - Disable LLVM bitcode reader --no-sort - Show symbols in order encountered --no-weak - Show only non-weak symbols --numeric-sort - Sort symbols by address --portability - Alias for --format=posix --print-armap - Print the archive map --print-file-name - Precede each symbol with the object file it came from --print-size - Show symbol size as well as address --radix=<value> - Radix (o/d/x) for printing symbol Values =d - decimal =o - octal =x - hexadecimal --reverse-sort - Sort in reverse order --size-sort - Sort symbols by size --undefined-only - Show only undefined symbols -x - Print symbol entry in hex, Mach-O only
常用选项
说明
-B (default)
使用BSD输出格式 ,同 -format=bsd
-P
使用POSIX输出格式, 通过 -format=posix
-debug-syms,-a
显示所有符号
–dynamic,-D
显示动态链接符号
-defined-only
仅显示在本文件中定义的符号
–extern-only,g
仅显示外部符号
–undefined-only,-u
仅显示引用但未在此文件中定义的符号
–no-weak,-W
不显示弱引用符号
–format=format, -f format
选择一个输出格式 sysv,posix,bsd, 默认值是bsd
–no-sort,-p
符号按顺序显示
–numeric-sort,-n,-v
符号按地址进行排序
–size-sort
符号按地址进行排序
–print-file-name,-A,-o
在每个符号前面加上它来自的文件
llvm-nm 与 gnu-nm 命令并不完全相同,llvm-nm 不支持 GNU nm所做的全部参数。
MachO Install Name
MachO 格式的动态库生成时,会在其 LC_ID_DYLIB 写入动态库的信息,其中会有一个 name 字段,包括路径和动态库的名称。这个 name 即为动态库的 Install Name,。路径可以是绝对路径,也可以是相对路径。当可执行文件或其他库文件链接此动态库时,链接器会把这个 Install Name 写入到该文件中的 LC_LOAD_DYLIB 中。dyld 会根据这个 Install Name,去查找并加载该动态库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 某库文件依赖动态库 libc++.1.dylib,其 LC_LOAD_DYLIB 包含 libc++ 的 Install name (id name) Load command 15 cmd LC_LOAD_DYLIB cmdsize 48 name /usr/lib/libc++.1.dylib (offset 24) time stamp 2 Thu Jan 1 08:00:02 1970 current version 400.9.0 compatibility version 1.0.0 # libc++.1.dylib 的 LC_ID_DYLIB 信息中包含 Install name (id name) Load command 3 cmd LC_ID_DYLIB cmdsize 48 name /usr/lib/libc++.1.dylib (offset 24) time stamp 1 Thu Jan 1 08:00:01 1970 current version 902.1.0 compatibility version 1.0.0
相对路径
在开发过程中,动态库可能被放置在不同的路径下(如:系统 Library 目录、嵌入 App、嵌入 Bundle 等),因此 Install Name 设置为相对路径,更便于开发者使用。