2017-08-31 111 views
1

我正在使用llc來使用命令行將.ll文件轉換爲.s。然後我想取這個文件,然後用nasm來創建一個可執行文件。雖然第一步似乎工作正常,但我無法完成第二步工作。如何從LLVM ir創建可執行文件?


原始文件被稱爲code.ll,幷包含下面的代碼:

define i32 @main() { 
    ret i32 0 
} 

現在我使用CMD通過鍵入來構建.s文件:

LLC code.ll

此工作正常,並創建一個code.s文件包含以下代碼:

.def  @feat.00; 
    .scl 3; 
    .type 0; 
    .endef 
    .globl @feat.00 
@feat.00 = 1 
    .def  _main; 
    .scl 2; 
    .type 32; 
    .endef 
    .text 
    .globl _main 
    .align 16, 0x90 
_main:         # @main 
# BB#0: 
    xorl %eax, %eax 
    ret 

現在我想要使用此代碼,關於其llc doc告訴我要創建可執行此:

彙編語言輸出然後可以通過本地傳遞彙編器和鏈接器來生成本機可執行文件。

於是我就用nasm(應該做我想做的,據我瞭解)通過鍵入:

NASM code.s

產生錯誤的下面的列表:

code.s:1: error: attempt to define a local label before any non-local labels 
code.s:1: error: parser: instruction expected 
code.s:2: error: attempt to define a local label before any non-local labels 
code.s:2: error: parser: instruction expected 
code.s:3: error: attempt to define a local label before any non-local labels 
code.s:3: error: parser: instruction expected 
code.s:4: error: attempt to define a local label before any non-local labels 
code.s:5: error: attempt to define a local label before any non-local labels 
code.s:5: error: parser: instruction expected 
code.s:6: error: parser: instruction expected 
code.s:7: error: parser: instruction expected 
code.s:8: error: parser: instruction expected 
code.s:9: error: parser: instruction expected 
code.s:12: error: parser: instruction expected 
code.s:13: error: parser: instruction expected 
code.s:14: error: parser: instruction expected 
BB#0::1: error: parser: instruction expected 
BB#0::2: error: parser: instruction expected 
BB#0::3: error: parser: instruction expected 
BB#0::4: error: parser: instruction expected 
BB#0::5: error: parser: instruction expected 
BB#0::8: error: parser: instruction expected 
BB#0::9: error: parser: instruction expected 
BB#0::10: error: parser: instruction expected 

由於我關於LLVM或彙編器的經驗接近於零,我無法自己解決這個問題。

如果我遺漏了一些重要的東西,請告訴我,我會盡快編輯我的答案。

+0

生成的代碼是AT&T語法,NASM不會理解它。您必須使用GNU彙編程序('as')來彙編該'.s'文件或任何能夠理解AT&T語法的彙編程序。 –

+0

@Michael Petch好的,雖然我無法測試它atm它肯定聽起來像這是我所需要的。你願意寫一個答案,以便我可以接受它。如果你沒有足夠的時間,我會在幾天內寫出自己的答案,這幾乎只是重申一下你的答案,簡要說明AT&T語法和NASM使用的語法。謝謝 – SleepingPanda

+0

當你認爲你已經解決它時,請成爲我的客人並自己回答。 –

回答

-1

感謝@Michael Petch@Ross Ridge的意見,我終於明白了爲什麼這不起作用,並找到了一個可行的選擇。


問題

有各種不同的彙編語言的,它在語法變化,並且不直接兼容的原因。由於nasm需要另一個彙編語言,而llc正在生成,所以它顯然不起作用,這就解釋了一長串錯誤。

怎麼做,而不是

考慮到llc有AT &牛逼彙編作爲輸出,這是爲GNU toolchain創建的,最明顯的一步是使用GCC建設code.s文件,之後創建可執行文件llc

要安裝GCC我下載MinGW,安裝了它,並呼籲

的MinGW得到安裝GCC

現在我可以訪問GCC可用於創建code.exe通過調用

gcc code.s -o code.exe

gcc [filename] -o [name of the created executable]


由於這種解決方案可能複雜得多,它需要我會很高興地看到一些替代/改進。

+1

如果你有'llvm'你不是已經有'clang'嗎?這將爲您節省添加YATC(另一個工具鏈)。 –

+0

弗蘭克是對的。 'clang'已經有了一個內置的彙編程序,或者可以使用binutils中的'as'。如果你已經有'clang',你不需要gcc來建立'.s'文件。 –

+0

@Frank C.據我所知,我沒有鐺/我需要下載它。但只要我以這種方式工作,我不想花費大量時間學習如何以另一種方式來做,即使我的方式更復雜 – SleepingPanda