为什么BSS段默认为“16”?

时间:2021-01-19 13:38:28

As per my knowledge, segmentation for c program is:

据我所知,c程序的分割是:

        High address
|---------------------------|
|env/cmd line args vars     |
|---------------------------|
|      stack segment        |--> uninitialized auto vars
|---------------------------|
|---------------------------|
|---------------------------|
|      heap segment         |--> dynamic allocated memory
|---------------------------|
|      BSS segment          |--> uninitialized static/global vars
|---------------------------|
|      data segment         |--> initialized static/global vars
|---------------------------|
|      text segment         |--> initialized auto vars/exec instructions
|---------------------------|
        Low address

On my RHEL 5.4 64-bit machine, for below c program

在我的RHEL 5.4 64位机器上,用于c程序以下

#include <stdio.h>
int main()
{
}

when I do:

当我做:

# size a.out
   text    data     bss     dec     hex filename
   1259     540      16    1815     717 a.out

I am unable to understand why is

我不明白为什么是这样

bss=16

bss = 16

As I am not declaring/initializing any global/static vars?

因为我没有声明/初始化任何全局/静态vars?

1 个解决方案

#1


2  

It's worse on Windows with gcc:

使用gcc时,情况更糟:

main.c:

c:

#include <stdio.h>

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

compile:

编译:

C:\>gcc main.c

size:

大小:

C:\>size a.exe
   text    data     bss     dec     hex filename
   6936    1580    1004    9520    2530 a.exe

bss includes the whole linked executable and in this case various libraries are linked in which do use static c initialisation.

bss包含整个链接的可执行文件,在这种情况下,各种库被链接,其中确实使用了静态c初始化。

Using -nostartfiles gets a much better result on windows. You can also try with -nostdlib and -nodefaultlibs

使用-nostartfiles在windows上得到了一个更好的结果。你也可以试试-nodefaultlibs和-nodefaultlibs

compile:

编译:

C:\>gcc -nostartfiles main.c

size:

大小:

C:\>size a.exe
   text    data     bss     dec     hex filename
    488     156      32     676     2a4 a.exe

Remove all the libraries (including the c library) and you get an "executable" with exactly what you've compiled and a bss size of 0:

删除所有库(包括c库),您将得到一个“可执行文件”,其中包含您所编译的内容,bss大小为0:

main.c:

c:

#include <stdio.h>

int _main( int argc, char* argv[] )
{
    return 0;
}

compile:

编译:

C:\>gcc -nostartfiles -nostdlib -nodefaultlibs main.c

size:

大小:

C:\>size a.exe
   text    data     bss     dec     hex filename
     28      20       0      48      30 a.exe

The executable won't run however!

但是可执行文件不会运行!

#1


2  

It's worse on Windows with gcc:

使用gcc时,情况更糟:

main.c:

c:

#include <stdio.h>

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

compile:

编译:

C:\>gcc main.c

size:

大小:

C:\>size a.exe
   text    data     bss     dec     hex filename
   6936    1580    1004    9520    2530 a.exe

bss includes the whole linked executable and in this case various libraries are linked in which do use static c initialisation.

bss包含整个链接的可执行文件,在这种情况下,各种库被链接,其中确实使用了静态c初始化。

Using -nostartfiles gets a much better result on windows. You can also try with -nostdlib and -nodefaultlibs

使用-nostartfiles在windows上得到了一个更好的结果。你也可以试试-nodefaultlibs和-nodefaultlibs

compile:

编译:

C:\>gcc -nostartfiles main.c

size:

大小:

C:\>size a.exe
   text    data     bss     dec     hex filename
    488     156      32     676     2a4 a.exe

Remove all the libraries (including the c library) and you get an "executable" with exactly what you've compiled and a bss size of 0:

删除所有库(包括c库),您将得到一个“可执行文件”,其中包含您所编译的内容,bss大小为0:

main.c:

c:

#include <stdio.h>

int _main( int argc, char* argv[] )
{
    return 0;
}

compile:

编译:

C:\>gcc -nostartfiles -nostdlib -nodefaultlibs main.c

size:

大小:

C:\>size a.exe
   text    data     bss     dec     hex filename
     28      20       0      48      30 a.exe

The executable won't run however!

但是可执行文件不会运行!