2017-06-12 128 views
7

我希望能夠使用Sierra 10.12.4在我的Mac上編寫和調試x64程序集。有人會認爲這不會是一個特別困難或模糊的願望,但儘管經過了許多小時的努力和大量的在線搜索,我還沒有成功,我還沒有找到任何其他人。是否可以在Mac OS上調試x64程序集?

我寧願使用NASM彙編程序,但如果必須使用GAS或任何帶有Intel語法的程序。 (順便說一句,注意,這兩個GDB和LLDB做工精細用的C文件用gcc編譯。)

這裏是我的情況,我已經試過:

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. 

GAS不起作用

我可以彙編,鏈接,並運行:

$ 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 OS。

有幾個老相關的StackOverflow問題,都沒有提供足夠的答案。

還有this way to use Xcode,奇蹟似乎工作...但它並沒有真正做我想要的。調試器實際上並不知道我的源文件;它只是單步執行指令並顯示反彙編代碼或其他內容。我也不想使用XCode。

幾個月前我在NASM郵件列表上詢問過這個問題,但沒有人回覆。

所以...

因此,它是目前不可能做到的最基本的東西的人可能會希望使用的是Mac電腦做一個?

如果有人有辦法做到這一點,請告訴我確切的必要命令。奇蹟

+0

什麼版本的'nasm'?什麼版本的'gdb','llvm'? –

+0

我已經試過了nasm版本2.13.01和2.14rc0,以及gdb版本7.12.1和8.0。 LLDB-370.0.42。 Apple llvm版本8.1.0。無論版本如何,我都會得到相同的結果。您是否暗示您已經開始調試以使用該軟件的某個版本? –

+0

我指的是nasm 2.13發行說明:「macho對象格式現在支持矮工具調試格式,正如新工具鏈所要求的那樣。」但你已經表示你是最新的。你在nasm論壇上問過嗎? –

回答

1

奇蹟,看來我可以用鏗鏘做到這一點:

$ clang -g -c -x assembler hello.s 
$ clang hello.o -o hello 
$ ./hello 
Hello, world! 
$ lldb hello 
(lldb) target create "hello" 
Current executable set to 'hello' (x86_64). 
(lldb) b hello.s:10 
Breakpoint 1: where = hello`main + 16, address = 0x0000000100000f7c 
(lldb) run 
Process 40460 launched: '/Users/mike/GoogleDrive/Projects/Sort/hello' (x86_64) 
Hello, world! 
Process 40460 stopped 
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 
    frame #0: 0x0000000100000f7c hello`main at hello.s:10 
    7  mov rbp, rsp 
    8  lea rdi, [rip + _main.S_0] 
    9  call _puts 
-> 10  mov rax, 0 
    11  mov rsp, rbp 
    12  pop rbp 
    13  ret 

不幸的是,據我可以告訴鐺的x64組件的支持完全是無證的,我想通了正確的咒語,要做到這一點只通過實驗。但是,我猜是這樣。

+0

我不知道在哪裏可以找到clang彙編程序的文檔,但Xcode有一個選項來顯示llvm彙編源代碼C/C++/ObjC源文件(Product-> Perform Action-> Assemble)。這可能會讓你得到類似的操作的例子,你可以爲你的目的進行嬰兒牀。 –

相關問題