无法使用MS Linker链接LLVM编译器生成的obj文件 [英] Failed to link obj files generated by LLVM compiler using MS Linker
问题描述
我使用llvm在线编译器来编译示例C代码,
I used llvm online compiler to compile my sample C code,
int main() { return 0; }
生成的LLVM程序集
; ModuleID = '/tmp/webcompile/_31588_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define i32 @main() nounwind uwtable {
%1 = alloca i32, align 4
store i32 0, i32* %1
ret i32 0
}
然后我将LLVM程序集编译为obj文件
then I compiled LLVM assembly to obj file,
llc -filetype=obj a.ll
当我尝试使用link.exe a.o
链接obj文件时,出现错误
when I tried to link the obj file using link.exe a.o
I got the error
fatal error LNK1107: invalid or corrupt file: cannot read at 0x438
如何生成正确的obj文件以馈入link.exe?
How can I generate the right obj file to feed into link.exe?
更多信息
- 我使用Visual Studio 11构建了LLVM.我没有安装cygwin.
- link.exe也来自Visual Studio 11
- LLVM是从最新的源代码构建的.
如果我使用VC ++编译器将相同的代码编译为汇编,则看起来像这样,
If I compile the same code using VC++ compiler into assembly, it looks like this,
; Listing generated by Microsoft (R) Optimizing Compiler Version 17.00.50402.0
include listing.inc
INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES
PUBLIC main
; Function compile flags: /Odtp
_TEXT SEGMENT
main PROC
; File c:\tmp\u.c
; Line 1
xor eax, eax
ret 0
main ENDP
_TEXT ENDS
END
llc -filetype=asm j.ll
生成以下代码.使用ml.exe也会失败.
llc -filetype=asm j.ll
generates the following code. It fails with ml.exe either.
.def _main;
.scl 2;
.type 32;
.endef
.text
.globl _main
.align 16, 0x90
_main: # @main
# BB#0:
pushl %eax
movl $0, (%esp)
xorl %eax, %eax
popl %edx
ret
推荐答案
您的IR用于x86-64/linux,如目标三元组中所述.因此,llc将为您生成(默认情况下)ELF对象文件,而不是COFF.肯定link.exe将不会接受它.
You IR is for x86-64/linux as stated in target triplet. So, llc will generate (by default) ELF object file for you, not COFF. Surely link.exe will not accept it.
请注意,您不能仅将目标三元组更改为某些窗口并假定一切正常:
Note that you cannot just change the target triple to some windows and assume everything will work:
- 发射COFF目标代码是WIP
- C/C ++不是目标无关的语言,因此,您不能在此处获取目标无关的IR: http://llvm.org/docs/FAQ.html#platformindependent
这篇关于无法使用MS Linker链接LLVM编译器生成的obj文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!