Compressing Mac OS X executables using UPX

UPX stands for Ultimate Packer for eXecutables and you can use it to compress many executable formats including Mac OS X fat binaries. UPX will create a new version of your executable that will self extract itself at runtime.

Things to remember when considering production code


UPX changes your executable. otool no longer produces the output you would expect and running the executable under GDB is completely un-symbolized (although you can get around this by attaching to the binary after it's been uncompressed in memory).

The crash reports created by Mac OS X on executables produced by UPX are almost empty - they only contain the registry dump because Mac OS X is unable to resolve the backtraces on the application threads.

Compiling UCL



UPX depends on UCL (portable lossless data compression library). Download the latest UCL version (ucl-1.03.tar.gz) from http://www.oberhumer.com/opensource/ucl/download/ and untar it.

Compile UCL using the usual ./configure, make sequence.

Compiling UPX



Download the source package from upx.sourceforge.net.

To compile upx, you need to tell it where the UCL library is stored (unless you're installed it).
cristi:upx-3.07-src diciu$ cd src/
export UPX_UCLDIR=/Users/diciu/Downloads/ucl-1.03
make


Once its compiled you can use it to compress any executable:

cristi:src diciu$ ./upx.out /tmp/LoudHush
Ultimate Packer for eXecutables
Copyright (C) 1996 - 2010
UPX 3.07 Markus Oberhumer, Laszlo Molnar & John Reiser Sep 08th 2010

File size Ratio Format Name
-------------------- ------ ----------- -----------
584560 -> 216576 37.05% Mach/fat LoudHush

Packed 1 file.


PS. If you get a NotCompressibleException it may be that your executable is too small and cannot be compressed (the size of the compressed binary exceeds the size of the original binary).