0

I'm just experimenting with some JIT compilation, using the asmjit library. Specifically, I want to call a member function of a class instance using the address of that instance as an argument for the jitted function.

I understand that I have to use the address as the first parameter of the object (given a member function without any arguments, the address will be the only parameter). For that reason, I mov the address into rdi. The problem is, that I do not know how the compiler (clang-12 in my case) named the function I want to call.

Calling global functions (or static ones) seems simple by getting the function pointer and calling that one (tried that successfully), but what about (non-static) member functions? Looks like clang does not name those functions regularly. I found that the compiler uses addresses to call those functions like call 407180 <_ZNK3Foo5printEv> (where Foo::print() is the function I want to call, located at 407180). Is there any way to call the function by name or get the function pointer to the class?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
jagemue
  • 363
  • 4
  • 16
  • 1
    For non-virtual functions you can figure out the mangled name. For virtual functions you have to go through the vtable. – Jester Sep 22 '21 at 14:39
  • 2
    To my knowledge C++ does not know i.e. specify something as an Application Binary Interface (ABI), which is what you seek, the ABI to call C++ member functions. This is dependend on your implementation in general, meaning your platform, like ARM or x86-64, Linux or Windows, and your compiler. So seems that you should look into the ABI of clang 12 of your plattform. – Superlokkus Sep 22 '21 at 14:40
  • The only other thing I think of would be if asmjit can handle that for you: https://asmjit.com/doc/group__asmjit__compiler.html – Superlokkus Sep 22 '21 at 14:42
  • The problem is not calling the function, the main issue is getting the address of the function to call. AsmJit even provides ThisCall calling convention for this use-case so all the builtins are there. Global functions are easy, non-virtual member functions may be possible with some compiler magic if you manage to get their addresses, but with virtual functions you would have to implement the dispatch the way compilers implement it for all the ABIs you want to support - in this case I would recommend using compiler explorer to get an idea. – Petr Sep 23 '21 at 08:24

0 Answers0