Something about dynamic lib – ldd,, nm, dlsym()

Dynamic lib may be the widely used mechanism for code reusing and memory saving, both for Windows and UNIX/Linux systems. To better understand the dynamic lib, we will try to fix the famous error during linking and investigate the symbols within the lib.

0. ldd

ldd cmd is used to display the necessary dynamic lib used by certain binary file.
root@arpsec01:~/tpm/rpc# ldd rpc => (0x00007ffff9bff000) => /usr/lib/ (0x00007fc6ec53e000) => /lib/x86_64-linux-gnu/ (0x00007fc6ec17f000) => /lib/x86_64-linux-gnu/ (0x00007fc6ebf61000) => /lib/x86_64-linux-gnu/ (0x00007fc6ebb86000)
/lib64/ (0x00007fc6ec7c4000) => /lib/x86_64-linux-gnu/ (0x00007fc6eb982000) => /lib/x86_64-linux-gnu/ (0x00007fc6eb76a000)

1. Error while loading shared libraries:

a. The dynamic lib is missing – install
b. The lib has been installed under /lib or /usr/lib – ldconfig
c. The lib has been installed under somewhere else, like /usr/local/lib –
# cat /etc/
echo “/usr/local/lib” >> /etc/
# ldconfig
d. The lib has been installed somewhere else and temporary linking is needed – LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

2. How could I know what (functions/variables) are contained by certain dynamic lib?

Generally speaking, there are 2 ways to do that.
a. nm -C -D path_to_a_dynamic_lib – nm is so powerful that it will list all the symbols contained by this dynamic lib, including functions (T) and variables. The most common case for nm would be ‘nm -C -D path_to_a_dynamic_lib | grep function_name’, which is used to determine if certain function is contained by this dynamic lib.
root@arpsec01:~# nm -C -D /usr/lib/ | grep RPC_OpenContext_TP
000000000001f9d0 T RPC_OpenContext_TP
b. dlsym() – actually a system call interfacing with ld. dlopen() is used to open certain dynamic lib and dlsym() then could be used to check if certain symbol is contained or not. Example code is below.
root@arpsec01:~/ld_blog# cat myld.c
* myld.c
* example source to use dl* functions
* to parse the content of a dynamic lib
* Usage: myld libname symbol
* Sep 2, 2013

#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char *argv[])
void *handle;
void *addr;

if (argc != 3)
printf(“Error: Missing parameters\n”);
return -1;

/* Get the handle of the lib */
handle = dlopen(argv[1], RTLD_NOW);
if (handle == NULL)
printf(“Error: dlopen %s [%s]\n”, argv[1], dlerror());
return -1;

/* Try the get the address of symbol */
addr = dlsym(handle, argv[2]);
if (addr == NULL)
printf(“Error: dlsym %s [%s]\n”, argv[2], dlerror());
return -1;

printf(“Symbol[%s]@0x%x\n”, argv[2], addr);

return 0;
root@arpsec01:~/ld_blog# gcc -o myld myld.c -ldl
myld.c: In function ‘main’:
myld.c:42:2: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘void *’ [-Wformat]
root@arpsec01:~/ld_blog# ./myld RPC_OpenContext_TP

3. Functions(symbols) are the memory address, so what?

root@arpsec01:~/ld_blog# cat myld2.c
* myld2.c
* Load the math library, and print the cosine of 2.0
* Reference: man 3 dlsym
* Sep 2, 2013

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

main(int argc, char **argv)
void *handle;
double (*cosine)(double);
char *error;

handle = dlopen(“”, RTLD_LAZY);
if (!handle) {
fprintf(stderr, “%s\n”, dlerror());

dlerror(); /* Clear any existing error */

/* Writing: cosine = (double (*)(double)) dlsym(handle, “cos”);
would seem more natural, but the C99 standard leaves
casting from “void *” to a function pointer undefined.
The assignment used below is the POSIX.1-2003 (Technical
Corrigendum 1) workaround; see the Rationale for the
POSIX specification of dlsym(). */

*(void **) (&cosine) = dlsym(handle, “cos”);

if ((error = dlerror()) != NULL) {
fprintf(stderr, “%s\n”, error);

printf(“%f\n”, (*cosine)(2.0));
root@arpsec01:~/ld_blog# gcc -o myld2 myld2.c -ldl
root@arpsec01:~/ld_blog# ./myld2

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in IDE_Make, Programming and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s