什么所有局部变量都转到 Data/BSS 段?
What all local variables goto Data/BSS segment?
此处 nm
的手册页:MAN NM 说
The symbol type. At least the following types are used; others are, as
well, depending on the object file format. If lowercase, the symbol is
usually local; if uppercase, the symbol is global (external)
下面有 "b" 和 "B" 代表 "uninitialized data section (known as BSS)" 和 "d" 和 "D" 代表 "initialized data section"
但我认为局部变量总是转到 Stack/Heap 而不是 "Data" 或 "BSS" 部分。那么nm在说什么局部变量呢?
"local" 在此上下文中表示文件范围。
即:
static int local_data = 1; /* initialised local data */
static int local_bss; /* uninitialised local bss */
int global_data = 1; /* initialised global data */
int global_bss; /* uninitialised global bss */
void main (void)
{
// Some code
}
函数范围的静态变量进入数据或 BSS(或文本)部分,具体取决于初始化:
void somefunc(void)
{
static char array1[256] = ""; // Goes in BSS, probably
static char array2[256] = "ABCDEF…XYZ"; // Goes in Data
static const char string[] = "Kleptomanic Hypochondriac";
// Goes in Text, probably
…
}
类似的规则适用于在文件范围定义的变量,有或没有 static
存储 class 说明符——未初始化或零初始化的数据进入 BSS 部分;初始化数据进入数据部分;常量数据可能会出现在文本部分。
此处 nm
的手册页:MAN NM 说
The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external)
下面有 "b" 和 "B" 代表 "uninitialized data section (known as BSS)" 和 "d" 和 "D" 代表 "initialized data section"
但我认为局部变量总是转到 Stack/Heap 而不是 "Data" 或 "BSS" 部分。那么nm在说什么局部变量呢?
"local" 在此上下文中表示文件范围。
即:
static int local_data = 1; /* initialised local data */
static int local_bss; /* uninitialised local bss */
int global_data = 1; /* initialised global data */
int global_bss; /* uninitialised global bss */
void main (void)
{
// Some code
}
函数范围的静态变量进入数据或 BSS(或文本)部分,具体取决于初始化:
void somefunc(void)
{
static char array1[256] = ""; // Goes in BSS, probably
static char array2[256] = "ABCDEF…XYZ"; // Goes in Data
static const char string[] = "Kleptomanic Hypochondriac";
// Goes in Text, probably
…
}
类似的规则适用于在文件范围定义的变量,有或没有 static
存储 class 说明符——未初始化或零初始化的数据进入 BSS 部分;初始化数据进入数据部分;常量数据可能会出现在文本部分。