What is symbol table and how is it integrated into the executable?

user198729 picture user198729 · Apr 5, 2010 · Viewed 32k times · Source

When I tried to debug an executable:

(gdb) break +1
No symbol table is loaded.  Use the "file" command.

What does that mean exactly?

Is the symbol table appended to the executable?

Answer

nategoose picture nategoose · Apr 5, 2010

There are two sets of symbols that gdb uses.

The -g set are debugging symbols, which make things a lot easier as they allow you to see your code and look at variables while debugging.

Another set of symbols is included by default when you compile. These are the linking symbols and live in the ELF (executable linkable format) symbol table. This contains a lot less info than the debug symbols, but contain the most important stuff, such as the addresses of the things in your executable (or library or object file). Without this information gdb won't even know where main is, so (gdb) break main would fail.

If you don't have the debugging symbols ( -g ) then you will still be able to (gdb) break main but you gdb will not have any concept of the lines of code in your source file. When you try to step through the code you will only advance 1 machine instruction at a time, rather than a line at a time.

The strip command is often used to strip off symbols from an executable (or other object file). This is often used if you don't want someone to be able to see the symbols or if you want to save space in the file. Symbol tables can get big. Strip removes both the debug symbols and the linker symbols, but it has several command line switches which can limit what it removes.

If you run the file command on your program one of the things it will tell you is weather or not the executable is has been stripped.

$ gcc my_prog.c -o my_prog
$ file my_prog
my_prog: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
$ strip my_prog
my_prog: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped
$