获取内存部分信息 [英] Getting memory section information

查看:125
本文介绍了获取内存部分信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释我如何以下code ++工程?

Can somebody explain me how the following code works?

# if   defined(__ELF__)
#  define __SECTION_FLAGS   ", \"aw\" , @progbits"
    /* writable flag needed for ld ".[cd]tors" sections bug workaround) */
# elif defined(__COFF__)
#  define __SECTION_FLAGS   ", \"dr\""
    /* untested, may be writable flag needed */
# endif

asm
(
    ".section .ctors" __SECTION_FLAGS "\n"
    ".globl __ctors_begin__\n"
    "__ctors_begin__:\n"
    ".previous\n"
);
asm /* ld ".[cd]tors" sections bug workaround */
(
    ".section .ctors0" __SECTION_FLAGS "\n"
    ".globl __ctors0_begin__\n"
    "__ctors0_begin__:\n"
    ".previous\n"
);

同样,我们也越来越 __ __ ctors_end __ __ ctors0_end 和析构函数的位置也获得这种方式。经过一番LD错误变通方法由指针指向的所有功能,从 __ __ ctors_begin __ __ ctors_end 执行。我不知道汇编,这code对我来说是不可能的跨preT。

Similarly we are getting __ctors_end__ , __ctors0_end__ and destructors location is also obtained this way. After some ld bug workarounds all functions pointed by pointers from __ctors_begin__ to __ctors_end__ are executed. I don't know assembler and this code is impossible for me to interpret.

BTW:我知道,从C调用C ++ contructors /析构函数不是一个任务被认为是安全的或容易

BTW: I know that invoking C++ contructors/destructors from C is not a task to be considered safe or easy.

推荐答案

这是不实际$由CPU执行C $ C,但它添加到目标文件的元数据。它告诉链接器创建在相同的部分,在那里的建设者都存储在最终的可执行的一些全局变量( __ ctors_begin __ 在上面的例子)(=部分)(即节名为 .ctors )。为了使它工作,你就必须确保与文件中的开始变量与第一,与结束的文件变量与最后(但也许你也可以使用 __ SECTION_FLAGS )。这就给了你,你要找的内存范围。

This isn't actually code executed by the CPU but it's added to the metadata of the object files. It tells the linker to create some global variables (__ctors_begin__ in the example above) in the same section (= part) of the final executable where the constructors are stored (that section is called .ctors). To make it work, you just have to make sure that the file with the "begin" variable is linked first and the file with the "end" variable is linked last (but maybe you can also control this with the __SECTION_FLAGS). That gives you the memory range you're looking for.

至于安全:那么,C ++运行时并不神奇。不知怎的,它必须知道如何在启动时运行的所有构造函数和析构函数和不改变所有的时间。因此,对于你的编译器的主版本号,这应该是pretty安全。此外,你就会知道pretty很快,当它突破: - )

As for "safe": Well, the C++ runtime isn't magic. Somehow it has to know how to run all the constructors and destructors at startup and that doesn't change all the time. So for a major version number of your compiler, this should be pretty safe. Also, you'll know pretty soon when it breaks :-)

这篇关于获取内存部分信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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