.bss vs COMMON:什么去哪儿了? [英] .bss vs COMMON: what goes where?
问题描述
从我的书中:
.bss:
未初始化的全局 C 变量
Uninitialized global C variables
常见:
尚未分配的未初始化数据对象
Uninitalized data objects that are not yet allocated
我不得不说,我看不出明显的区别.我什至不太明白什么是未初始化、未分配的数据对象……似乎什么都没有.我使用 GNU 的 readelf
工具尝试查看一些简单的 C 代码,但找不到单个 COMMON 符号.我读过诸如 FORTRAN 的 COMMON 类型是 COMMON 符号的示例之类的东西 - 但我不知道 FORTRAN
I have to say, I don't quite see a clear distinction. I don't even quite understand what an uninitizalied, non-allocated data object is...seems like nothing. I've used GNU's readelf
tool to try to take a look at some simple C code, but can't find a single COMMON symbol. I've read things like FORTRAN's COMMON type is an example of a COMMON symbol - but I don't know FORTRAN
谁能帮我区分这两者?如果可能的话,希望有一个 C 示例?非常感谢.
Can someone possibly distinguish the two for me? If at all possible, hopefully with a C example? Greatly appreciated.
编辑:来自 这个 在这里发布变量 c:
edit: from this post, the variable c here:
int c;
int main() {} ...
应该是常见的.但是使用 objdump -t
显示 c 在 .bss...
should be COMMON. But using objdump -t
shows for me that c is in .bss...
困惑
推荐答案
// file a.c
// file-scope
int a = 0; // goes into BSS
a.c
编译成目标文件a.o
后,a
符号进入BSS部分.
after compilation of a.c
into object file a.o
, a
symbol goes into BSS section.
// file b.c
// file-scope
int b; // goes into COMMON section
b.c
编译成目标文件b.o
后,b
符号进入COMMON段.
after compilation of b.c
into object file b.o
, b
symbol goes into COMMON section.
a.o
和 b.o
链接后,a
和 b
符号都进入 BSS 部分.通用符号只存在于目标文件中,不存在于可执行文件中.Unix 中 COMMON 符号的思想是在特定条件下允许在单个公共符号下对同一变量(在不同编译单元中)进行多个外部定义.
After linking of a.o
and b.o
, both a
and b
symbols goes into BSS section. Common symbols only exist in object files, not in executable files. The idea of COMMON symbols in Unix is to allow multiple external definitions of a same variable (in different compilation units) under a single common symbol under certain conditions.
这篇关于.bss vs COMMON:什么去哪儿了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!