C Programming

Dlsym() 3 C Function

“Linux has come up with many of its unique functions to perform routine tasks. The dlsym() function is one of them. The dlsym() function goal is to find the address of a defined symbol specified in a DLL (Dynamic Link Library) that has been made accessible via a dlopen() function call. After loading the dynamic shared object (common link library) file indicated by the null-terminated string filename, the function dlopen() outputs an anonymous “connection” for the loaded object. The named symbol is looked up in the dynamic link library (DLL) that has been fetched by the dlopen() method. In this article, we will talk about the use of dlsym().

If the desired symbol is not present in that DLL, the dependent DLLs of that DLL will be searched for it, after by any dependencies of those, and so on in a breadth-first fashion until the desired symbol is found or all the DLLs have been searched for this purpose. Although the sequence in which dependent DLLs at the same level are searched is undetermined, this search order determines how duplication symbols in distinct DLLs will be identified. Be aware that unloaded dependent dynamic libraries won’t be loaded as a consequence of a dlsym() search for dependent DLLs. Only the DLLs that were loaded as a component of the dlopen() call’s dependent DLLs will be scanned.”

RTLD_NOW: If the entire collection of dependent DLLs must be made available to subsequent calls to the dlsym() function, this flag is used to invoke dynamic link libraries.

RTLD_LAZY: When we use this flag, we are unable to find which dependent Dynamic Link Libraries are loaded is not known.

RTLD GLOBAL: Newly loaded libraries may use the symbols specified by this package for symbol determination.

RTLD_LOCAL: Since neither flag is mentioned, this is the opposite of RTLD GLOBAL and the default.

Here is the syntax of dlsym() command:

#include <dlfcn.h>

# void *dlsym(void *__restrict__ handler, const char *__restrict__ symbol_name);

The header file where the dlsym() function is defined is <dlfcn.h>. “symbol_name” is the character string that is a representation of the symbol’s name, and “handler” is the value obtained by a call to dlopen() (that has not yet been returned by request to dlclose()).

The NULL value is returned, If the handle does not point to an appropriate DLL that was opened by dlopen(). The NULL value is returned by the dlsym() function if no DLL linked with the handler matches the named symbol (symbol name).

Create the dlsym.c file using VIM editor or any editor of your choice, which may include nano etc.

Write the code given in the below screen to the C file created in the previous step. The header files are included in the first four lines of code in the example code below. Following that, the main function is launched, and within the main function, a handler pointer of the void type is created. The preceding line of code declares a character-encoded pointer of void type.

Using that, we invoked the dlopen method and supplied the RTLD LAZY flag along with the location of the .so file (which is a dynamic shared file in Linux). The handler variable will keep the results of dlopen. By returning 1, the next line determines whether the dlopen successfully loaded the dynamic link library. In the event of a failure, the error has been written on the screen, and the main function has been ended.

After the conditional expression, we now call the dlerror() method to fix the existing error. As soon as we add 1 integer ASCII to the encoded symbol of the character type that we have defined, it will have the value “puts.” The length of the string was calculated in the next line and saved in the encoded length variable, which was then employed in the for loop used for decoding. We use the ending symbol “0” to end the code after the for loop. Following that, we executed the dlsym() method, passing the handler and decoded code parameters, and the output variable included the results.

When invoking the dlsym() function to compile a C file, the compilation process in a C compiler differs from a standard compilation command. You would create the program using the following command if it were in a file called “dlsym.c”:

$ gcc -rdynamic -o foo dlsym.c -ldl

After a successful compilation, the output file is created and given the name dlsym.out. Simply enter the command./dlsym.out and hit the enter key to run the output file. The file’s output will be displayed on the terminal screen in an operating system similar to UNIX or Linux.

Using the vim editor, we have made a small adjustment to the C file’s last lines of codes. The print statement at the end of the main function must be substituted with the output, which has been transformed into the puts function. In the screen’s double quote marks, the text has been added.

Now compile the code again and see the output on the terminal screen; without using the print statement, we get the output on the screen using our output value which is changed to the puts function. Here is the output of this particular code file shown above.

Conclusion

Within the introductory paragraph of this article, we have discussed the use of the dlsym() 3 c function to find out the address of some specified defined symbol in DLL. We have discussed and elaborated two clear examples of C in the Kali Linux operating system for this. Both the examples are very simple yet very efficient simultaneously and demonstrate the usage of the dlsym() function very clearly for better understanding.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.