How do I compile rust with my own out-of-tree LLVM?

Hi rustaceans. I’m having a trouble with cross compiling (nostd code) Rust.

Basically I have my own LLVM that’s not in the upstream, and what I’d like to do is

  • Use rustc to compile rust code into LLVM IR
  • Use my own LLVM llc, opt to compile LLVM IR into objects, and finally linking the objects with ld or lld

Now I’m starting, so target platform is the same as host. That’s, I just want to split the build process into the above two phases.

I compiled my rust project into LLVM IR with cargo rustc -- --emit=llvm-ir and did the same with libcore.
The rustc and libcore are on the same commit. Then I used ls *.ll | xargs -IX llc -filetype=obj X to generate object files. No problem till here.

But when I tried to link all the code together, the linker complained about undefined references.
My project wanted _ZN4core9panicking5panic17he3feabc16d430735E but libcore provided _ZN4core9panicking5panic17ha8afdce0157d83a3E. Only the last hash part was different.

I’m using the 2224a42c3536 commit for both rustc and libcore. So I feel it might be an LLVM versioning problem but I can’t be sure.

So could you provide help on how to build rust projects in two phases? Thank you very much!

I’m a little out of my league here, but I noticed that was a call to panic. In your call to cargo did you set panic to abort on failure?

If not try adding this to your Cargo.toml.

panic = "abort"

or the -C panic=abort flag to the rustc call.

1 Like

Thank you for the solution.

I see, probably that’s the only workaround for me now.:thinking::worried: