目录
前言
__attribute__((section("section_name"))),其作用是将作用的函数或数据放入指定名为"section_name"对应的段中。
__attribute__主要用于改变所声明或定义的函数或 数据的特性,它有很多子项,用于改变作用对象的特性。比如对函数,noline将禁止进行内联扩展、noreturn表示没有返回值、pure表明函数除 返回值外,不会通过其它(如全局变量、指针)对函数外部产生任何影响。
一、编译时为变量指定段
Example
/* in RO section */
const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };
/* in RW section */
long long rw[10] __attribute__ ((section ("RW")));
/* in ZI section *
long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/
Note:This variable attribute is a GNU compiler extension supported by the ARM compiler.(MDK)
二、编译时为函数指定段
Example
In the following example, Function_Attributes_section_0 is placed into the RO section new_section rather than .text.
void Function_Attributes_section_0 (void)
__attribute__ ((section ("new_section")));
void Function_Attributes_section_0 (void)
{
static int aStatic =0;
aStatic++;
}
In the following example, section function attribute overrides the #pragma arm section setting.
#pragma arm section code="foo"
int f2()
{
return 1;
} // into the 'foo' area
__attribute__ ((section ("bar"))) int f3()
{
return 1;
} // into the 'bar' area
int f4()
{
return 1;
} // into the 'foo' area
#pragma arm section
三、带电复位后不清0的.noinit变量
.bss段的变量在芯片启动的.init4阶段会被清0,但是定义在.bss的.noinit段中的变量在.init4阶段不会被清0。单片机不掉电复位后,.noinit段的这些变量的值依然存在,可以继续使用。
.noinit段的定义方法:int bar __attribute__ ((section (".noinit")));
不能指定初值,因为这些变量是在.bss段。
(验证:定义一个.noinit段变量,按下开发板的复位键(单片机不掉电复位),这个变量的值不会被清0。)