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

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

问题描述

我正在Linux上使用C语言工作。我已经看到gcc的用法 __ section __ 属性(尤其是在Linux内核中)可将数据(通常是函数指针)收集到自定义ELF部分中。

I'm working in C on Linux. 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).

请参见< a href = http://mgalgs.github.io/2013/05/10/hacking-your-ELF-for-fun-and-profit.html rel = noreferrer>此处自定义ELF部分的使用。

See here for another example of using custom ELF sections.

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

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