How to count the uses of undefined symbols in an object file?

52 Views Asked by At

Suppose I'm "forensically" examining an object file that somebody else compiled. I don't have the source, and can't rely on debug symbols either. Still, I can use:

objdump -C -t my_object.o \
| sed -r '/\\*UND\\*/!d; s/000+\s+\*UND\*\s+000+ //;' \
| sort

to get the external symbols it uses.

But what if what I'm after is the count of uses of each of these symbols? Is there a way to get that without, say, arduously parsing the disassembled code?

Notes:

  • A GNU system (e.g. Linux)
  • Assume basic tools used in C++ software development are installed.
1

There are 1 best solutions below

2
vitaut On

For functions you could look at the number of relocations, for example:

// test.cc:
void foo();

int main() {
  foo();
  foo();
}
$ c++ -c test.cc
$ objdump -r test.o | grep foo
0000000000000005 R_X86_64_PC32     _Z3foov-0x0000000000000004
000000000000000a R_X86_64_PC32     _Z3foov-0x0000000000000004

Here we have two relocations, each corresponding to one function call.