问题描述
这是代码(退出):
.section .data,
.section .text,
.globl _start
_start:
movl $1, %eax
movl $32, %ebx
syscall
当我执行" as exit.s -o exit.o && ld exit.o -o exit -e _start && ./exit"
返回的错误是总线错误:10",输出的"echo $?
"是138
the return is "Bus error: 10" and the output of "echo $?
" is 138
我也在这个问题中尝试了正确答案的示例: Process命令Linux 64位中的行
I also tried the example of the correct answer in this question: Process command line in Linux 64 bit
stil收到总线错误" ...
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
(或由于,因为在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)
(nasm
自动翻译如果选择了Mach-O作为目标对象类型,则视情况而定)-请参见 Mac OS X ABI Mach-O文件格式参考.即使正确安装了说明,将它们放在错误的段/节中也可能导致总线错误.您可以使用.section __TEXT,__text
指令(请参见此处(用于指令语法),或者您也可以使用(简单的).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
在这里,按预期工作:
$ as -o exit.o exit.s; ld -o exit exit.o
$ ./exit; echo $?
32
这篇关于基本程序集不能在Mac(x86_64 + Lion)上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!