Click here to Skip to main content
16,022,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am trying to get the address of the caller function. Basically i am calling a noreturn function (lets say A) from main. In "A" i get the address of the caller using __builtin_return_address function. After getting the address i call another function "B" and in "B" i print the caller address.
C
void main() {
 A();
 return;
}

__noreturn void A(){
  void *caller = __builtin_return_address(0);
  B(caller);
}

void B(void *caller) {
 printf ("The caller address is %p",caller);
}
However, I am not able to get the correct caller address. My aim is to use addr2line utility and print the correct file name and the exact location from where "A" was invoked.

If i call the noreturn function A() from a wrapper. It gives me correct result.

C
void main() {
 wrapper();
 return;
}

void wrapper() {
   A();
}

__noreturn void A(){
  void *caller = __builtin_return_address(0);
  B(caller);
}

void B(void *caller) {
 printf ("The caller address is %p",caller);
}


Any help is appreciated.

Thanks

What I have tried:

Dumped the object code to debug and find the address which is printed. I see that the address which "__builtin_return_address" is some random address.

Tried calling __builtin_return_address with 1. However i get an error saying the output will be unpredictable.
Posted
Updated 2-Oct-24 21:59pm
v4
Comments
11917640 Member 30-Sep-24 1:49am    
With __noreturn optimization compiler is allowed to omit the code, that preserves the function return address. See also: https://answers.launchpad.net/gcc-arm-embedded/+question/404305 and https://stackoverflow.com/questions/8236959/what-are-sp-stack-and-lr-in-arm

You should feed the return address through __builtin_extract_address to get the actual address. See Return Address (Using the GNU Compiler Collection (GCC))[^]
 
Share this answer
 
Comments
Rahul VB 26-Sep-24 14:07pm    
Thanks for the feedback. Actually i just tried that as below, but i still get the same address:
__builtin_extract_return_addr((__builtin_return_address(0)));

I am thinking, could it be due to the "_noreturn" qualifier?
Pete O'Hanlon 27-Sep-24 3:24am    
_noreturn would definitely be an issue here. You should only use this qualifier when your function is using something like a signal or exit. As your function appears to return normally, you should remove this.
Rahul VB 27-Sep-24 6:18am    
I re-investigated every thing. The function needs to be "noreturn" because it signals a malfunction so it will exit and control wont come back to the caller and probably wont have a stack frame. Let me further debug.. thanks for your inputs
Rahul VB 27-Sep-24 7:20am    
Okay, so i digged a little more. Its due to the noreturn attribute.
After further investigation i found that if i remove the noreturn attribute from the function prototype i see that the return address from "__builtin_extract_address" is properly returned. When we mark a function as noreturn the compiler optimizes the object code and there is no stack frame of a no return function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900