General
cancel
Showing results for 
Search instead for 
Did you mean: 

An __builtin_return_address problem?

SOLVED
Go to solution
Wkdunreal
Advisor

An __builtin_return_address problem?

Hi,

Am using the native compiler (aCC) to compile.
In my source code at one instance , there is a need for __builtin_return_address to give me the return address of the function.

Using gcc it gets compiled but using aCC I seem to be having a lil bit of problem.

Can someone guide me whether there is a function similar to __builtin_return_address which can give me the function's return address on aCC compiler.

__builtin_return_address(LEVEL)

if i pass LEVEL=1 i get the address of a function a step higher in the stack .

Am using the HP-US 11.31 ia64.

Thanks in advance to all the Stalwarts of this community :)
Regards
8 REPLIES
Dennis Handly
Acclaimed Contributor
Solution

Re: An __builtin_return_address problem?

>whether there is a function similar to __builtin_return_address

Go through the aC++ documentation:
http://www.hp.com/go/cpp
Inline assembly for Itanium-based HP-UX
http://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801/?ciid=4308e2f5bde02110e2f5bde02110275d6e10RCRD
Table 1-7 Miscellaneous Opcodes
Table 1-40 Miscellaneous Opcodes
#include
_Asm_get_rp(void);

>if I pass LEVEL=1 I get the address of a function a step higher in the stack.

Chances are, not even GNU can do any levels higher than 0, except for every simple architectures, like HP 3000.

You'll need to use libunwind to get higher than 0. Your previous thread:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1369744
Wkdunreal
Advisor

Re: An __builtin_return_address problem?

BINGO!!!!

Thanks Dennis :D Thanks a million
The _Asm_get_rp(void); worked.

As soon as I run into other dead ends here will get back here :D.

Thanks again :):):)
Wkdunreal
Advisor

Re: An __builtin_return_address problem?

Hello again :)
I seem to have bumped into a problem using _Asm_get_rp().

I created a test shared object file lib.so

#include
#include
#include

void mycall()
{
void *p=_Asm_get_rp();
printf("%lx\n",p);
Dl_info info;
dladdr(p,&info);
printf("%s : %s\n",info.dli_sname,info.dli_fname);
}

And i created a try.c that would use this lib.so

#include
#include
#include
void Iwillallocate();
void func();
main()
{
func();
}

void func(){
Iwillallocate();
}

void Iwillallocate(){
mycall();
}
--------------------------------------------

I am compiling this using aCC
Created .so using aCC and all object files.

Te problem that i am facing is...
The return function name is coming out as
main()
instead of
Iwillallocate()


Could you please tell me what might I be doing wrong?

Thanks a million
Regards
Dennis Handly
Acclaimed Contributor

Re: An __builtin_return_address problem?

>Could you please tell me what might I be doing wrong?

You are optimizing and that inlines everything:
$ aCC -AA +DD64 itrc_get_rp.c -g
$ a.out
4000000000001290
_Z13Iwillallocatev: ./a.out
$ aCC -AA +DD64 itrc_get_rp.c -g +O2 +Oinfo
procedure func: info #20023-D: Inlined call to Iwillallocate
procedure main: info #20023-D: Inlined call to func
$ a.out
4000000000000e80
main: ./a.out

>printf("%lx\n", p);

You should use %p for pointers.
Wkdunreal
Advisor

Re: An __builtin_return_address problem?

Thanks for reply Dennis.

Are you creating a .so file (a library which contains mycall() ) or areyou inclusing mycall() in itrc_get_rp.c itself?

Could you show the exact contents of your vitrc_get_rp.c file?

Cause when I do not create an SO and include all the code in one file itself, it seems to give the right answer...

The problem comes when i create a separate library and try to call functions from this library (as I had shown in the code above.. the lib.c is the library file and try.c is the caller file.)

Thanks and regards
Dennis Handly
Acclaimed Contributor

Re: An __builtin_return_address problem?

>Are you creating a .so file (a library which contains mycall()) or are you including mycall() in itrc_get_rp.c itself?

The latter, the former is too hard. ;-)

>Could you show the exact contents of your vitrc_get_rp.c file?

All of your source in one file.

>when I do not create an SO and include all the code in one file itself, it seems to give the right answer.

Not when I optimize.

>The problem comes when i create a separate library and try to call functions from this library

That only fails when I optimize the executable.
You can add +d to stop inlining.
Wkdunreal
Advisor

Re: An __builtin_return_address problem?

:D

I suppose the former is indeed hard, sadly that is the only way I can proceed .... by making a library file....

When using gcc I never had these issues...
aCC seems a bit grumpy :|:|

I tried using the +d option as you had suggested... removing the inlining optimization... but then again when trying to use the "library creation method and calling the library function in a caller file" the +d gave the same result always pointing to main....

I really wonder what might be causing this problem ... Is it some other optimization that Is doing this... or maybe the way the .so file is made or called?

Very pinching indeed :|

Thanks a lot Dennis :D:D
Dennis Handly
Acclaimed Contributor

Re: An __builtin_return_address problem?

>by making a library file.

I couldn't duplicate it when I made a shlib.

>When using gcc I never had these issues...
aCC seems a bit grumpy

This likely nothing to do with aC++ if +d doesn't fix it.

>I really wonder what might be causing this problem

You should also use gdb and set a breakpoint where you call _Asm_get_rp and see what it thinks is the caller.

It could be dladdr(3) is broken.