How to extract information from a virtual table from a shared library?

I am using a performance analysis tool. One thing I do is to estimate the cost of calling a function. To do this, I need to know whether this function is virtual in the shared library.

To do this, I have access to the shared library assembly. I also have a schedule of execution calls. I can’t do anything at runtime; analysis must be done after runtime using information that I can get from the call schedule and shared libraries.

The only way I thought was to extract the vtable from the library and see if my function is in the vtable, but I did not find a way to extract the vtable of the class from the assembly.

I tried

readelf -s -W lib.so | c++filt | grep vtable 

but this only gives me the address of a good vtable (at least I think that it is one), and this address will not lead to anything.

Shared library compiled with gcc 4.3.5

Does anyone know a way to get this table? Or at least someone knows a way to find out if a function is virtual in a shared library?

thanks a lot

+2
source share
2 answers

Finally, we found a way to do this. It was not that difficult. In our case, the addresses of virtual tables are in the .dynsym section of the ELF shared library file. And then the contents of the virtual table are available in the .rela.dyn section. Therefore, we need to find the address and size of each virtual table, and then just read the .rela.dyn section to find the functions.

Of course, this is absolutely not portable, but in our case this is not a problem.

+3
source

0000000000400e80 w O.rodata 0000000000000020 vtable for testing

I use the command "objdump -x a.out | C ++ filt" and get the output above, obviously the vtable stored in the read-only section, as expected. Thank you for your advice.

0
source

Source: https://habr.com/ru/post/1488232/


All Articles