Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__attribute__ in GNU C

Why and how is __attribute__ used in GNU C programs?

like image 648
sud03r Avatar asked Sep 22 '09 07:09

sud03r


People also ask

What is __ attribute __ (( section?

__attribute__((section("name"))) variable attribute data and . bss . However, you might require additional data sections or you might want a variable to appear in a special section, for example, to map to special hardware. The section attribute specifies that a variable must be placed in a particular data section.

What is __ Attribute__ unused ))?

__attribute__((unused)) variable attribute Normally, the compiler warns if a variable is declared but is never referenced. This attribute informs the compiler that you expect a variable to be unused and tells it not to issue a warning if it is not used.

What is naked function in C?

The naked storage-class attribute is a Microsoft-specific extension to the C language. For functions declared with the naked storage-class attribute, the compiler generates code without prolog and epilog code. You can use this feature to write your own prolog/epilog code sequences using inline assembler code.

What is weak attribute in C?

weak. The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations.


2 Answers

For what GCC and GCC-compatible compilers use __attribute__ most other compilers use #pragma directives.

I think GCC's solution is better since the required behavior of an unrecognised #pragma is to ignore it, whereas if you use a compiler that does not understand an __attribute__ specification, it will not compile - which is generally better, since you then know what you need to port.

Attribute specifications are used to specify aspects of types, data, and functions such as storage and alignment that cannot be specified using C. Often these are target specific, mostly they are non-portable, certainly between compilers, and often between targets. Avoid their use except where it is absolutely necessary to use correct functions of code.

like image 93
Clifford Avatar answered Sep 20 '22 06:09

Clifford


One use is for enforcing memory alignment on variables and structure members. For example

float vect[4] __attribute__((aligned(16))); 

Will ensure that vect will be placed on a 16 byte memory boundary. I do not know if that is a gcc-ism or more generally applicable.

The compiler will typically only aligned vect on a 4 byte boundary. With 16 byte alignment it can be used directly with SIMD load instructions where you'd load it up into a 128 bit registers that allows addition, subtraction, dot products and all manner of vector operations.

Sometimes you want alignment so that a structure can be directly overlaid onto memory-mapped hardware registers. Or it has to be aligned so the hardware can write into it directly used a direct memory access (DMA) mechanism.

like image 22
George Phillips Avatar answered Sep 19 '22 06:09

George Phillips