Assume I have a C object-file app.o
compiled with gcc. How can I dump the file path to the original app.c
from which app.o
was compiled. My goal is to create a listing of all symbols + respective source file path using the binutils and gcc toolsuite.
By no means am I expecting an all-in-one solution. So I tried playing with multiple tools to gather the information I need.
Inspecting the object-file with a text-editor reveals that (appart from a lot of unreadable binary gibberish) the file does contain a reference to app.c
as a string embedded into the object-file format. However I did not find a way to extract that string using objdump
or nm
.
I was hoping objdump
would have some flag that could extract this source file string, but after trying virtually all options documented in the man
page I still couldn't find it.
With the path of the source file I was hoping I could run gcc -M <path-to-source>
. This would allow me to look through all the headers included by app.c
and find the in-source declarations.
Suppose a simple app.c
like this:
void foo(void) {
}
Compile it via gcc -c app.c -o app.o
.
Running objdump -t app.o
dumps the symbol table, but does not refer anywhere to the original app.c
.
Running cat app.o
does show that the object-file contains the file path to app.c
(relative to pwd at compile-time). But I wasn't exactly planning on writing my own object-file parser just to get to that string.
To answer my own question minutes after posting it (duh!):
readelf -s app.o
prints a symbol table including the name of the source file (app.c
). With that I am able to run gcc -M app.c
and then parse through all header files to gather the symbol declarations.