MODULE_AUTHOR宏(二)

前言
前一片文章MODULE_AUTHOR宏(一)中提到MODULE_AUTHOR宏展开的结果为 :

static const char __mod_authorXX[]  \
 __used  __attribute__ ((section(".modinfo"),unused,aligned(1)))  \
 = "author = tuxr"

实质是定义了一个常数字符串变量,本节在对该常数变量的属性attribute再做一些解析.

__attribute__关键字的用法可以参考手册Specifying Attributes of Variables

section(".modinfo")解析:
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:
(通常,编译器放置object到data和bss 部分。然而,某些时候你需要额外的section,或者你需要某些特殊的变量出现在专用的section,比如映射到专用的硬件。section熟性指定变量(或函数)放置到特殊的section。举例,以下小程序使用了多个专用的section名:)

      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.
(section 属性用在已初始化的全局变量上。对于未初始化的变量定义,gcc会发出警告然后忽略section属性。)

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.
(你只能把section属性用在已初始化的全局变量上是因为链接器的工作方式上。链接器需要每个object只定义一次,除了未初始化的变量它们被临时放在在common(or bss)部分且可以被多次定义。你可以强制一个变量初始化使用-fno-common标志或nocommon属性。)

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.
(某些文件格式不支持随意的section,所以section属性不是在所有平台都可用。如果你需要映射模块到某section,考虑使用连接器的工具。)

总结一下section (“section-name”)的用法:
1.对已初始化的全局变量,该变量将被链接到section-name
2.对未初始化的全局变量,该变量忽略section属性,并链接到bss
(深入理解计算机系统) bss段,data段、text段、堆(heap)和栈(stack)

static const char __mod_authorXX[]  \
 __used  __attribute__((section(".modinfo"),unused,aligned(1)))  \
 = "author = tuxr"

所以根据以上对section的理解,可以确定__mod_authorXX字符串常量被放置在TEXT段的.modinfo section;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值