hello world (x86-64 windows & linux)

时间:2021-06-11 15:12:32

Is llvm able to compile, assemble and link x86-64 code on windows and/or linux using llvm-mc and lld?

llvm是否能够使用llvm-mc和lld在windows和/或linux上编译、组装和链接x86-64代码?

If so, is there a hello-world level example out there? The documentation is pretty sparse at present.

如果是这样,是否存在一个地狱世界级别的例子?目前文档相当稀疏。


I've tried building a simple hello world (main0.cpp) using trunk LLVM (with clang & lld).

我尝试过使用trunk LLVM(使用clang和lld)构建一个简单的hello world (main0.cpp)。

main0.cpp:

main0.cpp:

int main(int argc, char const* argv[])
    { return 0; }

Compile (no errors):

编译(没有错误):

[MY-LLVM]/clang -S -o main0.s main0.cpp

Assemble (no errors):

组装(没有错误):

[MY-LLVM]/llvm-mc -arch=x86-64 -triple=x86_64-linux-gnu -o main0.o main0.s

Link (FAILS HERE!):

链接(在这里失败!):

[MY-LLVM]/lld -flavor gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib -L/<MY-LLVM>/lib -L/lib -L/usr/lib -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o -o main0.run main.o

Link error:

链接错误:

lld: unknown input file format for file main.o


I obtained the linker command via clang -o main0.run main0.cpp -### (replacing ld with [MY-LLVM]/lld -flavor gnu).

我通过clang -o main0运行main0获得了链接器命令。用[MY-LLVM]/lld -flavor gnu替换ld。

I assume that I'm either generating the wrong type of object file when assembling, or using the wrong parameters when linking.

我假设在组装时生成了错误类型的对象文件,或者在链接时使用了错误的参数。

Does anyone know how to do this right?

有人知道怎么做吗?


(My ultimate aim is to get full C++14 working on win64 (without massive hacks), but I'm struggling with getting trunk clang to work with the mingw tools so I thought I'd try pure LLVM).

(我的最终目标是在win64上完成c++ 14的工作(没有大规模的黑客攻击),但是我正在努力让trunk clang与mingw工具一起工作,所以我想试试纯LLVM。

1 个解决方案

#1


4  

lld developer here.

lld开发者。

lld self hosts on both Linux and Windows, so I would expect them to work here. The way I generally get clang to use lld is to make a symlink to lld named ld and add it to PATH. lld will behave like gnu-ld in this case.

lld self在Linux和Windows上都是主机,所以我希望它们在这里工作。我通常让clang使用lld的方法是创建一个名为ld的符号链接,并将其添加到PATH中。在这种情况下,lld的行为与gnu-ld类似。

As for llvm-mc, it is not an assembler. It is a tool for testing the MC layer of llvm. By default it simply parses the assembly and prints it back out. This is why lld is rejecting the main0.o file, as it's actually a text file.

至于llvm-mc,它不是汇编程序。它是一个用于测试llvm的MC层的工具。默认情况下,它只是解析程序集并将其打印出来。这就是为什么lld拒绝main0。o文件,因为它实际上是一个文本文件。

The correct thing to do here without having lld as ld in the path is either:

在这里,正确的做法是在路径中不使用lld作为ld:

clang -c -o main0.o main0.cpp

or:

或者:

clang -S -o main0.s main0.cpp
clang -c -o main0.o main0.s

If you really want the assembly for some reason.

如果你真的想要组装。

#1


4  

lld developer here.

lld开发者。

lld self hosts on both Linux and Windows, so I would expect them to work here. The way I generally get clang to use lld is to make a symlink to lld named ld and add it to PATH. lld will behave like gnu-ld in this case.

lld self在Linux和Windows上都是主机,所以我希望它们在这里工作。我通常让clang使用lld的方法是创建一个名为ld的符号链接,并将其添加到PATH中。在这种情况下,lld的行为与gnu-ld类似。

As for llvm-mc, it is not an assembler. It is a tool for testing the MC layer of llvm. By default it simply parses the assembly and prints it back out. This is why lld is rejecting the main0.o file, as it's actually a text file.

至于llvm-mc,它不是汇编程序。它是一个用于测试llvm的MC层的工具。默认情况下,它只是解析程序集并将其打印出来。这就是为什么lld拒绝main0。o文件,因为它实际上是一个文本文件。

The correct thing to do here without having lld as ld in the path is either:

在这里,正确的做法是在路径中不使用lld作为ld:

clang -c -o main0.o main0.cpp

or:

或者:

clang -S -o main0.s main0.cpp
clang -c -o main0.o main0.s

If you really want the assembly for some reason.

如果你真的想要组装。