Creating Rust Executable from LLVM Bit Code

How can I generate an executable file for an application written in Rust that has been compiled into LLVM-IR bit code?

If I try to compile a .bc file with rustc, it tells me stream did not contain valid UTF-8 , and I cannot figure out if it has a parameter for rustc in it.

Basically I want to achieve this: program.rs - program.rs program.bc program . Where program is the final executable. What steps should I take to achieve this?

+8
rust bitcode llvm-ir
source share
2 answers

Starting from this source code:

 fn main() { println!("Hello, world!"); } 

You can create an intermediate representation of LLVM (IR) or bitcode (BC):

 # IR in hello.ll rustc hello.rs --emit=llvm-ir # BC in hello.bc rustc hello.rs --emit=llvm-bc 

These files can then be further processed by LLVM to create an assembly or object file:

 # Assembly in hello.s llc hello.bc # Object in hello.o llc hello.bc --filetype=obj 

Then you need to link the files to create the executable. This requires binding to the standard Rust libraries. The path depends on the platform and version:

 cc -L/path/to/stage2/lib/rustlib/x86_64-apple-darwin/lib/ -lstd-2f39a9bd -o hello2 hello.o 

Then you can run the program:

 DYLD_LIBRARY_PATH=/path/to/stage2/lib/rustlib/x86_64-apple-darwin/lib/ ./hello2 

This answer has specific OS X solutions, but general concepts should be extended to Linux and Windows. The implementation will be slightly different for Linux and probably significantly for Windows.

+5
source share

This is not obvious, since the LLVM documentation is very unclear, but clang will compile both LLVM IR files (".ll") and bitcode files (".bc") and link to your system libraries.

On Linux with Rust 1.9:

 clang -dynamic-linker /usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-d16b8f0e.so hello.ll -o hello 
+4
source share

All Articles