利用gcc的__attribute__编译属性section子项构建初始化函数表
来源:互联网 发布:java课程设计案例源码 编辑:程序博客网 时间:2024/06/05 17:17
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
获取内置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: 0x8049768aha!call_ptr: 0x804976caloha!call_ptr: 0x8049770hello!
Have Fun!
转自:https://my.oschina.net/u/180497/blog/177206
0 0
- 利用gcc的__attribute__编译属性section子项构建初始化函数表
- 利用gcc的__attribute__编译属性section子项构建初始化函数表
- gcc的__attribute__((section(“”)))属性含义
- __attribute__编译属性---section
- gcc 的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- gcc的__attribute__编译属性
- 利用 GCC 的 __attribute__ 属性的section选项 来控制 数据区的基地址
- [随笔]gcc的__attribute__编译属性
- gcc 的__attribute__ 属性
- gcc编译器 __attribute__ ((section(".")))
- gcc 函数属性 __attribute__ ((constructor))使用
- mysql命令导出导入sql文件
- 一个月学习C++笔记(七)
- 向量在游戏中的应用 2017-1-30
- python技巧(11)
- Linux同步的实现初步学习
- 利用gcc的__attribute__编译属性section子项构建初始化函数表
- 简析TCP的三次握手与四次分手
- 【寒假任务】 洛谷1177 快速排序
- 2015年第六届蓝桥杯省赛真题之一奖券数目
- 使用JavaAPI操作HDFS
- 数据挖掘-oneR算法-Iris数据集分析-使用oneR算法进行分类预测(五)
- 【t081】序列长度
- Spring Boot 菜鸟教程 22 Redis
- tensorflow线性回归