什么时候将变量放在.rdata节而不是.text节中? [英] When is a variable placed in `.rdata` section and not in `.text` section?
问题描述
我试图理解.rdata
部分与.text
部分的含义.我正在尝试以下简单程序
I am trying to understand the implications of .rdata
section vs .text
section. I am trying a simple program as below
int main()
{
const int a = 10;
printf("%d\n", a);
return 0;
}
当我通过gcc -o a.out sample.c -Wl,Map,test.map
生成并转储map
文件并搜索sample.o
时,我发现以下分配情况
When I build and dump the map
file through gcc -o a.out sample.c -Wl,Map,test.map
and search for sample.o
, I find the following allocations
.text 0x0040138c 0x34 sample.o
.data 0x00402000 0x0 sample.o
.rdata 0x00403064 0x8 sample.o
.eh_frame 0x00404060 0x38 sample.o
.bss 0x00405020 0x0 sample.o
现在,如果我稍稍修改程序以使a
成为全局变量
Now, if I modify my program slightly to make a
a global variable as
const int a = 10;
int main()
{
printf("%d\n", a);
return 0;
}
通过重复与上述相同的步骤,我发现分配情况如下
By repeating the same step as above, I observe that the allocations are as below
.text 0x0040138c 0x2c sample.o
.data 0x00402000 0x0 sample.o
.rdata 0x00403064 0xc sample.o
.eh_frame 0x00404060 0x38 sample.o
.bss 0x00405020 0x0 sample.o
其中清楚地显示a
被分配为.rdata
部分为
Wherein it clearly shows a
is allocated into .rdata
section as
.rdata 0x00403064 0xc sample.o
0x00403064 a
从这些实验中,我了解到global
const
已分配到.rdata
部分中,而.text
部分的大小已减小.因此,我假设在第一个示例中,将a
分配给了.text
部分.
From these experiments, I understand the global
const
are alocated into .rdata
section, whereas the .text
section size has come down. Hence, I presume that a
was allocated into .text
section in the first example.
我的问题是:
-
确定
const
变量在.rdata
或.text
中的位置时是否考虑其范围?
Is scope of a
const
variable considered while determining it's place in.rdata
or.text
?
从我的实验中,我发现该变量在分配到.text
部分时需要8个字节,而在.rdata
部分中则需要4个字节.造成这种差异的原因是什么?
From my experiment, I observe that the variable required 8 bytes when it was allocated into .text
section as compared to 4 bytes in .rdata
section. What is the reason for this difference?
如果本地const
变量太多,则相应的.text
部分的大小将显着增加.在这种情况下,推荐的编程实践是什么?
If there are too many local const
variables, then the size of corresponding .text
section will increase significantly. What is the recommended programming practice in this scenario?
非常感谢.
推荐答案
在第一种情况下,变量被声明为局部变量.它具有自动"存储期限,这意味着它在封闭范围的末尾消失了.由于其存储持续时间,它不能永久占用任何内存(这与const
无关).因此,它通常存储在堆栈中或寄存器中.
In the first case, the variable is declared as a local variable. It has "automatic" storage duration, which means that it goes away at the end of the enclosing scope. It cannot occupy any piece of memory permanently because of its storage duration (this is true regardless of const
). Thus, it is usually stored on the stack, or in a register.
在第二种情况下,该变量被声明为全局变量.它具有静态的存储期限,因此在程序的整个生命周期中都将持续存在.可以将其存储在许多地方,例如.data
,.bss
,.text
或.rdata
(或.rodata
).
In the second case, the variable is declared as a global variable. It has static storage duration, so it persists for the lifetime of the program. This can be stored in many places, such as .data
, .bss
, .text
, or .rdata
(or .rodata
).
.data
通常用于具有某些预定义(非零)内容的可写静态数据,例如全局int foo = 42;
. .bss
用于初始化为零(或未初始化)的可写静态数据. rdata
用于常量静态数据,例如字符串和const
变量.当然,这些用法都是一般"的,并且随编译器的不同而有所不同.
.data
is generally used for writable static data with some predefined (nonzero) content, e.g. a global int foo = 42;
. .bss
is used for writable static data initialized to zero (or not initialized). rdata
is used for constant static data like strings and const
variables. Of course, these uses are all "in general" and may vary from compiler to compiler.
那么为什么.text
在第一种情况下变大?这是因为编译器必须生成一些额外的指令才能将10
加载到堆栈或寄存器中.
So why did .text
get bigger in the first case? It is because the compiler had to generate some extra instructions to load 10
on the stack or into a register.
这篇关于什么时候将变量放在.rdata节而不是.text节中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!