基本组件在 Mac (x86_64+Lion) 上不起作用? [英] basic assembly not working on Mac (x86_64+Lion)?
问题描述
这是代码(exit.s):
here is the code(exit.s):
.section .data,
.section .text,
.globl _start
_start:
movl $1, %eax
movl $32, %ebx
syscall
当我执行 " 作为 exit.s -o exit.o && ld exit.o -o exit -e _start && ./exit"
返回的是Bus error: 10",而echo $?
"的输出是138
the return is "Bus error: 10" and the output of "echo $?
" is 138
我也试过这个问题中正确答案的例子:处理命令Linux 64 位行
I also tried the example of the correct answer in this question: Process command line in Linux 64 bit
仍然得到总线错误"...
stil get "bus error"...
推荐答案
首先,您在 Mac OS X 上使用旧的 32 位 Linux 内核调用约定 - 这绝对行不通.
First, you are using old 32-bit Linux kernel calling convention on Mac OS X - this absolutely doesn't work.
其次,Mac OS X 中的系统调用以不同的方式构建——它们都有一个前导类标识符和一个系统调用号.该类可以是 Mach、BSD 或其他东西(参见 此处 在 XNU 源代码中)并向左移动 24 位.正常的 BSD 系统调用具有 2
类,因此从 0x2000000
开始.0
类中的系统调用无效.
Second, syscalls in Mac OS X are structured in a different way - they all have a leading class identifier and a syscall number. The class can be Mach, BSD or something else (see here in the XNU source) and is shifted 24 bits to the left. Normal BSD syscalls have class 2
and thus begin from 0x2000000
. Syscalls in class 0
are invalid.
根据 SysV AMD64 ABI 的 §A.2.1,也遵循在 Mac OS X 中,系统调用 ID(连同它在 XNU 上的类!)转到 %rax
(或 %eax
,因为 XNU 上未使用高 32 位).第一个参数在 %rdi
中.接下来转到 %rsi
.等等.%rcx
被内核使用并且它的值被破坏,这就是为什么 libc.dyld
中的所有函数在进行系统调用之前将其保存到 %r10
(类似于 syscall_sw.h
中的 kernel_trap
宏).
As per §A.2.1 of the SysV AMD64 ABI, also followed by Mac OS X, syscall id (together with its class on XNU!) goes to %rax
(or to %eax
as the high 32 bits are unused on XNU). The fist argument goes in %rdi
. Next goes to %rsi
. And so on. %rcx
is used by the kernel and its value is destroyed and that's why all functions in libc.dyld
save it into %r10
before making syscalls (similarly to the kernel_trap
macro from syscall_sw.h
).
第三,Mach-O 二进制文件中的代码部分称为 __text
而不是 Linux ELF 中的 .text
并且也驻留在 __TEXT
中段,统称为 (__TEXT,__text)
(如果选择 Mach-O 作为目标对象类型,nasm
会自动适当地翻译 .text
)- 请参阅 Mac OS X ABI Mach-O文件格式参考.即使您得到正确的汇编指令,将它们放在错误的段/节中也会导致总线错误.您可以使用 .section __TEXT,__text
指令(请参阅 here 用于指令语法)或者您也可以使用(更简单的).text
指令,或者您可以完全删除它,因为它是如果没有向 as
提供 -n
选项(请参阅 as
的联机帮助页),则假定.
Third, code sections in Mach-O binaries are called __text
and not .text
as in Linux ELF and also reside in the __TEXT
segment, collectively referred as (__TEXT,__text)
(nasm
automatically translates .text
as appropriate if Mach-O is selected as target object type) - see the Mac OS X ABI Mach-O File Format Reference. Even if you get the assembly instructions right, putting them in the wrong segment/section leads to bus error. You can either use the .section __TEXT,__text
directive (see here for directive syntax) or you can also use the (simpler) .text
directive, or you can drop it altogether since it is assumed if no -n
option was supplied to as
(see the manpage of as
).
第四,Mach-O ld
的默认入口点称为 start
(虽然,正如您已经知道的,它可以通过-e
链接器选项).
Fourth, the default entry point for the Mach-O ld
is called start
(although, as you've already figured it out, it can be changed via the -e
linker option).
鉴于上述所有内容,您应该修改汇编源代码如下:
Given all the above you should modify your assembler source to read as follows:
; You could also add one of the following directives for completeness
; .text
; or
; .section __TEXT,__text
.globl start
start:
movl $0x2000001, %eax
movl $32, %edi
syscall
就是这样,按预期工作:
Here it is, working as expected:
$ as -o exit.o exit.s; ld -o exit exit.o
$ ./exit; echo $?
32
这篇关于基本组件在 Mac (x86_64+Lion) 上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!