linux内核elf区段的,如何在C (gcc)中获得自定义ELF部分的开始和结束地址?

3

Collecting the information together from various answers, here is a working example of how to collect information into a custom linker section and then read the information from that section using the magic variables __start_SECTION and __stop_SECTION in your C program, where SECTION is the name of the section in the link map.

收集信息从不同的答案,这是一个工作的例子如何收集信息到一个自定义链接器部分,然后读取的信息部分使用魔法变量__start_SECTION和__stop_SECTION在C程序中,其中部分是部分的链接地图的名称。

The __start_SECTION and __stop_SECTION variables are made available by the linker so explicit extern references need to be created for these variables when they are used from C code.

__start_SECTION和__stop_SECTION变量由链接器提供,因此在使用C代码时需要为这些变量创建显式的外部引用。

There are also some problems if the alignment used by the compiler for calculating pointer/array offsets is different than the alignment of the objects packed in each section by the linker. One solution (used in this example) is to store only a pointer to the data in the linker section.

如果编译器用于计算指针/数组偏移量的对齐方式与链接器在每个部分中填充的对象的对齐方式不同,那么也存在一些问题。一个解决方案(在本例中使用)是仅在链接器部分中存储指向数据的指针。

#include

struct thing {

int val;

const char* str;

int another_val;

};

struct thing data1 = {1, "one"};

struct thing data2 = {2, "two"};

/* The following two pointers will be placed in "my_custom_section".

* Store pointers (instead of structs) in "my_custom_section" to ensure

* matching alignment when accessed using iterator in main(). */

struct thing *p_one __attribute__((section("my_custom_section"))) = &data1;

struct thing *p_two __attribute__((section("my_custom_section"))) = &data2;

/* The linker automatically creates these symbols for "my_custom_section". */

extern struct thing *__start_my_custom_section;

extern struct thing *__stop_my_custom_section;

int main(void) {

struct thing **iter = &__start_my_custom_section;

for ( ; iter < &__stop_my_custom_section; ++iter) {

printf("Have thing %d: '%s'\n", (*iter)->val, (*iter)->str);

}

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值