Failed to link obj files generated by LLVM compiler using MS Linker

亡梦爱人 提交于 2019-12-11 08:23:26

问题


I used llvm online compiler to compile my sample C code,

int main() { return 0; }

the generated LLVM assembly,

 ; 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
}

then I compiled LLVM assembly to obj file,

llc -filetype=obj a.ll

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

How can I generate the right obj file to feed into link.exe?


More information

  • I built LLVM using Visual Studio 11. I didn't have cygwin installed.
  • link.exe is also from Visual Studio 11
  • LLVM was built from latest source code.

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 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

回答1:


You are in a strange land, you can read over: http://llvm.org/docs/GettingStartedVS.html which may help




回答2:


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:

  1. COFF object code emission is WIP
  2. C/C++ are not target-independent languages, so, you cannot obtain target-independent IR here: http://llvm.org/docs/FAQ.html#platformindependent


来源:https://stackoverflow.com/questions/10080561/failed-to-link-obj-files-generated-by-llvm-compiler-using-ms-linker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!