David's Blog

Dynamic Linking in Rust's FFI: Understanding the Essentials

By David Li on Saturday, 24 November 2023 13:00:00 GMT

Dynamic Linking in Rust’s FFI: Understanding the Essentials

Rust’s Foreign Function Interface (FFI) allows Rust code to call functions defined in other programming languages, such as C or C++. One of the key features of Rust’s FFI is dynamic linking, which enables Rust to call functions from shared libraries at runtime. In this article, we will delve into the essentials of dynamic linking and how it works in Rust’s FFI.

What is Dynamic Linking?

Dynamic linking is a process of linking an executable program with a shared library or DLL (Dynamic Link Library) at runtime, instead of statically linking the library at compile time. In other words, dynamic linking allows the program to access functions and data in the shared library only when it is needed, rather than including the entire library in the program’s executable file.

Dynamic linking has several advantages over static linking, such as smaller executable size, efficient memory usage, and easier maintenance of the shared library. It also allows multiple programs to share the same library in memory, reducing the overall memory usage of the system.

How Dynamic Linking Works in Rust’s FFI?

Rust’s FFI relies on dynamic linking to call functions from shared libraries, such as C or C++ libraries. The process of dynamic linking in Rust’s FFI can be summarized in the following steps:

Step 1: Defining the External Function

The first step in using dynamic linking in Rust’s FFI is to define the external function that Rust will call from the shared library. This is done by using the extern keyword followed by the function signature, as shown below:

extern “C” { fn my_function(arg1: i32, arg2: f64) -> f64; } Here, extern “C” specifies the calling convention used by the function, which is the C calling convention. The function signature fn my_function(arg1: i32, arg2: f64) -> f64; specifies the name and argument types of the function that Rust will call from the shared library.

Step 2: Loading the Shared Library

The next step is to load the shared library at runtime using the dlopen function from the libc crate. The dlopen function takes the path to the shared library as an argument and returns a handle to the loaded library, as shown below:

use std::ffi::CString; use std::os::raw::c_void; use libc::{c_double, c_int, dlclose, dlopen, dlsym};

let path = CString::new(“/path/to/shared/library.so”).unwrap(); let handle = unsafe { dlopen(path.as_ptr(), libc::RTLD_NOW) }; Here, CString::new(“/path/to/shared/library.so”).unwrap() creates a C-style string from the path to the shared library. The dlopen function loads the shared library specified by the path and returns a handle to the loaded library. The RTLD_NOW flag specifies that all symbols in the library should be resolved immediately.

Step 3: Resolving the Function Symbol

Once the shared library is loaded, the next step is to resolve the function symbol using the dlsym function. The dlsym function takes the handle to the loaded library and the name of the symbol as arguments and returns a pointer to the symbol, as shown below:

let symbol_name = CString::new(“my_function”).unwrap(); let symbol = unsafe { let ptr = dlsym(handle, symbol_name.as_ptr()); std::mem::transmute::<*mut c_void, fn(c_int, c_double) -> c_double>(ptr) }; Here, CString::new(“my_function”).unwrap() creates a C-style string from the name of the function symbol. The dlsym function resolves the function symbol specified by the name and returns a pointer to the symbol. The std::mem::transmute function converts the pointer to the symbol into a Rust function pointer with the same signature as the external function.

Step 4: Calling the External Function

The final step is to call the external function using the Rust function pointer obtained from the dlsym function, as shown below:

let result = symbol(42, 3.14); Here, symbol(42, 3.14) calls the external function with the arguments 42 and 3.14 and returns the result.

Step 5: Unloading the Shared Library

Once the external function is called, it is important to unload the shared library using the dlclose function to free the memory used by the library, as shown below:

unsafe { dlclose(handle); } Here, dlclose(handle) unloads the shared library specified by the handle.

Conclusion

Dynamic linking is a powerful feature of Rust’s FFI that allows Rust code to call functions from shared

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-22.