public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* how --builtin--return--address() works??
@ 2008-09-15  7:25 RANJITH KUMAR KASUKURTHI
  2008-09-15  9:49 ` Andrew Haley
  0 siblings, 1 reply; 3+ messages in thread
From: RANJITH KUMAR KASUKURTHI @ 2008-09-15  7:25 UTC (permalink / raw)
  To: gcc-help

Hi all,
              I know that __builtin_return_address() function returns the
return address of the current function when zero is passed
as an argument.

1) In which header file  __builtin_return_address() function is defined?
2) Return address is stored on stack when a function is called. I think
__builtin_return_address()
  is reading that value on stack and returning. Am I right?

If so
3) The location where that return address is stored on stack may vary 
depending upon  number  of arguments  passed to the function, or number of
local variables inside the function, or whether the function is returning
a value or not........

How __builtin_return_address() function knows where exactly the return
address is stored on stack????

Thanks in advance.

P.S. : My machine is pentium 4 processor and operating system is linux and
the compiler is gcc.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how --builtin--return--address() works??
  2008-09-15  7:25 how --builtin--return--address() works?? RANJITH KUMAR KASUKURTHI
@ 2008-09-15  9:49 ` Andrew Haley
  2008-09-15 12:09   ` John Fine
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Haley @ 2008-09-15  9:49 UTC (permalink / raw)
  To: RANJITH KUMAR KASUKURTHI; +Cc: gcc-help

RANJITH KUMAR KASUKURTHI wrote:

>               I know that __builtin_return_address() function returns the
> return address of the current function when zero is passed
> as an argument.
> 
> 1) In which header file  __builtin_return_address() function is defined?

None.  It's a compiler builtin.

> 2) Return address is stored on stack when a function is called. I think
> __builtin_return_address()
>   is reading that value on stack and returning. Am I right?

It depends on the target; some targets pass return address in a
register.

> If so
> 3) The location where that return address is stored on stack may vary 
> depending upon  number  of arguments  passed to the function, or number of
> local variables inside the function, or whether the function is returning
> a value or not........

Not usually, no.  On those machines that pass the return address on the stack,
that address is on the top.

> How __builtin_return_address() function knows where exactly the return
> address is stored on stack????

The compiler knows where everything is: the return address is nothing
special.

Andrew.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how --builtin--return--address() works??
  2008-09-15  9:49 ` Andrew Haley
@ 2008-09-15 12:09   ` John Fine
  0 siblings, 0 replies; 3+ messages in thread
From: John Fine @ 2008-09-15 12:09 UTC (permalink / raw)
  To: gcc-help

I agree with Andrew's answer.  I just want to add a few details where I 
think it might have been misleading.

Andrew Haley wrote:
> RANJITH KUMAR KASUKURTHI wrote:
>
>   
>> 3) The location where that return address is stored on stack may vary 
>> depending upon  number  of arguments  passed to the function, or number of
>> local variables inside the function, or whether the function is returning
>> a value or not........
>>     
>
> Not usually, no.  On those machines that pass the return address on the stack,
> that address is on the top.
>   
 From the point of view of code running inside a function, the location 
of the return address typically does vary with the number of local 
variables.  It does not vary with the number of arguments.
>> How __builtin_return_address() function knows where exactly the return
>> address is stored on stack????
>>     
>
> The compiler knows where everything is: the return address is nothing
> special.
>
>   
Often the compiler can't know where "everything" (relevant to the 
current stack frame) is, because it often can't know how many arguments 
were passed.

But (as you indicated) it always knows where the return address is.  If 
it didn't, then it couldn't even generate correct code for the function 
to return.

Because of the flexibility the compiler has with register use, it 
typically would be impossible for a something like 
__builtin_return_address() to be fully defined by a header file.  The 
compiler can know all about the stack usage for saved registers and 
local variables, etc. so at any point in a function it knows where the 
return address is.  But code in a header file can't know that much.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-09-15 12:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-15  7:25 how --builtin--return--address() works?? RANJITH KUMAR KASUKURTHI
2008-09-15  9:49 ` Andrew Haley
2008-09-15 12:09   ` John Fine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).