I was reading the code of the bitcoin trezor MCU and found this:
(*(void (**)())(FLASH_APP_START + 4))();
By breaking down some things, I tried to analyze what this line meant:
( *(void (**)())(FLASH_APP_START + 4) ) ();
I can see that this is a function call with no arguments, due to the ()
at the end, and that the function is whatever
*(void (**)())(FLASH_APP_START + 4)
points to.
I know that FLASH_APP_START + 4
will resolve into something, so I just need to figure out what is this:
*(void (**)())
It is resolving to whatever void (**)()
points to. But what is void (**)()
? It looks like a casting to a function, maybe. But I'm not sure. Could you give me an example of what is this calling? Why would you need that?
The meaning of
(void (**)())
is: cast into pointer to pointer to function returning void. Thus, when you dereference it (*(void(**)())
), it's of type pointer to function returning void, and you can call it. The(FLASH_APP_START+4)
is a pointer into a function pointer table. If the type ofFLASH_APP_START
ischar*
, then the 2nd function in the list will be invoked, assuming 32 bit pointers. If the type ofFLASH_APP_START
isvoid*
, then 5th function in the table would be called.E.g. this code would invoke
fun2
on a machine with 32 bit pointers.If you need help decoding C types, cdecl.org is your friend.