加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为什么在BSS和数据段中将不同大小的内存分配给整数?

发布时间:2020-12-15 20:54:57 所属栏目:安全 来源:网络整理
导读:参见英文答案 Why the int type takes up 8 bytes in BSS section but 4 bytes in DATA section????????????????????????????????????1个 请仔细阅读以下程序 – #include stdio.h void main(){} 为每个段分配的内存如下(在Unix上使用size命令) – text data
参见英文答案 > Why the int type takes up 8 bytes in BSS section but 4 bytes in DATA section????????????????????????????????????1个
请仔细阅读以下程序 –

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

为每个段分配的内存如下(在Unix上使用size命令) –

text    data     bss     dec     hex filename
   1040     484      16    1540     604 try

声明全球变量后 –

#include <stdio.h>
int i;

void main()
{
}

为每个段分配的内存如下(在Unix上使用size命令)
变量’i’在BSS中接收到了内存(以前是16,现在是24) –

text    data     bss     dec     hex filename
   1040     484      24    1548     60c try

声明全局变量并将其初始化为10-

#include <stdio.h>
int i=10;

void main()
{
}

为每个段分配的内存如下(在Unix上使用size命令)
变量’i’在数据段中接收到内存(以前是484,现在是488) –

text    data     bss     dec     hex filename
   1040     488      16    1544     608 try

我的问题是为什么全局变量’i’在存储在BSS中时得到大小为8字节的内存,但在存储在数据段中时得到4字节?
为什么在BSS和数据段中为内存分配内存存在差异?

解决方法

why the global variable ‘i’ got the memory of size 8 bytes when it was stored in BSS but got 4 bytes when it was stored in data segment?

首先,为什么数据段中有4个字节?

许多人已经回答了这个问题 – .data段包含任何事先初始化的全局变量或静态变量.一个整数大小为4个字节,当你有全局int i = 10时,它以数据段大小反映;在你的程序中.

现在,为什么.bss段中有8个字节?

您正在观察此行为,因为GNU链接器GNU ld的默认链接描述文件.您可以获取有关链接描述文件here的信息.

在链接时,GNU链接器(GNU ld)使用默认链接描述文件.

默认链接描述文件指定.bss段的对齐方式.

如果要查看默认链接描述文件,可以使用命令执行此操作 –

gcc -Wl,-verbose main.c

此gcc命令的输出将包含以下语句:

using internal linker script:
==================================================
// The content between these two lines is the default linker script
==================================================

在默认的链接描述文件中,您可以找到.bss部分:

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

在这里,你可以看到. = ALIGN(.!= 0?64/8:1);表示默认对齐为8个字节.

该程序:

#include <stdio.h>
int i;

void main()
{
}

当使用默认链接描述文件构建时,由于8字节对齐,’i’在BSS中获得大小为8字节的内存:

# size a.out
   text    data     bss     dec     hex filename
   1040     484      24    1548     60c a.out

[bss = 24字节(16 8)]

GNU链接器提供了一个将您自己的链接描述文件传递给它的规定,在这种情况下,它使用传递给它的脚本来构建目标而不是默认的链接描述文件.

只是为了尝试这个,您可以在文件中复制默认链接描述文件的内容,并使用此命令将链接描述文件传递给GNU ld:

gcc -Xlinker -T my_linker_script main.c

由于您可以拥有自己的链接描述文件,因此可以对其进行更改并查看行为的更改.

在.bss部分中,更改此设置. = ALIGN(.!= 0?64/8:1);至 . = ALIGN(.!= 0?32/8:1);.这会将默认对齐方式从8个字节更改为4个字节.现在使用此更改使用链接描述文件构建目标.

输出是:

# size a.out
   text    data     bss     dec     hex filename
   1040     484      20    1544     608 a.out

在这里你可以看到bss大小是20字节(16 4)因为4字节对齐.

希望这能回答你的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读