summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/common-function-attributes.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/common-function-attributes.html
new repository
Diffstat (limited to 'devdocs/gcc~13/common-function-attributes.html')
-rw-r--r--devdocs/gcc~13/common-function-attributes.html405
1 files changed, 405 insertions, 0 deletions
diff --git a/devdocs/gcc~13/common-function-attributes.html b/devdocs/gcc~13/common-function-attributes.html
new file mode 100644
index 00000000..097ad354
--- /dev/null
+++ b/devdocs/gcc~13/common-function-attributes.html
@@ -0,0 +1,405 @@
+<div class="subsection-level-extent" id="Common-Function-Attributes"> <div class="nav-panel"> <p> Next: <a href="aarch64-function-attributes" accesskey="n" rel="next">AArch64 Function Attributes</a>, Up: <a href="function-attributes" accesskey="u" rel="up">Declaring Attributes of Functions</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="subsection" id="Common-Function-Attributes-1"><span>6.33.1 Common Function Attributes<a class="copiable-link" href="#Common-Function-Attributes-1"> ¶</a></span></h1> <p>The following attributes are supported on most targets. </p> <dl class="table"> <dt><code class="code">access (<var class="var">access-mode</var>, <var class="var">ref-index</var>)</code></dt> <dt><code class="code">access (<var class="var">access-mode</var>, <var class="var">ref-index</var>, <var class="var">size-index</var>)</code></dt> <dd> <p>The <code class="code">access</code> attribute enables the detection of invalid or unsafe accesses by functions to which they apply or their callers, as well as write-only accesses to objects that are never read from. Such accesses may be diagnosed by warnings such as <samp class="option">-Wstringop-overflow</samp>, <samp class="option">-Wuninitialized</samp>, <samp class="option">-Wunused</samp>, and others. </p> <p>The <code class="code">access</code> attribute specifies that a function to whose by-reference arguments the attribute applies accesses the referenced object according to <var class="var">access-mode</var>. The <var class="var">access-mode</var> argument is required and must be one of four names: <code class="code">read_only</code>, <code class="code">read_write</code>, <code class="code">write_only</code>, or <code class="code">none</code>. The remaining two are positional arguments. </p> <p>The required <var class="var">ref-index</var> positional argument denotes a function argument of pointer (or in C++, reference) type that is subject to the access. The same pointer argument can be referenced by at most one distinct <code class="code">access</code> attribute. </p> <p>The optional <var class="var">size-index</var> positional argument denotes a function argument of integer type that specifies the maximum size of the access. The size is the number of elements of the type referenced by <var class="var">ref-index</var>, or the number of bytes when the pointer type is <code class="code">void*</code>. When no <var class="var">size-index</var> argument is specified, the pointer argument must be either null or point to a space that is suitably aligned and large for at least one object of the referenced type (this implies that a past-the-end pointer is not a valid argument). The actual size of the access may be less but it must not be more. </p> <p>The <code class="code">read_only</code> access mode specifies that the pointer to which it applies is used to read the referenced object but not write to it. Unless the argument specifying the size of the access denoted by <var class="var">size-index</var> is zero, the referenced object must be initialized. The mode implies a stronger guarantee than the <code class="code">const</code> qualifier which, when cast away from a pointer, does not prevent the pointed-to object from being modified. Examples of the use of the <code class="code">read_only</code> access mode is the argument to the <code class="code">puts</code> function, or the second and third arguments to the <code class="code">memcpy</code> function. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((access (read_only, 1)))
+int puts (const char*);
+
+__attribute__ ((access (read_only, 2, 3)))
+void* memcpy (void*, const void*, size_t);</pre>
+</div> <p>The <code class="code">read_write</code> access mode applies to arguments of pointer types without the <code class="code">const</code> qualifier. It specifies that the pointer to which it applies is used to both read and write the referenced object. Unless the argument specifying the size of the access denoted by <var class="var">size-index</var> is zero, the object referenced by the pointer must be initialized. An example of the use of the <code class="code">read_write</code> access mode is the first argument to the <code class="code">strcat</code> function. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((access (read_write, 1), access (read_only, 2)))
+char* strcat (char*, const char*);</pre>
+</div> <p>The <code class="code">write_only</code> access mode applies to arguments of pointer types without the <code class="code">const</code> qualifier. It specifies that the pointer to which it applies is used to write to the referenced object but not read from it. The object referenced by the pointer need not be initialized. An example of the use of the <code class="code">write_only</code> access mode is the first argument to the <code class="code">strcpy</code> function, or the first two arguments to the <code class="code">fgets</code> function. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((access (write_only, 1), access (read_only, 2)))
+char* strcpy (char*, const char*);
+
+__attribute__ ((access (write_only, 1, 2), access (read_write, 3)))
+int fgets (char*, int, FILE*);</pre>
+</div> <p>The access mode <code class="code">none</code> specifies that the pointer to which it applies is not used to access the referenced object at all. Unless the pointer is null the pointed-to object must exist and have at least the size as denoted by the <var class="var">size-index</var> argument. When the optional <var class="var">size-index</var> argument is omitted for an argument of <code class="code">void*</code> type the actual pointer agument is ignored. The referenced object need not be initialized. The mode is intended to be used as a means to help validate the expected object size, for example in functions that call <code class="code">__builtin_object_size</code>. See <a class="xref" href="object-size-checking">Object Size Checking</a>. </p> <p>Note that the <code class="code">access</code> attribute merely specifies how an object referenced by the pointer argument can be accessed; it does not imply that an access <strong class="strong">will</strong> happen. Also, the <code class="code">access</code> attribute does not imply the attribute <code class="code">nonnull</code>; it may be appropriate to add both attributes at the declaration of a function that unconditionally manipulates a buffer via a pointer argument. See the <code class="code">nonnull</code> attribute for more information and caveats. </p> </dd> <dt>
+<span><code class="code">alias ("<var class="var">target</var>")</code><a class="copiable-link" href="#index-alias-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">alias</code> attribute causes the declaration to be emitted as an alias for another symbol, which must have been previously declared with the same type, and for variables, also the same size and alignment. Declaring an alias with a different type than the target is undefined and may be diagnosed. As an example, the following declarations: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void __f () { /* <span class="r">Do something.</span> */; }
+void f () __attribute__ ((weak, alias ("__f")));</pre>
+</div> <p>define ‘<samp class="samp">f</samp>’ to be a weak alias for ‘<samp class="samp">__f</samp>’. In C++, the mangled name for the target must be used. It is an error if ‘<samp class="samp">__f</samp>’ is not defined in the same translation unit. </p> <p>This attribute requires assembler and object file support, and may not be available on all targets. </p> </dd> <dt>
+<span><code class="code">aligned</code><a class="copiable-link" href="#index-aligned-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">aligned (<var class="var">alignment</var>)</code></dt> <dd>
+<p>The <code class="code">aligned</code> attribute specifies a minimum alignment for the first instruction of the function, measured in bytes. When specified, <var class="var">alignment</var> must be an integer constant power of 2. Specifying no <var class="var">alignment</var> argument implies the ideal alignment for the target. The <code class="code">__alignof__</code> operator can be used to determine what that is (see <a class="pxref" href="alignment">Determining the Alignment of Functions, Types or Variables</a>). The attribute has no effect when a definition for the function is not provided in the same translation unit. </p> <p>The attribute cannot be used to decrease the alignment of a function previously declared with a more restrictive alignment; only to increase it. Attempts to do otherwise are diagnosed. Some targets specify a minimum default alignment for functions that is greater than 1. On such targets, specifying a less restrictive alignment is silently ignored. Using the attribute overrides the effect of the <samp class="option">-falign-functions</samp> (see <a class="pxref" href="optimize-options">Options That Control Optimization</a>) option for this function. </p> <p>Note that the effectiveness of <code class="code">aligned</code> attributes may be limited by inherent limitations in the system linker and/or object file format. On some systems, the linker is only able to arrange for functions to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) See your linker documentation for further information. </p> <p>The <code class="code">aligned</code> attribute can also be used for variables and fields (see <a class="pxref" href="variable-attributes">Specifying Attributes of Variables</a>.) </p> </dd> <dt>
+<span><code class="code">alloc_align (<var class="var">position</var>)</code><a class="copiable-link" href="#index-alloc_005falign-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">alloc_align</code> attribute may be applied to a function that returns a pointer and takes at least one argument of an integer or enumerated type. It indicates that the returned pointer is aligned on a boundary given by the function argument at <var class="var">position</var>. Meaningful alignments are powers of 2 greater than one. GCC uses this information to improve pointer alignment analysis. </p> <p>The function parameter denoting the allocated alignment is specified by one constant integer argument whose number is the argument of the attribute. Argument numbering starts at one. </p> <p>For instance, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void* my_memalign (size_t, size_t) __attribute__ ((alloc_align (1)));</pre>
+</div> <p>declares that <code class="code">my_memalign</code> returns memory with minimum alignment given by parameter 1. </p> </dd> <dt>
+<span><code class="code">alloc_size (<var class="var">position</var>)</code><a class="copiable-link" href="#index-alloc_005fsize-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">alloc_size (<var class="var">position-1</var>, <var class="var">position-2</var>)</code></dt> <dd>
+<p>The <code class="code">alloc_size</code> attribute may be applied to a function that returns a pointer and takes at least one argument of an integer or enumerated type. It indicates that the returned pointer points to memory whose size is given by the function argument at <var class="var">position-1</var>, or by the product of the arguments at <var class="var">position-1</var> and <var class="var">position-2</var>. Meaningful sizes are positive values less than <code class="code">PTRDIFF_MAX</code>. GCC uses this information to improve the results of <code class="code">__builtin_object_size</code>. </p> <p>The function parameter(s) denoting the allocated size are specified by one or two integer arguments supplied to the attribute. The allocated size is either the value of the single function argument specified or the product of the two function arguments specified. Argument numbering starts at one for ordinary functions, and at two for C++ non-static member functions. </p> <p>For instance, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void* my_calloc (size_t, size_t) __attribute__ ((alloc_size (1, 2)));
+void* my_realloc (void*, size_t) __attribute__ ((alloc_size (2)));</pre>
+</div> <p>declares that <code class="code">my_calloc</code> returns memory of the size given by the product of parameter 1 and 2 and that <code class="code">my_realloc</code> returns memory of the size given by parameter 2. </p> </dd> <dt>
+<span><code class="code">always_inline</code><a class="copiable-link" href="#index-always_005finline-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function independent of any restrictions that otherwise apply to inlining. Failure to inline such a function is diagnosed as an error. Note that if such a function is called indirectly the compiler may or may not inline it depending on optimization level and a failure to inline an indirect call may or may not be diagnosed. </p> </dd> <dt>
+<span><code class="code">artificial</code><a class="copiable-link" href="#index-artificial-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute is useful for small inline wrappers that if possible should appear during debugging as a unit. Depending on the debug info format it either means marking the function as artificial or using the caller location for all instructions within the inlined body. </p> </dd> <dt>
+<span><code class="code">assume_aligned (<var class="var">alignment</var>)</code><a class="copiable-link" href="#index-assume_005faligned-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">assume_aligned (<var class="var">alignment</var>, <var class="var">offset</var>)</code></dt> <dd>
+<p>The <code class="code">assume_aligned</code> attribute may be applied to a function that returns a pointer. It indicates that the returned pointer is aligned on a boundary given by <var class="var">alignment</var>. If the attribute has two arguments, the second argument is misalignment <var class="var">offset</var>. Meaningful values of <var class="var">alignment</var> are powers of 2 greater than one. Meaningful values of <var class="var">offset</var> are greater than zero and less than <var class="var">alignment</var>. </p> <p>For instance </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void* my_alloc1 (size_t) __attribute__((assume_aligned (16)));
+void* my_alloc2 (size_t) __attribute__((assume_aligned (32, 8)));</pre>
+</div> <p>declares that <code class="code">my_alloc1</code> returns 16-byte aligned pointers and that <code class="code">my_alloc2</code> returns a pointer whose value modulo 32 is equal to 8. </p> </dd> <dt>
+<span><code class="code">cold</code><a class="copiable-link" href="#index-cold-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">cold</code> attribute on functions is used to inform the compiler that the function is unlikely to be executed. The function is optimized for size rather than speed and on many targets it is placed into a special subsection of the text section so all cold functions appear close together, improving code locality of non-cold parts of program. The paths leading to calls of cold functions within code are marked as unlikely by the branch prediction mechanism. It is thus useful to mark functions used to handle unlikely conditions, such as <code class="code">perror</code>, as cold to improve optimization of hot functions that do call marked functions in rare occasions. </p> <p>When profile feedback is available, via <samp class="option">-fprofile-use</samp>, cold functions are automatically detected and this attribute is ignored. </p> </dd> <dt>
+ <span><code class="code">const</code><a class="copiable-link" href="#index-const-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Calls to functions whose return value is not affected by changes to the observable state of the program and that have no observable effects on such state other than to return a value may lend themselves to optimizations such as common subexpression elimination. Declaring such functions with the <code class="code">const</code> attribute allows GCC to avoid emitting some calls in repeated invocations of the function with the same argument values. </p> <p>For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int square (int) __attribute__ ((const));</pre>
+</div> <p>tells GCC that subsequent calls to function <code class="code">square</code> with the same argument value can be replaced by the result of the first call regardless of the statements in between. </p> <p>The <code class="code">const</code> attribute prohibits a function from reading objects that affect its return value between successive invocations. However, functions declared with the attribute can safely read objects that do not change their return value, such as non-volatile constants. </p> <p>The <code class="code">const</code> attribute imposes greater restrictions on a function’s definition than the similar <code class="code">pure</code> attribute. Declaring the same function with both the <code class="code">const</code> and the <code class="code">pure</code> attribute is diagnosed. Because a const function cannot have any observable side effects it does not make sense for it to return <code class="code">void</code>. Declaring such a function is diagnosed. </p> <p>Note that a function that has pointer arguments and examines the data pointed to must <em class="emph">not</em> be declared <code class="code">const</code> if the pointed-to data might change between successive invocations of the function. In general, since a function cannot distinguish data that might change from data that cannot, const functions should never take pointer or, in C++, reference arguments. Likewise, a function that calls a non-const function usually must not be const itself. </p> </dd> <dt>
+ <span><code class="code">constructor</code><a class="copiable-link" href="#index-constructor-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">destructor</code></dt> <dt><code class="code">constructor (<var class="var">priority</var>)</code></dt> <dt><code class="code">destructor (<var class="var">priority</var>)</code></dt> <dd>
+<p>The <code class="code">constructor</code> attribute causes the function to be called automatically before execution enters <code class="code">main ()</code>. Similarly, the <code class="code">destructor</code> attribute causes the function to be called automatically after <code class="code">main ()</code> completes or <code class="code">exit ()</code> is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program. </p> <p>On some targets the attributes also accept an integer argument to specify a priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. Note that priorities 0-100 are reserved. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see <a class="pxref" href="c_002b_002b-attributes">C++-Specific Variable, Function, and Type Attributes</a>). However, at present, the order in which constructors for C++ objects with static storage duration and functions decorated with attribute <code class="code">constructor</code> are invoked is unspecified. In mixed declarations, attribute <code class="code">init_priority</code> can be used to impose a specific ordering. </p> <p>Using the argument forms of the <code class="code">constructor</code> and <code class="code">destructor</code> attributes on targets where the feature is not supported is rejected with an error. </p> </dd> <dt>
+<span><code class="code">copy</code><a class="copiable-link" href="#index-copy-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">copy (<var class="var">function</var>)</code></dt> <dd>
+<p>The <code class="code">copy</code> attribute applies the set of attributes with which <var class="var">function</var> has been declared to the declaration of the function to which the attribute is applied. The attribute is designed for libraries that define aliases or function resolvers that are expected to specify the same set of attributes as their targets. The <code class="code">copy</code> attribute can be used with functions, variables, or types. However, the kind of symbol to which the attribute is applied (either function or variable) must match the kind of symbol to which the argument refers. The <code class="code">copy</code> attribute copies only syntactic and semantic attributes but not attributes that affect a symbol’s linkage or visibility such as <code class="code">alias</code>, <code class="code">visibility</code>, or <code class="code">weak</code>. The <code class="code">deprecated</code> and <code class="code">target_clones</code> attribute are also not copied. See <a class="xref" href="common-type-attributes">Common Type Attributes</a>. See <a class="xref" href="common-variable-attributes">Common Variable Attributes</a>. </p> <p>For example, the <var class="var">StrongAlias</var> macro below makes use of the <code class="code">alias</code> and <code class="code">copy</code> attributes to define an alias named <var class="var">alloc</var> for function <var class="var">allocate</var> declared with attributes <var class="var">alloc_size</var>, <var class="var">malloc</var>, and <var class="var">nothrow</var>. Thanks to the <code class="code">__typeof__</code> operator the alias has the same type as the target function. As a result of the <code class="code">copy</code> attribute the alias also shares the same attributes as the target. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define StrongAlias(TargetFunc, AliasDecl) \
+ extern __typeof__ (TargetFunc) AliasDecl \
+ __attribute__ ((alias (#TargetFunc), copy (TargetFunc)));
+
+extern __attribute__ ((alloc_size (1), malloc, nothrow))
+ void* allocate (size_t);
+StrongAlias (allocate, alloc);</pre>
+</div> </dd> <dt>
+<span><code class="code">deprecated</code><a class="copiable-link" href="#index-deprecated-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">deprecated (<var class="var">msg</var>)</code></dt> <dd>
+<p>The <code class="code">deprecated</code> attribute results in a warning if the function is used anywhere in the source file. This is useful when identifying functions that are expected to be removed in a future version of a program. The warning also includes the location of the declaration of the deprecated function, to enable users to easily find further information about why the function is deprecated, or what they should do instead. Note that the warnings only occurs for uses: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int old_fn () __attribute__ ((deprecated));
+int old_fn ();
+int (*fn_ptr)() = old_fn;</pre>
+</div> <p>results in a warning on line 3 but not line 2. The optional <var class="var">msg</var> argument, which must be a string, is printed in the warning if present. </p> <p>The <code class="code">deprecated</code> attribute can also be used for variables and types (see <a class="pxref" href="variable-attributes">Specifying Attributes of Variables</a>, see <a class="pxref" href="type-attributes">Specifying Attributes of Types</a>.) </p> <p>The message attached to the attribute is affected by the setting of the <samp class="option">-fmessage-length</samp> option. </p> </dd> <dt>
+<span><code class="code">unavailable</code><a class="copiable-link" href="#index-unavailable-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">unavailable (<var class="var">msg</var>)</code></dt> <dd>
+<p>The <code class="code">unavailable</code> attribute results in an error if the function is used anywhere in the source file. This is useful when identifying functions that have been removed from a particular variation of an interface. Other than emitting an error rather than a warning, the <code class="code">unavailable</code> attribute behaves in the same manner as <code class="code">deprecated</code>. </p> <p>The <code class="code">unavailable</code> attribute can also be used for variables and types (see <a class="pxref" href="variable-attributes">Specifying Attributes of Variables</a>, see <a class="pxref" href="type-attributes">Specifying Attributes of Types</a>.) </p> </dd> <dt>
+ <span><code class="code">error ("<var class="var">message</var>")</code><a class="copiable-link" href="#index-error-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">warning ("<var class="var">message</var>")</code></dt> <dd>
+<p>If the <code class="code">error</code> or <code class="code">warning</code> attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, an error or warning (respectively) that includes <var class="var">message</var> is diagnosed. This is useful for compile-time checking, especially together with <code class="code">__builtin_constant_p</code> and inline functions where checking the inline function arguments is not possible through <code class="code">extern char [(condition) ? 1 : -1];</code> tricks. </p> <p>While it is possible to leave the function undefined and thus invoke a link failure (to define the function with a message in <code class="code">.gnu.warning*</code> section), when using these attributes the problem is diagnosed earlier and with exact location of the call even in presence of inline functions or when not emitting debugging information. </p> </dd> <dt>
+<span><code class="code">externally_visible</code><a class="copiable-link" href="#index-externally_005fvisible-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute, attached to a global variable or function, nullifies the effect of the <samp class="option">-fwhole-program</samp> command-line option, so the object remains visible outside the current compilation unit. </p> <p>If <samp class="option">-fwhole-program</samp> is used together with <samp class="option">-flto</samp> and <code class="command">gold</code> is used as the linker plugin, <code class="code">externally_visible</code> attributes are automatically added to functions (not variable yet due to a current <code class="command">gold</code> issue) that are accessed outside of LTO objects according to resolution file produced by <code class="command">gold</code>. For other linkers that cannot generate resolution file, explicit <code class="code">externally_visible</code> attributes are still necessary. </p> </dd> <dt>
+<span><code class="code">fd_arg</code><a class="copiable-link" href="#index-fd_005farg-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">fd_arg (<var class="var">N</var>)</code></dt> <dd>
+<p>The <code class="code">fd_arg</code> attribute may be applied to a function that takes an open file descriptor at referenced argument <var class="var">N</var>. </p> <p>It indicates that the passed filedescriptor must not have been closed. Therefore, when the analyzer is enabled with <samp class="option">-fanalyzer</samp>, the analyzer may emit a <samp class="option">-Wanalyzer-fd-use-after-close</samp> diagnostic if it detects a code path in which a function with this attribute is called with a closed file descriptor. </p> <p>The attribute also indicates that the file descriptor must have been checked for validity before usage. Therefore, analyzer may emit <samp class="option">-Wanalyzer-fd-use-without-check</samp> diagnostic if it detects a code path in which a function with this attribute is called with a file descriptor that has not been checked for validity. </p> </dd> <dt>
+<span><code class="code">fd_arg_read</code><a class="copiable-link" href="#index-fd_005farg_005fread-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">fd_arg_read (<var class="var">N</var>)</code></dt> <dd>
+<p>The <code class="code">fd_arg_read</code> is identical to <code class="code">fd_arg</code>, but with the additional requirement that it might read from the file descriptor, and thus, the file descriptor must not have been opened as write-only. </p> <p>The analyzer may emit a <samp class="option">-Wanalyzer-access-mode-mismatch</samp> diagnostic if it detects a code path in which a function with this attribute is called on a file descriptor opened with <code class="code">O_WRONLY</code>. </p> </dd> <dt>
+<span><code class="code">fd_arg_write</code><a class="copiable-link" href="#index-fd_005farg_005fwrite-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">fd_arg_write (<var class="var">N</var>)</code></dt> <dd>
+<p>The <code class="code">fd_arg_write</code> is identical to <code class="code">fd_arg_read</code> except that the analyzer may emit a <samp class="option">-Wanalyzer-access-mode-mismatch</samp> diagnostic if it detects a code path in which a function with this attribute is called on a file descriptor opened with <code class="code">O_RDONLY</code>. </p> </dd> <dt>
+<span><code class="code">flatten</code><a class="copiable-link" href="#index-flatten-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Generally, inlining into a function is limited. For a function marked with this attribute, every call inside this function is inlined, if possible. Functions declared with attribute <code class="code">noinline</code> and similar are not inlined. Whether the function itself is considered for inlining depends on its size and the current inlining parameters. </p> </dd> <dt>
+ <span><code class="code">format (<var class="var">archetype</var>, <var class="var">string-index</var>, <var class="var">first-to-check</var>)</code><a class="copiable-link" href="#index-format-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">format</code> attribute specifies that a function takes <code class="code">printf</code>, <code class="code">scanf</code>, <code class="code">strftime</code> or <code class="code">strfmon</code> style arguments that should be type-checked against a format string. For example, the declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern int
+my_printf (void *my_object, const char *my_format, ...)
+ __attribute__ ((format (printf, 2, 3)));</pre>
+</div> <p>causes the compiler to check the arguments in calls to <code class="code">my_printf</code> for consistency with the <code class="code">printf</code> style format string argument <code class="code">my_format</code>. </p> <p>The parameter <var class="var">archetype</var> determines how the format string is interpreted, and should be <code class="code">printf</code>, <code class="code">scanf</code>, <code class="code">strftime</code>, <code class="code">gnu_printf</code>, <code class="code">gnu_scanf</code>, <code class="code">gnu_strftime</code> or <code class="code">strfmon</code>. (You can also use <code class="code">__printf__</code>, <code class="code">__scanf__</code>, <code class="code">__strftime__</code> or <code class="code">__strfmon__</code>.) On MinGW targets, <code class="code">ms_printf</code>, <code class="code">ms_scanf</code>, and <code class="code">ms_strftime</code> are also present. <var class="var">archetype</var> values such as <code class="code">printf</code> refer to the formats accepted by the system’s C runtime library, while values prefixed with ‘<samp class="samp">gnu_</samp>’ always refer to the formats accepted by the GNU C Library. On Microsoft Windows targets, values prefixed with ‘<samp class="samp">ms_</samp>’ refer to the formats accepted by the <samp class="file">msvcrt.dll</samp> library. The parameter <var class="var">string-index</var> specifies which argument is the format string argument (starting from 1), while <var class="var">first-to-check</var> is the number of the first argument to check against the format string. For functions where the arguments are not available to be checked (such as <code class="code">vprintf</code>), specify the third parameter as zero. In this case the compiler only checks the format string for consistency. For <code class="code">strftime</code> formats, the third parameter is required to be zero. Since non-static C++ methods have an implicit <code class="code">this</code> argument, the arguments of such methods should be counted from two, not one, when giving values for <var class="var">string-index</var> and <var class="var">first-to-check</var>. </p> <p>In the example above, the format string (<code class="code">my_format</code>) is the second argument of the function <code class="code">my_print</code>, and the arguments to check start with the third argument, so the correct parameters for the format attribute are 2 and 3. </p> <p>The <code class="code">format</code> attribute allows you to identify your own functions that take format strings as arguments, so that GCC can check the calls to these functions for errors. The compiler always (unless <samp class="option">-ffreestanding</samp> or <samp class="option">-fno-builtin</samp> is used) checks formats for the standard library functions <code class="code">printf</code>, <code class="code">fprintf</code>, <code class="code">sprintf</code>, <code class="code">scanf</code>, <code class="code">fscanf</code>, <code class="code">sscanf</code>, <code class="code">strftime</code>, <code class="code">vprintf</code>, <code class="code">vfprintf</code> and <code class="code">vsprintf</code> whenever such warnings are requested (using <samp class="option">-Wformat</samp>), so there is no need to modify the header file <samp class="file">stdio.h</samp>. In C99 mode, the functions <code class="code">snprintf</code>, <code class="code">vsnprintf</code>, <code class="code">vscanf</code>, <code class="code">vfscanf</code> and <code class="code">vsscanf</code> are also checked. Except in strictly conforming C standard modes, the X/Open function <code class="code">strfmon</code> is also checked as are <code class="code">printf_unlocked</code> and <code class="code">fprintf_unlocked</code>. See <a class="xref" href="c-dialect-options">Options Controlling C Dialect</a>. </p> <p>For Objective-C dialects, <code class="code">NSString</code> (or <code class="code">__NSString__</code>) is recognized in the same context. Declarations including these format attributes are parsed for correct syntax, however the result of checking of such format strings is not yet defined, and is not carried out by this version of the compiler. </p> <p>The target may also provide additional types of format checks. See <a class="xref" href="target-format-checks">Format Checks Specific to Particular Target Machines</a>. </p> </dd> <dt>
+ <span><code class="code">format_arg (<var class="var">string-index</var>)</code><a class="copiable-link" href="#index-format_005farg-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">format_arg</code> attribute specifies that a function takes one or more format strings for a <code class="code">printf</code>, <code class="code">scanf</code>, <code class="code">strftime</code> or <code class="code">strfmon</code> style function and modifies it (for example, to translate it into another language), so the result can be passed to a <code class="code">printf</code>, <code class="code">scanf</code>, <code class="code">strftime</code> or <code class="code">strfmon</code> style function (with the remaining arguments to the format function the same as they would have been for the unmodified string). Multiple <code class="code">format_arg</code> attributes may be applied to the same function, each designating a distinct parameter as a format string. For example, the declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern char *
+my_dgettext (char *my_domain, const char *my_format)
+ __attribute__ ((format_arg (2)));</pre>
+</div> <p>causes the compiler to check the arguments in calls to a <code class="code">printf</code>, <code class="code">scanf</code>, <code class="code">strftime</code> or <code class="code">strfmon</code> type function, whose format string argument is a call to the <code class="code">my_dgettext</code> function, for consistency with the format string argument <code class="code">my_format</code>. If the <code class="code">format_arg</code> attribute had not been specified, all the compiler could tell in such calls to format functions would be that the format string argument is not constant; this would generate a warning when <samp class="option">-Wformat-nonliteral</samp> is used, but the calls could not be checked without the attribute. </p> <p>In calls to a function declared with more than one <code class="code">format_arg</code> attribute, each with a distinct argument value, the corresponding actual function arguments are checked against all format strings designated by the attributes. This capability is designed to support the GNU <code class="code">ngettext</code> family of functions. </p> <p>The parameter <var class="var">string-index</var> specifies which argument is the format string argument (starting from one). Since non-static C++ methods have an implicit <code class="code">this</code> argument, the arguments of such methods should be counted from two. </p> <p>The <code class="code">format_arg</code> attribute allows you to identify your own functions that modify format strings, so that GCC can check the calls to <code class="code">printf</code>, <code class="code">scanf</code>, <code class="code">strftime</code> or <code class="code">strfmon</code> type function whose operands are a call to one of your own function. The compiler always treats <code class="code">gettext</code>, <code class="code">dgettext</code>, and <code class="code">dcgettext</code> in this manner except when strict ISO C support is requested by <samp class="option">-ansi</samp> or an appropriate <samp class="option">-std</samp> option, or <samp class="option">-ffreestanding</samp> or <samp class="option">-fno-builtin</samp> is used. See <a class="xref" href="c-dialect-options">Options Controlling C Dialect</a>. </p> <p>For Objective-C dialects, the <code class="code">format-arg</code> attribute may refer to an <code class="code">NSString</code> reference for compatibility with the <code class="code">format</code> attribute above. </p> <p>The target may also allow additional types in <code class="code">format-arg</code> attributes. See <a class="xref" href="target-format-checks">Format Checks Specific to Particular Target Machines</a>. </p> </dd> <dt>
+<span><code class="code">gnu_inline</code><a class="copiable-link" href="#index-gnu_005finline-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute should be used with a function that is also declared with the <code class="code">inline</code> keyword. It directs GCC to treat the function as if it were defined in gnu90 mode even when compiling in C99 or gnu99 mode. </p> <p>If the function is declared <code class="code">extern</code>, then this definition of the function is used only for inlining. In no case is the function compiled as a standalone function, not even if you take its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it. This has almost the effect of a macro. The way to use this is to put a function definition in a header file with this attribute, and put another copy of the function, without <code class="code">extern</code>, in a library file. The definition in the header file causes most calls to the function to be inlined. If any uses of the function remain, they refer to the single copy in the library. Note that the two definitions of the functions need not be precisely the same, although if they do not have the same effect your program may behave oddly. </p> <p>In C, if the function is neither <code class="code">extern</code> nor <code class="code">static</code>, then the function is compiled as a standalone function, as well as being inlined where possible. </p> <p>This is how GCC traditionally handled functions declared <code class="code">inline</code>. Since ISO C99 specifies a different semantics for <code class="code">inline</code>, this function attribute is provided as a transition measure and as a useful feature in its own right. This attribute is available in GCC 4.1.3 and later. It is available if either of the preprocessor macros <code class="code">__GNUC_GNU_INLINE__</code> or <code class="code">__GNUC_STDC_INLINE__</code> are defined. See <a class="xref" href="inline">An Inline Function is As Fast As a Macro</a>. </p> <p>In C++, this attribute does not depend on <code class="code">extern</code> in any way, but it still requires the <code class="code">inline</code> keyword to enable its special behavior. </p> </dd> <dt>
+<span><code class="code">hot</code><a class="copiable-link" href="#index-hot-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">hot</code> attribute on a function is used to inform the compiler that the function is a hot spot of the compiled program. The function is optimized more aggressively and on many targets it is placed into a special subsection of the text section so all hot functions appear close together, improving locality. </p> <p>When profile feedback is available, via <samp class="option">-fprofile-use</samp>, hot functions are automatically detected and this attribute is ignored. </p> </dd> <dt>
+ <span><code class="code">ifunc ("<var class="var">resolver</var>")</code><a class="copiable-link" href="#index-ifunc-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">ifunc</code> attribute is used to mark a function as an indirect function using the STT_GNU_IFUNC symbol type extension to the ELF standard. This allows the resolution of the symbol value to be determined dynamically at load time, and an optimized version of the routine to be selected for the particular processor or other system characteristics determined then. To use this attribute, first define the implementation functions available, and a resolver function that returns a pointer to the selected implementation function. The implementation functions’ declarations must match the API of the function being implemented. The resolver should be declared to be a function taking no arguments and returning a pointer to a function of the same type as the implementation. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void *my_memcpy (void *dst, const void *src, size_t len)
+{
+ …
+ return dst;
+}
+
+static void * (*resolve_memcpy (void))(void *, const void *, size_t)
+{
+ return my_memcpy; // we will just always select this routine
+}</pre>
+</div> <p>The exported header file declaring the function the user calls would contain: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern void *memcpy (void *, const void *, size_t);</pre>
+</div> <p>allowing the user to call <code class="code">memcpy</code> as a regular function, unaware of the actual implementation. Finally, the indirect function needs to be defined in the same translation unit as the resolver function: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void *memcpy (void *, const void *, size_t)
+ __attribute__ ((ifunc ("resolve_memcpy")));</pre>
+</div> <p>In C++, the <code class="code">ifunc</code> attribute takes a string that is the mangled name of the resolver function. A C++ resolver for a non-static member function of class <code class="code">C</code> should be declared to return a pointer to a non-member function taking pointer to <code class="code">C</code> as the first argument, followed by the same arguments as of the implementation function. G++ checks the signatures of the two functions and issues a <samp class="option">-Wattribute-alias</samp> warning for mismatches. To suppress a warning for the necessary cast from a pointer to the implementation member function to the type of the corresponding non-member function use the <samp class="option">-Wno-pmf-conversions</samp> option. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">class S
+{
+private:
+ int debug_impl (int);
+ int optimized_impl (int);
+
+ typedef int Func (S*, int);
+
+ static Func* resolver ();
+public:
+
+ int interface (int);
+};
+
+int S::debug_impl (int) { /* <span class="r">…</span> */ }
+int S::optimized_impl (int) { /* <span class="r">…</span> */ }
+
+S::Func* S::resolver ()
+{
+ int (S::*pimpl) (int)
+ = getenv ("DEBUG") ? &amp;S::debug_impl : &amp;S::optimized_impl;
+
+ // Cast triggers -Wno-pmf-conversions.
+ return reinterpret_cast&lt;Func*&gt;(pimpl);
+}
+
+int S::interface (int) __attribute__ ((ifunc ("_ZN1S8resolverEv")));</pre>
+</div> <p>Indirect functions cannot be weak. Binutils version 2.20.1 or higher and GNU C Library version 2.11.1 are required to use this feature. </p> </dd> <dt><code class="code">interrupt</code></dt> <dt><code class="code">interrupt_handler</code></dt> <dd>
+<p>Many GCC back ends support attributes to indicate that a function is an interrupt handler, which tells the compiler to generate function entry and exit sequences that differ from those from regular functions. The exact syntax and behavior are target-specific; refer to the following subsections for details. </p> </dd> <dt>
+<span><code class="code">leaf</code><a class="copiable-link" href="#index-leaf-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Calls to external functions with this attribute must return to the current compilation unit only by return or by exception handling. In particular, a leaf function is not allowed to invoke callback functions passed to it from the current compilation unit, directly call functions exported by the unit, or <code class="code">longjmp</code> into the unit. Leaf functions might still call functions from other compilation units and thus they are not necessarily leaf in the sense that they contain no function calls at all. </p> <p>The attribute is intended for library functions to improve dataflow analysis. The compiler takes the hint that any data not escaping the current compilation unit cannot be used or modified by the leaf function. For example, the <code class="code">sin</code> function is a leaf function, but <code class="code">qsort</code> is not. </p> <p>Note that leaf functions might indirectly run a signal handler defined in the current compilation unit that uses static variables. Similarly, when lazy symbol resolution is in effect, leaf functions might invoke indirect functions whose resolver function or implementation function is defined in the current compilation unit and uses static variables. There is no standard-compliant way to write such a signal handler, resolver function, or implementation function, and the best that you can do is to remove the <code class="code">leaf</code> attribute or mark all such static variables <code class="code">volatile</code>. Lastly, for ELF-based systems that support symbol interposition, care should be taken that functions defined in the current compilation unit do not unexpectedly interpose other symbols based on the defined standards mode and defined feature test macros; otherwise an inadvertent callback would be added. </p> <p>The attribute has no effect on functions defined within the current compilation unit. This is to allow easy merging of multiple compilation units into one, for example, by using the link-time optimization. For this reason the attribute is not allowed on types to annotate indirect calls. </p> </dd> <dt>
+ <span><code class="code">malloc</code><a class="copiable-link" href="#index-malloc-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">malloc (<var class="var">deallocator</var>)</code></dt> <dt><code class="code">malloc (<var class="var">deallocator</var>, <var class="var">ptr-index</var>)</code></dt> <dd>
+<p>Attribute <code class="code">malloc</code> indicates that a function is <code class="code">malloc</code>-like, i.e., that the pointer <var class="var">P</var> returned by the function cannot alias any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by <var class="var">P</var>. In addition, GCC predicts that a function with the attribute returns non-null in most cases. </p> <p>Independently, the form of the attribute with one or two arguments associates <code class="code">deallocator</code> as a suitable deallocation function for pointers returned from the <code class="code">malloc</code>-like function. <var class="var">ptr-index</var> denotes the positional argument to which when the pointer is passed in calls to <code class="code">deallocator</code> has the effect of deallocating it. </p> <p>Using the attribute with no arguments is designed to improve optimization by relying on the aliasing property it implies. Functions like <code class="code">malloc</code> and <code class="code">calloc</code> have this property because they return a pointer to uninitialized or zeroed-out, newly obtained storage. However, functions like <code class="code">realloc</code> do not have this property, as they may return pointers to storage containing pointers to existing objects. Additionally, since all such functions are assumed to return null only infrequently, callers can be optimized based on that assumption. </p> <p>Associating a function with a <var class="var">deallocator</var> helps detect calls to mismatched allocation and deallocation functions and diagnose them under the control of options such as <samp class="option">-Wmismatched-dealloc</samp>. It also makes it possible to diagnose attempts to deallocate objects that were not allocated dynamically, by <samp class="option">-Wfree-nonheap-object</samp>. To indicate that an allocation function both satisifies the nonaliasing property and has a deallocator associated with it, both the plain form of the attribute and the one with the <var class="var">deallocator</var> argument must be used. The same function can be both an allocator and a deallocator. Since inlining one of the associated functions but not the other could result in apparent mismatches, this form of attribute <code class="code">malloc</code> is not accepted on inline functions. For the same reason, using the attribute prevents both the allocation and deallocation functions from being expanded inline. </p> <p>For example, besides stating that the functions return pointers that do not alias any others, the following declarations make <code class="code">fclose</code> a suitable deallocator for pointers returned from all functions except <code class="code">popen</code>, and <code class="code">pclose</code> as the only suitable deallocator for pointers returned from <code class="code">popen</code>. The deallocator functions must be declared before they can be referenced in the attribute. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int fclose (FILE*);
+int pclose (FILE*);
+
+__attribute__ ((malloc, malloc (fclose, 1)))
+ FILE* fdopen (int, const char*);
+__attribute__ ((malloc, malloc (fclose, 1)))
+ FILE* fopen (const char*, const char*);
+__attribute__ ((malloc, malloc (fclose, 1)))
+ FILE* fmemopen(void *, size_t, const char *);
+__attribute__ ((malloc, malloc (pclose, 1)))
+ FILE* popen (const char*, const char*);
+__attribute__ ((malloc, malloc (fclose, 1)))
+ FILE* tmpfile (void);</pre>
+</div> <p>The warnings guarded by <samp class="option">-fanalyzer</samp> respect allocation and deallocation pairs marked with the <code class="code">malloc</code>. In particular: </p> <ul class="itemize mark-bullet"> <li>The analyzer emits a <samp class="option">-Wanalyzer-mismatching-deallocation</samp> diagnostic if there is an execution path in which the result of an allocation call is passed to a different deallocator. </li>
+<li>The analyzer emits a <samp class="option">-Wanalyzer-double-free</samp> diagnostic if there is an execution path in which a value is passed more than once to a deallocation call. </li>
+<li>The analyzer considers the possibility that an allocation function could fail and return null. If there are execution paths in which an unchecked result of an allocation call is dereferenced or passed to a function requiring a non-null argument, it emits <samp class="option">-Wanalyzer-possible-null-dereference</samp> and <samp class="option">-Wanalyzer-possible-null-argument</samp> diagnostics. If the allocator always returns non-null, use <code class="code">__attribute__ ((returns_nonnull))</code> to suppress these warnings. For example: <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">char *xstrdup (const char *)
+ __attribute__((malloc (free), returns_nonnull));</pre>
+</div> </li>
+<li>The analyzer emits a <samp class="option">-Wanalyzer-use-after-free</samp> diagnostic if there is an execution path in which the memory passed by pointer to a deallocation call is used after the deallocation. </li>
+<li>The analyzer emits a <samp class="option">-Wanalyzer-malloc-leak</samp> diagnostic if there is an execution path in which the result of an allocation call is leaked (without being passed to the deallocation function). </li>
+<li>The analyzer emits a <samp class="option">-Wanalyzer-free-of-non-heap</samp> diagnostic if a deallocation function is used on a global or on-stack variable. </li>
+</ul> <p>The analyzer assumes that deallocators can gracefully handle the null pointer. If this is not the case, the deallocator can be marked with <code class="code">__attribute__((nonnull))</code> so that <samp class="option">-fanalyzer</samp> can emit a <samp class="option">-Wanalyzer-possible-null-argument</samp> diagnostic for code paths in which the deallocator is called with null. </p> </dd> <dt>
+<span><code class="code">no_icf</code><a class="copiable-link" href="#index-no_005ficf-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This function attribute prevents a functions from being merged with another semantically equivalent function. </p> </dd> <dt>
+ <span><code class="code">no_instrument_function</code><a class="copiable-link" href="#index-no_005finstrument_005ffunction-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>If any of <samp class="option">-finstrument-functions</samp>, <samp class="option">-p</samp>, or <samp class="option">-pg</samp> are given, profiling function calls are generated at entry and exit of most user-compiled functions. Functions with this attribute are not so instrumented. </p> </dd> <dt>
+<span><code class="code">no_profile_instrument_function</code><a class="copiable-link" href="#index-no_005fprofile_005finstrument_005ffunction-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">no_profile_instrument_function</code> attribute on functions is used to inform the compiler that it should not process any profile feedback based optimization code instrumentation. </p> </dd> <dt>
+<span><code class="code">no_reorder</code><a class="copiable-link" href="#index-no_005freorder-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Do not reorder functions or variables marked <code class="code">no_reorder</code> against each other or top level assembler statements the executable. The actual order in the program will depend on the linker command line. Static variables marked like this are also not removed. This has a similar effect as the <samp class="option">-fno-toplevel-reorder</samp> option, but only applies to the marked symbols. </p> </dd> <dt>
+<span><code class="code">no_sanitize ("<var class="var">sanitize_option</var>")</code><a class="copiable-link" href="#index-no_005fsanitize-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">no_sanitize</code> attribute on functions is used to inform the compiler that it should not do sanitization of any option mentioned in <var class="var">sanitize_option</var>. A list of values acceptable by the <samp class="option">-fsanitize</samp> option can be provided. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void __attribute__ ((no_sanitize ("alignment", "object-size")))
+f () { /* <span class="r">Do something.</span> */; }
+void __attribute__ ((no_sanitize ("alignment,object-size")))
+g () { /* <span class="r">Do something.</span> */; }</pre>
+</div> </dd> <dt>
+<span><code class="code">no_sanitize_address</code><a class="copiable-link" href="#index-no_005fsanitize_005faddress-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">no_address_safety_analysis</code></dt> <dd>
+<p>The <code class="code">no_sanitize_address</code> attribute on functions is used to inform the compiler that it should not instrument memory accesses in the function when compiling with the <samp class="option">-fsanitize=address</samp> option. The <code class="code">no_address_safety_analysis</code> is a deprecated alias of the <code class="code">no_sanitize_address</code> attribute, new code should use <code class="code">no_sanitize_address</code>. </p> </dd> <dt>
+<span><code class="code">no_sanitize_thread</code><a class="copiable-link" href="#index-no_005fsanitize_005fthread-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">no_sanitize_thread</code> attribute on functions is used to inform the compiler that it should not instrument memory accesses in the function when compiling with the <samp class="option">-fsanitize=thread</samp> option. </p> </dd> <dt>
+<span><code class="code">no_sanitize_undefined</code><a class="copiable-link" href="#index-no_005fsanitize_005fundefined-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">no_sanitize_undefined</code> attribute on functions is used to inform the compiler that it should not check for undefined behavior in the function when compiling with the <samp class="option">-fsanitize=undefined</samp> option. </p> </dd> <dt>
+<span><code class="code">no_sanitize_coverage</code><a class="copiable-link" href="#index-no_005fsanitize_005fcoverage-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">no_sanitize_coverage</code> attribute on functions is used to inform the compiler that it should not do coverage-guided fuzzing code instrumentation (<samp class="option">-fsanitize-coverage</samp>). </p> </dd> <dt>
+ <span><code class="code">no_split_stack</code><a class="copiable-link" href="#index-no_005fsplit_005fstack-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>If <samp class="option">-fsplit-stack</samp> is given, functions have a small prologue which decides whether to split the stack. Functions with the <code class="code">no_split_stack</code> attribute do not have that prologue, and thus may run with only a small amount of stack space available. </p> </dd> <dt>
+<span><code class="code">no_stack_limit</code><a class="copiable-link" href="#index-no_005fstack_005flimit-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute locally overrides the <samp class="option">-fstack-limit-register</samp> and <samp class="option">-fstack-limit-symbol</samp> command-line options; it has the effect of disabling stack limit checking in the function it applies to. </p> </dd> <dt>
+<span><code class="code">noclone</code><a class="copiable-link" href="#index-noclone-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This function attribute prevents a function from being considered for cloning—a mechanism that produces specialized copies of functions and which is (currently) performed by interprocedural constant propagation. </p> </dd> <dt>
+<span><code class="code">noinline</code><a class="copiable-link" href="#index-noinline-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This function attribute prevents a function from being considered for inlining. If the function does not have side effects, there are optimizations other than inlining that cause function calls to be optimized away, although the function call is live. To keep such calls from being optimized away, put </p>
+<div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">asm ("");</pre>
+</div> <p>(see <a class="pxref" href="extended-asm">Extended Asm - Assembler Instructions with C Expression Operands</a>) in the called function, to serve as a special side effect. </p> </dd> <dt>
+<span><code class="code">noipa</code><a class="copiable-link" href="#index-noipa-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Disable interprocedural optimizations between the function with this attribute and its callers, as if the body of the function is not available when optimizing callers and the callers are unavailable when optimizing the body. This attribute implies <code class="code">noinline</code>, <code class="code">noclone</code> and <code class="code">no_icf</code> attributes. However, this attribute is not equivalent to a combination of other attributes, because its purpose is to suppress existing and future optimizations employing interprocedural analysis, including those that do not have an attribute suitable for disabling them individually. This attribute is supported mainly for the purpose of testing the compiler. </p> </dd> <dt>
+ <span><code class="code">nonnull</code><a class="copiable-link" href="#index-nonnull-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">nonnull (<var class="var">arg-index</var>, …)</code></dt> <dd>
+<p>The <code class="code">nonnull</code> attribute may be applied to a function that takes at least one argument of a pointer type. It indicates that the referenced arguments must be non-null pointers. For instance, the declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern void *
+my_memcpy (void *dest, const void *src, size_t len)
+ __attribute__((nonnull (1, 2)));</pre>
+</div> <p>informs the compiler that, in calls to <code class="code">my_memcpy</code>, arguments <var class="var">dest</var> and <var class="var">src</var> must be non-null. </p> <p>The attribute has an effect both on functions calls and function definitions. </p> <p>For function calls: </p>
+<ul class="itemize mark-bullet"> <li>If the compiler determines that a null pointer is passed in an argument slot marked as non-null, and the <samp class="option">-Wnonnull</samp> option is enabled, a warning is issued. See <a class="xref" href="warning-options">Options to Request or Suppress Warnings</a>. </li>
+<li>The <samp class="option">-fisolate-erroneous-paths-attribute</samp> option can be specified to have GCC transform calls with null arguments to non-null functions into traps. See <a class="xref" href="optimize-options">Options That Control Optimization</a>. </li>
+<li>The compiler may also perform optimizations based on the knowledge that certain function arguments cannot be null. These optimizations can be disabled by the <samp class="option">-fno-delete-null-pointer-checks</samp> option. See <a class="xref" href="optimize-options">Options That Control Optimization</a>. </li>
+</ul> <p>For function definitions: </p>
+<ul class="itemize mark-bullet"> <li>If the compiler determines that a function parameter that is marked with nonnull is compared with null, and <samp class="option">-Wnonnull-compare</samp> option is enabled, a warning is issued. See <a class="xref" href="warning-options">Options to Request or Suppress Warnings</a>. </li>
+<li>The compiler may also perform optimizations based on the knowledge that <code class="code">nonnull</code> parameters cannot be null. This can currently not be disabled other than by removing the nonnull attribute. </li>
+</ul> <p>If no <var class="var">arg-index</var> is given to the <code class="code">nonnull</code> attribute, all pointer arguments are marked as non-null. To illustrate, the following declaration is equivalent to the previous example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern void *
+my_memcpy (void *dest, const void *src, size_t len)
+ __attribute__((nonnull));</pre>
+</div> </dd> <dt>
+<span><code class="code">noplt</code><a class="copiable-link" href="#index-noplt-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">noplt</code> attribute is the counterpart to option <samp class="option">-fno-plt</samp>. Calls to functions marked with this attribute in position-independent code do not use the PLT. </p> <div class="example smallexample"> <div class="group"><pre class="example-preformatted" data-language="cpp">/* Externally defined function foo. */
+int foo () __attribute__ ((noplt));
+
+int
+main (/* <span class="r">…</span> */)
+{
+ /* <span class="r">…</span> */
+ foo ();
+ /* <span class="r">…</span> */
+}</pre></div>
+</div> <p>The <code class="code">noplt</code> attribute on function <code class="code">foo</code> tells the compiler to assume that the function <code class="code">foo</code> is externally defined and that the call to <code class="code">foo</code> must avoid the PLT in position-independent code. </p> <p>In position-dependent code, a few targets also convert calls to functions that are marked to not use the PLT to use the GOT instead. </p> </dd> <dt>
+ <span><code class="code">noreturn</code><a class="copiable-link" href="#index-noreturn-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>A few standard library functions, such as <code class="code">abort</code> and <code class="code">exit</code>, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them <code class="code">noreturn</code> to tell the compiler this fact. For example, </p> <div class="example smallexample"> <div class="group"><pre class="example-preformatted" data-language="cpp">void fatal () __attribute__ ((noreturn));
+
+void
+fatal (/* <span class="r">…</span> */)
+{
+ /* <span class="r">…</span> */ /* <span class="r">Print error message.</span> */ /* <span class="r">…</span> */
+ exit (1);
+}</pre></div>
+</div> <p>The <code class="code">noreturn</code> keyword tells the compiler to assume that <code class="code">fatal</code> cannot return. It can then optimize without regard to what would happen if <code class="code">fatal</code> ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables. </p> <p>The <code class="code">noreturn</code> keyword does not affect the exceptional path when that applies: a <code class="code">noreturn</code>-marked function may still return to the caller by throwing an exception or calling <code class="code">longjmp</code>. </p> <p>In order to preserve backtraces, GCC will never turn calls to <code class="code">noreturn</code> functions into tail calls. </p> <p>Do not assume that registers saved by the calling function are restored before calling the <code class="code">noreturn</code> function. </p> <p>It does not make sense for a <code class="code">noreturn</code> function to have a return type other than <code class="code">void</code>. </p> </dd> <dt>
+<span><code class="code">nothrow</code><a class="copiable-link" href="#index-nothrow-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">nothrow</code> attribute is used to inform the compiler that a function cannot throw an exception. For example, most functions in the standard C library can be guaranteed not to throw an exception with the notable exceptions of <code class="code">qsort</code> and <code class="code">bsearch</code> that take function pointer arguments. </p> </dd> <dt>
+<span><code class="code">optimize (<var class="var">level</var>, …)</code><a class="copiable-link" href="#index-optimize-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">optimize (<var class="var">string</var>, …)</code></dt> <dd>
+<p>The <code class="code">optimize</code> attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line. The optimize attribute arguments of a function behave as if appended to the command-line. </p> <p>Valid arguments are constant non-negative integers and strings. Each numeric argument specifies an optimization <var class="var">level</var>. Each <var class="var">string</var> argument consists of one or more comma-separated substrings. Each substring that begins with the letter <code class="code">O</code> refers to an optimization option such as <samp class="option">-O0</samp> or <samp class="option">-Os</samp>. Other substrings are taken as suffixes to the <code class="code">-f</code> prefix jointly forming the name of an optimization option. See <a class="xref" href="optimize-options">Options That Control Optimization</a>. </p> <p>‘<samp class="samp">#pragma GCC optimize</samp>’ can be used to set optimization options for more than one function. See <a class="xref" href="function-specific-option-pragmas">Function Specific Option Pragmas</a>, for details about the pragma. </p> <p>Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘<samp class="samp">,</samp>’) within a single string. Spaces are not permitted within the strings. </p> <p>Not every optimization option that starts with the <var class="var">-f</var> prefix specified by the attribute necessarily has an effect on the function. The <code class="code">optimize</code> attribute should be used for debugging purposes only. It is not suitable in production code. </p> </dd> <dt>
+ <span><code class="code">patchable_function_entry</code><a class="copiable-link" href="#index-patchable_005ffunction_005fentry-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>In case the target’s text segment can be made writable at run time by any means, padding the function entry with a number of NOPs can be used to provide a universal tool for instrumentation. </p> <p>The <code class="code">patchable_function_entry</code> function attribute can be used to change the number of NOPs to any desired value. The two-value syntax is the same as for the command-line switch <samp class="option">-fpatchable-function-entry=N,M</samp>, generating <var class="var">N</var> NOPs, with the function entry point before the <var class="var">M</var>th NOP instruction. <var class="var">M</var> defaults to 0 if omitted e.g. function entry point is before the first NOP. </p> <p>If patchable function entries are enabled globally using the command-line option <samp class="option">-fpatchable-function-entry=N,M</samp>, then you must disable instrumentation on all functions that are part of the instrumentation framework with the attribute <code class="code">patchable_function_entry (0)</code> to prevent recursion. </p> </dd> <dt>
+ <span><code class="code">pure</code><a class="copiable-link" href="#index-pure-function-attribute"> ¶</a></span>
+</dt> <dd> <p>Calls to functions that have no observable effects on the state of the program other than to return a value may lend themselves to optimizations such as common subexpression elimination. Declaring such functions with the <code class="code">pure</code> attribute allows GCC to avoid emitting some calls in repeated invocations of the function with the same argument values. </p> <p>The <code class="code">pure</code> attribute prohibits a function from modifying the state of the program that is observable by means other than inspecting the function’s return value. However, functions declared with the <code class="code">pure</code> attribute can safely read any non-volatile objects, and modify the value of objects in a way that does not affect their return value or the observable state of the program. </p> <p>For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int hash (char *) __attribute__ ((pure));</pre>
+</div> <p>tells GCC that subsequent calls to the function <code class="code">hash</code> with the same string can be replaced by the result of the first call provided the state of the program observable by <code class="code">hash</code>, including the contents of the array itself, does not change in between. Even though <code class="code">hash</code> takes a non-const pointer argument it must not modify the array it points to, or any other object whose value the rest of the program may depend on. However, the caller may safely change the contents of the array between successive calls to the function (doing so disables the optimization). The restriction also applies to member objects referenced by the <code class="code">this</code> pointer in C++ non-static member functions. </p> <p>Some common examples of pure functions are <code class="code">strlen</code> or <code class="code">memcmp</code>. Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between consecutive calls (such as the standard C <code class="code">feof</code> function in a multithreading environment). </p> <p>The <code class="code">pure</code> attribute imposes similar but looser restrictions on a function’s definition than the <code class="code">const</code> attribute: <code class="code">pure</code> allows the function to read any non-volatile memory, even if it changes in between successive invocations of the function. Declaring the same function with both the <code class="code">pure</code> and the <code class="code">const</code> attribute is diagnosed. Because a pure function cannot have any observable side effects it does not make sense for such a function to return <code class="code">void</code>. Declaring such a function is diagnosed. </p> </dd> <dt>
+<span><code class="code">returns_nonnull</code><a class="copiable-link" href="#index-returns_005fnonnull-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">returns_nonnull</code> attribute specifies that the function return value should be a non-null pointer. For instance, the declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern void *
+mymalloc (size_t len) __attribute__((returns_nonnull));</pre>
+</div> <p>lets the compiler optimize callers based on the knowledge that the return value will never be null. </p> </dd> <dt>
+ <span><code class="code">returns_twice</code><a class="copiable-link" href="#index-returns_005ftwice-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">returns_twice</code> attribute tells the compiler that a function may return more than one time. The compiler ensures that all registers are dead before calling such a function and emits a warning about the variables that may be clobbered after the second return from the function. Examples of such functions are <code class="code">setjmp</code> and <code class="code">vfork</code>. The <code class="code">longjmp</code>-like counterpart of such function, if any, might need to be marked with the <code class="code">noreturn</code> attribute. </p> </dd> <dt>
+ <span><code class="code">section ("<var class="var">section-name</var>")</code><a class="copiable-link" href="#index-section-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Normally, the compiler places the code it generates in the <code class="code">text</code> section. Sometimes, however, you need additional sections, or you need certain particular functions to appear in special sections. The <code class="code">section</code> attribute specifies that a function lives in a particular section. For example, the declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern void foobar (void) __attribute__ ((section ("bar")));</pre>
+</div> <p>puts the function <code class="code">foobar</code> in the <code class="code">bar</code> section. </p> <p>Some file formats do not support arbitrary sections so the <code class="code">section</code> attribute is not available on all platforms. If you need to map the entire contents of a module to a particular section, consider using the facilities of the linker instead. </p> </dd> <dt>
+<span><code class="code">sentinel</code><a class="copiable-link" href="#index-sentinel-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">sentinel (<var class="var">position</var>)</code></dt> <dd>
+<p>This function attribute indicates that an argument in a call to the function is expected to be an explicit <code class="code">NULL</code>. The attribute is only valid on variadic functions. By default, the sentinel is expected to be the last argument of the function call. If the optional <var class="var">position</var> argument is specified to the attribute, the sentinel must be located at <var class="var">position</var> counting backwards from the end of the argument list. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((sentinel))
+is equivalent to
+__attribute__ ((sentinel(0)))</pre>
+</div> <p>The attribute is automatically set with a position of 0 for the built-in functions <code class="code">execl</code> and <code class="code">execlp</code>. The built-in function <code class="code">execle</code> has the attribute set with a position of 1. </p> <p>A valid <code class="code">NULL</code> in this context is defined as zero with any object pointer type. If your system defines the <code class="code">NULL</code> macro with an integer type then you need to add an explicit cast. During installation GCC replaces the system <code class="code">&lt;stddef.h&gt;</code> header with a copy that redefines NULL appropriately. </p> <p>The warnings for missing or incorrect sentinels are enabled with <samp class="option">-Wformat</samp>. </p> </dd> <dt>
+<span><code class="code">simd</code><a class="copiable-link" href="#index-simd-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">simd("<var class="var">mask</var>")</code></dt> <dd>
+<p>This attribute enables creation of one or more function versions that can process multiple arguments using SIMD instructions from a single invocation. Specifying this attribute allows compiler to assume that such versions are available at link time (provided in the same or another translation unit). Generated versions are target-dependent and described in the corresponding Vector ABI document. For x86_64 target this document can be found <a class="uref" href="https://sourceware.org/glibc/wiki/libmvec?action=AttachFile&amp;do=view&amp;target=VectorABI.txt">here</a>. </p> <p>The optional argument <var class="var">mask</var> may have the value <code class="code">notinbranch</code> or <code class="code">inbranch</code>, and instructs the compiler to generate non-masked or masked clones correspondingly. By default, all clones are generated. </p> <p>If the attribute is specified and <code class="code">#pragma omp declare simd</code> is present on a declaration and the <samp class="option">-fopenmp</samp> or <samp class="option">-fopenmp-simd</samp> switch is specified, then the attribute is ignored. </p> </dd> <dt>
+<span><code class="code">stack_protect</code><a class="copiable-link" href="#index-stack_005fprotect-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute adds stack protection code to the function if flags <samp class="option">-fstack-protector</samp>, <samp class="option">-fstack-protector-strong</samp> or <samp class="option">-fstack-protector-explicit</samp> are set. </p> </dd> <dt>
+<span><code class="code">no_stack_protector</code><a class="copiable-link" href="#index-no_005fstack_005fprotector-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute prevents stack protection code for the function. </p> </dd> <dt>
+<span><code class="code">target (<var class="var">string</var>, …)</code><a class="copiable-link" href="#index-target-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>Multiple target back ends implement the <code class="code">target</code> attribute to specify that a function is to be compiled with different target options than specified on the command line. The original target command-line options are ignored. One or more strings can be provided as arguments. Each string consists of one or more comma-separated suffixes to the <code class="code">-m</code> prefix jointly forming the name of a machine-dependent option. See <a class="xref" href="submodel-options">Machine-Dependent Options</a>. </p> <p>The <code class="code">target</code> attribute can be used for instance to have a function compiled with a different ISA (instruction set architecture) than the default. ‘<samp class="samp">#pragma GCC target</samp>’ can be used to specify target-specific options for more than one function. See <a class="xref" href="function-specific-option-pragmas">Function Specific Option Pragmas</a>, for details about the pragma. </p> <p>For instance, on an x86, you could declare one function with the <code class="code">target("sse4.1,arch=core2")</code> attribute and another with <code class="code">target("sse4a,arch=amdfam10")</code>. This is equivalent to compiling the first function with <samp class="option">-msse4.1</samp> and <samp class="option">-march=core2</samp> options, and the second function with <samp class="option">-msse4a</samp> and <samp class="option">-march=amdfam10</samp> options. It is up to you to make sure that a function is only invoked on a machine that supports the particular ISA it is compiled for (for example by using <code class="code">cpuid</code> on x86 to determine what feature bits and architecture family are used). </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int core2_func (void) __attribute__ ((__target__ ("arch=core2")));
+int sse3_func (void) __attribute__ ((__target__ ("sse3")));</pre>
+</div> <p>Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘<samp class="samp">,</samp>’) within a single string. Spaces are not permitted within the strings. </p> <p>The options supported are specific to each target; refer to <a class="ref" href="x86-function-attributes">x86 Function Attributes</a>, <a class="ref" href="powerpc-function-attributes">PowerPC Function Attributes</a>, <a class="ref" href="arm-function-attributes">ARM Function Attributes</a>, <a class="ref" href="aarch64-function-attributes">AArch64 Function Attributes</a>, <a class="ref" href="nios-ii-function-attributes">Nios II Function Attributes</a>, and <a class="ref" href="s_002f390-function-attributes">S/390 Function Attributes</a> for details. </p> </dd> <dt>
+<span><code class="code">symver ("<var class="var">name2</var>@<var class="var">nodename</var>")</code><a class="copiable-link" href="#index-symver-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>On ELF targets this attribute creates a symbol version. The <var class="var">name2</var> part of the parameter is the actual name of the symbol by which it will be externally referenced. The <code class="code">nodename</code> portion should be the name of a node specified in the version script supplied to the linker when building a shared library. Versioned symbol must be defined and must be exported with default visibility. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((__symver__ ("foo@VERS_1"))) int
+foo_v1 (void)
+{
+}</pre>
+</div> <p>Will produce a <code class="code">.symver foo_v1, foo@VERS_1</code> directive in the assembler output. </p> <p>One can also define multiple version for a given symbol (starting from binutils 2.35). </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((__symver__ ("foo@VERS_2"), __symver__ ("foo@VERS_3")))
+int symver_foo_v1 (void)
+{
+}</pre>
+</div> <p>This example creates a symbol name <code class="code">symver_foo_v1</code> which will be version <code class="code">VERS_2</code> and <code class="code">VERS_3</code> of <code class="code">foo</code>. </p> <p>If you have an older release of binutils, then symbol alias needs to be used: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((__symver__ ("foo@VERS_2")))
+int foo_v1 (void)
+{
+ return 0;
+}
+
+__attribute__ ((__symver__ ("foo@VERS_3")))
+__attribute__ ((alias ("foo_v1")))
+int symver_foo_v1 (void);</pre>
+</div> <p>Finally if the parameter is <code class="code">"<var class="var">name2</var>@@<var class="var">nodename</var>"</code> then in addition to creating a symbol version (as if <code class="code">"<var class="var">name2</var>@<var class="var">nodename</var>"</code> was used) the version will be also used to resolve <var class="var">name2</var> by the linker. </p> </dd> <dt>
+<span><code class="code">tainted_args</code><a class="copiable-link" href="#index-tainted_005fargs-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">tainted_args</code> attribute is used to specify that a function is called in a way that requires sanitization of its arguments, such as a system call in an operating system kernel. Such a function can be considered part of the “attack surface” of the program. The attribute can be used both on function declarations, and on field declarations containing function pointers. In the latter case, any function used as an initializer of such a callback field will be treated as being called with tainted arguments. </p> <p>The analyzer will pay particular attention to such functions when both <samp class="option">-fanalyzer</samp> and <samp class="option">-fanalyzer-checker=taint</samp> are supplied, potentially issuing warnings guarded by <samp class="option">-Wanalyzer-tainted-allocation-size</samp>, <samp class="option">-Wanalyzer-tainted-array-index</samp>, <samp class="option">-Wanalyzer-tainted-divisor</samp>, <samp class="option">-Wanalyzer-tainted-offset</samp>, and <samp class="option">-Wanalyzer-tainted-size</samp>. </p> </dd> <dt>
+<span><code class="code">target_clones (<var class="var">options</var>)</code><a class="copiable-link" href="#index-target_005fclones-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">target_clones</code> attribute is used to specify that a function be cloned into multiple versions compiled with different target options than specified on the command line. The supported options and restrictions are the same as for <code class="code">target</code> attribute. </p> <p>For instance, on an x86, you could compile a function with <code class="code">target_clones("sse4.1,avx")</code>. GCC creates two function clones, one compiled with <samp class="option">-msse4.1</samp> and another with <samp class="option">-mavx</samp>. </p> <p>On a PowerPC, you can compile a function with <code class="code">target_clones("cpu=power9,default")</code>. GCC will create two function clones, one compiled with <samp class="option">-mcpu=power9</samp> and another with the default options. GCC must be configured to use GLIBC 2.23 or newer in order to use the <code class="code">target_clones</code> attribute. </p> <p>It also creates a resolver function (see the <code class="code">ifunc</code> attribute above) that dynamically selects a clone suitable for current architecture. The resolver is created only if there is a usage of a function with <code class="code">target_clones</code> attribute. </p> <p>Note that any subsequent call of a function without <code class="code">target_clone</code> from a <code class="code">target_clone</code> caller will not lead to copying (target clone) of the called function. If you want to enforce such behaviour, we recommend declaring the calling function with the <code class="code">flatten</code> attribute? </p> </dd> <dt>
+<span><code class="code">unused</code><a class="copiable-link" href="#index-unused-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute, attached to a function, means that the function is meant to be possibly unused. GCC does not produce a warning for this function. </p> </dd> <dt>
+<span><code class="code">used</code><a class="copiable-link" href="#index-used-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly. </p> <p>When applied to a member function of a C++ class template, the attribute also means that the function is instantiated if the class itself is instantiated. </p> </dd> <dt>
+<span><code class="code">retain</code><a class="copiable-link" href="#index-retain-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>For ELF targets that support the GNU or FreeBSD OSABIs, this attribute will save the function from linker garbage collection. To support this behavior, functions that have not been placed in specific sections (e.g. by the <code class="code">section</code> attribute, or the <code class="code">-ffunction-sections</code> option), will be placed in new, unique sections. </p> <p>This additional functionality requires Binutils version 2.36 or later. </p> </dd> <dt>
+<span><code class="code">visibility ("<var class="var">visibility_type</var>")</code><a class="copiable-link" href="#index-visibility-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>This attribute affects the linkage of the declaration to which it is attached. It can be applied to variables (see <a class="pxref" href="common-variable-attributes">Common Variable Attributes</a>) and types (see <a class="pxref" href="common-type-attributes">Common Type Attributes</a>) as well as functions. </p> <p>There are four supported <var class="var">visibility_type</var> values: default, hidden, protected or internal visibility. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void __attribute__ ((visibility ("protected")))
+f () { /* <span class="r">Do something.</span> */; }
+int i __attribute__ ((visibility ("hidden")));</pre>
+</div> <p>The possible values of <var class="var">visibility_type</var> correspond to the visibility settings in the ELF gABI. </p> <dl class="table"> <dt><code class="code">default</code></dt> <dd>
+<p>Default visibility is the normal case for the object file format. This value is available for the visibility attribute to override other options that may change the assumed visibility of entities. </p> <p>On ELF, default visibility means that the declaration is visible to other modules and, in shared libraries, means that the declared entity may be overridden. </p> <p>On Darwin, default visibility means that the declaration is visible to other modules. </p> <p>Default visibility corresponds to “external linkage” in the language. </p> </dd> <dt><code class="code">hidden</code></dt> <dd>
+<p>Hidden visibility indicates that the entity declared has a new form of linkage, which we call “hidden linkage”. Two declarations of an object with hidden linkage refer to the same object if they are in the same shared object. </p> </dd> <dt><code class="code">internal</code></dt> <dd>
+<p>Internal visibility is like hidden visibility, but with additional processor specific semantics. Unless otherwise specified by the psABI, GCC defines internal visibility to mean that a function is <em class="emph">never</em> called from another module. Compare this with hidden functions which, while they cannot be referenced directly by other modules, can be referenced indirectly via function pointers. By indicating that a function cannot be called from outside the module, GCC may for instance omit the load of a PIC register since it is known that the calling function loaded the correct value. </p> </dd> <dt><code class="code">protected</code></dt> <dd>
+<p>Protected visibility is like default visibility except that it indicates that references within the defining module bind to the definition in that module. That is, the declared entity cannot be overridden by another module. </p> </dd> </dl> <p>All visibilities are supported on many, but not all, ELF targets (supported when the assembler supports the ‘<samp class="samp">.visibility</samp>’ pseudo-op). Default visibility is supported everywhere. Hidden visibility is supported on Darwin targets. </p> <p>The visibility attribute should be applied only to declarations that would otherwise have external linkage. The attribute should be applied consistently, so that the same entity should not be declared with different settings of the attribute. </p> <p>In C++, the visibility attribute applies to types as well as functions and objects, because in C++ types have linkage. A class must not have greater visibility than its non-static data member types and bases, and class members default to the visibility of their class. Also, a declaration without explicit visibility is limited to the visibility of its type. </p> <p>In C++, you can mark member functions and static member variables of a class with the visibility attribute. This is useful if you know a particular method or static member variable should only be used from one shared object; then you can mark it hidden while the rest of the class has default visibility. Care must be taken to avoid breaking the One Definition Rule; for example, it is usually not useful to mark an inline method as hidden without marking the whole class as hidden. </p> <p>A C++ namespace declaration can also have the visibility attribute. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">namespace nspace1 __attribute__ ((visibility ("protected")))
+{ /* <span class="r">Do something.</span> */; }</pre>
+</div> <p>This attribute applies only to the particular namespace body, not to other definitions of the same namespace; it is equivalent to using ‘<samp class="samp">#pragma GCC visibility</samp>’ before and after the namespace definition (see <a class="pxref" href="visibility-pragmas">Visibility Pragmas</a>). </p> <p>In C++, if a template argument has limited visibility, this restriction is implicitly propagated to the template instantiation. Otherwise, template instantiations and specializations default to the visibility of their template. </p> <p>If both the template and enclosing class have explicit visibility, the visibility from the template is used. </p> </dd> <dt>
+<span><code class="code">warn_unused_result</code><a class="copiable-link" href="#index-warn_005funused_005fresult-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">warn_unused_result</code> attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug, such as <code class="code">realloc</code>. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int fn () __attribute__ ((warn_unused_result));
+int foo ()
+{
+ if (fn () &lt; 0) return -1;
+ fn ();
+ return 0;
+}</pre>
+</div> <p>results in warning on line 5. </p> </dd> <dt>
+<span><code class="code">weak</code><a class="copiable-link" href="#index-weak-function-attribute"> ¶</a></span>
+</dt> <dd>
+<p>The <code class="code">weak</code> attribute causes a declaration of an external symbol to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions that can be overridden in user code, though it can also be used with non-function declarations. The overriding symbol must have the same type as the weak symbol. In addition, if it designates a variable it must also have the same size and alignment as the weak symbol. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker. </p> </dd> <dt>
+<span><code class="code">weakref</code><a class="copiable-link" href="#index-weakref-function-attribute"> ¶</a></span>
+</dt> <dt><code class="code">weakref ("<var class="var">target</var>")</code></dt> <dd>
+<p>The <code class="code">weakref</code> attribute marks a declaration as a weak reference. Without arguments, it should be accompanied by an <code class="code">alias</code> attribute naming the target symbol. Alternatively, <var class="var">target</var> may be given as an argument to <code class="code">weakref</code> itself, naming the target definition of the alias. The <var class="var">target</var> must have the same type as the declaration. In addition, if it designates a variable it must also have the same size and alignment as the declaration. In either form of the declaration <code class="code">weakref</code> implicitly marks the declared symbol as <code class="code">weak</code>. Without a <var class="var">target</var> given as an argument to <code class="code">weakref</code> or to <code class="code">alias</code>, <code class="code">weakref</code> is equivalent to <code class="code">weak</code> (in that case the declaration may be <code class="code">extern</code>). </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">/* Given the declaration: */
+extern int y (void);
+
+/* the following... */
+static int x (void) __attribute__ ((weakref ("y")));
+
+/* is equivalent to... */
+static int x (void) __attribute__ ((weakref, alias ("y")));
+
+/* or, alternatively, to... */
+static int x (void) __attribute__ ((weakref));
+static int x (void) __attribute__ ((alias ("y")));</pre>
+</div> <p>A weak reference is an alias that does not by itself require a definition to be given for the target symbol. If the target symbol is only referenced through weak references, then it becomes a <code class="code">weak</code> undefined symbol. If it is directly referenced, however, then such strong references prevail, and a definition is required for the symbol, not necessarily in the same translation unit. </p> <p>The effect is equivalent to moving all references to the alias to a separate translation unit, renaming the alias to the aliased symbol, declaring it as weak, compiling the two separate translation units and performing a link with relocatable output (i.e. <code class="code">ld -r</code>) on them. </p> <p>A declaration to which <code class="code">weakref</code> is attached and that is associated with a named <code class="code">target</code> must be <code class="code">static</code>. </p> </dd> <dt>
+<span><code class="code">zero_call_used_regs ("<var class="var">choice</var>")</code><a class="copiable-link" href="#index-zero_005fcall_005fused_005fregs-function-attribute"> ¶</a></span>
+</dt> <dd> <p>The <code class="code">zero_call_used_regs</code> attribute causes the compiler to zero a subset of all call-used registers<a class="footnote" id="DOCF7" href="#FOOT7"><sup>7</sup></a> at function return. This is used to increase program security by either mitigating Return-Oriented Programming (ROP) attacks or preventing information leakage through registers. </p> <p>In order to satisfy users with different security needs and control the run-time overhead at the same time, the <var class="var">choice</var> parameter provides a flexible way to choose the subset of the call-used registers to be zeroed. The three basic values of <var class="var">choice</var> are: </p> <ul class="itemize mark-bullet"> <li>‘<samp class="samp">skip</samp>’ doesn’t zero any call-used registers. </li>
+<li>‘<samp class="samp">used</samp>’ only zeros call-used registers that are used in the function. A “used” register is one whose content has been set or referenced in the function. </li>
+<li>‘<samp class="samp">all</samp>’ zeros all call-used registers. </li>
+</ul> <p>In addition to these three basic choices, it is possible to modify ‘<samp class="samp">used</samp>’ or ‘<samp class="samp">all</samp>’ as follows: </p> <ul class="itemize mark-bullet"> <li>Adding ‘<samp class="samp">-gpr</samp>’ restricts the zeroing to general-purpose registers. </li>
+<li>Adding ‘<samp class="samp">-arg</samp>’ restricts the zeroing to registers that can sometimes be used to pass function arguments. This includes all argument registers defined by the platform’s calling conversion, regardless of whether the function uses those registers for function arguments or not. </li>
+</ul> <p>The modifiers can be used individually or together. If they are used together, they must appear in the order above. </p> <p>The full list of <var class="var">choice</var>s is therefore: </p> <dl class="table"> <dt><code class="code">skip</code></dt> <dd>
+<p>doesn’t zero any call-used register. </p> </dd> <dt><code class="code">used</code></dt> <dd>
+<p>only zeros call-used registers that are used in the function. </p> </dd> <dt><code class="code">used-gpr</code></dt> <dd>
+<p>only zeros call-used general purpose registers that are used in the function. </p> </dd> <dt><code class="code">used-arg</code></dt> <dd>
+<p>only zeros call-used registers that are used in the function and pass arguments. </p> </dd> <dt><code class="code">used-gpr-arg</code></dt> <dd>
+<p>only zeros call-used general purpose registers that are used in the function and pass arguments. </p> </dd> <dt><code class="code">all</code></dt> <dd>
+<p>zeros all call-used registers. </p> </dd> <dt><code class="code">all-gpr</code></dt> <dd>
+<p>zeros all call-used general purpose registers. </p> </dd> <dt><code class="code">all-arg</code></dt> <dd>
+<p>zeros all call-used registers that pass arguments. </p> </dd> <dt><code class="code">all-gpr-arg</code></dt> <dd><p>zeros all call-used general purpose registers that pass arguments. </p></dd> </dl> <p>Of this list, ‘<samp class="samp">used-arg</samp>’, ‘<samp class="samp">used-gpr-arg</samp>’, ‘<samp class="samp">all-arg</samp>’, and ‘<samp class="samp">all-gpr-arg</samp>’ are mainly used for ROP mitigation. </p> <p>The default for the attribute is controlled by <samp class="option">-fzero-call-used-regs</samp>. </p>
+</dd> </dl> </div> <div class="footnotes-segment"> <h1 class="footnotes-heading">Footnotes</h1> <h2 class="footnote-body-heading"><a id="FOOT7" href="#DOCF7">(7)</a></h2> <p>A “call-used” register is a register whose contents can be changed by a function call; therefore, a caller cannot assume that the register has the same contents on return from the function as it had before calling the function. Such registers are also called “call-clobbered”, “caller-saved”, or “volatile”.</p> </div> <div class="nav-panel"> <p> Next: <a href="aarch64-function-attributes">AArch64 Function Attributes</a>, Up: <a href="function-attributes">Declaring Attributes of Functions</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/Common-Function-Attributes.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Common-Function-Attributes.html</a>
+ </p>
+</div>