如何获取C(gcc)中自定义ELF部分的开始和结束地址? [英] How do you get the start and end addresses of a custom ELF section in C (gcc)?

查看:991
本文介绍了如何获取C(gcc)中自定义ELF部分的开始和结束地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了gcc的用法 __section __ 属性(尤其是在Linux内核中)来收集自定义ELF段中的数据(通常是函数指针)。这些自定义部分中的东西是如何被检索和使用的?

I've seen the usage of of the gcc __section__ attribute (especially in the Linux kernel) to collect data (usually function pointers) into custom ELF sections. How is the "stuff" that gets put in those custom sections retrieved and used?

推荐答案

只要部分名称导致一个有效的C变量名称 gcc ld ,相反)会生成两个魔术变量: __ start_SECTION __ stop_SECTION 。这些可用于检索节的开始和结束地址,如下所示:

As long as the section name results in a valid C variable name, gcc (ld, rather) generates two magic variables: __start_SECTION and __stop_SECTION. Those can be used to retrieve the start and end addresses of a section, like so:

/**
 * Assuming you've tagged some stuff earlier with:
 * __attribute((__section__("my_custom_section")))
 */

struct thing *iter = &__start_my_custom_section;

for ( ; iter < &__stop_my_custom_section; ++iter) {
    /* do something with *iter */
}

我找不到这个功能的任何正式文档,只有几个不起眼的邮件列表引用。如果您知道文档的位置,请发表评论!

I couldn’t find any formal documentation for this feature, only a few obscure mailing list references. If you know where the docs are, drop a comment!

如果您使用自己的链接器脚本(如Linux内核那样),您必须添加魔术变量(参见 vmlinux.lds。[Sh]

If you're using your own linker script (as the Linux kernel does) you'll have to add the magic variables yourself (see vmlinux.lds.[Sh] and this SO answer).

参见 使用自定义ELF部分。

See here for another example of using custom ELF sections.

这篇关于如何获取C(gcc)中自定义ELF部分的开始和结束地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆