减少GCC目标EXE代码大小? [英] Reducing GCC target EXE code size?
问题描述
当我编译一个无操作程序时:
int main(void)
{
返回0;
使用各种编译器:
GCC(类似于LLVM的结果):给出一个10-KiB可执行文件(编译时使用 -s
) >
$ b
-
部分:
.CRT
,.bss
,.data
,.idata
,.rdata
,.text
,.tls
-
取决于
msvcrt.dll
和kernel32.dll
MSVC 2010:提供了一个5.5 KiB可执行文件(用 / MD / Ox
p>
-
部分:
.data
,.rdata
,.reloc
,.text
-
取决于
msvcr100.dll
和kernel32.dll
-
可以通过合并
.rdata
和.text
Windows Driver Kit 7.1:提供6.5 KiB可执行文件(使用 / MD / Ox
,与 msvcrt_winxp.obj
链接以允许它在XP上运行)
-
部分:
.data
,.rdata
,.text
-
取决于
msvcrt.dll
和kernel32.dll
可以通过合并 - $ c $>
.text
.rdata $ c进一步减少
Windows 2003 Driver Development Kit:提供3.5 KiB可执行文件
-
部分:
.data
,.rdata
,.text
-
取决于
msvcrt.dll
-
合并
.rdata
与.text
-
部分:
.data
,.text
-
取决于
msvcrt.dll
所以我想这个问题很简单:
是否有可能进一步减少GCC或LLVM的目标可执行文件大小,以便它们是更接近裸露的最小可能,同时仍然链接到 msvcrt.dll
?
(编辑:我是显然不是找像 UPX
等打包机)。
这并不是一件特别有意义的事情。它可能会消除一些事情,但只要你有一个实际上做了什么的程序,那么它会再次将这些东西拉回来。
例如,在另一个平台上(我没有做太多Windows操作),程序的最小尺寸比你想象的要大,因为每个程序都有一个atexit处理程序来清理。该处理程序有一个可能的错误情况,这意味着它拉入printf和所有I / O的东西。 Atexit本身也吸引了malloc和所有的内存交付的东西。毫无疑问,除此之外还有其他一些位。最终结果是一个400KB的静态二进制大小。这在一个没有操作的程序中很烦人,但实际上所有的程序都需要这个东西,所以这是一个有争议的问题。
一般来说,如果你想最小化程序大小,使用 -Os
进行编译,并尝试使用 -flto
或 -fwhole-program
(但这最后需要对构建过程进行大量更改)。另外,不要使用 -g
,并且去掉最后的二进制文件(如果不能打破这些文件的话)。
When I compiled a no-op program:
int main(void)
{
return 0;
}
with various compilers:
GCC (similar result to LLVM as well): Gave a 10-KiB executable (compiled with
-s
)Sections:
.CRT
,.bss
,.data
,.idata
,.rdata
,.text
,.tls
Depends on
msvcrt.dll
andkernel32.dll
MSVC 2010: Gave a 5.5 KiB executable (compiled with
/MD /Ox
)Sections:
.data
,.rdata
,.reloc
,.text
Depends on
msvcr100.dll
andkernel32.dll
Could have been further reduced by merging
.rdata
with.text
Windows Driver Kit 7.1: Gave a 6.5 KiB executable (compiled with
/MD /Ox
, linked withmsvcrt_winxp.obj
to allow it to run on XP)Sections:
.data
,.rdata
,.text
Depends on
msvcrt.dll
andkernel32.dll
Could have been further reduced by merging
.rdata
with.text
Windows 2003 Driver Development Kit: Gave a 3.5 KiB executable
Sections:
.data
,.rdata
,.text
Depends on
msvcrt.dll
Could have been further reduced by merging
.rdata
with.text
Tiny C Compiler (TCC): Gave a 1.5 KiB executable
Sections:
.data
,.text
Depends on
msvcrt.dll
So I guess the question is simple:
Is it possible to further reduce GCC or LLVM's target executable sizes so that they are closer to the bare-minimum possible, while still linking to msvcrt.dll
?
(Edit: I'm obviously not looking for packers like UPX
, etc.)
This isn't a particularly meaningful thing to do. It might be possible to eliminate a few things, but as soon as you have a program that actually does anything then it'll pull those things straight back in again.
For example, on another platform (I don't do much Windows stuff), the minimum size for a program is larger than you would think because every program has an atexit handler to clean up. That handler has a possible error case which means it pull in printf and all the I/O stuff. Atexit itself also pulls in malloc and all the memory handing stuff. And no doubt there's a few other bits besides. The end result is a 400KB static binary size. That's annoying in a no-op program, but in reality all programs would need this stuff, so it's a moot point.
In general, if you want to minimise program size, compile with -Os
, and try to use -flto
or -fwhole-program
(but this last will need lots of changes to your build procedure). Also, don't use -g
, and do strip the final binaries (if that doesn't break them).
这篇关于减少GCC目标EXE代码大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!