21
4
Is it possible to write (pack) a shorter than 145 bytes version of a program with "Hello world" (plus new line) output if the length of the program is measured as a number of bytes in program's ELF (x86) representation? Reduction technique in mind is described here:
Please reply with providing a code example. Note, 1 says "Hi world" and [2] returns 42 instead of an output, hence I assume current solution to be 142 + (len("Hello") - len("Hi")) which is 145.
[EDIT]
Limits of ELF: syntactically a program translated into ELF is determined by its interpreter (libc), which is a combination of:
- Target architecture (Generic, AMD64, ARM, IA-32, MIPS, etc.)
- OS kernel (which communicates with implementation of ELF interpreter and often is itself packed into an ELF binary)
From TIS spec. 1.2
There is one valid program interpreter for programs conforming to the ELF specification for the Intel architecture: /usr/lib/libc.so.1
In theory - all clear - multiple combinations times version differences (Arch, OS) are possible. In practice - because standard is less strict than any BNF for CFG or other finer (smaller) formal language - there can be implementation differences (options) including shorter length of the program.
On one hand, because ELF is not that precise (one naturally expects) it is much more difficult to do code-golf with it. Hence a "Hello world\n" program is expected to be by default in TIS ELF 1.2, x86 (Generic Intel), Linux 2.6.(20+). On the other hand having a shorter ELF that runs e.g. on *BSD seems like an extremely valuable knowledge to me!
For example ELF64 (latest draft) is much more interesting incl. the differences with ELF(x86). So, please share a solution that can be accepted with correction to specific configuration.
Motivation: It is the practical part that is interesting for me and code-golf is a very nice way to show how tricky it is to come up with any machine- (or even byte) code in general and why ELF does apparently such a good job (in my understanding). Thus a golf solution is not only cool per say but also can provide a practical knowledge of unexpected interpretation differences of ELF (if an alternative combination is given).
1
It's trivial to modify Adam Rosenfield's 116-byte ELF file to include a newline, giving a 117-byte executable that prints "Hello world\n" in a Linux terminal window. The byte-sequence is
– r.e.s. – 2012-05-03T02:41:10.2077F 45 4C 46 01 01 01 00 00 00 00 00 00 00 00 00 02 00 03 00 01 00 00 00 54 80 04 08 34 00 00 00 00 00 00 00 00 00 00 00 34 00 20 00 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 80 04 08 00 80 04 08 74 00 00 00 74 00 00 00 05 00 00 00 00 10 00 00 B0 04 31 DB 43 B9 69 80 04 08 31 D2 B2 0C CD 80 31 C0 40 CD 80 48 65 6C 6C 6F 20 77 6F 72 6C 64 0A
.Thanks for pointing out the duplication. I was not aware of it. @FUZxxl What is the problem of having a more precisely asked question? I see the redundancy with the previous question but I don't like the way it is asked at all, e.g. it says nothing about code-golf. If "less than 20 byte" is just an olympic puzzle then there is no duplication per say. Another matter - are you really an opinion that simply closing creates a better Q&A knowledge base? I have not even found "less than 20 byte" question. IMHO wikipedia achieves something similar with redirects - much more constructive. – Yauhen Yakimovich – 2012-05-03T08:59:53.363
1If it is not obvious enough - my current question is about "studying" ELF only which has normally longer header due to lot of metadata. E.g. going via asm code solution does here no good. Coming up with a new shorter execution format special designed for this question is also not accepted. It's about limits of ELF. Thus modification of Adam Rosenfield's 116-byte ELF file is the best answer so far. – Yauhen Yakimovich – 2012-05-03T09:07:13.150
1The scope of the question seems basically limited to x86 machines running an operating system that uses the ELF standard. Would that be virtually all x86 non-Windows machines? (BTW, I find that the executable mentioned in my previous comment -- just copy/paste/save the binary using a hex editor -- runs successfully on my x86-64 under both 32- and 64-bit versions of Ubuntu. Isn't that unexpected?) – r.e.s. – 2012-05-03T14:10:49.413
Since we typically have tags for language-specific questions, I went and created an [tag:executable-binary] tag for this question. I decided not to create [tag:x86] or [tag:elf] yet -- let's see if we get more questions like this first. – Ilmari Karonen – 2012-05-03T15:01:58.620
@FUZxxl: Duplicate of an SO question doesn't really count, does it? I wouldn't search SO before asking a question, too. – user unknown – 2012-05-04T01:48:59.060
@r.e.s. Linux for amd64 has code that can run programs compiled for x86, provided that all libraries the program needs are available for x86 as well. Actually, you can even use the old
int $0x80
method to call the operating system - even from amd64 code! – FUZxxl – 2012-05-04T06:11:06.023Another issue for the OP: I think the question should make it clear whether the desired "shortest ELF" executable binary is required to adhere to the ELF standard. (The cited tutorial admittedly violates it eventually, whereas the posting linked in my "answer" claims adherence to it.) – r.e.s. – 2012-05-04T12:15:11.643
ELF (x86) files are not compatible between operating systems. NetBSD might require a longer solution than Linux. – kernigh – 2012-05-04T17:20:29.297
Sorry, for not being precise enough after all @r.e.s. I hope new correction is much clearer. Asking code-golf about binary executable can be valid and reasonable but only if expected configuration is clearly stated (by OP or AP). – Yauhen Yakimovich – 2012-05-05T15:50:41.623
@Mego Hi Mego, I am not sure it if mine question is a duplicate. It was asked in 2012 not in 2015. But you have to figure it out yourself. It is really not about comparing programming languages in general. It is a very concrete task requiring knowledge of the machine code i.e. ELF dialect. I would not accept solution in ruby or perl. So no it is not really a duplicate. – Yauhen Yakimovich – 2016-12-20T10:43:56.363