[GD-Linux] Determining address of C++ member function at run-time...
                
                Brought to you by:
                
                    vexxed72
                    
                
            
            
        
        
        
    | 
      
      
      From: Colin F. <cp...@ea...> - 2001-12-12 07:27:21
      
     | 
| December 11, 4004 B.C.
Tuesday
I have a Linux shared-library (*.so) file with lots
of C++ classes.  I would like to determine the addresses
of the binary code implementations of various 
NON-STATIC member functions at run-time (not at
compile-time or doing "nm foo.so" at the command line).
For example, let's say I have:
class Foo
{
public:
  int Bar( int, char *, float );
};
I'd like to do something like:
printf( "%p", &(Foo::Bar) );
The problem is, this print statement gives me
a wacky answer (like 0xffff0000, for example).
I know that pointer-to-member-function is
more than a simple C pointer -- since it must
have information regarding virtual function
tables, etc.  I also know that there is an
implicit "this" parameter to all non-static
member functions:
int Foo::Bar( [Foo *this,] int, char *, float );
               IMPLICIT
Perhaps this is not always the case, especially
if the member function only accesses static
member functions and variables -- in which case
the "this" parameter is superfluous.
Anyhow, I want a 32-bit address of the 
assembly language implementation of Foo::Bar()
without instantiating a Foo class object,
and without calling Foo::Bar().
*** I don't mind any assembly language "hack",
or any non-portable, frown-upon trickery. ***
Ideally the technique would be "self-contained";
i.e., something I could do with a few lines of
code or assembly code, without changing any
external source files.  For example, I *don't*
want to modify the source code of the C++
classes themselves!  Oh, and the technique must
do calculations at run-time, and must work
even for re-located shared-library code.
One idea I had for this problem is this:
write code to call the method, but never
execute it, and then search backwards for
the "call" instruction, and get the address.
That leads me to another question:  How can
I get the run-time address of C++ statements?
For example, suppose I do this:
     
     goto  my_label;
    
     my_label:
       printf("Happy day, citizen!\n");
Could I figure out the approximate address of
the "printf" call (NOT the location of the 
implementation of printf(), but the local
"line" of code where the "call printf"
assembly language is) just by doing the
following:
    printf( "%x", (unsigned int)(my_label) );
or something similar?
Am I allowed to read any code pages just like
memory allocated on the heap?  I mean *MY* code
pages.  So is it possible to scan through the
assembly language opcodes of my own program
to search for code?
Okay, that's a lot of material -- and probably
too hard-core for this list -- but I think a
few of you guys have the expertise to help me
with this puzzle.  I did lots of Google searching,
but everyone is happy making statements like
"just declare a pointer-to-member-function to
implement callbacks...C++ is great...who cares
about the secret mechanics!"
--- Colin P. Fahey
 |