深入理解BSS(Block Started by Symbol)
Posted veli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深入理解BSS(Block Started by Symbol)相关的知识,希望对你有一定的参考价值。
理解ELF的BSS section, 可以概括为:
- Uninitialized global/static data
- "Block Started by Symbol"
- "Better Save Space"
- Has section header but occupies no space
CSAPP一书对bss的描述如下:
.bss: 未被初始化的全局的C变量。这一节在o文件中不占实际的空间,只是一个place holder。o文件格式之所以区分初始化的变量和未被初始化的变量是因为处于空间利用率上的考虑。没有被初始化的变量确实没有必要占用实际的磁盘空间。
关于"Better Save Space", 维基百科上是这么说的, (原文点这里)
Peter van der Linden, a C programmer and author, says, "Some people like to remember it as \'Better Save Space.\' Since the BSS segment only holds variables that don\'t have any value yet, it doesn\'t actually need to store the image of these variables. The size that BSS will require at runtime is recorded in the object file, but BSS (unlike the data segment) doesn\'t take up any actual space in the object file."
另外, 在Memory Layout of C Programs一文中,对BSS的描述如下:
Uninitialized data segment, often called the “bss” segment, named after an ancient assembler operator that stood for “block started by symbol.” Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code. For instance a variable declared static int i; would be contained in the BSS segment. For instance a global variable declared int j; would be contained in the BSS segment.
这里明确了那些初始化为0的全局变量和静态变量也是被保存在bss中。 也就是说,bss包含:
- 所有未被显示地初始化的全局变量和静态变量
- 所有被显示地初始化为0的全局变量和静态变量
1 If a variable declared static int i; it would be contained in the BSS segment. 2 If a global variable declared int j; it would be contained in the BSS segment. 3 If a variable declared static int i = 0; it would be contained in the BSS segment. 4 If a global variable declared int j = 0; it would be contained in the BSS segment.
以上是关于深入理解BSS(Block Started by Symbol)的主要内容,如果未能解决你的问题,请参考以下文章
(深入理解计算机系统) bss段,data段text段堆(heap)和栈(stack)(C/C++存储类型总结)(内存管理)