如何确定 Mac OS X 上静态库 (.a) 的目标体系结构?

发布于 2024-07-26 08:40:44 字数 253 浏览 6 评论 0原文

我有兴趣验证给定的 iPhone 静态库是否是为 ARM 或 Intel 构建的。

好奇心胜过一切。 是否有某种 Mac OS X 或 BSD 特定工具可以执行此操作? 这篇帖子给出了一个例子Linux。

I'm interested in verifying if a given iPhone static library has been built for ARM or Intel.

It's more curiosity than anything. Is there some kind of Mac OS X or BSD specific tool to do this? This post gives an example in Linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

表情可笑 2024-08-02 08:40:44

另一种选择是lipo; 它的输出比 otool 的输出更简短且更具可读性。

一个例子:

% lipo -info /usr/lib/libiodbc.a 
Architectures in the fat file: /usr/lib/libiodbc.a are: x86_64 i386 ppc
% lipo -info libnonfatarchive.a
input file libnonfatarchive.a is not a fat file
Non-fat file: libnonfatarchive.a is architecture: i386
%

Another option is lipo; its output is brief and more readable than otool's.

An example:

% lipo -info /usr/lib/libiodbc.a 
Architectures in the fat file: /usr/lib/libiodbc.a are: x86_64 i386 ppc
% lipo -info libnonfatarchive.a
input file libnonfatarchive.a is not a fat file
Non-fat file: libnonfatarchive.a is architecture: i386
%
如梦 2024-08-02 08:40:44

file 可能会告诉你。 otool 当然应该可以。 但我会先尝试 file
例如

logan:/Users/logan% file d2
d2: Mach-O executable ppc

带有存档的示例:

logan:/Users/logan% file /usr/lib/libMallocDebug.a
/usr/lib/libMallocDebug.a: Mach-O universal binary with 2 architectures
/usr/lib/libMallocDebug.a (for architecture i386):      current ar archive random library
/usr/lib/libMallocDebug.a (for architecture ppc):       current ar archive

file will probably tell you. otool certainly should be able to. But I'd try file first,
e.g.

logan:/Users/logan% file d2
d2: Mach-O executable ppc

Example with archive:

logan:/Users/logan% file /usr/lib/libMallocDebug.a
/usr/lib/libMallocDebug.a: Mach-O universal binary with 2 architectures
/usr/lib/libMallocDebug.a (for architecture i386):      current ar archive random library
/usr/lib/libMallocDebug.a (for architecture ppc):       current ar archive
九八野马 2024-08-02 08:40:44

正如前面提到的,file 并不总是有效。 otool -hv -arch all 可能是保证工作的最接近的东西 - 它提供了库中每个目标文件的体系结构信息。

例子:

% otool -hv /sw/lib/libfftw3.a
Archive : /sw/lib/libfftw3.a
/sw/lib/libfftw3.a(align.o):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64  X86_64        ALL  0x00      OBJECT     3        336 SUBSECTIONS_VIA_SYMBOLS
/sw/lib/libfftw3.a(alloc.o):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64  X86_64        ALL  0x00      OBJECT     3        416 SUBSECTIONS_VIA_SYMBOLS
...

As mentioned earlier, file does not always work. otool -hv -arch all is probably the closest thing that is guaranteed to work - it gives architecture information for every single object file in the library.

Example:

% otool -hv /sw/lib/libfftw3.a
Archive : /sw/lib/libfftw3.a
/sw/lib/libfftw3.a(align.o):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64  X86_64        ALL  0x00      OBJECT     3        336 SUBSECTIONS_VIA_SYMBOLS
/sw/lib/libfftw3.a(alloc.o):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64  X86_64        ALL  0x00      OBJECT     3        416 SUBSECTIONS_VIA_SYMBOLS
...
偏爱你一生 2024-08-02 08:40:44

如果有人来这里寻找有关如何判断库(或其中的目标文件)是否适用于 Mac Catalyst 的答案,请使用 otool -l 转储加载命令。 查找任何对象的 LC_BUILD_VERSION 部分。 Mac Catalyst 由平台 6 标识,而不是平台 1

If anyone comes here looking for answers about how to tell whether a library (or the object files in it) are intended for Mac Catalyst, use otool -l to dump the load commands. Find the LC_BUILD_VERSION section for any object. Mac Catalyst is identified by platform 6 rather than platform 1.

为人所爱 2024-08-02 08:40:44

此 bash 脚本将帮助您以编程方式将体系结构列表放入变量中。

list_archs.sh:

#! /bin/bash
lipo -info $1 | sed -En -e 's/^(Non-|Architectures in the )fat file: .+( is architecture| are): (.*)$/\3/p'

使用示例:

./list_archs.sh /usr/lib/libc.dylib
x86_64 i386

This bash script will help you programmatically get a list of architectures into a variable.

list_archs.sh:

#! /bin/bash
lipo -info $1 | sed -En -e 's/^(Non-|Architectures in the )fat file: .+( is architecture| are): (.*)$/\3/p'

Usage example:

./list_archs.sh /usr/lib/libc.dylib
x86_64 i386
世态炎凉 2024-08-02 08:40:44

作为替代方案,我发现 objdump 可以很好地工作。 例如,在我的环境中,我使用 vxWorks 构建库档案,并需要将它们链接到其他项目中。 为了测试存档是否是正确的体系结构,我可以执行如下操作(bash 语法):

if [ "$(objdumpsparc -a ${ARCHIVE_FILE} 2>&1 | ggrep -cvP 'elf32-sparc-vxworks')" -ne "0" ]; then
  echo "Cannot build with ${ARCHIVE_FILE}, it contains one or more non-sparc components"
fi;

这个示例并不完全正确,因为确实显示了一些行,但没有说 elf32-sparc-vxworks,但这很容易足以适应这一点。

这样做的一个好处是,大多数 *nix 操作系统上都安装了 objdump 或类似名称的变体,而其他响应中建议的工具却没有安装。

编辑我刚刚想到OP在OSX上询问。 我很抱歉。

As an alternative, I've found objdump can work well. As an example, in my environment I build library archives with vxWorks and need to link those into other projects. To test whether the archive is the correct architecture, I could do something like the following (bash syntax):

if [ "$(objdumpsparc -a ${ARCHIVE_FILE} 2>&1 | ggrep -cvP 'elf32-sparc-vxworks')" -ne "0" ]; then
  echo "Cannot build with ${ARCHIVE_FILE}, it contains one or more non-sparc components"
fi;

This example isn't precisely correct, because some lines DO show up that don't say elf32-sparc-vxworks, but it's easy enough to adapt this.

One nice benefit of this is that objdump, or a similarly named variant, is installed on most *nix operating systems, whereas tools suggested in other responses aren't.

edit It just occurred to me the OP was asking on OSX. My apologies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文