Fat Mach-O可执行多用途? [英] Fat Mach-O Executable Multi-purpose?

查看:143
本文介绍了Fat Mach-O可执行多用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Mac上使用Mach-O可执行文件,只是遇到一个问题,一个Fat Mach-O可执行文件可以有多种用途吗?例如

I am currently working with Mach-O Executables on my Mac and A question just came across me, Can a single Fat Mach-O Executable file have multiple purposes? Eg.

我可以使用一个带有一个胖头指定2个可执行文件的Mach-O可执行文件吗:

Could I Have a single Mach-O Executable File with a Fat Header specifying 2 Executables:

可执行文件1:此可执行文件可以是动态库,允许其代码加载到外部应用程序中.

Executable 1 : This executable could be a Dynamic Library allowing Its code to be loaded in external applications.

可执行文件2:此可执行文件可以是可执行文件,允许通过终端或作为应用程序独立启动.

Executable 2 : This executable could be an Executable allowing It to be independently launched through Terminal or as an Application.

我只想知道,在一个Mach-O二进制文件中可以有2个具有完全不同功能的可执行文件吗?

I just want to know, could this be possible to have 2 Executables with completely different functions inside a single Mach-O Binary File?

推荐答案

是可以的,但几乎没有用.在我解释为什么之前,这里是创建一个的方法:

Yes it is possible, but hardly useful. Before I get to why, here's how to create one:

获取此C文件:

#ifdef __LP64__
int main(void)
#else
int derp(void)
#endif
{
    return 123;
}

将其编译为64位可执行文件和32位共享库:

Compile it as a 64-bit executable and a 32-bit shared library:

gcc -o t t.c -Wall
gcc -m32 -o t.dylib -Wall t.c -shared

并将它们粉碎在一起:

lipo -create -output t.fat t t.dylib

现在,为什么那应该没用?
因为每个体系结构仅限于一个二进制文件,并且对使用哪个片几乎没有控制.
从理论上讲,您可以在同一个胖二进制文件中包含所有这些体系结构的切片:

Now, why is that supposed to be not useful?
Because you're limited to one binary per architecture, and you have little to no control over which slice is used.
In theory, you can have slices for all these architectures in the same fat binary:

  • i386
  • x86_64
  • x86_64h
  • armv6
  • armv6m
  • armv7
  • armv7s
  • armv7k
  • armv7m
  • arm64
  • i386
  • x86_64
  • x86_64h
  • armv6
  • armv6m
  • armv7
  • armv7s
  • armv7k
  • armv7m
  • arm64

因此,您可以将一个可执行文件,一个dylib,一个链接器和一个内核扩展粉碎成一个胖二进制文件,但是要获得任何有用的东西将很困难.
最大的问题是操作系统选择要加载的片.对于可执行文件,这将始终是您正在运行的处理器的最接近的匹配项.对于dylib,dylinkers和kexts,首先要确定它们要加载到的进程是32位还是64位,但是一旦做出区分,您也将获得与CPU的最接近的切片.功能.

So you could smash an executable, a dylib, a linker and a kernel extension into one fat binary, but you'd have a hard time getting anything useful out of it.
The biggest problem is that the OS chooses which slice to load. For executables, that will always be the closest match for the processor you're running on. For dylibs, dylinkers and kexts, it will first be determined whether the process they're gonna be loaded into is 32- or 64-bit, but once that distinction has been made, there too you will get the slice most closely matching your CPU's capabilities.

我想回到Mac OS X 10.5上,您可能已经将64位二进制文​​件与32位kext捆绑在一起,可以尝试加载.但是,除此之外,我无法想到一个用例.

I imagine back on Mac OS X 10.5 you could've had a 64-bit binary bundled with a 32-bit kext that it could try and load. However, outside of that I cannot think of a use case for this.

这篇关于Fat Mach-O可执行多用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆