1

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
woodings
  • 7,503
  • 5
  • 34
  • 52
  • 1
    I would assume that the asm isn't compatible between linux and windows, what does the same program look like if compiled to asm with the VS 11 compiler? – Grady Player Apr 09 '12 at 22:21
  • @GradyPlayer seems the assemblies generated by llvm and VC are quite different. How can I force llvm compiler to generate VC compatible code? – woodings Apr 09 '12 at 22:31
  • @woodings: write a conversion tool to convert the object files to COFF or modify the LLVM sources accordingly? Or simply follow the instructions on the page linked in Grady's answer. – 0xC0000022L Apr 10 '12 at 03:24

2 Answers2

1

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

Grady Player
  • 14,399
  • 2
  • 48
  • 76
1

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
Anton Korobeynikov
  • 9,074
  • 25
  • 28