Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an executable from LLVM ir?

I am currently using llc to convert a .ll file to a .s using the commandline. Then I want to take this file and then use nasm to create an executable from it. While the first step seems to work fine, I can't get the second step to work.


the original file is called code.ll and contains the following code:

define i32 @main() {
    ret i32 0
}

now I use the cmd to build the .s file by typing:

llc code.ll

this works fine and creates a code.s file containing the following code:

    .def     @feat.00;
    .scl    3;
    .type   0;
    .endef
    .globl  @feat.00
@feat.00 = 1
    .def     _main;
    .scl    2;
    .type   32;
    .endef
    .text
    .globl  _main
    .align  16, 0x90
_main:                                  # @main
# BB#0:
    xorl    %eax, %eax
    ret

Now I want to create an executable using this code, about which the llc doc tells me this:

The assembly language output can then be passed through a native assembler and linker to generate a native executable.

So I use nasm (which should do what I want as far as I understand) by typing:

nasm code.s

which produces the following list of errors:

code.s:1: error: attempt to define a local label before any non-local labels
code.s:1: error: parser: instruction expected
code.s:2: error: attempt to define a local label before any non-local labels
code.s:2: error: parser: instruction expected
code.s:3: error: attempt to define a local label before any non-local labels
code.s:3: error: parser: instruction expected
code.s:4: error: attempt to define a local label before any non-local labels
code.s:5: error: attempt to define a local label before any non-local labels
code.s:5: error: parser: instruction expected
code.s:6: error: parser: instruction expected
code.s:7: error: parser: instruction expected
code.s:8: error: parser: instruction expected
code.s:9: error: parser: instruction expected
code.s:12: error: parser: instruction expected
code.s:13: error: parser: instruction expected
code.s:14: error: parser: instruction expected
BB#0::1: error: parser: instruction expected
BB#0::2: error: parser: instruction expected
BB#0::3: error: parser: instruction expected
BB#0::4: error: parser: instruction expected
BB#0::5: error: parser: instruction expected
BB#0::8: error: parser: instruction expected
BB#0::9: error: parser: instruction expected
BB#0::10: error: parser: instruction expected

As my experience concerning LLVM or assembler is close to zero I were unable to solve this myself.

In case I have left out something important just tell me and I will edit my answer as fast as possible.

like image 619
lncr Avatar asked Aug 31 '17 16:08

lncr


People also ask

What is LLVM IR code?

LLVM IR is a low-level intermediate representation used by the LLVM compiler framework. You can think of LLVM IR as a platform-independent assembly language with an infinite number of function local registers.

How do I run a BC file?

It is possible to execute a bc-file with lli command. However that doesn't create a stand alone executable product. Show activity on this post. There's always the option of using llc to compile to assembly from which you can generate an executable.

What is LLVM .LL file?

An LL file is an intermediate representation created by the LLVM compiler. It contains source code that has been translated into LLVM's platform-agnostic assembly language. LL files are used to optimize programs for different target architectures.


1 Answers

Thanks to the comments of @Michael Petch and @Ross Ridge I finally understood why this does not work and found a working alternative.


The cause of the problem

There are different kinds of assembler language, which vary in syntax and are not directly compatible. As nasm is expecting another assembly language than llc is producing, it obviously does not work, which explains the long list of errors.

How to do it instead

Considering that llc has AT&T assembler as output, which was created for the GNU toolchain, the most obvious step would be to use GCC to create the executable after building the code.s file with llc.

To install GCC I downloaded MinGW, installed it and called

mingw-get install gcc

now I can access GCC which can be used to create code.exe by calling

gcc code.s -o code.exe

gcc [filename] -o [name of the created executable]


As this solution is probably more complicated than it needs to be I would be glad to see some alternatives/ improvements.

like image 170
lncr Avatar answered Oct 07 '22 17:10

lncr