How To dynamically execute LLVM IR code compiled from Rust using lli from LLVM?

I wrote a simple demo to test LLVM's lli
main.rs

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

config.toml

[unstable]
build-std =  ["std"]

Run the ‘RUSTFLAGS="--emit=llvm-ir" cargo build --release --target aarch64-apple-darwin ’command ,I got a lot of.ll files。
I tried to use llvm-link to link these.ll files (including std, core libraries, etc.) into one big.ll, like this:

llvm-link compiler_builtins-59cc49c2563d3e72.ll core-97f2d427dc9bdd3f.ll libc-ba232a479edf88b9.ll memchr-4bb0b939948ca089.ll std-244b20392450c079.ll rustc_std_workspace_core-6e5894cfbd0ecda9.ll alloc-fb6cb25c658d83ce.ll cfg_if-95a393895607d0a6.ll adler-ce0dcf70ed5901e2.ll rustc_demangle-45a9e7b38e140734.ll unwind-48d02b28a3ac365d.ll rustc_std_workspace_alloc-449750b875fea1a6.ll  panic_unwind-80c06602c7d941b0.ll gimli-beebdcfb329c0d8f.ll miniz_oxide-638dcd9ffe0164d0.ll std_detect-e10104f414f4e2b6.ll object-3900acc309910275.ll hashbrown-3a66e4def54bffa7.ll addr2line-1bc538c47ff78c6e.ll proc_macro-82b50ae392622964.ll main_ts-070006806ba705cf.ll -o ../output.bc

Then I use lli to execute the resulting large.ll(output.bc) file

lli ../output.bc > log.txt 2>&1

But I got a lot of misinformation

JIT session error: Symbols not found: [ ___rust_alloc_error_handler_should_panic, ___emutls_get_address, ___rust_alloc, ___rust_dealloc, ___rust_alloc_zeroed, ___rust_alloc_error_handler, ___rust_no_alloc_shim_is_unstable, ___rust_realloc ]
lli: Failed to materialize symbols: { (main, { __ZN5alloc3ffi5c_str7CString16into_boxed_c_str17hf701029c2b631ad1E, __ZN4core3fmt3num3imp55_$LT$impl$u20$core..fmt..LowerExp$u20$for$u20$isize$GT$3fmt17h93b2873839d73d13E, __ZN64_$LT$std..sync..condvar..Condvar$u20$as$u20$core..fmt..Debug$GT$3fmt17hf3480f2ac7abb799E, __ZN5adler7Adler3211write_slice17h85628f616cddafd0E, __ZN6object4read2pe6import20DelayLoadImportTable4name17h128290efe5f5d42cE, __ZN4core3fmt3num54_$LT$impl$u20$core..fmt..UpperHex$u20$for$u20$u128$GT$3fmt17hf235305925b6133fE, __ZN5alloc6string13FromUtf8Error8as_bytes17h9b310d385813c0c8E, __ZN58_$LT$std..sync..once..Once$u20$as$u20$core..fmt..Debug$GT$3fmt17he923dd6bc8b698d6E, __ZN5alloc6string5Drain6as_str17h9e6b152f03fb6cdaE, __ZN4core3fmt3num3imp55_$LT$impl$u20$core..fmt..LowerExp$u20$for$u20$usize$GT$3fmt17hc3b1a38cb941f36eE, __ZN5gimli4read6abbrev12Abbreviation3new17h526c09e7e4f54202E, __ZN63_$LT$std..sync..once..OnceState$u20$as$u20$core..fmt..Debug$GT$3fmt17hebe9b35c64184198E, __ZN14rustc_demangle8demangle17h419c7ebbc75c0aaeE, __ZN4core3fmt3num3imp55_$LT$impl$u20$core..fmt..UpperExp$u20$for$u20$isize$GT$3fmt17hdfcdb30a2ddd056dE, __ZN14rustc_demangle12try_demangle17h386949c91189b441E, __ZN4core3fmt3num3imp54_$LT$impl$u20$core..fmt..Display$u20$for$u20$isize$GT$3fmt17h94ccd4e5b6fa68e0E, __ZN5alloc6string6String12from_utf16le17ha8bd915478f54c40E, __ZN60_$LT$std..time..Instant$u20$as$u20$core..ops..arith..Sub$GT$3sub17h3b6ae82ba64cb049E, __ZN65_$LT$std..time..SystemTimeError$u20$as$u20$core..fmt..Display$GT$3fmt17h49d67ca8ac23f9b5E, __ZN5alloc6string6String12from_utf16be17h71cf0443b080b563E, __ZN55_$LT$std..time..Instant$u20$as$u20$core..fmt..Debug$GT$3fmt17h19bf7efbdb624d88E, ___emutls_v._ZN10proc_macro6bridge6server27ALREADY_RUNNING_SAME_THREAD7__getit5STATE17hdb6de83fd70f787bE, __ZN3std10sys_common4wtf87Wtf8Buf25push_code_point_unchecked17h05badccdc1d31042E, __ZN5alloc6string6String10from_utf1617hb3c70f1ac2cd781aE, __ZN4core3fmt3num54_$LT$impl$u20$core..fmt..LowerHex$u20$for$u20$u128$GT$3fmt17he2d6b7f35231162dE, __ZN5alloc6string6String17try_reserve_exact17h3fc4221c1ebeffd1E, __ZN64_$LT$std..sys_common..wtf8..Wtf8$u20$as$u20$core..fmt..Debug$GT$3fmt17h2763da414f160274E, __ZN5alloc6string6String12insert_bytes17h8fa8581ef791d053E, ......

Is there something wrong with my move?

The allocator shim is not included in the --emit llvm-bc output. It also looks like you aren't passing the bitcode of all dependencies to lli. Something you could try would be to use -Clinker-plugin-lto and the pass a program to the -Clinker argument which collects and saves bitcode files based on the arguments it gets and then pass all saved bitcode files to lli. Note that at least part of the bitcode will be wrapped in object files. I don't know if lli can directly extract it or if you need to manually extract the bitcode section from those object files.

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.