为什么BSS段" 16 QUOT;默认? [英] Why BSS segment is "16" by default?

查看:164
本文介绍了为什么BSS段" 16 QUOT;默认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按我的知识,对于分割C程序是:

As per my knowledge, segmentation for c program is:

        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

在我的RHEL 5.4的64位计算机,对于下面的C程序

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

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

当我做的:

# 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

由于我不声明/初始化的全局/静态瓦尔?

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

推荐答案

这是与海湾合作委员会在Windows糟糕的是:

It's worse on Windows with gcc:

main.c中:

#include <stdio.h>

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

编译:

C:\>gcc main.c

尺寸:

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

BSS包括整个链接的可执行并在这种情况下,各个库被链接在其中确实使用静态C初始化

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

使用 -nostartfiles 得到窗户上一个更好的结果。您也可以尝试用-nostdlib和-nodefaultlibs

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

编译:

C:\>gcc -nostartfiles main.c

尺寸:

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

删除所有的库(包括C库),你会得到一个可执行文件与什么你编译和0 BSS尺寸:

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;
}

编译:

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

尺寸:

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

该可执行文件不会跑不过!

The executable won't run however!

这篇关于为什么BSS段&QUOT; 16 QUOT;默认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆