我正在运行一个小测试,同时试图理解 larger problem .这是我的测试环境:
head.h:
#define MAX_BUFSIZE 500
typedef struct {
int head;
int tail;
int status;
int active;
void * dev[MAX_BUFSIZE];
char free[MAX_BUFSIZE];
int count;
} msg_fifo_t;
extern msg_fifo_t TxBufx[];
extern msg_fifo_t Rx_Buf[];
测试.c:
#include <stdio.h>
#include "head.h"
//msg_fifo_t TxBufx[10]; // This is the important line
int main(int argc, char * argv[])
{
// This part isn't really important...
printf("Hello Test\n");
return 0;
}
所以我使用这些文件并运行了三个测试来查看我得到的尺寸 -
测试 #1(代码如上):
> gcc -Os test.c
> ls -al a.out
-rwxrwxr-x 1 mike mike 7158 Jan 17 11:13 a.out
> size a.out
text data bss dec hex filename
1170 256 8 1434 59a a.out
测试 #2(取消注释“重要”行):
> gcc -Os test.c
> ls -al a.out
-rwxrwxr-x 1 mike mike 7181 Jan 17 11:14 a.out
> size a.out
text data bss dec hex filename
1170 256 25208 26634 680a a.out
测试 #3(取消注释“重要”行并将 TxBufx
大小更改为 100)
> gcc -Os test.c
> ls -al a.out
-rwxrwxr-x 1 mike mike 7181 Jan 17 11:14 a.out
> size a.out
text data bss dec hex filename
1170 256 252008 253434 3ddfa a.out
那么现在我的问题是:
bss 大小似乎与可执行文件的“大小”几乎没有关系(如
ls -al
命令所报告)- 谁能向我解释为什么会这样?该特征是否特定于编译器/链接器/或平台?
有没有比
size
更好的工具来理解这里发生的事情? (意思是什么真正构成了我的可执行文件的 7181 个字节?)
最佳答案
bss
段中的数据量对可执行文件在磁盘上的大小没有影响,因为 bss
段是什么——这是您的程序用于变量初始化为零。由于该部分的内容是事先已知的(全为零),因此实际存储在可执行文件中的唯一内容是该区域的大小。
因此,做随着代码的变化而变化的是 data
段的大小——表示静态初始化为非零值的变量,以及code
段,代表你的程序对应的编译后的可执行指令。
至于要使用的工具,大多数 Unix 系统上的 objdump(1) 实用程序(它是 GNU 工具链的一部分)或 MacOS X 上的 otool(1) 实用程序都可以用来获取有关哪些部分制作的更多详细信息启动您的可执行文件,以及每个文件中的符号。
关于c - 哪些部分构成可执行文件的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14384034/