64 位代码如何在 OS-X 10.5 上运行?
我最初认为 64 位指令无法在 OS-X 10.5 上运行。
我编写了一个小测试程序并使用 GCC -m64 编译它。 我使用 long long
表示 64 位整数。
使用的汇编指令看起来像是 64 位的。例如。 imultq
和 movq 8(%rbp),%rax
。
我好像工作了
我仅使用 printf
来使用 %lld
显示 64 位值。
- 这是预期的行为吗?
- 是否有任何
陷阱
会导致此失败? - 我可以在一个问题中提出多个问题吗?
- 这适用于其他操作系统吗?
I initially thought that 64 bit instructions would not work on OS-X 10.5.
I wrote a little test program and compiled it with GCC -m64
.
I used long long
for my 64 bit integers.
The assembly instructions used look like they are 64 bit. eg. imultq
and movq 8(%rbp),%rax
.
I seems to work.
I am only using printf
to display the 64 bit values using %lld
.
- Is this the expected behaviour?
- Are there any
gotcha's
that would cause this to fail? - Am I allowed to ask multiple questions in a question?
- Does this work on other OS's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了完全清楚地说明这一点,以下是 OS X 上 32 位和 64 位可执行文件的情况:
32 位和 64 位用户空间可执行文件都可以在操作系统中的 32 位和 64 位内核上运行X 10.6,无仿真。在 10.4 和 10.5 上,32 位和 64 位可执行文件都可以在 32 位内核上运行。 (Windows 上并非如此)
用户空间系统库和框架是在 10.5 和 10.6 上构建的 32/64 位 fat。无论您是针对 32 位、64 位还是两者进行构建,您都可以正常链接它们。一些库(基本上是 POSIX 层)也在 10.4 上构建了 32/64 位 fat,但其中许多不是。
在 10.6 上,构建工具默认生成 64 位可执行文件。在 10.5 及更早版本上,默认值为 32 位。
在 10.6 上,胖构建的可执行文件将默认在 64 位端运行。在 10.5 及更早版本中,默认执行 32 位端。
您始终可以使用
arch
命令手动指定要使用 fat 可执行文件的哪个片段。例如。arch -arch i386 someCommandToRunThatIWantToRunIn32BitMode
。对于应用程序包,您可以从命令行启动它们,或者如果您“获取应用程序的信息”,则有一个首选项。OS X 和 Linux 对 64 位可执行文件使用 LP64 模型。指针和
long
都是64位宽,int
仍然是32位,long long
仍然是64位。 (Windows 使用 LLP64 模型 -long
在 64 位 Windows 中为 32 位宽)。Just to make this completely clear, here is the situation for 32- and 64-bit executables on OS X:
Both 32- and 64-bit user space executables can be run on both 32- and 64-bit kernels in OS X 10.6, without emulation. On 10.4 and 10.5, both 32- and 64-bit executables can run on the 32-bit kernel. (This is not true on Windows)
The user space system libraries and frameworks are built 32/64-bit fat on 10.5 and 10.6. You can link against them normally, whether you're building for 32-bit, 64-bit, or both. A few libraries (basically the POSIX layer) are also built 32/64-bit fat on 10.4, but many of them are not.
On 10.6, the build tools produce 64-bit executables by default. On 10.5 and earlier, the default is 32-bit.
On 10.6, executables that are built fat will run the 64-bit side by default. On 10.5 and earlier, the 32-bit side is executed by default.
You can always manually specify which slice of a fat executable to use by using the
arch
command. eg.arch -arch i386 someCommandToRunThatIWantToRunIn32BitMode
. For application bundles, you can either launch them from the command line, or there is a preference if you "get info" on the application.OS X and Linux use the LP64 model for 64-bit executables. Pointers and
long
are 64 bits wide,int
is still 32 bits, andlong long
is still 64 bits. (Windows uses the LLP64 model instead --long
is 32 bits wide in 64 bit Windows).Mac OS X 10.5 很好地支持 64 位用户态应用程序。事实上,Xcode 在 10.5 中以 64 位运行在兼容的架构上。
只是内置应用程序(Finder、Safari、框架、守护进程等)在 10.6 中也有 64 位版本。
Mac OS X 10.5 supports 64-bit user-land applications pretty well. In fact, Xcode runs in 64-bit in 10.5 on a compatible architecture.
It's only the built-in applications (Finder, Safari, frameworks, daemons etc.) also have the 64-bit version in 10.6.
Meta:我不喜欢看到答案被删除。我想这已经在某处讨论过了。
不管怎样,KennyTM 和另一种鞋底让我开始了,虽然一个答案被删除了,但我很欣赏你的努力。
看起来这是 Mac 上的预期行为,甚至似乎也可以在 32 位 Linux 上运行(尽管我还没有进行广泛测试)
是的。对于 32 (-m32) 和 64 (-m64) 位模式,GCC 的行为有所不同(至少在我有限的观察中)。在 32 位中,我能够使用数组访问变量参数。在 64 位模式下,这不起作用。
我了解到您必须使用 stdarg.h 定义的 va_list 访问变量参数,因为它在两种模式下都有效。
现在我有一个命令行程序,可以在 Mac OS-X 上以 32 位和 64 位模式运行并通过所有测试用例。
该程序实现了一个链表垃圾收集器,从全局列表以及机器寄存器和堆栈中清除 16 字节对齐的 malloc 分配的对象 - 实际上,64 位模式下还有额外的寄存器,所以我还有一些工作要做做。
对象是 32 位或 64 位字的集合,它们链接在一起形成类似 LISP/Scheme 的数据结构。
总之,它是一个复杂的程序,对指针做了很多混乱,并且在 32 位和 64 位模式下工作相同。
提出多个问题并不能得到您可能想要的所有答案。
正如我所写,它似乎可以在 Linux 上运行。
再次感谢您在这件事上帮助我。
Meta: I don't like to see answers deleted. I guess this has been discussed somewhere.
Anyway, KennyTM and the other kind sole got me started and although one answer was deleted, I appreciated your efforts.
It looks like this is expected behaviour on the Mac, and it even seems to work on a 32-bit Linux as well (although I have not tested extensively)
Yep. GCC behaves different (at least in my limited observation) for 32 (-m32) and 64 (-m64) bit modes. In 32 bit, I was able to access variable arguments using an array. In 64 bit mode this just does not work.
I have learnt that you MUST access variable parameters using va_list as defined by stdarg.h because it works in both modes.
Now I have a command-line program that runs and passes all of my test cases in 32 bit and 64 bit modes on Mac OS-X.
The program implements a linked list garbage collector sweeping 16-byte aligned malloc-allocated objects from a global list as well as machine registers and the stack - actually, there are extra registers in 64 bit mode, so I still have a bit of work to do.
Objects are either a collection of 32 or 64 bit words which link together to form LISP/Scheme-like data structures.
In summary, it is a complex program that does a lot of messing with pointers and it works the same under 32 and 64 bit modes.
Asking multiple questions does not get you all the answers you might want.
It seems to work, as I wrote, on Linux.
Again, thank you for helping me with this.