免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1961 | 回复: 5
打印 上一主题 下一主题

怎么理解APUE2里对BSS的描述? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-06 23:09 |只看该作者 |倒序浏览
Note from Figure 7.6 that the contents of the uninitialized data segment are not stored in the program file on disk. This is because the kernel sets it to 0 before the program starts running. The only portions of the program that need to be saved in the program file are the text segment and the initialized data.

The size(1) command reports the sizes (in bytes) of the text, data, and bss segments. For example:

    $ size /usr/bin/cc /bin/sh
       text     data   bss     dec     hex   filename
      79606     1536   916   82058   1408a   /usr/bin/cc
     619234    21120 18260  658614   a0cb6   /bin/sh



The fourth and fifth columns are the total of the three sizes, displayed in decimal and hexadecimal, respectively.

========================================
我怎么觉得晕晕的,例子里的两个bin文件中不是有bss段吗?

论坛徽章:
0
2 [报告]
发表于 2007-06-06 23:18 |只看该作者
想明白了,size给的是空间的大小,APUE里说的应该是这段空间的内容并没有放在磁盘文件中。


====
[xxxxx@xx tmp]$ cat helloworld.c
#include <stdio.h>
int tmp[1000]={0};
int
main ()
{
  //int i;
  printf ("Hello world.\n");
  return 0;
}
[xxxxx@xx tmp]$ make helloworld   
cc     helloworld.c   -o helloworld
[xxxxx@xx tmp]$ size helloworld
   text    data     bss     dec     hex filename
    831     256    [red]4032[/red]    5119    13ff helloworld
========
编译器把赋成0初值的全局变量放到BSS里了?

论坛徽章:
0
3 [报告]
发表于 2007-06-06 23:22 |只看该作者
还有为啥多出来的是4032-4=4028?怎么不是1000*4?

注:4是没加"int tmp[1000]={0};"时的bss size。

[xxxxx@xx tmp]$ cat helloworld.c
#include <stdio.h>
//int tmp[1000]={0};
int
main ()
{
  //int i;
  printf ("Hello world.\n");
  return 0;
}
[xxxxx@xx tmp]$ make helloworld  
cc     helloworld.c   -o helloworld
[xxxxx@xx tmp]$ size helloworld  
   text    data     bss     dec     hex filename
    831     256       4    1091     443 helloworld

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2007-06-06 23:24 |只看该作者
原帖由 mjdcl 于 2007-6-6 23:18 发表
想明白了,size给的是空间的大小,APUE里说的应该是这段空间的内容并没有放在磁盘文件中。


====
[xxxxx@xx tmp]$ cat helloworld.c
#include <stdio.h>
int tmp[1000]={0};
int
main ()
{
  / ...

不一定是全局变量,但肯定是和其有关的一些信息,例如需要占用多少空间等等。

论坛徽章:
0
5 [报告]
发表于 2007-06-06 23:58 |只看该作者

  1. /**
  2. * 前两个是初使化了的数据
  3. * 后两个则是未初使化的数据,对static的数据也是一样.
  4. * 初始化的数据被存放在数据段,未初始化的则被放在bss.
  5. */
  6. char buffer[] = "This is a buffer";
  7. int array[] = {1, 3, 5, 7, 9};
  8. char info[256];
  9. int values[4];

  10. int main()
  11. {
  12.         info[0] = '1';
  13.         values[1] = 4;
  14.         return 0;
  15. }
复制代码


用mingw, gcc -S getSize.c 产生的汇编代码

  1.         .file        "getSize.c"
  2. .globl _buffer
  3.         .data          # 这里就是全局初始化了的数据
  4. _buffer:
  5.         .ascii "This is a buffer\0"
  6. .globl _array
  7.         .align 4
  8. _array:
  9.         .long        1
  10.         .long        3
  11.         .long        5
  12.         .long        7
  13.         .long        9
  14.         .def        ___main;        .scl        2;        .type        32;        .endef
  15.         .text
  16. .globl _main
  17.         .def        _main;        .scl        2;        .type        32;        .endef
  18. _main:
  19.         pushl        %ebp
  20.         movl        %esp, %ebp
  21.         subl        $8, %esp
  22.         andl        $-16, %esp
  23.         movl        $0, %eax
  24.         addl        $15, %eax
  25.         addl        $15, %eax
  26.         shrl        $4, %eax
  27.         sall        $4, %eax
  28.         movl        %eax, -4(%ebp)
  29.         movl        -4(%ebp), %eax
  30.         call        __alloca
  31.         call        ___main
  32.         movb        $49, _info
  33.         movl        $4, _values+4
  34.         movl        $0, %eax
  35.         leave
  36.         ret
  37.         .comm        _info, 256         # 256         这里就是未初始化的数据.
  38.         .comm        _values, 16         # 16           这里的256和16表示的应当是数据对齐,你info gas确认一下
复制代码


初始化的数据占据空间,而对于未初始化的数据,则只有等到程序开始执行之后,对其初始化为0

至于多出来的应当时考虑数据对齐后的结果吧.

[ 本帖最后由 coldwarm 于 2007-6-6 23:59 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-06-07 01:09 |只看该作者
你的编译器是什么? behavior 不太对,f应该在data段。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP