我希望能够在我的 Mac 上使用 Sierra 10.12.4 编写和调试 x64 程序集。人们可能会认为这不会是一个特别困难或晦涩的愿望,但尽管花费了很多时间的努力和大量的在线搜索,我仍然没有成功,而且我还没有找到其他人有这样的愿望。
我更喜欢使用 NASM 汇编器,但如果必须的话,我会使用 GAS 或任何具有 Intel 语法的东西。 (顺便说一句,请注意,gdb 和 lldb 都可以与 gcc 编译的 C 文件一起正常工作。)
这是我的情况和我尝试过的方法:
NASM 不起作用
我可以组装并链接一个文件并验证它是否有效。
$ nasm -f macho64 -g -F dwarf hello2.s -o hello2.o
$ gcc hello2.o -o hello2
$ ./hello2
Hello, world!
但我无法用 gdb 调试它(请注意,我确实做了所有必要的代码设计废话):
$ gdb hello2
GNU gdb (GDB) 8.0
<snip>
Reading symbols from hello2...done.
(gdb) list
1 section .data
2
3 msg: db "Hello, world!", 0
4
5 section .text
6 global _main
7 extern _puts
8
9 _main:
10 push rbp
(gdb) break 10
Breakpoint 1 at 0x0: file hello2.s, line 10.
(gdb) run
Starting program: /Users/mike/GoogleDrive/Projects/Sort/hello2
[New Thread 0x1403 of process 38022]
warning: unhandled dyld version (15)
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x0
Command aborted.
我无法使用 lldb 调试它:
$ lldb hello2
(lldb) target create "hello2"
Current executable set to 'hello2' (x86_64).
(lldb) b hello2.s:10
Breakpoint 1: no locations (pending).
WARNING: Unable to resolve breakpoint to any actual locations.
天然气不起作用
我可以组装、链接和运行:
$ gcc -g hello.s -o hello
$ ./hello
Hello, world!
但我无法使用 gdb 进行调试:
$ gdb hello
GNU gdb (GDB) 8.0
<snip>
Reading symbols from hello...Reading symbols from /Users/mike/GoogleDrive/Projects/Sort/hello.dSYM/Contents/Resources/DWARF/hello...done.
done.
(gdb) list
1 .intel_syntax
2 .text
3 .globl _main
4
5 _main:
6 push rbp
7 mov rbp, rsp
8 lea rdi, [rip + _main.S_0]
9 call _puts
10 mov rax, 0
(gdb) break 6
No line 6 in the current file.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (6) pending.
(gdb) run
Starting program: /Users/mike/GoogleDrive/Projects/Sort/hello
[New Thread 0x1403 of process 38063]
warning: unhandled dyld version (15)
Hello, world!
[Inferior 1 (process 38063) exited normally]
(所以它只是运行并忽略断点。)
我无法使用 lldb 调试它:
$ lldb hello
(lldb) target create "hello"
Current executable set to 'hello' (x86_64).
(lldb) b hello.s:6
Breakpoint 1: no locations (pending).
WARNING: Unable to resolve breakpoint to any actual locations.
我在网上找到的东西
Here是一篇关于gdb
不适用于新版本的 Mac 操作系统。
StackOverflow 有几个相关的老问题,但都没有提供足够的答案。
还有这种方式使用Xcode,这似乎奇迹般地有效......但它实际上并没有达到我想要的效果。调试器实际上并不知道我的源文件;它只是单步执行指令并显示反汇编代码或其他内容。我也不想使用XCode。
几个月前,我在 NASM 邮件列表上询问过这个问题,但没有人回复。
So...
那么目前是否无法做到人们可能想用计算机做的最基本的事情之一使用Mac?
如果有人有办法做到这一点,请准确地向我展示必要的命令。