
A Magnetized Needle and a Steady Hand - omnibrain
http://nullprogram.com/blog/2016/11/17/
======
userbinator
It's interesting to compare with DOS, where a do-nothing program is a single
byte:

    
    
        C3
    

A Hello World is roughly 20 bytes, the bulk of it being the string itself:

    
    
        95 BA 07 01 CD 21 C3 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 24
    

The ELF in the article, which is 130 by my count, could be reduced to 45 with
some tricks:

[http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...](http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html)

Also worth looking at is what the demoscene has done with binaries of 128
bytes or less:

[http://www.pouet.net/prodlist.php?type%5B%5D=32b&type%5B%5D=...](http://www.pouet.net/prodlist.php?type%5B%5D=32b&type%5B%5D=64b&type%5B%5D=128b&page=1)

~~~
joeyh

        joey@darkstar:~>touch true
        joey@darkstar:~>chmod +x true
        joey@darkstar:~>ls -l true
        -rwxr-xr-x 1 joey joey 0 Nov 18 10:53 true*
        joey@darkstar:~>if ./true; then echo yay; fi
        yay
    

Just saying.

~~~
aexaey
Also, you don't actually need a "true" binary for a shell of any reasonable
vintage:

    
    
      # which true
      /usr/bin/which: no true in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
    
      # true; echo $?
      0

------
tomcam
This article uses the ingenious technique of accreting a minimal executable
file via the Unix echo command, which can indeed write binary output using
C-style escape codes. A must for anyone interested in compiler writing because
it does a decent job explaining the ELF format in terms of the data structures
required ton hold one.

------
lutusp
From a modern perspective it's fun to think about the layers of sometimes-
trivial complexity that we fly over with modern development tools, but when I
wrote Apple Writer in 1978, I first had to write an assembler, and to do that,
I first had to enter seemingly endless amounts of object code to get to the
tipping point where I could enter comparatively efficient 6502 assembly-
language mnemonics and actually get something accomplished.

[https://en.wikipedia.org/wiki/Apple_Writer](https://en.wikipedia.org/wiki/Apple_Writer)

------
corecoder
Very nice; tried it and got a 129 byte working true command. Now I have to
understand what's in the 28896 bytes of the true command that comes with the
distro I have here.

~~~
oblio
GNU true:
[http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/true...](http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/true.c)

The real kicker is GNU false:
[http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/fals...](http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/false.c)

A sort of "reversed polarity", if you will.

~~~
LeifCarrotson
So it's basically

    
    
        int
        main (int argc, char **argv)
        {
          return EXIT_SUCCESS;
        }
    

plus all the stuff required to handle --version and --help.

But even compiling just that results in 60 kB on my system. What's in those
other kilobytes, indeed!

~~~
ktRolster
I don't know how you compiled it, but you can often cut down by using the
'-O3' command line option when you are compiling. Then you can use the 'strip
a.out' (or whatever your executable name is) to cut it down further.

Then if you open it in a hex editor (or use od -x), I'll bet you'll see pages
and pages of zeroes, which are used for aligning it appropriately in RAM when
it loads.

At least, those are all things that work on some systems, can't be sure about
yours.

~~~
aexaey
There is also an _sstrip_ tool [1], written by the same Brian Raiter already
mentioned here. It strips a bit more eagerly:

    
    
      $ gcc tiny.c -O3 -o tiny; wc -c tiny
      8456 tiny
    
      $ strip tiny; wc -c tiny
      6224 tiny
    
      $ sstrip tiny; wc -c tiny
      4140 tiny
    
    

[1]
[http://www.muppetlabs.com/~breadbox/software/elfkickers.html](http://www.muppetlabs.com/~breadbox/software/elfkickers.html)

------
pjc50
A long, long time ago I actually did this, in order to sneak a small exploit
in a COM file onto the school's pre-internet Novell Netware system.

I also had to rebuild a partition table by hand once with only a copy of MSDOS
debug on a boot floppy, and Peter Norton's excellent book on the PC.

------
minikites
I don't remember enough of the details to find a link, but I recall reading a
story where some system error destroyed the binaries for most of the usual
command line tools like cat and the author couldn't restart or rebuild the
server so they hand entered the hex (or something) for enough tools to
bootstrap themselves into a running system again.

~~~
_lm_
[http://www.ee.ryerson.ca/~elf/hack/recovery.html](http://www.ee.ryerson.ca/~elf/hack/recovery.html)

(Note that 'gnu' in the story is GNU Emacs!)

~~~
minikites
Thank you, this is such a great story.

------
TeMPOraL
The background story is a fun thought experiment and obviously it requires
some suspension of disbelief...

...but in the best HN tradition, let me poke some holes into it ;).

C-based technology we are all used to has some set of particular paradigms,
one of which being that programs are "write-only". You code, compile and ship
the binary. It wasn't the case with many pre-C technologies though, and it
isn't the case now.

So, looking just at the software I have on my PC right now, I wonder what this
potent computer virus would do to all the programs I wrote in Common Lisp -
they all ship a native compiler within them, that's _accessible in runtime_.
Moreover, it's often _essential in runtime_ for the program to work.

So I guess, if that virus came and wiped all the dev tools, the C world would
die, while we'd be looking for any compiled Lisp application and working to
break into the REPL ;).

~~~
khedoros1
It strikes me that a native compiler, accessible at runtime, would fit the
definition of "software development tool", so either the entire program, or
the portion contains the compiler, would be deleted.

Another challenging aspect is that just about every command shell can execute
a scripting language, so they'd be included too. I mean, it's just a repl with
easy access to the filesystem, right?

------
dmd
Ah, but what if the man page for elf has been sneakily changed by your
superhuman-intelligent adversary?

(cf. A Fire Upon the Deep, Virnor Vinge)

~~~
jedimastert
I think if there was a massive attack that infected a trusting trust attack on
every computer on the planet, we'd just have to start from scratch

~~~
pavel_lishin
Super-scratch. We'd have to trash every processor manufacturing plant on the
planet; Every single one almost certainly uses computer-controlled machinery
working from computer-stored plans.

This would be a fun novel to read. An alien intelligence that can only act at
a distance infects our computer systems, and we have to recover. A sort of
post-apocalyptic scenario, except without the magic "engines don't work, but
matches do" flavor. I hope Neal Stephenson is bored and looking for something
to write.

~~~
TeMPOraL
> _post-apocalyptic scenario, except without the magic "engines don't work,
> but matches do" flavor_

I don't know if you're referring to "Revolutions" TV series here, but I've
seen exactly those accusations of "magic" leveled against the show in the
past, on HN. I decided to watch it anyway, and...

(spoiler alert)

...by the half of the first season it's becoming slowly revealed that there's
no _magic_ that happens to disable only electronics and combustion engines -
it's omnipresent advanced nanotech _explicitly designed_ to do just that.

~~~
kazagistar
Actually, I instantly thought of Dies The Fire, a novel with the same concept.

------
agumonkey
once again, kragen marathon comes to mind
[https://www.reddit.com/r/programming/comments/9x15g/programm...](https://www.reddit.com/r/programming/comments/9x15g/programming_thought_experiment_stuck_in_a_room/c0ewj2c/)

~~~
CGamesPlay
This is really cool. Has anybody gameified this?

------
mirimir
There are analogous discussions about open-source hardware. How far back must
one go to be assured that there are no backdoors? It's a hard problem.

------
placebo
This reminds me a bit of a thought I had regarding how long would it take
civilisation to return to it's current level of technology if we'd all be
magically transferred back to the stone age retaining only our memories.

~~~
zentiggr
Generation one would be almost completely occupied finding the most efficient
way to archive as much of that knowledge as possible, IMO... I'd imagine it
might take a dozen generations or so, or more, depending on how much could be
archived before that first generation passed away and took that experience
with it. I see apprenticeships rampant!

~~~
burkaman
This is basically the plot of the Foundation series.

------
ksherlock
$ touch true; chmod 777 true; ./true && echo Success

Success

~~~
ben_bai
Congrats: Your shell just spawned another shell to run an empty script.

Also:

    
    
        # rm /bin/true && true && echo success
        success

------
saurik
The storyline backing this article is not just distracting, it is
misinformative: it teaches people the wrong indirect lessons about software
and engineering :/. If you don't have an assembler, you write an assembler,
you don't just make do without and try to bootstrap directly from a C
compiler; and if you have the luxury of extra hands and need people to work on
stuff in parallel--stuff you can't run yet anyway as if you don't have really
simple things like true coded I bet you don't have a shell (and there are only
a scant couple reasons true would even exist if you don't have a shell that
exposes branch comparisons as shell commands)--you don't set them to work
building the binaries by hand, you give them a rough outline of a programming
language you will have available at the right point in the development cycle
(maybe you throw them a C89 manual, as manuals still seem to exist in this
world?) and tell them to write it in that on the promise that it will be
useful later.

~~~
aji
it's obviously just a fun thought experiment.

and we'd probably dust off the old PDP7s and stuff that are more suited to
this kind of bootstrapping, instead of jumping straight to ELF and x86

------
krallja
`echo` is a shell builtin on my machine. In that case, we certainly have
access to an interpreter (the shell), which means we should have access to
other interpreters. Why not define `true` as

    
    
       #!/bin/sh
       exit 0
    

? Similarly, why not use `sh` or one of those other interpreters to write the
rest of the utilities? `cc` probably isn't that important after all. `chmod`
and `sh` are!

~~~
pricechild
This article isn't really about solving a silly problem with hands, arms &
tongue tied behind your back. It's about learning a little bit more about the
layers which put our systems together.

I don't think the author was seriously suggesting you use butterflies or a
steady hand either.

------
lugus35
I don't understand when Chris Wellons find the time to write all those fine
blog posts.

------
Beltiras
Ye gods. Github was intermittently inaccessible the other day and ruined about
half an hour of my time. World-wide EMP leaving all HDDs in ruins. Well, at
least it would keep us occupied for a decade rebuilding everything .....

------
hga
tl;dr: inspired in part by [http://xkcd.com/378/](http://xkcd.com/378/) on
writing the "true" program in machine code (that is, hex AKA binary bits).
Using the echo program....

