[英]why .bss explicitly initialize global variable to zero?
I am generating mips disassembly in order to simulating it. 我正在生成MIPS反汇编以对其进行仿真。 I need to have big data to work on it but I don't want to have big assembly files so I wanted to work on a big uninitialized array (and then possibly initialize it in my simulator...).
我需要处理大数据,但我不想拥有大汇编文件,所以我想处理一个未初始化的大数组(然后可能在模拟器中对其进行初始化...)。 So I need this array to be global.
所以我需要这个数组是全局的。 And global variables seem to be put on the .bss section to be initialized when the page is actually accessed.
实际访问页面时,似乎将全局变量放在要初始化的.bss节中。 The problem is in my binary the array is in the .bss section, but is explicitly filled with zero...This is not the behaviour expected if I understood correctly what I have found on internet...Is there a way for saying to the compiler (or linker, or loader...I don't understand well which one do what for that) to not really put zero in this array ?
问题是在我的二进制文件中,数组在.bss部分中,但显式填充为零...如果我正确理解了我在互联网上找到的内容,这不是预期的行为...有没有办法说编译器(或链接器或加载器...我不太明白哪一个为该做什么)没有真正在该数组中放入零? Or alternatively, can we have an option while compiling, or a C instruction for saying we don't want this array for being initialized with 0 ?
或者,我们可以在编译时有一个选项,还是可以使用C指令说明我们不希望将此数组初始化为0? (I tried to change the array section with attribute but it is still initialized with 0).
(我试图用属性更改数组部分,但它仍用0初始化)。
By the way, I am generating my disassembly file with objdump, and it normally skip blocks of zeroes, but I really need the other blocks of zeroes to be disassembled, so I using the "-z" option. 顺便说一句,我正在用objdump生成反汇编文件,它通常会跳过零块,但是我确实需要反汇编其他零块,因此我使用了“ -z”选项。
What I really don't understand is that everywhere I looked, it was said that .bss section didn't really put zero in the binary file... 我真正不明白的是,无论我到哪里看,都说.bss节没有真正在二进制文件中放入零...
The data for the .bss section isn't stored in the compiled object files because, well, there is no data—the compiler puts variables in that segment precisely because they should be zero-initialized. .bss节的数据未存储在已编译的目标文件中,因为没有数据-编译器将变量精确地放在该段中是因为它们应该被零初始化。
When the OS loads the executable, it just looks at the size of the .bss segment, allocates that much memory, and zero-initializes it for you. 当操作系统加载可执行文件时,它只会查看.bss段的大小,分配那么多的内存,然后为您将其初始化为零。 By not storing that data in the executable file, it reduces loading times.
通过不将数据存储在可执行文件中,可以减少加载时间。
If you want data to be initialized with certain data, then give it an initializer in your code. 如果要使用某些数据初始化数据,请在代码中为其提供初始化程序。 The compiler will then put it in the .data segment (initialized data) instead of .bss (uninitialized data).
然后,编译器会将其放在.data段(初始化数据)中,而不是.bss(未初始化数据)中。 When the OS then loads the executable, it will allocate the memory for the data and then copy it in from the executable.
然后,操作系统加载可执行文件时,它将为数据分配内存,然后从可执行文件中将其复制到其中。 This takes extra I/O, but your data is explicitly initialized how you want it.
这需要额外的I / O,但是您的数据已明确初始化为所需的方式。
Alternatively, you could leave the data stay in the .bss segment and then initialize it yourself at runtime. 或者,您可以将数据保留在.bss段中,然后在运行时自行初始化。 If the data is quick and easy to generate at runtime, it might be faster to recompute it at startup rather then read it off of disk.
如果数据在运行时快速且易于生成,则在启动时重新计算而不是从磁盘读取数据可能会更快。 But those situations are probably rare.
但是这些情况可能很少见。
I suspect that using the -z
option is causing objdump
to show you zeroes for the .bss
, even though the zeroes are not actually in your binary. 我怀疑使用
-z
选项会使objdump
为.bss
显示零,即使这些零实际上不在二进制文件中也是如此。 Try using od -t x4
to get a simple hexadecimal dump of what is really in the binary. 尝试使用
od -t x4
获得二进制文件中真正内容的简单十六进制转储。 If od
shows you blocks of zeroes, then they really are in the binary. 如果
od
显示零块,则它们实际上在二进制中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.