How to call a assembly function (included in library crate) from a binary crate?

How to call a assembly function (included in library crate) from a binary crate ?

boot.S

.globl __boot_; /* To be visible to linker for entry */

__boot_:
    csrw mie, zero;

lib.rs

#![no_std]
#![no_main]
use core::arch::global_asm;
global_asm!(include_str!("boot.S"));

nm output

riscv64-unknown-elf-nm target/riscv32imac-unknown-none-elf/debug/liblibs.rlib 

lib.rmeta:
riscv64-unknown-elf-nm: lib.rmeta: no symbols

libs-49cd470ca24674d0.17nxpthld8fimcwx.rcgu.o:
00000000 N __boot_
         U __stack_start_

binary crate Cargo.toml

[package]
name = "execs"
version = "0.1.0"
edition = "2021"


[dependencies]
libs = {path = "../libs" }

calling __boot_ from main.rs (binary crate)

fn test1 () {

    unsafe {
        libs::__boot_();
    }

}

Cargo build throws **error**.
 libs::__boot_() => Here I tried calling libs::__boot_; & libs::__boot_() as well. 
   |               ^^^^^^^ not found in `libs`
 libs::__boot_();
   |               ^^^^^^^ not found in `libs`

what am I missing? Clearly the rlib has the _boot from the nm command. Anything to be done to export the symbol from library crate?

Rust expects libs to have a function _boot_, but will apply rust name mangling to the name. Try adding extern "c" fn __boot_(); to lib.rs to tell rust that the library contains that function without a mangled name.

1 Like

love it. Rust forum never disappoints me. Thanks ! it should be extern and public to access it.

extern "C" {
 pub fn __boot_();
}

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.