Live data from Hacker News

3998-byte executable reduced to 45 bytes

muppetlabs.com

1–10 of 12 posts

Re: 3998-byte executable reduced to 45 bytes

#2
Neat! Optimization is fun. But you can also shrink code by going to higher-level languages... (yes, I know, it won't run as fast and will take a lot more memory when it runs, but still...)

  mbp:~/test doug$ cat tiny.pl
  return 42;

  mbp:~/test doug$ ls -la tiny.pl 
  -rw-r--r--   1 doug  doug  12 Oct 15 11:31 tiny.pl

Re: 3998-byte executable reduced to 45 bytes

#3
post #2

Neat! Optimization is fun. But you can also shrink code by going to higher-level languages... (yes, I know, it won't run as fast and will take a lot more memory when it runs, but still...) mbp:~/test doug$ cat tiny.pl return 42; mbp:~/test doug$ ls -la tiny.pl -rw-r--r-- 1 doug doug 12 Oct 15 11:31 tiny.pl

That's an interesting point, and certainly true in some areas, but having thought about it, I don't think it's fair.

The point of theexercise is to reduce to the smallest bit of program.. Your version should count the size of the interpeter as well, since it's necessary for execution.

Under that model, I could use a program of

1

I'll run this program with my new SwankyCool intrepreter [tm] which returns 42 if it sees a program comprising only "1"; Otherwise it calls perl to exec the rest of the file.

Now, in both cases, we're accepting that the entire OS is already there, but that was stated in his example. (ELF on Linux)

Re: 3998-byte executable reduced to 45 bytes

#4
post #2

Neat! Optimization is fun. But you can also shrink code by going to higher-level languages... (yes, I know, it won't run as fast and will take a lot more memory when it runs, but still...) mbp:~/test doug$ cat tiny.pl return 42; mbp:~/test doug$ ls -la tiny.pl -rw-r--r-- 1 doug doug 12 Oct 15 11:31 tiny.pl

This is an awesome old classic which is required reading for anybody who wants to understand how ELF object files are loaded and executed.

This is pretty far beyond 'optimization'. The executable is 7 bytes smaller than the minimum legal size for the object file header!

Re: 3998-byte executable reduced to 45 bytes

#6
My problem with the article is that it's not really theoretically useful, it's just a case for one particular program.

Writing large programs in C, I rarely see the need to optimise program size - I'm more interested in execution time. Even so, with thousands of lines of code, the executable is typically I think this article is slightly outdated as we don't run 64kb 8-bit machines anymore.

My last comment is that you could use an executable 'packer' which uncompresses at runtime, and that's far easier than his methods.

Re: 3998-byte executable reduced to 45 bytes

#9
post #6

My problem with the article is that it's not really theoretically useful, it's just a case for one particular program. Writing large programs in C, I rarely see the need to optimise program size - I'm more interested in execution time. Even so, with thousands of lines of code, the executable is typically I think this article is slightly outdated as we don't run 64kb 8-bit machines anymore. My last comment is that you…

This article is definitely not intended to teach you how to optimize your programs. That is, unless your usual programs are small enough to embed inside object file headers with the opcodes overlapping header field values.

It's a clever hack that teaches you about how ELF works by forcing you to think carefully about every single byte in a customized ELF executable.

I work frequently with object file formats (especially ELF) and write tools which manipulate executables and libraries and when I first read this article about 5 years ago I learned more about ELF than from any other source.

Highly recommended!

Post reply on HN