How can I determine the return address on stack?

前端 未结 6 2275
旧巷少年郎
旧巷少年郎 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:49

    To know where the return address is you need to know what the calling convention is. This will typically be set by the compiler and depends on the platform, but you can force it in platform specific ways, for example using __declspec(stdcall) on windows. An optimizing compiler may also invent its own calling convention for functions that don't have external scope.

    Barring the use of compiler built-ins to get the return address you would have to resort to inline assembler to get the value. Other techniques that appear to work in debug would be very vunerable to compiler optimizations messing them up.

提交回复
热议问题