llvmlite icon indicating copy to clipboard operation
llvmlite copied to clipboard

llvmlite.binding.TargetMachine.emit_object() emits unknown object format on Windows

Open OffensiverHase opened this issue 1 year ago • 2 comments

Hi, I am trying to create an AOT compiled language with llvmlite and I'm trying to get an object file and then link it via gcc, but gcc gives me the following error when running gcc main_temp.o -o main:

main_temp.o: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

I have seen that a similar question has been asked here, but I tried the solutions given there and they didn't work.

My code looks like this:

import llvmlite.binding as llvm

OUTPUT = 'main'

target = llvm.Target.from_triple(module.triple)  # 'x86_64-pc-windows-msvc'
target_machine = target.create_target_machine(codemodel='default')

with open(OUTPUT + '_temp.o', 'wb') as f:
    f.write(target_machine.emit_object(llvm_module))
subprocess.run(['gcc', OUTPUT + '_temp.o', '-o', OUTPUT], capture_output=True, text=True)
os.remove(OUTPUT + '_temp.o')

objdump -a main_temp.o also fails: objdump: main_temp.o: File format not recognized Thanks in advance

OffensiverHase avatar May 25 '24 15:05 OffensiverHase

I am not certain but the -msvc should cause LLVM to produce COFF but does gcc accept that? Is this the inverse problem that you need -elf for gcc to work?

sklam avatar May 28 '24 13:05 sklam

Sorry, but I cannot follow you. What do I have to do to get a .exe file? I thought elf is only for linux?

OffensiverHase avatar Jun 02 '24 14:06 OffensiverHase