Build the LLVM-IR Code to Binary/Executable File

I'm currently trying to convert LLVM-IR code into a final executable/binary file. Specifically, I want to generate the LLVM-IR of a program using cargo rustc -- --emit=llvm-ir, then perform some analysis on that LLVM-IR, and finally compile that LLVM-IR code into a binary. Here are the steps I've taken so far:

  1. Emit llvm-ir: cargo rustc -- --emit=llvm-ir
  2. Process the analysis and generate program.ll
  3. Generate object code: llc program.ll --filetype=obj

However, I'm unsure about the subsequent steps. To my knowledge, there should be a way to create a binary from the LLVM-IR code using cargo and rustc. Could anyone guide me on how to achieve this?

Thank you!
Have a nice Day :slight_smile:

Can't you just compile the whole thing normally using the regular cargo build workflow, and separately output the LLVM you want to analyze?

Or by "analyze", do you actually mean "modify", i.e. you want to transform the LLVM that rustc outputs? Because in that case, you'll have to replicate the whole linker invocation of rustc. I'm not sure whether that's easy or documented at all.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.