Why BSS segment is “16” by default?

泄露秘密 提交于 2019-12-01 18:25:59

It's worse on Windows with gcc:

main.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.

Using -nostartfiles gets a much better result on windows. You can also try with -nostdlib and -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:

main.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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!