I am asking this question to gather a knowledge base. I know in x86-64, a machine code function can have only one function return, i.e., ret
instruction. I know it can be compiler (GCC, clang, etc) and platform (MIPS, ARM, etc) specific. My question is, is it always to be true to have only one function return
in machine code for every platform and generated by any compiler?
Thanks in advance.
-
A function as such can have multiple returns like if(foo) {return blah} ; .elseif {} .else {}– blabbCommented Nov 25, 2023 at 8:10
1 Answer
As far as I remember ARM has at least 3 different possibilities to implement return from function functionality, and I had seen all of those in real code, allegedly generated by ordinary compiler.
The first pattern is push {list of registers, LR}
at the prologue of the function and then pop {same list of registers, PC}
at the end of the epilogue. It is used this way because call like instructions in ARM put the return address into LR register and not on stack, and if there is another function call inside the first one LR changes.
The second pattern is just BX LR
(or just B LR
) for functions without calls inside.
The third one is MOV PC,LR
.
It should be also taken in account, that ARM has 2 instruction sets, ARM and THUMB, and instructions from those 2 instruction sets may co-exists in the same executable. This gives us different encodings(and sometimes mnemonics) for all mentioned instructions, so technically all those are different instructions.
So the answer to you question is no, it is not always to be true to have only one function return in machine code for every platform and generated by any compiler.