The keyword __attribute__
allows you to specify special
attributes of variables or structure fields. This keyword is followed
by an attribute specification inside double parentheses. Four
attributes are currently defined for variables: aligned
,
mode
, packed
, and section
. Other attributes are
defined for functions, and thus not documented here;
see Function Attributes.
aligned (alignment)
int x __attribute__ ((aligned (16))) = 0;
causes the compiler to allocate the global variable x
on a
16-byte boundary. On a 68040, this could be used in conjunction with
an asm
expression to access the move16
instruction which
requires 16-byte aligned operands.
You can also specify the alignment of structure fields. For example, to
create a double-word aligned int
pair, you could write:
struct foo { int x[2] __attribute__ ((aligned (8))); };
This is an alternative to creating a union with a double
member
that forces the union to be double-word aligned.
It is not possible to specify the alignment of functions; the alignment of functions is determined by the machine's requirements and cannot be changed. You cannot specify alignment for a typedef name because such a name is just an alias, not a distinct type.
The aligned
attribute can only increase the alignment; but you
can decrease it by specifying packed
as well. See below.
The linker of your operating system imposes a maximum alignment. If the linker aligns each object file on a four byte boundary, then it is beyond the compiler's power to cause anything to be aligned to a larger boundary than that. For example, if the linker happens to put this object file at address 136 (eight more than a multiple of 64), then the compiler cannot guarantee an alignment of more than 8 just by aligning variables in the object file.
mode (mode)
packed
packed
attribute specifies that a variable or structure field
should have the smallest possible alignment---one byte for a variable,
and one bit for a field, unless you specify a larger value with the
aligned
attribute.
Here is a structure in which the field x
is packed, so that it
immediately follows a
:
struct foo { char a; int x[2] __attribute__ ((packed)); };
section ("section-name")
data
and bss
. Sometimes, however, you need additional sections,
or you need certain particular variables to appear in special sections,
for example to map to special hardware. The section
attribute specifies that a variable (or function) lives in a particular
section. For example, this small program uses several specific section names:
struct duart a __attribute__ ((section ("DUART_A"))) = { 0 }; struct duart b __attribute__ ((section ("DUART_B"))) = { 0 }; char stack[10000] __attribute__ ((section ("STACK"))) = { 0 }; int init_data_copy __attribute__ ((section ("INITDATACOPY"))) = 0; main() { /* Initialize stack pointer */ init_sp (stack + sizeof (stack)); /* Initialize initialized data */ memcpy (&init_data_copy, &data, &edata - &data); /* Turn on the serial ports */ init_duart (&a); init_duart (&b); }
Use the section
attribute with an initialized definition
of a global variable, as shown in the example. GNU CC issues
a warning and otherwise ignores the section
attribute in
uninitialized variable declarations.
You may only use the section
attribute with a fully initialized
global definition because of the way linkers work.
The linker requires each object be defined once, with the exception that
uninitialized variables tentatively go in the common
(or bss
)
section and can be multiply "defined".
Some file formats do not support arbitrary sections so the section
attribute is not available on all platforms.
If you need to map the entire contents of a module to a particular
section, consider using the facilities of the linker instead.
To specify multiple attributes, separate them by commas within the
double parentheses: for example, `__attribute__ ((aligned (16), packed))
'.