Size Is Everything: Making Tiny ELF Binaries
Milk Toast writes "According to this article one can start with a simple program consisting of 3998 bytes and reduce it down to a mere 45 bytes. Now if they could only reduce the size of my Office install." It involves digging into assembly, naturally, but it's interesting to see the extra code generated not only by the compiler, but by the other steps along the way.
I first heard about teeny ELF here.
and they want their "news" back.
Should ELF binaries not normally be really small?
Man, I've been reading Slashdot way too long.
(It's still a good article though. Worth rereading if you're at all interested in how ELF binaries work.)
As I recall, that's been on slashdot, and several posts already have mentioned this. Quite a few years ago. In fact, I think there was a Linux virus plus howto that linked to this on how to make small binaries to travel across networks with. Interesting read, and quite amusing to fit an entire virus inside an ELF header.
I Browse at +4 Flamebait
Open Source Sysadmin
AAAAa aaagh...get out of heeeere! you duplicate!</Mya-Rudolph-Impersonating-Donatella-Ve rsace>
It's 10 PM. Do you know if you're un-American?
It runs and it is only 8 bytes! ./tiny; echo $?
froze@butsuri:~$ cat > tiny
exit 42
froze@butsuri:~$ chmod a+x tiny
froze@butsuri:~$
42
froze@butsuri:~$ wc -c tiny
8 tiny
-- The morphemes of your disquisition are ascertainable, but they have eschewed an ambit of transpicuous exposition.
Erm, i'm not sure its nearly 5 in the morning here but initially he compiles with
gcc -Wall -s -O3 tiny.c
shouldn't
gcc -Wall -s -Os tiny.c
make more sense seeing as how -O(1-3) are for SPEED optimization where as -Os is to make things SMALL
pls. Expose my idiocy...
There is absolutely no point at all to this article. He started with a 3998 byte program, and ended with a 45 byte one. The kicker is, when the OS loads the program into ram, it'll stick both in a 4096 byte page! All he accomplished was to make that page mostly full of zero-fill padding!
Since nobody is actually going to waste their time doing this for programs large enough that the padding doesn't kill the gain, what's the point?
A deep unwavering belief is a sure sign you're missing something...
How far we've come. An advanced civilization took a computer the size of a city and millions of years to do this, and now we can come up with the same answer in only 45 bytes.
-- 'The' Lord and Master Bitman On High, Master Of All
... at how he describes the resemblence in the ELF and program headers? Or am I really that much of a geek? =P
He who laughs last is stuck in a time dilation bubble.
Back in 2002, after having perused the TinyELF article, I got into my Linux IA-32 assembly craze (which was an offshoot of my IA-32 OSDEV craze, lol).
.string "Hello Linux!\n"
Now I didn't bother with messing with ELF headers - but just wanted to create the smallest possible binary output -
Here is a cat-like program.
---------->
.text
.globl _start
_start:
incl %eax
doit:
incl %eax
incl %eax
pushl %esp
popl %ecx
xorl %edx, %edx
incl %edx
int $0x80
cmpl $0,%eax
je exit
incl %eax
incl %eax
incl %eax
incl %ebx
pushl %esp
popl %ecx
int $0x80
xorl %ebx, %ebx
jmp doit
exit:
incl %eax
int $0x80
---------->
Here is a Hello World type program
---------->
.global _start
_start:
pushl $13
popl %edx
pushl $msg
popl %ecx
incl %ebx
pushl $1
pushl $4
go: popl %eax
int $0x80
jmp go
msg:
---------->
it produces the number 42, using 45 bytes of code, jackass.
-- 'The' Lord and Master Bitman On High, Master Of All
my wife keeps reassuring me that size isn't important!
because it'd save me a lot of spamassassin CPU cycles.