Thursday, March 6, 2008

How do i reduce the size of my executable?

One of my firends asked this question to me over email.I thought it would be great to answer it here so that it will help many others with similar queries.
The size of an executable depends on a lot of factors like the compiler,linker & libraries that you use,the operating system on which these tools run,the format of the binary(say exe,dll or PE (Portable Executable) in general for windows,ELF for *nix etc).Most compilers provide you "compiler switches" which helps you to specify a lot of settings like even howmuch debugging info you want to keep in the final executable. The structure of PE & ELF binaries are described here Now let us deal with some scenareos
1.Imagine you want an "easy solution" to compress the executable with out worrying much about the internal structure of an executable. Great,have a look UPX (Ultimate Packer for eXecutables).A great opensource tool avilable at http://upx.sourceforge.net/ .It helps to compress many executable formats.(works with windows,linux etc).Just download the utility & run it along with your executable name and approptiate switches.I will give you an example below. Assume you have a 32 bit exe called myprog.exe on a windows machine & you have UPX,run it as shown below C:\Jobin\Personal\setups\upx302w\upx302w>upx- 9 -o Compressed.exe myprog.exe
Ultimate Packer for eXecutables
Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007
UPX 3.02w Markus Oberhumer, Laszlo Molnar & John Reiser Dec 16th 2007
File size Ratio Format Name
-------------------- ------ ----------- -----------
2720039 -> 2565927 94.33% win32/pe Compressed.exe
Packed 1 file.

2. Imagine your compiler is VC++ 6.0, and you want to use appropriate compiler switches and libraries to get your executable size less.
Well, this is the programmers way...great to know you are interested in reading this.If you remember those good old days when you worked on Turbo C/C++, you had something called as memory models(Tiny,Small,Medium etc).Also think about those 64K segments(CS,DS etc)...those near, far & huge pointers??? Remembering them?If you remember them, then you know how to play around with the compiler switches & memory models. otherwise let me take you to a crisp document which explains it step by step. Just have a look at http://www.hailstorm.net/papers/smallwin32.htm#smallapps

3.I want to still reduce the size of my executable verymuch,can you tell me any other way?
Well,As i mentioned earlier,your executable size is dependent on your compiler,linker,libraries etc.so to have the least size,you need to stay more closer to the CPU :-P
Imagine you wanted to print out the letter "A" from your program..you write the code as below
#include "stdio.h"
int main(void)
{
printf("A");
return 0;
}
Compile it & look at its size..huge??? that is exactly what I was telling you
insted,go to your command prompt and do as shown below
H:\>debug-a 100
138C:0100 mov ah,02
138C:0102 mov dl,41
138C:0104 int 21
138C:0106 int 20
138C:0108
-g
A
Program terminated normally
-r cx
CX 0000:08
-n out.com
-w
Writing 00008 bytes
-q
H:\>dir *.com
Volume in drive H is users Volume Serial Number is 0002-6F60
Directory of H:\
03/07/2008 12:32 PM 8 OUT.COM
1 File(s) 8 bytes 0 Dir(s) 881,819,648 bytes free
H:\>out
A
H:\>
Got it? Just in 8 bytes you printed out "A"...gr8!!! so if you want smaller sized exe,write your code in Assembly. I am sure it can be a debatable suggestion .You may ask me who the hell writes code in asm today??? all I have to say is that any someone might ,depending on their needs. If you decided to try your hands on assembly, you may find NASM handy and open source .Have more queries???shoot a mail to me or post a comment,i will be happy to help you out..have a good day!!!

No comments: