Monday, October 12, 2009

Large executables for GCC built on Windows (cygwin/MinGW) - how to reduce the size?

Yes, the large executables for GCC built on Windows (cygwin/MinGW) can be reduced.

First we see why such a large executable?

Generally the size of executables built on Windows (cygwin/MinGW) would be larger than 20 MB (this metric is derived by experimenting with the build of GCC compiler for IA64). This is because of the presence of symbol information for debugging.

If the executable built is not for debugging, then the symbol information shall be stripped-off from the executable.

Strip-off symbol information from executable

The symbol information can be stripped-off from the executable (built on either cygwin or MinGW) by any of the following ways:

  1. Strip utility from binutils
  2. Set the environment variable "CFLAGS" with '-s'

1. Strip utility of binutils

The strip utility of binutils removes symbols from object file. The symbol information from the executable can be stripped-off as follows:
strip cc1.exe

Note: Assume the executable from which the symbol information is to be removed is "cc1.exe".

2. Set the environment variable "CFLAGS" with '-s'

The environment variable "CFLAGS" should be set with '-s' before the build of executable on cygwin/MinGW.

I.e. The environment variable CFLAGS can be set in the shell as follows:
export CFLAGS="-s"

Note: To check whether the enovironment variable CFLAGS is set or not, use the shell command "env". The shell command "env" would list of environment variables.

After stripping off the symbol information, the size of the executable would be reduced to ~7 MB (this metric is derived by experimenting with the build of GCC compiler for IA64).

Executable Compression

The size of the executable can be reduced further by executable compression. Refer executable compression.

The executable compression can be done using the executable packer UPX. UPX is a free and open source executable packer. For details on UPX, refer upx.sourceforge.net.

The executable compression can be done as follows (in windows command prompt):
upx cc1.exe

Note: Assume the executable to be compressed is "cc1.exe".

After compressing the executable, the executable size would be reduced to ~2 MB (this metric is derived by experimenting with the build of GCC compiler for IA64).

Thursday, September 17, 2009

Default boot in dual boot systems

Most of us have dual boot on PCs/laptops. One for linux OS and the other for Windows OS.

The default boot would be linux OS. I.e. While the system is switched on and no selection on OS to boot is made, the boot done would be the linux OS.

And if anyone wants to work on Windows and he/she forgot to make the selection, the system might end-up in booting the linux OS.

Some people may mostly work on windows and they have linux for their least purpose. For them it would be better if the default boot is Windows.

So how to change it?

Simple! The dual boot is done by the GRUB (GRand Unified Bootloader). A change in GRUB is needed to employ this change. To do the change in GRUB first boot the linux OS on the system. Open the file "/boot/grub/menu.lst". At line 14 of file "/boot/grub/menu.lst", change the entry number of the "default" entry with the number corresponding to the entry for Windows OS. The entry list can be seen at the end of the file "/boot/grub/menu.lst".

An Example:

Before modification, the file "/boot/grub/menu.lst" was:

default 0
...
...
...
title Ubuntu 8.10, kernel 2.6.27-11-generic
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-11-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro quiet splash
initrd /initrd.img-2.6.27-11-generic
quiet

title Ubuntu 8.10, kernel 2.6.27-11-generic (recovery mode)
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-11-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro single
initrd /initrd.img-2.6.27-11-generic

title Ubuntu 8.10, kernel 2.6.27-7-generic
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-7-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro quiet splash
initrd /initrd.img-2.6.27-7-generic
quiet

title Ubuntu 8.10, kernel 2.6.27-7-generic (recovery mode)
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-7-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro single
initrd /initrd.img-2.6.27-7-generic

title Ubuntu 8.10, memtest86+
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /memtest86+.bin
quiet

### END DEBIAN AUTOMAGIC KERNELS LIST

# This entry automatically added by the Debian installer for a non-linux OS
# on /dev/sda1
title Microsoft Windows XP Professional
root (hd0,0)
savedefault
makeactive
chainloader +1

Here the entry number of "default" entry is '0'. The entry number '0' corresponds to first entry (i.e. Ubuntu 8.10, kernel 2.6.27-11-generic) of the OS list at the bottom of the file "/boot/grub/menu.lst". So by default the linux OS Ubuntu will get booted.

Now to change the default boot to Windows OS, the entry number of "default" entry should be changed to '5'. The entry number '5' corresponds to the fifth entry (i.e. Microsoft Windows XP Professional") of the OS list at the bottom of the file "/boot/grub/menu.lst".

After modification, the file "/boot/grub/menu.lst" is as follows:

default 5
...
...
...
title Ubuntu 8.10, kernel 2.6.27-11-generic
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-11-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro quiet splash
initrd /initrd.img-2.6.27-11-generic
quiet

title Ubuntu 8.10, kernel 2.6.27-11-generic (recovery mode)
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-11-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro single
initrd /initrd.img-2.6.27-11-generic

title Ubuntu 8.10, kernel 2.6.27-7-generic
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-7-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro quiet splash
initrd /initrd.img-2.6.27-7-generic
quiet

title Ubuntu 8.10, kernel 2.6.27-7-generic (recovery mode)
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /vmlinuz-2.6.27-7-generic root=UUID=7d85ba3f-2c1d-4f1e-b30d-6d1df00ac7a3 ro single
initrd /initrd.img-2.6.27-7-generic

title Ubuntu 8.10, memtest86+
uuid 17d035fe-5a14-49b6-887e-1a3ccd553e32
kernel /memtest86+.bin
quiet

### END DEBIAN AUTOMAGIC KERNELS LIST

# This entry automatically added by the Debian installer for a non-linux OS
# on /dev/sda1
title Microsoft Windows XP Professional
root (hd0,0)
savedefault
makeactive
chainloader +1

Now the default boot is Windows OS.

GCC build on Windows

GCC relies on POSIX (Portable Operating System Interface) functionality which is not available in Windows natively. Hence for building GCC on windows, a linux-like environment for Windows is used. The linux-like environment for windows is brought via:

  1. cygwin (or)
  2. MinGW (Minimalist GNU for Windows)

Both cygwin and MinGW output windows executable ('.exe' file). But the executable built on cygwin has the cygwin DLL dependency (I.e. The GCC compiler executable to run, the required cygwin DLLs should be made available to the executable). Whereas the executable built on MinGW has no such dependency.