gpt4 book ai didi

macos - OS/X 64 位汇编代码生成总线错误

转载 作者:行者123 更新时间:2023-12-04 22:57:46 30 4
gpt4 key购买 nike

我正在尝试在我的 64 位 Macbook Pro 上学习 NASM。我有以下代码,我试图将变量的值分配给初始化的变量。

global start
default rel

section .data
a: dq 1

section .bss

b: resq 1

section .text

start:
mov rax, a
mov [b], rax

代码编译并链接,但产生一个 bus error运行时。有没有人对如何克服这个问题有任何想法?

最佳答案

要回答有关 BUS ERROR 的具体问题,发生这种情况是因为您没有正确退出应用程序,并且处理器在代码中的最后一条指令之后开始执行内存中的内容。这最终导致故障。一旦处理器到达包含您的代码的可执行页面的末尾并开始执行 .data 时,可能会发生总线错误。部分。 .data部分不可执行,因此可能导致您观察到的错误。这只是一个有根据的猜测,因为它高度依赖于内存的内容和布局。

看来您正在绕过 C 运行时,因此您不能使用 RET 返回操作系统。您需要调用 64 位 OS/X 之一 SYSCALL s。

可以在 Apple's site 上找到 64 位 OS/X 系统调用的列表.您可以从此 tutorial 学习基础知识(在 64 位部分)。退出系统调用有一个条目:

1    AUE_EXIT   ALL   { void exit(int rval); } 


从教程中,参数传递约定被描述为:

  • arguments are passed on the registers rdi, rsi, rdx, r10, r8 and r9 syscall number in the rax register
  • the call is done via the syscall instruction
  • what OS X contributes to the mix is that you have to add 0x20000000 to the syscall number (still have to figure out why)


完整的调用约定在 64-bit System V ABI 中描述。 .关于 SYSCALL 的另一个重要说明是:

  • A system-call is done via the syscall instruction. The kernel destroys registers %rcx and %r11.


考虑到所有这些,我们想调用 1 AUE_EXIT ALL { void exit(int rval); } .系统调用号在第一列。在 64 位 OS/X 上,我们向其添加 0x2000000 并将其传递给 RAX。 exit 系统调用采用一个参数,因此它在 RDI 中传递。这是退出值。使用 exit 系统调用并返回 0 的代码可能如下所示:
mov eax, 0x2000001 
xor edi, edi ; Return exit value 0 to system
syscall

调试

正如@paulsm4 在他删除的答案中正确指出的那样:

Finally, I'm not sure where your "bus error" is coming from. But a debugger would tell you



要查找 SIGBUS 和 SIGSEGV 错误,最好使用调试器逐步执行汇编指令并找出故障所在。在这种情况下,您会发现在 mov [b], rax 之后调用了一条意外指令。 .

OS/X 上最常用的命令行调试器是 LLDB。您可以在 LLDB tutorial 中找到有关它使用的更多信息。 .

关于macos - OS/X 64 位汇编代码生成总线错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912536/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com