In this guide, we will explore the dlsym function in C++ including its syntax, output, and sample illustrations.
What is the dlsym Function?
The dlsym function is responsible for retrieving the address of a symbol declared within an object that has been accessed through a dlopen call. It requires two parameters: the symbol's name provided as a character string in the name parameter, and the handle argument derived from a previous dlopen invocation that has not been closed with dlclose. This method is specifically designed to locate the symbol's address within a shared object or dynamic library. It is an integral part of the dynamic linker/loader (dyld on macOS, ld on Linux, etc.) that facilitates dynamic linking. Upon loading the object referenced by the handle, dlsym automatically loads all associated objects and searches for the specified symbol.
- When utilizing dlsym to interact with the global symbol object, the process follows load ordering. This involves the application of the dependency order symbol resolution mechanism, which aligns with the principles outlined in dlopen.
- The constants RTLDDEFAULT and RTLDNEXT are currently reserved for potential future applications.
Syntax:
It has the following syntax:
void* dlsym(void* handle, const char* symbol);
Return Value:
In this scenario, if the handle parameter provided to dlsym does not correspond to a valid object that was opened using the dlopen function, or if there is no object containing the specified symbol within the objects linked to the handle, then dlsym will yield a NULL value. Additional detailed diagnostic information can be obtained by utilizing the dlerror function.
Pseudocode:
// Pseudocode for using the dlsym() function to get a function pointer from a dynamic library
// Now, open the dynamic library
handle = dlopen("libexample.so", RTLD_LAZY)
if the handle is NULL
Print "Error loading library: " + dlerror()
Exit program
// Here, we get a pointer to the function
function_pointer = dlsym(handle, "some_function")
if function_pointer is NULL
Print "Error getting symbol: " + dlerror()
Close the library handle
Exit program
// Call the function
function_pointer()
// Close the library handle
dlclose(handle)
Program 1:
Let's consider a scenario to demonstrate the dlsym function in C++.
#include <dlfcn.h>
#include <iostream>
int main() {
void* handle = dlopen("libex.so", RTLD_LAZY);
if (!handle) {
std::cerr << "Error loading library: " << dlerror() << std::endl;
return 1;
}
// Here, we get a pointer to the symbol named "some_function" from the loaded library
void (*some_function)() = (void (*)())dlsym(handle, "some_function");
if (!some_function) {
std::cerr << "Error getting symbol: " << dlerror() << std::endl;
dlclose(handle);
return 1;
}
// Call the function
some_function();
dlclose(handle);
return 0;
}
Output:
Program 2:
Let's consider another instance to demonstrate the dlsym function in C++.
#include <dlfcn.h>
#include <iostream>
typedef void (*FuncPtr)();
int main() {
void* handle = dlopen("./libex.so", RTLD_LAZY);
if (!handle) {
std::cerr << "Error loading library: " << dlerror() << std::endl;
return 1;
}
// Here, we get a pointer to the function named "some_function" from the loaded library
FuncPtr some_function = (FuncPtr)dlsym(handle, "some_function");
if (!some_function) {
std::cerr << "Error getting symbol: " << dlerror() << std::endl;
dlclose(handle);
return 1;
}
// Call the function
some_function();
dlclose(handle);
return 0;
}
Output: