【发布时间】:2018-02-09 16:12:48
【问题描述】:
我目前正在使用llc 使用命令行将.ll 文件转换为.s。然后我想获取这个文件,然后使用nasm 从中创建一个可执行文件。虽然第一步似乎工作正常,但我无法让第二步工作。
原始文件名为code.ll,包含以下代码:
define i32 @main() {
ret i32 0
}
现在我使用 cmd 通过键入以下内容来构建 .s 文件:
llc 代码.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 代码.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 或汇编程序的经验接近于零,我无法自己解决这个问题。
如果我遗漏了重要的内容,请告诉我,我会尽快修改答案。
【问题讨论】:
-
生成的代码是 AT&T 语法,NASM 无法理解。您必须使用 GNU 汇编器 (
as) 来汇编该.s文件或任何能够理解 AT&T 语法的汇编器。 -
@Michael Petch 好的,虽然我无法在 atm 测试它,但听起来这绝对是我需要的。你介意写一个答案,这样我就可以接受了。如果您没有时间,我会在几天后写一个自我答案,这几乎只是重申您的答案,并简要解释什么是 AT&T 语法以及 NASM 使用什么语法。谢谢
-
当您认为您已经解决问题时,请成为我的客人并自行回答。
-
LLVM 有它自己的汇编器,你应该可以使用或者更好
llc可以直接生成一个目标文件,而无需调用“本机汇编器”。你只需要使用对象文件上的本机链接器以创建本机可执行文件。 -
@MichaelPetch - 要生成英特尔汇编语法,请使用
llc -x86-asm-syntax=intel filename.ll但它仍然不是 100%nasm兼容。我当时只是使用clang来编译/链接到可执行文件。
标签: windows assembly llvm nasm llvm-ir