summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/return-address.html
blob: 43d5a5aad85c66ff6f22c948e14164aeedb14a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<div class="section-level-extent" id="Return-Address"> <div class="nav-panel"> <p> Next: <a href="vector-extensions" accesskey="n" rel="next">Using Vector Instructions through Built-in Functions</a>, Previous: <a href="function-names" accesskey="p" rel="prev">Function Names as Strings</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="section" id="Getting-the-Return-or-Frame-Address-of-a-Function"><span>6.51 Getting the Return or Frame Address of a Function<a class="copiable-link" href="#Getting-the-Return-or-Frame-Address-of-a-Function"> ¶</a></span></h1> <p>These functions may be used to get information about the callers of a function. </p> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005freturn_005faddress">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void *</code> <strong class="def-name">__builtin_return_address</strong> <code class="def-code-arguments">(unsigned int <var class="var">level</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005freturn_005faddress"> ¶</a></span>
</dt> <dd>
<p>This function returns the return address of the current function, or of one of its callers. The <var class="var">level</var> argument is number of frames to scan up the call stack. A value of <code class="code">0</code> yields the return address of the current function, a value of <code class="code">1</code> yields the return address of the caller of the current function, and so forth. When inlining the expected behavior is that the function returns the address of the function that is returned to. To work around this behavior use the <code class="code">noinline</code> function attribute. </p> <p>The <var class="var">level</var> argument must be a constant integer. </p> <p>On some machines it may be impossible to determine the return address of any function other than the current one; in such cases, or when the top of the stack has been reached, this function returns an unspecified value. In addition, <code class="code">__builtin_frame_address</code> may be used to determine if the top of the stack has been reached. </p> <p>Additional post-processing of the returned value may be needed, see <code class="code">__builtin_extract_return_addr</code>. </p> <p>The stored representation of the return address in memory may be different from the address returned by <code class="code">__builtin_return_address</code>. For example, on AArch64 the stored address may be mangled with return address signing whereas the address returned by <code class="code">__builtin_return_address</code> is not. </p> <p>Calling this function with a nonzero argument can have unpredictable effects, including crashing the calling program. As a result, calls that are considered unsafe are diagnosed when the <samp class="option">-Wframe-address</samp> option is in effect. Such calls should only be made in debugging situations. </p> <p>On targets where code addresses are representable as <code class="code">void *</code>, </p>
<div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void *addr = __builtin_extract_return_addr (__builtin_return_address (0));</pre>
</div> <p>gives the code address where the current function would return. For example, such an address may be used with <code class="code">dladdr</code> or other interfaces that work with code addresses. </p>
</dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fextract_005freturn_005faddr">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void *</code> <strong class="def-name">__builtin_extract_return_addr</strong> <code class="def-code-arguments">(void *<var class="var">addr</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fextract_005freturn_005faddr"> ¶</a></span>
</dt> <dd>
<p>The address as returned by <code class="code">__builtin_return_address</code> may have to be fed through this function to get the actual encoded address. For example, on the 31-bit S/390 platform the highest bit has to be masked out, or on SPARC platforms an offset has to be added for the true next instruction to be executed. </p> <p>If no fixup is needed, this function simply passes through <var class="var">addr</var>. </p>
</dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005ffrob_005freturn_005faddr">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void *</code> <strong class="def-name">__builtin_frob_return_addr</strong> <code class="def-code-arguments">(void *<var class="var">addr</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005ffrob_005freturn_005faddr"> ¶</a></span>
</dt> <dd><p>This function does the reverse of <code class="code">__builtin_extract_return_addr</code>. </p></dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fframe_005faddress">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void *</code> <strong class="def-name">__builtin_frame_address</strong> <code class="def-code-arguments">(unsigned int <var class="var">level</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fframe_005faddress"> ¶</a></span>
</dt> <dd>
<p>This function is similar to <code class="code">__builtin_return_address</code>, but it returns the address of the function frame rather than the return address of the function. Calling <code class="code">__builtin_frame_address</code> with a value of <code class="code">0</code> yields the frame address of the current function, a value of <code class="code">1</code> yields the frame address of the caller of the current function, and so forth. </p> <p>The frame is the area on the stack that holds local variables and saved registers. The frame address is normally the address of the first word pushed on to the stack by the function. However, the exact definition depends upon the processor and the calling convention. If the processor has a dedicated frame pointer register, and the function has a frame, then <code class="code">__builtin_frame_address</code> returns the value of the frame pointer register. </p> <p>On some machines it may be impossible to determine the frame address of any function other than the current one; in such cases, or when the top of the stack has been reached, this function returns <code class="code">0</code> if the first frame pointer is properly initialized by the startup code. </p> <p>Calling this function with a nonzero argument can have unpredictable effects, including crashing the calling program. As a result, calls that are considered unsafe are diagnosed when the <samp class="option">-Wframe-address</samp> option is in effect. Such calls should only be made in debugging situations. </p>
</dd>
</dl> </div>  <div class="nav-panel"> <p> Next: <a href="vector-extensions">Using Vector Instructions through Built-in Functions</a>, Previous: <a href="function-names">Function Names as Strings</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Return-Address.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Return-Address.html</a>
  </p>
</div>