Reputation: 1
I found the following procedure in dissasembly:
lwz r0, 0(r12)
stw r2, 0x14(r1)
mtctr r0
lwz r2, 4(r12)
bctr
What does this listing do? Is it AltiVec code?
What is the C-equivalent of this procedure?
Upvotes: -1
Views: 198
Reputation: 26785
That is a function call thunk. It is an island of code within one DLL that jumps to another DLL, saving the TOC and setting up the new one for the new function in the other DLL.
The caller in one DLL uses a bl
(branch & link) — not shown in your post — to reach the thunk, then the thunk saves the current TOC on the stack, loads the code pointer from the cross-dll entry as well as the new TOC.
On some PowerPC ABIs, a function pointer points to a few words of data, 2 or 3 words. The first word is a pointer to code, and the second word is a pointer to the data area for that code. (A third may be an environment pointer.) That data pointer must be in r2 when that function is called, and that is how the called function accesses its own global & static data, constants, etc..
The linker/loader fixes up these data entries to refer to the other DLLs.
Specifically, there is a function pointer (a pointer to data) in r12 when this code is executed.
The link register has already been set by the caller as part of reaching this thunk. The callee will return directly to the caller (who will immediately restore their own global data pointer from the stack).
Within a single DLL, the global pointer is properly set so it only has to be saved & restore when crossing DLL boundaries, or when making indirect function calls (because it could be crossing a DLL boundary).
There is no equivalent in C for this thunk itself, the closest is making a function call via a function pointer, which encompasses more than this thunk. In C, when the function call is direct but to a function in another DLL, under the covers, the compilation system will turn that into an indirect call via function pointer that is resolved dynamically during loading of DLLs.
Upvotes: 3