2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
6 .. index:: attribute of variables, variable attributes
8 .. _variable-attributes:
10 Specifying Attributes of Variables
11 **********************************
13 The keyword ``__attribute__`` allows you to specify special properties
14 of variables, function parameters, or structure, union, and, in C++, class
15 members. This ``__attribute__`` keyword is followed by an attribute
16 specification enclosed in double parentheses. Some attributes are currently
17 defined generically for variables. Other attributes are defined for
18 variables on particular target systems. Other attributes are available
19 for functions (see :ref:`function-attributes`), labels (see :ref:`label-attributes`),
20 enumerators (see :ref:`enumerator-attributes`), statements
21 (see :ref:`statement-attributes`), and for types (see :ref:`type-attributes`).
22 Other front ends might define more attributes
23 (see :ref:`c++-extensions`).
25 See :ref:`attribute-syntax`, for details of the exact syntax for using
31 .. _common-variable-attributes:
33 Common Variable Attributes
34 ^^^^^^^^^^^^^^^^^^^^^^^^^^
36 The following attributes are supported on most targets.
38 .. index:: alias variable attribute
40 .. var-attr:: alias ("target")
42 The ``alias`` variable attribute causes the declaration to be emitted
43 as an alias for another symbol known as an :dfn:`alias target`. Except
44 for top-level qualifiers the alias target must have the same type as
45 the alias. For instance, the following
50 extern int __attribute__ ((alias ("var_target"))) var_alias;
52 defines ``var_alias`` to be an alias for the ``var_target`` variable.
54 It is an error if the alias target is not defined in the same translation
57 Note that in the absence of the attribute GCC assumes that distinct
58 declarations with external linkage denote distinct objects. Using both
59 the alias and the alias target to access the same object is undefined
60 in a translation unit without a declaration of the alias with the attribute.
62 This attribute requires assembler and object file support, and may not be
63 available on all targets.
65 .. index:: aligned variable attribute
67 .. var-attr:: aligned, aligned (alignment)
69 The :var-attr:`aligned` attribute specifies a minimum alignment for the variable
70 or structure field, measured in bytes. When specified, :samp:`{alignment}` must
71 be an integer constant power of 2. Specifying no :samp:`{alignment}` argument
72 implies the maximum alignment for the target, which is often, but by no
73 means always, 8 or 16 bytes.
75 For example, the declaration:
79 int x __attribute__ ((aligned (16))) = 0;
81 causes the compiler to allocate the global variable ``x`` on a
82 16-byte boundary. On a 68040, this could be used in conjunction with
83 an ``asm`` expression to access the ``move16`` instruction which
84 requires 16-byte aligned operands.
86 You can also specify the alignment of structure fields. For example, to
87 create a double-word aligned ``int`` pair, you could write:
91 struct foo { int x[2] __attribute__ ((aligned (8))); };
93 This is an alternative to creating a union with a ``double`` member,
94 which forces the union to be double-word aligned.
96 As in the preceding examples, you can explicitly specify the alignment
97 (in bytes) that you wish the compiler to use for a given variable or
98 structure field. Alternatively, you can leave out the alignment factor
99 and just ask the compiler to align a variable or field to the
100 default alignment for the target architecture you are compiling for.
101 The default alignment is sufficient for all scalar types, but may not be
102 enough for all vector types on a target that supports vector operations.
103 The default alignment is fixed for a particular target ABI.
105 GCC also provides a target specific macro ``__BIGGEST_ALIGNMENT__``,
106 which is the largest alignment ever used for any data type on the
107 target machine you are compiling for. For example, you could write:
111 short array[3] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)));
113 The compiler automatically sets the alignment for the declared
114 variable or field to ``__BIGGEST_ALIGNMENT__``. Doing this can
115 often make copy operations more efficient, because the compiler can
116 use whatever instructions copy the biggest chunks of memory when
117 performing copies to or from the variables or fields that you have
118 aligned this way. Note that the value of ``__BIGGEST_ALIGNMENT__``
119 may change depending on command-line options.
121 When used on a struct, or struct member, the :var-attr:`aligned` attribute can
122 only increase the alignment; in order to decrease it, the :var-attr:`packed`
123 attribute must be specified as well. When used as part of a typedef, the
124 :var-attr:`aligned` attribute can both increase and decrease alignment, and
125 specifying the :var-attr:`packed` attribute generates a warning.
127 Note that the effectiveness of :var-attr:`aligned` attributes for static
128 variables may be limited by inherent limitations in the system linker
129 and/or object file format. On some systems, the linker is
130 only able to arrange for variables to be aligned up to a certain maximum
131 alignment. (For some linkers, the maximum supported alignment may
132 be very very small.) If your linker is only able to align variables
133 up to a maximum of 8-byte alignment, then specifying ``aligned(16)``
134 in an ``__attribute__`` still only provides you with 8-byte
135 alignment. See your linker documentation for further information.
137 Stack variables are not affected by linker restrictions; GCC can properly
138 align them on any target.
140 The :var-attr:`aligned` attribute can also be used for functions
141 (see :ref:`common-function-attributes`.)
143 .. index:: warn_if_not_aligned variable attribute
145 .. var-attr:: warn_if_not_aligned (alignment)
147 This attribute specifies a threshold for the structure field, measured
148 in bytes. If the structure field is aligned below the threshold, a
149 warning will be issued. For example, the declaration:
157 unsigned long long x __attribute__ ((warn_if_not_aligned (16)));
160 causes the compiler to issue an warning on ``struct foo``, like
161 :samp:`warning: alignment 8 of 'struct foo' is less than 16`.
162 The compiler also issues a warning, like :samp:`warning: 'x' offset
163 8 in 'struct foo' isn't aligned to 16`, when the structure field has
164 the misaligned offset:
168 struct __attribute__ ((aligned (16))) foo
172 unsigned long long x __attribute__ ((warn_if_not_aligned (16)));
175 This warning can be disabled by :option:`-Wno-if-not-aligned`.
176 The ``warn_if_not_aligned`` attribute can also be used for types
177 (see :ref:`common-type-attributes`.)
179 .. index:: strict_flex_array variable attribute
181 .. gcc-attr:: strict_flex_array (level)
183 The ``strict_flex_array`` attribute should be attached to the trailing
184 array field of a structure. It controls when to treat the trailing array
185 field of a structure as a flexible array member for the purposes of accessing
186 the elements of such an array.
187 :samp:`{level}` must be an integer betwen 0 to 3.
189 :samp:`{level}` =0 is the least strict level, all trailing arrays of structures
190 are treated as flexible array members. :samp:`{level}` =3 is the strictest level,
191 only when the trailing array is declared as a flexible array member per C99
192 standard onwards (:samp:`[]`), it is treated as a flexible array member.
194 There are two more levels in between 0 and 3, which are provided to support
195 older codes that use GCC zero-length array extension (:samp:`[0]`) or one-element
196 array as flexible array members (:samp:`[1]`):
197 When :samp:`{level}` is 1, the trailing array is treated as a flexible array member
198 when it is declared as either :samp:`[]`, :samp:`[0]`, or :samp:`[1]`;
199 When :samp:`{level}` is 2, the trailing array is treated as a flexible array member
200 when it is declared as either :samp:`[]`, or :samp:`[0]`.
202 This attribute can be used with or without the :option:`-fstrict-flex-arrays`.
203 When both the attribute and the option present at the same time, the level of
204 the strictness for the specific trailing array field is determined by the
207 .. index:: alloc_size variable attribute
209 .. var-attr:: alloc_size (position), alloc_size (position-1, position-2)
211 The ``alloc_size`` variable attribute may be applied to the declaration
212 of a pointer to a function that returns a pointer and takes at least one
213 argument of an integer type. It indicates that the returned pointer points
214 to an object whose size is given by the function argument at :samp:`{position}`,
215 or by the product of the arguments at :samp:`{position-1}` and :samp:`{position-2}`.
216 Meaningful sizes are positive values less than ``PTRDIFF_MAX``. Other
217 sizes are diagnosed when detected. GCC uses this information to improve
218 the results of ``__builtin_object_size``.
220 For instance, the following declarations
224 typedef __attribute__ ((alloc_size (1, 2))) void*
225 (*calloc_ptr) (size_t, size_t);
226 typedef __attribute__ ((alloc_size (1))) void*
227 (*malloc_ptr) (size_t);
229 specify that ``calloc_ptr`` is a pointer of a function that, like
230 the standard C function ``calloc``, returns an object whose size
231 is given by the product of arguments 1 and 2, and similarly, that
232 ``malloc_ptr``, like the standard C function ``malloc``,
233 returns an object whose size is given by argument 1 to the function.
235 .. index:: cleanup variable attribute
237 .. var-attr:: cleanup (cleanup_function)
239 The ``cleanup`` attribute runs a function when the variable goes
240 out of scope. This attribute can only be applied to auto function
241 scope variables; it may not be applied to parameters or variables
242 with static storage duration. The function must take one parameter,
243 a pointer to a type compatible with the variable. The return value
244 of the function (if any) is ignored.
246 If :option:`-fexceptions` is enabled, then :samp:`{cleanup_function}`
247 is run during the stack unwinding that happens during the
248 processing of the exception. Note that the ``cleanup`` attribute
249 does not allow the exception to be caught, only to perform an action.
250 It is undefined what happens if :samp:`{cleanup_function}` does not
253 .. index:: common variable attribute, nocommon variable attribute
255 .. option:: common, nocommon
257 The ``common`` attribute requests GCC to place a variable in
258 'common' storage. The ``nocommon`` attribute requests the
259 opposite---to allocate space for it directly.
261 These attributes override the default chosen by the
262 :option:`-fno-common` and :option:`-fcommon` flags respectively.
264 .. index:: copy variable attribute
266 .. var-attr:: copy, copy (variable)
268 The :var-attr:`copy` attribute applies the set of attributes with which
269 :samp:`{variable}` has been declared to the declaration of the variable
270 to which the attribute is applied. The attribute is designed for
271 libraries that define aliases that are expected to specify the same
272 set of attributes as the aliased symbols. The :var-attr:`copy` attribute
273 can be used with variables, functions or types. However, the kind
274 of symbol to which the attribute is applied (either varible or
275 function) must match the kind of symbol to which the argument refers.
276 The :var-attr:`copy` attribute copies only syntactic and semantic attributes
277 but not attributes that affect a symbol's linkage or visibility such as
278 ``alias``, :var-attr:`visibility`, or :var-attr:`weak`. The :var-attr:`deprecated`
279 attribute is also not copied. See :ref:`common-function-attributes`.
280 See :ref:`common-type-attributes`.
282 .. index:: deprecated variable attribute
284 .. var-attr:: deprecated, deprecated (msg)
286 The :var-attr:`deprecated` attribute results in a warning if the variable
287 is used anywhere in the source file. This is useful when identifying
288 variables that are expected to be removed in a future version of a
289 program. The warning also includes the location of the declaration
290 of the deprecated variable, to enable users to easily find further
291 information about why the variable is deprecated, or what they should
292 do instead. Note that the warning only occurs for uses:
296 extern int old_var __attribute__ ((deprecated));
298 int new_fn () { return old_var; }
300 results in a warning on line 3 but not line 2. The optional :samp:`{msg}`
301 argument, which must be a string, is printed in the warning if
304 The :var-attr:`deprecated` attribute can also be used for functions and
305 types (see :ref:`common-function-attributes`,
306 see :ref:`common-type-attributes`).
308 The message attached to the attribute is affected by the setting of
309 the :option:`-fmessage-length` option.
311 .. index:: unavailable variable attribute
313 .. var-attr:: unavailable, unavailable (msg)
315 The :var-attr:`unavailable` attribute indicates that the variable so marked
316 is not available, if it is used anywhere in the source file. It behaves
317 in the same manner as the :var-attr:`deprecated` attribute except that the
318 compiler will emit an error rather than a warning.
320 It is expected that items marked as :var-attr:`deprecated` will eventually be
321 withdrawn from interfaces, and then become unavailable. This attribute
322 allows for marking them appropriately.
324 The :var-attr:`unavailable` attribute can also be used for functions and
325 types (see :ref:`common-function-attributes`,
326 see :ref:`common-type-attributes`).
328 .. index:: mode variable attribute
330 .. var-attr:: mode (mode)
332 This attribute specifies the data type for the declaration---whichever
333 type corresponds to the mode :samp:`{mode}`. This in effect lets you
334 request an integer or floating-point type according to its width.
336 See :ref:`gccint:machine-modes`,
337 for a list of the possible keywords for :samp:`{mode}`.
338 You may also specify a mode of ``byte`` or ``__byte__`` to
339 indicate the mode corresponding to a one-byte integer, ``word`` or
340 ``__word__`` for the mode of a one-word integer, and ``pointer``
341 or ``__pointer__`` for the mode used to represent pointers.
343 .. index:: nonstring variable attribute
345 .. var-attr:: nonstring
347 The :var-attr:`nonstring` variable attribute specifies that an object or member
348 declaration with type array of ``char``, ``signed char``, or
349 ``unsigned char``, or pointer to such a type is intended to store
350 character arrays that do not necessarily contain a terminating ``NUL``.
351 This is useful in detecting uses of such arrays or pointers with functions
352 that expect ``NUL`` -terminated strings, and to avoid warnings when such
353 an array or pointer is used as an argument to a bounded string manipulation
354 function such as ``strncpy``. For example, without the attribute, GCC
355 will issue a warning for the ``strncpy`` call below because it may
356 truncate the copy without appending the terminating ``NUL`` character.
357 Using the attribute makes it possible to suppress the warning. However,
358 when the array is declared with the attribute the call to ``strlen`` is
359 diagnosed because when the array doesn't contain a ``NUL`` -terminated
360 string the call is undefined. To copy, compare, of search non-string
361 character arrays use the ``memcpy``, ``memcmp``, ``memchr``,
362 and other functions that operate on arrays of bytes. In addition,
363 calling ``strnlen`` and ``strndup`` with such arrays is safe
364 provided a suitable bound is specified, and not diagnosed.
370 char name [32] __attribute__ ((nonstring));
373 int f (struct Data *pd, const char *s)
375 strncpy (pd->name, s, sizeof pd->name);
377 return strlen (pd->name); // unsafe, gets a warning
380 .. index:: packed variable attribute
384 The :var-attr:`packed` attribute specifies that a structure member should have
385 the smallest possible alignment---one bit for a bit-field and one byte
386 otherwise, unless a larger value is specified with the :var-attr:`aligned`
387 attribute. The attribute does not apply to non-member objects.
389 For example in the structure below, the member array ``x`` is packed
390 so that it immediately follows ``a`` with no intervening padding:
397 int x[2] __attribute__ ((packed));
402 The 4.1, 4.2 and 4.3 series of GCC ignore the
403 :var-attr:`packed` attribute on bit-fields of type ``char``. This has
404 been fixed in GCC 4.4 but the change can lead to differences in the
405 structure layout. See the documentation of
406 :option:`-Wpacked-bitfield-compat` for more information.
408 .. index:: section variable attribute
410 .. var-attr:: section ("section-name")
412 Normally, the compiler places the objects it generates in sections like
413 ``data`` and ``bss``. Sometimes, however, you need additional sections,
414 or you need certain particular variables to appear in special sections,
415 for example to map to special hardware. The ``section``
416 attribute specifies that a variable (or function) lives in a particular
417 section. For example, this small program uses several specific section names:
421 struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
422 struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
423 char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
424 int init_data __attribute__ ((section ("INITDATA")));
428 /* Initialize stack pointer */
429 init_sp (stack + sizeof (stack));
431 /* Initialize initialized data */
432 memcpy (&init_data, &data, &edata - &data);
434 /* Turn on the serial ports */
439 Use the ``section`` attribute with
440 *global* variables and not *local* variables,
441 as shown in the example.
443 You may use the ``section`` attribute with initialized or
444 uninitialized global variables but the linker requires
445 each object be defined once, with the exception that uninitialized
446 variables tentatively go in the ``common`` (or ``bss``) section
447 and can be multiply 'defined'. Using the ``section`` attribute
448 changes what section the variable goes into and may cause the
449 linker to issue an error if an uninitialized variable has multiple
450 definitions. You can force a variable to be initialized with the
451 :option:`-fno-common` flag or the ``nocommon`` attribute.
453 Some file formats do not support arbitrary sections so the ``section``
454 attribute is not available on all platforms.
455 If you need to map the entire contents of a module to a particular
456 section, consider using the facilities of the linker instead.
458 .. index:: tls_model variable attribute
460 .. var-attr:: tls_model ("tls_model")
462 The ``tls_model`` attribute sets thread-local storage model
463 (see :ref:`thread-local`) of a particular ``__thread`` variable,
464 overriding :option:`-ftls-model=` command-line switch on a per-variable
466 The :samp:`{tls_model}` argument should be one of ``global-dynamic``,
467 ``local-dynamic``, ``initial-exec`` or ``local-exec``.
469 Not all targets support this attribute.
471 .. index:: unused variable attribute
475 This attribute, attached to a variable or structure field, means that
476 the variable or field is meant to be possibly unused. GCC does not
477 produce a warning for this variable or field.
479 .. index:: used variable attribute
483 This attribute, attached to a variable with static storage, means that
484 the variable must be emitted even if it appears that the variable is not
487 When applied to a static data member of a C++ class template, the
488 attribute also means that the member is instantiated if the
489 class itself is instantiated.
491 .. index:: retain variable attribute
495 For ELF targets that support the GNU or FreeBSD OSABIs, this attribute
496 will save the variable from linker garbage collection. To support
497 this behavior, variables that have not been placed in specific sections
498 (e.g. by the ``section`` attribute, or the ``-fdata-sections`` option),
499 will be placed in new, unique sections.
501 This additional functionality requires Binutils version 2.36 or later.
503 .. index:: uninitialized variable attribute
505 .. var-attr:: uninitialized
507 This attribute, attached to a variable with automatic storage, means that
508 the variable should not be automatically initialized by the compiler when
509 the option ``-ftrivial-auto-var-init`` presents.
511 With the option ``-ftrivial-auto-var-init``, all the automatic variables
512 that do not have explicit initializers will be initialized by the compiler.
513 These additional compiler initializations might incur run-time overhead,
514 sometimes dramatically. This attribute can be used to mark some variables
515 to be excluded from such automatical initialization in order to reduce runtime
518 This attribute has no effect when the option ``-ftrivial-auto-var-init``
521 .. index:: vector_size variable attribute
523 .. var-attr:: vector_size (bytes)
525 This attribute specifies the vector size for the type of the declared
526 variable, measured in bytes. The type to which it applies is known as
527 the :dfn:`base type`. The :samp:`{bytes}` argument must be a positive
528 power-of-two multiple of the base type size. For example, the declaration:
532 int foo __attribute__ ((vector_size (16)));
534 causes the compiler to set the mode for ``foo``, to be 16 bytes,
535 divided into ``int`` sized units. Assuming a 32-bit ``int``,
536 ``foo`` 's type is a vector of four units of four bytes each, and
537 the corresponding mode of ``foo`` is ``V4SI``.
538 See :ref:`vector-extensions`, for details of manipulating vector variables.
540 This attribute is only applicable to integral and floating scalars,
541 although arrays, pointers, and function return values are allowed in
542 conjunction with this construct.
544 Aggregates with this attribute are invalid, even if they are of the same
545 size as a corresponding scalar. For example, the declaration:
550 struct S __attribute__ ((vector_size (16))) foo;
552 is invalid even if the size of the structure is the same as the size of
555 .. index:: visibility variable attribute
557 .. var-attr:: visibility ("visibility_type")
559 This attribute affects the linkage of the declaration to which it is attached.
560 The :var-attr:`visibility` attribute is described in
561 :ref:`common-function-attributes`.
563 .. index:: weak variable attribute
567 The :var-attr:`weak` attribute is described in
568 :ref:`common-function-attributes`.
570 .. index:: noinit variable attribute
574 Any data with the :var-attr:`noinit` attribute will not be initialized by
575 the C runtime startup code, or the program loader. Not initializing
576 data in this way can reduce program startup times.
578 This attribute is specific to ELF targets and relies on the linker
579 script to place sections with the ``.noinit`` prefix in the right
582 .. index:: persistent variable attribute
584 .. var-attr:: persistent
586 Any data with the :var-attr:`persistent` attribute will not be initialized by
587 the C runtime startup code, but will be initialized by the program
588 loader. This enables the value of the variable to :samp:`persist`
589 between processor resets.
591 This attribute is specific to ELF targets and relies on the linker
592 script to place the sections with the ``.persistent`` prefix in the
593 right location. Specifically, some type of non-volatile, writeable
596 .. index:: objc_nullability variable attribute
598 .. var-attr:: objc_nullability (nullability kind)
602 Objective-C and Objective-C++ only
604 This attribute applies to pointer variables only. It allows marking the
605 pointer with one of four possible values describing the conditions under
606 which the pointer might have a ``nil`` value. In most cases, the
607 attribute is intended to be an internal representation for property and
608 method nullability (specified by language keywords); it is not recommended
611 When :samp:`{nullability kind}` is ``"unspecified"`` or ``0``, nothing is
612 known about the conditions in which the pointer might be ``nil``. Making
613 this state specific serves to avoid false positives in diagnostics.
615 When :samp:`{nullability kind}` is ``"nonnull"`` or ``1``, the pointer has
616 no meaning if it is ``nil`` and thus the compiler is free to emit
617 diagnostics if it can be determined that the value will be ``nil``.
619 When :samp:`{nullability kind}` is ``"nullable"`` or ``2``, the pointer might
620 be ``nil`` and carry meaning as such.
622 When :samp:`{nullability kind}` is ``"resettable"`` or ``3`` (used only in
623 the context of property attribute lists) this describes the case in which a
624 property setter may take the value ``nil`` (which perhaps causes the
625 property to be reset in some manner to a default) but for which the property
626 getter will never validly return ``nil``.
628 .. _arc-variable-attributes:
630 ARC Variable Attributes
631 ^^^^^^^^^^^^^^^^^^^^^^^
633 .. index:: aux variable attribute, ARC
635 .. arc-var-attr:: aux
637 The :var-attr:`aux` attribute is used to directly access the ARC's
638 auxiliary register space from C. The auxilirary register number is
639 given via attribute argument.
641 .. _avr-variable-attributes:
643 AVR Variable Attributes
644 ^^^^^^^^^^^^^^^^^^^^^^^
646 .. index:: progmem variable attribute, AVR
648 .. avr-var-attr:: progmem
650 The :var-attr:`progmem` attribute is used on the AVR to place read-only
651 data in the non-volatile program memory (flash). The :var-attr:`progmem`
652 attribute accomplishes this by putting respective variables into a
653 section whose name starts with ``.progmem``.
655 This attribute works similar to the ``section`` attribute
656 but adds additional checking.
658 * Ordinary AVR cores with 32 general purpose registers:
659 :var-attr:`progmem` affects the location
660 of the data but not how this data is accessed.
661 In order to read data located with the :var-attr:`progmem` attribute
662 (inline) assembler must be used.
666 /* Use custom macros from http://nongnu.org/avr-libc/user-manual/AVR-LibC */
667 #include <avr/pgmspace.h>
669 /* Locate var in flash memory */
670 const int var[2] PROGMEM = { 1, 2 };
674 /* Access var[] by accessor macro from avr/pgmspace.h */
675 return (int) pgm_read_word (& var[i]);
678 AVR is a Harvard architecture processor and data and read-only data
679 normally resides in the data memory (RAM).
681 See also the :ref:`avr-named-address-spaces` section for
682 an alternate way to locate and access data in flash memory.
684 * AVR cores with flash memory visible in the RAM address range:
685 On such devices, there is no need for attribute :var-attr:`progmem` or
686 :ref:`avr-named-address-spaces` qualifier at all.
687 Just use standard C / C++. The compiler will generate ``LD*``
688 instructions. As flash memory is visible in the RAM address range,
689 and the default linker script does *not* locate ``.rodata`` in
690 RAM, no special features are needed in order not to waste RAM for
691 read-only data or to read from flash. You might even get slightly better
693 avoiding :var-attr:`progmem` and ``__flash``. This applies to devices from
694 families ``avrtiny`` and ``avrxmega3``, see :ref:`avr-options` for
697 * Reduced AVR Tiny cores like ATtiny40:
698 The compiler adds ``0x4000``
699 to the addresses of objects and declarations in :var-attr:`progmem` and locates
700 the objects in flash memory, namely in section ``.progmem.data``.
701 The offset is needed because the flash memory is visible in the RAM
702 address space starting at address ``0x4000``.
704 Data in :var-attr:`progmem` can be accessed by means of ordinary C |nbsp| code,
705 no special functions or macros are needed.
709 /* var is located in flash memory */
710 extern const int var[2] __attribute__((progmem));
717 Please notice that on these devices, there is no need for :var-attr:`progmem`
720 .. index:: io variable attribute, AVR
722 .. avr-var-attr:: io, io (addr)
724 Variables with the :var-attr:`io` attribute are used to address
725 memory-mapped peripherals in the io address range.
726 If an address is specified, the variable
727 is assigned that address, and the value is interpreted as an
728 address in the data address space.
733 volatile int porta __attribute__((io (0x22)));
735 The address specified in the address in the data address range.
737 Otherwise, the variable it is not assigned an address, but the
738 compiler will still use in/out instructions where applicable,
739 assuming some other module assigns an address in the io address range.
744 extern volatile int porta __attribute__((io));
746 .. index:: io_low variable attribute, AVR
748 .. avr-var-attr:: io_low, io_low (addr)
750 This is like the :var-attr:`io` attribute, but additionally it informs the
751 compiler that the object lies in the lower half of the I/O area,
752 allowing the use of ``cbi``, ``sbi``, ``sbic`` and ``sbis``
755 .. index:: address variable attribute, AVR
757 .. avr-var-attr:: address, address (addr)
759 Variables with the :var-attr:`address` attribute are used to address
760 memory-mapped peripherals that may lie outside the io address range.
764 volatile int porta __attribute__((address (0x600)));
766 .. index:: absdata variable attribute, AVR
768 .. avr-var-attr:: absdata
770 Variables in static storage and with the :var-attr:`absdata` attribute can
771 be accessed by the ``LDS`` and ``STS`` instructions which take
774 * This attribute is only supported for the reduced AVR Tiny core
777 * You must make sure that respective data is located in the
778 address range ``0x40``... ``0xbf`` accessible by
779 ``LDS`` and ``STS``. One way to achieve this as an
780 appropriate linker description file.
782 * If the location does not fit the address range of ``LDS``
783 and ``STS``, there is currently (Binutils 2.26) just an unspecific
786 ``module.cc:(.text+0x1c): warning: internal error: out of range error``
788 See also the :option:`-mabsdata` :ref:`avr-options`.
790 .. _blackfin-variable-attributes:
792 Blackfin Variable Attributes
793 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
795 Three attributes are currently defined for the Blackfin.
797 .. index:: l1_data variable attribute, Blackfin, l1_data_A variable attribute, Blackfin, l1_data_B variable attribute, Blackfin
799 .. blackfin-var-attr:: l1_data, l1_data_A, l1_data_B
801 Use these attributes on the Blackfin to place the variable into L1 Data SRAM.
802 Variables with :var-attr:`l1_data` attribute are put into the specific section
803 named ``.l1.data``. Those with ``l1_data_A`` attribute are put into
804 the specific section named ``.l1.data.A``. Those with ``l1_data_B``
805 attribute are put into the specific section named ``.l1.data.B``.
807 .. index:: l2 variable attribute, Blackfin
809 .. blackfin-var-attr:: l2
811 Use this attribute on the Blackfin to place the variable into L2 SRAM.
812 Variables with :var-attr:`l2` attribute are put into the specific section
815 .. _h8-300-variable-attributes:
817 H8/300 Variable Attributes
818 ^^^^^^^^^^^^^^^^^^^^^^^^^^
820 These variable attributes are available for H8/300 targets:
822 .. index:: eightbit_data variable attribute, H8/300, eight-bit data on the H8/300, H8/300H, and H8S
824 .. h8-300-var-attr:: eightbit_data
826 Use this attribute on the H8/300, H8/300H, and H8S to indicate that the specified
827 variable should be placed into the eight-bit data section.
828 The compiler generates more efficient code for certain operations
829 on data in the eight-bit data area. Note the eight-bit data area is limited to
832 You must use GAS and GLD from GNU binutils version 2.7 or later for
833 this attribute to work correctly.
835 .. index:: tiny_data variable attribute, H8/300, tiny data section on the H8/300H and H8S
837 .. h8-300-var-attr:: tiny_data
839 Use this attribute on the H8/300H and H8S to indicate that the specified
840 variable should be placed into the tiny data section.
841 The compiler generates more efficient code for loads and stores
842 on data in the tiny data section. Note the tiny data area is limited to
843 slightly under 32KB of data.
845 .. _ia-64-variable-attributes:
847 IA-64 Variable Attributes
848 ^^^^^^^^^^^^^^^^^^^^^^^^^
850 The IA-64 back end supports the following variable attribute:
852 .. index:: model variable attribute, IA-64
854 .. ia-64-var-attr:: model (model-name)
856 On IA-64, use this attribute to set the addressability of an object.
857 At present, the only supported identifier for :samp:`{model-name}` is
858 ``small``, indicating addressability via 'small' (22-bit)
859 addresses (so that their addresses can be loaded with the ``addl``
860 instruction). Caveat: such addressing is by definition not position
861 independent and hence this attribute must not be used for objects
862 defined by shared libraries.
864 .. _loongarch-variable-attributes:
866 LoongArch Variable Attributes
867 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
869 One attribute is currently defined for the LoongArch.
871 .. index:: model variable attribute, LoongArch
873 .. loongarch-var-attr:: model("name")
875 Use this attribute on the LoongArch to use a different code model for
876 addressing this variable, than the code model specified by the global
877 :option:`-mcmodel` option. This attribute is mostly useful if a
878 ``section`` attribute and/or a linker script will locate this object
879 specially. Currently the only supported values of :samp:`{name}` are
880 ``normal`` and ``extreme``.
882 .. _m32r-d-variable-attributes:
884 M32R/D Variable Attributes
885 ^^^^^^^^^^^^^^^^^^^^^^^^^^
887 One attribute is currently defined for the M32R/D.
889 .. index:: model-name variable attribute, M32R/D, variable addressability on the M32R/D
891 .. m32r-d-var-attr:: model (model-name)
893 Use this attribute on the M32R/D to set the addressability of an object.
894 The identifier :samp:`{model-name}` is one of ``small``, ``medium``,
895 or ``large``, representing each of the code models.
897 Small model objects live in the lower 16MB of memory (so that their
898 addresses can be loaded with the ``ld24`` instruction).
900 Medium and large model objects may live anywhere in the 32-bit address space
901 (the compiler generates ``seth/add3`` instructions to load their
904 .. _mep-variable-attributes:
906 MeP Variable Attributes
907 ^^^^^^^^^^^^^^^^^^^^^^^
909 The MeP target has a number of addressing modes and busses. The
910 :var-attr:`near` space spans the standard memory space's first 16 megabytes
911 (24 bits). The :var-attr:`far` space spans the entire 32-bit memory space.
912 The :var-attr:`based` space is a 128-byte region in the memory space that
913 is addressed relative to the ``$tp`` register. The :var-attr:`tiny`
914 space is a 65536-byte region relative to the ``$gp`` register. In
915 addition to these memory regions, the MeP target has a separate 16-bit
916 control bus which is specified with :var-attr:`cb` attributes.
918 .. index:: based variable attribute, MeP
920 .. mep-var-attr:: based
922 Any variable with the :var-attr:`based` attribute is assigned to the
923 ``.based`` section, and is accessed with relative to the
926 .. index:: tiny variable attribute, MeP
928 .. mep-var-attr:: tiny
930 Likewise, the :var-attr:`tiny` attribute assigned variables to the
931 ``.tiny`` section, relative to the ``$gp`` register.
933 .. index:: near variable attribute, MeP
937 Variables with the :var-attr:`near` attribute are assumed to have addresses
938 that fit in a 24-bit addressing mode. This is the default for large
939 variables (``-mtiny=4`` is the default) but this attribute can
940 override ``-mtiny=`` for small variables, or override ``-ml``.
942 .. index:: far variable attribute, MeP
944 .. mep-var-attr:: far
946 Variables with the :var-attr:`far` attribute are addressed using a full
947 32-bit address. Since this covers the entire memory space, this
948 allows modules to make no assumptions about where variables might be
951 .. mep-var-attr:: io, io (addr)
953 Variables with the :var-attr:`io` attribute are used to address
954 memory-mapped peripherals. If an address is specified, the variable
955 is assigned that address, else it is not assigned an address (it is
956 assumed some other module assigns an address). Example:
960 int timer_count __attribute__((io(0x123)));
962 .. index:: cb variable attribute, MeP
964 .. mep-var-attr:: cb, cb (addr)
966 Variables with the :var-attr:`cb` attribute are used to access the control
967 bus, using special instructions. ``addr`` indicates the control bus
972 int cpu_clock __attribute__((cb(0x123)));
974 .. _microsoft-windows-variable-attributes:
976 Microsoft Windows Variable Attributes
977 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
979 You can use these attributes on Microsoft Windows targets.
980 :ref:`x86-variable-attributes` for additional Windows compatibility
981 attributes available on all x86 targets.
983 .. index:: dllimport variable attribute, dllexport variable attribute
985 .. microsoft-windows-var-attr:: dllimport, dllexport
987 The :var-attr:`dllimport` and :var-attr:`dllexport` attributes are described in
988 :ref:`microsoft-windows-function-attributes`.
990 .. index:: selectany variable attribute
992 .. microsoft-windows-var-attr:: selectany
994 The :microsoft-windows-var-attr:`selectany` attribute causes an initialized global variable to
995 have link-once semantics. When multiple definitions of the variable are
996 encountered by the linker, the first is selected and the remainder are
997 discarded. Following usage by the Microsoft compiler, the linker is told
998 *not* to warn about size or content differences of the multiple
1001 Although the primary usage of this attribute is for POD types, the
1002 attribute can also be applied to global C++ objects that are initialized
1003 by a constructor. In this case, the static initialization and destruction
1004 code for the object is emitted in each translation defining the object,
1005 but the calls to the constructor and destructor are protected by a
1006 link-once guard variable.
1008 The :microsoft-windows-var-attr:`selectany` attribute is only available on Microsoft Windows
1009 targets. You can use ``__declspec (selectany)`` as a synonym for
1010 ``__attribute__ ((selectany))`` for compatibility with other
1013 .. index:: shared variable attribute
1015 .. microsoft-windows-var-attr:: shared
1017 On Microsoft Windows, in addition to putting variable definitions in a named
1018 section, the section can also be shared among all running copies of an
1019 executable or DLL. For example, this small program defines shared data
1020 by putting it in a named section :microsoft-windows-var-attr:`shared` and marking the section
1025 int foo __attribute__((section ("shared"), shared)) = 0;
1030 /* Read and write foo. All running
1031 copies see the same value. */
1035 You may only use the :var-attr:`shared` attribute along with ``section``
1036 attribute with a fully-initialized global definition because of the way
1037 linkers work. See ``section`` attribute for more information.
1039 The :microsoft-windows-var-attr:`shared` attribute is only available on Microsoft Windows.
1041 .. _msp430-variable-attributes:
1043 MSP430 Variable Attributes
1044 ^^^^^^^^^^^^^^^^^^^^^^^^^^
1046 .. index:: upper variable attribute, MSP430, either variable attribute, MSP430
1048 .. msp430-var-attr:: upper, either
1050 These attributes are the same as the MSP430 function attributes of the
1051 same name (see :ref:`msp430-function-attributes`).
1053 .. index:: lower variable attribute, MSP430
1055 .. msp430-var-attr:: lower
1057 This option behaves mostly the same as the MSP430 function attribute of the
1058 same name (see :ref:`msp430-function-attributes`), but it has some additional
1061 If :option:`-mdata-region=` { ``upper,either,none`` } has been passed, or
1062 the ``section`` attribute is applied to a variable, the compiler will
1063 generate 430X instructions to handle it. This is because the compiler has
1064 to assume that the variable could get placed in the upper memory region
1065 (above address 0xFFFF). Marking the variable with the :var-attr:`lower` attribute
1066 informs the compiler that the variable will be placed in lower memory so it
1067 is safe to use 430 instructions to handle it.
1069 In the case of the ``section`` attribute, the section name given
1070 will be used, and the ``.lower`` prefix will not be added.
1072 .. _nvidia-ptx-variable-attributes:
1074 Nvidia PTX Variable Attributes
1075 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1077 These variable attributes are supported by the Nvidia PTX back end:
1079 .. index:: shared attribute, Nvidia PTX
1081 .. nvidia-ptx-var-attr:: shared
1083 Use this attribute to place a variable in the ``.shared`` memory space.
1084 This memory space is private to each cooperative thread array; only threads
1085 within one thread block refer to the same instance of the variable.
1086 The runtime does not initialize variables in this memory space.
1088 .. _powerpc-variable-attributes:
1090 PowerPC Variable Attributes
1091 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1093 Three attributes currently are defined for PowerPC configurations:
1094 ``altivec``, :var-attr:`ms_struct` and ``gcc_struct``.
1096 .. index:: ms_struct variable attribute, PowerPC, gcc_struct variable attribute, PowerPC
1098 For full documentation of the struct attributes please see the
1099 documentation in :ref:`x86-variable-attributes`.
1101 .. index:: altivec variable attribute, PowerPC
1103 For documentation of ``altivec`` attribute please see the
1104 documentation in :ref:`powerpc-type-attributes`.
1106 .. index:: saddr variable attribute, RL78
1108 .. _rl78-variable-attributes:
1110 RL78 Variable Attributes
1111 ^^^^^^^^^^^^^^^^^^^^^^^^
1113 The RL78 back end supports the ``saddr`` variable attribute. This
1114 specifies placement of the corresponding variable in the SADDR area,
1115 which can be accessed more efficiently than the default memory region.
1117 .. _v850-variable-attributes:
1119 V850 Variable Attributes
1120 ^^^^^^^^^^^^^^^^^^^^^^^^
1122 These variable attributes are supported by the V850 back end:
1124 .. index:: sda variable attribute, V850
1126 .. v850-var-attr:: sda
1128 Use this attribute to explicitly place a variable in the small data area,
1129 which can hold up to 64 kilobytes.
1131 .. index:: tda variable attribute, V850
1133 .. v850-var-attr:: tda
1135 Use this attribute to explicitly place a variable in the tiny data area,
1136 which can hold up to 256 bytes in total.
1138 .. index:: zda variable attribute, V850
1140 .. v850-var-attr:: zda
1142 Use this attribute to explicitly place a variable in the first 32 kilobytes
1145 .. _x86-variable-attributes:
1147 x86 Variable Attributes
1148 ^^^^^^^^^^^^^^^^^^^^^^^
1150 Two attributes are currently defined for x86 configurations:
1151 :x86-var-attr:`ms_struct` and ``gcc_struct``.
1153 .. index:: ms_struct variable attribute, x86, gcc_struct variable attribute, x86
1155 .. x86-var-attr:: ms_struct, gcc_struct
1157 If :var-attr:`packed` is used on a structure, or if bit-fields are used,
1158 it may be that the Microsoft ABI lays out the structure differently
1159 than the way GCC normally does. Particularly when moving packed
1160 data between functions compiled with GCC and the native Microsoft compiler
1161 (either via function call or as data in a file), it may be necessary to access
1164 The :var-attr:`ms_struct` and ``gcc_struct`` attributes correspond
1165 to the :option:`-mms-bitfields` and :option:`-mno-ms-bitfields`
1166 command-line options, respectively;
1167 see :ref:`x86-options`, for details of how structure layout is affected.
1168 See :ref:`x86-type-attributes`, for information about the corresponding
1169 attributes on types.
1171 .. _xstormy16-variable-attributes:
1173 Xstormy16 Variable Attributes
1174 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1176 One attribute is currently defined for xstormy16 configurations:
1177 :xstormy16-var-attr:`below100`.
1179 .. index:: below100 variable attribute, Xstormy16
1181 .. xstormy16-var-attr:: below100
1183 If a variable has the :xstormy16-var-attr:`below100` attribute (``BELOW100`` is
1184 allowed also), GCC places the variable in the first 0x100 bytes of
1185 memory and use special opcodes to access it. Such variables are
1186 placed in either the ``.bss_below100`` section or the
1187 ``.data_below100`` section.