C程序中.bss段的模糊行为 [英] Ambiguous behaviour of .bss segment in C program

查看:269
本文介绍了C程序中.bss段的模糊行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

并执行以下命令来了解.bss段的大小更改。

  gcc test.c -o test 
size test

输出结果如下: -

 文本数据bss十进制十六进制文件名
1115 552 8 1675 68b test

我没有声明全局或静态范围。所以请解释为什么bss段的大小是8字节。



我做了以下更改: - $ / $>

 #包括< stdio.h中> 
int x; //声明的全局变量
int main()
{
return 0;
}

但令我吃惊的是,输出与之前一样: -

 文本数据bss十进制十六进制文件名
1115 552 8 1675 68b测试

请解释。
然后我初始化全局: -

$ p $ #include< stdio.h>
int x = 67; //初始化全局变量
int main()
{
return 0;
}

数据段的大小如预期般增加,但我没有预料到大小的bss段减少到4(相反,当没有声明时为8)。请解释。

 文本数据bss十进制十六进制文件名
1115 556 4 1675 68b测试

我也尝试了objdump和nm命令,但它们也显示了变量x占用.bss(在第二种情况下)。然而,在size命令中没有显示bss大小的变化。



我按照以下步骤执行:
http://codingfox.com/10-7-memory-segments-code-data-bss/
其中输出如期完美。

解决方案

当您编译一个简单的 main 程序你也链接启动代码。
此代码负责初始化bss。



该代码是使用8字节的代码,您在.bss部分看到的代码。



您可以使用 - nostartfiles gcc选项:


-nostartfiles



链接时不要使用标准系统启动文件。除非使用-nostdlib或-nodefaultlibs,否则通常会使用标准系统库。

要进行测试,请使用以下代码

 #include< stdio.h> 

int _start()
{
return 0;
}

并用

编译

  gcc -nostartfiles test.c 

你会看到。 bss设为0

 文本数据bss十进制十六进制文件名
206 224 0 430 1ae测试
code>


I wrote the simple C program (test.c) below:-

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

and executed the follwing to understand size changes in .bss segment.

gcc test.c -o test
size test

The output came out as:-

   text    data     bss     dec     hex filename
   1115     552       8    1675     68b test

I didn't declare anything globally or of static scope. So please explain why the bss segment size is of 8 bytes.

I made the following change:-

#include<stdio.h>
int x;    //declared global variable
int main()
{
   return 0;
}

But to my surprise, the output was same as previous:-

   text    data     bss     dec     hex filename
   1115     552       8    1675     68b test

Please explain. I then initialized the global:-

#include<stdio.h>
int x=67;    //initialized global variable
int main()
{
   return 0;
}

The data segment size increased as expected, but I didn't expect the size of bss segment to reduce to 4 (on the contrary to 8 when nothing was declared). Please explain.

text       data     bss     dec     hex filename
1115        556       4    1675     68b test

I also tried the comands objdump, and nm, but they too showed variable x occupying .bss (in 2nd case). However, no change in bss size is shown upon size command.

I followed the procedure according to: http://codingfox.com/10-7-memory-segments-code-data-bss/ where the outputs are coming perfectly as expected.

解决方案

When you compile a simple main program you are also linking startup code. This code is responsible, among other things, to init bss.

That code is the code that "uses" 8 bytes you are seeing in .bss section.

You can strip that code using -nostartfiles gcc option:

-nostartfiles

Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib or -nodefaultlibs is used

To make a test use the following code

#include<stdio.h>

int _start()
{
   return 0;
}

and compile it with

gcc -nostartfiles test.c

Youll see .bss set to 0

   text    data     bss     dec     hex filename
    206     224       0     430     1ae test

这篇关于C程序中.bss段的模糊行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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