【问题标题】:How can I create an executable from LLVM ir?如何从 LLVM ir 创建可执行文件?
【发布时间】: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


【解决方案1】:

感谢@Michael Petch@Ross Ridge 的cmets,我终于明白了为什么这不起作用并找到了可行的替代方案。


问题原因

有不同种类的汇编语言,它们的语法各不相同,并且不直接兼容。由于nasm 期望使用另一种汇编语言而不是llc 产生的汇编语言,它显然不起作用,这就解释了长长的错误列表。

怎么做

考虑到llc 有AT&T 汇编器作为输出,它是为GNU toolchain 创建的,最明显的步骤是在使用llc 构建code.s 文件后使用GCC 创建可执行文件。

要安装GCC 我下载了MinGW,安装并调用

mingw-get 安装 gcc

现在我可以访问GCC,它可以通过调用来创建code.exe

gcc code.s -o code.exe

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


由于这个解决方案可能比它需要的更复杂,我很高兴看到一些替代方案/改进。

【讨论】:

  • 如果你有llvm,你不是已经有clang了吗?这将节省您添加 YATC(又一个工具链)。
  • 弗兰克是对的。 clang 已经内置了一个汇编器,或者可以使用 binutils 中的 as。如果您已经拥有 clang,则不需要 gcc 来构建 .s 文件。
  • @Frank C. 据我所知,我没有 clang/我需要下载它。但只要我这样做的方式有效,我不想花大量时间学习如何以另一种方式去做,即使我的方式更复杂
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
相关资源
最近更新 更多