section ("section-name")

本文介绍如何在程序中使用section属性来指定全局变量位于特定部分,包括初始化定义和注意事项,以及某些文件格式不支持任意部分的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
              
              main()
              {
                /* Initialize stack pointer */
                init_sp (stack + sizeof (stack));
              
                /* Initialize initialized data */
                memcpy (&init_data, &data, &edata - &data);
              
                /* Turn on the serial ports */
                init_duart (&a);
                init_duart (&b);
              }
         

    Use the section attribute with an initialized definition of a global variable, as shown in the example. GCC issues a warning and otherwise ignores the section attribute in uninitialized variable declarations.

    You may only use the section attribute with a fully initialized global definition because of the way linkers work. The linker requires each object be defined once, with the exception that uninitialized variables tentatively go in the common (or bss) section and can be multiply “defined”. You can force a variable to be initialized with the -fno-common flag or the nocommon attribute.

    Some file formats do not support arbitrary sections so the section attribute is not available on all platforms. If you need to map the entire contents of a module to a particular section, consider using the facilities of the linker instead.

 

原文见:http://mcuos.com/archiver/tid-2780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值