member function pointer address in shared library
-
21-06-2021 - |
Question
The problem I am trying to solve is to make a list of function names, mapped to the corresponding member function pointers.
I was trying to obtain the function pointer directly from the .so file using the nm command, and then call them through the function pointer, but I could not do so successfully.
In the .so file:
0xd52=>DerivedModel::DoSomething()
0xd94=>DerivedModel::checkReference()
The offset is 0x42.
But at runtime:
0x804d26e DoSomething
0x804d29c checkReference
The offset is 0x2E.
The offset between the functions is different somehow. Why should it differ? I suppose the vtable is identical, so the offset should be the same.
Solution
The reason is nm returns virtual address of the symbol. Shared library is normally stores symbol tables, data section at different places (check ELF format). nm returns address of symbol table. However when you loads the library, loader look up into symbol table and loads data section for each function so address offset changes at run time.
OTHER TIPS
The following could be the index in the symbols table.
0xd52=>DerivedModel::DoSomething()
0xd94=>DerivedModel::checkReference()