0

1,I used the following instructions to generate the LLVM IR rustc --emit=llvm-ir main.rs

2,then convert to assembly code using LLC

llc-10 main.ll -o main.s

3,want to compile main.s into an executable Using assembler -- as as -o main.o main.s Using linker -- ld ld -s -o main main.o while one error appeared, which is “undefined reference to rust_eh_personality”. enter image description here

i want to ask why and how can i do?

jing haha
  • 1
  • 4
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – KamilCuk May 24 '21 at 11:26
  • Welcome to StackOverflow! It looks like you just need to link with the Rust standard library, just like you would need to do with most C code. This is possible (at least dynamically, using the files in the `lib` dir of your toolchain) but unusual. Plus, Rust uses a slightly modified LLVM which doesn't have a `llc` command, so the IR/bitcode may not be compatible with your version of `llc`. `rustc`'s IR output is only meant for debugging as far as I'm aware. Can you explain more about your use case? Why are you emitting IR and using `llc` instead of something more-compiled like an object file? – Coder-256 May 25 '21 at 02:04
  • 1
    To be clear, your question is mostly answerable as-is, but I highly suspect that this is not the best way to accomplish your end goal, which is why it would be useful to know more about the bigger picture. – Coder-256 May 25 '21 at 02:07
  • Thank you for answering my question. My ultimate goal is to make my LLVM PASS work in RUST code, so I plan to convert RUST code to IR and load my PASS with OPT, and then generate an executable from IR code loaded with my PASS. In C/C ++, this is easy, but I don't know how to do this correctly in Rust, so my problem is how to load my LLVM PASS in Rust. – jing haha May 26 '21 at 11:11
  • @Coder-256Thank you.Now I also think that after using rustc to convert rust code to IR, using the LLI and LLC tools provided by native llvm may cause problems because the llvm used by rust has changed. It's worth mentioning that I use - L to link the rust Library Directory, and the result is the same.ld -L/home/me/snap/rustup/common/rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib -o main main.o – jing haha May 28 '21 at 03:59

0 Answers0