0

I write a nothing.c, which is just one line as follows

int main(){}

Then I compile it using command gcc nothing.c -o nothing

Here's what I get using command readelf -x .text nothing

Hex dump of section '.text':

  0x00001040 f30f1efa 31ed4989 d15e4889 e24883e4 ....1.I..^H..H..
  0x00001050 f050544c 8d055601 0000488d 0ddf0000 .PTL..V...H.....
  0x00001060 00488d3d c1000000 ff15722f 0000f490 .H.=......r/....
  0x00001070 488d3d99 2f000048 8d05922f 00004839 H.=./..H.../..H9
  0x00001080 f8741548 8b054e2f 00004885 c07409ff .t.H..N/..H..t..
  0x00001090 e00f1f80 00000000 c30f1f80 00000000 ................
  0x000010a0 488d3d69 2f000048 8d35622f 00004829 H.=i/..H.5b/..H)
  0x000010b0 fe4889f0 48c1ee3f 48c1f803 4801c648 .H..H..?H...H..H
  0x000010c0 d1fe7414 488b0525 2f000048 85c07408 ..t.H..%/..H..t.
  0x000010d0 ffe0660f 1f440000 c30f1f80 00000000 ..f..D..........
  0x000010e0 f30f1efa 803d252f 00000075 2b554883 .....=%/...u+UH.
  0x000010f0 3d022f00 00004889 e5740c48 8b3d062f =./...H..t.H.=./
  0x00001100 0000e829 ffffffe8 64ffffff c605fd2e ...)....d.......
  0x00001110 0000015d c30f1f00 c30f1f80 00000000 ...]............
  0x00001120 f30f1efa e977ffff fff30f1e fa554889 .....w.......UH.
  0x00001130 e5b80000 00005dc3 0f1f8400 00000000 ......].........
  0x00001140 f30f1efa 41574c8d 3da32c00 00415649 ....AWL.=.,..AVI
  0x00001150 89d64155 4989f541 544189fc 55488d2d ..AUI..ATA..UH.-
  0x00001160 942c0000 534c29fd 4883ec08 e88ffeff .,..SL).H.......
  0x00001170 ff48c1fd 03741f31 db0f1f80 00000000 .H...t.1........
  0x00001180 4c89f24c 89ee4489 e741ff14 df4883c3 L..L..D..A...H..
  0x00001190 014839dd 75ea4883 c4085b5d 415c415d .H9.u.H...[]A\A]
  0x000011a0 415e415f c366662e 0f1f8400 00000000 A^A_.ff.........
  0x000011b0 f30f1efa c3                         .....

So what does it do?

Tanix
  • 3
  • 2
  • Mainly library functions such as `init`. `init` is called by the OS, it sets up file descriptors like stdout, stderr and stdin and then calls main. You can disassemble the binary with `objdump -d nothing`. – Martin Fink Oct 25 '21 at 06:53
  • The startup code also prepares `argv` and `argc`. – the busybee Oct 25 '21 at 07:06
  • related: https://stackoverflow.com/questions/1315926/gcc-empty-program-23202-bytes – bolov Oct 25 '21 at 17:27
  • Interestingly, you can actually compile a valid .c file with zero characters. You'll need to do a little linker abuse though. https://oxasploits.com/posts/shortest-valid-c-quine-compiler-specifications-linker-magic/ – oxagast Feb 25 '23 at 05:11

2 Answers2

0

So what does it do?

You can see what it does:

objdump -d nothing

Disassembly of section .text:

0000000000001040 <_start>:
    1040:       31 ed                   xor    %ebp,%ebp
    1042:       49 89 d1                mov    %rdx,%r9
    1045:       5e                      pop    %rsi
    1046:       48 89 e2                mov    %rsp,%rdx
    1049:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    104d:       50                      push   %rax
    104e:       54                      push   %rsp
    104f:       4c 8d 05 3a 01 00 00    lea    0x13a(%rip),%r8        # 1190 <__libc_csu_fini>
    1056:       48 8d 0d d3 00 00 00    lea    0xd3(%rip),%rcx        # 1130 <__libc_csu_init>
    105d:       48 8d 3d c1 00 00 00    lea    0xc1(%rip),%rdi        # 1125 <main>
    1064:       ff 15 76 2f 00 00       call   *0x2f76(%rip)        # 3fe0 <__libc_start_main@GLIBC_2.2.5>
    106a:       f4                      hlt
    106b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)

0000000000001070 <deregister_tm_clones>:
    1070:       48 8d 3d b1 2f 00 00    lea    0x2fb1(%rip),%rdi        # 4028 <__TMC_END__>
    1077:       48 8d 05 aa 2f 00 00    lea    0x2faa(%rip),%rax        # 4028 <__TMC_END__>
... etc.
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
-1

The compiler injects info when you are compiling the source code. This highly depends on the operating system and the compiler you are using. For example, on a macOS, the compiler injects the so-called 'unwind info' which does something with unwinding the stack when there is an exception.

To get to know what the compiler injects in your .text file besides the empty main, you should generate a .map file in which you will see clearly what's going on. The next question will be why the compiler injects this extra section?

To generate a .map file use the following command:

gcc -Wl,-map,nothing.map nothing.c -o nothing
Alaa Mahran
  • 663
  • 4
  • 12