Live data from Hacker News

Main is usually a function – when is it not?

jroweboy.github.io

21–30 of 62 posts

Re: Main is usually a function – when is it not?

#21
post #15
post #6

Earlier quoted context omitted.

I think that sets the address of main to NULL, so it segfaults as soon as `_start` jumps to `main`. It's known for being one of the smallest compilable C programs. EDIT: Your original post contained int main=0;

Not quite - what matters (normally) is the address of the symbol, not the bytes located there, since in the case of a real function those bytes would be the instructions. So this will either execute the bytes at &main as instructions (4 zero bytes, and whatever follows), or, more likely, crash due to memory protections, as described in the article.

Yup. You can get a working program with the simple:

  const int main = 0xC3;
...which is just a return. Or you can get fancy and make it exit successfully by clearing eax first:

  const int main = 0xC3C031;

Re: Main is usually a function – when is it not?

#22
I'm impressed with the authors efforts, and share much of his pain when it comes to doing weird things with C.

For other obfuscated fun, see this poem/tree printer in 505 bytes[0], or this program:

    main(G){10>5*G):10)&&main(2+G);}
[0] - https://github.com/lelandbatey/tiny_tree_printer

Re: Main is usually a function – when is it not?

#23
post #14

Not working on Mac, but I'm gonna tweet it anyway :)

I spent a bit getting it to work on OS X. The ASM becomes:

    movq $0x2000004, %rax  ; // BSD syscalls are divided into classes.
    movq $1, %rdi          ; // 64-bit registers use %rdi instead of %ebx
    lea message(%rip), %rsi; // Relative Address of message
    movq $13, %rdx         ; // Same Length
    syscall                ; // x86-64 ASM syscall

    movq $0x2000001, %rax  ; // Exit Syscall
    movq $0, %rdi;
    syscall

    message: .ascii "Hello World!\n";

Re: Main is usually a function – when is it not?

#26

> My problem solving process is typically the same thing I imagine most programmers do. I'm afraid I may be a victim of Poe's law here, but this attitude is... distressing. It completely discounts the value of problem solving, which, once upon a time, was the entire point of the profession.

"Problem solving" is perhaps the wrong phrase here. It's more like "information gathering", which is an important step in problem solving.

Re: Main is usually a function – when is it not?

#27

In python it is still a function but it is also an object?

Well more properly python doesn't have a "main" function. You just directly execute the initial script, which will import other modules, define functions and classes, etc, and finally (hopefully) execute something.

Re: Main is usually a function – when is it not?

#28
post #14

Not working on Mac, but I'm gonna tweet it anyway :)

  echo 'const int main[]={3850979413,184,3234285568,33554436,29869896,1207959552,1652109,3343384576,3522,1208291072,114887,3343385088,199,1208291072,1869376613,1919899424,169960556};'>t.c&&gcc t.c&&./a.out
But it is too long to tweet

[Edit]

Less compact but more readable:

  echo 'const int main[] = { 3850979413, 184, 3234285568, 33554436, 29869896, 1207959552, 1652109, 3343384576, 3522, 1208291072, 114887, 3343385088, 199, 1208291072, 1869376613, 1919899424, 169960556};' > test.c && gcc -Wall test.c -o test && ./test

Re: Main is usually a function – when is it not?

#29
post #7

Reminds me of this 'Hello World': http://stackoverflow.com/questions/15182496/why-does-this-co... I bet these two ideas would go well together.

Sadly the libc prng is not specified, and thus non-portable. It might work if things are checked on the same kernel and libc combination, or it might not. The Java PRNG is specified and globally portable.
Post reply on HN