macOS Command - otool

otool


macOS 使用交流 QQ 群:658095824,V : ez-code


一、otool 简介

Mac OS X下二进制可执行文件的动态链接库是dylib文件。

所谓dylib,就是bsd风格的动态库。基本可以认为等价于windows的dll和linux的so。mac基于bsd,所以也使用的是dylib。

Linux下用ldd查看,苹果系统用otool。


查看 otool 地址

1、电脑已安装 Xcode

输入:

$ otool -help

返回:

error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool: unknown char `p' in flag -help

Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool [-arch arch_type] [-fahlLDtdorSTMRIHGvVcXmqQjCP] [-mcpu=arg] [--version] <object file> ...
	-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 (obsolete)
	-T print the table of contents of a dynamic shared library (obsolete)
	-M print the module table of a dynamic shared library (obsolete)
	-R print the reference table of a dynamic shared library (obsolete)
	-I print the indirect symbol table
	-H print the two-level hints table (obsolete)
	-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 的地址:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool

进入地址发现,otool 文件是个软连接。


不知道为什么会输出 unknown char 'p' in flag -help 这个错误, otool -potool -helpotool --help,都会报错。


查看 otool 指向的软连接地址:

cd进入otool所在的目录,使用 ls -l 命令;

$ cd /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin 

$ ls -l

返回:


total 209368
-rwxr-xr-x  1 root  wheel     33920  3 20 11:34 ar
-rwxr-xr-x  1 root  wheel     28000  3 20 11:34 as
...

-rwxr-xr-x  1 root  wheel     32672  3 20 11:34 llvm-otool
...
lrwxr-xr-x  1 root  wheel        10  3 22 15:43 otool -> llvm-otool
...
-rwxr-xr-x  1 root  wheel    640352  3 20 11:34 otool-classic

可以发现 otool 指向 llvm-otoolllvm-otool 和 otool 在同一个文件夹下。

可以发现,这个文件夹下面还有很多有用的文件,如 lipo。


2、未安装 Xcode 的电脑

换一台没有安装xcode 的电脑,输入otool 命令会提示安装:

这里写图片描述

一般安装要10分钟以上,网络不好,耗时更长;
这里写图片描述


$ otool /Applications/NeteaseMusic.app 
error: /Library/Developer/CommandLineTools/usr/bin/otool: one of -fahlLtdoOrTMRIHCGScisP or --version must be specified
Usage: /Library/Developer/CommandLineTools/usr/bin/otool [-arch arch_type] [-fahlLDtdorSTMRIHGvVcXmqQjCP] [-mcpu=arg] [--version] <object file> ...
	-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

可以发现 otool 地址在:/Library/Developer/CommandLineTools/usr/bin/otool
实际上已安装 xcode 的Mac 上也有这个文件;
不清楚调用优先级是如何的。


二、用法


1、查看动态链接库 otool -L

输入命令:

$ otool -L  /Applications/Pomotodo.app/Contents/MacOS/Pomotodo 
/Applications/Pomotodo.app/Contents/MacOS/Pomotodo:

返回:

	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.82.104)
	/System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 157.0.0)
	/System/Library/Frameworks/ServiceManagement.framework/Versions/A/ServiceManagement (compatibility version 1.0.0, current version 972.50.27)
	@rpath/Sparkle.framework/Versions/A/Sparkle (compatibility version 1.6.0, current version 1.14.0)
	/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration (compatibility version 1.0.0, current version 888.51.1)
	/System/Library/Frameworks/WebKit.framework/Versions/A/WebKit (compatibility version 1.0.0, current version 603.1.30)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.63.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.50.2)
	/System/Library/Frameworks/AVFoundation.framework/Versions/A/AVFoundation (compatibility version 1.0.0, current version 2.0.0)
	/System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork (compatibility version 1.0.0, current version 811.4.18)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1349.64.0)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1070.22.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 775.19.0)

2、otool -ov

得到的内容太多,不在此一一列举

$ otool -ov /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text 
/Applications/Sublime Text.app/Contents/MacOS/Sublime Text:
Contents of (__DATA,__objc_classlist) section
0000000100742950 0x1007467a8 _OBJC_CLASS_$_WorkQueueCallback
           isa 0x100746780 _OBJC_METACLASS_$_WorkQueueCallback
    superclass 0x0 _OBJC_CLASS_$_NSObject
         cache 0x0
        vtable 0x0
          data 0x100742a40 (struct class_ro_t *)
                    flags 0x0
            instanceStart 8
             instanceSize 16
                 reserved 0x0
               ivarLayout 0x0
                     name 0x10064cba3 WorkQueueCallback
              baseMethods 0x100742a88 (struct method_list_t *)
		   entsize 24
		     count 1
		      name 0x10064a14e processItems:
		     types 0x10064cd14 v24@0:8@16
		       imp -[WorkQueueCallback processItems:]
            baseProtocols 0x0
                    ivars 0x100742aa8
                    entsize 32
                      count 1
			   offset 0x1007466d8 8
			     name 0x10064a147 runner
			     type 0x10064cce9 ^{ns_work_queue_runner=^^?^{work_queue}@@}
			alignment 3
			     size 8
           weakIvarLayout 0x0
           baseProperties 0x0
Meta Class
           isa 0x0
    superclass 0x0 _OBJC_METACLASS_$_NSObject
         cache 0x0
        vtable 0x0
          data 0x1007429f8 (struct class_ro_t *)

3、汇编码 otool -tV

则整个ARM的汇编码就都显示出来了,数据量如瀑布,大家可以试试。。。。
能看到ARM的汇编码,那接下来怎么用就看大家的了,想象空间无限啊!


4、查看 Mach-O头结构等

$ otool -h /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text 

Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedfacf 16777223          3  0x80           2    27       4336 0x00218085


一个 Mach-O 的文件头结构为:
这里写图片描述

上述结果的意思是:

magic(魔数)- 0xfeedfacf
cputype(CPU类型)- 16777223
cpusubtype(CPU子类型)- 3
caps - 0x80
filetype(文件类型) - 2
ncmds - 27
sizeofcmds(加载命令大小) - 4336
flags(动态连接器dyld标志) - 0x00218085


各字段的含义,可参看/usr/include/mach-o/loader.h


5、查看ipa包是否加壳

otool -l 可执行文件 | grep crypt1

显示:

     cryptoff 16384
    cryptsize 6651904
      cryptid 0
     cryptoff 16384
    cryptsize 6553600
      cryptid 0123456

其中cryptid代表是否加壳,1代表加壳,0代表已脱壳。我们发现打印了两遍,其实代表着该可执行文件支持两种架构armv7和arm64.


参考

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: macOS是苹果公司开发的一款操作系统,而Arm-Linux是一款基于Arm架构的Linux操作系统。MacOS Arm-Linux指的是在Mac电脑上运行基于Arm架构的Linux系统。 苹果公司宣布将在不久的将来改变Mac电脑的芯片架构,从之前使用的Intel处理器转变为自家研发的Arm架构处理器。这意味着未来的Mac电脑将可以运行使用Arm架构编译的软件。 在这种背景下,Mac电脑将具备运行Arm-Linux操作系统的能力。Arm-Linux是Linux操作系统的一个分支,被广泛应用于嵌入式设备和移动设备等领域。将Arm-Linux移植到Mac电脑上,用户可以通过在Mac上运行Linux,获得更多的软件和应用选择。 MacOS Arm-Linux的引入将为用户提供更多的自由和灵活性。Arm-Linux操作系统具有良好的兼容性和可定制性,用户可以根据个人需求进行自定义设置,进行系统优化和调试等操作。同时,对于开发者来说,Arm-Linux也提供了更多的开发工具和环境,使他们能够更方便地进行软件开发和调试。 总之,MacOS Arm-Linux的结合将为用户带来更广阔的选择和更多的可能性,使Mac电脑成为一款更加强大和多功能的设备。用户可以享受到更多的软件和应用,同时也能更灵活地进行系统设置和软件开发等操作。这对于个人和开发者来说都是一种积极的变化,有助于提升用户体验和系统的可定制性。 ### 回答2: macOS是苹果公司自主开发的操作系统,主要运行于苹果的个人电脑和服务器上。而arm-linux是一种基于ARM架构的Linux操作系统。 macOS使用的是x86架构,而arm-linux则是基于ARM架构的操作系统。这两种架构在硬件和指令集上存在差异,因此软件在不同架构上运行时需要进行编译和适配。 最近,苹果公司宣布将在将来的Mac电脑上使用自家研发的ARM架构芯片。这一决定的背后有很多原因,其中包括更好的性能、能效和统一化等。换句话说,未来的Mac电脑将会运行基于ARM架构的芯片,并适配macOS操作系统。 对于软件开发者和用户来说,这个转变意味着一些软件可能需要重新编写或适配以在新的硬件和操作系统上运行。macOS上原本运行良好的x86架构软件需要重新编译为ARM架构版本,以确保兼容性和性能。 然而,这个转变也给开发者带来了新的机会。ARM架构在移动设备上已经得到广泛应用,移植到Mac电脑上有望带来更好的性能和能效。此外,将来的Mac电脑和iOS设备将共享更多的软件和生态系统,有助于提升跨设备的一体化体验。 总之,macOS将转向ARM架构的决定对于苹果公司和用户来说都是一个重要的里程碑。它将带来更好的性能、能效和统一化,并为开发者带来新的机遇。也许,未来的Mac电脑将更加强大和多样化。 ### 回答3: macOS是苹果公司的操作系统,它最初是基于PowerPC架构的,后来转向了x86架构。而ARM架构是一种使用更低功耗的处理器架构,被广泛应用于手机、平板电脑和嵌入式系统等领域。 近年来,ARM架构的处理器性能不断提升,开始逐渐应用于个人电脑领域。所以有人开始猜测苹果是否会将ARM架构引入其Mac电脑产品线。 然而,直到2020年底,苹果才正式发布了基于ARM架构的Mac电脑。这代表着苹果公司将逐渐适应ARM架构,并且未来的Mac电脑将会更加高效、节能。 对于开发者和用户来说,macOS转向ARM架构意味着一些程序和应用可能需要重新编译和优化,以适应新架构的要求。同时,用户也将享受到更长续航时间、更流畅的系统操作和更低的功耗。 对于整个行业来说,macOS转向ARM架构也体现了ARM架构的不断演进和扩大应用领域。它有望推动ARM架构在电脑领域的发展,进一步加强ARM与x86架构的竞争。特别是在移动办公、云计算和物联网等领域,ARM架构有望发挥更大的优势。 总之,macOS转向ARM架构是苹果公司顺应时代发展趋势的举措。它将为用户带来更好的系统体验,也为整个行业的发展带来新的机遇和挑战。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI工程化

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值