How can I determine the return address on stack?

前端 未结 6 2268
旧巷少年郎
旧巷少年郎 2020-12-28 17:29

I know that if I am inside some function foo() which is called somewhere from bar() function, then this return address is pushed on stack.

6条回答
  •  天命终不由人
    2020-12-28 17:54

    There is a gcc builtin for this: void * __builtin_return_address (unsigned int level)

    See http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html

    On some architectures, you can find it on the stack relative to the first parameter. On ia32, for example, the parameters are pushed (in opposite order) and then a call is made that will push the return address. Remember that the stack almost always (and on ia32) grows downward. Although technically you need the ABI or calling conventions (sometimes called linkage conventions) for your language and hardware platform, in practice you can usually guess if you know how the procedure call machine op works.

    The relationship between the first parameter to a function and the position of the return address on the stack is far more likely to be a reliably fixed value than the relationship between a local and the return address. However, you can certainly print out the address of a local, and of the first parameter, and you will often find the PC right in between.

    $ expand < ra.c
    #include 
    
    int main(int ac, char **av) {
      printf("%p\n", __builtin_return_address(0));
      return 0;
    }
    $ cc -Wall ra.c; ./a.out
    0xb7e09775
    $ 
    

提交回复
热议问题