Week 3: ELF
For these exercises, you need to be on a Linux machine in order to use objdump and readelf (on a Mac, otool provides similar functionality but with different command-line interface).
1. The different kinds of ELF objects
1.1 Relocatable file
A relocatable file is an object file that holds code and data suitable for linking with other object files to create an executable or a shared object file. In other word, you can say that relocatable file is a foundation for creating executables and libraries.
This is kind of file you get if you compile a source code like this:
$ gcc -c test.c
That will produce test.o, which is a relocatable file.
Kernel module (either suffixed with .o or .ko) is also a form of relocatable file.
1.2 Executable file
An executable file is an object file that holds a program suitable for execution. Yes, that means, your XMMS mp3 player, your vcd software player, even your text editor are all ELF executable files.
This is also a familiar file if you compile a program:
$ gcc -o test test.c
After you make sure the executable bit of "test" is enabled, you can execute it. The question is, what about shell script? A shell script is not an ELF executable, but the interpreter (e.g., bash) is.
1.3 Shared object file
A shared object file holds code and data suitable for linking in two contexts:
- The link editor may process it with other relocatable and shared shared object file to create another object file.
- The dynamic linker combines it with an executable file and other shared objects to create a process image. In simple words, these are the files that you usually see with suffix .so (normally located inside /usr/lib on most Unix installations).
2. Executable and Linkable Format (ELF) exercises
Select some binary to work with, e.g., your assignment1 or assignment2 programs, or any other program you have compiled. I will use main
from assignment2 in the examples below.
Display the file header
$ readelf -Wh /lib64/ld-linux-x86-64.so.2 $ readelf -Wh main
$ objdump -f main
Display information related to all the sections
An object file contains various sections. To retrieve information related to all the sections, supply the -s option.
$ objdump -s main
Display a particular section
$ objdump -s -j.rodata main
Show source with disasembly
$ objdump -S -j .text programname
Show all symbols defined in a library
$ objdump -T libname.so | grep text
3. Libraries
To create a library of object files:
$ ar rvs libname.a file1.o file2.o ...
To see the contents of an archive:
$ ar -t libvec.a
To see all the library dependencies of an executable (e.g., /bin/ls) that uses shared libraries
$ ldd /bin/ls