如何从 LLVM ir 创建可执行文件?
How can I create an executable from LLVM ir?
我目前正在使用 llc to convert a .ll
file to a .s
using the commandline. Then I want to take this file and then use nasm 从中创建可执行文件。虽然第一步似乎工作正常,但我无法让第二步工作。
原始文件名为 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 告诉我:
The assembly language output can then be passed through a native assembler and linker to generate a native executable.
所以我使用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 或汇编程序的经验接近于零,我无法自己解决这个问题。
如果我遗漏了一些重要的事情,请告诉我,我会尽快编辑我的答案。
感谢 and 的评论,我终于明白了为什么这不起作用并找到了一个可行的替代方案。
问题原因
汇编语言种类繁多,语法各不相同,并不直接兼容。由于 nasm is expecting another assembly language than llc 正在生成,它显然不起作用,这解释了长长的错误列表。
如何代替
考虑到 llc has AT&T assembler as output, which was created for the GNU toolchain, the most obvious step would be to use GCC to create the executable after building the code.s
file with llc。
安装GCC I downloaded MinGW,安装并调用
mingw-get install gcc
现在我可以访问 GCC,它可以通过调用
创建 code.exe
gcc code.s -o code.exe
gcc [filename] -o [name of the created executable]
由于此解决方案可能比它需要的更复杂,我很高兴看到一些替代方案/改进。
我目前正在使用 llc to convert a .ll
file to a .s
using the commandline. Then I want to take this file and then use nasm 从中创建可执行文件。虽然第一步似乎工作正常,但我无法让第二步工作。
原始文件名为 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 告诉我:
The assembly language output can then be passed through a native assembler and linker to generate a native executable.
所以我使用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 或汇编程序的经验接近于零,我无法自己解决这个问题。
如果我遗漏了一些重要的事情,请告诉我,我会尽快编辑我的答案。
感谢
问题原因
汇编语言种类繁多,语法各不相同,并不直接兼容。由于 nasm is expecting another assembly language than llc 正在生成,它显然不起作用,这解释了长长的错误列表。
如何代替
考虑到 llc has AT&T assembler as output, which was created for the GNU toolchain, the most obvious step would be to use GCC to create the executable after building the code.s
file with llc。
安装GCC I downloaded MinGW,安装并调用
mingw-get install gcc
现在我可以访问 GCC,它可以通过调用
创建code.exe
gcc code.s -o code.exe
gcc [filename] -o [name of the created executable]
由于此解决方案可能比它需要的更复杂,我很高兴看到一些替代方案/改进。