/* Force a compilation error if condition is false, but also produce a result * (of value 0 and type size_t), so it can be used e.g. in a structure * initializer (or wherever else comma expressions aren't permitted). */ /* Linux calls these BUILD_BUG_ON_ZERO/_NULL, which is rather misleading. */ #define STATIC_ZERO_ASSERT(condition) (sizeof(struct { int:-!(condition); }) ) #define STATIC_NULL_ASSERT(condition) ((void *)STATIC_ZERO_ASSERT(condition) ) /* Force a compilation error if condition is false */ #define STATIC_ASSERT(condition) ((void)STATIC_ZERO_ASSERT(condition))
struct p { int a; char b; short c; }__attribute__((aligned(4))) pp; struct m { char a; int b; short c; }__attribute__((aligned(4))) mm; struct o { int a; char b; short c; }oo; struct x { int a; char b; struct p px; short c; }__attribute__((aligned(8))) xx; int main() { printf("sizeof(int)=%d,sizeof(short)=%d.sizeof(char)=%d\n",sizeof(int) ,sizeof(short),sizeof(char)); printf("pp=%d,mm=%d \n", sizeof(pp),sizeof(mm)); printf("oo=%d,xx=%d \n", sizeof(oo),sizeof(xx)); return0; }
提到section,就得说RO RI ZI了,在ARM编译器编译之后,代码被划分为不同的段,RO Section(ReadOnly)中存放代码段和常量,RW Section(ReadWrite)中存放可读写静态变量和全局变量,ZI Section(ZeroInit)是存放在RW段中初始化为0的变量。 于是本文的大体意思就清晰了,attribute((section(“section_name”))),其作用是将作用的函数或数据放入指定名为”section_name”对应的段中。
1)、编译时为变量指定段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
__attribute__((section("name"))) RealView Compilation Tools for µVision Compiler Reference Guide Version 4.0 Home > Compiler-specific Features > Variable attributes > __attribute__((section("name"))) 4.5.6. __attribute__((section("name"))) Normally, the ARM compiler places the objects it generates in sections like 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. If you use the section attribute, read-only variables are placed in RO data sections, read-write variables are placed in RW data sections unless you use the zero_init attribute. In thiscase, the variable is placed in a ZI section. Note This variable attribute is a GNU compiler extension supported by the ARM compiler. Example /* in RO section */ constint descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 }; /* in RW section */ longlong rw[10] __attribute__ ((section ("RW"))); /* in ZI section * long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/
__attribute__((section("name"))) RealView Compilation Tools for µVision Compiler Reference Guide Version 4.0 Home > Compiler-specific Features > Function attributes > __attribute__((section("name"))) 4.3.13. __attribute__((section("name"))) The section function attribute enables you to place code in different sections of the image. Note This function attribute is a GNU compiler extension that is supported by the ARM compiler. 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) { staticint aStatic =0; aStatic++; } In the following example, section function attribute overrides the #pragma arm section setting. #pragma arm section code="foo" int f2() { return1; } // into the 'foo' area __attribute__ ((section ("bar"))) int f3() { return1; } // into the 'bar' area int f4() { return1; } // into the 'foo' area #pragma arm section