gcc的__attribute__编译属性有很多子项,用于改变作用对象的特性。这里讨论section子项的作用。

__attribute__的section子项使用方式为:

__attribute__((section("section_name")))

其作用是将作用的函数或数据放入指定名为"section_name"的段。

看以下程序片段:

#include <unistd.h>
#include <stdint.h>
#include <stdio.h>typedef void (*myown_call)(void);extern myown_call _myown_start;
extern myown_call _myown_end;#define _init __attribute__((unused, section(".myown")))
#define func_init(func) myown_call _fn_##func _init = funcstatic void mspec1(void)
{write(1, "aha!\n", 5);
}static void mspec2(void)
{write(1, "aloha!\n", 7);
}static void mspec3(void)
{write(1, "hello!\n", 7);
}func_init(mspec1);
func_init(mspec2);
func_init(mspec3);/* exactly like below:
static myown_call mc1  __attribute__((unused, section(".myown"))) = mspec1;
static myown_call mc2  __attribute__((unused, section(".myown"))) = mspec2;
static myown_call mc3  __attribute__((unused, section(".myown"))) = mspec3;
*/void do_initcalls(void)
{myown_call *call_ptr = &_myown_start;do {fprintf (stderr, "call_ptr: %p\n", call_ptr);(*call_ptr)();++call_ptr;} while (call_ptr < &_myown_end);}int main(void)
{do_initcalls();return 0;
}

在自定义的.myown段依次填入mspec1/mspec2/mspec3的函数指针,并在do_initcalls中依次调用,从而达到构造并调用初始化函数列表的目的。

两个extern变量:

extern myown_call _myown_start;
extern myown_call _myown_end;

来自ld的链接脚本,可以使用:

ld --verbose > s.lds

获取内置lds脚本,并在:

__bss_start = .;

之前添加以下内容:

_myown_start = .;
.myown              : { *(.myown) } = 0x90000000
_myown_end = .;
code_segment   : { *(code_segment) }

即定义了.myown段及_myown_start/_myown_end变量(0x90000000这个数值可能需要调整)。
保存修改后的链接器脚本,假设程序为s.c,链接器脚本保存为s.lds,使用以下命令编译:

gcc s.c -Wl,-Ts.lds

执行结果:

[root@localhost ]# ./a.out 
call_ptr: 0x8049768
aha!
call_ptr: 0x804976c
aloha!
call_ptr: 0x8049770
hello!

--------------------- 
作者:期待一片自己的蓝天 
来源:CSDN 
原文:https://blog.csdn.net/nyist327/article/details/59481809 
版权声明:本文为博主原创文章,转载请附上博文链接!

__attribute__((section(section_name)))相关推荐

  1. __attribute__((section(”name“)))的一些理解

    __attribute__((section("name")))是gcc编译器支持的一个编译特性(arm编译器也支持此特性),实现在编译时把某个函数/数据放到name的数据段中. ...

  2. __attribute__((section(name))) study again

    目录 code tree source code init.h init.c gen-ld.sh init-section.lds ld.lds main.c module1_init.c modul ...

  3. __attribute__((section(”XXXX“)))的编译属性---section

    __attribute__((section("name")))是gcc编译器支持的一个编译特性(arm编译器也支持此特性),实现在编译时把某个函数/数据放到name的数据段中. ...

  4. __attribute__((used)) __attribute__((section(x)))

    1.通知编译器在目标文件中保留一个静态函数,即使它没有被引用. 2.标记为attribute__((used))的函数被标记在目标文件中,以避免链接器删除未使用的节. 3.静态变量也可以标记为used ...

  5. __attribute__((section(x))) 使用详解

    无论是GNU还是ARM的编译器, 都支持 __attribute__所指定的编译属性,这里着重讲解一下在KEIL 环境下__attribute__中的section的使用方法. section关键字可 ...

  6. 利用gcc的__attribute__编译属性section子项构建初始化函数表【转】

    转自:https://my.oschina.net/u/180497/blog/177206 gcc的__attribute__编译属性有很多子项,用于改变作用对象的特性.这里讨论section子项的 ...

  7. __attribute__之section详解

    前言 第一次接触 "section" 是在公司的一个STM32的项目代码中,前工程师将所有的初始化函数都使用的"section"进行设定了属性.当时知道其目的, ...

  8. __attribute__编绎属性、关于__init、__initdata、__exit、__exitdata及类似的宏、关于initcall的一些宏定义、__setup宏的来源及使用...

    1. gcc的__attribute__编绎属性 要了解Linux Kernel代码的分段信息,需要了解一下gcc的__attribute__的编绎属性,__attribute__主要用于改变所声明或 ...

  9. __attribute__

    前言 本文介绍__attribute__ 的使用方法 __attribute__介绍 要了解Linux Kernel代码的分段信息,需要了解一下gcc的__attribute__的编绎属性,__att ...

最新文章

  1. 亲试虚拟机为REDHAT5装VM-tool
  2. springMVC,mybatis配置事务
  3. 讲真!朋友圈别设置三天可见!
  4. 有缘网分布式爬虫案例
  5. another mysql daemon_MySQL错误Another MySQL daemon already running with the same unix socket.
  6. FPGA状态机三段式
  7. 监控与分析工具nmon的安装与使用
  8. Python带你在朋友圈环球旅行
  9. 皮克定理(格点三角形求面积或求三角形里格点(整点)个数)
  10. python爬取携程旅游_「携程网 机票」使用requests爬取携程网飞机票价格实例 - seo实验室...
  11. python获取期货数据_【python量化】期货ML策略(一)数据获取
  12. [转]基于大规模语料的新词发现算法
  13. matlab保存pgm图形,matlab读写pgm文件
  14. python清华大学出版社答案_清华大学出版社的这本Python入门书,出版8年 仍经久不衰...
  15. Rust编译`Couldn‘t resolve host name (Could not resolve host: crates)` 错误的解决办法
  16. 苹果11怎样设置自动锁屏 iPhone11自动锁屏操作方法
  17. 如何将判决书上的文字提取出来
  18. MATLAB的MEX文件编写和调试
  19. Hadoop-HA高可用架构分布式环境搭建教程
  20. python 动态导入模块和类

热门文章

  1. Spring框架----Spring的bean的作用范围
  2. Android继承AppCompatActivity实现全屏设置
  3. 阿里天池全国社保比赛心得
  4. css3-background clip 和background origin
  5. maven pom.xml指定jdk
  6. Android实例-屏幕操持常亮(XE8+小米2)
  7. 【Centos配置2】远程管理必备工具配置:ssh/vnc/webadmin
  8. idea卸载不干净怎么办_家里拖地老是不干净怎么办,来跟我学!
  9. linux 渲染程序图层管理,Linux驱动多图层开发--lcdc/framebuffer的注册(RK平台)
  10. C语言口令,某一本地口令验证函数(c语言环境,x86_32指令集)包含如下关键代码:某用户的口令保存在字符数组origpassw...