-5

I found the way to do it on quora but I can not understand it.

You can achieve this using opcodes.....

const int main[] = {  -443987883, 440, 113408, -1922629632,  4149, 899584, 84869120, 15544,  266023168, 1818576901, 1461743468, 1684828783,  -1017312735};
Now follow below steps....
Save that in hello.c
then gcc -o hello hello.c
then ./hello
Hola..... Hello World!

Op-codes will be specific for specific hardware and controller based on architecture of it. If I understood correctly then op-codes will be different for different hardware and platforms. So this is not a generic solution. However I tried to compiler it on IDEone & it gives run time error. Is there any generic solution?

Similar question on SE

Community
  • 1
  • 1
kapilddit
  • 1,729
  • 4
  • 26
  • 51

1 Answers1

5

In C, the main function is the entrypoint into your program. In the program binary, the machine code into which the function is compile will get placed in the .text section of the binary as well as the location of the main function in the symbol table.

In the above code we create a constant array with the same main symbol name. Since the array is constant, this will also get placed in the .text section of the binary. And since any code in the .text section is executable, there is nothing to stop the opcodes in the main array getting executed.

doron
  • 27,972
  • 12
  • 65
  • 103