为什么未对可执行文件中的填充字节进行优化? [英] Why aren't padding bytes in executables optimized?

查看:57
本文介绍了为什么未对可执行文件中的填充字节进行优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个小型Mac Mach-O可执行文件时,"Hello World"例如,用C语言编写,由于对齐填充,大小约为48k.但是,与此同时,我可以在.bss部分中定义100k的0-es(例如,通过指定100k个字符的静态数组),并且它仅将几个字节添加到磁盘上的可执行文件中.

When I create a small Mac Mach-O executable, "Hello World" written in C for example, it's about 48k in size, because of alignment padding. Yet at the same time I can define 100k of zero-es in the .bss section (for example by specifying a static array of 100k chars) and it's adding just a few bytes to my executable on disk.

我想知道为什么padding(我认为应该在加载后才起作用)也不被打包".不知何故.

I wonder why the padding(, which only plays a role after being loaded, I suppose) is not also "packed" somehow.

推荐答案

您似乎不欣赏padding的作用:确保对象的大小满足约束,例如为特定数字的倍数.填充字节的通常并不重要,但是必不可少.

You seem not to appreciate the role of padding: to ensure that the size of an object satisfies a constraint, such as being a multiple of a specific number. The values of padding bytes typically are not significant, but their presence is indispensable.

分配给ELF(例如).bss节的对象的情况恰恰相反:它们的值很重要,但是它们的大小不需要通过磁盘上的表示来反映.此外,此类对象的值仅是通过将它们分配给.bss来确定的,因此,就内存中表示的大小而言,每个对象实际上都在二进制文件中占用O(1)空间.

Exactly the opposite is the case for objects assigned to an ELF (say) .bss section: their values are important, but their sizes do not need to be reflected by their on-disk representations. Moreover, the values of such objects are established simply by the fact that they are assigned to .bss, so indeed, each one consumes O(1) space in the binary with respect to the size of its in-memory representation.

附录:可以想象可以设计一种二进制格式来承载压缩数据,从而使此类填充确实在磁盘上被压缩.人们只能推测为什么这种格式不受欢迎,但是我认为可以肯定地猜测,至少部分答案是未压缩二进制格式的简单性和加载速度比相对少量的节省空间更有价值.压缩格式即可实现.

Addendum: it is conceivable that a binary format could be designed that carried compressed data, so that such padding indeed was compressed on disk. One can only speculate about why such formats are not popular, but I think it's pretty safe to guess that at least part of the answer is that the simplicity and loading speed of uncompressed binary formats are more valued than the comparatively small amounts of space saving that compressed formats could achieve.

这篇关于为什么未对可执行文件中的填充字节进行优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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