March 12, 2018

In this article I would like to mention about the dynamic linker on UNIX-GNU/Linux systems. So, what is the dynamic linker? The dynamic linker is a run-time linker to allow its caller process to map its shared objects (.so files) to own virtual memory space, when the process file (ELF) is linked as shared. This is very important to reduce executable memory foot-print especially when we have very limited amount of memory. Ok, how can we understand an ELF file linked as shared or static. The answer is, by using readelf tool. This tool is so useful than you expected.

When we check the binary with readelf command, we will see the output below,

$ readelf -l main

Now compile the code as statically linked,

$ gcc -o main main.c -Wall -static

Last but not least, I’ll conduct a test to what is going to happen when executable binary is not able to locate dynamic linker.

$ gcc -o main main.c -Wall -Wl,--dynamic-linker=/lib/noname

As we can see the terminal output, we got ‘No such file or directory‘ error although we have the binary main located in the working directory.
This happened because once execv system call got executed, it attempts to invoke dynamic linker (in this case /lib/noname) through the executable file main. We can see its detail as below.

I wanted to share an important detail (at least for me) regarding ELF files in this article. In the past, one of the wrong configured dynamic linker file made me spent a lot of time and effort and I thought this would be a useful hint for your projects.