xref: /netbsd-src/share/man/man3/attribute.3 (revision c1f2b118ff38e526579c0672a603052db71eceab)
1*c1f2b118Schristos.\" $NetBSD: attribute.3,v 1.18 2018/09/14 20:53:49 christos Exp $
2084bbf58Sjruoho.\"
3084bbf58Sjruoho.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
4084bbf58Sjruoho.\" All rights reserved.
5084bbf58Sjruoho.\"
6084bbf58Sjruoho.\" This code is derived from software contributed to The NetBSD Foundation
7084bbf58Sjruoho.\" by Jukka Ruohonen.
8084bbf58Sjruoho.\"
9084bbf58Sjruoho.\" Redistribution and use in source and binary forms, with or without
10084bbf58Sjruoho.\" modification, are permitted provided that the following conditions
11084bbf58Sjruoho.\" are met:
12084bbf58Sjruoho.\" 1. Redistributions of source code must retain the above copyright
13084bbf58Sjruoho.\"    notice, this list of conditions and the following disclaimer.
14084bbf58Sjruoho.\" 2. Redistributions in binary form must reproduce the above copyright
15084bbf58Sjruoho.\"    notice, this list of conditions and the following disclaimer in the
16084bbf58Sjruoho.\"    documentation and/or other materials provided with the distribution.
17084bbf58Sjruoho.\"
18084bbf58Sjruoho.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19084bbf58Sjruoho.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20084bbf58Sjruoho.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21084bbf58Sjruoho.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22084bbf58Sjruoho.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23084bbf58Sjruoho.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24084bbf58Sjruoho.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25084bbf58Sjruoho.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26084bbf58Sjruoho.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27084bbf58Sjruoho.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28084bbf58Sjruoho.\" POSSIBILITY OF SUCH DAMAGE.
29084bbf58Sjruoho.\"
30*c1f2b118Schristos.Dd September 14, 2018
31084bbf58Sjruoho.Dt ATTRIBUTE 3
32084bbf58Sjruoho.Os
33084bbf58Sjruoho.Sh NAME
34084bbf58Sjruoho.Nm attribute
35bcf09c16Sapb.Nd non-standard compiler attribute extensions
36084bbf58Sjruoho.Sh SYNOPSIS
37084bbf58Sjruoho.In sys/cdefs.h
38084bbf58Sjruoho.Pp
39e4082b0bSjruoho.Ic __dead
40e4082b0bSjruoho.Pp
41e4082b0bSjruoho.Ic __pure
42e4082b0bSjruoho.Pp
43e4082b0bSjruoho.Ic __constfunc
44e4082b0bSjruoho.Pp
45e4082b0bSjruoho.Ic __noinline
46e4082b0bSjruoho.Pp
47e4082b0bSjruoho.Ic __unused
48e4082b0bSjruoho.Pp
49e4082b0bSjruoho.Ic __used
50e4082b0bSjruoho.Pp
5128fabf43Sapb.Ic __diagused
5228fabf43Sapb.Pp
5328fabf43Sapb.Ic __debugused
5428fabf43Sapb.Pp
55084bbf58Sjruoho.Ic __packed
56084bbf58Sjruoho.Pp
57084bbf58Sjruoho.Fn __aligned "x"
58084bbf58Sjruoho.Fn __section "section"
59cc55e6fcSjruoho.Pp
60cc55e6fcSjruoho.Ic __read_mostly
61cc55e6fcSjruoho.Pp
62cc55e6fcSjruoho.Ic __cacheline_aligned
631194e4acSjruoho.Pp
641194e4acSjruoho.Fn __predict_true "exp"
651194e4acSjruoho.Pp
66b3d5f36dSjym.Fn __predict_false "exp"
67*c1f2b118Schristos.Pp
68*c1f2b118Schristos.Fn __printflike "fmtarg" "firstvararg"
69*c1f2b118Schristos.Pp
70*c1f2b118Schristos.Fn __sysloglike "fmtarg" "firstvararg"
71084bbf58Sjruoho.Sh DESCRIPTION
72bcf09c16SapbAs an extension to the C standard, some compilers allow non-standard
73bcf09c16Sapbattributes to be associated with functions, variables, or types, to
74bcf09c16Sapbmodify some aspect of the way the compiler treats the associated item.
75084bbf58SjruohoThe
76084bbf58Sjruoho.Tn GNU
77084bbf58SjruohoCompiler Collection
78bcf09c16Sapb.Pq Tn GCC ,
79bcf09c16Sapband
80bcf09c16Sapb.Tn LLVM/Clang ,
81bcf09c16Sapbuse the
82084bbf58Sjruoho.Em __attribute__
83bcf09c16Sapbsyntax for such attributes,
84bcf09c16Sapbbut different versions of the compilers support different attributes,
85bcf09c16Sapband other compilers may use entirely different syntax.
86bcf09c16Sapb.Pp
87bcf09c16Sapb.Nx
88bcf09c16Sapbcode should usually avoid direct use of the
89bcf09c16Sapb.Em __attribute__
90bcf09c16Sapbor similar syntax provided by specific compilers.
91bcf09c16SapbInstead,
92c1b5b457Swiz.Nx Ap s
93bcf09c16Sapb.In sys/cdefs.h
94bcf09c16Sapbheader file
95bcf09c16Sapbprovides several attribute macros in a namespace
96bcf09c16Sapbreserved for the implementation (beginning with
97bcf09c16Sapb.Ql __ ) ,
98bcf09c16Sapbthat expand to the appropriate syntax for the compiler that is in use.
99e4082b0bSjruoho.Sh ATTRIBUTES
100084bbf58Sjruoho.Bl -tag -width abc
101e4082b0bSjruoho.It Ic __dead
102bcf09c16SapbCertain functions, such as
103e4082b0bSjruoho.Xr abort 3
104e4082b0bSjruohoand
105bcf09c16Sapb.Xr exit 3 ,
10612289360Sjoergcan never return.
107bcf09c16SapbWhen such a function is declared with
108e4082b0bSjruoho.Ic __dead ,
10912289360Sjoergcertain optimizations are possible and warnings sensitive to the code flow graph
11012289360Sjoergmay be pruned.
111e4082b0bSjruohoObviously a
112e4082b0bSjruoho.Ic __dead
113e4082b0bSjruohofunction can never have return type other than
114e4082b0bSjruoho.Vt void .
115e4082b0bSjruoho.It Ic __pure
116e4082b0bSjruohoA
117e4082b0bSjruoho.Ic __pure
118e4082b0bSjruohofunction is defined to be one that has no effects except
119e4082b0bSjruohothe return value, which is assumed to depend only on the
120e4082b0bSjruohofunction parameters and/or global variables.
121e4082b0bSjruohoAny access to parameters and/or global variables must also be read-only.
122e4082b0bSjruohoA function that depends on volatile memory, or other comparable
123e4082b0bSjruohosystem resource that can change between two consecutive calls,
124e4082b0bSjruohocan never be
125e4082b0bSjruoho.Ic __pure .
126e4082b0bSjruohoMany
127e4082b0bSjruoho.Xr math 3
128e4082b0bSjruohofunctions satisfy the definition of a
129e4082b0bSjruoho.Ic __pure
130e4082b0bSjruohofunction, at least in theory.
131e4082b0bSjruohoOther examples include
132e4082b0bSjruoho.Xr strlen 3
133e4082b0bSjruohoand
134e4082b0bSjruoho.Xr strcmp 3 .
135e4082b0bSjruoho.It Ic __constfunc
136e4082b0bSjruohoA
137e4082b0bSjruoho.Dq const function
138e4082b0bSjruohois a stricter variant of
139e4082b0bSjruoho.Dq pure functions .
140e4082b0bSjruohoIn addition to the restrictions of pure functions, a function declared with
141e4082b0bSjruoho.Ic __constfunc
142e4082b0bSjruohocan never access global variables nor take pointers as parameters.
143e4082b0bSjruohoThe return value of these functions must depend
144e4082b0bSjruohoonly on the passed-by-value parameters.
145e4082b0bSjruohoNote also that a function that calls non-const functions can not be
146e4082b0bSjruoho.Ic __constfunc .
147e4082b0bSjruohoThe canonical example of a const function would be
148e4082b0bSjruoho.Xr abs 3 .
149e4082b0bSjruohoAs with pure functions,
150e4082b0bSjruohocertain micro-optimizations are possible for functions declared with
151e4082b0bSjruoho.Ic __constfunc .
152e4082b0bSjruoho.It Ic __noinline
153e4082b0bSjruohoSometimes it is known that inlining is undesirable or that
154e4082b0bSjruohoa function will perform incorrectly when inlined.
155e4082b0bSjruohoThe
156e4082b0bSjruoho.Ic __noinline
157e4082b0bSjruohomacro expands to a function attribute that prevents
158bcf09c16Sapbthe compiler from inlining the function, irrespective
159bcf09c16Sapbof whether the function was declared with the
160e4082b0bSjruoho.Em inline
161e4082b0bSjruohokeyword.
162e4082b0bSjruohoThe attribute takes precedence over all
163e4082b0bSjruohoother compiler options related to inlining.
164e4082b0bSjruoho.It Ic __unused
165e4082b0bSjruohoMarking an unused function with the
166e4082b0bSjruoho.Ic __unused
167bcf09c16Sapbmacro inhibits warnings that a function is defined but not used.
168bcf09c16SapbMarking a variable with the
169bcf09c16Sapb.Ic __unused
170bcf09c16Sapbmacro inhibits warnings that the variable is unused,
171bcf09c16Sapbor that it is set but never read.
172e4082b0bSjruoho.It Ic __used
173e4082b0bSjruohoThe
174e4082b0bSjruoho.Ic __used
175bcf09c16Sapbmacro expands to an attribute that informs the compiler
176e4082b0bSjruohothat a static variable or function is to be always retained
177e4082b0bSjruohoin the object file even if it is unreferenced.
17828fabf43Sapb.It Ic __diagused
17928fabf43SapbThe
18028fabf43Sapb.Ic __diagused
18128fabf43Sapbmacro expands to an attribute that informs the compiler
18228fabf43Sapbthat a variable or function is used only in diagnostic code,
18328fabf43Sapband may be unused in non-diagnostic code.
18428fabf43Sapb.Pp
185b3d7317aSapbIn the kernel, variables that are used when
186b3d7317aSapb.Dv DIAGNOSTIC
187b3d7317aSapbis defined, but unused when
188b3d7317aSapb.Dv DIAGNOSTIC
189b3d7317aSapbis not defined, may be declared with
190b3d7317aSapb.Ic __diagused .
191b3d7317aSapbIn userland, variables that are used when
192b3d7317aSapb.Dv NDEBUG
193b3d7317aSapbis not defined, but unused when
194b3d7317aSapb.Dv NDEBUG
195b3d7317aSapbis defined, may be declared with
196b3d7317aSapb.Ic __diagused .
19728fabf43Sapb.Pp
198b3d7317aSapbVariables used only in
199b3d7317aSapb.Xr assert 3
200b3d7317aSapbor
201b3d7317aSapb.Xr KASSERT 9
202b3d7317aSapbmacros are likely candidates for being declared with
203b3d7317aSapb.Ic __diagused .
20428fabf43Sapb.It Ic __debugused
20528fabf43SapbThe
20628fabf43Sapb.Ic __debugused
20728fabf43Sapbmacro expands to an attribute that informs the compiler
20828fabf43Sapbthat a variable or function is used only in debug code,
20928fabf43Sapband may be unused in non-debug code.
21028fabf43Sapb.Pp
211b3d7317aSapbIn either the kernel or userland, variables that are used when
212b3d7317aSapb.Dv DEBUG
213b3d7317aSapbis defined, but unused when
214b3d7317aSapb.Dv DEBUG
215b3d7317aSapbis not defined, may be declared with
216b3d7317aSapb.Ic __debugused .
21728fabf43Sapb.Pp
218b3d7317aSapbIn the kernel, variables used only in
219b3d7317aSapb.Xr KDASSERT 9
220b3d7317aSapbmacros are likely candidates for being declared with
221b3d7317aSapb.Ic __debugused .
222b3d7317aSapbThere is no established convention for the use of
223b3d7317aSapb.Dv DEBUG
224b3d7317aSapbin userland code.
225084bbf58Sjruoho.It Ic __packed
226084bbf58SjruohoThe
227084bbf58Sjruoho.Ic __packed
228084bbf58Sjruohomacro expands to an attribute that forces a variable or
229084bbf58Sjruohostructure field to have the smallest possible alignment,
230084bbf58Sjruohopotentially disregarding architecture specific alignment requirements.
231084bbf58SjruohoThe smallest possible alignment is effectively one byte
232084bbf58Sjruohofor variables and one bit for fields.
233084bbf58SjruohoIf specified on a
234084bbf58Sjruoho.Vt struct
235084bbf58Sjruohoor
236084bbf58Sjruoho.Vt union ,
237084bbf58Sjruohoall variables therein are also packed.
238b71dd4cbSjruohoThe
239b71dd4cbSjruoho.Ic __packed
240b71dd4cbSjruohomacro is often useful when dealing with data that
241b71dd4cbSjruohois in a particular static format on the disk, wire, or memory.
242084bbf58Sjruoho.It Fn __aligned "x"
243084bbf58SjruohoThe
244084bbf58Sjruoho.Fn __aligned
245084bbf58Sjruohomacro expands to an attribute that specifies the minimum alignment
246084bbf58Sjruohoin bytes for a variable, structure field, or function.
247084bbf58SjruohoIn other words, the specified object should have an alignment of at least
248084bbf58Sjruoho.Fa x
249084bbf58Sjruohobytes, as opposed to the minimum alignment requirements dictated
250084bbf58Sjruohoby the architecture and the
251084bbf58Sjruoho.Tn ABI .
252084bbf58SjruohoPossible use cases include:
253084bbf58Sjruoho.Bl -bullet -offset indent
254084bbf58Sjruoho.It
255084bbf58SjruohoMixing assembly and C code.
256084bbf58Sjruoho.It
257084bbf58SjruohoDealing with hardware that may impose alignment requirements
258084bbf58Sjruohogreater than the architecture itself.
259084bbf58Sjruoho.It
260084bbf58SjruohoUsing instructions that may impose special alignment requirements.
261084bbf58SjruohoTypical example would be alignment of frequently used objects along
262084bbf58Sjruohoprocessor cache lines.
263084bbf58Sjruoho.El
264084bbf58Sjruoho.Pp
265084bbf58SjruohoNote that when used with functions, structures, or structure members,
266084bbf58Sjruoho.Fn __aligned
267084bbf58Sjruohocan only be used to increase the alignment.
268b71dd4cbSjruohoIf the macro is however used as part of a
269b71dd4cbSjruoho.Vt typedef ,
270b71dd4cbSjruohothe alignment can both increase and decrease.
271b71dd4cbSjruohoOtherwise it is only possible to decrease the alignment
272084bbf58Sjruohofor variables and fields by using the
273084bbf58Sjruoho.Ic __packed
274084bbf58Sjruohomacro.
275084bbf58SjruohoThe effectiveness of
276084bbf58Sjruoho.Fn __aligned
277084bbf58Sjruohois largely dependent on the linker.
278c29a3ebaSjruohoThe
279c29a3ebaSjruoho.Xr __alignof__ 3
280c29a3ebaSjruohooperator can be used to examine the alignment.
281084bbf58Sjruoho.It Fn __section "section"
282084bbf58SjruohoThe
283084bbf58Sjruoho.Fn __section
284084bbf58Sjruohomacro expands to an attribute that specifies a particular
285084bbf58Sjruoho.Fa section
286084bbf58Sjruohoto which a variable or function should be placed.
287084bbf58SjruohoNormally the compiler places the generated objects to sections such as
288084bbf58Sjruoho.Dq data
289084bbf58Sjruohoor
290084bbf58Sjruoho.Dq text .
291084bbf58SjruohoBy using
292084bbf58Sjruoho.Fn __section ,
293084bbf58Sjruohoit is possible to override this behavior, perhaps in order to place
294084bbf58Sjruohosome variables into particular sections specific to unique hardware.
295cc55e6fcSjruoho.It Ic __read_mostly
296cc55e6fcSjruohoThe
297cc55e6fcSjruoho.Ic __read_mostly
298cc55e6fcSjruohomacro uses
299084bbf58Sjruoho.Fn __section
300cc55e6fcSjruohoto place a variable or function into the
301cc55e6fcSjruoho.Dq .data.read_mostly
302cc55e6fcSjruohosection of the (kernel)
303cc55e6fcSjruoho.Xr elf 5 .
304cc55e6fcSjruohoThe use of
305cc55e6fcSjruoho.Ic __read_mostly
306cc55e6fcSjruohoallows infrequently modified data to be grouped together;
307cc55e6fcSjruohoit is expected that the cachelines of rarely and frequently modified
308cc55e6fcSjruohodata structures are this way separated.
309cc55e6fcSjruohoCandidates for
310cc55e6fcSjruoho.Ic __read_mostly
311cc55e6fcSjruohoinclude variables that are initialized once,
312cc55e6fcSjruohoread very often, and seldom written to.
313cc55e6fcSjruoho.It Ic __cacheline_aligned
314cc55e6fcSjruohoThe
315cc55e6fcSjruoho.Ic __cacheline_aligned
316cc55e6fcSjruohomacro behaves like
317cc55e6fcSjruoho.Ic __read_mostly ,
318cc55e6fcSjruohobut the used section is
319cc55e6fcSjruoho.Dq .data.cacheline_aligned
320cc55e6fcSjruohoinstead.
321cc55e6fcSjruohoIt also uses
322cc55e6fcSjruoho.Fn __aligned
323cc55e6fcSjruohoto set the minimum alignment into a predefined coherency unit.
324cc55e6fcSjruohoThis should ensure that frequently used data structures are
325cc55e6fcSjruohoaligned on cacheline boundaries.
32643b7e121SjruohoBoth
32743b7e121Sjruoho.Ic __cacheline_aligned
32843b7e121Sjruohoand
32943b7e121Sjruoho.Ic __read_mostly
33043b7e121Sjruohoare only available for the kernel.
3311194e4acSjruoho.It Ic __predict_true
3321194e4acSjruohoA branch is generally defined to be a conditional execution of a
3331194e4acSjruohoprogram depending on whether a certain flow control mechanism is altered.
3341194e4acSjruohoTypical example would be a
3351194e4acSjruoho.Dq if-then-else
3361194e4acSjruohosequence used in high-level languages or
3371194e4acSjruohoa jump instruction used in machine-level code.
3381194e4acSjruohoA branch prediction would then be defined as an
3391194e4acSjruohoattempt to guess whether a conditional branch will be taken.
3401194e4acSjruoho.Pp
3411194e4acSjruohoThe macros
3421194e4acSjruoho.Fn __predict_true
3431194e4acSjruohoand
3441194e4acSjruoho.Fn __predict_false
3451194e4acSjruohoannotate the likelihood of whether
3461194e4acSjruohoa branch will evaluate to true or false.
3471194e4acSjruohoThe rationale is to improve instruction pipelining.
3481194e4acSjruohoSemantically
3491194e4acSjruoho.Ic __predict_true
3501194e4acSjruohoexpects that the integral expression
3511194e4acSjruoho.Fa exp
352c3da4293Sriastradhyields nonzero.
3531194e4acSjruoho.It Ic __predict_false
3541194e4acSjruohoThe
3551194e4acSjruoho.Ic __predict_false
3561194e4acSjruohoexpands to an attribute that instructs the compiler
3571194e4acSjruohoto predict that a given branch will be likely false.
3581194e4acSjruohoAs programmers are notoriously bad at predicting
3591194e4acSjruohothe likely behavior of their code, profiling and
3601194e4acSjruohoempirical evidence should precede the use of
3611194e4acSjruoho.Ic __predict_false
3621194e4acSjruohoand
3631194e4acSjruoho.Ic __predict_true .
364*c1f2b118Schristos.It Fn __printflike "fmtarg" "firstvararg"
365*c1f2b118SchristosMarks a function as taking printf-like arguments.
366*c1f2b118Schristos.Fa fmtarg
367*c1f2b118Schristosis the index of the format string in the argument list, and
368*c1f2b118Schristos.Fa firstvararg
369*c1f2b118Schristosis the index of the first item of the vararg list.
370*c1f2b118Schristos.It Fn __sysloglike "fmtarg" "firstvararg"
371*c1f2b118SchristosMarks a function as taking syslog-like arguments.
372*c1f2b118SchristosAllows use of the %m formatting code.
373*c1f2b118Schristos.Fa fmtarg
374*c1f2b118Schristosis the index of the format string in the argument list, and
375*c1f2b118Schristos.Fa firstvararg
376*c1f2b118Schristosis the index of the first item of the vararg list, or
377*c1f2b118Schristos.Dv 0
378*c1f2b118Schristosif the argument is a
379*c1f2b118Schristos.Ft va_list .
380084bbf58Sjruoho.El
381084bbf58Sjruoho.Sh SEE ALSO
382bcf09c16Sapb.Xr clang 1 ,
383084bbf58Sjruoho.Xr gcc 1 ,
384595da22eSjruoho.Xr __builtin_object_size 3 ,
385ec9cee68Sjruoho.Xr cdefs 3 ,
386084bbf58Sjruoho.Xr c 7
387084bbf58Sjruoho.Sh CAVEATS
388084bbf58SjruohoIt goes without saying that portable applications
389084bbf58Sjruohoshould steer clear from non-standard extensions specific
390084bbf58Sjruohoto any given compiler.
391084bbf58SjruohoEven when portability is not a concern,
392b71dd4cbSjruohouse these macros sparsely and wisely.
393