Common-Function-Attributes.html 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Common Function Attributes</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Common Function Attributes">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Common Function Attributes">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Function-Attributes.html#Function-Attributes" rel="up" title="Function Attributes">
  30. <link href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" rel="next" title="AArch64 Function Attributes">
  31. <link href="Function-Attributes.html#Function-Attributes" rel="prev" title="Function Attributes">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Common-Function-Attributes"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" accesskey="n" rel="next">AArch64 Function Attributes</a>, Up: <a href="Function-Attributes.html#Function-Attributes" accesskey="u" rel="up">Function Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Common-Function-Attributes-1"></a>
  69. <h4 class="subsection">6.31.1 Common Function Attributes</h4>
  70. <p>The following attributes are supported on most targets.
  71. </p>
  72. <dl compact="compact">
  73. <dt><code>alias (&quot;<var>target</var>&quot;)</code></dt>
  74. <dd><a name="index-alias-function-attribute"></a>
  75. <p>The <code>alias</code> attribute causes the declaration to be emitted as an
  76. alias for another symbol, which must be specified. For instance,
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample">void __f () { /* <span class="roman">Do something.</span> */; }
  80. void f () __attribute__ ((weak, alias (&quot;__f&quot;)));
  81. </pre></div>
  82. <p>defines &lsquo;<samp>f</samp>&rsquo; to be a weak alias for &lsquo;<samp>__f</samp>&rsquo;. In C++, the
  83. mangled name for the target must be used. It is an error if &lsquo;<samp>__f</samp>&rsquo;
  84. is not defined in the same translation unit.
  85. </p>
  86. <p>This attribute requires assembler and object file support,
  87. and may not be available on all targets.
  88. </p>
  89. </dd>
  90. <dt><code>aligned (<var>alignment</var>)</code></dt>
  91. <dd><a name="index-aligned-function-attribute"></a>
  92. <p>This attribute specifies a minimum alignment for the function,
  93. measured in bytes.
  94. </p>
  95. <p>You cannot use this attribute to decrease the alignment of a function,
  96. only to increase it. However, when you explicitly specify a function
  97. alignment this overrides the effect of the
  98. <samp>-falign-functions</samp> (see <a href="Optimize-Options.html#Optimize-Options">Optimize Options</a>) option for this
  99. function.
  100. </p>
  101. <p>Note that the effectiveness of <code>aligned</code> attributes may be
  102. limited by inherent limitations in your linker. On many systems, the
  103. linker is only able to arrange for functions to be aligned up to a
  104. certain maximum alignment. (For some linkers, the maximum supported
  105. alignment may be very very small.) See your linker documentation for
  106. further information.
  107. </p>
  108. <p>The <code>aligned</code> attribute can also be used for variables and fields
  109. (see <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>.)
  110. </p>
  111. </dd>
  112. <dt><code>alloc_align</code></dt>
  113. <dd><a name="index-alloc_005falign-function-attribute"></a>
  114. <p>The <code>alloc_align</code> attribute is used to tell the compiler that the
  115. function return value points to memory, where the returned pointer minimum
  116. alignment is given by one of the functions parameters. GCC uses this
  117. information to improve pointer alignment analysis.
  118. </p>
  119. <p>The function parameter denoting the allocated alignment is specified by
  120. one integer argument, whose number is the argument of the attribute.
  121. Argument numbering starts at one.
  122. </p>
  123. <p>For instance,
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">void* my_memalign(size_t, size_t) __attribute__((alloc_align(1)))
  127. </pre></div>
  128. <p>declares that <code>my_memalign</code> returns memory with minimum alignment
  129. given by parameter 1.
  130. </p>
  131. </dd>
  132. <dt><code>alloc_size</code></dt>
  133. <dd><a name="index-alloc_005fsize-function-attribute"></a>
  134. <p>The <code>alloc_size</code> attribute is used to tell the compiler that the
  135. function return value points to memory, where the size is given by
  136. one or two of the functions parameters. GCC uses this
  137. information to improve the correctness of <code>__builtin_object_size</code>.
  138. </p>
  139. <p>The function parameter(s) denoting the allocated size are specified by
  140. one or two integer arguments supplied to the attribute. The allocated size
  141. is either the value of the single function argument specified or the product
  142. of the two function arguments specified. Argument numbering starts at
  143. one.
  144. </p>
  145. <p>For instance,
  146. </p>
  147. <div class="smallexample">
  148. <pre class="smallexample">void* my_calloc(size_t, size_t) __attribute__((alloc_size(1,2)))
  149. void* my_realloc(void*, size_t) __attribute__((alloc_size(2)))
  150. </pre></div>
  151. <p>declares that <code>my_calloc</code> returns memory of the size given by
  152. the product of parameter 1 and 2 and that <code>my_realloc</code> returns memory
  153. of the size given by parameter 2.
  154. </p>
  155. </dd>
  156. <dt><code>always_inline</code></dt>
  157. <dd><a name="index-always_005finline-function-attribute"></a>
  158. <p>Generally, functions are not inlined unless optimization is specified.
  159. For functions declared inline, this attribute inlines the function
  160. independent of any restrictions that otherwise apply to inlining.
  161. Failure to inline such a function is diagnosed as an error.
  162. Note that if such a function is called indirectly the compiler may
  163. or may not inline it depending on optimization level and a failure
  164. to inline an indirect call may or may not be diagnosed.
  165. </p>
  166. </dd>
  167. <dt><code>artificial</code></dt>
  168. <dd><a name="index-artificial-function-attribute"></a>
  169. <p>This attribute is useful for small inline wrappers that if possible
  170. should appear during debugging as a unit. Depending on the debug
  171. info format it either means marking the function as artificial
  172. or using the caller location for all instructions within the inlined
  173. body.
  174. </p>
  175. </dd>
  176. <dt><code>assume_aligned</code></dt>
  177. <dd><a name="index-assume_005faligned-function-attribute"></a>
  178. <p>The <code>assume_aligned</code> attribute is used to tell the compiler that the
  179. function return value points to memory, where the returned pointer minimum
  180. alignment is given by the first argument.
  181. If the attribute has two arguments, the second argument is misalignment offset.
  182. </p>
  183. <p>For instance
  184. </p>
  185. <div class="smallexample">
  186. <pre class="smallexample">void* my_alloc1(size_t) __attribute__((assume_aligned(16)))
  187. void* my_alloc2(size_t) __attribute__((assume_aligned(32, 8)))
  188. </pre></div>
  189. <p>declares that <code>my_alloc1</code> returns 16-byte aligned pointer and
  190. that <code>my_alloc2</code> returns a pointer whose value modulo 32 is equal
  191. to 8.
  192. </p>
  193. </dd>
  194. <dt><code>bnd_instrument</code></dt>
  195. <dd><a name="index-bnd_005finstrument-function-attribute"></a>
  196. <p>The <code>bnd_instrument</code> attribute on functions is used to inform the
  197. compiler that the function should be instrumented when compiled
  198. with the <samp>-fchkp-instrument-marked-only</samp> option.
  199. </p>
  200. </dd>
  201. <dt><code>bnd_legacy</code></dt>
  202. <dd><a name="index-bnd_005flegacy-function-attribute"></a>
  203. <a name="index-Pointer-Bounds-Checker-attributes"></a>
  204. <p>The <code>bnd_legacy</code> attribute on functions is used to inform the
  205. compiler that the function should not be instrumented when compiled
  206. with the <samp>-fcheck-pointer-bounds</samp> option.
  207. </p>
  208. </dd>
  209. <dt><code>cold</code></dt>
  210. <dd><a name="index-cold-function-attribute"></a>
  211. <p>The <code>cold</code> attribute on functions is used to inform the compiler that
  212. the function is unlikely to be executed. The function is optimized for
  213. size rather than speed and on many targets it is placed into a special
  214. subsection of the text section so all cold functions appear close together,
  215. improving code locality of non-cold parts of program. The paths leading
  216. to calls of cold functions within code are marked as unlikely by the branch
  217. prediction mechanism. It is thus useful to mark functions used to handle
  218. unlikely conditions, such as <code>perror</code>, as cold to improve optimization
  219. of hot functions that do call marked functions in rare occasions.
  220. </p>
  221. <p>When profile feedback is available, via <samp>-fprofile-use</samp>, cold functions
  222. are automatically detected and this attribute is ignored.
  223. </p>
  224. </dd>
  225. <dt><code>const</code></dt>
  226. <dd><a name="index-const-function-attribute"></a>
  227. <a name="index-functions-that-have-no-side-effects"></a>
  228. <p>Many functions do not examine any values except their arguments, and
  229. have no effects except the return value. Basically this is just slightly
  230. more strict class than the <code>pure</code> attribute below, since function is not
  231. allowed to read global memory.
  232. </p>
  233. <a name="index-pointer-arguments"></a>
  234. <p>Note that a function that has pointer arguments and examines the data
  235. pointed to must <em>not</em> be declared <code>const</code>. Likewise, a
  236. function that calls a non-<code>const</code> function usually must not be
  237. <code>const</code>. It does not make sense for a <code>const</code> function to
  238. return <code>void</code>.
  239. </p>
  240. </dd>
  241. <dt><code>constructor</code></dt>
  242. <dt><code>destructor</code></dt>
  243. <dt><code>constructor (<var>priority</var>)</code></dt>
  244. <dt><code>destructor (<var>priority</var>)</code></dt>
  245. <dd><a name="index-constructor-function-attribute"></a>
  246. <a name="index-destructor-function-attribute"></a>
  247. <p>The <code>constructor</code> attribute causes the function to be called
  248. automatically before execution enters <code>main ()</code>. Similarly, the
  249. <code>destructor</code> attribute causes the function to be called
  250. automatically after <code>main ()</code> completes or <code>exit ()</code> is
  251. called. Functions with these attributes are useful for
  252. initializing data that is used implicitly during the execution of
  253. the program.
  254. </p>
  255. <p>You may provide an optional integer priority to control the order in
  256. which constructor and destructor functions are run. A constructor
  257. with a smaller priority number runs before a constructor with a larger
  258. priority number; the opposite relationship holds for destructors. So,
  259. if you have a constructor that allocates a resource and a destructor
  260. that deallocates the same resource, both functions typically have the
  261. same priority. The priorities for constructor and destructor
  262. functions are the same as those specified for namespace-scope C++
  263. objects (see <a href="C_002b_002b-Attributes.html#C_002b_002b-Attributes">C++ Attributes</a>). However, at present, the order in which
  264. constructors for C++ objects with static storage duration and functions
  265. decorated with attribute <code>constructor</code> are invoked is unspecified.
  266. In mixed declarations, attribute <code>init_priority</code> can be used to
  267. impose a specific ordering.
  268. </p>
  269. </dd>
  270. <dt><code>deprecated</code></dt>
  271. <dt><code>deprecated (<var>msg</var>)</code></dt>
  272. <dd><a name="index-deprecated-function-attribute"></a>
  273. <p>The <code>deprecated</code> attribute results in a warning if the function
  274. is used anywhere in the source file. This is useful when identifying
  275. functions that are expected to be removed in a future version of a
  276. program. The warning also includes the location of the declaration
  277. of the deprecated function, to enable users to easily find further
  278. information about why the function is deprecated, or what they should
  279. do instead. Note that the warnings only occurs for uses:
  280. </p>
  281. <div class="smallexample">
  282. <pre class="smallexample">int old_fn () __attribute__ ((deprecated));
  283. int old_fn ();
  284. int (*fn_ptr)() = old_fn;
  285. </pre></div>
  286. <p>results in a warning on line 3 but not line 2. The optional <var>msg</var>
  287. argument, which must be a string, is printed in the warning if
  288. present.
  289. </p>
  290. <p>The <code>deprecated</code> attribute can also be used for variables and
  291. types (see <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>, see <a href="Type-Attributes.html#Type-Attributes">Type Attributes</a>.)
  292. </p>
  293. </dd>
  294. <dt><code>error (&quot;<var>message</var>&quot;)</code></dt>
  295. <dt><code>warning (&quot;<var>message</var>&quot;)</code></dt>
  296. <dd><a name="index-error-function-attribute"></a>
  297. <a name="index-warning-function-attribute"></a>
  298. <p>If the <code>error</code> or <code>warning</code> attribute
  299. is used on a function declaration and a call to such a function
  300. is not eliminated through dead code elimination or other optimizations,
  301. an error or warning (respectively) that includes <var>message</var> is diagnosed.
  302. This is useful
  303. for compile-time checking, especially together with <code>__builtin_constant_p</code>
  304. and inline functions where checking the inline function arguments is not
  305. possible through <code>extern char [(condition) ? 1 : -1];</code> tricks.
  306. </p>
  307. <p>While it is possible to leave the function undefined and thus invoke
  308. a link failure (to define the function with
  309. a message in <code>.gnu.warning*</code> section),
  310. when using these attributes the problem is diagnosed
  311. earlier and with exact location of the call even in presence of inline
  312. functions or when not emitting debugging information.
  313. </p>
  314. </dd>
  315. <dt><code>externally_visible</code></dt>
  316. <dd><a name="index-externally_005fvisible-function-attribute"></a>
  317. <p>This attribute, attached to a global variable or function, nullifies
  318. the effect of the <samp>-fwhole-program</samp> command-line option, so the
  319. object remains visible outside the current compilation unit.
  320. </p>
  321. <p>If <samp>-fwhole-program</samp> is used together with <samp>-flto</samp> and
  322. <code>gold</code> is used as the linker plugin,
  323. <code>externally_visible</code> attributes are automatically added to functions
  324. (not variable yet due to a current <code>gold</code> issue)
  325. that are accessed outside of LTO objects according to resolution file
  326. produced by <code>gold</code>.
  327. For other linkers that cannot generate resolution file,
  328. explicit <code>externally_visible</code> attributes are still necessary.
  329. </p>
  330. </dd>
  331. <dt><code>flatten</code></dt>
  332. <dd><a name="index-flatten-function-attribute"></a>
  333. <p>Generally, inlining into a function is limited. For a function marked with
  334. this attribute, every call inside this function is inlined, if possible.
  335. Whether the function itself is considered for inlining depends on its size and
  336. the current inlining parameters.
  337. </p>
  338. </dd>
  339. <dt><code>format (<var>archetype</var>, <var>string-index</var>, <var>first-to-check</var>)</code></dt>
  340. <dd><a name="index-format-function-attribute"></a>
  341. <a name="index-functions-with-printf_002c-scanf_002c-strftime-or-strfmon-style-arguments"></a>
  342. <a name="index-Wformat-3"></a>
  343. <p>The <code>format</code> attribute specifies that a function takes <code>printf</code>,
  344. <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> style arguments that
  345. should be type-checked against a format string. For example, the
  346. declaration:
  347. </p>
  348. <div class="smallexample">
  349. <pre class="smallexample">extern int
  350. my_printf (void *my_object, const char *my_format, ...)
  351. __attribute__ ((format (printf, 2, 3)));
  352. </pre></div>
  353. <p>causes the compiler to check the arguments in calls to <code>my_printf</code>
  354. for consistency with the <code>printf</code> style format string argument
  355. <code>my_format</code>.
  356. </p>
  357. <p>The parameter <var>archetype</var> determines how the format string is
  358. interpreted, and should be <code>printf</code>, <code>scanf</code>, <code>strftime</code>,
  359. <code>gnu_printf</code>, <code>gnu_scanf</code>, <code>gnu_strftime</code> or
  360. <code>strfmon</code>. (You can also use <code>__printf__</code>,
  361. <code>__scanf__</code>, <code>__strftime__</code> or <code>__strfmon__</code>.) On
  362. MinGW targets, <code>ms_printf</code>, <code>ms_scanf</code>, and
  363. <code>ms_strftime</code> are also present.
  364. <var>archetype</var> values such as <code>printf</code> refer to the formats accepted
  365. by the system&rsquo;s C runtime library,
  366. while values prefixed with &lsquo;<samp>gnu_</samp>&rsquo; always refer
  367. to the formats accepted by the GNU C Library. On Microsoft Windows
  368. targets, values prefixed with &lsquo;<samp>ms_</samp>&rsquo; refer to the formats accepted by the
  369. <samp>msvcrt.dll</samp> library.
  370. The parameter <var>string-index</var>
  371. specifies which argument is the format string argument (starting
  372. from 1), while <var>first-to-check</var> is the number of the first
  373. argument to check against the format string. For functions
  374. where the arguments are not available to be checked (such as
  375. <code>vprintf</code>), specify the third parameter as zero. In this case the
  376. compiler only checks the format string for consistency. For
  377. <code>strftime</code> formats, the third parameter is required to be zero.
  378. Since non-static C++ methods have an implicit <code>this</code> argument, the
  379. arguments of such methods should be counted from two, not one, when
  380. giving values for <var>string-index</var> and <var>first-to-check</var>.
  381. </p>
  382. <p>In the example above, the format string (<code>my_format</code>) is the second
  383. argument of the function <code>my_print</code>, and the arguments to check
  384. start with the third argument, so the correct parameters for the format
  385. attribute are 2 and 3.
  386. </p>
  387. <a name="index-ffreestanding-3"></a>
  388. <a name="index-fno_002dbuiltin-2"></a>
  389. <p>The <code>format</code> attribute allows you to identify your own functions
  390. that take format strings as arguments, so that GCC can check the
  391. calls to these functions for errors. The compiler always (unless
  392. <samp>-ffreestanding</samp> or <samp>-fno-builtin</samp> is used) checks formats
  393. for the standard library functions <code>printf</code>, <code>fprintf</code>,
  394. <code>sprintf</code>, <code>scanf</code>, <code>fscanf</code>, <code>sscanf</code>, <code>strftime</code>,
  395. <code>vprintf</code>, <code>vfprintf</code> and <code>vsprintf</code> whenever such
  396. warnings are requested (using <samp>-Wformat</samp>), so there is no need to
  397. modify the header file <samp>stdio.h</samp>. In C99 mode, the functions
  398. <code>snprintf</code>, <code>vsnprintf</code>, <code>vscanf</code>, <code>vfscanf</code> and
  399. <code>vsscanf</code> are also checked. Except in strictly conforming C
  400. standard modes, the X/Open function <code>strfmon</code> is also checked as
  401. are <code>printf_unlocked</code> and <code>fprintf_unlocked</code>.
  402. See <a href="C-Dialect-Options.html#C-Dialect-Options">Options Controlling C Dialect</a>.
  403. </p>
  404. <p>For Objective-C dialects, <code>NSString</code> (or <code>__NSString__</code>) is
  405. recognized in the same context. Declarations including these format attributes
  406. are parsed for correct syntax, however the result of checking of such format
  407. strings is not yet defined, and is not carried out by this version of the
  408. compiler.
  409. </p>
  410. <p>The target may also provide additional types of format checks.
  411. See <a href="Target-Format-Checks.html#Target-Format-Checks">Format Checks Specific to Particular
  412. Target Machines</a>.
  413. </p>
  414. </dd>
  415. <dt><code>format_arg (<var>string-index</var>)</code></dt>
  416. <dd><a name="index-format_005farg-function-attribute"></a>
  417. <a name="index-Wformat_002dnonliteral-1"></a>
  418. <p>The <code>format_arg</code> attribute specifies that a function takes a format
  419. string for a <code>printf</code>, <code>scanf</code>, <code>strftime</code> or
  420. <code>strfmon</code> style function and modifies it (for example, to translate
  421. it into another language), so the result can be passed to a
  422. <code>printf</code>, <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> style
  423. function (with the remaining arguments to the format function the same
  424. as they would have been for the unmodified string). For example, the
  425. declaration:
  426. </p>
  427. <div class="smallexample">
  428. <pre class="smallexample">extern char *
  429. my_dgettext (char *my_domain, const char *my_format)
  430. __attribute__ ((format_arg (2)));
  431. </pre></div>
  432. <p>causes the compiler to check the arguments in calls to a <code>printf</code>,
  433. <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> type function, whose
  434. format string argument is a call to the <code>my_dgettext</code> function, for
  435. consistency with the format string argument <code>my_format</code>. If the
  436. <code>format_arg</code> attribute had not been specified, all the compiler
  437. could tell in such calls to format functions would be that the format
  438. string argument is not constant; this would generate a warning when
  439. <samp>-Wformat-nonliteral</samp> is used, but the calls could not be checked
  440. without the attribute.
  441. </p>
  442. <p>The parameter <var>string-index</var> specifies which argument is the format
  443. string argument (starting from one). Since non-static C++ methods have
  444. an implicit <code>this</code> argument, the arguments of such methods should
  445. be counted from two.
  446. </p>
  447. <p>The <code>format_arg</code> attribute allows you to identify your own
  448. functions that modify format strings, so that GCC can check the
  449. calls to <code>printf</code>, <code>scanf</code>, <code>strftime</code> or <code>strfmon</code>
  450. type function whose operands are a call to one of your own function.
  451. The compiler always treats <code>gettext</code>, <code>dgettext</code>, and
  452. <code>dcgettext</code> in this manner except when strict ISO C support is
  453. requested by <samp>-ansi</samp> or an appropriate <samp>-std</samp> option, or
  454. <samp>-ffreestanding</samp> or <samp>-fno-builtin</samp>
  455. is used. See <a href="C-Dialect-Options.html#C-Dialect-Options">Options
  456. Controlling C Dialect</a>.
  457. </p>
  458. <p>For Objective-C dialects, the <code>format-arg</code> attribute may refer to an
  459. <code>NSString</code> reference for compatibility with the <code>format</code> attribute
  460. above.
  461. </p>
  462. <p>The target may also allow additional types in <code>format-arg</code> attributes.
  463. See <a href="Target-Format-Checks.html#Target-Format-Checks">Format Checks Specific to Particular
  464. Target Machines</a>.
  465. </p>
  466. </dd>
  467. <dt><code>gnu_inline</code></dt>
  468. <dd><a name="index-gnu_005finline-function-attribute"></a>
  469. <p>This attribute should be used with a function that is also declared
  470. with the <code>inline</code> keyword. It directs GCC to treat the function
  471. as if it were defined in gnu90 mode even when compiling in C99 or
  472. gnu99 mode.
  473. </p>
  474. <p>If the function is declared <code>extern</code>, then this definition of the
  475. function is used only for inlining. In no case is the function
  476. compiled as a standalone function, not even if you take its address
  477. explicitly. Such an address becomes an external reference, as if you
  478. had only declared the function, and had not defined it. This has
  479. almost the effect of a macro. The way to use this is to put a
  480. function definition in a header file with this attribute, and put
  481. another copy of the function, without <code>extern</code>, in a library
  482. file. The definition in the header file causes most calls to the
  483. function to be inlined. If any uses of the function remain, they
  484. refer to the single copy in the library. Note that the two
  485. definitions of the functions need not be precisely the same, although
  486. if they do not have the same effect your program may behave oddly.
  487. </p>
  488. <p>In C, if the function is neither <code>extern</code> nor <code>static</code>, then
  489. the function is compiled as a standalone function, as well as being
  490. inlined where possible.
  491. </p>
  492. <p>This is how GCC traditionally handled functions declared
  493. <code>inline</code>. Since ISO C99 specifies a different semantics for
  494. <code>inline</code>, this function attribute is provided as a transition
  495. measure and as a useful feature in its own right. This attribute is
  496. available in GCC 4.1.3 and later. It is available if either of the
  497. preprocessor macros <code>__GNUC_GNU_INLINE__</code> or
  498. <code>__GNUC_STDC_INLINE__</code> are defined. See <a href="Inline.html#Inline">An Inline
  499. Function is As Fast As a Macro</a>.
  500. </p>
  501. <p>In C++, this attribute does not depend on <code>extern</code> in any way,
  502. but it still requires the <code>inline</code> keyword to enable its special
  503. behavior.
  504. </p>
  505. </dd>
  506. <dt><code>hot</code></dt>
  507. <dd><a name="index-hot-function-attribute"></a>
  508. <p>The <code>hot</code> attribute on a function is used to inform the compiler that
  509. the function is a hot spot of the compiled program. The function is
  510. optimized more aggressively and on many targets it is placed into a special
  511. subsection of the text section so all hot functions appear close together,
  512. improving locality.
  513. </p>
  514. <p>When profile feedback is available, via <samp>-fprofile-use</samp>, hot functions
  515. are automatically detected and this attribute is ignored.
  516. </p>
  517. </dd>
  518. <dt><code>ifunc (&quot;<var>resolver</var>&quot;)</code></dt>
  519. <dd><a name="index-ifunc-function-attribute"></a>
  520. <a name="index-indirect-functions"></a>
  521. <a name="index-functions-that-are-dynamically-resolved"></a>
  522. <p>The <code>ifunc</code> attribute is used to mark a function as an indirect
  523. function using the STT_GNU_IFUNC symbol type extension to the ELF
  524. standard. This allows the resolution of the symbol value to be
  525. determined dynamically at load time, and an optimized version of the
  526. routine can be selected for the particular processor or other system
  527. characteristics determined then. To use this attribute, first define
  528. the implementation functions available, and a resolver function that
  529. returns a pointer to the selected implementation function. The
  530. implementation functions&rsquo; declarations must match the API of the
  531. function being implemented, the resolver&rsquo;s declaration is be a
  532. function returning pointer to void function returning void:
  533. </p>
  534. <div class="smallexample">
  535. <pre class="smallexample">void *my_memcpy (void *dst, const void *src, size_t len)
  536. {
  537. &hellip;
  538. }
  539. static void (*resolve_memcpy (void)) (void)
  540. {
  541. return my_memcpy; // we'll just always select this routine
  542. }
  543. </pre></div>
  544. <p>The exported header file declaring the function the user calls would
  545. contain:
  546. </p>
  547. <div class="smallexample">
  548. <pre class="smallexample">extern void *memcpy (void *, const void *, size_t);
  549. </pre></div>
  550. <p>allowing the user to call this as a regular function, unaware of the
  551. implementation. Finally, the indirect function needs to be defined in
  552. the same translation unit as the resolver function:
  553. </p>
  554. <div class="smallexample">
  555. <pre class="smallexample">void *memcpy (void *, const void *, size_t)
  556. __attribute__ ((ifunc (&quot;resolve_memcpy&quot;)));
  557. </pre></div>
  558. <p>Indirect functions cannot be weak. Binutils version 2.20.1 or higher
  559. and GNU C Library version 2.11.1 are required to use this feature.
  560. </p>
  561. </dd>
  562. <dt><code>interrupt</code></dt>
  563. <dt><code>interrupt_handler</code></dt>
  564. <dd><p>Many GCC back ends support attributes to indicate that a function is
  565. an interrupt handler, which tells the compiler to generate function
  566. entry and exit sequences that differ from those from regular
  567. functions. The exact syntax and behavior are target-specific;
  568. refer to the following subsections for details.
  569. </p>
  570. </dd>
  571. <dt><code>leaf</code></dt>
  572. <dd><a name="index-leaf-function-attribute"></a>
  573. <p>Calls to external functions with this attribute must return to the
  574. current compilation unit only by return or by exception handling. In
  575. particular, a leaf function is not allowed to invoke callback functions
  576. passed to it from the current compilation unit, directly call functions
  577. exported by the unit, or <code>longjmp</code> into the unit. Leaf functions
  578. might still call functions from other compilation units and thus they
  579. are not necessarily leaf in the sense that they contain no function
  580. calls at all.
  581. </p>
  582. <p>The attribute is intended for library functions to improve dataflow
  583. analysis. The compiler takes the hint that any data not escaping the
  584. current compilation unit cannot be used or modified by the leaf
  585. function. For example, the <code>sin</code> function is a leaf function, but
  586. <code>qsort</code> is not.
  587. </p>
  588. <p>Note that leaf functions might indirectly run a signal handler defined
  589. in the current compilation unit that uses static variables. Similarly,
  590. when lazy symbol resolution is in effect, leaf functions might invoke
  591. indirect functions whose resolver function or implementation function is
  592. defined in the current compilation unit and uses static variables. There
  593. is no standard-compliant way to write such a signal handler, resolver
  594. function, or implementation function, and the best that you can do is to
  595. remove the <code>leaf</code> attribute or mark all such static variables
  596. <code>volatile</code>. Lastly, for ELF-based systems that support symbol
  597. interposition, care should be taken that functions defined in the
  598. current compilation unit do not unexpectedly interpose other symbols
  599. based on the defined standards mode and defined feature test macros;
  600. otherwise an inadvertent callback would be added.
  601. </p>
  602. <p>The attribute has no effect on functions defined within the current
  603. compilation unit. This is to allow easy merging of multiple compilation
  604. units into one, for example, by using the link-time optimization. For
  605. this reason the attribute is not allowed on types to annotate indirect
  606. calls.
  607. </p>
  608. </dd>
  609. <dt><code>malloc</code></dt>
  610. <dd><a name="index-malloc-function-attribute"></a>
  611. <a name="index-functions-that-behave-like-malloc"></a>
  612. <p>This tells the compiler that a function is <code>malloc</code>-like, i.e.,
  613. that the pointer <var>P</var> returned by the function cannot alias any
  614. other pointer valid when the function returns, and moreover no
  615. pointers to valid objects occur in any storage addressed by <var>P</var>.
  616. </p>
  617. <p>Using this attribute can improve optimization. Functions like
  618. <code>malloc</code> and <code>calloc</code> have this property because they return
  619. a pointer to uninitialized or zeroed-out storage. However, functions
  620. like <code>realloc</code> do not have this property, as they can return a
  621. pointer to storage containing pointers.
  622. </p>
  623. </dd>
  624. <dt><code>no_icf</code></dt>
  625. <dd><a name="index-no_005ficf-function-attribute"></a>
  626. <p>This function attribute prevents a functions from being merged with another
  627. semantically equivalent function.
  628. </p>
  629. </dd>
  630. <dt><code>no_instrument_function</code></dt>
  631. <dd><a name="index-no_005finstrument_005ffunction-function-attribute"></a>
  632. <a name="index-finstrument_002dfunctions-1"></a>
  633. <p>If <samp>-finstrument-functions</samp> is given, profiling function calls are
  634. generated at entry and exit of most user-compiled functions.
  635. Functions with this attribute are not so instrumented.
  636. </p>
  637. </dd>
  638. <dt><code>no_profile_instrument_function</code></dt>
  639. <dd><a name="index-no_005fprofile_005finstrument_005ffunction-function-attribute"></a>
  640. <p>The <code>no_profile_instrument_function</code> attribute on functions is used
  641. to inform the compiler that it should not process any profile feedback based
  642. optimization code instrumentation.
  643. </p>
  644. </dd>
  645. <dt><code>no_reorder</code></dt>
  646. <dd><a name="index-no_005freorder-function-attribute"></a>
  647. <p>Do not reorder functions or variables marked <code>no_reorder</code>
  648. against each other or top level assembler statements the executable.
  649. The actual order in the program will depend on the linker command
  650. line. Static variables marked like this are also not removed.
  651. This has a similar effect
  652. as the <samp>-fno-toplevel-reorder</samp> option, but only applies to the
  653. marked symbols.
  654. </p>
  655. </dd>
  656. <dt><code>no_sanitize_address</code></dt>
  657. <dt><code>no_address_safety_analysis</code></dt>
  658. <dd><a name="index-no_005fsanitize_005faddress-function-attribute"></a>
  659. <p>The <code>no_sanitize_address</code> attribute on functions is used
  660. to inform the compiler that it should not instrument memory accesses
  661. in the function when compiling with the <samp>-fsanitize=address</samp> option.
  662. The <code>no_address_safety_analysis</code> is a deprecated alias of the
  663. <code>no_sanitize_address</code> attribute, new code should use
  664. <code>no_sanitize_address</code>.
  665. </p>
  666. </dd>
  667. <dt><code>no_sanitize_thread</code></dt>
  668. <dd><a name="index-no_005fsanitize_005fthread-function-attribute"></a>
  669. <p>The <code>no_sanitize_thread</code> attribute on functions is used
  670. to inform the compiler that it should not instrument memory accesses
  671. in the function when compiling with the <samp>-fsanitize=thread</samp> option.
  672. </p>
  673. </dd>
  674. <dt><code>no_sanitize_undefined</code></dt>
  675. <dd><a name="index-no_005fsanitize_005fundefined-function-attribute"></a>
  676. <p>The <code>no_sanitize_undefined</code> attribute on functions is used
  677. to inform the compiler that it should not check for undefined behavior
  678. in the function when compiling with the <samp>-fsanitize=undefined</samp> option.
  679. </p>
  680. </dd>
  681. <dt><code>no_split_stack</code></dt>
  682. <dd><a name="index-no_005fsplit_005fstack-function-attribute"></a>
  683. <a name="index-fsplit_002dstack-1"></a>
  684. <p>If <samp>-fsplit-stack</samp> is given, functions have a small
  685. prologue which decides whether to split the stack. Functions with the
  686. <code>no_split_stack</code> attribute do not have that prologue, and thus
  687. may run with only a small amount of stack space available.
  688. </p>
  689. </dd>
  690. <dt><code>no_stack_limit</code></dt>
  691. <dd><a name="index-no_005fstack_005flimit-function-attribute"></a>
  692. <p>This attribute locally overrides the <samp>-fstack-limit-register</samp>
  693. and <samp>-fstack-limit-symbol</samp> command-line options; it has the effect
  694. of disabling stack limit checking in the function it applies to.
  695. </p>
  696. </dd>
  697. <dt><code>noclone</code></dt>
  698. <dd><a name="index-noclone-function-attribute"></a>
  699. <p>This function attribute prevents a function from being considered for
  700. cloning&mdash;a mechanism that produces specialized copies of functions
  701. and which is (currently) performed by interprocedural constant
  702. propagation.
  703. </p>
  704. </dd>
  705. <dt><code>noinline</code></dt>
  706. <dd><a name="index-noinline-function-attribute"></a>
  707. <p>This function attribute prevents a function from being considered for
  708. inlining.
  709. If the function does not have side-effects, there are optimizations
  710. other than inlining that cause function calls to be optimized away,
  711. although the function call is live. To keep such calls from being
  712. optimized away, put
  713. </p><div class="smallexample">
  714. <pre class="smallexample">asm (&quot;&quot;);
  715. </pre></div>
  716. <p>(see <a href="Extended-Asm.html#Extended-Asm">Extended Asm</a>) in the called function, to serve as a special
  717. side-effect.
  718. </p>
  719. </dd>
  720. <dt><code>nonnull (<var>arg-index</var>, &hellip;)</code></dt>
  721. <dd><a name="index-nonnull-function-attribute"></a>
  722. <a name="index-functions-with-non_002dnull-pointer-arguments"></a>
  723. <p>The <code>nonnull</code> attribute specifies that some function parameters should
  724. be non-null pointers. For instance, the declaration:
  725. </p>
  726. <div class="smallexample">
  727. <pre class="smallexample">extern void *
  728. my_memcpy (void *dest, const void *src, size_t len)
  729. __attribute__((nonnull (1, 2)));
  730. </pre></div>
  731. <p>causes the compiler to check that, in calls to <code>my_memcpy</code>,
  732. arguments <var>dest</var> and <var>src</var> are non-null. If the compiler
  733. determines that a null pointer is passed in an argument slot marked
  734. as non-null, and the <samp>-Wnonnull</samp> option is enabled, a warning
  735. is issued. The compiler may also choose to make optimizations based
  736. on the knowledge that certain function arguments will never be null.
  737. </p>
  738. <p>If no argument index list is given to the <code>nonnull</code> attribute,
  739. all pointer arguments are marked as non-null. To illustrate, the
  740. following declaration is equivalent to the previous example:
  741. </p>
  742. <div class="smallexample">
  743. <pre class="smallexample">extern void *
  744. my_memcpy (void *dest, const void *src, size_t len)
  745. __attribute__((nonnull));
  746. </pre></div>
  747. </dd>
  748. <dt><code>noplt</code></dt>
  749. <dd><a name="index-noplt-function-attribute"></a>
  750. <p>The <code>noplt</code> attribute is the counterpart to option <samp>-fno-plt</samp>.
  751. Calls to functions marked with this attribute in position-independent code
  752. do not use the PLT.
  753. </p>
  754. <div class="smallexample">
  755. <pre class="smallexample">/* Externally defined function foo. */
  756. int foo () __attribute__ ((noplt));
  757. int
  758. main (/* <span class="roman">&hellip;</span> */)
  759. {
  760. /* <span class="roman">&hellip;</span> */
  761. foo ();
  762. /* <span class="roman">&hellip;</span> */
  763. }
  764. </pre></div>
  765. <p>The <code>noplt</code> attribute on function <code>foo</code>
  766. tells the compiler to assume that
  767. the function <code>foo</code> is externally defined and that the call to
  768. <code>foo</code> must avoid the PLT
  769. in position-independent code.
  770. </p>
  771. <p>In position-dependent code, a few targets also convert calls to
  772. functions that are marked to not use the PLT to use the GOT instead.
  773. </p>
  774. </dd>
  775. <dt><code>noreturn</code></dt>
  776. <dd><a name="index-noreturn-function-attribute"></a>
  777. <a name="index-functions-that-never-return"></a>
  778. <p>A few standard library functions, such as <code>abort</code> and <code>exit</code>,
  779. cannot return. GCC knows this automatically. Some programs define
  780. their own functions that never return. You can declare them
  781. <code>noreturn</code> to tell the compiler this fact. For example,
  782. </p>
  783. <div class="smallexample">
  784. <pre class="smallexample">void fatal () __attribute__ ((noreturn));
  785. void
  786. fatal (/* <span class="roman">&hellip;</span> */)
  787. {
  788. /* <span class="roman">&hellip;</span> */ /* <span class="roman">Print error message.</span> */ /* <span class="roman">&hellip;</span> */
  789. exit (1);
  790. }
  791. </pre></div>
  792. <p>The <code>noreturn</code> keyword tells the compiler to assume that
  793. <code>fatal</code> cannot return. It can then optimize without regard to what
  794. would happen if <code>fatal</code> ever did return. This makes slightly
  795. better code. More importantly, it helps avoid spurious warnings of
  796. uninitialized variables.
  797. </p>
  798. <p>The <code>noreturn</code> keyword does not affect the exceptional path when that
  799. applies: a <code>noreturn</code>-marked function may still return to the caller
  800. by throwing an exception or calling <code>longjmp</code>.
  801. </p>
  802. <p>Do not assume that registers saved by the calling function are
  803. restored before calling the <code>noreturn</code> function.
  804. </p>
  805. <p>It does not make sense for a <code>noreturn</code> function to have a return
  806. type other than <code>void</code>.
  807. </p>
  808. </dd>
  809. <dt><code>nothrow</code></dt>
  810. <dd><a name="index-nothrow-function-attribute"></a>
  811. <p>The <code>nothrow</code> attribute is used to inform the compiler that a
  812. function cannot throw an exception. For example, most functions in
  813. the standard C library can be guaranteed not to throw an exception
  814. with the notable exceptions of <code>qsort</code> and <code>bsearch</code> that
  815. take function pointer arguments.
  816. </p>
  817. </dd>
  818. <dt><code>optimize</code></dt>
  819. <dd><a name="index-optimize-function-attribute"></a>
  820. <p>The <code>optimize</code> attribute is used to specify that a function is to
  821. be compiled with different optimization options than specified on the
  822. command line. Arguments can either be numbers or strings. Numbers
  823. are assumed to be an optimization level. Strings that begin with
  824. <code>O</code> are assumed to be an optimization option, while other options
  825. are assumed to be used with a <code>-f</code> prefix. You can also use the
  826. &lsquo;<samp>#pragma GCC optimize</samp>&rsquo; pragma to set the optimization options
  827. that affect more than one function.
  828. See <a href="Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas">Function Specific Option Pragmas</a>, for details about the
  829. &lsquo;<samp>#pragma GCC optimize</samp>&rsquo; pragma.
  830. </p>
  831. <p>This attribute should be used for debugging purposes only. It is not
  832. suitable in production code.
  833. </p>
  834. </dd>
  835. <dt><code>pure</code></dt>
  836. <dd><a name="index-pure-function-attribute"></a>
  837. <a name="index-functions-that-have-no-side-effects-1"></a>
  838. <p>Many functions have no effects except the return value and their
  839. return value depends only on the parameters and/or global variables.
  840. Such a function can be subject
  841. to common subexpression elimination and loop optimization just as an
  842. arithmetic operator would be. These functions should be declared
  843. with the attribute <code>pure</code>. For example,
  844. </p>
  845. <div class="smallexample">
  846. <pre class="smallexample">int square (int) __attribute__ ((pure));
  847. </pre></div>
  848. <p>says that the hypothetical function <code>square</code> is safe to call
  849. fewer times than the program says.
  850. </p>
  851. <p>Some common examples of pure functions are <code>strlen</code> or <code>memcmp</code>.
  852. Interesting non-pure functions are functions with infinite loops or those
  853. depending on volatile memory or other system resource, that may change between
  854. two consecutive calls (such as <code>feof</code> in a multithreading environment).
  855. </p>
  856. </dd>
  857. <dt><code>returns_nonnull</code></dt>
  858. <dd><a name="index-returns_005fnonnull-function-attribute"></a>
  859. <p>The <code>returns_nonnull</code> attribute specifies that the function
  860. return value should be a non-null pointer. For instance, the declaration:
  861. </p>
  862. <div class="smallexample">
  863. <pre class="smallexample">extern void *
  864. mymalloc (size_t len) __attribute__((returns_nonnull));
  865. </pre></div>
  866. <p>lets the compiler optimize callers based on the knowledge
  867. that the return value will never be null.
  868. </p>
  869. </dd>
  870. <dt><code>returns_twice</code></dt>
  871. <dd><a name="index-returns_005ftwice-function-attribute"></a>
  872. <a name="index-functions-that-return-more-than-once"></a>
  873. <p>The <code>returns_twice</code> attribute tells the compiler that a function may
  874. return more than one time. The compiler ensures that all registers
  875. are dead before calling such a function and emits a warning about
  876. the variables that may be clobbered after the second return from the
  877. function. Examples of such functions are <code>setjmp</code> and <code>vfork</code>.
  878. The <code>longjmp</code>-like counterpart of such function, if any, might need
  879. to be marked with the <code>noreturn</code> attribute.
  880. </p>
  881. </dd>
  882. <dt><code>section (&quot;<var>section-name</var>&quot;)</code></dt>
  883. <dd><a name="index-section-function-attribute"></a>
  884. <a name="index-functions-in-arbitrary-sections"></a>
  885. <p>Normally, the compiler places the code it generates in the <code>text</code> section.
  886. Sometimes, however, you need additional sections, or you need certain
  887. particular functions to appear in special sections. The <code>section</code>
  888. attribute specifies that a function lives in a particular section.
  889. For example, the declaration:
  890. </p>
  891. <div class="smallexample">
  892. <pre class="smallexample">extern void foobar (void) __attribute__ ((section (&quot;bar&quot;)));
  893. </pre></div>
  894. <p>puts the function <code>foobar</code> in the <code>bar</code> section.
  895. </p>
  896. <p>Some file formats do not support arbitrary sections so the <code>section</code>
  897. attribute is not available on all platforms.
  898. If you need to map the entire contents of a module to a particular
  899. section, consider using the facilities of the linker instead.
  900. </p>
  901. </dd>
  902. <dt><code>sentinel</code></dt>
  903. <dd><a name="index-sentinel-function-attribute"></a>
  904. <p>This function attribute ensures that a parameter in a function call is
  905. an explicit <code>NULL</code>. The attribute is only valid on variadic
  906. functions. By default, the sentinel is located at position zero, the
  907. last parameter of the function call. If an optional integer position
  908. argument P is supplied to the attribute, the sentinel must be located at
  909. position P counting backwards from the end of the argument list.
  910. </p>
  911. <div class="smallexample">
  912. <pre class="smallexample">__attribute__ ((sentinel))
  913. is equivalent to
  914. __attribute__ ((sentinel(0)))
  915. </pre></div>
  916. <p>The attribute is automatically set with a position of 0 for the built-in
  917. functions <code>execl</code> and <code>execlp</code>. The built-in function
  918. <code>execle</code> has the attribute set with a position of 1.
  919. </p>
  920. <p>A valid <code>NULL</code> in this context is defined as zero with any pointer
  921. type. If your system defines the <code>NULL</code> macro with an integer type
  922. then you need to add an explicit cast. GCC replaces <code>stddef.h</code>
  923. with a copy that redefines NULL appropriately.
  924. </p>
  925. <p>The warnings for missing or incorrect sentinels are enabled with
  926. <samp>-Wformat</samp>.
  927. </p>
  928. </dd>
  929. <dt><code>simd</code></dt>
  930. <dt><code>simd(&quot;<var>mask</var>&quot;)</code></dt>
  931. <dd><a name="index-simd-function-attribute"></a>
  932. <p>This attribute enables creation of one or more function versions that
  933. can process multiple arguments using SIMD instructions from a
  934. single invocation. Specifying this attribute allows compiler to
  935. assume that such versions are available at link time (provided
  936. in the same or another translation unit). Generated versions are
  937. target-dependent and described in the corresponding Vector ABI document. For
  938. x86_64 target this document can be found
  939. <a href="https://sourceware.org/glibc/wiki/libmvec?action=AttachFile&amp;do=view&amp;target=VectorABI.txt">here</a><!-- /@w -->.
  940. </p>
  941. <p>The optional argument <var>mask</var> may have the value
  942. <code>notinbranch</code> or <code>inbranch</code>,
  943. and instructs the compiler to generate non-masked or masked
  944. clones correspondingly. By default, all clones are generated.
  945. </p>
  946. <p>The attribute should not be used together with Cilk Plus <code>vector</code>
  947. attribute on the same function.
  948. </p>
  949. <p>If the attribute is specified and <code>#pragma omp declare simd</code> is
  950. present on a declaration and the <samp>-fopenmp</samp> or <samp>-fopenmp-simd</samp>
  951. switch is specified, then the attribute is ignored.
  952. </p>
  953. </dd>
  954. <dt><code>stack_protect</code></dt>
  955. <dd><a name="index-stack_005fprotect-function-attribute"></a>
  956. <p>This attribute adds stack protection code to the function if
  957. flags <samp>-fstack-protector</samp>, <samp>-fstack-protector-strong</samp>
  958. or <samp>-fstack-protector-explicit</samp> are set.
  959. </p>
  960. </dd>
  961. <dt><code>target (<var>options</var>)</code></dt>
  962. <dd><a name="index-target-function-attribute"></a>
  963. <p>Multiple target back ends implement the <code>target</code> attribute
  964. to specify that a function is to
  965. be compiled with different target options than specified on the
  966. command line. This can be used for instance to have functions
  967. compiled with a different ISA (instruction set architecture) than the
  968. default. You can also use the &lsquo;<samp>#pragma GCC target</samp>&rsquo; pragma to set
  969. more than one function to be compiled with specific target options.
  970. See <a href="Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas">Function Specific Option Pragmas</a>, for details about the
  971. &lsquo;<samp>#pragma GCC target</samp>&rsquo; pragma.
  972. </p>
  973. <p>For instance, on an x86, you could declare one function with the
  974. <code>target(&quot;sse4.1,arch=core2&quot;)</code> attribute and another with
  975. <code>target(&quot;sse4a,arch=amdfam10&quot;)</code>. This is equivalent to
  976. compiling the first function with <samp>-msse4.1</samp> and
  977. <samp>-march=core2</samp> options, and the second function with
  978. <samp>-msse4a</samp> and <samp>-march=amdfam10</samp> options. It is up to you
  979. to make sure that a function is only invoked on a machine that
  980. supports the particular ISA it is compiled for (for example by using
  981. <code>cpuid</code> on x86 to determine what feature bits and architecture
  982. family are used).
  983. </p>
  984. <div class="smallexample">
  985. <pre class="smallexample">int core2_func (void) __attribute__ ((__target__ (&quot;arch=core2&quot;)));
  986. int sse3_func (void) __attribute__ ((__target__ (&quot;sse3&quot;)));
  987. </pre></div>
  988. <p>You can either use multiple
  989. strings separated by commas to specify multiple options,
  990. or separate the options with a comma (&lsquo;<samp>,</samp>&rsquo;) within a single string.
  991. </p>
  992. <p>The options supported are specific to each target; refer to <a href="x86-Function-Attributes.html#x86-Function-Attributes">x86 Function Attributes</a>, <a href="PowerPC-Function-Attributes.html#PowerPC-Function-Attributes">PowerPC Function Attributes</a>,
  993. <a href="ARM-Function-Attributes.html#ARM-Function-Attributes">ARM Function Attributes</a>,and <a href="Nios-II-Function-Attributes.html#Nios-II-Function-Attributes">Nios II Function Attributes</a>,
  994. for details.
  995. </p>
  996. </dd>
  997. <dt><code>target_clones (<var>options</var>)</code></dt>
  998. <dd><a name="index-target_005fclones-function-attribute"></a>
  999. <p>The <code>target_clones</code> attribute is used to specify that a function
  1000. be cloned into multiple versions compiled with different target options
  1001. than specified on the command line. The supported options and restrictions
  1002. are the same as for <code>target</code> attribute.
  1003. </p>
  1004. <p>For instance, on an x86, you could compile a function with
  1005. <code>target_clones(&quot;sse4.1,avx&quot;)</code>. GCC creates two function clones,
  1006. one compiled with <samp>-msse4.1</samp> and another with <samp>-mavx</samp>.
  1007. It also creates a resolver function (see the <code>ifunc</code> attribute
  1008. above) that dynamically selects a clone suitable for current architecture.
  1009. </p>
  1010. </dd>
  1011. <dt><code>unused</code></dt>
  1012. <dd><a name="index-unused-function-attribute"></a>
  1013. <p>This attribute, attached to a function, means that the function is meant
  1014. to be possibly unused. GCC does not produce a warning for this
  1015. function.
  1016. </p>
  1017. </dd>
  1018. <dt><code>used</code></dt>
  1019. <dd><a name="index-used-function-attribute"></a>
  1020. <p>This attribute, attached to a function, means that code must be emitted
  1021. for the function even if it appears that the function is not referenced.
  1022. This is useful, for example, when the function is referenced only in
  1023. inline assembly.
  1024. </p>
  1025. <p>When applied to a member function of a C++ class template, the
  1026. attribute also means that the function is instantiated if the
  1027. class itself is instantiated.
  1028. </p>
  1029. </dd>
  1030. <dt><code>visibility (&quot;<var>visibility_type</var>&quot;)</code></dt>
  1031. <dd><a name="index-visibility-function-attribute"></a>
  1032. <p>This attribute affects the linkage of the declaration to which it is attached.
  1033. It can be applied to variables (see <a href="Common-Variable-Attributes.html#Common-Variable-Attributes">Common Variable Attributes</a>) and types
  1034. (see <a href="Common-Type-Attributes.html#Common-Type-Attributes">Common Type Attributes</a>) as well as functions.
  1035. </p>
  1036. <p>There are four supported <var>visibility_type</var> values: default,
  1037. hidden, protected or internal visibility.
  1038. </p>
  1039. <div class="smallexample">
  1040. <pre class="smallexample">void __attribute__ ((visibility (&quot;protected&quot;)))
  1041. f () { /* <span class="roman">Do something.</span> */; }
  1042. int i __attribute__ ((visibility (&quot;hidden&quot;)));
  1043. </pre></div>
  1044. <p>The possible values of <var>visibility_type</var> correspond to the
  1045. visibility settings in the ELF gABI.
  1046. </p>
  1047. <dl compact="compact">
  1048. <dt><code>default</code></dt>
  1049. <dd><p>Default visibility is the normal case for the object file format.
  1050. This value is available for the visibility attribute to override other
  1051. options that may change the assumed visibility of entities.
  1052. </p>
  1053. <p>On ELF, default visibility means that the declaration is visible to other
  1054. modules and, in shared libraries, means that the declared entity may be
  1055. overridden.
  1056. </p>
  1057. <p>On Darwin, default visibility means that the declaration is visible to
  1058. other modules.
  1059. </p>
  1060. <p>Default visibility corresponds to &ldquo;external linkage&rdquo; in the language.
  1061. </p>
  1062. </dd>
  1063. <dt><code>hidden</code></dt>
  1064. <dd><p>Hidden visibility indicates that the entity declared has a new
  1065. form of linkage, which we call &ldquo;hidden linkage&rdquo;. Two
  1066. declarations of an object with hidden linkage refer to the same object
  1067. if they are in the same shared object.
  1068. </p>
  1069. </dd>
  1070. <dt><code>internal</code></dt>
  1071. <dd><p>Internal visibility is like hidden visibility, but with additional
  1072. processor specific semantics. Unless otherwise specified by the
  1073. psABI, GCC defines internal visibility to mean that a function is
  1074. <em>never</em> called from another module. Compare this with hidden
  1075. functions which, while they cannot be referenced directly by other
  1076. modules, can be referenced indirectly via function pointers. By
  1077. indicating that a function cannot be called from outside the module,
  1078. GCC may for instance omit the load of a PIC register since it is known
  1079. that the calling function loaded the correct value.
  1080. </p>
  1081. </dd>
  1082. <dt><code>protected</code></dt>
  1083. <dd><p>Protected visibility is like default visibility except that it
  1084. indicates that references within the defining module bind to the
  1085. definition in that module. That is, the declared entity cannot be
  1086. overridden by another module.
  1087. </p>
  1088. </dd>
  1089. </dl>
  1090. <p>All visibilities are supported on many, but not all, ELF targets
  1091. (supported when the assembler supports the &lsquo;<samp>.visibility</samp>&rsquo;
  1092. pseudo-op). Default visibility is supported everywhere. Hidden
  1093. visibility is supported on Darwin targets.
  1094. </p>
  1095. <p>The visibility attribute should be applied only to declarations that
  1096. would otherwise have external linkage. The attribute should be applied
  1097. consistently, so that the same entity should not be declared with
  1098. different settings of the attribute.
  1099. </p>
  1100. <p>In C++, the visibility attribute applies to types as well as functions
  1101. and objects, because in C++ types have linkage. A class must not have
  1102. greater visibility than its non-static data member types and bases,
  1103. and class members default to the visibility of their class. Also, a
  1104. declaration without explicit visibility is limited to the visibility
  1105. of its type.
  1106. </p>
  1107. <p>In C++, you can mark member functions and static member variables of a
  1108. class with the visibility attribute. This is useful if you know a
  1109. particular method or static member variable should only be used from
  1110. one shared object; then you can mark it hidden while the rest of the
  1111. class has default visibility. Care must be taken to avoid breaking
  1112. the One Definition Rule; for example, it is usually not useful to mark
  1113. an inline method as hidden without marking the whole class as hidden.
  1114. </p>
  1115. <p>A C++ namespace declaration can also have the visibility attribute.
  1116. </p>
  1117. <div class="smallexample">
  1118. <pre class="smallexample">namespace nspace1 __attribute__ ((visibility (&quot;protected&quot;)))
  1119. { /* <span class="roman">Do something.</span> */; }
  1120. </pre></div>
  1121. <p>This attribute applies only to the particular namespace body, not to
  1122. other definitions of the same namespace; it is equivalent to using
  1123. &lsquo;<samp>#pragma GCC visibility</samp>&rsquo; before and after the namespace
  1124. definition (see <a href="Visibility-Pragmas.html#Visibility-Pragmas">Visibility Pragmas</a>).
  1125. </p>
  1126. <p>In C++, if a template argument has limited visibility, this
  1127. restriction is implicitly propagated to the template instantiation.
  1128. Otherwise, template instantiations and specializations default to the
  1129. visibility of their template.
  1130. </p>
  1131. <p>If both the template and enclosing class have explicit visibility, the
  1132. visibility from the template is used.
  1133. </p>
  1134. </dd>
  1135. <dt><code>warn_unused_result</code></dt>
  1136. <dd><a name="index-warn_005funused_005fresult-function-attribute"></a>
  1137. <p>The <code>warn_unused_result</code> attribute causes a warning to be emitted
  1138. if a caller of the function with this attribute does not use its
  1139. return value. This is useful for functions where not checking
  1140. the result is either a security problem or always a bug, such as
  1141. <code>realloc</code>.
  1142. </p>
  1143. <div class="smallexample">
  1144. <pre class="smallexample">int fn () __attribute__ ((warn_unused_result));
  1145. int foo ()
  1146. {
  1147. if (fn () &lt; 0) return -1;
  1148. fn ();
  1149. return 0;
  1150. }
  1151. </pre></div>
  1152. <p>results in warning on line 5.
  1153. </p>
  1154. </dd>
  1155. <dt><code>weak</code></dt>
  1156. <dd><a name="index-weak-function-attribute"></a>
  1157. <p>The <code>weak</code> attribute causes the declaration to be emitted as a weak
  1158. symbol rather than a global. This is primarily useful in defining
  1159. library functions that can be overridden in user code, though it can
  1160. also be used with non-function declarations. Weak symbols are supported
  1161. for ELF targets, and also for a.out targets when using the GNU assembler
  1162. and linker.
  1163. </p>
  1164. </dd>
  1165. <dt><code>weakref</code></dt>
  1166. <dt><code>weakref (&quot;<var>target</var>&quot;)</code></dt>
  1167. <dd><a name="index-weakref-function-attribute"></a>
  1168. <p>The <code>weakref</code> attribute marks a declaration as a weak reference.
  1169. Without arguments, it should be accompanied by an <code>alias</code> attribute
  1170. naming the target symbol. Optionally, the <var>target</var> may be given as
  1171. an argument to <code>weakref</code> itself. In either case, <code>weakref</code>
  1172. implicitly marks the declaration as <code>weak</code>. Without a
  1173. <var>target</var>, given as an argument to <code>weakref</code> or to <code>alias</code>,
  1174. <code>weakref</code> is equivalent to <code>weak</code>.
  1175. </p>
  1176. <div class="smallexample">
  1177. <pre class="smallexample">static int x() __attribute__ ((weakref (&quot;y&quot;)));
  1178. /* is equivalent to... */
  1179. static int x() __attribute__ ((weak, weakref, alias (&quot;y&quot;)));
  1180. /* and to... */
  1181. static int x() __attribute__ ((weakref));
  1182. static int x() __attribute__ ((alias (&quot;y&quot;)));
  1183. </pre></div>
  1184. <p>A weak reference is an alias that does not by itself require a
  1185. definition to be given for the target symbol. If the target symbol is
  1186. only referenced through weak references, then it becomes a <code>weak</code>
  1187. undefined symbol. If it is directly referenced, however, then such
  1188. strong references prevail, and a definition is required for the
  1189. symbol, not necessarily in the same translation unit.
  1190. </p>
  1191. <p>The effect is equivalent to moving all references to the alias to a
  1192. separate translation unit, renaming the alias to the aliased symbol,
  1193. declaring it as weak, compiling the two separate translation units and
  1194. performing a reloadable link on them.
  1195. </p>
  1196. <p>At present, a declaration to which <code>weakref</code> is attached can
  1197. only be <code>static</code>.
  1198. </p>
  1199. </dd>
  1200. </dl>
  1201. <hr>
  1202. <div class="header">
  1203. <p>
  1204. Next: <a href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" accesskey="n" rel="next">AArch64 Function Attributes</a>, Up: <a href="Function-Attributes.html#Function-Attributes" accesskey="u" rel="up">Function Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  1205. </div>
  1206. </body>
  1207. </html>