Dlsym Function In C++

In this article, we will discuss the dlsym function in C++ with its syntax, return value, and examples.

What is the dlsym Function?

The address of a symbol declared inside an object made available by a dlopen call is obtained by the dlsym function. The name parameter will represent the character string of the symbol's name, whereas the handle argument is the value obtained from a prior call to dlopen that was not been released with dlclose. The dlsym method is used to find the address of a symbol in a shared object or dynamic library. It is a feature of the dynamic linker/loader (dyld on macOS, ld. On Linux, etc.), which allows dynamic linking. When the object referred to by the handle is loaded, the dlsym function loads all objects automatically and looks for the designated symbol.

  • When performing dlsym actions on the global symbol object, load ordering is employed. The dependency order symbol resolution mechanism, as detailed in dlopen, will be applied.
  • The flags RTLDDEFAULT and RTLDNEXT are set reserved for potential future use.
  • Syntax:

It has the following syntax:

Example

void* dlsym(void* handle, const char* symbol);

Return Value:

In this case, the dlsym will return a NULL value if the handle argument does not relate to a valid object opened by the dlopen function or if there is no object of the named symbol in any of the objects associated with the handle. More thorough diagnostic data will be accessible via the dlerror method.

Pseudocode:

Example

// 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 us take an example to illustrate the dlsym function in C++.

Example

#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 us take another example to illustrate the dlsym function in C++.

Example

#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:

Input Required

This code uses input(). Please provide values below: