简体   繁体   English

为什么使用 GCC 编译时,do.o 文件的 a.bss 部分的大小为 0?

[英]Why do .o files appear to have a .bss section of size 0 when compiled with GCC?

While working on a C program, I altered my build to produce ao file instead of a stand-alone binary.在处理 C 程序时,我更改了构建以生成 ao 文件而不是独立的二进制文件。 When doing so, I noticed that the size of the.bss section of the resulting.o file is 0, according to readelf -S even though there are clearly uninitialized globals in the source.这样做时,我注意到结果 .o 文件的 .bss 部分的大小是 0,根据readelf -S即使源中有明显未初始化的全局变量。

I can replicate this with a simple program, test.c:我可以用一个简单的程序 test.c 来复制它:

char arr[42];
int main(int argc, char **argv){
  return 0;
}

The binary resulting from gcc -o test test.c has an eighty byte.bss section, according to readelf, which is roughly what I'd expect, as I expected some minor overhead.根据 readelf 的说法,由gcc -o test test.c产生的二进制文件有一个 80 字节的.bss 部分,这大致是我所期望的,因为我预计会有一些小的开销。

However, if I build ao file with gcc -c -o test.o test.c , the size of the bss section is reported as zero.但是,如果我使用gcc -c -o test.o test.c构建 ao 文件,则 bss 部分的大小报告为零。 Clearly I'm misunderstanding something about the nature of ELF object files, but I'm not quite sure what I'm missing.显然我误解了 ELF object 文件的性质,但我不太确定我错过了什么。

ELF object files have a dummy.bss segment. ELF object 文件有一个 dummy.bss 段。 They can not be loaded directly (unlike an executable or shared library).它们不能直接加载(与可执行文件或共享库不同)。

The information about the global variables is stored in the.symtab section, which is used by the linker to construct the.bss and other sections in the final binary.有关全局变量的信息存储在.symtab 部分中,linker 使用该部分来构造最终二进制文件中的.bss 和其他部分。

Try doing readelf -s test.o to reveal the symbol table.尝试执行readelf -s test.o以显示符号表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM