在BSS和数据段整型变量大小 [英] integer variable size in bss and data segment

查看:313
本文介绍了在BSS和数据段整型变量大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是测试程序在Linux 6.3理解I2C存储器模型籽粒版本2.6.32-279.el6.x86_64。

I am using a test program for understanding C memory model on linux 6.3 with kernal version 2.6.32-279.el6.x86_64 .

首先,我有以下编译code,

First i have compile below code,

#include <stdio.h>
int main(void)
{
    static int i = 100; /* Initialized static variable stored in DS*/
    return 0;
}

上运行size命令,下面我得到的,

on running size command , i got below ,

[root@rachitjain jan14]# size a.out
   text    data     bss     dec     hex filename
   1040     488      16    1544     608 a.out

那么,消除对静态变量'我'的intialization后,我的code而成,

then, after removing the intialization for static variable 'i' , my code becomes ,

include <stdio.h>
int main(void)
{
    static int i ;
    return 0;
}

在上面编译运行后的大小上,

On running size on after compiling above ,

[root@rachitjain jan14]# size a.out
   text    data     bss     dec     hex filename
   1040     484      24    1548     60c a.out

有在bss段8字节增量但只有4个字节中的数据部分减少。为什么大小是整数得到翻倍,而移动到BSS段?

There is 8 byte increment in bss section but only 4 bytes are reduced in the data section. Why the size is integer in getting doubled while moving to bss segment ?

我已经测试过这个角色,浮为好,观察到了同样的行为。

I have tested this character and float as well , observed the same behavioral.

推荐答案

看,答案是.bss段有一个要求64位要对齐和.data没有这个要求。

Look, the answer is that the .bss section has a requirement to be aligned on 64 bits and the .data does not have this requirement.

你怎么看?当你建立你的程序的LD,以建立你的程序使用一个默认的链接脚本。你可以看到,如果添加轮候册,-verbose:

How can you see this? When you build your program ld uses a default linker script in order to build your program. You can see it if you add -Wl,-verbose:

g++ main.cpp -Wl,-verbose

在建64位的一个应用,这是对.bss段默认aligment:

When you build 64 bit applicaton this is a default aligment for .bss section:

  .bss            :
  {
   *(.dynbss)
   *(.bss .bss.* .gnu.linkonce.b.*)
   *(COMMON)
   /* Align here to ensure that the .bss section occupies space up to
      _end.  Align after .bss to ensure correct alignment even if the
      .bss section disappears because there are no input sections.
      FIXME: Why do we need it? When there is no .bss section, we don't
      pad the .data section.  */
   . = ALIGN(. != 0 ? 64 / 8 : 1);
  }

(!= 0八分之六十四?:1)

正如你所看到对齐; 告诉对齐到8字节

As you can see ALIGN(. != 0 ? 64 / 8 : 1); tells to align to 8 bytes

在建的32位应用(G ++ -m32的main.cpp轮候册,-verbose)这是.bss段默认aligment:

When you build 32 bit applicaton (g++ -m32 main.cpp -Wl,-verbose) this is a default aligment for .bss section:

  .bss            :
  {
   *(.dynbss)
   *(.bss .bss.* .gnu.linkonce.b.*)
   *(COMMON)
   /* Align here to ensure that the .bss section occupies space up to
      _end.  Align after .bss to ensure correct alignment even if the
      .bss section disappears because there are no input sections.
      FIXME: Why do we need it? When there is no .bss section, we don't
      pad the .data section.  */
   . = ALIGN(. != 0 ? 32 / 8 : 1);
  }

您.data段显然没有任何ALIGN默认链接脚本命令:

Your .data section obviously does not have any ALIGN commands in the default linker script:

  .data           :
  {
    *(.data .data.* .gnu.linkonce.d.*)
    SORT(CONSTRUCTORS)
  }

相关链接:

  • http://sourceware.org/ml/binutils/2009-05/msg00174.html
  • http://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/expressions.html)
  • http://lwn.net/Articles/531148/
  • ALIGN in Linker Scripts

这篇关于在BSS和数据段整型变量大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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