windows - 如何从 LLVM ir 创建可执行文件?

标签 windows assembly llvm nasm llvm-ir

我目前正在使用 llc使用命令行将 .ll 文件转换为 .s。然后我想拿这个文件然后使用 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 或汇编程序的经验接近于零,因此我无法自己解决这个问题。

如果我遗漏了一些重要的东西,请告诉我,我会尽快编辑我的答案。

最佳答案

感谢@Michael Petch的评论和 @Ross Ridge我终于明白了为什么这行不通并找到了可行的替代方案。


问题原因

汇编语言有多种,语法各不相同,不能直接兼容。作为nasm期待另一种汇编语言而不是 llc正在生产,它显然不起作用,这解释了长长的错误列表。

如何做到这一点

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

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

mingw-get install gcc

现在我可以访问 GCC可用于通过调用

创建code.exe

gcc code.s -o code.exe

gcc [文件名] -o [创建的可执行文件的名称]


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

关于windows - 如何从 LLVM ir 创建可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985953/

相关文章:

java - 如何将 PATH 环境变量设置为指向 JRE 1.5 版

c# - 通用 Windows 10 WebView - 如何清除/禁用缓存?

pointers - 'fword ptr' 到底是什么?

assembly - 编译器通常使用寄存器来实现 "intended"的目的吗?

clang - 如何为 Apple M1 构建 LLVM (clang,clang++)?

llvm - 具有整数结果的整数的 Julia/LLVM 高效除法

c# - 在 Windows Phone 8.1 中如何知道哪一页是我的最后一页

windows - WinHttp : can it be used in parallel?

assembly - Vic 20 使用汇编器滚动

c++ - 如何直接从 LLVM 源代码树使用新编译的 LLVM 工具?