Hacker News new | ask | show | jobs
by anshargal 1317 days ago
I had to add

  #define _GNU_SOURCE
  #include <sys/mman.h>
  #include <unistd.h>
I've tested in Fabrice Bellard JSLinux with tcc (x86 arch) and on https://replit.com/languages/c (x64). I failed to see any side effect at all. gdb "catch syscall" doesn't show anything interesting too. Looks like TINY_ELF_PROGRAM is not doing anything.
1 comments

you can disassemble the code portion, they are single-byte instructions

    push 0x2a
    pop edi
    push 0x3c
    pop eax
    db 0x0f, 0x05 ; invalid?

    ; set the first syscall argument to 42
    push   0x2a
    pop    edi

    ; select syscall 60 (sys_exit)
    push   0x3c
    pop    eax

    ; sys_exit(42)
    syscall
LOL my asm is rusty, didn't even know about the syscall instruction (I'd have used int 0x80 here)
What's the point of `push`ing constants to the stack and `pop`ing them to registers instead of `mov`ing them directly?
I think they encode smaller?
That's cool - I wonder why exit code is not set to 42 in practice? binary still returns 0, must be a bug somewhere.
A successful run not exiting with EXIT_SUCCESS usually requires a good reason to justify it.