繁体   English   中英

如何从LLVM IR生成可执行文件

[英]How to generate executable from LLVM IR

我在阅读http://www.stephendiehl.com/llvm/#llvm-introduction,其中有一段LLVM IR,如下所示:

declare i32 @putchar(i32)

define i32 @add(i32 %a, i32 %b) {
  %1 = add i32 %a, %b
  ret i32 %1
}

define void @main() {
  %1 = call i32 @add(i32 0, i32 97)
  call i32 @putchar(i32 %1)
  ret void
}

我想尝试使用llvmnasm来运行它,但是失败了:

llc -march=x86-64 h1.bc -o h1.s
nasm -f macho -o h1.o h1.s
# failed here

错误的第一行是:

h1.s:1: error: attempt to define a local label before any non-local labels
h1.s:1: error: parser: instruction expected
h1.s:2: error: attempt to define a local label before any non-local labels
h1.s:2: error: parser: instruction expected
h1.s:3: error: attempt to define a local label before any non-local labels
h1.s:3: error: parser: instruction expected
h1.s:4: error: attempt to define a local label before any non-local labels

llc生成的代码似乎不是本机的OS X汇编代码,请参见http://peter.michaux.ca/articles/assembly-hello-world-for-os-x

生成可执行文件的正确命令是什么?

nasm不支持AT&T汇编语法,因此会出现错误。 在OS X上,您需要使用“ as”进行组装(除非明确要求,否则在99%的情况下请忽略nasm)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM