- 学习__attribute__用法
1.描述
__attribute__作用:设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
1.1.Declaring Attributes of Functions
In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your code more carefully.
The keyword __attribute__allows you to specify special attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses. The following attributes are currently defined for functions on all targets: noreturn, noinline, always_inline, pure, const, format, format_arg, no_instrument_function, section, constructor, destructor, used, unused, deprecated, weak, malloc, and alias. Several other attributes are defined for functions on particular target systems.
案例1:__attribute__noreturn
This attribute tells the compiler that the function won’t ever return, and this can be used to suppress errors about code paths not being reached. The C library functions abort() and exit() are both declared with this attribute:
extern void exit(int) __attribute__((noreturn));
extern void abort(void) __attribute__((noreturn));
1.2.Specifying Attributes of Variables
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. Some attributes are currently defined generically for variables.
1.3.Specifying Attributes of Types
The keyword __attribute__allows you to specify special attributes of struct and union types when you define such types. This keyword is followed by an attribute specification inside double parentheses. Six attributes are currently defined for types: aligned, packed, transparent_union, unused, deprecated and may_alias.
案例3:__attribute__((section(“section_name”)))
Normally, the compiler places the objects it generates in sections like 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 __attribute__ ((section ("INITDATA"))) = 0;
关于__init理解:
// include/linux/init.h:
#define __init __section(.init.text) __cold notrace
/* Simple shorthand for a section definition */
#ifndef __section
# define __section(S) __attribute__ ((__section__(#S)))
#endif
It instructs the compiler to mark this function in a special way. At the end the linker collects all functions with this mark at the end (or begin) of the binary file. When the kernel starts, this code runs only once (initialization). After it runs, the kernel can free this memory to reuse it and you will see the kernel.
参考资料:
http://www.unixwiz.net/techtips/gnu-c-attributes.html
https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Attributes.html
https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html