__attribute__实际上是gcc专有的一种语法,是用来设置函数属性、变量属性、类属性的
语法:之前在C中的结构体对齐中提到过,当时是用来告诉编译器这个结构体的对齐方式
,其实他还有很多种用法,可以设置很多的属性。
语法: __attribute__ (parameter)
对于变量:
int a __attribute__ ((xxxxx)) = 10; //也可以放在变量的前面,比较灵活
int a __attribute__ ((xxxxx)); // 也可以放在变量的前面,比较灵活
对于结构体:
struct info{
.....
} __attribute__ ((xxxxx));

struct info{
.....
} __attribute__ ((xxxxx)) sb;

struct info{
.....
}sb __attribute__ ((xxxxx));
对于函数:
void __attribute__ ((xxxxx)) func(void); //注意可以放在void的前面,总之只要
void func(void) __attribute__ ((xxxxx)); //是在 func(void) 的左右都行

void __attribute__ ((xxxxx)) func(void)
{

}

例如:
struct fd{
...
...
}__sttribute__ ((align(4))) fd; //指定struct fd类的字节对齐方式

int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
这里涉及到两个属性:

weak属性:使得cpu_mmc_init是一个弱标号(我估计是内部链接),而不是一个全局标号
,另外即使cpu_mmc_init函数没有定义,调用cpu_mmc_init编译器也是不会报错的,不过
没有意义,所以一般会和alias属性连用。

alias属性:指定cpu_mmc_init是后面 "__def_mmc_init" 函数的一个别名,所以
cpu_mmc_init函数如果没有定义,那么调用cpu_mmc_init其实就是会调用__def_mmc_init
函数,如果定义就不会调用这个了,注意__def_mmc_init函数是一定要有定义的。

unused属性:指定这个变量或者函数如果没有被使用也不要输出警告信息。

section属性:指定该函数或者是变量最后链接在我们的指定段中,用法:section
(".u_boot_cmd"),注意了,我们链接脚本中使用的段名其实就是一个变量的形式,他是
不需要定义的,能够直接用,也就是说,我们可以在连接脚本中随意使用一个段名,如果
我们在我们的C中有该段的内容就会链接到该段中,如果没有就直接跳过。注意在我们的
内核驱动中一般是用 __section__ 来代替我们的section,具体差别不详。
__attribute__ ((__section__(".u_kernel_cmd")))

align属性:设置字节对齐