问题描述
如何静态告诉Visual C ++将全局变量放置在内存中的给定绝对地址上,例如 __ attribute __((at(address)))
是?
How can I statically tell Visual C++ to place a global variable at a given absolute address in memory, like what __attribute__((at(address)))
does?
推荐答案
可以完成,但我不认为有预定义的方法可以完成,因此需要进行一些实验.即使您在用户代码执行开始时在运行时创建变量,即使我看不出任何好处.
It can be done but I don't believe there is a predefined way to do it so it will take some experimentation. Even though I don't see much benefit if you create your variable at run time just at the start of user code execution.
因此,请首先使用分配,在其中指定用于初始化变量的部分/段. MS 特定说明符.然后,在实际情况下启动应用程序,转储或调试它,然后查看变量出现的位置.注意重定位(有一些方法可以强制不重定位,但是并不能保证始终都遵循它们).另一种方法是在您的应用中使用一些代码像这样一个可以找到您定义的部分的地址.
So first specify the section/segment where to init your variable using the allocate MS specific specifier. Then either start your application in real scenario, dump it or debug it and see where your variable appears. Watch for relocations (there is some ways to try to enforce no relocation but they are not guaranteed to be honored all the time). Another way is to use some code in your app like this one to find the address of the section you defined.
如果由于某种原因而无法获得一致的行为,则可以使用此实用程序来操纵目标文件的虚拟地址.总的来说,除了障碍,但总的来说,我不明白为什么如果您足够坚持,那么您将无法使它适用于您的特定方案.
If you for some reason cannot get a consistent behavior you can use this utility to manipulate the virtual address of your object file. All in all except hurdles along the way but overall I don't see why you wouldn't be able to get it to work for your specific scenario if you are persistent enough.
这篇关于如何在内存中的给定绝对地址处放置变量(使用Visual C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!